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

Added a controller spec for testing, rewritten error propagation

Frank Bergmanna8cf96e1c7d0
diff --git a/app/services/service_result.rb b/app/services/service_result.rb
index eac8a70be2f..09bbf9505ac 100644
--- a/app/services/service_result.rb
+++ b/app/services/service_result.rb
@@ -198,6 +198,12 @@ class ServiceResult
end
end
+ def merge_dependent_errors!
+ dependent_results.each do |dependent_result|
+ errors.merge!(dependent_result.errors)
+ end
+ end
+
private
def initialize_errors(errors)
diff --git a/modules/storages/app/controllers/storages/admin/storages_controller.rb b/modules/storages/app/controllers/storages/admin/storages_controller.rb
index 6e45a0258c4..4ae489cffa7 100644
--- a/modules/storages/app/controllers/storages/admin/storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/storages_controller.rb
@@ -86,11 +86,13 @@ class Storages::Admin::StoragesController < ApplicationController
def create
service_result = Storages::Storages::CreateService.new(user: current_user).call(permitted_storage_params)
@object = service_result.result
+ @oauth_application = result_from_first_dependent_service(service_result)
- if service_result.success? && (@oauth_application = service_result.dependent_results&.first&.result)
+ if service_result.success? && @oauth_application
flash[:notice] = I18n.t(:notice_successful_create)
render :show_oauth_application
else
+ service_result.merge_dependent_errors!
@errors = service_result.errors
render :new
end
@@ -167,6 +169,10 @@ class Storages::Admin::StoragesController < ApplicationController
private
+ def result_from_first_dependent_service(service_result)
+ service_result.dependent_results&.first&.result
+ end
+
def ensure_storages_module_active
return if OpenProject::FeatureDecisions.storages_module_active?
diff --git a/modules/storages/app/services/storages/storages/create_service.rb b/modules/storages/app/services/storages/storages/create_service.rb
index 27646a440e4..38ac8635e8c 100644
--- a/modules/storages/app/services/storages/storages/create_service.rb
+++ b/modules/storages/app/services/storages/storages/create_service.rb
@@ -39,14 +39,13 @@ module Storages::Storages
def after_perform(service_call)
super(service_call)
- storage = service_call.result
+ storage = service_call.result
# Automatically create an OAuthApplication object for the Nextcloud storage
# using values from storage (particularly :host) as defaults
if storage.provider_type == 'nextcloud'
persist_service_result = ::Storages::OAuthApplications::CreateService.new(storage:, user:).call
- # Show errors in storage form, so use merge! instead of add_dependent
- service_call.merge!(persist_service_result)
+ service_call.add_dependent!(persist_service_result)
end
service_call
diff --git a/modules/storages/spec/controller/storages_controller_controller_spec.rb b/modules/storages/spec/controller/storages_controller_controller_spec.rb
new file mode 100644
index 00000000000..ddfebe8055f
--- /dev/null
+++ b/modules/storages/spec/controller/storages_controller_controller_spec.rb
@@ -0,0 +1,73 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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_relative '../spec_helper'
+
+# These specs mainly check that error messages from a sub-service
+# (about unsafe hosts with HTTP protocol) are passed to the main form.
+describe ::Storages::Admin::StoragesController, with_flag: { storages_module_active: true }, type: :controller do
+ render_views # rendering views is stubbed by default in controller specs
+ include StorageServerHelpers
+
+ let(:admin) { create :admin }
+ let(:host) { "www.example.com" }
+ let(:content_type_json) { { 'Content-Type' => 'application/json; charset=utf-8' } }
+
+ before do
+ login_as admin
+ end
+
+ describe 'with valid attributes', webmock: true do
+ let(:params) { { storages_storage: { name: "My Nextcloud", host: "https://#{host}" } } }
+
+ before do
+ mock_server_capabilities_response("https://#{host}")
+ post :create, params:
+ end
+
+ it 'is successful' do
+ expect(response).to be_successful
+ expect(response.body).not_to include("Redirect uri must be an HTTPS/SSL URI")
+ expect(response.body).to include(I18n.t(:notice_successful_create))
+ end
+ end
+
+ describe 'with invalid attributes', webmock: true do
+ let(:params) { { storages_storage: { name: "My Nextcloud", host: "http://#{host}" } } }
+
+ before do
+ mock_server_capabilities_response("http://#{host}")
+ post :create, params:
+ end
+
+ it 'complains about HTTP being invalid' do
+ expect(response).to be_successful # you get a 200 response despite errors...
+ expect(response.body).to include("Redirect uri must be an HTTPS/SSL URI")
+ end
+ end
+end