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
[#60151] added unit tests for new strategies
Eric Schubert86981128b837
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rbindex 19494f7c514..34650890de2 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb@@ -38,6 +38,8 @@ module Storagescase strategy.keywhen :noopAuthenticationStrategies::Noop.new+ when :failure+ AuthenticationStrategies::Failure.newwhen :basic_authAuthenticationStrategies::BasicAuth.newwhen :sso_user_tokendiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/failure.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/failure.rbnew file mode 100644index 00000000000..bc0c5fa3f23--- /dev/null+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/failure.rb@@ -0,0 +1,52 @@+# 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.+#++++module Storages+ module Peripherals+ module StorageInteraction+ module AuthenticationStrategies+ class Failure+ def self.strategy+ Strategy.new(:failure)+ end++ # rubocop:disable Lint/UnusedMethodArgument+ def call(storage:, http_options: {})+ data = ::Storages::StorageErrorData.new(source: self.class)+ log_message = "Authentication was forced to fail. No request executed."+ Failures::Builder.call(code: :error, log_message:, data:)+ end++ # rubocop:enable Lint/UnusedMethodArgument+ end+ end+ end+ end+enddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rbindex 69ea9acd007..ba325f17f73 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rb@@ -33,23 +33,28 @@ module Storagesmodule StorageInteractionmodule AuthenticationStrategiesmodule NextcloudStrategies+ extend TaggedLogging+UserLess = -> do::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::BasicAuth.strategyendUserBound = ->(user:, storage:) do- sso_preferred = storage.audience.present? && user.authentication_provider.present?+ with_tagged_logger do+ sso_preferred = storage.audience.present? && user.authentication_provider.present?- if sso_preferred- ::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::SsoUserToken- .strategy- .with_user(user)- elsif storage.oauth_configuration.present?- ::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken- .strategy- .with_user(user)- else- ::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::Noop.strategy+ if sso_preferred+ ::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::SsoUserToken+ .strategy+ .with_user(user)+ elsif storage.oauth_client.present?+ ::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken+ .strategy+ .with_user(user)+ else+ error "No user-bound authentication strategy applicable for file storage #{storage.id}."+ ::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::Failure.strategy+ endendendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/one_drive_strategies.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/one_drive_strategies.rbindex 7833c61c6c1..cd751169908 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/one_drive_strategies.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/one_drive_strategies.rb@@ -37,13 +37,11 @@ module Storages::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthClientCredentials.strategyend- # rubocop:disable Lint/UnusedBlockArgument- UserBound = ->(user:, storage:) do+ UserBound = ->(user:, storage:) do # rubocop:disable Lint/UnusedBlockArgument::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken.strategy.with_user(user)end- # rubocop:enable Lint/UnusedBlockArgumentendendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/sso_user_token.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/sso_user_token.rbindex 6e817e5ddc7..3e02634232c 100644--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/sso_user_token.rb+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/sso_user_token.rb@@ -51,7 +51,8 @@ module Storagesyield OpenProject.httpx.with(opts)end,->(error) do- Failures::Builder.call(code: :unauthorized, log_message: error.message)+ log_message = "Failed to fetch access token for user #{@user}. Error: #{error.inspect}"+ Failures::Builder.call(code: :unauthorized, log_message:, data: error)end)enddiff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rbindex ae6530096f5..7ccee78c7f2 100644--- a/modules/storages/app/models/storages/storage.rb+++ b/modules/storages/app/models/storages/storage.rb@@ -149,7 +149,7 @@ module Storages# Returns a value of an audience, if configured for this storage.# The presence of an audience signals that this storage prioritizes- # remote authentication over token exchange if possible.+ # remote authentication via Single-Sign-On if possible.def audienceraise Errors::SubclassResponsibilityenddiff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies/user_bound_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies/user_bound_spec.rbnew file mode 100644index 00000000000..b5424ad0997--- /dev/null+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies/user_bound_spec.rb@@ -0,0 +1,86 @@+# 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::NextcloudStrategies::UserBound do+ context "if file storage is configured for sso and oauth and user is provisioned by an IDP" do+ let(:provider) { create(:oidc_provider) }+ let(:user) { create(:user, identity_url: "#{provider.slug}:me") }+ let(:storage) { create(:nextcloud_storage_configured) }++ it "must use an SsoUserToken strategy" do+ strategy = described_class.call(user:, storage:)+ expect(strategy.key).to eq(:sso_user_token)+ end+ end++ context "if file storage is configured for sso and oauth and user is local" do+ let(:user) { create(:user) }+ let(:storage) { create(:nextcloud_storage_configured) }++ it "must use an OAuthUserToken strategy" do+ strategy = described_class.call(user:, storage:)+ expect(strategy.key).to eq(:oauth_user_token)+ end+ end++ context "if file storage is configured only for oauth and user is provided by an IDP" do+ let(:provider) { create(:oidc_provider) }+ let(:user) { create(:user, identity_url: "#{provider.slug}:me") }+ let(:storage) { create(:nextcloud_storage_configured, nextcloud_audience: nil) }++ it "must use an OAuthUserToken strategy" do+ strategy = described_class.call(user:, storage:)+ expect(strategy.key).to eq(:oauth_user_token)+ end+ end++ context "if file storage is configured only for oauth and user is local" do+ let(:user) { create(:user) }+ let(:storage) { create(:nextcloud_storage_configured, nextcloud_audience: nil) }++ it "must use an OAuthUserToken strategy" do+ strategy = described_class.call(user:, storage:)+ expect(strategy.key).to eq(:oauth_user_token)+ end+ end++ context "if file storage is not fully configured" do+ let(:user) { create(:user) }+ let(:storage) { create(:nextcloud_storage, nextcloud_audience: nil) }++ it "must return the failure strategy" do+ strategy = described_class.call(user:, storage:)+ expect(strategy.key).to eq(:failure)+ end+ end+enddiff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/sso_user_token_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/sso_user_token_spec.rbnew file mode 100644index 00000000000..e00c48648ed--- /dev/null+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/sso_user_token_spec.rb@@ -0,0 +1,79 @@+# 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::SsoUserToken do+ include Dry::Monads[:result]++ let(:storage) { create(:nextcloud_storage) }++ subject(:strategy) { described_class.new(create(:user)) }++ before do+ service = instance_double(OpenIDConnect::UserTokens::FetchService)+ allow(OpenIDConnect::UserTokens::FetchService).to receive(:new).and_return(service)+ allow(service).to receive(:access_token_for).with(audience: storage.audience).and_return(access_token_result)+ end++ context "if access token can be fetched successfully" do+ let(:token) { "my_access_token" }+ let(:access_token_result) { Success(token) }++ it "must yield with access token" do+ was_yielded = false++ strategy.call(storage:) do |http|+ was_yielded = true+ expect(http.instance_variable_get(:@options).headers["authorization"]).to eq("Bearer #{token}")+ end++ expect(was_yielded).to be_truthy+ end+ end++ context "if fetching access token fails" do+ let(:error) { "U shall not pass!" }+ let(:access_token_result) { Failure(error) }++ it "must not yield and return failure" do+ was_yielded = false+ result = strategy.call(storage:) { was_yielded = true }++ expect(was_yielded).to be_falsy+ expect(result).to be_failure+ expect(result.result).to eq(:unauthorized)+ expect(result.errors).to be_a(Storages::StorageError)+ expect(result.errors.data).to eq(error)+ expect(result.errors.data).to eq(error)+ end+ end+enddiff --git a/spec/factories/user_factory.rb b/spec/factories/user_factory.rbindex 9bf5bc3dc40..53a62955634 100644--- a/spec/factories/user_factory.rb+++ b/spec/factories/user_factory.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbH@@ -34,6 +36,7 @@ FactoryBot.define dosequence(:mail) { |n| "bobmail#{n}[email protected]" }password { "adminADMIN!" }password_confirmation { "adminADMIN!" }+ identity_url { nil }transient dopreferences { {} }