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

[#49113] fixed 20000 redundant test cases for connection manager

Eric Schubert00caa2e602f3
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index c309ab1d62f..45c05a1dd1f 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -158,7 +158,7 @@ module OAuthClients
service_result = refresh_token
if service_result.success?
:connected
- elsif service_result.result == 'invalid_request'
+ elsif service_result.errors.data.payload[:error] == 'invalid_request'
:failed_authorization
else
:error
@@ -203,15 +203,17 @@ module OAuthClients
ServiceResult.success(result: rack_access_token)
rescue Rack::OAuth2::Client::Error => e
- service_result_with_error(e.response, i18n_rack_oauth2_error_message(e))
+ service_result_with_error(:bad_request, e.response, i18n_rack_oauth2_error_message(e))
rescue Faraday::TimeoutError, Faraday::ConnectionFailed, Faraday::ParsingError, Faraday::SSLError => e
service_result_with_error(
- e.response,
+ :internal_server_error,
+ e,
"#{I18n.t('oauth_client.errors.oauth_returned_http_error')}: #{e.class}: #{e.message.to_html}"
)
rescue StandardError => e
service_result_with_error(
- e.response,
+ :error,
+ e,
"#{I18n.t('oauth_client.errors.oauth_returned_standard_error')}: #{e.class}: #{e.message.to_html}"
)
end
@@ -258,10 +260,10 @@ module OAuthClients
end
end
- def service_result_with_error(data, log_message = nil)
+ def service_result_with_error(code, data, log_message = nil)
error_data = ::Storages::StorageErrorData.new(source: self, payload: data)
- ServiceResult.failure(result: :bad_request,
- errors: ::Storages::StorageError.new(code: :bad_request, data: error_data, log_message:))
+ ServiceResult.failure(result: code,
+ errors: ::Storages::StorageError.new(code:, data: error_data, log_message:))
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/service_result_refinements.rb b/modules/storages/app/common/storages/peripherals/service_result_refinements.rb
index 3c9b813a583..7385c7ceed8 100644
--- a/modules/storages/app/common/storages/peripherals/service_result_refinements.rb
+++ b/modules/storages/app/common/storages/peripherals/service_result_refinements.rb
@@ -27,6 +27,11 @@
#++
module Storages::Peripherals
+ # rubocop:disable Lint/EmptyClass
+ class UnknownSource; end
+
+ # rubocop:enable Lint/EmptyClass
+
module ServiceResultRefinements
refine ServiceResult do
def match(on_success:, on_failure:)
@@ -50,6 +55,18 @@ module Storages::Peripherals
bind(&other)
end
+
+ def error_source
+ if errors.is_a?(::Storages::StorageError) && errors.data&.source.present?
+ errors.data.source
+ else
+ UnknownSource
+ end
+ end
+
+ def error_payload
+ errors.data&.payload
+ end
end
end
end
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.rb
index 1375b6ff9f3..4726eb267a7 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
@@ -57,7 +57,7 @@ module Storages
result = file_ids.map do |file_id|
file_info_result = FileInfoQuery.call(storage: @storage, user:, file_id:)
if file_info_result.failure? &&
- file_info_result.errors.data.source.is_a?(::OAuthClients::ConnectionManager)
+ file_info_result.error_source.is_a?(::OAuthClients::ConnectionManager)
# errors in the connection manager must short circuit the query and return the error
return file_info_result
end
@@ -74,11 +74,10 @@ module Storages
if query_result.success?
query_result.result
else
- storage_error = query_result.errors
StorageFileInfo.new(
id: file_id,
- status: storage_error.data.payload.dig(:error, :code),
- status_code: Rack::Utils::SYMBOL_TO_STATUS_CODE[storage_error.code]
+ status: query_result.error_payload.dig(:error, :code),
+ status_code: Rack::Utils::SYMBOL_TO_STATUS_CODE[query_result.errors.code]
)
end
end
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.rb
index 32371a640b3..6bf0a7d6bea 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
@@ -90,7 +90,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FileInfoQuer
expect(storage_files).to be_failure
expect(storage_files.result).to eq(:not_found)
- expect(storage_files.errors.data.to_json).to eq(not_found_json)
+ expect(storage_files.errors.data.payload.to_json).to eq(not_found_json)
end
it 'returns a forbidden error if the API call returns a 403' do
@@ -102,7 +102,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FileInfoQuer
expect(storage_files).to be_failure
expect(storage_files.result).to eq(:forbidden)
- expect(storage_files.errors.data.to_json).to eq(forbidden_json)
+ expect(storage_files.errors.data.payload.to_json).to eq(forbidden_json)
end
end
end
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_link_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_link_query_spec.rb
index d8a7d1dbfad..0c0786e52d5 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_link_query_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_link_query_spec.rb
@@ -84,7 +84,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::OpenLinkQuer
expect(open_link_result).to be_failure
expect(open_link_result.result).to eq(:not_found)
- expect(open_link_result.errors.data.to_json).to eq(not_found_json)
+ expect(open_link_result.errors.data.payload.to_json).to eq(not_found_json)
end
it 'returns a forbidden error if the API call returns a 403' do
@@ -96,7 +96,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::OpenLinkQuer
expect(open_link_result).to be_failure
expect(open_link_result.result).to eq(:forbidden)
- expect(open_link_result.errors.data.to_json).to eq(forbidden_json)
+ expect(open_link_result.errors.data.payload.to_json).to eq(forbidden_json)
end
end
end
diff --git a/spec/services/oauth_clients/connection_manager_spec.rb b/spec/services/oauth_clients/connection_manager_spec.rb
index e6a4179cd92..0d6bb5998db 100644
--- a/spec/services/oauth_clients/connection_manager_spec.rb
+++ b/spec/services/oauth_clients/connection_manager_spec.rb
@@ -31,6 +31,8 @@
require 'spec_helper'
RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
+ using Storages::Peripherals::ServiceResultRefinements
+
let(:user) { create(:user) }
let(:host) { "https://example.org" }
@@ -105,7 +107,7 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
context 'with no OAuthClientToken present' do
it 'returns a redirection URL' do
- expect(subject.success).to be_falsey
+ expect(subject.success).to be_falsy
expect(subject.result).to be_a String
# Details of string are tested above in section #get_authorization_uri
end
@@ -117,7 +119,7 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
it 'returns the redirect URL' do
allow(configuration).to receive(:scope).and_return(%w[email])
- expect(subject.success).to be_falsey
+ expect(subject.success).to be_falsy
expect(subject.result).to be_a String
expect(subject.result).to include oauth_client.integration.host
expect(subject.result).to include "&state=some_state"
@@ -183,10 +185,9 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
shared_examples 'OAuth2 error response' do
it 'returns a specific error message' do
- expect(subject.success).to be_falsey
- expect(subject.result).to eq error_message
- expect(subject.errors[:base].count).to be(1)
- expect(subject.errors[:base].first).to include I18n.t("oauth_client.errors.rack_oauth2.#{error_message}")
+ expect(subject.success).to be_falsy
+ expect(subject.result).to eq(:bad_request)
+ expect(subject.error_payload[:error]).to eq(error_message)
end
end
@@ -203,22 +204,6 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
end
end
- context 'with known reply invalid_grant' do
- before do
- stub_request(:post, File.join(host, '/index.php/apps/oauth2/api/v1/token'))
- .to_return(status: 400,
- body: { error: "invalid_grant" }.to_json,
- headers: { "content-type" => "application/json; charset=utf-8" })
- end
-
- it 'returns a specific error message' do
- expect(subject.success).to be_falsey
- expect(subject.result).to eq 'invalid_grant'
- expect(subject.errors[:base].count).to be(1)
- expect(subject.errors[:base].first).to include I18n.t('oauth_client.errors.rack_oauth2.invalid_grant')
- end
- end
-
context 'with unknown reply' do
before do
stub_request(:post, File.join(host, '/index.php/apps/oauth2/api/v1/token'))
@@ -227,11 +212,12 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
headers: { "content-type" => "application/json; charset=utf-8" })
end
- it 'returns an unspecific error message' do
- expect(subject.success).to be_falsey
- expect(subject.result).to eq 'invalid_requesttt'
- expect(subject.errors[:base].count).to be(1)
- expect(subject.errors[:base].first).to include I18n.t('oauth_client.errors.oauth_returned_error')
+ it 'returns an error wrapping the unknown response' do
+ expect(subject.success).to be_falsy
+ expect(subject.result).to eq(:bad_request)
+ expect(subject.error_payload[:error]).to eq('invalid_requesttt')
+ expect(subject.error_source).to be_a(described_class)
+ expect(subject.errors.log_message).to include I18n.t('oauth_client.errors.oauth_returned_error')
end
end
@@ -245,11 +231,12 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
)
end
- it 'returns an unspecific error message' do
- expect(subject.success).to be_falsey
- expect(subject.result).to eq "unexpected token at 'some: very, invalid> <json}'"
- expect(subject.errors[:base].count).to be(1)
- expect(subject.errors[:base].first).to include I18n.t('oauth_client.errors.oauth_returned_http_error')
+ it 'returns an error wrapping the parsing error' do
+ expect(subject.success).to be_falsy
+ expect(subject.result).to eq(:internal_server_error)
+ expect(subject.error_payload.class).to be(Faraday::ParsingError)
+ expect(subject.error_source).to be_a(described_class)
+ expect(subject.errors.log_message).to include I18n.t('oauth_client.errors.oauth_returned_http_error')
end
end
@@ -259,11 +246,10 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
.to_return(status: 500)
end
- it 'returns an unspecific error message' do
- expect(subject.success).to be_falsey
- expect(subject.result).to eq 'Unknown :: '
- expect(subject.errors[:base].count).to be(1)
- expect(subject.errors[:base].first).to include I18n.t('oauth_client.errors.oauth_returned_error')
+ it 'returns an error wrapping the empty error' do
+ expect(subject.success).to be_falsy
+ expect(subject.result).to eq(:bad_request)
+ expect(subject.error_payload[:error]).to eq('Unknown')
end
end
@@ -272,11 +258,12 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
stub_request(:post, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_raise(Faraday::ConnectionFailed)
end
- it 'returns an unspecific error message' do
- expect(subject.success).to be_falsey
- expect(subject.result).to eq("Exception from WebMock")
- expect(subject.errors[:base].count).to be(1)
- expect(subject.errors[:base].first).to include I18n.t('oauth_client.errors.oauth_returned_http_error')
+ it 'returns an error wrapping the server error' do
+ expect(subject.success).to be_falsy
+ expect(subject.result).to eq(:internal_server_error)
+ expect(subject.error_payload.class).to be(Faraday::ConnectionFailed)
+ expect(subject.error_source).to be_a(described_class)
+ expect(subject.errors.log_message).to include I18n.t('oauth_client.errors.oauth_returned_http_error')
end
end
@@ -285,11 +272,12 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
stub_request(:post, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_raise(Faraday::SSLError)
end
- it 'returns an unspecific error message' do
- expect(subject.success).to be_falsey
- expect(subject.result).to eq("Exception from WebMock")
- expect(subject.errors[:base].count).to be(1)
- expect(subject.errors[:base].first).to include I18n.t('oauth_client.errors.oauth_returned_http_error')
+ it 'returns an error wrapping the server error' do
+ expect(subject.success).to be_falsy
+ expect(subject.result).to eq(:internal_server_error)
+ expect(subject.error_payload.class).to be(Faraday::SSLError)
+ expect(subject.error_source).to be_a(described_class)
+ expect(subject.errors.log_message).to include I18n.t('oauth_client.errors.oauth_returned_http_error')
end
end
@@ -298,11 +286,12 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
stub_request(:post, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_timeout
end
- it 'returns an unspecific error message' do
- expect(subject.success).to be_falsey
- expect(subject.result).to eq("execution expired")
- expect(subject.errors[:base].count).to be(1)
- expect(subject.errors[:base].first).to include I18n.t('oauth_client.errors.oauth_returned_http_error')
+ it 'returns an error wrapping the server timeout' do
+ expect(subject.success).to be_falsy
+ expect(subject.result).to eq(:internal_server_error)
+ expect(subject.error_payload.class).to be(Faraday::ConnectionFailed)
+ expect(subject.error_source).to be_a(described_class)
+ expect(subject.errors.log_message).to include I18n.t('oauth_client.errors.oauth_returned_http_error')
end
end
end
@@ -312,8 +301,9 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
context 'without existing OAuthClientToken' do
it 'returns an error message' do
- expect(subject.success).to be_falsey
- expect(subject.errors[:base].first)
+ expect(subject.success).to be_falsy
+ expect(subject.result).to eq(:error)
+ expect(subject.errors.log_message)
.to include I18n.t('oauth_client.errors.refresh_token_called_without_existing_token')
end
end
@@ -364,8 +354,10 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
end
it 'returns dependent error from model validation', :webmock do
- expect(subject.success).to be_falsey
- expect(subject.errors.size).to be(1)
+ expect(subject.success).to be_falsy
+ expect(subject.result).to eq(:error)
+ expect(subject.error_payload.class).to be(AttrRequired::AttrMissing)
+ expect(subject.error_payload.message).to include("'access_token' required.")
end
end
@@ -378,8 +370,9 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
end
it 'returns a server error', :webmock do
- expect(subject.success).to be_falsey
- expect(subject.errors.size).to be(1)
+ expect(subject.success).to be_falsy
+ expect(subject.result).to eq(:bad_request)
+ expect(subject.error_payload[:error]).to eq('invalid_request')
end
end
@@ -390,14 +383,16 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
.to_timeout
end
- it 'returns a valid ClientToken object', :webmock do
- expect(subject.success).to be_falsey
- expect(subject.result).to eq("execution expired")
- expect(subject.errors.size).to be(1)
+ it 'returns an error wrapping a timeout', :webmock do
+ expect(subject.success).to be_falsy
+ expect(subject.result).to eq(:internal_server_error)
+ expect(subject.error_payload.class).to be(Faraday::ConnectionFailed)
+ expect(subject.error_source).to be_a(described_class)
+ expect(subject.errors.log_message).to include('Faraday::ConnectionFailed: execution expired')
end
end
- context 'with parrallel requests for refresh', :aggregate_failures do
+ context 'with parallel requests for refresh', :aggregate_failures do
after do
Storages::Storage.destroy_all
User.destroy_all
@@ -544,7 +539,11 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
end
context 'with invalid refresh token' do
- let(:refresh_service_result) { ServiceResult.failure(result: 'invalid_request') }
+ 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
@@ -595,7 +594,7 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
let(:yield_service_result) { ServiceResult.failure(result: :error) }
it 'returns a ServiceResult with success, without refreshing the token' do
- expect(subject.success).to be_falsey
+ expect(subject.success).to be_falsy
expect(subject.result).to be :error
expect(instance).not_to have_received(:refresh_token)
expect(oauth_client_token).not_to have_received(:reload)
@@ -606,7 +605,7 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
let(:yield_service_result) { ServiceResult.failure(result: :unauthorized) }
it 'returns a ServiceResult with success, without refresh' do
- expect(subject.success).to be_falsey
+ expect(subject.success).to be_falsy
expect(subject.result).to be :unauthorized
expect(instance).to have_received(:refresh_token)
expect(oauth_client_token).to have_received(:reload)
@@ -615,10 +614,14 @@ RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
context 'with yield returning :unauthorized and the refresh returning with a :failure' do
let(:yield_service_result) { ServiceResult.failure(result: :unauthorized) }
- let(:refresh_service_result) { ServiceResult.failure }
+ let(:refresh_service_result) do
+ data = Storages::StorageErrorData.new(source: nil, payload: { error: 'invalid_request' })
+ ServiceResult.failure(result: :error,
+ errors: Storages::StorageError.new(code: :error, data:))
+ end
it 'returns a ServiceResult with success, without refresh' do
- expect(subject.success).to be_falsey
+ expect(subject.success).to be_falsy
expect(subject.result).to be :error
expect(instance).to have_received(:refresh_token)
expect(oauth_client_token).not_to have_received(:reload)
diff --git a/spec/services/oauth_clients/one_drive_connection_manager_spec.rb b/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
index 9a40f9f370b..c438d92f978 100644
--- a/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
+++ b/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
@@ -30,7 +30,7 @@
require 'spec_helper'
-RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
+RSpec.describe OAuthClients::ConnectionManager, :webmock, type: :model do
let(:user) { create(:user) }
let(:storage) { create(:one_drive_storage, :with_oauth_client, tenant_id: 'consumers') }
let(:token) { create(:oauth_client_token, oauth_client: storage.oauth_client, user:) }
@@ -82,7 +82,7 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
end
end
- context 'with access token present', webmock: true do
+ context 'with access token present', :webmock do
before { token }
context 'with access token valid' do