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
[#53369] replace authorization state with auth check query
Eric Schubert66f51b6a880b
diff --git a/app/controllers/oauth_clients_controller.rb b/app/controllers/oauth_clients_controller.rbindex b0458a7d27f..48389a335a4 100644--- a/app/controllers/oauth_clients_controller.rb+++ b/app/controllers/oauth_clients_controller.rb@@ -70,6 +70,7 @@ class OAuthClientsController < ApplicationControllerendend+ # rubocop:disable Metrics/AbcSizedef ensure_connectionclient_id = params.fetch(:oauth_client_id)storage_id = params.fetch(:storage_id)@@ -88,7 +89,9 @@ class OAuthClientsController < ApplicationControllerelseroot_urlend- if connection_manager.authorization_state_connected?+ auth_state = ::Storages::Peripherals::StorageInteraction::Authentication+ .authorization_state(storage: oauth_client.integration, user: User.current)+ if auth_state == :connectedredirect_to(destination_url)elsenonce = SecureRandom.uuid@@ -97,6 +100,8 @@ class OAuthClientsController < ApplicationControllerendend+ # rubocop:enable Metrics/AbcSize+privatedef handle_absent_oauth_clientdiff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rbindex ac0d6e70462..6875c40902c 100644--- a/app/services/oauth_clients/connection_manager.rb+++ b/app/services/oauth_clients/connection_manager.rb@@ -55,7 +55,7 @@ module OAuthClients# Return the Nextcloud OAuth authorization URI that a user needs to open to grant access and eventually obtain# a token.- @redirect_url = get_authorization_uri(state:)+ @redirect_url = @config.authorization_uri(state:)ServiceResult.failure(result: @redirect_url)end@@ -90,19 +90,6 @@ module OAuthClientsendend- # rubocop:enable Metrics/AbcSize-- # Returns the URI of the "authorize" endpoint of the OAuth2 Authorization Server.- # @param state (OAuth2 RFC) is a nonce referencing a cookie containing the calling page (URL + params) to which to- # return to at the end of the whole flow.- # @param scope (OAuth2 RFC) specifies the resources to access. Nextcloud has only one global scope.- def get_authorization_uri(state: nil)- client = rack_oauth_client # Configure and start the rack-oauth2 client- client.authorization_uri(scope: @config.scope, state:)- end-- # rubocop:disable Metrics/AbcSize-# Called by callback_page with a cryptographic "code" that indicates# that the user has successfully authorized the OAuth2 Authorization Server.# We now are going to exchange this code to a token (bearer+refresh)@@ -140,42 +127,6 @@ module OAuthClients# rubocop:enable Metrics/AbcSize- # Called by StorageRepresenter to inquire about the status of the OAuth2- # authentication server.- # Returns :connected/:authorization_failed or :error for a general error.- # We have decided to distinguish between only these 3 cases, because the- # front-end (and a normal user) probably wouldn't know how to deal with- # other options.- def authorization_state- oauth_client_token = get_existing_token- return :failed_authorization unless oauth_client_token-- state = @config.authorization_state_check(oauth_client_token.access_token)- case state- when :success- :connected- when :refresh_needed- service_result = refresh_token- if service_result.success?- :connected- elsif service_result.errors.data.payload[:error] == 'invalid_request'- :failed_authorization- else- :error- end- else- state- end- rescue StandardError- :error- end-- %i[connected failed_authorization error].each do |authorization_result|- define_method(:"authorization_state_#{authorization_result}?") do- authorization_state == authorization_result- end- end-# @returns ServiceResult with result to be :error or any type of object with datadef request_with_token_refresh(oauth_client_token)# `yield` needs to returns a ServiceResult:diff --git a/modules/storages/app/common/storages/peripherals/nextcloud.rb b/modules/storages/app/common/storages/peripherals/nextcloud.rbindex 0d5a40cc18b..d59f4d003d3 100644--- a/modules/storages/app/common/storages/peripherals/nextcloud.rb+++ b/modules/storages/app/common/storages/peripherals/nextcloud.rb@@ -32,6 +32,7 @@ module Storagesmodule PeripheralsNextcloud = Dry::Container::Namespace.new('nextcloud') donamespace('queries') do+ register(:auth_check, StorageInteraction::Nextcloud::AuthCheckQuery)register(:download_link, StorageInteraction::Nextcloud::DownloadLinkQuery)register(:file_ids, StorageInteraction::Nextcloud::FileIdsQuery)register(:file_info, StorageInteraction::Nextcloud::FileInfoQuery)diff --git a/modules/storages/app/common/storages/peripherals/oauth_configurations/configuration_interface.rb b/modules/storages/app/common/storages/peripherals/oauth_configurations/configuration_interface.rbindex a5442092376..a4e457ff49e 100644--- a/modules/storages/app/common/storages/peripherals/oauth_configurations/configuration_interface.rb+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/configuration_interface.rb@@ -32,25 +32,14 @@ module Storagesmodule Peripheralsmodule OAuthConfigurationsclass ConfigurationInterface- def authorization_state_check(_) = raise ::Storages::Errors::SubclassResponsibility-def scope = raise ::Storages::Errors::SubclassResponsibilitydef basic_rack_oauth_client = raise ::Storages::Errors::SubclassResponsibilitydef to_httpx_oauth_config = raise ::Storages::Errors::SubclassResponsibility- private-- def authorization_check_wrapper- case yield- in { status: 200..299 }- :success- in { status: 401 | 403 }- :refresh_needed- else- :error- end+ def authorization_uri(state:)+ basic_rack_oauth_client.authorization_uri(scope:, state:)endendenddiff --git a/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb b/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rbindex 33570cad6b5..eb6c47c6db8 100644--- a/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb@@ -44,19 +44,6 @@ module Storages# rubocop:enable Lint/MissingSuper- def authorization_state_check(token)- authorization_check_wrapper do- OpenProject.httpx.get(- Util.join_uri_path(@uri, "/ocs/v1.php/cloud/user"),- headers: {- "Authorization" => "Bearer #{token}",- "OCS-APIRequest" => "true",- "Accept" => "application/json"- }- )- end- end-def extract_origin_user_id(rack_access_token)rack_access_token.raw_attributes[:user_id]enddiff --git a/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb b/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rbindex c66ea8c86a2..9b9568d601f 100644--- a/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb@@ -46,15 +46,6 @@ module Storages# rubocop:enable Lint/MissingSuper- def authorization_state_check(access_token)- authorization_check_wrapper do- OpenProject.httpx.get(- Util.join_uri_path(@uri, "/v1.0/me"),- headers: { "Authorization" => "Bearer #{access_token}", "Accept" => "application/json" }- )- end- end-def extract_origin_user_id(rack_access_token)OpenProject.httpx.get(Util.join_uri_path(@uri, "/v1.0/me"),diff --git a/modules/storages/app/common/storages/peripherals/one_drive.rb b/modules/storages/app/common/storages/peripherals/one_drive.rbindex 399df0b46c3..5ec1cc2975a 100644--- a/modules/storages/app/common/storages/peripherals/one_drive.rb+++ b/modules/storages/app/common/storages/peripherals/one_drive.rb@@ -32,6 +32,7 @@ module Storagesmodule PeripheralsOneDrive = Dry::Container::Namespace.new('one_drive') donamespace('queries') do+ register(:auth_check, StorageInteraction::OneDrive::AuthCheckQuery)register(:download_link, StorageInteraction::OneDrive::DownloadLinkQuery)register(:files, StorageInteraction::OneDrive::FilesQuery)register(:file_info, StorageInteraction::OneDrive::FileInfoQuery)diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rbindex 908ea6a6eec..f4807cb61c2 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb@@ -59,7 +59,7 @@ module Storages.resolve("#{storage.short_provider_type}.queries.auth_check").call(storage:, auth_strategy:).match(- on_success: ->(result) { result },+ on_success: ->(*) { :connected },on_failure: ->(error) { error.code == :unauthorized ? :failed_authorization : :error })enddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/auth_check_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/auth_check_query.rbnew file mode 100644index 00000000000..16a58ee046d--- /dev/null+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/auth_check_query.rb@@ -0,0 +1,71 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module Storages+ module Peripherals+ module StorageInteraction+ module Nextcloud+ class AuthCheckQuery+ Auth = ::Storages::Peripherals::StorageInteraction::Authentication++ using ServiceResultRefinements++ def self.call(storage:, auth_strategy:)+ new(storage).call(auth_strategy:)+ end++ def initialize(storage)+ @storage = storage+ end++ def call(auth_strategy:)+ Auth[auth_strategy].call(storage: @storage, http_options: Util.ocs_api_request) do |http|+ handle_response http.get(Util.join_uri_path(@storage.uri, '/ocs/v1.php/cloud/user'))+ end+ end++ private++ def handle_response(response)+ case response+ in { status: 200..299 }+ ServiceResult.success+ in { status: 401 }+ ServiceResult.failure(result: :unauthorized, errors: ::Storages::StorageError.new(code: :unauthorized))+ else+ data = ::Storages::StorageErrorData.new(source: self, payload: response)+ ServiceResult.failure(result: :error, errors: ::Storages::StorageError.new(code: :error, data:))+ end+ end+ end+ end+ end+ end+enddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/auth_check_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/auth_check_query.rbnew file mode 100644index 00000000000..812525fb7f8--- /dev/null+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/auth_check_query.rb@@ -0,0 +1,71 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module Storages+ module Peripherals+ module StorageInteraction+ module OneDrive+ class AuthCheckQuery+ Auth = ::Storages::Peripherals::StorageInteraction::Authentication++ using ServiceResultRefinements++ def self.call(storage:, auth_strategy:)+ new(storage).call(auth_strategy:)+ end++ def initialize(storage)+ @storage = storage+ end++ def call(auth_strategy:)+ Auth[auth_strategy].call(storage: @storage) do |http|+ handle_response http.get(Util.join_uri_path(@storage.uri, '/v1.0/me'))+ end+ end++ private++ def handle_response(response)+ case response+ in { status: 200..299 }+ ServiceResult.success+ in { status: 401 }+ ServiceResult.failure(result: :unauthorized, errors: ::Storages::StorageError.new(code: :unauthorized))+ else+ data = ::Storages::StorageErrorData.new(source: self, payload: response)+ ServiceResult.failure(result: :error, errors: ::Storages::StorageError.new(code: :error, data:))+ end+ end+ end+ end+ end+ end+enddiff --git a/modules/storages/lib/api/v3/storages/storage_representer.rb b/modules/storages/lib/api/v3/storages/storage_representer.rbindex 405a5f16323..722a3913594 100644--- a/modules/storages/lib/api/v3/storages/storage_representer.rb+++ b/modules/storages/lib/api/v3/storages/storage_representer.rb@@ -74,16 +74,6 @@ module API::V3::Storagesextend ClassMethods- def initialize(model, current_user:, embed_links: nil)- if model.oauth_configuration.present?- # Do not instantiate a connection manager, if representer is used for parsing- @connection_manager =- ::OAuthClients::ConnectionManager.new(user: current_user, configuration: model.oauth_configuration)- end-- super- end-property :idproperty :name@@ -158,7 +148,8 @@ module API::V3::Storagesendlink :authorizationState do- urn = case authorization_state+ auth_state = authorization_state+ urn = case auth_statewhen :connectedURN_CONNECTION_CONNECTEDwhen :failed_authorization@@ -166,7 +157,7 @@ module API::V3::StorageselseURN_CONNECTION_ERRORend- title = I18n.t(:"oauth_client.urn_connection_status.#{authorization_state}")+ title = I18n.t(:"oauth_client.urn_connection_status.#{auth_state}"){ href: urn, title: }end@@ -174,7 +165,7 @@ module API::V3::Storageslink :authorize donext unless authorization_state == :failed_authorization- { href: @connection_manager.get_authorization_uri, title: 'Authorize' }+ { href: represented.oauth_configuration.authorization_uri(state: nil), title: 'Authorize' }endlink :projectStorages do@@ -231,7 +222,8 @@ module API::V3::Storagesenddef authorization_state- @authorization_state ||= @connection_manager.authorization_state+ ::Storages::Peripherals::StorageInteraction::Authentication.authorization_state(storage: represented,+ user: current_user)endendenddiff --git a/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rb b/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rbindex 65ca341bbce..2dbda82112f 100644--- a/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rb+++ b/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rb@@ -36,16 +36,16 @@ RSpec.describe API::V3::Storages::StorageRepresenter, 'rendering' dolet(:oauth_client_credentials) { build_stubbed(:oauth_client) }let(:storage) { build_stubbed(:nextcloud_storage, oauth_application:, oauth_client: oauth_client_credentials) }let(:user) { build_stubbed(:user) }+ let(:auth_check_result) { ServiceResult.success }let(:representer) { described_class.new(storage, current_user: user, embed_links: true) }- let(:connection_manager) { instance_double(OAuthClients::ConnectionManager) }subject(:generated) { representer.to_json }before do- allow(OAuthClients::ConnectionManager)- .to receive(:new).and_return(connection_manager)- allow(connection_manager)- .to receive_messages(authorization_state: :connected, get_authorization_uri: 'https://example.com/authorize')+ Storages::Peripherals::Registry.stub(+ "#{storage.short_provider_type}.queries.auth_check",+ ->(_) { auth_check_result }+ )enddescribe '_links' dodiff --git a/modules/storages/spec/requests/api/v3/file_links/file_links_api_spec.rb b/modules/storages/spec/requests/api/v3/file_links/file_links_api_spec.rbindex c1d5cf5b3ff..0a1e11a04b5 100644--- a/modules/storages/spec/requests/api/v3/file_links/file_links_api_spec.rb+++ b/modules/storages/spec/requests/api/v3/file_links/file_links_api_spec.rb@@ -65,7 +65,7 @@ RSpec.describe 'API v3 file links resource' doshared_let(:oauth_client_token) { create(:oauth_client_token, oauth_client:, user: current_user) }shared_let(:project_storage) { create(:project_storage, project:, storage:) }- let!(:another_project_storage) { nil } # create(:project_storage, project:, storage: another_storage)+ let!(:another_project_storage) { nil }let(:file_link) docreate(:file_link, creator: current_user, container: work_package, storage:)@@ -79,20 +79,11 @@ RSpec.describe 'API v3 file links resource' docreate(:file_link, creator: current_user, container: work_package, storage: another_storage)end- let(:connection_manager) { instance_double(OAuthClients::ConnectionManager) }let(:sync_service) { instance_double(Storages::FileLinkSyncService) }subject(:response) { last_response }before do- # Mock ConnectionManager to behave as if connected- allow(OAuthClients::ConnectionManager)- .to receive(:new).and_return(connection_manager)- allow(connection_manager)- .to receive_messages(get_access_token: ServiceResult.success(result: oauth_client_token), authorization_state: :connected,- get_authorization_uri: 'https://example.com/authorize')-- # Mock FileLinkSyncService as if Nextcloud would respond positivelyallow(Storages::FileLinkSyncService).to receive(:new).and_return(sync_service)allow(sync_service).to receive(:call) do |file_links|diff --git a/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb b/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rbindex f1db282004e..31011698613 100644--- a/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb+++ b/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb@@ -43,20 +43,23 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json doshared_let(:user_without_project) { create(:user) }shared_let(:admin) { create(:admin) }shared_let(:oauth_application) { create(:oauth_application) }- shared_let(:storage) { create(:nextcloud_storage, creator: user_with_permissions, oauth_application:) }+ shared_let(:oauth_client) { create(:oauth_client) }+ shared_let(:storage) { create(:nextcloud_storage, creator: user_with_permissions, oauth_application:, oauth_client:) }shared_let(:project_storage) { create(:project_storage, project:, storage:) }- let(:authorize_url) { 'https://example.com/authorize' }- let(:connection_manager) { instance_double(OAuthClients::ConnectionManager) }let(:current_user) { user_with_permissions }+ let(:auth_check_result) { ServiceResult.success }subject(:last_response) doget pathendbefore do- allow(connection_manager).to receive_messages(get_authorization_uri: authorize_url, authorization_state: :connected)- allow(OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)+ Storages::Peripherals::Registry.stub(+ "#{storage.short_provider_type}.queries.auth_check",+ ->(_) { auth_check_result }+ )+login_as current_userend@@ -233,17 +236,13 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json doshared_examples 'a storage authorization result' do |expected:, has_authorize_link:|subject { last_response.body }- before do- allow(connection_manager).to receive(:authorization_state).and_return(authorization_state)- end-it "returns #{expected}" doexpect(subject).to be_json_eql(expected.to_json).at_path('_links/authorizationState/href')endit "has #{has_authorize_link ? '' : 'no'} authorize link" doif has_authorize_link- expect(subject).to be_json_eql(authorize_url.to_json).at_path('_links/authorize/href')+ expect(subject).to have_json_path('_links/authorize/href')elseexpect(subject).not_to have_json_path('_links/authorize/href')end@@ -251,7 +250,7 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json doendcontext 'when authorization succeeds and storage is connected' do- let(:authorization_state) { :connected }+ let(:auth_check_result) { ServiceResult.success }include_examples 'a storage authorization result',expected: API::V3::Storages::URN_CONNECTION_CONNECTED,@@ -259,7 +258,7 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json doendcontext 'when authorization fails' do- let(:authorization_state) { :failed_authorization }+ let(:auth_check_result) { ServiceResult.failure(errors: Storages::StorageError.new(code: :unauthorized)) }include_examples 'a storage authorization result',expected: API::V3::Storages::URN_CONNECTION_AUTH_FAILED,@@ -267,7 +266,7 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json doendcontext 'when authorization fails with an error' do- let(:authorization_state) { :error }+ let(:auth_check_result) { ServiceResult.failure(errors: Storages::StorageError.new(code: :error)) }include_examples 'a storage authorization result',expected: API::V3::Storages::URN_CONNECTION_ERROR,diff --git a/spec/services/oauth_clients/connection_manager_spec.rb b/spec/services/oauth_clients/connection_manager_spec.rbindex b89a424a719..fb464a3dfa5 100644--- a/spec/services/oauth_clients/connection_manager_spec.rb+++ b/spec/services/oauth_clients/connection_manager_spec.rb@@ -493,92 +493,6 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model doendend- describe '#authorization_state' do- subject { instance.authorization_state }-- context 'without access token present' do- it 'returns :failed_authorization' do- expect(subject).to eq :failed_authorization- expect(instance).to be_authorization_state_failed_authorization- end- end-- context 'with access token present', :webmock do- before do- oauth_client_token- end-- context 'with access token valid' do- context 'without other errors or exceptions' do- before do- allow(configuration).to receive(:authorization_state_check).and_return(:success)- end-- it 'returns :connected' do- expect(subject).to eq :connected- expect(instance).to be_authorization_state_connected- end- end-- context 'with some other error or exception' do- before do- allow(configuration).to receive(:authorization_state_check).and_return(:error)- end-- it 'returns :error' do- expect(subject).to eq :error- expect(instance).to be_authorization_state_error- end- end- end-- context 'with outdated access token' do- let(:new_oauth_client_token) { create(:oauth_client_token) }- let(:refresh_service_result) { ServiceResult.success }-- before do- allow(configuration).to receive(:authorization_state_check).and_return(:refresh_needed)- allow(instance).to receive(:refresh_token).and_return(refresh_service_result)- end-- context 'with valid refresh token' do- it 'refreshes the access token and returns :connected' do- expect(subject).to eq :connected- expect(instance).to have_received(:refresh_token)- end- end-- context 'with invalid refresh token' do- let(:refresh_service_result) do- data = Storages::StorageErrorData.new(source: nil, payload: { error: 'invalid_request' })- ServiceResult.failure(result: :bad_request,- errors: Storages::StorageError.new(code: :bad_request, data:))- end-- it 'refreshes the access token and returns :failed_authorization' do- expect(subject).to eq :failed_authorization- expect(instance).to have_received(:refresh_token)- end- end-- context 'with some other error while refreshing access token' do- let(:refresh_service_result) { ServiceResult.failure }-- it 'returns :error' do- expect(subject).to eq :error- expect(instance).to have_received(:refresh_token)- end- end- end- end-- context 'with both invalid access token and refresh token', :webmock do- it 'returns :failed_authorization' do- expect(subject).to eq :failed_authorization- end- end- end-describe '#request_with_token_refresh' dolet(:yield_service_result) { ServiceResult.success }let(:refresh_service_result) { ServiceResult.success }diff --git a/spec/services/oauth_clients/one_drive_connection_manager_spec.rb b/spec/services/oauth_clients/one_drive_connection_manager_spec.rbindex 9f530e21b35..76ddc524188 100644--- a/spec/services/oauth_clients/one_drive_connection_manager_spec.rb+++ b/spec/services/oauth_clients/one_drive_connection_manager_spec.rb@@ -133,136 +133,4 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model doendendend-- describe '#authorization_state' do- subject(:authorization_state) { connection_manager.authorization_state }-- context 'without access token present' do- it 'returns :failed_authorization' do- expect(authorization_state).to eq :failed_authorization- end- end-- context 'with access token present', :webmock do- before { token }-- context 'with access token valid' do- context 'without other errors or exceptions' do- before { stub_request(:get, 'https://graph.microsoft.com/v1.0/me').to_return(status: 200) }-- it 'returns :connected' do- expect(authorization_state).to eq :connected- end- end-- context 'with some other error or exception' do- before { stub_request(:get, 'https://graph.microsoft.com/v1.0/me').to_timeout }-- it 'returns :error' do- expect(authorization_state).to eq :error- end- end- end-- context 'with outdated access token' do- shared_examples 'refresh' do |_code|- before do- stub_request(:get, 'https://graph.microsoft.com/v1.0/me').to_return(status: 401)- end-- context 'with valid refresh token' do- it 'refreshes the access token and returns :connected' do- expect(authorization_state).to eq :connected- end- end-- context 'with invalid refresh token' do- it 'refreshes the access token and returns :failed_authorization' do- allow(token).to receive(:updated_at).and_return(3.months.ago)- expect(authorization_state).to eq :failed_authorization- end- end-- context 'with some other error while refreshing access token' do- it 'returns :error' do- expect(authorization_state).to eq :error- end- end- end-- context 'when Unauthorized is returned' do- before do- stub_request(:get, 'https://graph.microsoft.com/v1.0/me').to_return(status: 401)- end-- context 'with valid refresh token' do- before do- stub_request(:post, 'https://login.microsoftonline.com/consumers/oauth2/v2.0/token')- .to_return(status: 200,- body: { access_token: 'ThereIsNoPeaceOnlyPassion' }.to_json,- headers: { 'Content-Type' => 'application/json' })- end-- it 'refreshes the access token and returns :connected' do- expect(authorization_state).to eq :connected- end- end-- context 'with invalid refresh token' do- before do- token.update!(updated_at: 3.months.ago)- stub_request(:post, 'https://login.microsoftonline.com/consumers/oauth2/v2.0/token')- .to_return(status: 400,- body: { error: 'invalid_request', error_message: 'nope. not happening' }.to_json,- headers: { 'Content-Type' => 'application/json' })- end-- it 'refreshes the access token and returns :failed_authorization' do- expect(authorization_state).to eq :failed_authorization- end- end-- context 'with some other error while refreshing access token' do- it 'returns :error' do- token.update!(updated_at: 3.months.ago)-- stub_request(:get, 'https://graph.microsoft.com/v1.0/me').to_return(status: 401)- stub_request(:post, 'https://login.microsoftonline.com/consumers/oauth2/v2.0/token')- .to_return(status: 400,- body: { error: 'you_error', error_message: 'it is not me, it is you' }.to_json,- headers: { 'Content-Type' => 'application/json' })-- expect(authorization_state).to eq :error- end- end- end-- context 'when Forbidden is returned' do- before do- stub_request(:get, 'https://graph.microsoft.com/v1.0/me').to_return(status: 403)- end-- context 'with valid refresh token' do- it 'refreshes the access token and returns :connected' do- expect(authorization_state).to eq :connected- end- end-- context 'with invalid refresh token' do- before do- token.update!(updated_at: 3.months.ago)- stub_request(:post, 'https://login.microsoftonline.com/consumers/oauth2/v2.0/token')- .to_return(status: 400,- body: { error: 'invalid_request', error_message: 'nope. not happening' }.to_json,- headers: { 'Content-Type' => 'application/json' })- end-- it 'refreshes the access token and returns :failed_authorization' do- expect(authorization_state).to eq :failed_authorization- end- end- end- end- end- endend