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

Failed HTTP requests do not raise errors from TOKEN_OBTAINED_EVENT

Jan Sandbrink6785cf87e796
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index f5c75376fc5..6cd40bdb606 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -102,6 +102,7 @@ module OAuthClients
update_oauth_client_token(oauth_client_token, service_result.result)
else
rack_access_token = service_result.result
+ id_create_error = nil
OAuthClientToken.transaction do
oauth_client_token = OAuthClientToken.new(
user: @user,
@@ -116,9 +117,14 @@ module OAuthClients
RemoteIdentities::CreateService
.call(user: @user, integration: @oauth_client.integration, token: oauth_client_token)
- .on_failure { raise ActiveRecord::Rollback }
+ .on_failure do |e|
+ id_create_error = e
+ raise ActiveRecord::Rollback
+ end
end
+ return id_create_error if id_create_error
+
ServiceResult.new(success: oauth_client_token.errors.empty?,
result: oauth_client_token,
errors: oauth_client_token.errors)
diff --git a/app/services/remote_identities/create_service.rb b/app/services/remote_identities/create_service.rb
index fe01cbb893c..5bd33a8adf8 100644
--- a/app/services/remote_identities/create_service.rb
+++ b/app/services/remote_identities/create_service.rb
@@ -46,7 +46,10 @@ module RemoteIdentities
def call
if @model.new_record?
- @model.origin_user_id = @integration.extract_origin_user_id(@token)
+ user_id = @integration.extract_origin_user_id(@token)
+ return user_id if user_id.failure?
+
+ @model.origin_user_id = user_id.result
if @model.save
OpenProject::Notifications.send(
OpenProject::Events::REMOTE_IDENTITY_CREATED,
diff --git a/app/services/service_result.rb b/app/services/service_result.rb
index e06ed7a838c..d459fc654a7 100644
--- a/app/services/service_result.rb
+++ b/app/services/service_result.rb
@@ -214,8 +214,12 @@ class ServiceResult
def message
if @message
@message
- elsif failure? && errors.is_a?(ActiveModel::Errors)
- errors.full_messages.join(" ")
+ elsif failure?
+ if errors.is_a?(ActiveModel::Errors)
+ errors.full_messages.join(" ")
+ elsif errors.respond_to?(:message)
+ errors.message
+ end
end
end
diff --git a/modules/openid_connect/spec/services/openid_connect/user_tokens/fetch_service_spec.rb b/modules/openid_connect/spec/services/openid_connect/user_tokens/fetch_service_spec.rb
index dd2c7ac469b..a940b65d6de 100644
--- a/modules/openid_connect/spec/services/openid_connect/user_tokens/fetch_service_spec.rb
+++ b/modules/openid_connect/spec/services/openid_connect/user_tokens/fetch_service_spec.rb
@@ -55,6 +55,7 @@ RSpec.describe OpenIDConnect::UserTokens::FetchService, :webmock do
refresh_token: "refresh-token-exchanged",
audiences: [aud]))
end
+ allow(OpenProject::Notifications).to receive(:send)
end
describe "#access_token_for" do
@@ -67,10 +68,13 @@ RSpec.describe OpenIDConnect::UserTokens::FetchService, :webmock do
end
it "emits appropriate event" do
- allow(OpenProject::Notifications)
- .to receive(:send).with(described_class::TOKEN_OBTAINED_EVENT, token: instance_of(OpenIDConnect::UserToken),
- audience: queried_audience).once
- expect(result.value!).to eq(access_token)
+ result.value!
+
+ expect(OpenProject::Notifications).to have_received(:send).with(
+ described_class::TOKEN_OBTAINED_EVENT,
+ token: instance_of(OpenIDConnect::UserToken),
+ audience: queried_audience
+ ).once
end
it "does not create RemoteIdentity if storage with appropriate audience is absent" do
@@ -78,23 +82,15 @@ RSpec.describe OpenIDConnect::UserTokens::FetchService, :webmock do
expect { result }.not_to change(RemoteIdentity, :count)
end
- context "when storage with appropriate audience is present", :storage_server_helpers, :webmock do
- it "raises an error if auth source is not present" do
- storage = create(:nextcloud_storage, storage_audience: existing_audience)
- stub_nextcloud_user_query(storage.host)
+ context "when OpenProject notification raises an error" do
+ let(:error) { StandardError.new("I am an error") }
- expect { result }.to raise_error("RemoteIdentity creation failed: Auth Source can't be blank.")
+ before do
+ allow(OpenProject::Notifications).to receive(:send).and_raise(error)
end
- it "creates remote identity if auth source is present" do
- slug = create(:oidc_provider).slug
- user.identity_url = "#{slug}:qweqweqweqwe"
- user.save
-
- storage = create(:nextcloud_storage, storage_audience: existing_audience)
- stub_nextcloud_user_query(storage.host)
-
- expect { result }.to change(RemoteIdentity, :count).from(0).to(1)
+ it "raises the same error" do
+ expect { result }.to raise_error(error)
end
end
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index be1a3195856..c767edf5479 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -233,10 +233,7 @@ module Storages
::Storages::Peripherals::Registry
.resolve("#{self}.queries.user")
.call(auth_strategy:, storage: self)
- .match(
- on_success: ->(user) { user[:id] },
- on_failure: ->(error) { raise "UserQuery responed with #{error}" }
- )
+ .map { |user| user[:id] } # rubocop:disable Rails/Pluck
end
end
end
diff --git a/modules/storages/app/models/storages/storage_error.rb b/modules/storages/app/models/storages/storage_error.rb
index e1e73276d90..218b13bf640 100644
--- a/modules/storages/app/models/storages/storage_error.rb
+++ b/modules/storages/app/models/storages/storage_error.rb
@@ -49,6 +49,10 @@ module Storages
errors
end
+ def message
+ to_s
+ end
+
def to_s
output = code.to_s
output << " | #{log_message}" unless log_message.nil?
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index 15a129864c1..fb40e45e1b8 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -145,7 +145,9 @@ module OpenProject::Storages
if storage
RemoteIdentities::CreateService
.call(user: token.user, integration: storage, token:)
- .on_failure { |failure| raise "RemoteIdentity creation failed: #{failure.message}" }
+ .on_failure do |failure|
+ Rails.logger.error("RemoteIdentity creation for user #{token.user.id} failed: #{failure.message}")
+ end
end
end
end
diff --git a/modules/storages/spec/lib/notification_subscription_spec.rb b/modules/storages/spec/lib/notification_subscription_spec.rb
new file mode 100644
index 00000000000..079e98c5a9a
--- /dev/null
+++ b/modules/storages/spec/lib/notification_subscription_spec.rb
@@ -0,0 +1,100 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 "Subscriptions to OpenProject::Notification" do # rubocop:disable RSpec/DescribeClass
+ describe "OpenIDConnect::UserTokens::FetchService::TOKEN_OBTAINED_EVENT" do
+ subject(:notification_sent) do
+ OpenProject::Notifications.send(
+ OpenIDConnect::UserTokens::FetchService::TOKEN_OBTAINED_EVENT,
+ token:,
+ audience:
+ )
+ end
+
+ let(:token) { create(:oidc_user_token) }
+ let(:audience) { "the-storage-audience" }
+ let(:id_create_result) { ServiceResult.success }
+
+ let!(:storage) { create(:nextcloud_storage, storage_audience: "the-storage-audience") }
+
+ before do
+ allow(RemoteIdentities::CreateService).to receive(:call).and_return(id_create_result)
+ end
+
+ it "creates a remote identity" do
+ notification_sent
+
+ expect(RemoteIdentities::CreateService).to have_received(:call).with(user: token.user, integration: storage, token:)
+ end
+
+ context "when obtained token is for an audience that's not a storage" do
+ let(:audience) { "not-the-storage-audience" }
+
+ it "does not create a remote identity for the storage" do
+ notification_sent
+
+ expect(RemoteIdentities::CreateService).not_to have_received(:call)
+ end
+ end
+
+ context "when creation of remote identity raises an error" do
+ let(:error) { StandardError.new("ouch, that hurt") }
+
+ before do
+ allow(RemoteIdentities::CreateService).to receive(:call).and_raise(error)
+ end
+
+ it "raises the same error" do
+ expect { notification_sent }.to raise_error(error)
+ end
+ end
+
+ context "when creation of remote identity fails" do
+ let(:id_create_result) { ServiceResult.failure(errors: StandardError.new(message)) }
+ let(:message) { "ouch, that was bad" }
+
+ before do
+ allow(Rails.logger).to receive(:error)
+ end
+
+ it "raises no error" do
+ expect { notification_sent }.not_to raise_error
+ end
+
+ it "logs an error" do
+ notification_sent
+
+ expect(Rails.logger).to have_received(:error).with(/#{message}/)
+ end
+ end
+ end
+end
diff --git a/spec/services/oauth_clients/connection_manager_one_drive_spec.rb b/spec/services/oauth_clients/connection_manager_one_drive_spec.rb
index 4c843461c01..ed758d94c7c 100644
--- a/spec/services/oauth_clients/connection_manager_one_drive_spec.rb
+++ b/spec/services/oauth_clients/connection_manager_one_drive_spec.rb
@@ -74,8 +74,14 @@ RSpec.describe OAuthClients::ConnectionManager, :oauth_connection_helpers, :webm
.to_return(status: 404)
end
- it "raises an error" do
- expect { subject.code_to_token(code) }.to raise_error(RuntimeError, /Storages::StorageErrorData.*@status=404/)
+ it "returns a failure", :aggregate_failures do
+ result = subject.code_to_token(code)
+ expect(result).to be_failure
+ expect(result.errors).to be_a(Storages::StorageError)
+ end
+
+ it "does not create a token" do
+ expect { subject.code_to_token(code) }.not_to change(OAuthClientToken, :count)
end
end
end