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
Re-Implements the NextcloudConnectionValidator using the multple status retun (#18471)
Marcello Rochac76232b25fe4
diff --git a/app/models/types/patterns/token_property_mapper.rb b/app/models/types/patterns/token_property_mapper.rbindex 1488f1aba2e..a739a0e3fec 100644--- a/app/models/types/patterns/token_property_mapper.rb+++ b/app/models/types/patterns/token_property_mapper.rb@@ -47,7 +47,7 @@ module Typescreation_date: { fn: ->(wp) { wp.created_at }, label: -> { WorkPackage.human_attribute_name(:created_at) } },estimated_time: { fn: ->(wp) { wp.estimated_hours }, label: -> { WorkPackage.human_attribute_name(:estimated_hours) } },finish_date: { fn: ->(wp) { wp.due_date }, label: -> { WorkPackage.human_attribute_name(:due_date) } },- parent_id: { fn: ->(wp) { wp.parent&.id }, label: -> { WorkPackage.human_attribute_name(:parent_id) } },+ parent_id: { fn: ->(wp) { wp.parent&.id }, label: -> { WorkPackage.human_attribute_name(:id) } },parent_assignee: { fn: ->(wp) { wp.parent&.assigned_to&.name }, label: -> {WorkPackage.human_attribute_name(:assigned_to)} },diff --git a/modules/storages/app/common/storages/peripherals/connection_validators/base_validator_group.rb b/modules/storages/app/common/storages/peripherals/connection_validators/base_validator_group.rbnew file mode 100644index 00000000000..36e5501170a--- /dev/null+++ b/modules/storages/app/common/storages/peripherals/connection_validators/base_validator_group.rb@@ -0,0 +1,82 @@+# 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 ConnectionValidators+ class BaseValidatorGroup+ include TaggedLogging++ def initialize(storage)+ @storage = storage+ @results = ValidationGroupResult.new+ end++ def call+ catch :interrupted do+ validate+ end++ @results+ end++ private++ def validate = raise Errors::SubclassResponsibility++ def register_checks(*keys)+ keys.each { @results.register_check(it) }+ end++ def update_result(...)+ @results.update_result(...)+ end++ def pass_check(key)+ update_result(key, CheckResult.success(key))+ end++ def fail_check(key, message)+ update_result(key, CheckResult.failure(key, message))+ throw :interrupted+ end++ def warn_check(key, message, halt_validation: false)+ update_result(key, CheckResult.warning(key, message))+ throw :interrupted if halt_validation+ end++ def message(key, context = {})+ I18n.t("storages.health.connection_validation.#{key}", **context)+ end+ end+ end+ end+enddiff --git a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/ampf_connection_validator.rb b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/ampf_configuration_validator.rbsimilarity index 96%rename from modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/ampf_connection_validator.rbrename to modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/ampf_configuration_validator.rbindex 3bdebff3dac..e97338cb089 100644--- a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/ampf_connection_validator.rb+++ b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/ampf_configuration_validator.rb@@ -32,19 +32,19 @@ module Storagesmodule Peripheralsmodule ConnectionValidatorsmodule Nextcloud- class AmpfConnectionValidator < BaseValidator+ class AmpfConfigurationValidator < BaseValidatorGroupusing ServiceResultRefinementsprivatedef validateregister_checks(- :userless_access, :group_folder_presence, :files_request, :group_folder_contents+ :files_request, :userless_access, :group_folder_presence, :group_folder_contents)+ files_request_failed_with_unknown_erroruserless_access_deniedgroup_folder_not_found- files_request_failed_with_unknown_errorwith_unexpected_contentenddiff --git a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/authentication_validator.rb b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/authentication_validator.rbindex 6289b27dac3..511c53dceb0 100644--- a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/authentication_validator.rb+++ b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/authentication_validator.rb@@ -32,7 +32,7 @@ module Storagesmodule Peripheralsmodule ConnectionValidatorsmodule Nextcloud- class AuthenticationValidator < BaseValidator+ class AuthenticationValidator < BaseValidatorGroupdef initialize(storage)super@user = User.currentdiff --git a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator.rb b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/storage_configuration_validator.rbsimilarity index 85%rename from modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator.rbrename to modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/storage_configuration_validator.rbindex 1d64b0b2982..cebf4393508 100644--- a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator.rb+++ b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/storage_configuration_validator.rb@@ -32,21 +32,31 @@ module Storagesmodule Peripheralsmodule ConnectionValidatorsmodule Nextcloud- class BaseConfigurationValidator < BaseValidator+ class StorageConfigurationValidator < BaseValidatorGroupprivatedef validate- register_checks(:capabilities_request, :host_url_accessible, :dependencies_check, :dependencies_versions)+ register_checks(:storage_configured, :capabilities_request,+ :host_url_accessible, :dependencies_check, :dependencies_versions)+ storage_configuration_statuscapabilities_request_statushost_url_not_foundmissing_dependenciesversion_mismatchend+ def storage_configuration_status+ if @storage.configured?+ pass_check(:storage_configured)+ else+ fail_check(:storage_configured, message(:not_configured))+ end+ end+def capabilities_request_statusif capabilities.failure? && capabilities.result != :not_found- fail_check(:capabilities_request, message(:error))+ fail_check(:capabilities_request, message(:unknown_error))elsepass_check(:capabilities_request)enddiff --git a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_validator.rb b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud_validator.rbsimilarity index 51%rename from modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_validator.rbrename to modules/storages/app/common/storages/peripherals/connection_validators/nextcloud_validator.rbindex 34afb6f8842..7c75def5754 100644--- a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_validator.rb+++ b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud_validator.rb@@ -31,56 +31,41 @@module Storagesmodule Peripheralsmodule ConnectionValidators- module Nextcloud- class BaseValidator- include TaggedLogging-- def initialize(storage)- @storage = storage+ class NextcloudValidator+ # Class Level interface will be moved to a superclass/mixin once we do the OneDrive port+ class << self+ def validation_groups+ @validation_groups ||= {}end- def call- catch :interrupted do- validate- end-- @results+ def register_group(group_name, klass, when: ->(*) { true })+ validation_groups[group_name] = { klass:, when: }end+ end- private-- def validate = raise Errors::SubclassResponsibility+ register_group :base_configuration, Nextcloud::StorageConfigurationValidator+ register_group :authentication, Nextcloud::AuthenticationValidator,+ when: ->(_, result) { result.group(:base_configuration).non_failure? }+ register_group :ampf_configuration, Nextcloud::AmpfConfigurationValidator,+ when: ->(storage, result) {+ result.group(:base_configuration).non_failure? && storage.automatic_management_enabled?+ }- def register_checks(*keys)- @results = keys.to_h { [it, CheckResult.skipped(it)] }- end+ def initialize(storage:)+ @storage = storage+ end- def update_result(key, value)- if @results&.has_key?(key)- @results[key] = value- else- raise ArgumentError, "Check #{key} not registered."+ def validate+ validation_groups.each_with_object(ValidatorResult.new) do |(key, group_metadata), result|+ if group_metadata[:when].call(@storage, result)+ result.add_group_result(key, group_metadata[:klass].new(@storage).call)endend+ end- def pass_check(key)- update_result(key, CheckResult.success(key))- end-- def fail_check(key, message)- update_result(key, CheckResult.failure(key, message))- throw :interrupted- end+ private- def warn_check(key, message, halt_validation: false)- update_result(key, CheckResult.warning(key, message))- throw :interrupted if halt_validation- end-- def message(key, context = {})- I18n.t("storages.health.connection_validation.#{key}", **context)- end- end+ def validation_groups = self.class.validation_groupsendendenddiff --git a/modules/storages/app/common/storages/peripherals/connection_validators/validation_group_result.rb b/modules/storages/app/common/storages/peripherals/connection_validators/validation_group_result.rbnew file mode 100644index 00000000000..7b87cd1b589--- /dev/null+++ b/modules/storages/app/common/storages/peripherals/connection_validators/validation_group_result.rb@@ -0,0 +1,68 @@+# 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 ConnectionValidators+ class ValidationGroupResult+ delegate :[], to: :@results++ def initialize+ @results = {}+ end++ def success? = @results.values.all?(&:success?)+ def non_failure? = @results.values.none?(&:failure?)+ def failure? = @results.values.any?(&:failure?)+ def warning? = @results.values.any?(&:warning?)++ def tally+ @results.values.map(&:state).tally+ end++ def register_checks(keys)+ Array(keys).each { register_check(it) }+ end++ def register_check(key)+ warn("Overriding already defined check") if @results.key?(key)++ @results[key] = CheckResult.skipped(key)+ end++ def update_result(key, value)+ raise(ArgumentError, "Check #{key} not registered.") unless @results.key?(key)++ @results[key] = value+ end+ end+ end+ end+enddiff --git a/modules/storages/app/common/storages/peripherals/connection_validators/validator_result.rb b/modules/storages/app/common/storages/peripherals/connection_validators/validator_result.rbnew file mode 100644index 00000000000..5dd26295ad6--- /dev/null+++ b/modules/storages/app/common/storages/peripherals/connection_validators/validator_result.rb@@ -0,0 +1,58 @@+# 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 ConnectionValidators+ class ValidatorResult+ private attr_reader :group_results++ delegate :each_pair, :empty?, to: :group_results++ def initialize+ @group_results = {}+ end++ def healthy? = group_results.values.all?(&:success?)+ def unhealthy? = group_results.values.any?(&:failure?)+ def warning? = group_results.values.any?(&:warning?)++ def group(key) = group_results.fetch(key)+ alias_method :fetch, :group++ def add_group_result(key, result)+ Kernel.warn "Overwriting #{key} results" if group_results.key?(key)++ group_results[key] = result+ end+ end+ end+ end+enddiff --git a/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/ampf_connection_validator_spec.rb b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/ampf_configuration_validator_spec.rbsimilarity index 94%rename from modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/ampf_connection_validator_spec.rbrename to modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/ampf_configuration_validator_spec.rbindex c5ffae86fa5..7ddf4de2900 100644--- a/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/ampf_connection_validator_spec.rb+++ b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/ampf_configuration_validator_spec.rb@@ -35,7 +35,7 @@ module Storagesmodule Peripheralsmodule ConnectionValidatorsmodule Nextcloud- RSpec.describe AmpfConnectionValidator, :webmock do+ RSpec.describe AmpfConfigurationValidator, :webmock dolet(:storage) { create(:nextcloud_storage_configured, :as_automatically_managed) }let(:project_folder_id) { "1337" }let!(:project_storage) do@@ -57,9 +57,7 @@ module Storagesendit "pass all checks" do- results = validator.call-- expect(results.values).to all(be_success)+ expect(validator.call).to be_successendcontext "if userless authentication fails" do@@ -68,8 +66,8 @@ module Storagesit "fails and skips the next checks" doresults = validator.call- states = results.values.map { it.state }.tally- expect(states).to eq({ failure: 1, skipped: 3 })+ states = results.tally+ expect(states).to eq({ success: 1, failure: 1, skipped: 2 })expect(results[:userless_access]).to be_failureexpect(results[:userless_access].message).to eq(i18n_message(:userless_access_denied))enddiff --git a/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/authentication_validator_spec.rb b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/authentication_validator_spec.rbindex 97370e73b37..547620bd4dd 100644--- a/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/authentication_validator_spec.rb+++ b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/authentication_validator_spec.rb@@ -48,9 +48,7 @@ module Storagesbefore { User.current = user }it "passes when the user has a token and the request works", vcr: "nextcloud/user_query_success" do- result = validator.call-- expect(result.values).to all(be_success)+ expect(validator.call).to be_successendit "returns a warning when there's no token for the current user" do@@ -87,9 +85,7 @@ module Storagesit "succeeds give the user is provisioned and tokens can be acquired" docreate(:oidc_user_token, user:, extra_audiences: storage.audience)- result = validator.call-- expect(result.values).to all(be_success)+ expect(validator.call).to be_successenddescribe "error and warning handling" do@@ -100,7 +96,7 @@ module Storagesexpect(result[:non_provisioned_user]).to be_warningexpect(result[:non_provisioned_user].message).to eq(I18n.t(i18n_key(:oidc_non_provisioned_user)))- state_count = result.values.map { it.state }.tally+ state_count = result.tallyexpect(state_count).to eq({ skipped: 3, warning: 1 })end@@ -111,7 +107,7 @@ module Storagesexpect(result[:provisioned_user_provider]).to be_warningexpect(result[:provisioned_user_provider].message).to eq(I18n.t(i18n_key(:oidc_non_oidc_user)))- state_count = result.values.map { it.state }.tally+ state_count = result.tallyexpect(state_count).to eq({ success: 1, skipped: 2, warning: 1 })endend@@ -138,9 +134,7 @@ module Storagesrefresh_token: expired_storage_token.refresh_token }).and_return_json(status: 200, body: { access_token: "NEW_TOKEN" })- result = validator.call-- expect(result.values).to all(be_success)+ expect(validator.call).to be_successexpect(refresh_request).to have_been_requested.onceend@@ -176,8 +170,7 @@ module Storagesgrant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE }).and_return_json(status: 200, body: { access_token: "NEW_TOKEN" })- result = validator.call- expect(result.values).to all(be_success)+ expect(validator.call).to be_successexpect(exchange_request).to have_been_requested.onceenddiff --git a/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator_spec.rb b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/storage_configuration_validator_spec.rbsimilarity index 81%rename from modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator_spec.rbrename to modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/storage_configuration_validator_spec.rbindex a417e9a74b3..be25efca2ae 100644--- a/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator_spec.rb+++ b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/storage_configuration_validator_spec.rb@@ -35,19 +35,29 @@ module Storagesmodule Peripheralsmodule ConnectionValidatorsmodule Nextcloud- RSpec.describe BaseConfigurationValidator, :webmock do+ RSpec.describe StorageConfigurationValidator, :webmock dolet(:storage) { create(:nextcloud_storage_with_local_connection, :as_not_automatically_managed) }subject(:validator) { described_class.new(storage) }- it "returns a hash with check 'key' as key and a CheckResult as value", vcr: "nextcloud/capabilities_success" do+ it "returns a GroupValidationResult", vcr: "nextcloud/capabilities_success" doresults = validator.call- expect(results).to be_a(Hash)- expect(results.values).to all(be_success)+ expect(results).to be_a(ValidationGroupResult)+ expect(results).to be_successend- describe "when errors occurs" do+ describe "possible error scenarios" do+ context "when the storage is not configured" do+ let(:storage) { create(:nextcloud_storage) }++ it "the check fails" do+ results = validator.call+ expect(results[:storage_configured]).to be_a_failure+ expect(results[:storage_configured].message).to eq(I18n.t(i18n_key(:not_configured)))+ end+ end+it "base url could not be reached" dostub_request(:get, UrlBuilder.url(storage.uri, "/ocs/v2.php/cloud/capabilities")).to_return(status: 404, body: "Not Found")diff --git a/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud_connection_validator_spec.rb b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud_connection_validator_spec.rbnew file mode 100644index 00000000000..ebae600a5cb--- /dev/null+++ b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud_connection_validator_spec.rb@@ -0,0 +1,62 @@+# 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++module Storages+ module Peripherals+ module ConnectionValidators+ RSpec.describe NextcloudValidator, :webmock do+ it "does not run the AMPF tests if the storage is not automatically managed",+ vcr: "nextcloud/capabilities_success" do+ results = described_class.new(storage: create(:nextcloud_storage_with_local_connection)).validate++ expect { results.group(:ampf_configuration) }.to raise_error(KeyError)+ end++ it "aggregates all the results from the tests", vcr: "nextcloud/capabilities_success" do+ results = described_class.new(storage: create(:nextcloud_storage_with_local_connection)).validate++ expect(results).to be_warning+ expect(results.group(:base_configuration)).to be_success+ expect(results.group(:authentication)).to be_warning+ end++ it "does not run any further tests if base configuration failed", vcr: "nextcloud/capabilities_invalid_data" do+ results = described_class.new(storage: create(:nextcloud_storage_with_local_connection)).validate++ expect { results.group(:authentication) }.to raise_error(KeyError)+ expect { results.group(:ampf_configuration) }.to raise_error(KeyError)+ end+ end+ end+ end+end