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] updated open file link query
Eric Schubert00b3dc041e14
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/open_file_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/open_file_link_query.rbindex b7e95f71d67..5a74dcdc87d 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/open_file_link_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/open_file_link_query.rb@@ -34,17 +34,17 @@ module Storagesmodule Nextcloudclass OpenFileLinkQuerydef initialize(storage)- @uri = storage.uri+ @storage = storage.uriend- def self.call(storage:, user:, file_id:, open_location: false)- new(storage).call(user:, file_id:, open_location:)+ def self.call(storage:, auth_strategy:, file_id:, open_location: false)+ new(storage).call(auth_strategy:, file_id:, open_location:)end# rubocop:disable Lint/UnusedMethodArgument- def call(user:, file_id:, open_location: false)+ def call(auth_strategy:, file_id:, open_location: false)location_flag = open_location ? 0 : 1- ServiceResult.success(result: Util.join_uri_path(@uri, "index.php/f/#{file_id}?openfile=#{location_flag}"))+ ServiceResult.success(result: Util.join_uri_path(@storage.uri, "index.php/f/#{file_id}?openfile=#{location_flag}"))end# rubocop:enable Lint/UnusedMethodArgumentdiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query.rbindex 1c5f523f3b3..f49282ae860 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query.rb@@ -34,9 +34,10 @@ module Storagesmodule OneDriveclass OpenFileLinkQueryusing ::Storages::Peripherals::ServiceResultRefinements+ Auth = ::Storages::Peripherals::StorageInteraction::Authentication- def self.call(storage:, user:, file_id:, open_location: false)- new(storage).call(user:, file_id:, open_location:)+ def self.call(storage:, auth_strategy:, file_id:, open_location: false)+ new(storage).call(auth_strategy:, file_id:, open_location:)enddef initialize(storage)@@ -44,29 +45,27 @@ module Storages@delegate = Internal::DriveItemQuery.new(storage)end- def call(user:, file_id:, open_location: false)- @user = user-- Util.using_user_token(@storage, user) do |token|+ def call(auth_strategy:, file_id:, open_location: false)+ Auth[auth_strategy].call(storage: @storage) do |http|if open_location- request_parent_id(token).call(file_id) >> request_web_url(token)+ request_parent_id(http).call(file_id) >> request_web_url(http)else- request_web_url(token).call(file_id)+ request_web_url(http).call(file_id)endendendprivate- def request_web_url(token)+ def request_web_url(http)->(file_id) do- @delegate.call(token:, drive_item_id: file_id, fields: %w[webUrl]).map(&web_url)+ @delegate.call(http:, drive_item_id: file_id, fields: %w[webUrl]).map(&web_url)endend- def request_parent_id(token)+ def request_parent_id(http)->(file_id) do- @delegate.call(token:, drive_item_id: file_id, fields: %w[parentReference]).map(&parent_id)+ @delegate.call(http:, drive_item_id: file_id, fields: %w[parentReference]).map(&parent_id)endenddiff --git a/modules/storages/app/models/storages/project_storage.rb b/modules/storages/app/models/storages/project_storage.rbindex 51663bc4ecc..a1fe8fa686b 100644--- a/modules/storages/app/models/storages/project_storage.rb+++ b/modules/storages/app/models/storages/project_storage.rb@@ -89,7 +89,7 @@ module StorageselsePeripherals::Registry.resolve("#{storage.short_provider_type}.queries.open_file_link")- .call(storage:, user:, file_id: project_folder_id)+ .call(storage:, auth_strategy:, file_id: project_folder_id)endenddiff --git a/modules/storages/lib/api/v3/file_links/file_links_open_api.rb b/modules/storages/lib/api/v3/file_links/file_links_open_api.rbindex 5989101b21b..2368aaf8e0a 100644--- a/modules/storages/lib/api/v3/file_links/file_links_open_api.rb+++ b/modules/storages/lib/api/v3/file_links/file_links_open_api.rb@@ -33,11 +33,15 @@ class API::V3::FileLinks::FileLinksOpenAPI < API::OpenProjectAPIresources :open doget do+ auth_strategy = Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken+ .strategy+ .with_user(current_user)+Storages::Peripherals::Registry.resolve("#{@file_link.storage.short_provider_type}.queries.open_file_link").call(storage: @file_link.storage,- user: current_user,+ auth_strategy:,file_id: @file_link.origin_id,open_location: ActiveModel::Type::Boolean.new.cast(params[:location]))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 defacc3139a..14085dea0de 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@@ -143,7 +143,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FileInfoQuerit "must return not found" doresult = subject.call(auth_strategy:, file_id:)expect(result).to be_failure- expect(result.error_source).to be_a(Storages::Peripherals::StorageInteraction::OneDrive::Internal::DriveItemQuery)+ expect(result.error_source).to be(Storages::Peripherals::StorageInteraction::OneDrive::Internal::DriveItemQuery)result.match(on_failure: ->(error) { expect(error.code).to eq(:not_found) },diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query_spec.rbindex 78ca03cc9b8..9324d99f226 100644--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query_spec.rb+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_file_link_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::OpenFileLinkQuery, :vcr, :webmock do@@ -36,58 +36,49 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::OpenFileLinklet(:user) { create(:user) }let(:storage) { create(:sharepoint_dev_drive_storage, oauth_client_token_user: user) }- let(:file_id) { '01AZJL5PJTICED3C5YSVAY6NWTBNA2XERU' }+ let(:file_id) { "01AZJL5PJTICED3C5YSVAY6NWTBNA2XERU" }+ 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], %i[key open_location])+ expect(method.parameters).to contain_exactly(%i[keyreq storage],+ %i[keyreq auth_strategy],+ %i[keyreq file_id],+ %i[key open_location])end- context 'with outbound requests successful' do- context 'with open location flag not set', vcr: 'one_drive/open_file_link_query_success' do- it 'returns the url for opening the file on storage' do- call = subject.call(user:, file_id:)+ context "with outbound requests successful" do+ context "with open location flag not set", vcr: "one_drive/open_file_link_query_success" do+ it "returns the url for opening the file on storage" do+ call = subject.call(auth_strategy:, file_id:)expect(call).to be_success- expect(call.result).to eq('https://finn.sharepoint.com/sites/openprojectfilestoragetests/_layouts/15/Doc.aspx?sourcedoc=%7B3D884033-B88B-4195-8F36-D30B41AB9234%7D&file=Document.docx&action=default&mobileredirect=true')+ expect(call.result).to eq("https://finn.sharepoint.com/sites/openprojectfilestoragetests/_layouts/15/Doc.aspx?sourcedoc=%7B3D884033-B88B-4195-8F36-D30B41AB9234%7D&file=Document.docx&action=default&mobileredirect=true")endend- context 'with open location flag set', vcr: 'one_drive/open_file_link_location_query_success' do- it 'returns the url for opening the file on storage' do- call = subject.call(user:, file_id:, open_location: true)+ context "with open location flag set", vcr: "one_drive/open_file_link_location_query_success" do+ it "returns the url for opening the file on storage" do+ call = subject.call(auth_strategy:, file_id:, open_location: true)expect(call).to be_success- expect(call.result).to eq('https://finn.sharepoint.com/sites/openprojectfilestoragetests/VCR/Folder')+ expect(call.result).to eq("https://finn.sharepoint.com/sites/openprojectfilestoragetests/VCR/Folder")endendend- context 'with not existent oauth token' do- let(:user_without_token) { create(:user) }-- it 'must return unauthorized when called' 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 not existent file id', vcr: 'one_drive/open_file_link_query_missing_file_id' do- let(:file_id) { 'iamnotexistent' }+ context "with not existent file id", vcr: "one_drive/open_file_link_query_missing_file_id" do+ let(:file_id) { "iamnotexistent" }- 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_failure- expect(result.error_source).to be_a(Storages::Peripherals::StorageInteraction::OneDrive::Internal::DriveItemQuery)+ expect(result.error_source).to be(Storages::Peripherals::StorageInteraction::OneDrive::Internal::DriveItemQuery)result.match(on_failure: ->(error) { expect(error.code).to eq(:not_found) },@@ -95,25 +86,5 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::OpenFileLink)endend-- context 'with invalid oauth token', vcr: 'one_drive/open_file_link_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-- 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_infos) { fail "Expected failure, got #{file_infos}" }- )- end- endendenddiff --git a/modules/storages/spec/support/fixtures/vcr_cassettes/one_drive/open_file_link_query_invalid_token.yml b/modules/storages/spec/support/fixtures/vcr_cassettes/one_drive/open_file_link_query_invalid_token.ymldeleted file mode 100644index 2502b6bf767..00000000000--- a/modules/storages/spec/support/fixtures/vcr_cassettes/one_drive/open_file_link_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/01AZJL5PJTICED3C5YSVAY6NWTBNA2XERU?$select=webUrl- 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:- - 79a87165-2edf-413e-bb42-7387830b7e4f- Client-Request-Id:- - 79a87165-2edf-413e-bb42-7387830b7e4f- X-Ms-Ags-Diagnostic:- - '{"ServerInfo":{"DataCenter":"Germany West Central","Slice":"E","Ring":"5","ScaleUnit":"005","RoleInstance":"FR3PEPF00000366"}}'- 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:25 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:26","request-id":"79a87165-2edf-413e-bb42-7387830b7e4f","client-request-id":"79a87165-2edf-413e-bb42-7387830b7e4f"}}}'- recorded_at: Tue, 30 Jan 2024 11:55:26 GMT-recorded_with: VCR 6.2.0