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
[#53996] refinements on create folder commands
Eric Schubert1d835808c060
diff --git a/modules/storages/app/common/storages/peripherals/service_result_refinements.rb b/modules/storages/app/common/storages/peripherals/service_result_refinements.rbindex 3d3bb1a8114..c071701434c 100644--- a/modules/storages/app/common/storages/peripherals/service_result_refinements.rb+++ b/modules/storages/app/common/storages/peripherals/service_result_refinements.rb@@ -28,61 +28,65 @@# See COPYRIGHT and LICENSE files for more details.#++-module Storages::Peripherals- # rubocop:disable Lint/EmptyClass- class UnknownSource; end-- # rubocop:enable Lint/EmptyClass-- module ServiceResultRefinements- refine ServiceResult do- def match(on_success:, on_failure:)- if success?- on_success.call(result)- else- on_failure.call(errors)+module Storages+ module Peripherals+ # rubocop:disable Lint/EmptyClass+ class UnknownSource; end++ # rubocop:enable Lint/EmptyClass++ module ServiceResultRefinements+ refine ServiceResult do+ def match(on_success:, on_failure:)+ if success?+ on_success.call(result)+ else+ on_failure.call(errors)+ endend- end- def bind- return self if failure?+ def bind+ return self if failure?- yield result- end+ yield result+ end++ def >>(other)+ unless other.respond_to?(:call)+ raise TypeError, "Expected an object responding to 'call', got #{other.class.name}."+ end- def >>(other)- unless other.respond_to?(:call)- raise TypeError, "Expected an object responding to 'call', got #{other.class.name}."+ bind(&other)end- bind(&other)- end+ def error_source+ if errors.is_a?(::Storages::StorageError) && errors.data&.source.present?+ errors.data.source+ else+ UnknownSource+ end+ end- def error_source- if errors.is_a?(::Storages::StorageError) && errors.data&.source.present?- errors.data.source- else- UnknownSource+ def error_payload+ errors.data&.payloadend- end- def error_payload- errors.data&.payload- end+ def result_or+ return result if success?- def result_or- return result if success?+ yield errors+ end- yield errors- end- alias_method :error_and, :result_or+ alias_method :error_and, :result_or++ def result_and+ return errors if failure?- def result_and- return errors if failure?+ yield result+ end- yield result+ alias_method :error_or, :result_andend- alias_method :error_or, :result_andendendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rbindex c8a623bb52e..340b7e1008e 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb@@ -28,99 +28,113 @@# See COPYRIGHT and LICENSE files for more details.#++-module Storages::Peripherals::StorageInteraction::Nextcloud- class CreateFolderCommand- using Storages::Peripherals::ServiceResultRefinements+module Storages+ module Peripherals+ module StorageInteraction+ module Nextcloud+ class CreateFolderCommand+ using ServiceResultRefinements- Auth = ::Storages::Peripherals::StorageInteraction::Authentication+ def self.call(storage:, auth_strategy:, folder_name:, parent_location:)+ new(storage).call(auth_strategy:, folder_name:, parent_location:)+ end- def self.call(storage:, auth_strategy:, folder_name:, parent_location:)- new(storage).call(auth_strategy:, folder_name:, parent_location:)- end+ def initialize(storage)+ @storage = storage+ end- def initialize(storage)- @storage = storage- end+ def call(auth_strategy:, folder_name:, parent_location:)+ origin_user_id = Util.origin_user_id(caller: self.class, storage: @storage, auth_strategy:)+ if origin_user_id.failure?+ return origin_user_id+ end- def call(auth_strategy:, folder_name:, parent_location:)- origin_user_id = Util.origin_user_id(caller: self.class, storage: @storage, auth_strategy:)- if origin_user_id.failure?- return origin_user_id- end+ folder_path = Util.join_uri_path(parent_location, folder_name)+ path_prefix = URI.parse(Util.join_uri_path(base_uri, CGI.escapeURIComponent(origin_user_id.result))).path+ request_url = Util.join_uri_path(base_uri,+ CGI.escapeURIComponent(origin_user_id.result),+ Util.escape_path(folder_path))- folder_path = Util.join_uri_path(parent_location, folder_name)- base_url = Util.join_uri_path(@storage.uri, "remote.php/dav/files", CGI.escapeURIComponent(origin_user_id.result))- path_prefix = URI.parse(base_url).path- request_url = Util.join_uri_path(base_url, Util.escape_path(folder_path))+ create_folder_request(auth_strategy, request_url, path_prefix)+ end- Auth[auth_strategy].call(storage: @storage) do |http|- result = handle_response(http.mkcol(request_url))- return result if result.failure?+ private- handle_response(http.propfind(request_url, requested_properties)).map(&storage_file(path_prefix))- end- end+ def create_folder_request(auth_strategy, request_url, path_prefix)+ Authentication[auth_strategy].call(storage: @storage) do |http|+ result = handle_response(http.mkcol(request_url))+ return result if result.failure?- private-- def handle_response(response)- case response- in { status: 200..299 }- ServiceResult.success(result: response)- in { status: 401 }- Util.failure(code: :unauthorized,- data: Util.error_data_from_response(caller: self.class, response:),- log_message: "Outbound request not authorized!")- in { status: 404 | 409 } # webDAV endpoint returns 409 if path does not exist- Util.failure(code: :not_found,- data: Util.error_data_from_response(caller: self.class, response:),- log_message: "Outbound request destination not found!")- in { status: 405 } # webDAV endpoint returns 405 if folder already exists- Util.failure(code: :conflict,- data: Util.error_data_from_response(caller: self.class, response:),- log_message: "Folder already exists")- else- Util.failure(code: :error,- data: Util.error_data_from_response(caller: self.class, response:),- log_message: "Outbound request failed with unknown error!")- end- end+ handle_response(http.propfind(request_url, requested_properties)).map do |response|+ storage_file(path_prefix, response)+ end+ end+ end- def requested_properties- Nokogiri::XML::Builder.new do |xml|- xml["d"].propfind(- "xmlns:d" => "DAV:",- "xmlns:oc" => "http://owncloud.org/ns"- ) do- xml["d"].prop do- xml["oc"].fileid- xml["oc"].size- xml["d"].getlastmodified- xml["oc"].send(:"owner-display-name")+ def handle_response(response)+ case response+ in { status: 200..299 }+ ServiceResult.success(result: response)+ in { status: 401 }+ Util.failure(code: :unauthorized,+ data: Util.error_data_from_response(caller: self.class, response:),+ log_message: "Outbound request not authorized!")+ in { status: 404 | 409 } # webDAV endpoint returns 409 if path does not exist+ Util.failure(code: :not_found,+ data: Util.error_data_from_response(caller: self.class, response:),+ log_message: "Outbound request destination not found!")+ in { status: 405 } # webDAV endpoint returns 405 if folder already exists+ Util.failure(code: :conflict,+ data: Util.error_data_from_response(caller: self.class, response:),+ log_message: "Folder already exists")+ else+ Util.failure(code: :error,+ data: Util.error_data_from_response(caller: self.class, response:),+ log_message: "Outbound request failed with unknown error!")+ end+ end++ def requested_properties+ Nokogiri::XML::Builder.new do |xml|+ xml["d"].propfind(+ "xmlns:d" => "DAV:",+ "xmlns:oc" => "http://owncloud.org/ns"+ ) do+ xml["d"].prop do+ xml["oc"].fileid+ xml["oc"].size+ xml["d"].getlastmodified+ xml["oc"].send(:"owner-display-name")+ end+ end+ end.to_xmlend- end- end.to_xml- end- def storage_file(path_prefix)- ->(response) do- xml = response.xml- path = xml.xpath('//d:response/d:href/text()').to_s- timestamp = xml.xpath('//d:response/d:propstat/d:prop/d:getlastmodified/text()').to_s- creator = xml.xpath('//d:response/d:propstat/d:prop/oc:owner-display-name/text()').to_s- location = CGI.unescapeURIComponent(path.gsub(path_prefix, "")).delete_suffix("/")-- Storages::StorageFile.new(- id: xml.xpath('//d:response/d:propstat/d:prop/oc:fileid/text()').to_s,- name: location.split('/').last,- size: xml.xpath('//d:response/d:propstat/d:prop/oc:size/text()').to_s,- mime_type: "application/x-op-directory",- created_at: Time.zone.parse(timestamp),- last_modified_at: Time.zone.parse(timestamp),- created_by_name: creator,- last_modified_by_name: creator,- location:- )+ # rubocop:disable Metrics/AbcSize+ def storage_file(path_prefix, response)+ xml = response.xml+ path = xml.xpath("//d:response/d:href/text()").to_s+ timestamp = xml.xpath("//d:response/d:propstat/d:prop/d:getlastmodified/text()").to_s+ creator = xml.xpath("//d:response/d:propstat/d:prop/oc:owner-display-name/text()").to_s+ location = CGI.unescapeURIComponent(path.gsub(path_prefix, "")).delete_suffix("/")++ StorageFile.new(+ id: xml.xpath("//d:response/d:propstat/d:prop/oc:fileid/text()").to_s,+ name: location.split("/").last,+ size: xml.xpath("//d:response/d:propstat/d:prop/oc:size/text()").to_s,+ mime_type: "application/x-op-directory",+ created_at: Time.zone.parse(timestamp),+ last_modified_at: Time.zone.parse(timestamp),+ created_by_name: creator,+ last_modified_by_name: creator,+ location:+ )+ end++ # rubocop:enable Metrics/AbcSize++ def base_uri = Util.join_uri_path(@storage.uri, "remote.php", "dav", "files")+ endendendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rbindex 4264f7dbaf7..67a38616255 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb@@ -88,7 +88,8 @@ module Storages::Peripherals::StorageInteraction::Nextcloud::Utilwhen :basic_authServiceResult.success(result: storage.username)when :oauth_user_token- origin_user_id = OAuthClientToken.find_by(user_id: auth_strategy.user, oauth_client: storage.oauth_client)&.origin_user_id+ origin_user_id = OAuthClientToken.where(user_id: auth_strategy.user, oauth_client: storage.oauth_client)+ .pick(:origin_user_id)if origin_user_id.present?ServiceResult.success(result: origin_user_id)elsediff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rbindex 75b3173e815..67a5ff82e1a 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/create_folder_command.rb@@ -35,8 +35,6 @@ module Storagesclass CreateFolderCommandusing ServiceResultRefinements- Auth = ::Storages::Peripherals::StorageInteraction::Authentication-def self.call(storage:, auth_strategy:, folder_name:, parent_location:)new(storage).call(auth_strategy:, folder_name:, parent_location:)end@@ -46,7 +44,7 @@ module Storagesenddef call(auth_strategy:, folder_name:, parent_location:)- Auth[auth_strategy].call(storage: @storage, http_options:) do |http|+ Authentication[auth_strategy].call(storage: @storage, http_options:) do |http|handle_response http.post(uri_for(parent_location), body: payload(folder_name))endenddiff --git a/modules/storages/app/services/storages/one_drive_managed_folder_sync_service.rb b/modules/storages/app/services/storages/one_drive_managed_folder_sync_service.rbindex d4519be3481..956abdefe9a 100644--- a/modules/storages/app/services/storages/one_drive_managed_folder_sync_service.rb+++ b/modules/storages/app/services/storages/one_drive_managed_folder_sync_service.rb@@ -190,7 +190,7 @@ module Storagesenddef auth_strategy- Peripherals::StorageInteraction::AuthenticationStrategies::OAuthClientCredentials.strategy+ Peripherals::Registry.resolve("one_drive.authentication.userless").callenddef admin_client_tokens_scopediff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/create_folder_command_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/create_folder_command_spec.rbdeleted file mode 100644index 60cf729f64e..00000000000--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/create_folder_command_spec.rb+++ /dev/null@@ -1,197 +0,0 @@-# frozen_string_literal: true--#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 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"-require_module_spec_helper--RSpec.describe "create folder command" do- using Storages::Peripherals::ServiceResultRefinements-- shared_examples_for "basic command setup" do- it "is registered as commands.create_folder" do- expect(Storages::Peripherals::Registry- .resolve("#{storage.short_provider_type}.commands.create_folder")).to eq(described_class)- end-- it "responds to #call with correct parameters" do- expect(described_class).to respond_to(:call)-- method = described_class.method(:call)- expect(method.parameters).to contain_exactly(%i[keyreq storage],- %i[keyreq auth_strategy],- %i[keyreq folder_name],- %i[keyreq parent_location])- end- end-- shared_examples_for "successful folder creation" do- it "creates a folder" do- result = described_class.call(storage:, auth_strategy:, folder_name:, parent_location:)-- result.match(- on_success: ->(response) do- expect(response).to be_a(Storages::StorageFile)- expect(response.name).to eq(folder_name)- expect(response.location).to eq(path)- end,- on_failure: ->(error) { fail "Expected success, got #{error}" }- )- ensure- delete_created_folder(storage, result.result)- end- end-- shared_examples_for "parent not found" do- it "returns a failure" do- result = described_class.call(storage:, auth_strategy:, folder_name:, parent_location:)-- result.match(- on_failure: ->(error) do- expect(error.code).to eq(:not_found)- expect(error.data.source).to eq(error_source)- end,- on_success: ->(result) { fail "Expected failure, got #{result}" }- )- end- end-- shared_examples_for "folder already exists" do- it "returns a failure" do- result = described_class.call(storage:, auth_strategy:, folder_name:, parent_location:)-- result.match(- on_failure: ->(error) do- expect(error.code).to eq(:conflict)- expect(error.data.source).to eq(error_source)- end,- on_success: ->(result) { fail "Expected failure, got #{result}" }- )- end- end-- describe Storages::Peripherals::StorageInteraction::OneDrive::CreateFolderCommand, :vcr, :webmock do- let(:storage) { create(:sharepoint_dev_drive_storage) }- let(:auth_strategy) do- Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthClientCredentials.strategy- end-- it_behaves_like "basic command setup"-- context "when creating a folder in the root", vcr: "one_drive/create_folder_root" do- let(:folder_name) { "Földer CreatedBy Çommand" }- let(:parent_location) { Storages::Peripherals::ParentFolder.new("/") }- let(:path) { "/#{folder_name}" }-- it_behaves_like "successful folder creation"- end-- context "when creating a folder in a parent folder", vcr: "one_drive/create_folder_parent" do- let(:folder_name) { "Földer CreatedBy Çommand" }- let(:parent_location) { Storages::Peripherals::ParentFolder.new("01AZJL5PKU2WV3U3RKKFF2A7ZCWVBXRTEU") }- let(:path) { "/Folder with spaces/#{folder_name}" }-- it_behaves_like "successful folder creation"- end-- context "when creating a folder in a non-existing parent folder", vcr: "one_drive/create_folder_parent_not_found" do- let(:folder_name) { "Földer CreatedBy Çommand" }- let(:parent_location) { Storages::Peripherals::ParentFolder.new("01AZJL5PKU2WV3U3RKKFF4A7ZCWVBXRTEU") }- let(:error_source) { described_class }-- it_behaves_like "parent not found"- end-- context "when folder already exists", vcr: "one_drive/create_folder_already_exists" do- let(:folder_name) { "Folder" }- let(:parent_location) { Storages::Peripherals::ParentFolder.new("/") }- let(:error_source) { described_class }-- it_behaves_like "folder already exists"- end- end-- describe Storages::Peripherals::StorageInteraction::Nextcloud::CreateFolderCommand, :vcr, :webmock do- let(:user) { create(:user) }- let(:storage) do- create(:nextcloud_storage_with_local_connection, :as_not_automatically_managed, oauth_client_token_user: user)- end- let(:auth_strategy) do- Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken.strategy.with_user(user)- end-- it_behaves_like "basic command setup"-- context "when creating a folder in the root", vcr: "nextcloud/create_folder_root" do- let(:folder_name) { "Földer CreatedBy Çommand" }- let(:parent_location) { Storages::Peripherals::ParentFolder.new("/") }- let(:path) { "/#{folder_name}" }-- it_behaves_like "successful folder creation"- end-- context "when creating a folder in a parent folder", vcr: "nextcloud/create_folder_parent" do- let(:folder_name) { "Földer CreatedBy Çommand" }- let(:parent_location) { Storages::Peripherals::ParentFolder.new("/Folder") }- let(:path) { "/Folder/#{folder_name}" }-- it_behaves_like "successful folder creation"- end-- context "when creating a folder in a non-existing parent folder", vcr: "nextcloud/create_folder_parent_not_found" do- let(:folder_name) { "New Folder" }- let(:parent_location) { Storages::Peripherals::ParentFolder.new("/DeathStar3") }- let(:error_source) { described_class }-- it_behaves_like "parent not found"- end-- context "when folder already exists", vcr: "nextcloud/create_folder_already_exists" do- let(:folder_name) { "Folder" }- let(:parent_location) { Storages::Peripherals::ParentFolder.new("/") }- let(:error_source) { described_class }-- it_behaves_like "folder already exists"- end- end-- private-- def delete_created_folder(storage, folder)- location = if storage.provider_type_nextcloud?- folder.location- else- folder.id- end-- Storages::Peripherals::Registry- .resolve("#{storage.short_provider_type}.commands.delete_folder")- .call(storage:, auth_strategy:, location:)- end-enddiff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command_spec.rbnew file mode 100644index 00000000000..615493b1a1a--- /dev/null+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command_spec.rb@@ -0,0 +1,84 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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"+require_module_spec_helper++RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::CreateFolderCommand, :vcr, :webmock do+ let(:user) { create(:user) }+ let(:storage) do+ create(:nextcloud_storage_with_local_connection, :as_not_automatically_managed, oauth_client_token_user: user)+ end+ let(:auth_strategy) do+ Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken.strategy.with_user(user)+ end++ it_behaves_like "basic command setup"++ context "when creating a folder in the root", vcr: "nextcloud/create_folder_root" do+ let(:folder_name) { "Földer CreatedBy Çommand" }+ let(:parent_location) { Storages::Peripherals::ParentFolder.new("/") }+ let(:path) { "/#{folder_name}" }++ it_behaves_like "successful folder creation"+ end++ context "when creating a folder in a parent folder", vcr: "nextcloud/create_folder_parent" do+ let(:folder_name) { "Földer CreatedBy Çommand" }+ let(:parent_location) { Storages::Peripherals::ParentFolder.new("/Folder") }+ let(:path) { "/Folder/#{folder_name}" }++ it_behaves_like "successful folder creation"+ end++ context "when creating a folder in a non-existing parent folder", vcr: "nextcloud/create_folder_parent_not_found" do+ let(:folder_name) { "New Folder" }+ let(:parent_location) { Storages::Peripherals::ParentFolder.new("/DeathStar3") }+ let(:error_source) { described_class }++ it_behaves_like "parent not found"+ end++ context "when folder already exists", vcr: "nextcloud/create_folder_already_exists" do+ let(:folder_name) { "Folder" }+ let(:parent_location) { Storages::Peripherals::ParentFolder.new("/") }+ let(:error_source) { described_class }++ it_behaves_like "folder already exists"+ end++ private++ def delete_created_folder(folder)+ Storages::Peripherals::Registry+ .resolve("nextcloud.commands.delete_folder")+ .call(storage:, auth_strategy:, location: folder.location)+ end+enddiff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/create_folder_command_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/create_folder_command_spec.rbnew file mode 100644index 00000000000..6e3fcb92f09--- /dev/null+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/create_folder_command_spec.rb@@ -0,0 +1,81 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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"+require_module_spec_helper++RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::CreateFolderCommand, :vcr, :webmock do+ let(:storage) { create(:sharepoint_dev_drive_storage) }+ let(:auth_strategy) do+ Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthClientCredentials.strategy+ end++ it_behaves_like "basic command setup"++ context "when creating a folder in the root", vcr: "one_drive/create_folder_root" do+ let(:folder_name) { "Földer CreatedBy Çommand" }+ let(:parent_location) { Storages::Peripherals::ParentFolder.new("/") }+ let(:path) { "/#{folder_name}" }++ it_behaves_like "successful folder creation"+ end++ context "when creating a folder in a parent folder", vcr: "one_drive/create_folder_parent" do+ let(:folder_name) { "Földer CreatedBy Çommand" }+ let(:parent_location) { Storages::Peripherals::ParentFolder.new("01AZJL5PKU2WV3U3RKKFF2A7ZCWVBXRTEU") }+ let(:path) { "/Folder with spaces/#{folder_name}" }++ it_behaves_like "successful folder creation"+ end++ context "when creating a folder in a non-existing parent folder", vcr: "one_drive/create_folder_parent_not_found" do+ let(:folder_name) { "Földer CreatedBy Çommand" }+ let(:parent_location) { Storages::Peripherals::ParentFolder.new("01AZJL5PKU2WV3U3RKKFF4A7ZCWVBXRTEU") }+ let(:error_source) { described_class }++ it_behaves_like "parent not found"+ end++ context "when folder already exists", vcr: "one_drive/create_folder_already_exists" do+ let(:folder_name) { "Folder" }+ let(:parent_location) { Storages::Peripherals::ParentFolder.new("/") }+ let(:error_source) { described_class }++ it_behaves_like "folder already exists"+ end++ private++ def delete_created_folder(folder)+ Storages::Peripherals::Registry+ .resolve("one_drive.commands.delete_folder")+ .call(storage:, auth_strategy:, location: folder.id)+ end+enddiff --git a/modules/storages/spec/support/shared_examples_for_adapters/create_folder_command_examples.rb b/modules/storages/spec/support/shared_examples_for_adapters/create_folder_command_examples.rbnew file mode 100644index 00000000000..1ef93d5e417--- /dev/null+++ b/modules/storages/spec/support/shared_examples_for_adapters/create_folder_command_examples.rb@@ -0,0 +1,85 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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.+#++++RSpec.shared_examples_for "basic command setup" do+ it "is registered as commands.create_folder" do+ expect(Storages::Peripherals::Registry+ .resolve("#{storage.short_provider_type}.commands.create_folder")).to eq(described_class)+ end++ it "responds to #call with correct parameters" do+ expect(described_class).to respond_to(:call)++ method = described_class.method(:call)+ expect(method.parameters).to contain_exactly(%i[keyreq storage],+ %i[keyreq auth_strategy],+ %i[keyreq folder_name],+ %i[keyreq parent_location])+ end+end++RSpec.shared_examples_for "successful folder creation" do+ it "creates a folder" do+ result = described_class.call(storage:, auth_strategy:, folder_name:, parent_location:)++ expect(result).to be_success++ response = result.result+ expect(response).to be_a(Storages::StorageFile)+ expect(response.name).to eq(folder_name)+ expect(response.location).to eq(path)+ ensure+ delete_created_folder(response)+ end+end++RSpec.shared_examples_for "parent not found" do+ it "returns a failure" do+ result = described_class.call(storage:, auth_strategy:, folder_name:, parent_location:)++ expect(result).to be_failure++ error = result.errors+ expect(error.code).to eq(:not_found)+ expect(error.data.source).to eq(error_source)+ end+end++RSpec.shared_examples_for "folder already exists" do+ it "returns a failure" do+ result = described_class.call(storage:, auth_strategy:, folder_name:, parent_location:)++ expect(result).to be_failure++ error = result.errors+ expect(error.code).to eq(:conflict)+ expect(error.data.source).to eq(error_source)+ end+end