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

Fix spec due to validation change

Marcello Rochaf3162f37c710
diff --git a/app/services/oauth_clients/one_drive_connection_manager.rb b/app/services/oauth_clients/one_drive_connection_manager.rb
index 73edb94b18d..975761554fc 100644
--- a/app/services/oauth_clients/one_drive_connection_manager.rb
+++ b/app/services/oauth_clients/one_drive_connection_manager.rb
@@ -44,25 +44,20 @@ module OAuthClients
current_token = get_existing_token
return :failed_authorization unless current_token
- response = Net::HTTP.start(GRAPH_API_URI.host.host, GRAPH_API_URI.host.port, use_ssl: true) do |http|
- http.get('/v1.0/me', { 'Authorization' => "Bearer #{oauth_client_token.access_token}" })
+ response = Net::HTTP.start(GRAPH_API_URI.host, GRAPH_API_URI.port, use_ssl: true) do |http|
+ http.get('/v1.0/me', { 'Authorization' => "Bearer #{current_token.access_token}" })
end
case response
when Net::HTTPSuccess
:connected
when Net::HTTPForbidden, Net::HTTPUnauthorized
- service_result = refresh_token # `refresh_token` already has exception handling
+ service_result = refresh_token
if service_result.success?
:connected
elsif service_result.result == 'invalid_request'
- # This can happen if the Authorization Server invalidated all tokens.
- # Then the user would ideally be asked to reauthorize.
:failed_authorization
else
- # It could also be that some other error happened, i.e. firewall badly configured.
- # Then the user needs to know that something is technically off. The user could try
- # to reload the page or contact an admin.
:error
end
else
@@ -73,7 +68,7 @@ module OAuthClients
end
def get_authorization_uri(scope: [], state: nil)
- joined_scope = (scope | DEFAULT_SCOPES).join(' ')
+ joined_scope = Array(scope) | DEFAULT_SCOPES
super(scope: joined_scope, state:)
end
diff --git a/spec/services/oauth_clients/connection_manager_spec.rb b/spec/services/oauth_clients/connection_manager_spec.rb
index 0e9297a1c60..6d8852ffc0e 100644
--- a/spec/services/oauth_clients/connection_manager_spec.rb
+++ b/spec/services/oauth_clients/connection_manager_spec.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2023 the OpenProject GmbH
@@ -344,7 +346,7 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model do
before do
# Simulate a token too long
response_body = {
- access_token: "x" * 257, # will fail model validation
+ access_token: nil, # will fail model validation
token_type: "Bearer",
expires_in: 3601,
refresh_token: "xUwFp...1FROJ",
@@ -356,9 +358,7 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model do
it 'returns dependent error from model validation', webmock: true do
expect(subject.success).to be_falsey
- expect(subject.result).to be_nil
expect(subject.errors.size).to be(1)
- puts subject.errors
end
end
@@ -371,7 +371,6 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model do
it 'returns a server error', webmock: true do
expect(subject.success).to be_falsey
expect(subject.errors.size).to be(1)
- puts subject.errors
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
new file mode 100644
index 00000000000..6a63700302a
--- /dev/null
+++ b/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
@@ -0,0 +1,200 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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.
+#++
+
+require 'spec_helper'
+
+RSpec.describe OAuthClients::OneDriveConnectionManager, webmock: true 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:) }
+
+ subject(:connection_manager) do
+ described_class.new(user:, oauth_client: storage.oauth_client, tenant_id: storage.tenant_id)
+ end
+
+ describe '#get_authorization_uri' do
+ it 'always add the necessary scopes' do
+ uri = connection_manager.get_authorization_uri(scope: nil, state: nil)
+
+ expect(uri).to include OAuthClients::OneDriveConnectionManager::DEFAULT_SCOPES.join('%20')
+ end
+
+ it 'adds the state if present' do
+ uri = connection_manager.get_authorization_uri(scope: nil, state: 'https://some.site.com')
+ expect(uri).to include "&state=https"
+ end
+ end
+
+ describe '#get_access_token' do
+ subject(:access_token_result) { connection_manager.get_access_token }
+
+ context 'with no OAuthClientToken present' do
+ it 'returns a redirection URL' do
+ expect(access_token_result).to be_failure
+ expect(access_token_result.result).to eq(connection_manager.get_authorization_uri)
+ end
+ end
+
+ context 'with an OAuthClientToken present' do
+ before { token }
+
+ it 'returns the OAuthClientToken' do
+ expect(access_token_result).to be_truthy
+ expect(access_token_result.result).to be_a OAuthClientToken # The one and only...
+ expect(access_token_result.result).to eql token
+ end
+ end
+ end
+
+ 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: true 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 Unathorized 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
+ 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
+ end
+end