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

[#61178] Add test related to RemoteIdentity upd.

Pavel Balashouaae3b48ba519
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 61826319d7f..fbfc5b9461f 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1049,6 +1049,10 @@ en:
is_milestone: "Is milestone"
color: "Color"
patterns: "Patterns"
+ remote_identity:
+ auth_source: "Auth Source"
+ integration: "Integration"
+ user: "User"
user:
admin: "Administrator"
auth_source: "Authentication source"
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 b26a600ba22..2cddefdf98f 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
@@ -66,6 +66,38 @@ RSpec.describe OpenIDConnect::UserTokens::FetchService, :webmock do
expect(result.value!).to eq(access_token)
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)
+ end
+
+ it "does not create RemoteIdentity if storage with appropriate audience is absent" do
+ create(:nextcloud_storage, storage_audience: "not-expected-audience")
+ 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)
+
+ expect { result }.to raise_error("RemoteIdentity creation failed: Auth Source can't be blank.")
+ 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)
+ end
+ end
+
context "when the token doesn't expire and can't be parsed as JWT" do
let(:jwt_parser) { instance_double(OpenIDConnect::JwtParser, parse: Failure("Not a valid JWT")) }
@@ -142,6 +174,13 @@ RSpec.describe OpenIDConnect::UserTokens::FetchService, :webmock do
it { is_expected.to be_success }
+ 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-refreshed")
+ end
+
it "returns the refreshed access token" do
expect(result.value!).to eq("access-token-refreshed")
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
index 8f4eded8f64..42dcbbe621d 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
@@ -74,7 +74,7 @@ module Storages
auth_source = if auth_strategy.key == :oauth_user_token
storage.oauth_client
else
- user.authentication_provider
+ auth_strategy.user.authentication_provider
end
origin_user_id = RemoteIdentity.where(user_id: auth_strategy.user,
auth_source:,
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index 774858fa5d1..15a129864c1 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -145,7 +145,7 @@ module OpenProject::Storages
if storage
RemoteIdentities::CreateService
.call(user: token.user, integration: storage, token:)
- .on_failure { raise "RemoteIdentity creation failed." }
+ .on_failure { |failure| raise "RemoteIdentity creation failed: #{failure.message}" }
end
end
end
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/specific_bearer_token_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/specific_bearer_token_spec.rb
new file mode 100644
index 00000000000..2086eada9e7
--- /dev/null
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/specific_bearer_token_spec.rb
@@ -0,0 +1,49 @@
+# 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"
+require_module_spec_helper
+
+RSpec.describe Storages::Peripherals::StorageInteraction::AuthenticationStrategies::SpecificBearerToken do
+ let(:storage) { create(:nextcloud_storage) }
+ let(:access_token) { "my_access_token" }
+
+ it "must yield with passed access token without" do
+ strategy = described_class.strategy.with_token(access_token)
+ was_yielded = false
+
+ Storages::Peripherals::StorageInteraction::Authentication[strategy].call(storage:) do |http|
+ was_yielded = true
+ expect(http.instance_variable_get(:@options).headers["authorization"]).to eq("Bearer #{access_token}")
+ end
+
+ expect(was_yielded).to be_truthy
+ end
+end
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/util_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/util_spec.rb
new file mode 100644
index 00000000000..6b9ed71f4c5
--- /dev/null
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/util_spec.rb
@@ -0,0 +1,57 @@
+# 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"
+require_module_spec_helper
+
+RSpec.describe(Storages::Peripherals::StorageInteraction::Nextcloud::Util) do
+ describe ".origin_user_id" do
+ it "responds with appropriate origin_user_id when user has two remote identities" \
+ "for one integration within two different auth_sources" do
+ oauth_client = create(:oauth_client)
+ oidc_provider = create(:oidc_provider)
+ integration = create(:nextcloud_storage, oauth_client:)
+ user = create(:user, identity_url: "#{oidc_provider.slug}:123123213")
+ create(:remote_identity, user:, auth_source: oauth_client, integration:, origin_user_id: "456")
+ create(:remote_identity, user:, auth_source: oidc_provider, integration:, origin_user_id: "123")
+ sso_strategy = Storages::Peripherals::StorageInteraction::AuthenticationStrategies::SsoUserToken
+ .strategy
+ .with_user(user)
+ oauth_strategy = Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken
+ .strategy
+ .with_user(user)
+
+ expect(described_class.origin_user_id(caller: self.class, storage: integration,
+ auth_strategy: sso_strategy).result).to eq("123")
+ expect(described_class.origin_user_id(caller: self.class, storage: integration,
+ auth_strategy: oauth_strategy).result).to eq("456")
+ end
+ end
+end
diff --git a/spec/models/remote_identity_spec.rb b/spec/models/remote_identity_spec.rb
index 3e57caf0650..525f27bb5a3 100644
--- a/spec/models/remote_identity_spec.rb
+++ b/spec/models/remote_identity_spec.rb
@@ -33,23 +33,22 @@ require "spec_helper"
RSpec.describe RemoteIdentity do
let(:user) { create(:user) }
let(:integration) { create(:nextcloud_storage) }
- let(:auth_source) { create(:oauth_client) }
+ let(:oauth_client) { create(:oauth_client) }
+ let(:oidc_provider) { create(:oidc_provider) }
- it "a user can have multiple identities in different oauth clients" do
- other_client = create(:oauth_client)
- create(:remote_identity, user:, auth_source:)
- second_identity = build(:remote_identity, user:, auth_source: other_client)
+ it "a user can have multiple identities within one integration for different auth source" do
+ create(:remote_identity, user:, auth_source: oauth_client, integration:)
+ second_identity = build(:remote_identity, user:, auth_source: oidc_provider, integration:)
expect(second_identity).to be_valid
end
- it "a user can only have one identity per oauth client and integration" do
- create(:remote_identity, user:, auth_source:, integration:)
+ it "a user can only have one identity per auth_source and integration" do
+ create(:remote_identity, user:, auth_source: oauth_client, integration:)
- invalid = build(:remote_identity, user:, auth_source:, integration:)
+ invalid = build(:remote_identity, user:, auth_source: oauth_client, integration:)
expect(invalid).not_to be_valid
- p invalid.errors
expect(invalid.errors.size).to eq(1)
end
@@ -59,9 +58,17 @@ RSpec.describe RemoteIdentity do
expect { user.destroy }.to change(described_class, :count).by(-1)
end
- it "is destroyed when the related oauth client is destroyed" do
- create(:remote_identity, auth_source:)
+ it "is destroyed when the related auth source is destroyed" do
+ create(:remote_identity, auth_source: oauth_client)
+ create(:remote_identity, auth_source: oidc_provider)
- expect { auth_source.destroy }.to change(described_class, :count).by(-1)
+ expect { oauth_client.destroy }.to change(described_class, :count).by(-1)
+ expect { oidc_provider.destroy }.to change(described_class, :count).by(-1)
+ end
+
+ it "is destroyed when the related integration is destroyed" do
+ create(:remote_identity, integration:)
+
+ expect { integration.destroy }.to change(described_class, :count).by(-1)
end
end
diff --git a/spec/services/remote_identities/create_service_spec.rb b/spec/services/remote_identities/create_service_spec.rb
index 03b99b0fe0b..21ccd74bed4 100644
--- a/spec/services/remote_identities/create_service_spec.rb
+++ b/spec/services/remote_identities/create_service_spec.rb
@@ -20,7 +20,7 @@ RSpec.describe RemoteIdentities::CreateService, :storage_server_helpers, :webmoc
before { stub_nextcloud_user_query(integration.host) }
describe ".call" do
- it "requires a user, a oauth configuration and a rack token" do
+ it "requires certain parameters" do
method = described_class.method :call
expect(method.parameters).to contain_exactly(%i[keyreq user], %i[keyreq token], %i[keyreq integration])
@@ -46,5 +46,27 @@ RSpec.describe RemoteIdentities::CreateService, :storage_server_helpers, :webmoc
result = service.call.result
expect(result).to be_a RemoteIdentity
end
+
+ it "sets origin_user_id" do
+ expect { service.call.result }.to change {
+ RemoteIdentity.pluck(:origin_user_id)
+ }.from([]).to(["admin"])
+ 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 }
+ end
+
+ 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
+
+ model = service.call.result
+ model.origin_user_id = "123123"
+ model.save
+ service.call.result
+ end
end
end