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
[#53622] use authentication in files info queries
Eric Schubert905a5dd7f679
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.rbindex f01c030fbf7..2c65be434ee 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@@ -33,41 +33,37 @@ module Storages::Peripherals::StorageInteraction::Nextcloudusing Storages::Peripherals::ServiceResultRefinementsFILE_INFO_PATH = "ocs/v1.php/apps/integration_openproject/fileinfo"+ Auth = ::Storages::Peripherals::StorageInteraction::Authentication- def initialize(storage)- @uri = storage.uri- @configuration = storage.oauth_configuration+ def self.call(storage:, auth_strategy:, file_id:)+ new(storage).call(auth_strategy:, file_id:)end- def self.call(storage:, user:, file_id:)- new(storage).call(user:, file_id:)+ def initialize(storage)+ @storage = storageend- def call(user:, file_id:)- Util.token(user:, configuration: @configuration) do |token|- file_info(file_id, token).map(&parse_json) >> handle_failure >> create_storage_file_info+ def call(auth_strategy:, file_id:)+ Auth[auth_strategy].call(storage: @storage, http_options: Util.ocs_api_request) do |http|+ file_info(http, file_id).map(&parse_json) >> handle_failure >> create_storage_file_infoendendprivate- def file_info(file_id, token)- response = OpenProject- .httpx- .with(headers: { "Authorization" => "Bearer #{token.access_token}",- "Accept" => "application/json",- "OCS-APIRequest" => "true" })- .get(Util.join_uri_path(@uri, FILE_INFO_PATH, file_id))+ def file_info(http, file_id)+ response = http.get(Util.join_uri_path(@storage.uri, FILE_INFO_PATH, file_id))+ error_data = Storages::StorageErrorData.new(source: self.class, payload: response)case responsein { status: 200..299 }ServiceResult.success(result: response.body)in { status: 404 }- Util.error(:not_found, "Outbound request destination not found!", response)+ Util.error(:not_found, "Outbound request destination not found!", error_data)in { status: 401 }- Util.error(:unauthorized, "Outbound request not authorized!", response)+ Util.error(:unauthorized, "Outbound request not authorized!", error_data)else- Util.error(:error, "Outbound request failed!")+ Util.error(:error, "Outbound request failed!", error_data)endend@@ -81,15 +77,17 @@ module Storages::Peripherals::StorageInteraction::Nextclouddef handle_failure->(response_object) do+ error_data = Storages::StorageErrorData.new(source: self.class, payload: response_object)+case response_object.ocs.data.statuscodewhen 200..299ServiceResult.success(result: response_object)when 403- Util.error(:forbidden, "Access to storage file forbidden!", response_object)+ Util.error(:forbidden, "Access to storage file forbidden!", error_data)when 404- Util.error(:not_found, "Storage file not found!", response_object)+ Util.error(:not_found, "Storage file not found!", error_data)else- Util.error(:error, "Outbound request failed!", response_object)+ Util.error(:error, "Outbound request failed!", error_data)endendenddiff --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.rbindex 929e27e06aa..b5bba8ab6f4 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@@ -33,17 +33,17 @@ module Storages::Peripherals::StorageInteraction::Nextcloudusing Storages::Peripherals::ServiceResultRefinementsFILES_INFO_PATH = "ocs/v1.php/apps/integration_openproject/filesinfo"+ Auth = ::Storages::Peripherals::StorageInteraction::Authentication- def initialize(storage)- @uri = storage.uri- @configuration = storage.oauth_configuration+ def self.call(storage:, auth_strategy:, file_ids:)+ new(storage).call(auth_strategy:, file_ids:)end- def self.call(storage:, user:, file_ids: [])- new(storage).call(user:, file_ids:)+ def initialize(storage)+ @storage = storageend- def call(user:, file_ids: [])+ def call(auth_strategy:, file_ids:)if file_ids.nil?return Util.error(:error, "File IDs can not be nil", file_ids)end@@ -52,32 +52,26 @@ module Storages::Peripherals::StorageInteraction::Nextcloudreturn ServiceResult.success(result: [])end- Util.token(user:, configuration: @configuration) do |token|- files_info(file_ids, token).map(&parse_json) >> handle_failure >> create_storage_file_infos+ Auth[auth_strategy].call(storage: @storage, http_options: Util.ocs_api_request) do |http|+ files_info(http, file_ids).map(&parse_json) >> handle_failure >> create_storage_file_infosendendprivate- def files_info(file_ids, token)- response = OpenProject- .httpx- .with(headers: { "Authorization" => "Bearer #{token.access_token}",- "Accept" => "application/json",- "Content-Type" => "application/json",- "OCS-APIRequest" => "true" })- .post(Util.join_uri_path(@uri.to_s, FILES_INFO_PATH),- json: { fileIds: file_ids })+ def files_info(http, file_ids)+ response = http.post(Util.join_uri_path(@storage.uri, FILES_INFO_PATH), json: { fileIds: file_ids })+ error_data = Storages::StorageErrorData.new(source: self.class, payload: response)case responsein { status: 200..299 }- ServiceResult.success(result: response.body.to_s)+ ServiceResult.success(result: response.body)in { status: 404 }- Util.error(:not_found, "Outbound request destination not found!", response)+ Util.error(:not_found, "Outbound request destination not found!", error_data)in { status: 401 }- Util.error(:unauthorized, "Outbound request not authorized!", response)+ Util.error(:unauthorized, "Outbound request not authorized!", error_data)else- Util.error(:error, "Outbound request failed!", response)+ Util.error(:error, "Outbound request failed!", error_data)endend@@ -94,7 +88,8 @@ module Storages::Peripherals::StorageInteraction::Nextcloudif response_object.ocs.meta.status == "ok"ServiceResult.success(result: response_object)else- Util.error(:error, "Outbound request failed!", response_object)+ error_data = Storages::StorageErrorData.new(source: self.class, payload: response_object)+ Util.error(:error, "Outbound request failed!", error_data)endendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/file_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/file_info_query.rbindex 379bed8f693..4d110509cc6 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/file_info_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/file_info_query.rb@@ -34,9 +34,10 @@ module Storagesmodule OneDriveclass FileInfoQueryFIELDS = %w[id name fileSystemInfo file folder size createdBy lastModifiedBy parentReference].freeze+ Auth = ::Storages::Peripherals::StorageInteraction::Authentication- def self.call(storage:, user:, file_id:)- new(storage).call(user:, file_id:)+ def self.call(storage:, auth_strategy:, file_id:)+ new(storage).call(auth_strategy:, file_id:)enddef initialize(storage)@@ -44,18 +45,18 @@ module Storages@delegate = Internal::DriveItemQuery.new(storage)end- def call(user:, file_id:)+ def call(auth_strategy:, file_id:)if file_id.nil?return ServiceResult.failure(result: :error,errors: ::Storages::StorageError.new(code: :error,data: StorageErrorData.new(source: self.class),- log_message: 'File ID can not be nil')+ log_message: "File ID can not be nil"))end- Util.using_user_token(@storage, user) do |token|- @delegate.call(token:, drive_item_id: file_id, fields: FIELDS).map(&storage_file_infos)+ Auth[auth_strategy].call(storage: @storage) do |http|+ @delegate.call(http:, drive_item_id: file_id, fields: FIELDS).map(&storage_file_infos)endend@@ -65,7 +66,7 @@ module Storagesdef storage_file_infos->(json) doStorageFileInfo.new(- status: 'ok',+ status: "ok",status_code: 200,id: json[:id],name: json[:name],diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_info_query.rbindex 59f2dccebdd..f32ddc46187 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_info_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_info_query.rb@@ -35,25 +35,24 @@ module Storagesclass FilesInfoQueryusing ServiceResultRefinements- def self.call(storage:, user:, file_ids: [])- new(storage).call(user:, file_ids:)+ def self.call(storage:, auth_strategy:, file_ids: [])+ new(storage).call(auth_strategy:, file_ids:)enddef initialize(storage)@storage = storage- @uri = storage.uriend- def call(user:, file_ids:)+ def call(auth_strategy:, file_ids:)if file_ids.nil?return ServiceResult.failure(result: :error,- errors: ::Storages::StorageError.new(code: :error, log_message: 'File IDs can not be nil')+ errors: ::Storages::StorageError.new(code: :error, log_message: "File IDs can not be nil"))endresult = file_ids.map do |file_id|- file_info_result = FileInfoQuery.call(storage: @storage, user:, file_id:)+ file_info_result = FileInfoQuery.call(storage: @storage, auth_strategy:, file_id:)if file_info_result.failure? &&file_info_result.error_source.is_a?(::OAuthClients::ConnectionManager)# errors in the connection manager must short circuit the query and return the errordiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/children_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/children_query.rbindex 2acd8a2e370..d1bf993579a 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/children_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/children_query.rb@@ -34,11 +34,7 @@ module Storagesmodule OneDrivemodule Internalclass ChildrenQuery- UTIL = ::Storages::Peripherals::StorageInteraction::OneDrive::Util-- def self.call(storage:, http:, folder:, fields: [])- new(storage).call(http:, folder:, fields:)- end+ Util = ::Storages::Peripherals::StorageInteraction::OneDrive::Utildef initialize(storage)@storage = storage@@ -47,7 +43,7 @@ module Storagesdef call(http:, folder:, fields: [])select_url_query = if fields.empty?- ''+ ""else"?$select=#{fields.join(',')}"end@@ -58,7 +54,7 @@ module Storagesprivatedef make_children_request(folder, http, select_url_query)- response = http.get(UTIL.join_uri_path(@uri, uri_path_for(folder) + select_url_query))+ response = http.get(Util.join_uri_path(@uri, uri_path_for(folder) + select_url_query))handle_responses(response)end@@ -68,13 +64,13 @@ module StoragesServiceResult.success(result: response.json(symbolize_keys: true))in { status: 404 }ServiceResult.failure(result: :not_found,- errors: UTIL.storage_error(response:, code: :not_found, source: self))+ errors: Util.storage_error(response:, code: :not_found, source: self))in { status: 403 }ServiceResult.failure(result: :forbidden,- errors: UTIL.storage_error(response:, code: :forbidden, source: self))+ errors: Util.storage_error(response:, code: :forbidden, source: self))in { status: 401 }ServiceResult.failure(result: :unauthorized,- errors: UTIL.storage_error(response:, code: :unauthorized, source: self))+ errors: Util.storage_error(response:, code: :unauthorized, source: self))elsedata = ::Storages::StorageErrorData.new(source: self.class, payload: response)ServiceResult.failure(result: :error, errors: ::Storages::StorageError.new(code: :error, data:))diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rbindex cb52988648f..9ddf3b51624 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb@@ -34,34 +34,26 @@ module Storagesmodule OneDrivemodule Internalclass DriveItemQuery- UTIL = ::Storages::Peripherals::StorageInteraction::OneDrive::Util-- def self.call(storage:, token:, drive_item_id:, fields: [])- new(storage).call(token:, drive_item_id:, fields:)- end+ Util = ::Storages::Peripherals::StorageInteraction::OneDrive::Utildef initialize(storage)@storage = storage- @uri = storage.uriend- def call(token:, drive_item_id:, fields: [])+ def call(http:, drive_item_id:, fields: [])select_url_query = if fields.empty?- ''+ ""else"?$select=#{fields.join(',')}"end- make_file_request(drive_item_id, token, select_url_query)+ make_file_request(drive_item_id, http, select_url_query)endprivate- def make_file_request(drive_item_id, token, select_url_query)- response = OpenProject.httpx.get(- UTIL.join_uri_path(@uri, uri_path_for(drive_item_id) + select_url_query),- headers: { 'Authorization' => "Bearer #{token.access_token}" }- )+ def make_file_request(drive_item_id, http, select_url_query)+ response = http.get(Util.join_uri_path(@storage.uri, uri_path_for(drive_item_id) + select_url_query))handle_responses(response)end@@ -71,13 +63,13 @@ module StoragesServiceResult.success(result: response.json(symbolize_keys: true))in { status: 404 }ServiceResult.failure(result: :not_found,- errors: UTIL.storage_error(response:, code: :not_found, source: self))+ errors: Util.storage_error(response:, code: :not_found, source: self))in { status: 403 }ServiceResult.failure(result: :forbidden,- errors: UTIL.storage_error(response:, code: :forbidden, source: self))+ errors: Util.storage_error(response:, code: :forbidden, source: self))in { status: 401 }ServiceResult.failure(result: :unauthorized,- errors: UTIL.storage_error(response:, code: :unauthorized, source: self))+ errors: Util.storage_error(response:, code: :unauthorized, source: self))elsedata = ::Storages::StorageErrorData.new(source: self.class, payload: response)ServiceResult.failure(result: :error, errors: ::Storages::StorageError.new(code: :error, data:))@@ -85,7 +77,11 @@ module Storagesenddef uri_path_for(file_id)- "/v1.0/drives/#{@storage.drive_id}/items/#{file_id}"+ if file_id == "/"+ "/v1.0/drives/#{@storage.drive_id}/root"+ else+ "/v1.0/drives/#{@storage.drive_id}/items/#{file_id}"+ endendendenddiff --git a/modules/storages/app/controllers/storages/project_storages_controller.rb b/modules/storages/app/controllers/storages/project_storages_controller.rbindex f210a5c2854..8a63558450d 100644--- a/modules/storages/app/controllers/storages/project_storages_controller.rb+++ b/modules/storages/app/controllers/storages/project_storages_controller.rb@@ -37,6 +37,7 @@ class Storages::ProjectStoragesController < ApplicationControllerbefore_action :find_project_by_project_idbefore_action :render_403, unless: -> { User.current.allowed_in_project?(:view_file_links, @project) }+ # rubocop:disable Metrics/AbcSizedef openif @object.project_folder_automatic?@storage = @object.storage@@ -44,9 +45,7 @@ class Storages::ProjectStoragesController < ApplicationControllerif @object.project_folder_id.present?::Storages::Peripherals::Registry.resolve("#{@storage.short_provider_type}.queries.file_info")- .call(storage: @storage,- user: current_user,- file_id: @object.project_folder_id)+ .call(storage: @storage, auth_strategy:, file_id: @object.project_folder_id).match(on_success: user_can_read_project_folder,on_failure: user_can_not_read_project_folder@@ -64,8 +63,16 @@ class Storages::ProjectStoragesController < ApplicationControllerendend+ # rubocop:enable Metrics/AbcSize+private+ def auth_strategy+ Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken+ .strategy+ .with_user(current_user)+ end+def user_can_read_project_folder->(_) dorespond_to do |format|diff --git a/modules/storages/app/services/projects/copy/file_links_dependent_service.rb b/modules/storages/app/services/projects/copy/file_links_dependent_service.rbindex dd8ce290da7..27b28a51eaa 100644--- a/modules/storages/app/services/projects/copy/file_links_dependent_service.rb+++ b/modules/storages/app/services/projects/copy/file_links_dependent_service.rb@@ -31,11 +31,11 @@module Projects::Copyclass FileLinksDependentService < Dependencydef self.human_name- I18n.t(:'projects.copy.work_package_file_links')+ I18n.t(:"projects.copy.work_package_file_links")enddef source_count- source.work_packages.joins(:file_links).count('file_links.id')+ source.work_packages.joins(:file_links).count("file_links.id")endprotected@@ -55,11 +55,11 @@ module Projects::Copytmp = state.copied_project_storages.find { |item| item["source"].storage_id == storage_id }- source_project_storage = tmp['source']- target_project_storage = tmp['target']+ source_project_storage = tmp["source"]+ target_project_storage = tmp["target"]storage = source_project_storage.storage- if source_project_storage.project_folder_mode == 'automatic'+ if source_project_storage.project_folder_mode == "automatic"files_info_query_result = files_info_query(storage:,file_ids: source_file_links.map(&:origin_id))folder_files_file_ids_deep_query_result = folder_files_file_ids_deep_query(@@ -71,7 +71,7 @@ module Projects::Copystorage_id: old_file_link.storage_id,creator_id: User.current.id,container_id: state.work_package_id_lookup[old_file_link.container_id.to_s],- container_type: 'WorkPackage',+ container_type: "WorkPackage",origin_name: old_file_link.origin_name,origin_mime_type: old_file_link.origin_mime_type}@@ -80,7 +80,7 @@ module Projects::Copy.find { |i| i.id.to_i == old_file_link.origin_id.to_i }.location- attributes['origin_id'] =+ attributes["origin_id"] =if source_project_storage.file_inside_project_folder?(original_file_location)new_file_location = original_file_location.gsub(source_project_storage.project_folder_path_escaped,@@ -99,7 +99,7 @@ module Projects::Copystorage_id: old_file_link.storage_id,creator_id: User.current.id,container_id: state.work_package_id_lookup[old_file_link.container_id.to_s],- container_type: 'WorkPackage',+ container_type: "WorkPackage",origin_name: old_file_link.origin_name,origin_mime_type: old_file_link.origin_mime_type,origin_id: old_file_link.origin_id@@ -116,7 +116,7 @@ module Projects::Copydef files_info_query(storage:, file_ids:)Storages::Peripherals::Registry.resolve("#{storage.short_provider_type}.queries.files_info")- .call(storage:, user: User.current, file_ids:)+ .call(storage:, auth_strategy:, file_ids:).on_failure { |r| add_error!("files_info_query", r.to_active_model_errors) }.resultend@@ -128,5 +128,13 @@ module Projects::Copy.on_failure { |r| add_error!("folder_files_file_ids_deep_query", r.to_active_model_errors) }.resultend++ private++ def auth_strategy+ Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken+ .strategy+ .with_user(User.current)+ endendenddiff --git a/modules/storages/app/services/storages/file_link_sync_service.rb b/modules/storages/app/services/storages/file_link_sync_service.rbindex c82fa26adc6..b8e5a5ff72b 100644--- a/modules/storages/app/services/storages/file_link_sync_service.rb+++ b/modules/storages/app/services/storages/file_link_sync_service.rb@@ -55,7 +55,7 @@ class Storages::FileLinkSyncServicestorage = ::Storages::Storage.find(storage_id)::Storages::Peripherals::Registry.resolve("#{storage.short_provider_type}.queries.files_info")- .call(storage:, user: @user, file_ids: file_links.map(&:origin_id))+ .call(storage:, auth_strategy:, file_ids: file_links.map(&:origin_id)).map { |file_infos| to_hash(file_infos) }.match(on_success: set_file_link_status(file_links),@@ -68,6 +68,12 @@ class Storages::FileLinkSyncService)end+ def auth_strategy+ Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken+ .strategy+ .with_user(@user)+ end+def to_hash(file_infos)file_infos.index_by { |file_info| file_info.id.to_s }.to_henddiff --git a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb b/modules/storages/lib/api/v3/storage_files/storage_files_api.rbindex 96b2250a2c4..3378e5e9f08 100644--- a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb+++ b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb@@ -38,7 +38,7 @@ module API::V3::StorageFileshelpers dodef validate_upload_request(body)if Storages::Storage::one_drive_without_ee_token?(@storage.provider_type)- log_message = 'The request can not be handled due to invalid or missing Enterprise token.'+ log_message = "The request can not be handled due to invalid or missing Enterprise token."return ServiceResult.failure(errors: Storages::StorageError.new(code: :missing_ee_token_for_one_drive, log_message:))end@@ -48,7 +48,7 @@ module API::V3::StorageFilesServiceResult.success(result: { file_name:, parent: }.transform_keys(&:to_s))elseServiceResult.failure(errors: Storages::StorageError.new(code: :bad_request,- log_message: 'Request body malformed!'))+ log_message: "Request body malformed!"))endend@@ -78,11 +78,11 @@ module API::V3::StorageFiles)end- route_param :file_id, type: String, desc: 'Storage file id' do+ route_param :file_id, type: String, desc: "Storage file id" doget doStorages::Peripherals::Registry.resolve("#{@storage.short_provider_type}.queries.file_info")- .call(storage: @storage, user: current_user, file_id: params[:file_id])+ .call(storage: @storage, auth_strategy:, file_id: params[:file_id]).map { |file_info| to_storage_file(file_info) }.match(on_success: ->(storage_file) {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.rbindex a9e61226e7b..ddee8abd7a0 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@@ -35,10 +35,12 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::FilesInfoQuusing Storages::Peripherals::ServiceResultRefinementslet(:user) { create(:user) }-let(:storage) docreate(: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)+ endsubject { described_class.new(storage) }@@ -48,7 +50,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::FilesInfoQucontext "without outbound request involved" docontext "with an empty array of file ids" doit "returns an empty array" do- result = subject.call(user:, file_ids: [])+ result = subject.call(auth_strategy:, file_ids: [])expect(result).to be_successexpect(result.result).to eq([])@@ -57,7 +59,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::FilesInfoQucontext "with nil" doit "returns an error" do- result = subject.call(user:, file_ids: nil)+ result = subject.call(auth_strategy:, file_ids: nil)expect(result).to be_failureexpect(result.result).to eq(:error)@@ -69,7 +71,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::FilesInfoQuvcr: "nextcloud/files_info_query_success" docontext "with an array of file ids" doit "must return an array of file information when called" do- result = subject.call(user:, file_ids:)+ result = subject.call(auth_strategy:, file_ids:)expect(result).to be_successresult.match(@@ -83,32 +85,13 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::FilesInfoQuendend- context "with outbound request not authorized",- vcr: "nextcloud/files_info_query_unauthorized" do- context "with an array of file ids" do- before do- token = build_stubbed(:oauth_client_token, oauth_client: storage.oauth_client)- allow(Storages::Peripherals::StorageInteraction::Nextcloud::Util)- .to receive(:token)- .and_yield(token)- end-- it "must return an error when called" do- subject.call(user:, file_ids:).match(- on_success: ->(file_infos) { fail "Expected failure, got #{file_infos}" },- on_failure: ->(error) { expect(error.code).to eq(:unauthorized) }- )- end- end- end-context "with outbound request not found" docontext "with a single file id",vcr: "nextcloud/files_info_query_not_found" dolet(:file_ids) { %w[1234] }it "returns an HTTP 200 with individual status code per file ID" do- subject.call(user:, file_ids:).match(+ subject.call(auth_strategy:, file_ids:).match(on_success: ->(file_infos) doexpect(file_infos.size).to eq(1)expect(file_infos.first.to_h).to include(status: "Not Found", status_code: 404)@@ -125,7 +108,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::FilesInfoQulet(:file_ids) { %w[182 1234] }it "returns an HTTP 200 with individual status code per file ID" do- subject.call(user:, file_ids:).match(+ subject.call(auth_strategy:, file_ids:).match(on_success: ->(file_infos) doexpect(file_infos.size).to eq(2)expect(file_infos.map(&:status_code)).to contain_exactly(403, 404)diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/file_info_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/file_info_query_spec.rbindex 50c756d7582..defacc3139a 100644--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/file_info_query_spec.rb+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/file_info_query_spec.rb@@ -28,7 +28,7 @@# See COPYRIGHT and LICENSE files for more details.#++-require 'spec_helper'+require "spec_helper"require_module_spec_helperRSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FileInfoQuery, :vcr, :webmock do@@ -36,21 +36,24 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FileInfoQuerlet(:user) { create(:user) }let(:storage) { create(:sharepoint_dev_drive_storage, oauth_client_token_user: user) }+ let(:auth_strategy) do+ Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken.strategy.with_user(user)+ endsubject { described_class.new(storage) }- describe '#call' do- it 'responds with correct parameters' do+ describe "#call" do+ it "responds with correct parameters" doexpect(described_class).to respond_to(:call)method = described_class.method(:call)- expect(method.parameters).to contain_exactly(%i[keyreq storage], %i[keyreq user], %i[keyreq file_id])+ expect(method.parameters).to contain_exactly(%i[keyreq storage], %i[keyreq auth_strategy], %i[keyreq file_id])end- context 'without outbound request involved' do- context 'with nil' do- it 'returns an error' do- result = subject.call(user:, file_id: nil)+ context "without outbound request involved" do+ context "with nil" do+ it "returns an error" do+ result = subject.call(auth_strategy:, file_id: nil)expect(result).to be_failureexpect(result.error_source).to eq(described_class)@@ -60,13 +63,13 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FileInfoQuerendend- context 'with outbound requests successful' do- context 'with a file id requested', vcr: 'one_drive/file_info_query_success_file' do- let(:file_id) { '01AZJL5PNCQCEBFI3N7JGZSX5AOX32Z3LA' }+ context "with outbound requests successful" do+ context "with a file id requested", vcr: "one_drive/file_info_query_success_file" do+ let(:file_id) { "01AZJL5PNCQCEBFI3N7JGZSX5AOX32Z3LA" }# rubocop:disable RSpec/ExampleLength- it 'must return the file information when called' do- result = subject.call(user:, file_id:)+ it "must return the file information when called" do+ result = subject.call(auth_strategy:, file_id:)expect(result).to be_successresult.match(@@ -74,21 +77,21 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FileInfoQuerexpect(file_info).to be_a(Storages::StorageFileInfo)expect(file_info.to_h).to eq({- status: 'ok',+ status: "ok",status_code: 200,- id: '01AZJL5PNCQCEBFI3N7JGZSX5AOX32Z3LA',- name: 'NextcloudHub.md',+ id: "01AZJL5PNCQCEBFI3N7JGZSX5AOX32Z3LA",+ name: "NextcloudHub.md",size: 1095,- mime_type: 'application/octet-stream',- created_at: Time.parse('2023-09-26T14:45:25Z'),- last_modified_at: Time.parse('2023-09-26T14:46:13Z'),- owner_name: 'Eric Schubert',- owner_id: '0a0d38a9-a59b-4245-93fa-0d2cf727f17a',- last_modified_by_name: 'Eric Schubert',- last_modified_by_id: '0a0d38a9-a59b-4245-93fa-0d2cf727f17a',+ mime_type: "application/octet-stream",+ created_at: Time.parse("2023-09-26T14:45:25Z"),+ last_modified_at: Time.parse("2023-09-26T14:46:13Z"),+ owner_name: "Eric Schubert",+ owner_id: "0a0d38a9-a59b-4245-93fa-0d2cf727f17a",+ last_modified_by_name: "Eric Schubert",+ last_modified_by_id: "0a0d38a9-a59b-4245-93fa-0d2cf727f17a",permissions: nil,trashed: false,- location: '/Folder/Subfolder/NextcloudHub.md'+ location: "/Folder/Subfolder/NextcloudHub.md"})end,on_failure: ->(error) { fail "Expected success, got #{error}" }@@ -97,12 +100,12 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FileInfoQuer# rubocop:enable RSpec/ExampleLengthend- context 'with a folder id requested', vcr: 'one_drive/file_info_query_success_folder' do- let(:file_id) { '01AZJL5PNQYF5NM3KWYNA3RJHJIB2XMMMB' }+ context "with a folder id requested", vcr: "one_drive/file_info_query_success_folder" do+ let(:file_id) { "01AZJL5PNQYF5NM3KWYNA3RJHJIB2XMMMB" }# rubocop:disable RSpec/ExampleLength- it 'must return the file information when called' do- result = subject.call(user:, file_id:)+ it "must return the file information when called" do+ result = subject.call(auth_strategy:, file_id:)expect(result).to be_successresult.match(@@ -110,21 +113,21 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FileInfoQuerexpect(file_info).to be_a(Storages::StorageFileInfo)expect(file_info.to_h).to eq({- status: 'ok',+ status: "ok",status_code: 200,- id: '01AZJL5PNQYF5NM3KWYNA3RJHJIB2XMMMB',- name: 'Ümlæûts',+ id: "01AZJL5PNQYF5NM3KWYNA3RJHJIB2XMMMB",+ name: "Ümlæûts",size: 18007,- mime_type: 'application/x-op-directory',- created_at: Time.parse('2023-10-09T15:26:32Z'),- last_modified_at: Time.parse('2023-10-09T15:26:32Z'),- owner_name: 'Eric Schubert',- owner_id: '0a0d38a9-a59b-4245-93fa-0d2cf727f17a',- last_modified_by_name: 'Eric Schubert',- last_modified_by_id: '0a0d38a9-a59b-4245-93fa-0d2cf727f17a',+ mime_type: "application/x-op-directory",+ created_at: Time.parse("2023-10-09T15:26:32Z"),+ last_modified_at: Time.parse("2023-10-09T15:26:32Z"),+ owner_name: "Eric Schubert",+ owner_id: "0a0d38a9-a59b-4245-93fa-0d2cf727f17a",+ last_modified_by_name: "Eric Schubert",+ last_modified_by_id: "0a0d38a9-a59b-4245-93fa-0d2cf727f17a",permissions: nil,trashed: false,- location: '/Folder/Ümlæûts'+ location: "/Folder/Ümlæûts"})end,on_failure: ->(error) { fail "Expected success, got #{error}" }@@ -134,11 +137,11 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FileInfoQuerendend- context 'with outbound request returning not found', vcr: 'one_drive/file_info_query_one_not_found' do- let(:file_id) { 'not_existent' }+ context "with outbound request returning not found", vcr: "one_drive/file_info_query_one_not_found" do+ let(:file_id) { "not_existent" }- it 'must return not found' do- result = subject.call(user:, file_id:)+ it "must return not found" do+ result = subject.call(auth_strategy:, file_id:)expect(result).to be_failureexpect(result.error_source).to be_a(Storages::Peripherals::StorageInteraction::OneDrive::Internal::DriveItemQuery)@@ -148,60 +151,4 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FileInfoQuer)endend-- context 'with invalid oauth token', vcr: 'one_drive/file_info_query_invalid_token' do- let(:file_id) { '01AZJL5PNCQCEBFI3N7JGZSX5AOX32Z3LA' }-- before do- token = build_stubbed(:oauth_client_token, oauth_client: storage.oauth_client)- allow(Storages::Peripherals::StorageInteraction::OneDrive::Util)- .to receive(:using_user_token)- .and_yield(token)- end-- it 'must return unauthorized' do- result = subject.call(user:, file_id:)- expect(result).to be_failure- expect(result.error_source).to be_a(Storages::Peripherals::StorageInteraction::OneDrive::Internal::DriveItemQuery)-- result.match(- on_failure: ->(error) { expect(error.code).to eq(:unauthorized) },- on_success: ->(file_info) { fail "Expected failure, got #{file_info}" }- )- end- end-- context 'with not existent oauth token' do- let(:file_id) { '01AZJL5PNCQCEBFI3N7JGZSX5AOX32Z3LA' }- let(:user_without_token) { create(:user) }-- it 'must return unauthorized' do- result = subject.call(user: user_without_token, file_id:)- expect(result).to be_failure- expect(result.error_source).to be_a(OAuthClients::ConnectionManager)-- result.match(- on_failure: ->(error) { expect(error.code).to eq(:unauthorized) },- on_success: ->(file_infos) { fail "Expected failure, got #{file_infos}" }- )- end- end-- context 'with network errors' do- let(:file_id) { '01AZJL5PNCQCEBFI3N7JGZSX5AOX32Z3LA' }-- before do- request = HTTPX::Request.new(:get, 'https://my.timeout.org/')- httpx_double = class_double(HTTPX, get: HTTPX::ErrorResponse.new(request, 'Timeout happens', {}))-- allow(OpenProject).to receive(:httpx).and_return(httpx_double)- end-- it 'must return an error with wrapped network error response' do- error = subject.call(user:, file_id:)- expect(error).to be_failure- expect(error.result).to eq(:error)- expect(error.error_payload).to be_a(HTTPX::ErrorResponse)- end- endenddiff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/files_info_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/files_info_query_spec.rbindex ddb07c5c36d..2ec58c727af 100644--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/files_info_query_spec.rb+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/files_info_query_spec.rb@@ -36,6 +36,9 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesInfoQuelet(:user) { create(:user) }let(:storage) { create(:sharepoint_dev_drive_storage, oauth_client_token_user: user) }+ let(:auth_strategy) do+ Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken.strategy.with_user(user)+ endsubject { described_class.new(storage) }@@ -44,13 +47,13 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesInfoQueexpect(described_class).to respond_to(:call)method = described_class.method(:call)- expect(method.parameters).to contain_exactly(%i[keyreq storage], %i[keyreq user], %i[key file_ids])+ expect(method.parameters).to contain_exactly(%i[keyreq storage], %i[keyreq auth_strategy], %i[key file_ids])endcontext "without outbound request involved" docontext "with an empty array of file ids" doit "returns an empty array" do- result = subject.call(user:, file_ids: [])+ result = subject.call(auth_strategy:, file_ids: [])expect(result).to be_successexpect(result.result).to eq([])@@ -59,7 +62,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesInfoQuecontext "with nil" doit "returns an error" do- result = subject.call(user:, file_ids: nil)+ result = subject.call(auth_strategy:, file_ids: nil)expect(result).to be_failureexpect(result.result).to eq(:error)@@ -79,7 +82,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesInfoQue# rubocop:disable RSpec/ExampleLengthit "must return an array of file information when called" do- result = subject.call(user:, file_ids:)+ result = subject.call(auth_strategy:, file_ids:)expect(result).to be_successresult.match(@@ -153,7 +156,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesInfoQuelet(:file_ids) { %w[01AZJL5PJTICED3C5YSVAY6NWTBNA2XERU not_existent] }it "must return an array of file information when called" do- result = subject.call(user:, file_ids:)+ result = subject.call(auth_strategy:, file_ids:)expect(result).to be_successresult.match(@@ -169,77 +172,5 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesInfoQueendendend-- context "with invalid oauth token", vcr: "one_drive/files_info_query_invalid_token" do- before do- token = build_stubbed(:oauth_client_token, oauth_client: storage.oauth_client)- allow(Storages::Peripherals::StorageInteraction::OneDrive::Util)- .to receive(:using_user_token)- .and_yield(token)- end-- context "with an array of file ids" do- let(:file_ids) { %w[01AZJL5PKU2WV3U3RKKFF2A7ZCWVBXRTEU] }-- it "must return an array of file information when called" do- result = subject.call(user:, file_ids:)- expect(result).to be_success-- result.match(- on_success: ->(file_infos) do- expect(file_infos.size).to eq(1)- expect(file_infos).to all(be_a(Storages::StorageFileInfo))- expect(file_infos[0].id).to eq("01AZJL5PKU2WV3U3RKKFF2A7ZCWVBXRTEU")- expect(file_infos[0].status).to eq("InvalidAuthenticationToken")- expect(file_infos[0].status_code).to eq(401)- end,- on_failure: ->(error) { fail "Expected success, got #{error}" }- )- end- end- end-- context "with not existent oauth token" do- let(:file_ids) { %w[01AZJL5PKU2WV3U3RKKFF2A7ZCWVBXRTEU] }- let(:user_without_token) { create(:user) }-- it "must return unauthorized when called" do- result = subject.call(user: user_without_token, file_ids:)- expect(result).to be_failure- expect(result.error_source).to be_a(OAuthClients::ConnectionManager)-- result.match(- on_failure: ->(error) { expect(error.code).to eq(:unauthorized) },- on_success: ->(file_infos) { fail "Expected failure, got #{file_infos}" }- )- end- end-- context "with network errors" do- let(:file_ids) { %w[01AZJL5PKU2WV3U3RKKFF2A7ZCWVBXRTEU] }-- before do- request = HTTPX::Request.new(:get, "https://my.timeout.org/")- httpx_double = class_double(HTTPX, get: HTTPX::ErrorResponse.new(request, "Timeout happens", {}))-- allow(OpenProject).to receive(:httpx).and_return(httpx_double)- end-- it "must return an array of file information when called" do- result = subject.call(user:, file_ids:)- expect(result).to be_success-- result.match(- on_success: ->(file_infos) do- expect(file_infos.size).to eq(1)- expect(file_infos).to all(be_a(Storages::StorageFileInfo))- expect(file_infos[0].id).to eq("01AZJL5PKU2WV3U3RKKFF2A7ZCWVBXRTEU")- expect(file_infos[0].status).to eq("Timeout happens")- expect(file_infos[0].status_code).to eq(500)- end,- on_failure: ->(error) { fail "Expected success, got #{error}" }- )- end- endendenddiff --git a/modules/storages/spec/support/fixtures/vcr_cassettes/one_drive/file_info_query_invalid_token.yml b/modules/storages/spec/support/fixtures/vcr_cassettes/one_drive/file_info_query_invalid_token.ymldeleted file mode 100644index d005a0fb4f2..00000000000--- a/modules/storages/spec/support/fixtures/vcr_cassettes/one_drive/file_info_query_invalid_token.yml+++ /dev/null@@ -1,51 +0,0 @@-----http_interactions:-- request:- method: get- uri: https://graph.microsoft.com/v1.0/drives/b!dmVLG22QlE2PSW0AqVB7UOhZ8n7tjkVGkgqLNnuw2OBb-brzKzZAR4DYT1k9KPXs/items/01AZJL5PNCQCEBFI3N7JGZSX5AOX32Z3LA?$select=id,name,fileSystemInfo,file,folder,size,createdBy,lastModifiedBy,parentReference- body:- encoding: US-ASCII- string: ''- headers:- Authorization:- - Bearer <BEARER TOKEN>- User-Agent:- - httpx.rb/1.2.1- Accept:- - "*/*"- Accept-Encoding:- - gzip, deflate- response:- status:- code: 401- message: Unauthorized- headers:- Transfer-Encoding:- - chunked- Content-Type:- - application/json- Content-Encoding:- - gzip- Vary:- - Accept-Encoding- Strict-Transport-Security:- - max-age=31536000- Request-Id:- - bb2f6d5c-3c01-41a8-be44-3d2bb300998e- Client-Request-Id:- - bb2f6d5c-3c01-41a8-be44-3d2bb300998e- X-Ms-Ags-Diagnostic:- - '{"ServerInfo":{"DataCenter":"Germany West Central","Slice":"E","Ring":"5","ScaleUnit":"005","RoleInstance":"FR3PEPF000002D9"}}'- Www-Authenticate:- - Bearer realm="", authorization_uri="https://login.microsoftonline.com/common/oauth2/authorize",- client_id="00000003-0000-0000-c000-000000000000"- Date:- - Tue, 30 Jan 2024 11:56:00 GMT- body:- encoding: UTF-8- string: '{"error":{"code":"InvalidAuthenticationToken","message":"IDX14100:- JWT is not well formed, there are no dots (.).\nThe token needs to be in JWS- or JWE Compact Serialization Format. (JWS): ''EncodedHeader.EndcodedPayload.EncodedSignature''.- (JWE): ''EncodedProtectedHeader.EncodedEncryptedKey.EncodedInitializationVector.EncodedCiphertext.EncodedAuthenticationTag''.","innerError":{"date":"2024-01-30T11:56:01","request-id":"bb2f6d5c-3c01-41a8-be44-3d2bb300998e","client-request-id":"bb2f6d5c-3c01-41a8-be44-3d2bb300998e"}}}'- recorded_at: Tue, 30 Jan 2024 11:56:01 GMT-recorded_with: VCR 6.2.0diff --git a/modules/storages/spec/support/fixtures/vcr_cassettes/one_drive/files_info_query_invalid_token.yml b/modules/storages/spec/support/fixtures/vcr_cassettes/one_drive/files_info_query_invalid_token.ymldeleted file mode 100644index 6bea5b664d2..00000000000--- a/modules/storages/spec/support/fixtures/vcr_cassettes/one_drive/files_info_query_invalid_token.yml+++ /dev/null@@ -1,51 +0,0 @@-----http_interactions:-- request:- method: get- uri: https://graph.microsoft.com/v1.0/drives/b!dmVLG22QlE2PSW0AqVB7UOhZ8n7tjkVGkgqLNnuw2OBb-brzKzZAR4DYT1k9KPXs/items/01AZJL5PKU2WV3U3RKKFF2A7ZCWVBXRTEU?$select=id,name,fileSystemInfo,file,folder,size,createdBy,lastModifiedBy,parentReference- body:- encoding: US-ASCII- string: ''- headers:- Authorization:- - Bearer <BEARER TOKEN>- User-Agent:- - httpx.rb/1.2.1- Accept:- - "*/*"- Accept-Encoding:- - gzip, deflate- response:- status:- code: 401- message: Unauthorized- headers:- Transfer-Encoding:- - chunked- Content-Type:- - application/json- Content-Encoding:- - gzip- Vary:- - Accept-Encoding- Strict-Transport-Security:- - max-age=31536000- Request-Id:- - 6bcd1876-dd4b-4a6d-a28c-3ff3130e654d- Client-Request-Id:- - 6bcd1876-dd4b-4a6d-a28c-3ff3130e654d- X-Ms-Ags-Diagnostic:- - '{"ServerInfo":{"DataCenter":"Germany West Central","Slice":"E","Ring":"5","ScaleUnit":"005","RoleInstance":"FR3PEPF000002C3"}}'- Www-Authenticate:- - Bearer realm="", authorization_uri="https://login.microsoftonline.com/common/oauth2/authorize",- client_id="00000003-0000-0000-c000-000000000000"- Date:- - Tue, 30 Jan 2024 11:55:53 GMT- body:- encoding: UTF-8- string: '{"error":{"code":"InvalidAuthenticationToken","message":"IDX14100:- JWT is not well formed, there are no dots (.).\nThe token needs to be in JWS- or JWE Compact Serialization Format. (JWS): ''EncodedHeader.EndcodedPayload.EncodedSignature''.- (JWE): ''EncodedProtectedHeader.EncodedEncryptedKey.EncodedInitializationVector.EncodedCiphertext.EncodedAuthenticationTag''.","innerError":{"date":"2024-01-30T11:55:53","request-id":"6bcd1876-dd4b-4a6d-a28c-3ff3130e654d","client-request-id":"6bcd1876-dd4b-4a6d-a28c-3ff3130e654d"}}}'- recorded_at: Tue, 30 Jan 2024 11:55:53 GMT-recorded_with: VCR 6.2.0