From 62328f8dbe6ef703d62f957fffe800883ca91449 Mon Sep 17 00:00:00 2001 From: Pavel Balashou Date: Tue, 24 Oct 2023 17:50:57 +0200 Subject: [PATCH] [#50684] Migrate to httpx http ruby client https://community.openproject.org/work_packages/50684 --- Gemfile | 2 + Gemfile.lock | 6 ++- .../nextcloud/add_user_to_group_command.rb | 36 ++++++++------- .../nextcloud/copy_template_folder_command.rb | 46 +++++++++++-------- .../nextcloud/create_folder_command.rb | 28 ++++++----- .../nextcloud/download_link_query.rb | 32 +++++++------ .../nextcloud/file_info_query.rb | 28 +++++------ .../nextcloud/files_info_query.rb | 30 ++++++------ .../nextcloud/files_query.rb | 40 ++++++++-------- .../nextcloud/group_users_query.rb | 23 +++++----- .../internal/delete_entity_command.rb | 18 ++++---- .../nextcloud/internal/propfind_query.rb | 36 ++++++++------- .../remove_user_from_group_command.rb | 33 ++++++------- .../nextcloud/rename_file_command.rb | 42 ++++++++++++----- .../nextcloud/set_permissions_command.rb | 30 ++++++------ .../storage_interaction/nextcloud/util.rb | 4 ++ .../copy_template_folder_command_spec.rb | 1 + .../nextcloud/files_info_query_spec.rb | 2 +- .../redirect_uri_from_state_service_spec.rb | 1 - spec/support/webmock.rb | 2 + 20 files changed, 252 insertions(+), 188 deletions(-) diff --git a/Gemfile b/Gemfile index 7d82c3b1ce69..1f8441c5bbc7 100644 --- a/Gemfile +++ b/Gemfile @@ -217,6 +217,8 @@ gem 'factory_bot_rails', '~> 6.2.0', require: false gem 'turbo-rails', "~> 1.1" +gem 'httpx' + group :test do gem 'launchy', '~> 2.5.0' gem 'rack-test', '~> 2.1.0' diff --git a/Gemfile.lock b/Gemfile.lock index 91961a4e970f..952e33cedbfc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -549,8 +549,11 @@ GEM htmlbeautifier (1.4.2) htmldiff (0.0.1) htmlentities (4.3.4) + http-2-next (1.0.1) http_parser.rb (0.6.0) httpclient (2.8.3) + httpx (1.0.2) + http-2-next (>= 1.0.1) i18n (1.14.1) concurrent-ruby (~> 1.0) i18n-js (4.2.3) @@ -1078,6 +1081,7 @@ DEPENDENCIES grids! html-pipeline (~> 2.14.0) htmldiff + httpx i18n-js (~> 4.2.3) json_schemer (~> 2.0.0) json_spec (~> 1.1.4) @@ -1207,4 +1211,4 @@ RUBY VERSION ruby 3.2.1p31 BUNDLED WITH - 2.4.7 + 2.4.6 diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb index 72bfc5cdc154..a36cfb77b2a5 100644 --- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb +++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb @@ -41,19 +41,23 @@ def self.call(storage:, user:, group: storage.group) end def call(user:, group: @group) - response = Util.http(@uri).post( - Util.join_uri_path(@uri.path, 'ocs/v1.php/cloud/users', CGI.escapeURIComponent(user), 'groups'), - "groupid=#{CGI.escapeURIComponent(group)}", - Util - .basic_auth_header(@username, @password) - .merge( - 'OCS-APIRequest' => 'true' - ) - ) + response = Util + .httpx + .basic_auth(@username, @password) + .with(headers: { 'OCS-APIRequest' => 'true' }) + .post( + Util.join_uri_path( + @uri, + 'ocs/v1.php/cloud/users', + CGI.escapeURIComponent(user), + 'groups' + ), + form: { "groupid" => CGI.escapeURIComponent(group) }, + ) - case response - when Net::HTTPSuccess - statuscode = Nokogiri::XML(response.body).xpath('/ocs/meta/statuscode').text + case response.status + when 200 + statuscode = Nokogiri::XML(response.body.to_s).xpath('/ocs/meta/statuscode').text case statuscode when "100" ServiceResult.success(message: "User has been added successfully") @@ -68,13 +72,13 @@ def call(user:, group: @group) when "105" Util.error(:error, "Failed to add user to group") end - when Net::HTTPMethodNotAllowed + when 405 Util.error(:not_allowed) - when Net::HTTPUnauthorized + when 401 Util.error(:unauthorized) - when Net::HTTPNotFound + when 404 Util.error(:not_found) - when Net::HTTPConflict + when 409 Util.error(:conflict) else Util.error(:error) 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 b41c18b35b73..f4ea5b8916f3 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 @@ -60,14 +60,14 @@ def build_origin_paths escaped_username = CGI.escapeURIComponent(@username) source = Util.join_uri_path( - @uri.path, + @uri, "remote.php/dav/files", escaped_username, Util.escape_path(input[:source_path]) ) destination = Util.join_uri_path( - @uri.path, + @uri, "remote.php/dav/files", escaped_username, Util.escape_path(input[:destination_path]) @@ -82,14 +82,17 @@ def validate_destination request = Net::HTTP::Head.new(urls[:destination_url]) request.initialize_http_header Util.basic_auth_header(@username, @password) - response = Util.http(@uri).request(request) + response = Util + .httpx + .basic_auth(@username, @password) + .head(urls[:destination_url]) - case response - when Net::HTTPSuccess + case response.status + when 200 Util.error(:conflict, 'Destination folder already exists.') - when Net::HTTPUnauthorized + when 401 Util.error(:unauthorized, "unauthorized (validate_destination)") - when Net::HTTPNotFound + when 404 ServiceResult.success(result: urls) else Util.error(:unknown, "Unexpected response (validate_destination): #{response.code}", response) @@ -100,24 +103,27 @@ def validate_destination # rubocop:disable Metrics/AbcSize def copy_folder ->(urls) do - headers = Util.basic_auth_header(@username, @password) - headers['Destination'] = urls[:destination_url] - headers['Depth'] = 'infinity' - - request = Net::HTTP::Copy.new(urls[:source_url], headers) - response = Util.http(@uri).request(request) - - case response - when Net::HTTPCreated + response = Util + .httpx + .basic_auth(@username, @password) + .request("COPY", + urls[:source_url], + headers: { + 'Destination' => urls[:destination_url], + 'Depth' => 'infinity', + }) + + case response.status + when 201 ServiceResult.success(message: 'Folder was successfully copied') - when Net::HTTPUnauthorized + when 401 Util.error(:unauthorized, "Unauthorized (copy_folder)") - when Net::HTTPNotFound + when 404 Util.error(:not_found, "Project folder not found (copy_folder)") - when Net::HTTPConflict + when 409 Util.error(:conflict, Util.error_text_from_response(response)) else - Util.error(:unknown, "Unexpected response (copy_folder): #{response.code}", response) + Util.error(:unknown, "Unexpected response (copy_folder): #{response.status}", response) end end 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 363322e8111a..68df18716aa0 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 @@ -42,26 +42,32 @@ def self.call(storage:, folder_path:) # rubocop:disable Metrics/AbcSize def call(folder_path:) - response = Util.http(@uri).mkcol( - Util.join_uri_path(@uri.path, "remote.php/dav/files", CGI.escapeURIComponent(@username), Util.escape_path(folder_path)), - nil, - Util.basic_auth_header(@username, @password) - ) + response = Util + .httpx + .basic_auth(@username, @password) + .mkcol( + Util.join_uri_path( + @uri, + "remote.php/dav/files", + CGI.escapeURIComponent(@username), + Util.escape_path(folder_path) + ) + ) - case response - when Net::HTTPSuccess + case response.status + when 201 ServiceResult.success(message: 'Folder was successfully created.') - when Net::HTTPMethodNotAllowed + when 405 if Util.error_text_from_response(response) == 'The resource you tried to create already exists' ServiceResult.success(message: 'Folder already exists.') else Util.error(:not_allowed) end - when Net::HTTPUnauthorized + when 401 Util.error(:unauthorized) - when Net::HTTPNotFound + when 404 Util.error(:not_found) - when Net::HTTPConflict + when 409 Util.error(:conflict, Util.error_text_from_response(response)) else Util.error(:error) diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb index e9a8c56ed694..186645748a9b 100644 --- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb +++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb @@ -45,22 +45,24 @@ def self.call(storage:, user:, file_link:) def call(user:, file_link:) result = Util.token(user:, configuration: @configuration) do |token| service_result = begin - response = Util.http(@uri).post( - Util.join_uri_path(@uri.path, '/ocs/v2.php/apps/dav/api/v1/direct'), - { fileId: file_link.origin_id }.to_json, - { - 'Authorization' => "Bearer #{token.access_token}", - 'OCS-APIRequest' => 'true', - 'Accept' => 'application/json', - 'Content-Type' => 'application/json' - } - ) - case response - when Net::HTTPSuccess + response = Util + .httpx + .post( + Util.join_uri_path(@uri, '/ocs/v2.php/apps/dav/api/v1/direct'), + json: { fileId: file_link.origin_id }, + headers: { + 'Authorization' => "Bearer #{token.access_token}", + 'OCS-APIRequest' => 'true', + 'Accept' => 'application/json', + 'Content-Type' => 'application/json' + } + ) + case response.status + when 200 ServiceResult.success(result: response) - when Net::HTTPNotFound + when 404 Util.error(:not_found, 'Outbound request destination not found!', response) - when Net::HTTPUnauthorized + when 401 Util.error(:unauthorized, 'Outbound request not authorized!', response) else Util.error(:error, 'Outbound request failed!') @@ -72,7 +74,7 @@ def call(user:, file_link:) if resp.body.blank? Util.error(:unauthorized, 'Outbound request not authorized!') else - ServiceResult.success(result: resp.body) + ServiceResult.success(result: resp.body.to_s) end end end diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb index 85d4d8384b85..3f1a544c640e 100644 --- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb +++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb @@ -52,21 +52,23 @@ def call(user:, file_id:) private def file_info(file_id, token) - response = Util.http(@uri).get( - Util.join_uri_path(@uri.path, FILE_INFO_PATH, file_id), - { - 'Authorization' => "Bearer #{token.access_token}", - 'Accept' => 'application/json', - 'OCS-APIRequest' => 'true' - } - ) - - case response - when Net::HTTPSuccess + response = Util + .httpx + .get( + Util.join_uri_path(@uri, FILE_INFO_PATH, file_id), + { + 'Authorization' => "Bearer #{token.access_token}", + 'Accept' => 'application/json', + 'OCS-APIRequest' => 'true' + } + ) + + case response.status + when 200 ServiceResult.success(result: response.body) - when Net::HTTPNotFound + when 404 Util.error(:not_found, 'Outbound request destination not found!', response) - when Net::HTTPUnauthorized + when 401 Util.error(:unauthorized, 'Outbound request not authorized!', response) else Util.error(:error, 'Outbound request failed!') diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb index adc858f0f7e5..70e345055589 100644 --- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb +++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb @@ -60,22 +60,22 @@ def call(user:, file_ids: []) private def files_info(file_ids, token) - response = Util.http(@uri).post( - Util.join_uri_path(@uri.path, FILES_INFO_PATH), - { fileIds: file_ids }.to_json, - { - 'Authorization' => "Bearer #{token.access_token}", - 'Accept' => 'application/json', - 'Content-Type' => 'application/json', - 'OCS-APIRequest' => 'true' - } - ) - case response - when Net::HTTPSuccess - ServiceResult.success(result: response.body) - when Net::HTTPNotFound + httpx = HTTPX.with(headers: + { + 'Authorization' => "Bearer #{token.access_token}", + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + 'OCS-APIRequest' => 'true' + } + ) + response = httpx.post(Util.join_uri_path(@uri.to_s, FILES_INFO_PATH), json: { fileIds: file_ids }) + + case response.status + when 200 + ServiceResult.success(result: response.body.to_s) + when 404 Util.error(:not_found, 'Outbound request destination not found!', response) - when Net::HTTPUnauthorized + when 401 Util.error(:unauthorized, 'Outbound request not authorized!', response) else Util.error(:error, 'Outbound request failed!') diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb index 4c22a48461b1..d207b192823d 100644 --- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb +++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb @@ -40,24 +40,26 @@ def self.call(storage:, user:, folder:) # rubocop:disable Metrics/AbcSize def call(user:, folder:) result = Util.token(user:, configuration: @configuration) do |token| - base_path = Util.join_uri_path(@uri.path, "remote.php/dav/files") - @location_prefix = Util.join_uri_path(base_path, token.origin_user_id.gsub(' ', '%20')) - - response = Util.http(@uri).propfind( - Util.join_uri_path(base_path, CGI.escapeURIComponent(token.origin_user_id), requested_folder(folder)), - requested_properties, - { - 'Depth' => '1', - 'Authorization' => "Bearer #{token.access_token}" - } - ) - - case response - when Net::HTTPSuccess + @location_prefix = Util.join_uri_path(@uri.path, "remote.php/dav/files", token.origin_user_id.gsub(' ', '%20')) + + response = Util + .httpx + .request( + "PROPFIND", + Util.join_uri_path(@uri, "remote.php/dav/files", CGI.escapeURIComponent(token.origin_user_id), requested_folder(folder)), + xml: requested_properties, + headers: { + 'Depth' => '1', + 'Authorization' => "Bearer #{token.access_token}" + } + ) + + case response.status + when 207 ServiceResult.success(result: response.body) - when Net::HTTPNotFound + when 404 Util.error(:not_found) - when Net::HTTPUnauthorized + when 401 Util.error(:unauthorized) else Util.error(:error) @@ -105,9 +107,9 @@ def storage_files(response) .to_a parent, *files = - a.map do |file_element| - storage_file(file_element) - end + a.map do |file_element| + storage_file(file_element) + end ::Storages::StorageFiles.new(files, parent, ancestors(parent.location)) end diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb index b3502e58af67..94012192239a 100644 --- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb +++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb @@ -42,23 +42,24 @@ def self.call(storage:, group: storage.group) # rubocop:disable Metrics/AbcSize def call(group:) - response = Util.http(@uri).get( - Util.join_uri_path(@uri.path, "ocs/v1.php/cloud/groups", CGI.escapeURIComponent(group)), - Util.basic_auth_header(@username, @password).merge('OCS-APIRequest' => 'true') - ) - case response - when Net::HTTPSuccess - group_users = Nokogiri::XML(response.body) + response = Util + .httpx + .basic_auth(@username, @password) + .with(headers: {'OCS-APIRequest' => 'true'}) + .get(Util.join_uri_path(@uri, "ocs/v1.php/cloud/groups", CGI.escapeURIComponent(group))) + case response.status + when 200 + group_users = Nokogiri::XML(response.body.to_s) .xpath('/ocs/data/users/element') .map(&:text) ServiceResult.success(result: group_users) - when Net::HTTPMethodNotAllowed + when 405 Util.error(:not_allowed) - when Net::HTTPUnauthorized + when 401 Util.error(:unauthorized) - when Net::HTTPNotFound + when 404 Util.error(:not_found) - when Net::HTTPConflict + when 409 Util.error(:conflict, error_text_from_response(response)) else Util.error(:error) diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb index 04c7e8ab72e6..b2a65fdd6333 100644 --- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb +++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb @@ -32,7 +32,7 @@ class DeleteEntityCommand def initialize(storage) @uri = storage.uri - @base_path = UTIL.join_uri_path(@uri.path, "remote.php/dav/files", CGI.escapeURIComponent(storage.username)) + @base_path = @username = storage.username @password = storage.password end @@ -42,17 +42,17 @@ def self.call(storage:, location:) end def call(location:) - response = UTIL.http(@uri).delete( - UTIL.join_uri_path(@base_path, UTIL.escape_path(location)), - UTIL.basic_auth_header(@username, @password) - ) + response = UTIL + .httpx + .basic_auth(@username, @password) + .delete(UTIL.join_uri_path(@uri, "remote.php/dav/files", CGI.escapeURIComponent(@username), UTIL.escape_path(location))) - case response - when Net::HTTPSuccess + case response.status + when 204 ServiceResult.success - when Net::HTTPNotFound + when 404 UTIL.error(:not_found) - when Net::HTTPUnauthorized + when 401 UTIL.error(:unauthorized) else UTIL.error(:error) diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb index 8789e410e0f6..2638ed797bdd 100644 --- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb +++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb @@ -90,20 +90,24 @@ def call(depth:, path:, props:) end end.to_xml - response = UTIL.http(@uri).propfind( - UTIL.join_uri_path( - @uri.path, - 'remote.php/dav/files', - CGI.escapeURIComponent(@username), - UTIL.escape_path(path) - ), - body, - UTIL.basic_auth_header(@username, @password).merge('Depth' => depth) - ) + response = UTIL + .httpx + .basic_auth(@username, @password) + .with(headers: { "Depth" => depth}) + .request( + "PROPFIND", + UTIL.join_uri_path( + @uri, + 'remote.php/dav/files', + CGI.escapeURIComponent(@username), + UTIL.escape_path(path) + ), + xml: body, + ) - case response - when Net::HTTPSuccess - doc = Nokogiri::XML response.body + case response.status + when 200 + doc = Nokogiri::XML(response.body.to_s) result = {} doc.xpath('/d:multistatus/d:response').each do |resource_section| resource = CGI.unescape(resource_section.xpath("d:href").text.strip) @@ -119,11 +123,11 @@ def call(depth:, path:, props:) end ServiceResult.success(result:) - when Net::HTTPMethodNotAllowed + when 405 UTIL.error(:not_allowed) - when Net::HTTPUnauthorized + when 401 UTIL.error(:unauthorized) - when Net::HTTPNotFound + when 404 UTIL.error(:not_found) else UTIL.error(:error) diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb index 87dfb63a7ec2..fe2214cf33ae 100644 --- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb +++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb @@ -41,19 +41,20 @@ def self.call(storage:, user:, group: storage.group) # rubocop:disable Metrics/AbcSize def call(user:, group: @group) - response = Util.http(@uri).delete( - Util.join_uri_path(@uri.path, - "ocs/v1.php/cloud/users", - CGI.escapeURIComponent(user), - "groups?groupid=#{CGI.escapeURIComponent(group)}"), - Util.basic_auth_header(@username, @password).merge( - 'OCS-APIRequest' => 'true' - ) - ) + response = Util + .httpx + .basic_auth(@username, @password) + .with(headers: { 'OCS-APIRequest' => 'true' }) + .delete( + Util.join_uri_path(@uri, + "ocs/v1.php/cloud/users", + CGI.escapeURIComponent(user), + "groups?groupid=#{CGI.escapeURIComponent(group)}") + ) - case response - when Net::HTTPSuccess - statuscode = Nokogiri::XML(response.body).xpath('/ocs/meta/statuscode').text + case response.status + when 200 + statuscode = Nokogiri::XML(response.body.to_s).xpath('/ocs/meta/statuscode').text case statuscode when "100" ServiceResult.success(message: "User has been removed from group") @@ -69,13 +70,13 @@ def call(user:, group: @group) message = Nokogiri::XML(response.body).xpath('/ocs/meta/message').text Util.error(:error, "Failed to remove user #{user} from group #{group}: #{message}") end - when Net::HTTPMethodNotAllowed + when 405 Util.error(:not_allowed) - when Net::HTTPUnauthorized + when 401 Util.error(:unauthorized) - when Net::HTTPNotFound + when 404 Util.error(:not_found) - when Net::HTTPConflict + when 409 Util.error(:conflict) else Util.error(:error) diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb index f9f2d7b1fc6f..2f6d955c96aa 100644 --- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb +++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb @@ -32,7 +32,7 @@ class RenameFileCommand def initialize(storage) @uri = storage.uri - @base_path = Util.join_uri_path(@uri.path, "remote.php/dav/files", CGI.escapeURIComponent(storage.username)) + @base_path = Util.join_uri_path(@uri, "remote.php/dav/files", CGI.escapeURIComponent(storage.username)) @username = storage.username @password = storage.password end @@ -42,23 +42,43 @@ def self.call(storage:, source:, target:) end def call(source:, target:) - response = Util.http(@uri).move( - Util.join_uri_path(@base_path, Util.escape_path(source)), - Util.basic_auth_header(@username, @password).merge( - 'Destination' => Util.join_uri_path(@base_path, Util.escape_path(target)) - ) - ) + response = Util + .httpx + .basic_auth(@username, @password) + .request( + "MOVE", + Util.join_uri_path(@base_path, Util.escape_path(source)), + headers: { + 'Destination' => Util.join_uri_path(@uri.path, "remote.php/dav/files", CGI.escapeURIComponent(@username), Util.escape_path(target)) + } + ) - case response - when Net::HTTPSuccess + case response.status + when 201 ServiceResult.success - when Net::HTTPNotFound + when 404 Util.error(:not_found) - when Net::HTTPUnauthorized + when 401 Util.error(:unauthorized) else Util.error(:error) end end end + + def let_the_show_begin(modul) + begin + const = Object.const_get(modul) + puts 'START' + puts const + if [Module, Class].include?(const.class) + const.constants.each do |constant| + let_the_show_begin("#{modul}::#{constant}") + end + end + puts 'END' + rescue => e + puts "ERORR: #{e.message}" + end + end end diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb index 8530e81fbd59..2447e3fbbd15 100644 --- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb +++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb @@ -77,26 +77,30 @@ def call(path:, permissions:) end end.to_xml - response = Util.http(@uri).proppatch( - Util.join_uri_path(@uri.path, - "remote.php/dav/files", - CGI.escapeURIComponent(@username), - Util.escape_path(path)), - body, - Util.basic_auth_header(@username, @password) - ) + response = Util + .httpx + .with(debug_level: 1, debug: $stdout) + .basic_auth(@username, @password) + .request( + "PROPPATCH", + Util.join_uri_path(@uri, + "remote.php/dav/files", + CGI.escapeURIComponent(@username), + Util.escape_path(path)), + xml: body + ) - case response - when Net::HTTPSuccess - doc = Nokogiri::XML(response.body) + case response.status + when 207 + doc = Nokogiri::XML(response.body.to_s) if doc.xpath("/d:multistatus/d:response/d:propstat[d:status[text() = 'HTTP/1.1 200 OK']]/d:prop/nc:acl-list").present? ServiceResult.success else Util.error(:error, "nc:acl properly has not been set for #{path}") end - when Net::HTTPNotFound + when 404 Util.error(:not_found) - when Net::HTTPUnauthorized + when 401 Util.error(:unauthorized) else Util.error(:error) 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 98ff4eb6c45f..d9a7d8f87af7 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 @@ -75,6 +75,10 @@ def http(uri) http end + def httpx + HTTPX.plugin(:basic_auth).plugin(:webdav) + end + def error_text_from_response(response) Nokogiri::XML(response.body).xpath("//s:message").text end diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command_spec.rb index 2ab785810b10..431d79de2984 100644 --- a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command_spec.rb +++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command_spec.rb @@ -116,6 +116,7 @@ expect(result).to be_failure expect(result.errors.code).to eq(:conflict) + expect(result.errors.log_message).to eq("The destination node is not found") end end end diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/files_info_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/files_info_query_spec.rb index d40f992e6798..a18ff41bc574 100644 --- a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/files_info_query_spec.rb +++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/files_info_query_spec.rb @@ -31,7 +31,7 @@ require 'spec_helper' require_module_spec_helper -RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::FilesInfoQuery, :vcr, :webmock do +RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::FilesInfoQuery, :webmock do using Storages::Peripherals::ServiceResultRefinements let(:user) { create(:user) } diff --git a/spec/services/oauth_clients/redirect_uri_from_state_service_spec.rb b/spec/services/oauth_clients/redirect_uri_from_state_service_spec.rb index 9323d81bb26d..3927cee14466 100644 --- a/spec/services/oauth_clients/redirect_uri_from_state_service_spec.rb +++ b/spec/services/oauth_clients/redirect_uri_from_state_service_spec.rb @@ -27,7 +27,6 @@ #++ require 'spec_helper' -require 'webmock/rspec' RSpec.describe OAuthClients::RedirectUriFromStateService, type: :model do let(:state) { 'asdf123425' } diff --git a/spec/support/webmock.rb b/spec/support/webmock.rb index ce192bf81399..3b6ccb9bb112 100644 --- a/spec/support/webmock.rb +++ b/spec/support/webmock.rb @@ -25,7 +25,9 @@ # # See COPYRIGHT and LICENSE files for more details. #++ + require 'webmock/rspec' +require "httpx/adapters/webmock" RSpec.configure do |config| config.before(:suite) do