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
Cleanup/completion of contracts specs for storages module (#10279)
Frank Bergmann9fa891a755e6
diff --git a/modules/storages/app/contracts/storages/file_links/create_contract.rb b/modules/storages/app/contracts/storages/file_links/create_contract.rbindex 7c5275b267a..09e3e76be62 100644--- a/modules/storages/app/contracts/storages/file_links/create_contract.rb+++ b/modules/storages/app/contracts/storages/file_links/create_contract.rb@@ -28,9 +28,18 @@class Storages::FileLinks::CreateContract < BaseContractvalidate :validate_storage_url+ validate :validate_user_allowed_to_manageprivate+ # Check that the current has the permission on the project.+ # model variable is available because the concern is executed inside a contract.+ def validate_user_allowed_to_manage+ unless user.allowed_to?(:manage_file_links, model.container.project)+ errors.add :base, :error_unauthorized+ end+ end+def validate_storage_urlerrors.add(:storage_id, :invalid) if model.storage_id.blank?enddiff --git a/modules/storages/app/contracts/storages/project_storages/base_contract.rb b/modules/storages/app/contracts/storages/project_storages/base_contract.rbindex fa9b5f0e01b..6d3c0e746e4 100644--- a/modules/storages/app/contracts/storages/project_storages/base_contract.rb+++ b/modules/storages/app/contracts/storages/project_storages/base_contract.rb@@ -31,13 +31,20 @@# Used by: projects_storages_controller.rb and in the APImodule Storages::ProjectStoragesclass BaseContract < ::ModelContract- include ActiveModel::Validations # Include validation library+ # "Concern" just injects a permission checking routine.+ # Not sure where this concern is reused.+ include ::Storages::ProjectStorages::Concerns::ManageStoragesGuarded+ # Include validation library+ include ActiveModel::Validations# Attributes project and storage can be writtenattribute :project+ validates_presence_of :projectattribute :storage+ validates_presence_of :storage- # Attribute creator can be written by the creator of the object+ # Attribute "creator" can only have the value of current user.+ # "writable: false" means that the attribute can't be overwritten with an update.attribute :creator, writable: false dovalidate_creator_is_userenddiff --git a/modules/storages/app/contracts/storages/project_storages/concerns/manage_storages_guarded.rb b/modules/storages/app/contracts/storages/project_storages/concerns/manage_storages_guarded.rbnew file mode 100644index 00000000000..5d082f68f7d--- /dev/null+++ b/modules/storages/app/contracts/storages/project_storages/concerns/manage_storages_guarded.rb@@ -0,0 +1,69 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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.+#++++# Purpose: This is a "concern" to check if a user is authorized to+# Manage Storages. ToDo: Why "Guarded"?+# It acts as a factored-out add-on to a model, that's why there is+# model specific functions in the "included" callback.+# Reference: Here is a tutorial about concerns. They basically inject+# the content of the "included do ... end" block into their target.+# http://vaidehijoshi.github.io/blog/2015/10/13/stop-worrying-and-start-being-concerned-activesupport-concerns/+# Used by: base_contract.rb and therefore also in CreateContract.+# This concern could also have been written using a validation in the contract.+# However, as a concern it may be reused in other parts of the module (not yet).+# Returns: An array of errors with section and code++# ToDo: I tried to change the path to manage_project_storages_guarded.rb,+# but got an error running the spec then.++module Storages::ProjectStorages+ module Concerns+ module ManageStoragesGuarded+ # extend is like include, but imports methods as class (not instance) methods.+ # Using extend ActiveSupport::Concern is part of the Concern pattern.+ extend ActiveSupport::Concern++ # "included" is a callback that is invoked whenever this Concern is+ # included in another module or class, injecting the contents of the do-end block+ included do+ # Generic validation to call a custom procedure in a Rails+ validate :validate_user_allowed_to_manage++ private++ # Check that the current has the permission on the project.+ # model variable is available because the concern is executed inside a contract.+ def validate_user_allowed_to_manage+ unless user.allowed_to?(:manage_storages_in_project, model.project)+ errors.add :base, :error_unauthorized+ end+ end+ end+ end+ end+enddiff --git a/modules/storages/app/contracts/storages/storages/delete_contract.rb b/modules/storages/app/contracts/storages/storages/delete_contract.rbindex 363c5713bf5..4c0ef902898 100644--- a/modules/storages/app/contracts/storages/storages/delete_contract.rb+++ b/modules/storages/app/contracts/storages/storages/delete_contract.rb@@ -30,8 +30,6 @@module Storagesmodule Storagesclass DeleteContract < ::DeleteContract- include ::Storages::Storages::Concerns::ManageStoragesGuarded-delete_permission :adminendenddiff --git a/modules/storages/app/services/storages/project_storages/create_service.rb b/modules/storages/app/services/storages/project_storages/create_service.rbindex e0d7396edd9..59b2c32ec15 100644--- a/modules/storages/app/services/storages/project_storages/create_service.rb+++ b/modules/storages/app/services/storages/project_storages/create_service.rb@@ -26,7 +26,16 @@# See COPYRIGHT and LICENSE files for more details.#+++# See: ../storages/create_service.rb for comments pm servicesmodule Storages::ProjectStoragesclass CreateService < ::BaseServices::Create+ protected++ # Override the "creator_id" parameter with the actual user.+ # before_perform is called in the service before attributes are set.+ def before_perform(params, _service_result)+ params[:creator_id] = user.id+ super(params, _service_result)+ endendenddiff --git a/modules/storages/app/services/storages/storages/create_service.rb b/modules/storages/app/services/storages/storages/create_service.rbindex 54c36065314..a436539b4b6 100644--- a/modules/storages/app/services/storages/storages/create_service.rb+++ b/modules/storages/app/services/storages/storages/create_service.rb@@ -26,14 +26,22 @@# See COPYRIGHT and LICENSE files for more details.#++-# The comments here are also valid for the other *_service.rb files# The logic for creating storage was extracted from the controller and put into# a service: https://dev.to/joker666/ruby-on-rails-pattern-service-objects-b19# Purpose: create and persist a Storages::Storage record# Used by: Storages::Admin::StoragesController#create, could also be used by the-# API in the future+# API in the future.# Reference: https://www.openproject.org/docs/development/concepts/contracted-services/+# The comments here are also valid for the other *_service.rb filesmodule Storages::Storagesclass CreateService < ::BaseServices::Create+ protected++ # Override the "creator_id" parameter with the actual user.+ # before_perform is called in the service before attributes are set.+ def before_perform(params, _service_result)+ params[:creator_id] = user.id+ super(params, _service_result)+ endendenddiff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rbindex d2b638f7151..b4a2358ec95 100644--- a/modules/storages/lib/open_project/storages/engine.rb+++ b/modules/storages/lib/open_project/storages/engine.rb@@ -49,7 +49,6 @@ module OpenProject::Storagessettings: {},name: 'OpenProject Storages' do# Defines permission constraints used in the module (controller, etc.)- # Apparently only defined the name, dependencies and global(?) permissions?# Permissions documentation: https://www.openproject.org/docs/development/concepts/permissions/#definition-of-permissionsproject_module :storages, dependencies: :work_package_tracking dopermission :view_file_links,diff --git a/modules/storages/spec/contracts/storages/file_links/create_contract_spec.rb b/modules/storages/spec/contracts/storages/file_links/create_contract_spec.rbnew file mode 100644index 00000000000..b33cb4ae7a2--- /dev/null+++ b/modules/storages/spec/contracts/storages/file_links/create_contract_spec.rb@@ -0,0 +1,39 @@+#-- 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 'spec_helper'+require 'contracts/shared/model_contract_shared_context'+require_relative 'shared_contract_examples'++describe Storages::FileLinks::CreateContract do+ include_context 'ModelContract shared context'++ it_behaves_like 'file_link contract' do+ let(:contract) { described_class.new(file_link, current_user) }+ end+enddiff --git a/modules/storages/spec/contracts/storages/file_links/delete_contract_spec.rb b/modules/storages/spec/contracts/storages/file_links/delete_contract_spec.rbnew file mode 100644index 00000000000..59f697ab54c--- /dev/null+++ b/modules/storages/spec/contracts/storages/file_links/delete_contract_spec.rb@@ -0,0 +1,59 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 'contracts/shared/model_contract_shared_context'++describe ::Storages::FileLinks::DeleteContract do+ include_context 'ModelContract shared context'++ let(:current_user) { create(:user) }+ let(:role) { create(:existing_role, permissions: [:manage_file_links]) }+ let(:project) { create(:project, members: { current_user => role }) }+ let(:work_package) { create(:work_package, project: project) }+ let(:file_link) { create(:file_link, container: work_package) }+ let(:contract) { described_class.new(file_link, current_user) }++ before do+ login_as(current_user)+ end++ # Default test setup should be valid ("happy test setup").+ # This tests works with manage_storages_in_project permissions for current_user.+ it_behaves_like 'contract is valid'++ # Now we remove the permissions from the user by creating a role without special perms.+ context 'without manage_storages_in_project permission for project' do+ let(:role) { create(:existing_role) }++ it_behaves_like 'contract is invalid'+ end++ # Generic checks that the contract is valid for valid admin, but invalid otherwise+ it_behaves_like 'contract is valid for active admins and invalid for regular users'+enddiff --git a/modules/storages/spec/contracts/storages/file_links/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/file_links/shared_contract_examples.rbnew file mode 100644index 00000000000..3a6836f4010--- /dev/null+++ b/modules/storages/spec/contracts/storages/file_links/shared_contract_examples.rb@@ -0,0 +1,56 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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_relative '../../../support/storage_server_helpers'++shared_examples_for 'file_link contract' do+ let(:current_user) { create(:user) }+ let(:role) { create(:existing_role, permissions: [:manage_file_links]) }+ let(:project) { create(:project, members: { current_user => role }) }+ let(:work_package) { create(:work_package, project: project) }+ let(:storage) { create(:storage) }+ let(:file_link) { create(:file_link, container: work_package, storage: storage) }++ it_behaves_like 'contract is valid for active admins and invalid for regular users'++ describe 'validations' do+ context 'when all attributes are valid' do+ it_behaves_like 'contract is valid'+ end++ context 'when storage_id is invalid' do+ context 'as it is empty' do+ let(:storage_id) { "" }+ let(:file_link) { create(:file_link, container: work_package, storage_id: storage_id) }++ it_behaves_like 'contract is invalid'+ end+ end+ end+enddiff --git a/modules/storages/spec/contracts/storages/project_storages/create_contract_spec.rb b/modules/storages/spec/contracts/storages/project_storages/create_contract_spec.rbindex e5361faddf4..22ef54bad9a 100644--- a/modules/storages/spec/contracts/storages/project_storages/create_contract_spec.rb+++ b/modules/storages/spec/contracts/storages/project_storages/create_contract_spec.rb@@ -46,5 +46,36 @@ describe Storages::ProjectStorages::CreateContract do)endlet(:contract) { described_class.new(project_storage, current_user) }++ subject(:contract) do+ described_class.new(project_storage, current_user)+ end+ end++ context 'when checking creator_id' do+ let(:current_user) { create(:admin) }+ let(:other_user) { create(:user) }++ context 'as creator_id == user_id' do+ let(:project_storage) { create(:project_storage, creator: current_user) }+ let(:contract) { described_class.new(project_storage, current_user) }++ before do+ login_as(current_user)+ end++ it_behaves_like 'contract is valid'+ end++ context 'as creator_id != user_id' do+ let(:project_storage) { create(:project_storage, creator: other_user) }+ let(:contract) { described_class.new(project_storage, current_user) }++ before do+ login_as(current_user)+ end++ it_behaves_like 'contract is invalid'+ endendenddiff --git a/modules/storages/spec/contracts/storages/project_storages/delete_contract_spec.rb b/modules/storages/spec/contracts/storages/project_storages/delete_contract_spec.rbindex d4111c50ec6..66a1a47a117 100644--- a/modules/storages/spec/contracts/storages/project_storages/delete_contract_spec.rb+++ b/modules/storages/spec/contracts/storages/project_storages/delete_contract_spec.rb@@ -34,20 +34,25 @@ require 'contracts/shared/model_contract_shared_context'describe ::Storages::ProjectStorages::DeleteContract doinclude_context 'ModelContract shared context'+ let(:current_user) { create(:user) }let(:role) { create(:existing_role, permissions: [:manage_storages_in_project]) }let(:project) { create(:project, members: { current_user => role }) }- let(:current_user) { create(:user) }let(:project_storage) { create(:project_storage, project: project) }let(:contract) { described_class.new(project_storage, current_user) }# Default test setup should be valid ("happy test setup").- # The example (in other words tests) below was included above from "ModelContract shared context".- # It assumes a current_user and and a contract to be defined.+ # The example below was included above from "ModelContract shared context".+ # This tests works with manage_storages_in_project permissions for current_user.it_behaves_like 'contract is valid'+ # Now we remove the permissions from the user by creating a role without special perms.context 'without manage_storages_in_project permission for project' do+ # existing_role is a role _without_ the :manage_storages_in_project permissionlet(:role) { create(:existing_role) }it_behaves_like 'contract is invalid'end++ # Generic checks that the contract is valid for valid admin, but invalid otherwise+ it_behaves_like 'contract is valid for active admins and invalid for regular users'enddiff --git a/modules/storages/spec/contracts/storages/project_storages/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/project_storages/shared_contract_examples.rbindex 85af8b0b6dd..78fd6801cd9 100644--- a/modules/storages/spec/contracts/storages/project_storages/shared_contract_examples.rb+++ b/modules/storages/spec/contracts/storages/project_storages/shared_contract_examples.rb@@ -28,20 +28,52 @@# See COPYRIGHT and LICENSE files for more details.#+++# Include OpenProject support/*.rb filesrequire 'spec_helper'-# Purpose: Common testing logic between create and update specs.-# These two contracts share a lot, they are also both children of a BaseContract.+# Purpose: Common testing logic shared between create and update specs.shared_examples_for 'ProjectStorages contract' dolet(:current_user) { create(:user) }- let(:role) { create(:existing_role, permissions: [:manage_storages_in_project]) }- let(:project) { create(:project, members: { current_user => role }) }- let(:storage) { create(:storage) }+ # The user needs "edit_project" to see the project's settings page+ let(:role) { create(:role, permissions: %i[manage_storages_in_project edit_project]) }+ # Create a project managed by current user and with Storages enabled.+ let(:project) do+ create(:project,+ members: { current_user => role },+ enabled_module_names: %i[storages])+ end+ let(:storage) { create(:storage, name: "Storage 1") }let(:storage_creator) { current_user }+ # This is not 100% precise, as the required permission is not :admin+ # but :manage_storages_in_project, but let's still include this.+ it_behaves_like 'contract is valid for active admins and invalid for regular users'+describe 'validations' do- context 'when all attributes are valid' do+ context 'when authorized, with permissions and all attributes are valid' doit_behaves_like 'contract is valid'end++ context 'when project is invalid' do+ context 'as it is nil' do+ let(:project) { nil }++ it_behaves_like 'contract is invalid'+ end+ end++ context 'when storage is invalid' do+ context 'as it is nil' do+ let(:storage) { nil }++ it_behaves_like 'contract is invalid'+ end+ end++ context 'when not the necessary permissions' do+ let(:current_user) { build_stubbed(:user) }++ it_behaves_like 'contract user is unauthorized'+ endendenddiff --git a/modules/storages/spec/contracts/storages/storages/delete_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/delete_contract_spec.rbindex f11699d8ce7..5e77e5c8c0c 100644--- a/modules/storages/spec/contracts/storages/storages/delete_contract_spec.rb+++ b/modules/storages/spec/contracts/storages/storages/delete_contract_spec.rb@@ -31,34 +31,12 @@require 'spec_helper'require 'contracts/shared/model_contract_shared_context'-describe PlaceholderUsers::DeleteContract do+describe ::Storages::Storages::DeleteContract doinclude_context 'ModelContract shared context'- let(:placeholder_user) { create(:placeholder_user) }- let(:role) { create :existing_role, permissions: [:manage_members] }- let(:shared_project) { create(:project, members: { placeholder_user => role, current_user => role }) }- let(:not_shared_project) { create(:project, members: { placeholder_user => role }) }- let(:contract) { described_class.new(placeholder_user, current_user) }+ let(:storage) { create(:storage) }+ let(:contract) { described_class.new(storage, current_user) }+ # Generic checks that the contract is valid for valid admin, but invalid otherwiseit_behaves_like 'contract is valid for active admins and invalid for regular users'-- context 'when user with global permission to manage_placeholders' do- let(:current_user) { create(:user, global_permission: %i[manage_placeholder_user]) }-- before do- shared_project- end-- context 'when user is allowed to manage members in all projects of the placeholder user' do- it_behaves_like 'contract is valid'- end-- context 'when user is not allowed to manage members in all projects of the placeholder user' do- before do- not_shared_project- end-- it_behaves_like 'contract user is unauthorized'- end- endenddiff --git a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rbindex 1ae9aeaba84..006ce130abb 100644--- a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb+++ b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb@@ -28,16 +28,16 @@# See COPYRIGHT and LICENSE files for more details.#++-require_relative '../../../spec_helper'+require 'spec_helper'+require_relative '../../../support/storage_server_helpers'shared_examples_for 'storage contract', :storage_server_helpers, webmock: true do+ # Only admins have the right to create/delete storages.+ let(:current_user) { create(:admin) }let(:storage_name) { 'Storage 1' }let(:storage_provider_type) { 'nextcloud' }let(:storage_host) { 'https://host1.example.com' }let(:storage_creator) { current_user }-- let(:current_user) { create(:admin) }-let(:host_response_code) { '200' }let(:host_response_message) { 'OK' }let(:host_response_major_version) { 23 }diff --git a/modules/storages/spec/services/storages/file_links/create_service_spec.rb b/modules/storages/spec/services/storages/file_links/create_service_spec.rbnew file mode 100644index 00000000000..769cb89804c--- /dev/null+++ b/modules/storages/spec/services/storages/file_links/create_service_spec.rb@@ -0,0 +1,36 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 'services/base_services/behaves_like_create_service'++describe ::Storages::FileLinks::CreateService, type: :model do+ it_behaves_like 'BaseServices create service' do+ let(:factory) { :file_link }+ end+enddiff --git a/modules/storages/spec/services/storages/file_links/delete_service_spec.rb b/modules/storages/spec/services/storages/file_links/delete_service_spec.rbnew file mode 100644index 00000000000..41850295ab1--- /dev/null+++ b/modules/storages/spec/services/storages/file_links/delete_service_spec.rb@@ -0,0 +1,36 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 'services/base_services/behaves_like_delete_service'++describe ::Storages::FileLinks::DeleteService, type: :model do+ it_behaves_like 'BaseServices delete service' do+ let(:factory) { :file_link }+ end+enddiff --git a/modules/storages/spec/services/storages/file_links/set_attributes_service_spec.rb b/modules/storages/spec/services/storages/file_links/set_attributes_service_spec.rbnew file mode 100644index 00000000000..a746f5ba8b7--- /dev/null+++ b/modules/storages/spec/services/storages/file_links/set_attributes_service_spec.rb@@ -0,0 +1,110 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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'++describe ::Storages::FileLinks::SetAttributesService, type: :model do+ let(:current_user) { build_stubbed(:admin) }++ let(:contract_instance) do+ contract = instance_double(::Storages::Storages::BaseContract, 'contract_instance')+ allow(contract)+ .to receive(:validate)+ .and_return(contract_valid)+ allow(contract)+ .to receive(:errors)+ .and_return(contract_errors)+ contract+ end++ let(:contract_errors) { instance_double(ActiveModel::Errors, 'contract_errors') }+ let(:contract_valid) { true }+ let(:model_valid) { true }++ let(:instance) do+ described_class.new(user: current_user,+ model: model_instance,+ contract_class: contract_class,+ contract_options: {})+ end+ let(:model_instance) { ::Storages::Storage.new }+ let(:contract_class) do+ allow(::Storages::Storages::CreateContract)+ .to receive(:new)+ .and_return(contract_instance)++ ::Storages::Storages::CreateContract+ end++ let(:params) { {} }++ before do+ allow(model_instance)+ .to receive(:valid?)+ .and_return(model_valid)+ end++ subject { instance.call(params) }++ it 'returns the instance as the result' do+ expect(subject.result)+ .to eql model_instance+ end++ it 'is a success' do+ expect(subject)+ .to be_success+ end++ context 'with params' do+ let(:params) do+ {+ name: 'Foobar'+ }+ end++ it 'assigns the params' do+ subject++ expect(model_instance.name).to eq 'Foobar'+ end+ end++ context 'with an invalid contract' do+ let(:contract_valid) { false }++ it 'returns failure' do+ expect(subject).not_to be_success+ end++ it "returns the contract's errors" do+ expect(subject.errors)+ .to eql(contract_errors)+ end+ end+enddiff --git a/modules/storages/spec/services/storages/project_storages/create_service_spec.rb b/modules/storages/spec/services/storages/project_storages/create_service_spec.rbnew file mode 100644index 00000000000..266dc9d6504--- /dev/null+++ b/modules/storages/spec/services/storages/project_storages/create_service_spec.rb@@ -0,0 +1,36 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 'services/base_services/behaves_like_create_service'++describe ::Storages::ProjectStorages::CreateService, type: :model do+ it_behaves_like 'BaseServices create service' do+ let(:factory) { :project_storage }+ end+enddiff --git a/modules/storages/spec/services/storages/project_storages/set_attributes_service_spec.rb b/modules/storages/spec/services/storages/project_storages/set_attributes_service_spec.rbnew file mode 100644index 00000000000..8a14cc48b5b--- /dev/null+++ b/modules/storages/spec/services/storages/project_storages/set_attributes_service_spec.rb@@ -0,0 +1,110 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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'++describe ::Storages::ProjectStorages::SetAttributesService, type: :model do+ let(:current_user) { build_stubbed(:admin) }++ let(:contract_instance) do+ contract = instance_double(::Storages::Storages::BaseContract, 'contract_instance')+ allow(contract)+ .to receive(:validate)+ .and_return(contract_valid)+ allow(contract)+ .to receive(:errors)+ .and_return(contract_errors)+ contract+ end++ let(:contract_errors) { instance_double(ActiveModel::Errors, 'contract_errors') }+ let(:contract_valid) { true }+ let(:model_valid) { true }++ let(:instance) do+ described_class.new(user: current_user,+ model: model_instance,+ contract_class: contract_class,+ contract_options: {})+ end+ let(:model_instance) { ::Storages::Storage.new }+ let(:contract_class) do+ allow(::Storages::Storages::CreateContract)+ .to receive(:new)+ .and_return(contract_instance)++ ::Storages::Storages::CreateContract+ end++ let(:params) { {} }++ before do+ allow(model_instance)+ .to receive(:valid?)+ .and_return(model_valid)+ end++ subject { instance.call(params) }++ it 'returns the instance as the result' do+ expect(subject.result)+ .to eql model_instance+ end++ it 'is a success' do+ expect(subject)+ .to be_success+ end++ context 'with params' do+ let(:params) do+ {+ name: 'Foobar'+ }+ end++ it 'assigns the params' do+ subject++ expect(model_instance.name).to eq 'Foobar'+ end+ end++ context 'with an invalid contract' do+ let(:contract_valid) { false }++ it 'returns failure' do+ expect(subject).not_to be_success+ end++ it "returns the contract's errors" do+ expect(subject.errors)+ .to eql(contract_errors)+ end+ end+enddiff --git a/modules/storages/spec/services/storages/storages/update_service_spec.rb b/modules/storages/spec/services/storages/storages/update_service_spec.rbindex ba033283089..db4049fe19c 100644--- a/modules/storages/spec/services/storages/storages/update_service_spec.rb+++ b/modules/storages/spec/services/storages/storages/update_service_spec.rb@@ -31,6 +31,8 @@require 'spec_helper'require 'services/base_services/behaves_like_update_service'-describe PlaceholderUsers::UpdateService, type: :model do- it_behaves_like 'BaseServices update service'+describe ::Storages::Storages::UpdateService, type: :model do+ it_behaves_like 'BaseServices update service' do+ let(:factory) { :storage }+ endend