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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
when POSTing file links, prevent any FileLink creation if one of them is invalid
Christophe Bliarddbbbd5b7592b
diff --git a/docs/api/apiv3/paths/work_package_file_links.yml b/docs/api/apiv3/paths/work_package_file_links.ymlindex 5c2fbc9872b..8d2fda9dfc8 100644--- a/docs/api/apiv3/paths/work_package_file_links.yml+++ b/docs/api/apiv3/paths/work_package_file_links.yml@@ -10,12 +10,14 @@ post:Creates file links on a work package.The request body must contain the generated storage token, and not the storage id. In addition, the request must- send the origin file id and the stored meta data.+ send the origin file id, name, and mime type. The request should also send the origin meta data: `createdAt`, `createdByName`, `lastModifiedAt`, `lastModifiedByName` can the stored meta data.Up to 20 file links can be submitted at once. If there is already a link on the given work package with the samefile id from the same storage, the previous data is not overwritten.As it is possible to link folders from a storage to a work package, the _mimeType_ of this entity can be empty.++ If some data is invalid, no file links will be created.parameters:- name: iddescription: Work package iddiff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.ymlindex 23ffb7d27fd..c5b8affcefc 100644--- a/modules/storages/config/locales/en.yml+++ b/modules/storages/config/locales/en.yml@@ -13,6 +13,8 @@ en:name: "Name"creator: "Creator"provider_type: "Provider type"+ storages/file_link:+ origin_id: "Origin Id"errors:messages:diff --git a/modules/storages/lib/api/v3/file_links/create_endpoint.rb b/modules/storages/lib/api/v3/file_links/create_endpoint.rbindex c6f50017f27..78f03a711e2 100644--- a/modules/storages/lib/api/v3/file_links/create_endpoint.rb+++ b/modules/storages/lib/api/v3/file_links/create_endpoint.rb@@ -47,12 +47,18 @@ module API::V3::FileLinks# global_result using the `add_dependent!` method.def process(request, params_elements)global_result = ServiceResult.new(success: true)- params_elements.each do |params|- # call the default API::Utilities::Endpoints::Create#process- # implementation for each of the params_element array- one_result = super(request, params)- # merge service result in one- global_result.add_dependent!(one_result)++ Storages::FileLink.transaction do+ params_elements.each do |params|+ # call the default API::Utilities::Endpoints::Create#process+ # implementation for each of the params_element array+ one_result = super(request, params)+ # merge service result in one+ global_result.add_dependent!(one_result)+ end++ # rollback records created if an error occurred (validation failed)+ raise ActiveRecord::Rollback if global_result.failure?endglobal_resultenddiff --git a/modules/storages/spec/factories/file_link_element_factory.rb b/modules/storages/spec/factories/file_link_element_factory.rbnew file mode 100644index 00000000000..88638e080bb--- /dev/null+++ b/modules/storages/spec/factories/file_link_element_factory.rb@@ -0,0 +1,60 @@+#-- 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.+#++++# Expects parameters: storage, container_id+FactoryBot.define do+ factory :file_link_element, class: Hash do+ sequence(:origin_id) { |n| "10000#{n}" } # ID within external storage (i.e. Nextcloud)+ sequence(:origin_name) { |n| "file_name_#{n}.txt" } # File name within external storage (i.e. Nextcloud)+ origin_mime_type { "text/plain" }+ origin_created_at { Time.zone.now }+ origin_updated_at { Time.zone.now }+ origin_created_by_name { "Peter Pan" }+ origin_last_modified_by_name { "Petra Panadera" }+ storage_url { "https://nextcloud.example.com" }++ trait :invalid do+ origin_id { "I'm invalid" }+ end++ initialize_with do+ origin_data = attributes.select { |key, _| key.starts_with?('origin_') }+ .transform_keys { |key| key.to_s.gsub('origin_', '').camelcase(:lower).to_sym }+ .then { |data| data.transform_values { |v| v.respond_to?(:iso8601) ? v.iso8601 : v } }+ .then { |data| data.transform_keys { |k| k.to_sym == :updatedAt ? :lastModifiedAt : k } }+ {+ originData: origin_data,+ _links: {+ storageUrl: {+ href: attributes[:storage_url]+ }+ }+ }+ end+ end+enddiff --git a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rbindex 8c6b8c72a77..e49a9e9cc19 100644--- a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb+++ b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb@@ -90,52 +90,73 @@ describe 'API v3 file links resource', type: :request do{_type: "Collection",_embedded: {- elements: [- {- originData: {- id: 5503,- name: "logo.png",- mimeType: "image/png",- createdAt: "2021-12-19T09:42:10.170Z",- lastModifiedAt: "2021-12-20T14:00:13.987Z",- createdByName: "Luke Skywalker",- lastModifiedByName: "Anakin Skywalker"- },- _links: {- storageUrl: {- href: storage_url1- }- }- },- {- originData: {- id: 5514,- name: "plans.md",- mimeType: "text/markdown",- createdAt: "2021-12-14T09:01:23.456Z",- lastModifiedAt: "2021-12-14T09:01:23.456Z",- createdByName: "Yoda",- lastModifiedByName: "Yoda"- },- _links: {- storageUrl: {- href: storage_url2- }- }- }- ]+ elements: embedded_elements}}end+ let(:embedded_elements) do+ # first record is not using a factory here so that the test documents what+ # the request looks like, and it tests the factory.+ [+ {+ originData: {+ id: 5503,+ name: "logo.png",+ mimeType: "image/png",+ createdAt: "2021-12-19T09:42:10.170Z",+ lastModifiedAt: "2021-12-20T14:00:13.987Z",+ createdByName: "Luke Skywalker",+ lastModifiedByName: "Anakin Skywalker"+ },+ _links: {+ storageUrl: {+ href: storage_url1+ }+ }+ },+ build(:file_link_element, storage_url: storage_url2)+ ]+ endbefore doheader 'Content-Type', 'application/json'post path, params.to_jsonend- it_behaves_like 'API V3 collection response', 2, 2, 'FileLink' do- let(:elements) { Storages::FileLink.all.order(id: :asc) }- let(:expected_status_code) { 201 }+ context 'when all embedded file link elements are valid' do+ it_behaves_like 'API V3 collection response', 2, 2, 'FileLink' do+ let(:elements) { Storages::FileLink.all.order(id: :asc) }+ let(:expected_status_code) { 201 }+ end++ it 'creates corresponding FileLink records', :aggregate_failures do+ expect(Storages::FileLink.count).to eq 2+ Storages::FileLink.find_each.with_index do |file_link, i|+ file_link.attributes.each do |(key, value)|+ # check nil values to ensure the :file_link_element factory is accurate+ expect(value).not_to be_nil,+ "expected attribute #{key.inspect} of FileLink ##{i + 1} to be set.\ngot nil."+ end+ end+ end+ end++ context 'when some embedded file link elements are NOT valid' do+ let(:embedded_elements) do+ [+ build(:file_link_element, :invalid, storage_url: storage_url1),+ build(:file_link_element, origin_name: "the valid one", storage_url: storage_url1)+ ]+ end++ it_behaves_like 'constraint violation' do+ let(:message) { 'Error attempting to create dependent object: File link ' }+ end++ it 'does not create any FileLink records' do+ expect(Storages::FileLink.find_by(origin_name: "the valid one")).to be_nil+ expect(Storages::FileLink.count).to eq 0+ endendcontext 'when storage host is invalid' do@@ -181,17 +202,13 @@ describe 'API v3 file links resource', type: :request doendcontext 'when _embedded/elements is empty' do- let(:params) do- { _embedded: { elements: [] } }- end+ let(:embedded_elements) { [] }it_behaves_like 'missing property', I18n.t('api_v3.errors.missing_property', property: '_embedded/elements')endcontext 'when _embedded/elements is not an array' do- let(:params) do- { _embedded: { elements: 42 } }- end+ let(:embedded_elements) { 42 }it_behaves_like 'format error',I18n.t('api_v3.errors.invalid_format',@@ -204,31 +221,7 @@ describe 'API v3 file links resource', type: :request docontext "when more than #{API::V3::FileLinks::ParseCreateParamsService::MAX_ELEMENTS} embedded elements" dolet(:max) { API::V3::FileLinks::ParseCreateParamsService::MAX_ELEMENTS }let(:too_many) { max + 1 }- let(:params) do- {- _type: "Collection",- _embedded: {- elements: 1.upto(too_many).map do |id|- {- originData: {- id: id,- name: "logo#{id}.png",- mimeType: "image/png",- createdAt: "2021-12-19T09:42:10.170Z",- lastModifiedAt: "2021-12-20T14:00:13.987Z",- createdByName: "Luke Skywalker",- lastModifiedByName: "Anakin Skywalker"- },- _links: {- storageUrl: {- href: storage_url1- }- }- }- end- }- }- end+ let(:embedded_elements) { build_list(:file_link_element, too_many, storage_url: storage_url1) }it_behaves_like 'constraint violation' dolet(:message) { "Too many elements created at once. Expected #{max} at most, got #{too_many}." }