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

WIP - Moved request_with_token_refresh to connection manager

Frank Bergmannd7ea8d80b432
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index f27e85d3d44..76de3f3d4af 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -145,6 +145,27 @@ module OAuthClients
:error
end
+ # @returns ServiceResult with result to be :error or any type of object with data
+ def request_with_token_refresh
+ # `yield` needs to returns a ServiceResult:
+ # success: result= any object with data
+ # failure: result= :error or :not_authorized
+ yield_service_result = yield
+
+ if yield_service_result.failure? && yield_service_result.result == :not_authorized
+ refresh_service_result = refresh_token
+ if refresh_service_result.failure?
+ failed_service_result = ServiceResult.failure(result: :error)
+ failed_service_result.merge!(refresh_service_result)
+ return failed_service_result
+ end
+
+ yield_service_result = yield # Should contain result=<data> in case of success
+ end
+
+ yield_service_result
+ end
+
private
# Check if a OAuthClientToken already exists and return nil otherwise.
diff --git a/modules/storages/app/services/storages/file_link_sync_service.rb b/modules/storages/app/services/storages/file_link_sync_service.rb
index 07b85d80c39..f4f6de23d1a 100644
--- a/modules/storages/app/services/storages/file_link_sync_service.rb
+++ b/modules/storages/app/services/storages/file_link_sync_service.rb
@@ -56,8 +56,8 @@ class Storages::FileLinkSyncService
@service_result
end
- def set_file_link_permissions(storage_file_links, parsed_response)
- storage_file_links.each do |file_link|
+ def set_file_link_permissions(file_links, parsed_response)
+ file_links.each do |file_link|
origin_file_info_hash = parsed_response[file_link.origin_id]
case origin_file_info_hash['statuscode']
@@ -128,9 +128,11 @@ class Storages::FileLinkSyncService
@oauth_client_token = connection_manager_service_result.result
storage_origin_file_ids = storage_file_links.map(&:origin_id)
- nextcloud_request_result = nextcloud_request_with_token_refresh(connection_manager) do
+ nextcloud_request_result = connection_manager.request_with_token_refresh do
response = request_files_info(storage_origin_file_ids)
- # Extract ServiceResult with result = :error, :not_authorized or object with data
+ # Parse HTTP response an return a ServiceResult with:
+ # success: result= Hash with Nextcloud filesinfo (name of endpoint) data
+ # failure: result= :error or :not_authorized
parse_files_info_response(response)
end
@service_result.merge!(nextcloud_request_result) # Pass errors from Nextcloud into service result
@@ -143,27 +145,6 @@ class Storages::FileLinkSyncService
set_file_link_permissions(storage_file_links, nextcloud_request_result.result)
end
- # @returns ServiceResult with result to be :error or any type of object with data
- def nextcloud_request_with_token_refresh(connection_manager)
- # `yield` needs to returns a ServiceResult:
- # success: result= any object with data
- # failure: result= :error or :not_authorized
- yield_service_result = yield
-
- if yield_service_result.failure? && yield_service_result.result == :not_authorized
- refresh_service_result = connection_manager.refresh_token
- if refresh_service_result.failure?
- failed_service_result = ServiceResult.failure(result: :error)
- failed_service_result.merge!(refresh_service_result)
- return failed_service_result
- end
-
- yield_service_result = yield # Should contain result=<data> in case of success
- end
-
- yield_service_result
- end
-
def set_error_for_file_links(storage_file_links)
@service_result.result += storage_file_links.each { |file_link| file_link.origin_permission = :error }
@service_result.success = false
diff --git a/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb b/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
index a64a8b1a9f6..b6ca5b49d94 100644
--- a/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
+++ b/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
@@ -65,9 +65,6 @@ module API
.where(container_id: @work_package.id, container_type: 'WorkPackage'))
.all
- # ToDo: API allows queries on storages, how to integrate here?
- # Example: -> grids_api.rb
-
# Synchronize with Nextcloud. StorageAPI handles OAuth2 for us.
service_result = ::Storages::FileLinkSyncService
.new(user: current_user)
diff --git a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
index 7de3b8cb18d..7de130aa9e9 100644
--- a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
+++ b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
@@ -29,7 +29,7 @@
require 'spec_helper'
require_module_spec_helper
-describe 'API v3 file links resource', with_flag: { storages_module_active: true }, type: :request, webmock: true do
+describe 'API v3 file links resource', with_flag: { storages_module_active: true }, type: :request do
include API::V3::Utilities::PathHelper
let(:permissions) { %i(view_work_packages view_file_links) }
diff --git a/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb b/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb
index 970569e2e88..44f99fec33f 100644
--- a/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb
+++ b/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb
@@ -66,7 +66,7 @@ describe ::Storages::FileLinkSyncService, type: :model do
end
# We're going to mock OAuth2 autentication failures below
- let(:connection_manager) { instance_double(::OAuthClients::ConnectionManager) }
+ let(:connection_manager) { ::OAuthClients::ConnectionManager.new(user:, oauth_client: oauth_client1) }
let(:authorize_url) { 'https://example.com/authorize' }
let(:instance) { described_class.new(user:) }
@@ -236,6 +236,8 @@ describe ::Storages::FileLinkSyncService, type: :model do
allow(connection_manager)
.to receive(:get_access_token)
.and_return(ServiceResult.success(result: oauth_client_token1))
+ # We can't mock :request_with_token_refresh easily, as it takes a block
+ # of the instance to be tested. So we use a "real" ConnectionManager instance instead.
# Mock Nextcloud to return:
# first: a 401 indicating an outdated OAuth2 Bearer token and
@@ -250,6 +252,7 @@ describe ::Storages::FileLinkSyncService, type: :model do
headers: { 'Content-Type': 'application/json; charset=utf-8' },
body: { ocs: { meta: ocs_meta_s200, data: { '24': file_info1_s200 } } }.to_json)
end
+ # ToDo: Do I check that refresh_token is only called once?
# Just check that some update has happened.
it 'updates the file_link information' do
diff --git a/spec/services/oauth_clients/connection_manager_spec.rb b/spec/services/oauth_clients/connection_manager_spec.rb
index a507785c2fa..f76d2759aab 100644
--- a/spec/services/oauth_clients/connection_manager_spec.rb
+++ b/spec/services/oauth_clients/connection_manager_spec.rb
@@ -440,4 +440,83 @@ describe ::OAuthClients::ConnectionManager, type: :model do
end
end
end
+
+ describe '#request_with_token_refresh' do
+ let(:yield_service_result) { ServiceResult.success }
+ let(:refresh_service_result) { ServiceResult.success }
+
+ subject do
+ instance.request_with_token_refresh { yield_service_result }
+ end
+
+ before do
+ allow(instance).to receive(:refresh_token).and_return refresh_service_result
+ end
+
+ context 'with yield=:success and refresh not called' do
+ it 'returns a ServiceResult with success, without refresh' do
+ expect(subject.success).to be_truthy
+ expect(instance).not_to have_received(:refresh_token)
+ end
+ end
+
+ context 'with yield=:error and refresh not called' do
+ let(:yield_service_result) { ServiceResult.failure(result: :error) }
+
+ it 'returns a ServiceResult with success, without refresh' do
+ expect(subject.success).to be_falsey
+ expect(subject.result).to be :error
+ expect(instance).not_to have_received(:refresh_token)
+ end
+ end
+
+ context 'with yield=:not_authorized and refresh=:success' do
+ let(:yield_service_result) { ServiceResult.failure(result: :not_authorized) }
+
+ it 'returns a ServiceResult with success, without refresh' do
+ expect(subject.success).to be_falsey
+ expect(subject.result).to be :not_authorized
+ expect(instance).to have_received(:refresh_token)
+ end
+ end
+
+ context 'with yield=:not_authorized and refresh=:failure' do
+ let(:yield_service_result) { ServiceResult.failure(result: :not_authorized) }
+ let(:refresh_service_result) { ServiceResult.failure }
+
+ it 'returns a ServiceResult with success, without refresh' do
+ expect(subject.success).to be_falsey
+ expect(subject.result).to be :error
+ expect(instance).to have_received(:refresh_token)
+ end
+ end
+
+ context 'with yield=:not_authorized first time and success the second time' do
+ let(:yield_double_object) { Object.new }
+ let(:yield_service_result1) { ServiceResult.failure(result: :not_authorized) }
+ let(:yield_service_result2) { ServiceResult.success }
+ let(:refresh_service_result) { ServiceResult.success }
+
+ subject do
+ instance.request_with_token_refresh { yield_double_object.yield_twice_method }
+ end
+
+ before do
+ allow(instance).to receive(:refresh_token).and_return refresh_service_result
+ allow(yield_double_object)
+ .to receive(:yield_twice_method)
+ .and_return(
+ yield_service_result1,
+ yield_service_result2
+ )
+ end
+
+ it 'returns a ServiceResult with success, without refresh' do
+ expect(subject.success).to be_truthy
+ expect(subject).to be yield_service_result2
+ expect(instance).to have_received(:refresh_token)
+ expect(yield_double_object).to have_received(:yield_twice_method).twice
+ end
+ end
+ end
end