Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#40228] added more file link representer specs

Eric Schubert0bcd28b6a038
diff --git a/modules/storages/app/services/storages/storage_url_service.rb b/modules/storages/app/services/storages/storage_url_service.rb
index 1caec3f16e9..500ffce1f90 100644
--- a/modules/storages/app/services/storages/storage_url_service.rb
+++ b/modules/storages/app/services/storages/storage_url_service.rb
@@ -39,7 +39,7 @@ module Storages
def call(action)
return ServiceResult.new(success: false, message: 'invalid action type') unless ACTION_TYPES.include?(action)
- ServiceResult.new(success: true, result: "#{@file_link.storage.host}f/#{@file_link.origin_id}")
+ ServiceResult.new(success: true, result: "#{@file_link.storage.host}/f/#{@file_link.origin_id}")
end
end
end
diff --git a/modules/storages/lib/api/v3/file_links/file_link_representer.rb b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
index 09de1058d12..64876346c85 100644
--- a/modules/storages/lib/api/v3/file_links/file_link_representer.rb
+++ b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
@@ -32,6 +32,7 @@ module API
class FileLinkRepresenter < ::API::Decorators::Single
include API::Decorators::LinkedResource
include API::Decorators::DateProperty
+ include ::API::Caching::CachedRepresenter
property :id
@@ -50,12 +51,11 @@ module API
link :self do
{
- href: api_v3_paths.file_link(represented.container_id, represented.id),
- method: :get
+ href: api_v3_paths.file_link(represented.container_id, represented.id)
}
end
- link :delete do
+ link :delete, cache_if: -> { user_allowed_to_manage?(represented) } do
{
href: api_v3_paths.file_link(represented.container_id, represented.id),
method: :delete
@@ -65,28 +65,19 @@ module API
link :creator do
{
href: api_v3_paths.user(represented.creator_id),
- method: :get
+ title: represented.creator.name
}
end
- link :staticDownloadLocation do
+ link :originOpen do
{
- href: api_v3_paths.file_link_download(represented.container_id, represented.id),
- method: :get
+ href: ::Storages::StorageUrlService.new(represented).call('open').result
}
end
- link :openLocation do
+ link :staticOriginOpen do
{
- href: ::Storages::StorageUrlService.new(represented).call('open').result,
- method: :get
- }
- end
-
- link :staticOpenLocation do
- {
- href: api_v3_paths.file_link_open(represented.container_id, represented.id),
- method: :get
+ href: api_v3_paths.file_link_open(represented.container_id, represented.id)
}
end
@@ -96,8 +87,7 @@ module API
skip_render: ->(*) { true },
getter: ->(*) {},
setter: ->(fragment:, **) {
- # TODO: canonize url correctly
- canonical_url = fragment['href']
+ canonical_url = fragment['href'].gsub(/\/+$/, '')
represented.storage = ::Storages::Storage.find_by(host: canonical_url)
}
@@ -111,6 +101,10 @@ module API
private
+ def user_allowed_to_manage?(model)
+ current_user.allowed_to?(:manage_file_links, model.container.project)
+ end
+
def make_origin_data(model)
{
id: model.origin_id,
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index fab28eb2909..ae9c122d781 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -76,10 +76,6 @@ module OpenProject::Storages
"#{work_package(work_package_id)}/file_links/#{file_link_id}"
end
- add_api_path :file_link_download do |work_package_id, file_link_id|
- "#{work_package(work_package_id)}/file_links/#{file_link_id}/download"
- end
-
add_api_path :file_link_open do |work_package_id, file_link_id|
"#{work_package(work_package_id)}/file_links/#{file_link_id}/open"
end
diff --git a/modules/storages/spec/lib/api/v3/file_links/file_link_representer_parsing_spec.rb b/modules/storages/spec/lib/api/v3/file_links/file_link_representer_parsing_spec.rb
index aee13cf4315..24c31558482 100644
--- a/modules/storages/spec/lib/api/v3/file_links/file_link_representer_parsing_spec.rb
+++ b/modules/storages/spec/lib/api/v3/file_links/file_link_representer_parsing_spec.rb
@@ -1,6 +1,6 @@
#-- copyright
# OpenProject is an open source project management software.
-# Copyright (C) 2012-2021 the OpenProject GmbH
+# Copyright (C) 2012-2022 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
@@ -29,9 +29,18 @@
require 'spec_helper'
describe ::API::V3::FileLinks::FileLinkRepresenter, 'parsing' do
+ include ::API::V3::Utilities::PathHelper
+
let(:file_link) { build_stubbed(:file_link) }
+ let(:storage) { build_stubbed(:storage) }
+
+ let(:current_user) { build_stubbed(:user) }
- current_user { build_stubbed(:user) }
+ before do
+ allow(::Storages::Storage).to receive(:find_by)
+ .with(host: storage.host)
+ .and_return storage
+ end
describe 'parsing' do
subject(:parsed) { representer.from_hash parsed_hash }
@@ -42,7 +51,11 @@ describe ::API::V3::FileLinks::FileLinkRepresenter, 'parsing' do
let(:parsed_hash) do
{
- "_type" => "FileLink",
+ "_links" => {
+ "storageUrl" => {
+ "href" => storage.host
+ }
+ },
"originData" => {
"id" => 5503,
"name" => "logo.png",
@@ -64,24 +77,55 @@ describe ::API::V3::FileLinks::FileLinkRepresenter, 'parsing' do
end
it 'are not used by the parsing' do
- expect(parsed).not_to have_attributes(
- created_at: DateTime.parse(parsed_hash["createdAt"]).in_time_zone,
- updated_at: DateTime.parse(parsed_hash["updatedAt"]).in_time_zone
- )
+ expect(parsed).not_to have_attributes(created_at: DateTime.parse(parsed_hash["createdAt"]).in_time_zone,
+ updated_at: DateTime.parse(parsed_hash["updatedAt"]).in_time_zone)
+ end
+ end
+
+ describe 'storage' do
+ context 'if storage url is given with trailing slashes' do
+ let(:parsed_hash) do
+ {
+ "_links" => {
+ "storageUrl" => {
+ "href" => "#{storage.host}/////"
+ }
+ }
+ }
+ end
+
+ it 'is parsed correctly' do
+ expect(parsed).to have_attributes(storage_id: storage.id)
+ end
+ end
+
+ context 'if storage is given as resource' do
+ let(:parsed_hash) do
+ {
+ "_links" => {
+ "storage" => {
+ "href" => api_v3_paths.storage(storage.id)
+ }
+ }
+ }
+ end
+
+ it 'is parsed correctly' do
+ expect(parsed).to have_attributes(storage_id: storage.id)
+ end
end
end
describe 'originData' do
it 'is parsed correctly' do
- expect(parsed).to have_attributes(
- origin_id: "5503",
- origin_name: "logo.png",
- origin_mime_type: "image/png",
- origin_created_by_name: "Luke Skywalker",
- origin_last_modified_by_name: "Anakin Skywalker",
- origin_created_at: DateTime.new(2021, 12, 19, 9, 42, 10.17, '+00:00').in_time_zone,
- origin_updated_at: DateTime.new(2021, 12, 20, 14, 0, 13.987, '+00:00').in_time_zone
- )
+ expect(parsed).to have_attributes(storage_id: storage.id,
+ origin_id: "5503",
+ origin_name: "logo.png",
+ origin_mime_type: "image/png",
+ origin_created_by_name: "Luke Skywalker",
+ origin_last_modified_by_name: "Anakin Skywalker",
+ origin_created_at: DateTime.new(2021, 12, 19, 9, 42, 10.17, '+00:00').in_time_zone,
+ origin_updated_at: DateTime.new(2021, 12, 20, 14, 0, 13.987, '+00:00').in_time_zone)
end
end
end
diff --git a/modules/storages/spec/lib/api/v3/file_links/file_link_representer_rendering_spec.rb b/modules/storages/spec/lib/api/v3/file_links/file_link_representer_rendering_spec.rb
new file mode 100644
index 00000000000..65520984b58
--- /dev/null
+++ b/modules/storages/spec/lib/api/v3/file_links/file_link_representer_rendering_spec.rb
@@ -0,0 +1,151 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe ::API::V3::FileLinks::FileLinkRepresenter, 'rendering' do
+ include ::API::V3::Utilities::PathHelper
+
+ let(:storage) { build_stubbed(:storage) }
+ let(:container) { build_stubbed(:work_package) }
+ let(:creator) { build_stubbed(:user, firstname: 'Rey', lastname: 'Palpatine') }
+ let(:file_link) { build_stubbed(:file_link, storage: storage, container: container, creator: creator) }
+ let(:user) { build_stubbed(:user) }
+ let(:representer) { described_class.new(file_link, current_user: user) }
+
+ subject(:generated) { representer.to_json }
+
+ before do
+ allow(::Storages::StorageUrlService).to receive(:find).and_return creator
+ end
+
+ describe '_links' do
+ context 'self' do
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'self' }
+ let(:href) { "/api/v3/work_packages/#{container.id}/file_links/#{file_link.id}" }
+ end
+ end
+
+ context 'storage' do
+ it_behaves_like 'has a titled link' do
+ let(:link) { 'storage' }
+ let(:href) { "/api/v3/storages/#{storage.id}" }
+ let(:title) { storage.name }
+ end
+ end
+
+ context 'container' do
+ it_behaves_like 'has a titled link' do
+ let(:link) { 'container' }
+ let(:href) { "/api/v3/work_packages/#{container.id}" }
+ let(:title) { container.name }
+ end
+ end
+
+ context 'creator' do
+ it_behaves_like 'has a titled link' do
+ let(:link) { 'creator' }
+ let(:href) { "/api/v3/users/#{creator.id}" }
+ let(:title) { creator.name }
+ end
+ end
+
+ context 'delete' do
+ let(:permission) { :manage_file_links }
+
+ it_behaves_like 'has an untitled action link' do
+ let(:link) { 'delete' }
+ let(:href) { "/api/v3/work_packages/#{container.id}/file_links/#{file_link.id}" }
+ let(:method) { :delete }
+ end
+ end
+
+ context 'originOpen' do
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'originOpen' }
+ let(:href) { "#{storage.host}/f/#{file_link.origin_id}" }
+ end
+ end
+
+ context 'staticOriginOpen' do
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'staticOriginOpen' }
+ let(:href) { "/api/v3/work_packages/#{container.id}/file_links/#{file_link.id}/open" }
+ end
+ end
+ end
+
+ describe 'properties' do
+ it_behaves_like 'property', :_type do
+ let(:value) { 'FileLink' }
+ end
+
+ it_behaves_like 'property', :id do
+ let(:value) { file_link.id }
+ end
+
+ it_behaves_like 'datetime property', :createdAt do
+ let(:value) { file_link.created_at }
+ end
+
+ it_behaves_like 'datetime property', :updatedAt do
+ let(:value) { file_link.updated_at }
+ end
+
+ context 'originData' do
+ it_behaves_like 'property', 'originData/id' do
+ let(:value) { file_link.origin_id }
+ end
+
+ it_behaves_like 'property', 'originData/name' do
+ let(:value) { file_link.origin_name }
+ end
+
+ it_behaves_like 'property', 'originData/mimeType' do
+ let(:value) { file_link.origin_mime_type }
+ end
+
+ it_behaves_like 'datetime property', 'originData/createdAt' do
+ let(:value) { file_link.origin_created_at }
+ end
+
+ it_behaves_like 'datetime property', 'originData/lastModifiedAt' do
+ let(:value) { file_link.origin_updated_at }
+ end
+
+ it_behaves_like 'property', 'originData/createdByName' do
+ let(:value) { file_link.origin_created_by_name }
+ end
+
+ it_behaves_like 'property', 'originData/lastModifiedByName' do
+ let(:value) { file_link.origin_last_modified_by_name }
+ end
+ end
+ end
+end