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
[#55382] extracted validator to own class
Eric Schubert26233a005b45
diff --git a/modules/storages/app/common/storages/peripherals/one_drive_connection_validator.rb b/modules/storages/app/common/storages/peripherals/one_drive_connection_validator.rbnew file mode 100644index 00000000000..01a7c9e2788--- /dev/null+++ b/modules/storages/app/common/storages/peripherals/one_drive_connection_validator.rb@@ -0,0 +1,154 @@+# frozen_string_literal:true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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+ class OneDriveConnectionValidator+ include Dry::Monads[:maybe]++ using ServiceResultRefinements++ def initialize(storage:)+ @storage = storage+ end++ def validate+ maybe_is_not_configured+ .or { tenant_id_wrong }+ .or { client_id_wrong }+ .or { client_secret_wrong }+ .or { drive_id_wrong }+ .or { request_failed_with_unknown_error }+ .or { drive_with_unexpected_content }+ .value_or(ConnectionValidation.new(icon: "check-circle",+ scheme: :success,+ description: I18n.t("storages.connection_validation.success")))+ end++ private++ def query+ @query ||= Peripherals::Registry+ .resolve("#{@storage.short_provider_type}.queries.files")+ .call(storage: @storage, auth_strategy:, folder: root_folder)+ end++ def maybe_is_not_configured+ return None() if @storage.configured?++ Some(ConnectionValidation.new(icon: :alert,+ scheme: :warning,+ description: I18n.t("storages.connection_validation.not_configured")))+ end++ def drive_id_wrong+ return None() if query.result != :not_found++ Some(ConnectionValidation.new(icon: :skip,+ scheme: :danger,+ description: I18n.t("storages.connection_validation.drive_id_wrong")))+ end++ def tenant_id_wrong+ return None() if query.result != :unauthorized++ payload = JSON.parse(query.error_payload)+ return None() if payload["error"] != "invalid_request"++ tenant_id_string = "Tenant '#{@storage.tenant_id}' not found."+ return None() unless payload["error_description"].include?(tenant_id_string)++ Some(ConnectionValidation.new(icon: :skip,+ scheme: :danger,+ description: I18n.t("storages.connection_validation.tenant_id_wrong")))+ end++ def client_id_wrong+ return None() if query.result != :unauthorized++ payload = JSON.parse(query.error_payload)+ return None() if payload["error"] != "unauthorized_client"++ Some(ConnectionValidation.new(icon: :skip,+ scheme: :danger,+ description: I18n.t("storages.connection_validation.client_id_wrong")))+ end++ def client_secret_wrong+ return None() if query.result != :unauthorized++ payload = JSON.parse(query.error_payload)+ return None() if payload["error"] != "invalid_client"++ Some(ConnectionValidation.new(icon: :skip,+ scheme: :danger,+ description: I18n.t("storages.connection_validation.client_secret_wrong")))+ end++ # rubocop:disable Metrics/AbcSize+ def drive_with_unexpected_content+ return None() if query.failure?+ return None() unless @storage.automatic_management_enabled?++ expected_folder_ids = @storage.project_storages+ .where(project_folder_mode: "automatic")+ .map(&:project_folder_id)++ unexpected_files = query.result.files.reject { |file| expected_folder_ids.include?(file.id) }+ return None() if unexpected_files.empty?++ Some(ConnectionValidation.new(icon: :alert,+ scheme: :warning,+ description: I18n.t("storages.connection_validation.unexpected_content")))+ end++ # rubocop:enable Metrics/AbcSize++ def request_failed_with_unknown_error+ return None() if query.success?++ Rails.logger.error("Connection validation failed with unknown error:\n\t" \+ "status: #{query.result}\n\tresponse: #{query.error_payload}")++ Some(ConnectionValidation.new(icon: :skip,+ scheme: :danger,+ description: I18n.t("storages.connection_validation.unknown_error")))+ end++ def root_folder+ Peripherals::ParentFolder.new("/")+ end++ def auth_strategy+ Peripherals::Registry.resolve("#{@storage.short_provider_type}.authentication.userless").call+ end+ end+ end+enddiff --git a/modules/storages/app/controllers/storages/admin/connection_validation_controller.rb b/modules/storages/app/controllers/storages/admin/connection_validation_controller.rbindex 28de15ce1e2..123a95a9407 100644--- a/modules/storages/app/controllers/storages/admin/connection_validation_controller.rb+++ b/modules/storages/app/controllers/storages/admin/connection_validation_controller.rb@@ -32,9 +32,6 @@ module Storagesmodule Adminclass ConnectionValidationController < ApplicationControllerinclude OpTurbo::ComponentStream- include Dry::Monads[:maybe]-- using Peripherals::ServiceResultRefinementslayout "admin"@@ -44,128 +41,22 @@ module Storagesbefore_action :find_model_object, only: %i[validate_connection]- # rubocop:disable Metrics/AbcSizedef validate_connection- @result = maybe_is_not_configured- .or { drive_id_wrong }- .or { tenant_id_wrong }- .or { client_id_wrong }- .or { client_secret_wrong }- .or { request_failed_with_unknown_error }- .or { drive_with_unexpected_content }- .value_or(ConnectionValidation.new(icon: "check-circle",- scheme: :success,- description: I18n.t("storages.connection_validation.success")))+ @result = Peripherals::OneDriveConnectionValidator+ .new(storage: @storage)+ .validaterespond_to do |format|format.turbo_streamendend- # rubocop:enable Metrics/AbcSize-private- def query- @query ||= Peripherals::Registry- .resolve("#{@storage.short_provider_type}.queries.files")- .call(storage: @storage, auth_strategy:, folder: root_folder)- end-- def maybe_is_not_configured- return None() if @storage.configured?-- Some(ConnectionValidation.new(icon: :alert,- scheme: :warning,- description: I18n.t("storages.connection_validation.not_configured")))- end-- def drive_id_wrong- return None() if query.result != :not_found-- Some(ConnectionValidation.new(icon: :skip,- scheme: :danger,- description: I18n.t("storages.connection_validation.drive_id_wrong")))- end-- def tenant_id_wrong- return None() if query.result != :unauthorized-- payload = JSON.parse(query.error_payload)- return None() if payload["error"] != "invalid_request"-- tenant_id_string = "Tenant '#{@storage.tenant_id}' not found."- return None() unless payload["error_description"].include?(tenant_id_string)-- Some(ConnectionValidation.new(icon: :skip,- scheme: :danger,- description: I18n.t("storages.connection_validation.tenant_id_wrong")))- end-- def client_id_wrong- return None() if query.result != :unauthorized-- payload = JSON.parse(query.error_payload)- return None() if payload["error"] != "unauthorized_client"-- Some(ConnectionValidation.new(icon: :skip,- scheme: :danger,- description: I18n.t("storages.connection_validation.client_id_wrong")))- end-- def client_secret_wrong- return None() if query.result != :unauthorized-- payload = JSON.parse(query.error_payload)- return None() if payload["error"] != "invalid_client"-- Some(ConnectionValidation.new(icon: :skip,- scheme: :danger,- description: I18n.t("storages.connection_validation.client_secret_wrong")))- end-- # rubocop:disable Metrics/AbcSize- def drive_with_unexpected_content- return None() if query.failure?- return None() unless @storage.automatic_management_enabled?-- expected_folder_ids = @storage.project_storages- .where(project_folder_mode: "automatic")- .map(&:project_folder_id)-- unexpected_files = query.result.files.reject { |file| expected_folder_ids.include?(file.id) }- return None() if unexpected_files.empty?-- Some(ConnectionValidation.new(icon: :alert,- scheme: :warning,- description: I18n.t("storages.connection_validation.unexpected_content")))- end-- # rubocop:enable Metrics/AbcSize-- def request_failed_with_unknown_error- return None() if query.success?-- Rails.logger.error("Connection validation failed with unknown error:\n\t" \- "status: #{query.result}\n\tresponse: #{query.error_payload}")-- Some(ConnectionValidation.new(icon: :skip,- scheme: :danger,- description: I18n.t("storages.connection_validation.unknown_error")))- end-def find_model_object(object_id = :storage_id)super@storage = @objectend-- def root_folder- Peripherals::ParentFolder.new("/")- end-- def auth_strategy- Peripherals::Registry.resolve("#{@storage.short_provider_type}.authentication.userless").call- endendendenddiff --git a/modules/storages/spec/requests/storages/admin/connection_validation_spec.rb b/modules/storages/spec/requests/storages/admin/connection_validation_spec.rbnew file mode 100644index 00000000000..46a62bbd487--- /dev/null+++ b/modules/storages/spec/requests/storages/admin/connection_validation_spec.rb@@ -0,0 +1,57 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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 "connection validation", :skip_csrf do+ describe "POST /admin/settings/storages/:id/connection_validation/validate_connection" do+ let(:storage) { create(:one_drive_storage) }+ let(:user) { create(:admin) }+ let(:validator) do+ double = instance_double(Storages::Peripherals::OneDriveConnectionValidator)+ allow(double).to receive_messages(validate: validation_result)+ double+ end+ let(:validation_result) do+ Storages::ConnectionValidation.new(icon: "check-circle", scheme: :default, description: "Successful!")+ end++ current_user { user }++ before do+ allow(Storages::Peripherals::OneDriveConnectionValidator).to receive(:new).and_return(validator)+ end++ it "returns a connection validation result" do+ response = post validate_connection_admin_settings_storage_connection_validation_path(storage.id, format: :turbo_stream)+ expect(response.status).to eq(200)++ doc = Nokogiri::HTML(response.body)+ end+ end+end