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
Make sure that remote identity is updated from ConnectionManager
Jan Sandbrinke5fc797f251a
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rbindex 6cd40bdb606..1037fcc1446 100644--- a/app/services/oauth_clients/connection_manager.rb+++ b/app/services/oauth_clients/connection_manager.rb@@ -93,7 +93,7 @@ module OAuthClientsdef code_to_token(code)# Return a Rack::OAuth2::AccessToken::Bearer or an error stringservice_result = request_new_token(authorization_code: code)- return service_result unless service_result.success?+ return service_result if service_result.failure?# Check for existing OAuthClientToken and update,# or create a new one from Rack::OAuth::AccessToken::Bearer@@ -116,7 +116,7 @@ module OAuthClientsoauth_client_token.save!RemoteIdentities::CreateService- .call(user: @user, integration: @oauth_client.integration, token: oauth_client_token)+ .call(user: @user, integration: @oauth_client.integration, token: oauth_client_token, force_update: true).on_failure do |e|id_create_error = eraise ActiveRecord::Rollbackdiff --git a/app/services/remote_identities/create_service.rb b/app/services/remote_identities/create_service.rbindex 5bd33a8adf8..59373bb2a8d 100644--- a/app/services/remote_identities/create_service.rb+++ b/app/services/remote_identities/create_service.rb@@ -32,36 +32,45 @@ module RemoteIdentitiesclass CreateServiceattr_reader :user, :model- def self.call(user:, integration:, token:)- new(user:, integration:, token:).call+ def self.call(user:, integration:, token:, force_update: false)+ new(user:, integration:, token:, force_update:).callend- def initialize(user:, integration:, token:)+ def initialize(user:, integration:, token:, force_update: false)@user = user@integration = integration@token = token+ @force_update = force_update@model = RemoteIdentity.find_or_initialize_by(user:, auth_source: token.auth_source, integration:)enddef call- if @model.new_record?+ if @model.new_record? || @force_updateuser_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,- integration: @integration- )- ServiceResult.success(result: @model)- else- ServiceResult.failure(result: @model, errors: @model.errors)- end- else- ServiceResult.success(result: @model)+ return success unless @model.changed?+ return failure unless @model.save++ OpenProject::Notifications.send(+ OpenProject::Events::REMOTE_IDENTITY_CREATED,+ integration: @integration+ )end++ success+ end++ private++ def success+ ServiceResult.success(result: @model)+ end++ def failure+ ServiceResult.failure(result: @model, errors: @model.errors)endendenddiff --git a/spec/services/remote_identities/create_service_spec.rb b/spec/services/remote_identities/create_service_spec.rbindex 21ccd74bed4..566d1ea67ae 100644--- a/spec/services/remote_identities/create_service_spec.rb+++ b/spec/services/remote_identities/create_service_spec.rb@@ -4,7 +4,7 @@ require "spec_helper"require "services/base_services/behaves_like_create_service"-RSpec.describe RemoteIdentities::CreateService, :storage_server_helpers, :webmock, type: :model do+RSpec.describe RemoteIdentities::CreateService, :storage_server_helpers, type: :model dolet(:user) { create(:user) }let(:integration) { create(:nextcloud_storage_configured) }let(:oauth_config) { integration.oauth_configuration }@@ -17,16 +17,24 @@ RSpec.describe RemoteIdentities::CreateService, :storage_server_helpers, :webmocsubject(:service) { described_class.new(user:, token: oauth_client_token, integration:) }- before { stub_nextcloud_user_query(integration.host) }+ before do+ allow(OpenProject::Notifications).to receive(:send)+ allow(integration).to receive(:extract_origin_user_id).and_return(ServiceResult.success(result: "the-extracted-user-id"))+ enddescribe ".call" doit "requires certain parameters" domethod = described_class.method :call- expect(method.parameters).to contain_exactly(%i[keyreq user], %i[keyreq token], %i[keyreq integration])+ expect(method.parameters).to contain_exactly(+ %i[keyreq user],+ %i[keyreq token],+ %i[keyreq integration],+ %i[key force_update]+ )end- it "succeeds", :webmock do+ it "succeeds" doexpect(described_class.call(user:, token: oauth_client_token, integration:)).to be_successendend@@ -50,23 +58,66 @@ RSpec.describe RemoteIdentities::CreateService, :storage_server_helpers, :webmocit "sets origin_user_id" doexpect { service.call.result }.to change {RemoteIdentity.pluck(:origin_user_id)- }.from([]).to(["admin"])+ }.from([]).to(["the-extracted-user-id"])end- it "emits only one event for a number of subsequent calls if model has not changed" do- allow(OpenProject::Notifications)- .to receive(:send).with(OpenProject::Events::REMOTE_IDENTITY_CREATED, integration:).once- 3.times { service.call.result }+ context "when calling multiple times, without the model changing in-between" do+ before do+ 2.times { service.call }+ end++ it "emits only one event" do+ expect(OpenProject::Notifications).to have_received(:send).with(+ OpenProject::Events::REMOTE_IDENTITY_CREATED,+ integration:+ ).once+ end++ it "only queries for the origin_user_id once" do+ expect(integration).to have_received(:extract_origin_user_id).once+ endend- it "emits two events for 2 subsequent calls if model has changed between them" do- allow(OpenProject::Notifications)- .to receive(:send).with(OpenProject::Events::REMOTE_IDENTITY_CREATED, integration:).twice+ context "when calling multiple times, with changes to the model in between" do+ before do+ model = service.call.result+ model.update!(origin_user_id: "the-changed-user-id")+ service.call+ end++ it "emits only one event" do+ expect(OpenProject::Notifications).to have_received(:send).with(+ OpenProject::Events::REMOTE_IDENTITY_CREATED,+ integration:+ ).once+ end++ it "only queries for the origin_user_id once" do+ expect(integration).to have_received(:extract_origin_user_id).once+ end++ it "does not undo changes to the model" do+ expect(RemoteIdentity.last.origin_user_id).to eq("the-changed-user-id")+ end++ context "when the force_update flag is enabled" do+ subject(:service) { described_class.new(user:, token: oauth_client_token, integration:, force_update: true) }++ it "emits multiple events" do+ expect(OpenProject::Notifications).to have_received(:send).with(+ OpenProject::Events::REMOTE_IDENTITY_CREATED,+ integration:+ ).twice+ end++ it "queries for the origin_user_id again" do+ expect(integration).to have_received(:extract_origin_user_id).twice+ end- model = service.call.result- model.origin_user_id = "123123"- model.save- service.call.result+ it "updates the model" do+ expect(RemoteIdentity.last.origin_user_id).to eq("the-extracted-user-id")+ end+ endendendend