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

[#62103] added controller and component tests

Eric Schubert58ec05b63e5a
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 71598b8e5de..8a0d5ed933d 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -2053,7 +2053,6 @@ en:
work_package_subject_generation: Work Package Subject Generation
upsale:
title: "Enterprise add-on"
- automatic_subject_generation:
plan_title: "Enterprise %{plan} add-on"
plan_name: "%{plan} enterprise plan"
plan_text_html: "Available only through the %{plan_name}."
diff --git a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud_validator.rb b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud_validator.rb
index 7c75def5754..ebb1a41ed9d 100644
--- a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud_validator.rb
+++ b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud_validator.rb
@@ -63,6 +63,10 @@ module Storages
end
end
+ def report_cache_key
+ "nextcloud_storage_#{@storage.id}_health_status_report"
+ end
+
private
def validation_groups = self.class.validation_groups
diff --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.rb
index c5953f62ab4..60df21e45f2 100644
--- 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.rb
@@ -32,8 +32,7 @@ module Storages
module Peripherals
module ConnectionValidators
class ValidationGroupResult
- # TODO: remove []=
- delegate :[], :[]=, :each_pair, to: :@results
+ delegate :[], :each_pair, to: :@results
def initialize
@results = {}
diff --git a/modules/storages/app/components/storages/admin/health/check_result_component.html.erb b/modules/storages/app/components/storages/admin/health/check_result_component.html.erb
index 75803712c73..83e72962e51 100644
--- a/modules/storages/app/components/storages/admin/health/check_result_component.html.erb
+++ b/modules/storages/app/components/storages/admin/health/check_result_component.html.erb
@@ -53,7 +53,9 @@ See COPYRIGHT and LICENSE files for more details.
if data[:error_text].present?
cell.with_row(mt: 1) do
- render(Primer::Beta::Text.new) { data[:error_text] }
+ render(Primer::Beta::Text.new(test_selector: "op-storages--health-status-check-information")) do
+ data[:error_text]
+ end
end
end
end
diff --git a/modules/storages/app/components/storages/admin/health/health_report_component.html.erb b/modules/storages/app/components/storages/admin/health/health_report_component.html.erb
index 1be63b06bbf..30cd46ad77e 100644
--- a/modules/storages/app/components/storages/admin/health/health_report_component.html.erb
+++ b/modules/storages/app/components/storages/admin/health/health_report_component.html.erb
@@ -59,7 +59,7 @@ See COPYRIGHT and LICENSE files for more details.
@report.each_pair do |group, checks|
report_container.with_row(mt: 3) do
- render(Primer::Beta::BorderBox.new) do |box|
+ render(Primer::Beta::BorderBox.new(test_selector: "op-storages--health-report-group")) do |box|
box.with_header do
flex_layout(justify_content: :space_between) do |header|
header.with_column do
diff --git a/modules/storages/app/components/storages/admin/side_panel/health_status_component.html.erb b/modules/storages/app/components/storages/admin/side_panel/health_status_component.html.erb
index cb134f97a5f..40106b19fe0 100644
--- a/modules/storages/app/components/storages/admin/side_panel/health_status_component.html.erb
+++ b/modules/storages/app/components/storages/admin/side_panel/health_status_component.html.erb
@@ -32,11 +32,7 @@ See COPYRIGHT and LICENSE files for more details.
render(Primer::OpenProject::SidePanel::Section.new) do |section|
section.with_title { I18n.t("storages.health.title") }
- flex_layout do |health_status_container|
- health_status_container.with_row do
- render(Storages::Admin::SidePanel::ValidationResultComponent.new(storage: model, result: report))
- end
- end
+ render(Storages::Admin::SidePanel::ValidationResultComponent.new(storage: model, result: report))
end
end
%>
diff --git a/modules/storages/app/components/storages/admin/side_panel/health_status_component.rb b/modules/storages/app/components/storages/admin/side_panel/health_status_component.rb
index dc01abec961..79dfa84b1dc 100644
--- a/modules/storages/app/components/storages/admin/side_panel/health_status_component.rb
+++ b/modules/storages/app/components/storages/admin/side_panel/health_status_component.rb
@@ -39,7 +39,16 @@ module Storages
private
def report
- Rails.cache.read("storage_#{model.id}_health_status_report")
+ validator = case model.provider_type
+ when ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD
+ Peripherals::ConnectionValidators::NextcloudValidator.new(storage: model)
+ when ::Storages::Storage::PROVIDER_TYPE_ONE_DRIVE
+ raise "Unsupported provider type: #{model.provider_type}"
+ else
+ raise "Unsupported provider type: #{model.provider_type}"
+ end
+
+ Rails.cache.read(validator.report_cache_key)
end
end
end
diff --git a/modules/storages/app/components/storages/admin/side_panel/validation_result_component.rb b/modules/storages/app/components/storages/admin/side_panel/validation_result_component.rb
index 7f647c76f89..facb3cbdf2b 100644
--- a/modules/storages/app/components/storages/admin/side_panel/validation_result_component.rb
+++ b/modules/storages/app/components/storages/admin/side_panel/validation_result_component.rb
@@ -65,7 +65,7 @@ module Storages
I18n.t("storages.health.summary.warning")
end
- "#{text} #{I18n.t('storages.health.checked', datetime: @result.latest_timestamp)}"
+ "#{text} #{I18n.t('storages.health.checked', datetime: helpers.format_time(@result.latest_timestamp))}"
end
end
end
diff --git a/modules/storages/app/controllers/storages/admin/health_status_controller.rb b/modules/storages/app/controllers/storages/admin/health_status_controller.rb
index 249f9df6cdb..957ba672ee0 100644
--- a/modules/storages/app/controllers/storages/admin/health_status_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/health_status_controller.rb
@@ -47,7 +47,7 @@ module Storages
end
def show
- @report = Rails.cache.read(cache_key)
+ @report = Rails.cache.read(validator.report_cache_key)
end
def create
@@ -71,21 +71,22 @@ module Storages
end
def create_and_cache_report
- case @storage.provider_type
- when ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD
- report = Peripherals::ConnectionValidators::NextcloudValidator.new(storage: @storage).validate
- when ::Storages::Storage::PROVIDER_TYPE_ONE_DRIVE
- raise "Unsupported provider type: #{@storage.provider_type}"
- else
- raise "Unsupported provider type: #{@storage.provider_type}"
- end
-
- Rails.cache.write(cache_key, report, expires_in: 6.hours)
+ report = validator.validate
+ Rails.cache.write(validator.report_cache_key, report, expires_in: 6.hours)
report
end
- def cache_key = "storage_#{@storage.id}_health_status_report"
+ def validator
+ @validator ||= case @storage.provider_type
+ when ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD
+ Peripherals::ConnectionValidators::NextcloudValidator.new(storage: @storage)
+ when ::Storages::Storage::PROVIDER_TYPE_ONE_DRIVE
+ raise "Unsupported provider type: #{@storage.provider_type}"
+ else
+ raise "Unsupported provider type: #{@storage.provider_type}"
+ end
+ end
end
end
end
diff --git a/modules/storages/spec/components/storages/admin/health/check_result_component_spec.rb b/modules/storages/spec/components/storages/admin/health/check_result_component_spec.rb
new file mode 100644
index 00000000000..c57b3725819
--- /dev/null
+++ b/modules/storages/spec/components/storages/admin/health/check_result_component_spec.rb
@@ -0,0 +1,95 @@
+# 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 "rails_helper"
+
+RSpec.describe Storages::Admin::Health::CheckResultComponent, type: :component do
+ let(:group_key) { :base_configuration }
+
+ subject(:check_result_component) { described_class.new(group: group_key, result: check_result) }
+
+ before do
+ render_inline(check_result_component)
+ end
+
+ context "if check result is successful" do
+ let(:check_result) { Storages::Peripherals::ConnectionValidators::CheckResult.success(:capabilities_request) }
+
+ it "renders the component" do
+ expect(page).to have_text(I18n.t("storages.health.checks.#{group_key}.#{check_result.key}"))
+ expect(page).to have_css(".color-fg-success", text: "Passed")
+ expect(page).to have_no_link("More information")
+ expect(page).not_to have_test_selector("op-storages--health-status-check-information")
+ end
+ end
+
+ context "if check result is skipped" do
+ let(:check_result) { Storages::Peripherals::ConnectionValidators::CheckResult.skipped(:capabilities_request) }
+
+ it "renders the component" do
+ expect(page).to have_text(I18n.t("storages.health.checks.#{group_key}.#{check_result.key}"))
+ expect(page).to have_css(".color-fg-attention", text: "Skipped")
+ expect(page).to have_no_link("More information")
+ expect(page).not_to have_test_selector("op-storages--health-status-check-information")
+ end
+ end
+
+ context "if check result is a warning" do
+ let(:check_result) do
+ Storages::Peripherals::ConnectionValidators::CheckResult.warning(
+ :capabilities_request,
+ "it is no moon"
+ )
+ end
+
+ it "renders the component" do
+ expect(page).to have_text(I18n.t("storages.health.checks.#{group_key}.#{check_result.key}"))
+ expect(page).to have_css(".color-fg-attention", text: "Warning")
+ expect(page).to have_link("More information")
+ expect(page).to have_test_selector("op-storages--health-status-check-information", text: check_result.message)
+ end
+ end
+
+ context "if check result is a failure" do
+ let(:check_result) do
+ Storages::Peripherals::ConnectionValidators::CheckResult.failure(
+ :capabilities_request,
+ "it is really no moon"
+ )
+ end
+
+ it "renders the component" do
+ expect(page).to have_text(I18n.t("storages.health.checks.#{group_key}.#{check_result.key}"))
+ expect(page).to have_css(".color-fg-danger", text: "Failed")
+ expect(page).to have_link("More information")
+ expect(page).to have_test_selector("op-storages--health-status-check-information", text: check_result.message)
+ end
+ end
+end
diff --git a/modules/storages/spec/components/storages/admin/health/health_report_component_spec.rb b/modules/storages/spec/components/storages/admin/health/health_report_component_spec.rb
new file mode 100644
index 00000000000..4f443208a7c
--- /dev/null
+++ b/modules/storages/spec/components/storages/admin/health/health_report_component_spec.rb
@@ -0,0 +1,125 @@
+# 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 "rails_helper"
+
+RSpec.describe Storages::Admin::Health::HealthReportComponent, type: :component do
+ let(:storage) { create(:nextcloud_storage_configured) }
+
+ subject(:health_report_component) { described_class.new(storage:, report:) }
+
+ before do
+ render_inline(health_report_component)
+ end
+
+ context "if report is not available" do
+ let(:report) { nil }
+
+ it "renders a placeholder blankslate" do
+ expect(page).to have_text("No report available")
+ expect(page).to have_link("Run tests now")
+ end
+ end
+
+ context "if report is available" do
+ let(:report) do
+ # rubocop:disable Naming/VariableNumber
+ generate_test_report(
+ group_1: %i[success success],
+ group_2: %i[skipped skipped],
+ group_3: %i[success success warning warning],
+ group_4: %i[success failure failure],
+ group_5: %i[success failure warning]
+ )
+ # rubocop:enable Naming/VariableNumber
+ end
+
+ it "renders a summary" do
+ expect(page).to have_text("3 checks failed")
+ expect(page).to have_text("Some checks failed and the system does not work as expected.")
+ end
+
+ it "renders each group separately" do
+ expect(page).to have_test_selector("op-storages--health-report-group", count: 5)
+
+ summaries = {
+ 0 => "All checks passed",
+ 1 => "All checks passed",
+ 2 => "2 checks returned a warning",
+ 3 => "2 checks failed",
+ 4 => "1 check failed"
+ }
+
+ page.all(test_selector("op-storages--health-report-group")).each_with_index do |group, idx|
+ expect(group).to have_text("Group #{idx + 1}")
+ expect(group).to have_text(summaries[idx])
+ end
+ end
+ end
+
+ private
+
+ def generate_test_group(group_key, checks)
+ group = Storages::Peripherals::ConnectionValidators::ValidationGroupResult.new
+
+ checks.each_with_index do |check, idx|
+ key = :"check_#{idx + 1}"
+ result = case check
+ when :success
+ Storages::Peripherals::ConnectionValidators::CheckResult.success(key)
+ when :warning
+ Storages::Peripherals::ConnectionValidators::CheckResult.warning(key, "it is no moon")
+ when :failure
+ Storages::Peripherals::ConnectionValidators::CheckResult.failure(key, "it is really no moon")
+ else
+ Storages::Peripherals::ConnectionValidators::CheckResult.skipped(key)
+ end
+
+ group.register_check(key)
+ group.update_result(key, result)
+ allow(I18n).to receive(:t).with("storages.health.checks.#{group_key}.#{key}").and_return(key.to_s.humanize)
+ end
+
+ group
+ end
+
+ def generate_test_report(map)
+ allow(I18n).to receive(:t).and_call_original
+ report = Storages::Peripherals::ConnectionValidators::ValidatorResult.new
+
+ map.each_pair do |key, values|
+ result = generate_test_group(key, values)
+ report.add_group_result(key, result)
+ allow(I18n).to receive(:t).with("storages.health.checks.#{key}.header").and_return(key.to_s.humanize)
+ end
+
+ report
+ end
+end
diff --git a/modules/storages/spec/controllers/storages/admin/health_status_controller_spec.rb b/modules/storages/spec/controllers/storages/admin/health_status_controller_spec.rb
new file mode 100644
index 00000000000..7d0adb71e2d
--- /dev/null
+++ b/modules/storages/spec/controllers/storages/admin/health_status_controller_spec.rb
@@ -0,0 +1,103 @@
+# 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"
+
+RSpec.describe Storages::Admin::HealthStatusController do
+ let(:user) { build_stubbed(:admin) }
+ let(:storage) { build_stubbed(:nextcloud_storage_configured) }
+ let(:params) { { storage_id: storage.id } }
+
+ before do
+ allow(Storages::Storage).to receive(:find).with(storage.id.to_s).and_return(storage)
+
+ login_as user
+ end
+
+ context "if not admin" do
+ let(:user) { build_stubbed(:user) }
+
+ it "renders 403" do
+ get :show, params: params
+ expect(response).to have_http_status :forbidden
+ end
+ end
+
+ context "if not logged in" do
+ let(:user) { User.anonymous }
+
+ it "renders 403" do
+ get :show, params: params
+ expect(response.status)
+ .to redirect_to signin_url(back_url: admin_settings_storage_health_status_report_url(storage))
+ end
+ end
+
+ describe "#show" do
+ it "renders the show page" do
+ get :show, params: params
+ expect(response).to be_successful
+ expect(response).to render_template "show"
+ end
+ end
+
+ describe "#create" do
+ let(:cache_key) { "my_cache_key" }
+
+ before do
+ validator = instance_double(Storages::Peripherals::ConnectionValidators::NextcloudValidator)
+ report = Storages::Peripherals::ConnectionValidators::ValidatorResult.new
+ allow(Storages::Peripherals::ConnectionValidators::NextcloudValidator).to receive(:new).and_return(validator)
+ allow(validator).to receive_messages(validate: report, report_cache_key: cache_key)
+ end
+
+ it "creates and caches a health status report and redirects to show" do
+ post :create, params: params
+ expect(response.status).to redirect_to admin_settings_storage_health_status_report_path(storage)
+ expect(Rails.cache.read(cache_key)).to be_a(Storages::Peripherals::ConnectionValidators::ValidatorResult)
+ end
+ end
+
+ describe "#create_health_status_report" do
+ let(:cache_key) { "my_cache_key" }
+
+ before do
+ validator = instance_double(Storages::Peripherals::ConnectionValidators::NextcloudValidator)
+ report = Storages::Peripherals::ConnectionValidators::ValidatorResult.new
+ allow(Storages::Peripherals::ConnectionValidators::NextcloudValidator).to receive(:new).and_return(validator)
+ allow(validator).to receive_messages(validate: report, report_cache_key: cache_key)
+ end
+
+ it "creates and caches a health status report and updates page via turbo stream" do
+ post :create_health_status_report, params: params, as: :turbo_stream
+ expect(response).to be_successful
+ end
+ end
+end