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

Making more tests pass

Marcello Rocha3519239d48eb
diff --git a/app/controllers/oauth_clients_controller.rb b/app/controllers/oauth_clients_controller.rb
index 9a44a805efc..b7be42bcc58 100644
--- a/app/controllers/oauth_clients_controller.rb
+++ b/app/controllers/oauth_clients_controller.rb
@@ -126,7 +126,10 @@ class OAuthClientsController < ApplicationController
end
def set_connection_manager
- @connection_manager = OAuthClients::ConnectionManager.new(user: User.current, oauth_client: @oauth_client)
+ @connection_manager = OAuthClients::ConnectionManager.new(
+ user: User.current,
+ configuration: @oauth_client.integration.oauth_configuration
+ )
end
def find_oauth_client
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index 9bca7971b75..d5fc4b27191 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -140,7 +140,8 @@ module OAuthClients
oauth_client_token = get_existing_token
return :failed_authorization unless oauth_client_token
- case @config.authorization_state_check(oauth_client_token.access_token)
+ state = @config.authorization_state_check(oauth_client_token.access_token)
+ case state
when :success
:connected
when :refresh_needed
@@ -153,7 +154,7 @@ module OAuthClients
:error
end
else
- response
+ state
end
rescue StandardError
:error
diff --git a/spec/services/oauth_clients/connection_manager_spec.rb b/spec/services/oauth_clients/connection_manager_spec.rb
index ac96b749369..cc7014961da 100644
--- a/spec/services/oauth_clients/connection_manager_spec.rb
+++ b/spec/services/oauth_clients/connection_manager_spec.rb
@@ -36,11 +36,12 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
let(:host) { "https://example.org" }
let(:storage) { create(:nextcloud_storage, :with_oauth_client, host: "#{host}/") }
let(:oauth_client) { storage.oauth_client }
+ let(:configuration) { storage.oauth_configuration }
let(:scope) { [:all] } # OAuth2 resources to access, specific to provider
let(:oauth_client_token) { create(:oauth_client_token, oauth_client:, user:) }
- let(:instance) { described_class.new(user:, configuration: storage.oauth_configuration) }
+ let(:instance) { described_class.new(user:, configuration:) }
# The get_authorization_uri method returns the OAuth2 authorization URI as a string. That URI is the starting point for
# a user to grant OpenProject access to Nextcloud.
@@ -494,8 +495,7 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
context 'with access token valid' do
context 'without other errors or exceptions' do
before do
- stub_request(:get, File.join(host, OAuthClients::ConnectionManager::AUTHORIZATION_CHECK_PATH))
- .to_return(status: 200)
+ allow(configuration).to receive(:authorization_state_check).and_return(:success)
end
it 'returns :connected' do
@@ -505,8 +505,7 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
context 'with some other error or exception' do
before do
- stub_request(:get, File.join(host, OAuthClients::ConnectionManager::AUTHORIZATION_CHECK_PATH))
- .to_timeout
+ allow(configuration).to receive(:authorization_state_check).and_return(:error)
end
it 'returns :error' do
@@ -519,45 +518,34 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
let(:new_oauth_client_token) { create(:oauth_client_token) }
let(:refresh_service_result) { ServiceResult.success }
- shared_examples 'refresh' do |code|
- before do
- stub_request(:get, File.join(host, OAuthClients::ConnectionManager::AUTHORIZATION_CHECK_PATH))
- .to_return(status: code)
- 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) { ServiceResult.failure(result: 'invalid_request') }
+ 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
- it 'refreshes the access token and returns :failed_authorization' do
- expect(subject).to eq :failed_authorization
- expect(instance).to have_received(:refresh_token)
- 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 some other error while refreshing access token' do
- let(:refresh_service_result) { ServiceResult.failure }
+ context 'with invalid refresh token' do
+ let(:refresh_service_result) { ServiceResult.failure(result: 'invalid_request') }
- it 'returns :error' do
- expect(subject).to eq :error
- expect(instance).to have_received(:refresh_token)
- 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 'when Uanthorized is returned' do
- it_behaves_like 'refresh', 401
- end
+ context 'with some other error while refreshing access token' do
+ let(:refresh_service_result) { ServiceResult.failure }
- context 'when Forbidded is returned' do
- it_behaves_like 'refresh', 403
+ it 'returns :error' do
+ expect(subject).to eq :error
+ expect(instance).to have_received(:refresh_token)
+ end
end
end
end
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 930960dd0fd..305d87c9df8 100644
--- a/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
+++ b/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
@@ -36,7 +36,7 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
let(:token) { create(:oauth_client_token, oauth_client: storage.oauth_client, user:) }
subject(:connection_manager) do
- OAuthClients::ConnectionManager.new(user:, oauth_client: nil, configuration: storage.oauth_configuration)
+ described_class.new(user:, configuration: storage.oauth_configuration)
end
describe '#get_authorization_uri' do