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

[48573] Added specs for CopyTemplateFolderCommand

Andreas Pfohl44831946011c
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
index e2423aa7205..a5d238a3824 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
@@ -30,11 +30,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
class CopyTemplateFolderCommand
using Storages::Peripherals::ServiceResultRefinements
- def initialize(storage)
- @storage = storage
+ def initialize(storage:)
+ @uri = URI(storage.host).normalize
+ @username = storage.username
+ @password = storage.password
end
- def call(source_path, destination_path)
+ def call(source_path:, destination_path:)
validate_inputs(source_path, destination_path) >>
build_origin_paths >>
validate_destination >>
@@ -52,16 +54,16 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def build_origin_paths
->(input) do
source = Util.join_uri_path(
- @storage.host,
+ @uri,
"remote.php/dav/files",
- CGI.escapeURIComponent(@storage.username),
+ CGI.escapeURIComponent(@username),
Util.escape_path(input[:source_path])
)
destination = Util.join_uri_path(
- @storage.host,
+ @uri,
"remote.php/dav/files",
- CGI.escapeURIComponent(@storage.username),
+ CGI.escapeURIComponent(@username),
Util.escape_path(input[:destination_path])
)
@@ -72,9 +74,9 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def validate_destination
->(urls) do
request = Net::HTTP::Head.new(urls[:destination_url])
- request.initialize_http_header Util.basic_auth_header(@storage.username, @storage.password)
+ request.initialize_http_header Util.basic_auth_header(@username, @password)
- response = Util.http(@storage.host).request(request)
+ response = Util.http(@uri).request(request)
case response
when Net::HTTPSuccess
@@ -95,21 +97,21 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
request = Net::HTTP::Copy.new(urls[:source_url])
request['Destination'] = urls[:destination_url]
- headers = Util.basic_auth_header(@storage.username, @storage.password)
+ headers = Util.basic_auth_header(@username, @password)
headers['Depth'] = 'infinity'
request.initialize_http_header headers
- response = Util.http(@storage.host).request(request)
+ response = Util.http(@uri).request(request)
case response
- when Net::HTTPSuccess
+ when Net::HTTPCreated
ServiceResult.success(message: 'Folder was successfully copied.')
when Net::HTTPUnauthorized
Util.error(:not_authorized)
when Net::HTTPNotFound
Util.error(:not_found)
when Net::HTTPConflict
- Util.error(:conflict, error_text_from_response(response))
+ Util.error(:conflict, Util.error_text_from_response(response))
else
Util.error(:unknown)
end
diff --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.rb
index 5afbdb70588..18f15a33c33 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
@@ -58,17 +58,11 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
when Net::HTTPNotFound
Util.error(:not_found)
when Net::HTTPConflict
- Util.error(:conflict, error_text_from_response(response))
+ Util.error(:conflict, Util.error_text_from_response(response))
else
Util.error(:error)
end
end
# rubocop:enable Metrics/AbcSize
-
- private
-
- def error_text_from_response(response)
- Nokogiri::XML(response.body).xpath("//s:message").text
- end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
index 86f27eaab27..042e70678e7 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
@@ -70,5 +70,9 @@ module Storages::Peripherals::StorageInteraction::Nextcloud::Util
http.use_ssl = uri.scheme == 'https'
http
end
+
+ def error_text_from_response(response)
+ Nokogiri::XML(response.body).xpath("//s:message").text
+ end
end
end
diff --git a/modules/storages/spec/common/peripherals/copy_template_folder_command_spec.rb b/modules/storages/spec/common/peripherals/copy_template_folder_command_spec.rb
new file mode 100644
index 00000000000..4e04733611e
--- /dev/null
+++ b/modules/storages/spec/common/peripherals/copy_template_folder_command_spec.rb
@@ -0,0 +1,122 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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'
+
+RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::CopyTemplateFolderCommand, webmock: true do
+ using Storages::Peripherals::ServiceResultRefinements
+
+ let(:user) { create(:user) }
+ let(:url) { 'https://example.com' }
+ let(:origin_user_id) { 'OpenProject' }
+ let(:storage) { build(:nextcloud_storage, :as_automatically_managed, host: url, password: 'OpenProjectSecurePassword') }
+
+ subject { described_class.new(storage:) }
+
+ describe '#call' do
+ context 'when the source path is blank' do
+ it 'returns an error' do
+ result = subject.call(source_path: '', destination_path: 'destination')
+
+ expect(result).to be_failure
+ expect(result.errors.log_message).to eq('Source and destination paths must be present.')
+ end
+ end
+
+ context 'when the destination path is blank' do
+ it 'returns an error' do
+ result = subject.call(source_path: 'source', destination_path: '')
+
+ expect(result).to be_failure
+ expect(result.errors.log_message).to eq('Source and destination paths must be present.')
+ end
+ end
+
+ context 'when the source path is not blank' do
+ context 'when the destination path is not blank' do
+ let(:source_path) { 'source' }
+ let(:destination_path) { 'destination' }
+ let(:source_url) { "#{url}/remote.php/dav/files/#{CGI.escape(origin_user_id)}/#{source_path}" }
+ let(:destination_url) { "#{url}/remote.php/dav/files/#{CGI.escape(origin_user_id)}/#{destination_path}" }
+
+ context 'when the destination exsists' do
+ before do
+ stub_request(:head, destination_url).to_return(status: 200)
+ end
+
+ it 'returns an error' do
+ result = subject.call(source_path:, destination_path:)
+
+ expect(result).to be_failure
+ expect(result.errors.log_message).to eq('Destination folder already exists.')
+ end
+ end
+
+ context 'when the destination does not exist' do
+ before do
+ stub_request(:head, destination_url).to_return(status: 404)
+ end
+
+ context 'when the folder is copied successfully' do
+ before do
+ stub_request(:copy, source_url).to_return(status: 201)
+ end
+
+ it 'must be successful' do
+ result = subject.call(source_path:, destination_path:)
+
+ expect(result).to be_success
+ end
+ end
+
+ context 'when the folder is not copied successfully' do
+ before do
+ body = <<~XML
+ <?xml version="1.0" encoding="utf-8"?>
+ <d:error
+ xmlns:d="DAV:"
+ xmlns:s="http://sabredav.org/ns">
+ <s:exception>Sabre\\DAV\\Exception\\Conflict</s:exception>
+ <s:message>The destination node is not found</s:message>
+ </d:error>
+ XML
+ stub_request(:copy, source_url).to_return(status: 409, body:)
+ end
+
+ it 'must fail with :conflict' do
+ result = subject.call(source_path:, destination_path:)
+
+ expect(result).to be_failure
+ expect(result.errors.code).to eq(:conflict)
+ end
+ end
+ end
+ end
+ end
+ end
+end