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

[#55382] added unit tests for controller and validator

Eric Schuberta45a4c132cd2
diff --git a/modules/storages/app/components/storages/admin/sidebar/validation_result_component.html.erb b/modules/storages/app/components/storages/admin/sidebar/validation_result_component.html.erb
index 16c3869be12..383618b5dff 100644
--- a/modules/storages/app/components/storages/admin/sidebar/validation_result_component.html.erb
+++ b/modules/storages/app/components/storages/admin/sidebar/validation_result_component.html.erb
@@ -30,14 +30,16 @@ See COPYRIGHT and LICENSE files for more details.
<%=
flex_layout do |container|
container.with_row do
- render(Primer::Beta::Text.new(font_weight: :bold)) { I18n.t("storages.health.connection_validation.subtitle") }
+ render(Primer::Beta::Text.new(font_weight: :bold, test_selector: "validation-result--subtitle")) do
+ I18n.t("storages.health.connection_validation.subtitle")
+ end
end
if @result.type != :none
container.with_row(mt: 2) do
status = status_indicator
- concat(render(Primer::Beta::Text.new(pr: 2)) do
+ concat(render(Primer::Beta::Text.new(pr: 2, test_selector: "validation-result--timestamp")) do
I18n.t('storages.health.checked', datetime: helpers.format_time(@result.timestamp))
end)
concat(render(Primer::Beta::Label.new(scheme: status[:scheme])) { status[:label] })
@@ -46,7 +48,9 @@ See COPYRIGHT and LICENSE files for more details.
if @result.description.present?
container.with_row(mt: 2) do
- render(Primer::Beta::Text.new(color: :muted)) { @result.description }
+ render(Primer::Beta::Text.new(color: :muted, test_selector: "validation-result--description")) do
+ @result.description
+ end
end
end
end
diff --git a/modules/storages/spec/common/storages/peripherals/one_drive_connection_validator_spec.rb b/modules/storages/spec/common/storages/peripherals/one_drive_connection_validator_spec.rb
new file mode 100644
index 00000000000..37744b344ce
--- /dev/null
+++ b/modules/storages/spec/common/storages/peripherals/one_drive_connection_validator_spec.rb
@@ -0,0 +1,164 @@
+#-- 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"
+require_module_spec_helper
+
+RSpec.describe Storages::Peripherals::OneDriveConnectionValidator do
+ let(:storage) { create(:one_drive_storage, oauth_client: create(:oauth_client)) }
+
+ before do
+ Storages::Peripherals::Registry.stub("#{storage.short_provider_type}.queries.files", ->(_) { response })
+ end
+
+ subject { described_class.new(storage:).validate }
+
+ context "if storage is not yet configured" do
+ let(:storage) { create(:one_drive_storage) }
+
+ it "returns a validation failure" do
+ expect(subject.type).to eq(:none)
+ expect(subject.description).to eq("The connection could not be validated. Please finish configuration first.")
+ end
+ end
+
+ context "if the storage's tenant id could not be found" do
+ let(:error_payload) do
+ {
+ error: "invalid_request",
+ error_description: "There is an error. Tenant '#{storage.tenant_id}' not found. This is VERY bad."
+ }.to_json
+ end
+ let(:response) { build_failure(code: :unauthorized, payload: error_payload) }
+
+ it "returns a validation failure" do
+ expect(subject.type).to eq(:error)
+ expect(subject.description)
+ .to eq("The configured directory (tenant) id is invalid. Please check the configuration.")
+ end
+ end
+
+ context "if the storage's client id could not be found" do
+ let(:error_payload) { { error: "unauthorized_client" }.to_json }
+ let(:response) { build_failure(code: :unauthorized, payload: error_payload) }
+
+ it "returns a validation failure" do
+ expect(subject.type).to eq(:error)
+ expect(subject.description).to eq("The configured OAuth 2 client id is invalid. Please check the configuration.")
+ end
+ end
+
+ context "if the storage's client secret is wrong" do
+ let(:error_payload) { { error: "invalid_client" }.to_json }
+ let(:response) { build_failure(code: :unauthorized, payload: error_payload) }
+
+ it "returns a validation failure" do
+ expect(subject.type).to eq(:error)
+ expect(subject.description)
+ .to eq("The configured OAuth 2 client secret is invalid. Please check the configuration.")
+ end
+ end
+
+ context "if the storage's drive id could not be found" do
+ let(:response) { build_failure(code: :not_found, payload: nil) }
+
+ it "returns a validation failure" do
+ expect(subject.type).to eq(:error)
+ expect(subject.description).to eq("The configured drive id could not be found. Please check the configuration.")
+ end
+ end
+
+ context "if the request fails with an unknown error" do
+ let(:response) { build_failure(code: :error, payload: nil) }
+
+ before do
+ allow(Rails.logger).to receive(:error)
+ end
+
+ it "returns a validation failure" do
+ expect(subject.type).to eq(:error)
+ expect(subject.description)
+ .to eq("The connection could not be validated. An unknown error occurred. " \
+ "Please check the server logs for further information.")
+ end
+
+ it "logs the error message" do
+ described_class.new(storage:).validate
+ expect(Rails.logger).to have_received(:error)
+ end
+ end
+
+ context "if the request returns unexpected files" do
+ let(:storage) { create(:one_drive_storage, :as_automatically_managed, oauth_client: create(:oauth_client)) }
+ let(:project_folder_id) { "1337" }
+ let(:project_storage) do
+ create(:project_storage,
+ :as_automatically_managed,
+ project_folder_id:,
+ storage:,
+ project: create(:project))
+ end
+ let(:files_result) do
+ Storages::StorageFiles.new(
+ [
+ Storages::StorageFile.new(id: project_folder_id, name: "I am your father"),
+ Storages::StorageFile.new(id: "noooooooooo", name: "testimony_of_luke_skywalker.md")
+ ],
+ Storages::StorageFile.new(id: "root", name: "root"),
+ []
+ )
+ end
+ let(:response) { ServiceResult.success(result: files_result) }
+
+ before do
+ project_storage
+ end
+
+ it "returns a validation failure" do
+ expect(subject.type).to eq(:warning)
+ expect(subject.description).to eq("Unexpected content found in the drive.")
+ end
+ end
+
+ context "if everything was fine" do
+ let(:response) { ServiceResult.success }
+
+ it "returns a validation success" do
+ expect(subject.type).to eq(:healthy)
+ expect(subject.description).to be_nil
+ end
+ end
+
+ private
+
+ def build_failure(code:, payload:)
+ data = Storages::StorageErrorData.new(source: "query", payload:)
+ error = Storages::StorageError.new(code:, data:)
+ ServiceResult.failure(result: code, errors: error)
+ end
+end
diff --git a/modules/storages/spec/requests/storages/admin/connection_validation_spec.rb b/modules/storages/spec/requests/storages/admin/connection_validation_spec.rb
index 46a62bbd487..10fc607eb80 100644
--- a/modules/storages/spec/requests/storages/admin/connection_validation_spec.rb
+++ b/modules/storages/spec/requests/storages/admin/connection_validation_spec.rb
@@ -37,9 +37,6 @@ RSpec.describe "connection validation", :skip_csrf do
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 }
@@ -47,11 +44,93 @@ RSpec.describe "connection validation", :skip_csrf 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)
+ subject do
+ post validate_connection_admin_settings_storage_connection_validation_path(storage.id, format: :turbo_stream)
+ end
+
+ shared_examples_for "a validation result template" do |show_timestamp:, label:, description:|
+ it "returns a turbo update template" do
+ expect(subject.status).to eq(200)
+
+ doc = Nokogiri::HTML(subject.body)
+ expect(doc.xpath(xpath_for_subtitle).text).to eq("Connection validation")
+
+ if show_timestamp
+ expect(doc.xpath(xpath_for_timestamp)).not_to be_empty
+ else
+ expect(doc.xpath(xpath_for_timestamp)).to be_empty
+ end
+
+ if label.present?
+ expect(doc.xpath(xpath_for_label).text).to eq(label)
+ else
+ expect(doc.xpath(xpath_for_label).text).to be_empty
+ end
+
+ if description.present?
+ expect(doc.xpath(xpath_for_description).text).to eq(description)
+ else
+ expect(doc.xpath(xpath_for_description).text).to be_empty
+ end
+ end
+ end
+
+ context "if the a validation result of type :none (no validation executed) is returned" do
+ let(:validation_result) do
+ Storages::ConnectionValidation.new(type: :none, timestamp: Time.current, description: "not configured")
+ end
+
+ it_behaves_like "a validation result template", show_timestamp: false, label: nil, description: "not configured"
+ end
+
+ context "if validator returns an error" do
+ let(:validation_result) do
+ Storages::ConnectionValidation.new(type: :error, timestamp: Time.current, description: "An error occurred")
+ end
+
+ it_behaves_like "a validation result template",
+ show_timestamp: true, label: "Error", description: "An error occurred"
+ end
+
+ context "if validator returns a warning" do
+ let(:validation_result) do
+ Storages::ConnectionValidation.new(type: :warning,
+ timestamp: Time.current,
+ description: "There is something weird...")
+ end
+
+ it_behaves_like "a validation result template",
+ show_timestamp: true, label: "Warning", description: "There is something weird..."
+ end
+
+ context "if validator returns a success" do
+ let(:validation_result) do
+ Storages::ConnectionValidation.new(type: :healthy, timestamp: Time.current, description: nil)
+ end
- doc = Nokogiri::HTML(response.body)
+ it_behaves_like "a validation result template",
+ show_timestamp: true, label: "Healthy", description: nil
end
end
+
+ private
+
+ # rubocop:disable Layout/LineLength
+ def xpath_for_subtitle
+ "//turbo-stream[@target='connection_validation_result']/template/div/div/span[@data-test-selector='validation-result--subtitle']"
+ end
+
+ def xpath_for_timestamp
+ "//turbo-stream[@target='connection_validation_result']/template/div/div/span[@data-test-selector='validation-result--timestamp']"
+ end
+
+ def xpath_for_label
+ "//turbo-stream[@target='connection_validation_result']/template/div/div/span[contains(@class, 'Label')]"
+ end
+
+ def xpath_for_description
+ "//turbo-stream[@target='connection_validation_result']/template/div/div/span[@data-test-selector='validation-result--description']"
+ end
+
+ # rubocop:enable Layout/LineLength
end