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

Add services and contracts for ProjectStorage (#10249)

Frank Bergmannc5fa8a003d35
diff --git a/modules/storages/app/cells/storages/projects_storages_table_cell.rb b/modules/storages/app/cells/storages/projects_storages_table_cell.rb
index 81424f76373..6629017183c 100644
--- a/modules/storages/app/cells/storages/projects_storages_table_cell.rb
+++ b/modules/storages/app/cells/storages/projects_storages_table_cell.rb
@@ -66,10 +66,10 @@ module Storages
def headers
[
- ['name', { caption: Storages::Storage.human_attribute_name(:name) }],
+ ['name', { caption: ::Storages::Storage.human_attribute_name(:name) }],
['provider_type', { caption: I18n.t('storages.provider_types.label') }],
['creator', { caption: I18n.t('storages.label_creator') }],
- ['created_at', { caption: Storages::ProjectStorage.human_attribute_name(:created_at) }]
+ ['created_at', { caption: ::Storages::ProjectStorage.human_attribute_name(:created_at) }]
]
end
end
diff --git a/modules/storages/app/contracts/storages/project_storages/base_contract.rb b/modules/storages/app/contracts/storages/project_storages/base_contract.rb
new file mode 100644
index 00000000000..fa9b5f0e01b
--- /dev/null
+++ b/modules/storages/app/contracts/storages/project_storages/base_contract.rb
@@ -0,0 +1,51 @@
+#-- 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.
+#++
+
+# A "contract" is an OpenProject patter used to validate parameters
+# before actually creating a model.
+# Used by: projects_storages_controller.rb and in the API
+module Storages::ProjectStorages
+ class BaseContract < ::ModelContract
+ include ActiveModel::Validations # Include validation library
+
+ # Attributes project and storage can be written
+ attribute :project
+ attribute :storage
+
+ # Attribute creator can be written by the creator of the object
+ attribute :creator, writable: false do
+ validate_creator_is_user
+ end
+
+ def validate_creator_is_user
+ unless creator == user
+ errors.add(:creator, :invalid)
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/contracts/storages/project_storages/create_contract.rb b/modules/storages/app/contracts/storages/project_storages/create_contract.rb
new file mode 100644
index 00000000000..7fc303d5bd9
--- /dev/null
+++ b/modules/storages/app/contracts/storages/project_storages/create_contract.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.
+#++
+
+module Storages::ProjectStorages
+ # We create the empty class here, so that other pieces of code that looks for the contract in its default place will
+ # find it. So, the CreateService for Storages will expect its contract to be here.
+ #
+ # We inherit from a BaseContract as the BaseContract here is sharing a lot in common with the UpdateContract.
+ class CreateContract < ::Storages::ProjectStorages::BaseContract
+ end
+end
diff --git a/modules/storages/app/contracts/storages/project_storages/delete_contract.rb b/modules/storages/app/contracts/storages/project_storages/delete_contract.rb
new file mode 100644
index 00000000000..f822c3b872a
--- /dev/null
+++ b/modules/storages/app/contracts/storages/project_storages/delete_contract.rb
@@ -0,0 +1,33 @@
+#-- 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.
+#++
+
+module Storages::ProjectStorages
+ class DeleteContract < ::DeleteContract
+ delete_permission(:manage_storages_in_project)
+ end
+end
diff --git a/modules/storages/app/contracts/storages/storages/base_contract.rb b/modules/storages/app/contracts/storages/storages/base_contract.rb
index 685a31b93d7..0a0dc95d04b 100644
--- a/modules/storages/app/contracts/storages/storages/base_contract.rb
+++ b/modules/storages/app/contracts/storages/storages/base_contract.rb
@@ -29,6 +29,10 @@
require 'net/http'
require 'uri'
+# Called by: Somewhere from within a service (ToDo:)
+# UpdateServer by default checks if UpdateContract exists
+# and uses the contract to validate the model under consideration
+# (normally it's a model).
module Storages::Storages
class BaseContract < ::ModelContract
MINIMAL_NEXTCLOUD_VERSION = 23
diff --git a/modules/storages/app/contracts/storages/storages/update_contract.rb b/modules/storages/app/contracts/storages/storages/update_contract.rb
index c2ce143caae..42bd1e332ca 100644
--- a/modules/storages/app/contracts/storages/storages/update_contract.rb
+++ b/modules/storages/app/contracts/storages/storages/update_contract.rb
@@ -26,9 +26,6 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-require 'net/http'
-require 'uri'
-
# See also: base_contract.rb for comments
module Storages::Storages
class UpdateContract < ::Storages::Storages::BaseContract
diff --git a/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb b/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb
index 99722a0eda0..5caf47e1ea9 100644
--- a/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb
@@ -1,5 +1,3 @@
-#-- encoding: UTF-8
-
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2022 the OpenProject GmbH
@@ -28,51 +26,107 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-# Purpose: Controls the page that enables/disables Storages per project.
-# See also: storages_controller.rb for a controller with very similar structure.
-# ToDo: Some differences to StoragesController?
+# This controller manages the creation and deletion of ProjectStorage objects.
+# ProjectStorages belong to projects and indicate that the respective
+# Storage (i.e. a Nextcloud server) is enabled in the project.
+# Please see the standard Rails documentation on controllers:
+# https://guides.rubyonrails.org/action_controller_overview.html
+# Called by: Calls to the controller methods are initiated by user Web GUI
+# actions and mapped to this controller by storages/config/routes.rb.
class Storages::Admin::ProjectsStoragesController < Projects::SettingsController
+ # This is the resource handled in this controller.
+ # So the controller knows that the ID in params (URl) refer to instances of this model.
+ # This defines @object as the model instance.
model_object Storages::ProjectStorage
- before_action :find_model_object, only: %i[destroy]
- before_action :find_optional_project
- before_action :authorize
+ before_action :find_model_object, only: %i[destroy] # Fill @object with ProjectStorage
+ # No need to before_action :find_project_by_project_id as SettingsController already checks
+ # No need to check for before_action :authorize, as the SettingsController already checks this.
+ # This MenuController method defines the default menu item to be used (highlighted)
+ # when rendering the main menu in the left (part of the base layout).
+ # The menu item itself is registered in modules/storages/lib/open_project/storages/engine.rb
menu_item :settings_projects_storages
+ # Show a HTML page with the list of ProjectStorages
+ # Called by: Project -> Settings -> File Storages
def index
+ # Just get the list of ProjectStorages associated with the project
@projects_storages = Storages::ProjectStorage.where(project: @project).includes(:storage)
-
+ # Render the list storages using Ruby "cells" in the /app/cell folder which defines
+ # the ways rows are rendered in a table layout.
render '/storages/project_settings/index'
end
+ # Show a HTML page with a form in order to create a new ProjectStorage
+ # Called by: When a user clicks on the "+New" button in Project -> Settings -> File Storages
def new
- @project_storage = Storages::ProjectStorage.new(project: @project)
+ # Create an empty ProjectStorage object, but don't save it to the database yet.
+ # @project was calculated in before_action (see comments above).
+ # @project_storage is used in the view in order to render the form for a new object
+ @project_storage = ::Storages::ProjectStorages::SetAttributesService
+ .new(user: current_user,
+ model: Storages::ProjectStorage.new,
+ contract_class: EmptyContract)
+ .call({ project: @project })
+ .result
+
+ # Calculate the list of available Storage objects, subtracting already enabled storages.
@available_storages = Storages::Storage.where.not(id: @project.projects_storages.pluck(:storage_id))
+ # Show the HTML form to create the object.
render '/storages/project_settings/new'
end
+ # Create a new ProjectStorage object.
+ # Called by: The new page above with form-data from that form.
+ # rubocop:disable Metrics/AbcSize
def create
+ # Check params and overwrite creator_id and project_id in untrusted data from the Internet
+ # @project was calculated by before_action :find_optional_project.
combined_params = permitted_project_storage_params
.to_h
- .reverse_merge(creator_id: User.current.id, project_id: @project.id)
+ .reverse_merge(creator_id: current_user.id, project_id: @project.id)
+ service_result = ::Storages::ProjectStorages::CreateService
+ .new(user: current_user)
+ .call(combined_params)
- @project_storage = Storages::ProjectStorage.create combined_params
+ # Create success/error messages to the user
+ if service_result.success?
+ flash[:notice] = I18n.t(:notice_successful_create)
+ else
+ flash[:error] = service_result.message || I18n.t('notice_internal_server_error')
+ end
- redirect_to project_settings_projects_storages_path
+ redirect_to project_settings_projects_storages_path # Redirect: Project -> Settings -> File Storages
end
+ # rubocop:enable Metrics/AbcSize
+ # Purpose: Destroy a ProjectStorage object
+ # Called by: By pressing a "Delete" icon in the Project's settings ProjectStorages page
+ # It redirects back to the list of ProjectStorages in the project
def destroy
- Storages::FileLink.joins(:container).where(work_packages: { project_id: @project.id }).delete_all
- @object.destroy
+ # The complex logic for deleting associated objects was moved into a service:
+ # https://dev.to/joker666/ruby-on-rails-pattern-service-objects-b19
+ service_result = Storages::ProjectStorages::DeleteService
+ .new(user: current_user, model: @object)
+ .call
+
+ # Handle errors.
+ unless service_result.success?
+ flash[:error] = service_result.errors.full_messages
+ end
+ # Redirect the user to the URL of Projects -> Settings -> File Storages
redirect_to project_settings_projects_storages_path
end
private
+ # Define the list of permitted parameters for creating/updating a ProjectStorage.
+ # Called by create and update actions above.
def permitted_project_storage_params
+ # "params" is an instance of ActionController::Parameters
params
.require(:storages_project_storage)
.permit('storage_id')
diff --git a/modules/storages/app/services/storages/project_storages/create_service.rb b/modules/storages/app/services/storages/project_storages/create_service.rb
new file mode 100644
index 00000000000..e0d7396edd9
--- /dev/null
+++ b/modules/storages/app/services/storages/project_storages/create_service.rb
@@ -0,0 +1,32 @@
+#-- 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.
+#++
+
+module Storages::ProjectStorages
+ class CreateService < ::BaseServices::Create
+ end
+end
diff --git a/modules/storages/app/services/storages/project_storages/delete_service.rb b/modules/storages/app/services/storages/project_storages/delete_service.rb
new file mode 100644
index 00000000000..0b1cabdda64
--- /dev/null
+++ b/modules/storages/app/services/storages/project_storages/delete_service.rb
@@ -0,0 +1,66 @@
+#-- encoding: UTF-8
+
+#-- 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.
+#++
+
+module Storages::ProjectStorages
+ class DeleteService < ::BaseServices::Delete
+ # "persist" is a callback from BaseContracted.perform
+ # that is supposed to do the actual work in a contract.
+ # So in a DeleteService it performs the actual delete,
+ # except for the @object.destroy that is already performed
+ # by ::BaseServices::Delete
+ def persist(service_result)
+ # Perform the @object.destroy etc. in the super-class
+ service_result = super(service_result)
+
+ delete_associated_file_links
+
+ # Just return the result from the superclass.
+ # There is currently no need to incorporate errors from
+ # delete_associated_file_links, because it doesn't fail (at the moment).
+ service_result
+ end
+
+ private
+
+ # Delete FileLinks with the same Storage as the ProjectStorage.
+ # Also, they are attached to WorkPackages via the Project.
+ def delete_associated_file_links
+ # work_packages is an ActiveRecord::Relation, not an array of objects!
+ work_packages = WorkPackage.where(project_id: model.project_id)
+ file_links = Storages::FileLink.where(storage_id: model.storage_id,
+ container: work_packages)
+ # use file_links.to_sql to check the SQL generated by the lines above.
+ # It uses a fast SQL "container_id in (select * from work_packages...)", so the
+ # delete_all below is executed using a single query.
+ # Reference: https://api.rubyonrails.org/classes/ActiveRecord/Relation.html
+ file_links.delete_all
+ end
+ end
+end
diff --git a/modules/storages/app/services/storages/project_storages/set_attributes_service.rb b/modules/storages/app/services/storages/project_storages/set_attributes_service.rb
new file mode 100644
index 00000000000..93556160ce0
--- /dev/null
+++ b/modules/storages/app/services/storages/project_storages/set_attributes_service.rb
@@ -0,0 +1,34 @@
+#-- 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.
+#++
+
+# ToDo: Where is this used?
+# It's called by CreateService when setting attributes
+module Storages::ProjectStorages
+ class SetAttributesService < ::BaseServices::SetAttributes
+ end
+end
diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.yml
index 6714f0f5df0..53620d5439f 100644
--- a/modules/storages/config/locales/en.yml
+++ b/modules/storages/config/locales/en.yml
@@ -2,7 +2,7 @@
en:
permission_view_file_links: "View file links"
permission_manage_file_links: "Manage file links"
- permission_manage_storage_in_project: "Manage file storage in project"
+ permission_manage_storages_in_project: "Manage file storages in project"
project_module_storages: "File storages"
activerecord:
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index 33409f99348..75453fc21a7 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -56,7 +56,7 @@ module OpenProject::Storages
permission :manage_file_links,
{},
dependencies: %i[view_file_links]
- permission :manage_storage_in_project,
+ permission :manage_storages_in_project,
{ 'storages/admin/projects_storages': %i[index new create destroy] },
dependencies: %i[]
end
diff --git a/modules/storages/spec/contracts/storages/project_storages/create_contract_spec.rb b/modules/storages/spec/contracts/storages/project_storages/create_contract_spec.rb
new file mode 100644
index 00000000000..e5361faddf4
--- /dev/null
+++ b/modules/storages/spec/contracts/storages/project_storages/create_contract_spec.rb
@@ -0,0 +1,50 @@
+#-- encoding: UTF-8
+
+#-- 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'
+require_relative 'shared_contract_examples'
+
+describe Storages::ProjectStorages::CreateContract do
+ include_context 'ModelContract shared context'
+
+ it_behaves_like 'ProjectStorages contract' do
+ # current_user, project, storage and other objects defined in the shared_contract_examples
+ # that includes all the stuff shared between create and update.
+ let(:project_storage) do
+ ::Storages::ProjectStorage.new(
+ creator: current_user,
+ project: project,
+ storage: storage
+ )
+ end
+ let(:contract) { described_class.new(project_storage, current_user) }
+ end
+end
diff --git a/modules/storages/spec/contracts/storages/project_storages/delete_contract_spec.rb b/modules/storages/spec/contracts/storages/project_storages/delete_contract_spec.rb
new file mode 100644
index 00000000000..d4111c50ec6
--- /dev/null
+++ b/modules/storages/spec/contracts/storages/project_storages/delete_contract_spec.rb
@@ -0,0 +1,53 @@
+#-- encoding: UTF-8
+
+#-- 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::ProjectStorages::DeleteContract do
+ include_context 'ModelContract shared context'
+
+ 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.
+ it_behaves_like 'contract is valid'
+
+ context 'without manage_storages_in_project permission for project' do
+ let(:role) { create(:existing_role) }
+
+ it_behaves_like 'contract is invalid'
+ end
+end
diff --git a/modules/storages/spec/contracts/storages/project_storages/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/project_storages/shared_contract_examples.rb
new file mode 100644
index 00000000000..85af8b0b6dd
--- /dev/null
+++ b/modules/storages/spec/contracts/storages/project_storages/shared_contract_examples.rb
@@ -0,0 +1,47 @@
+#-- encoding: UTF-8
+
+#-- 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'
+
+# Purpose: Common testing logic between create and update specs.
+# These two contracts share a lot, they are also both children of a BaseContract.
+shared_examples_for 'ProjectStorages contract' do
+ let(: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) }
+ let(:storage_creator) { current_user }
+
+ describe 'validations' do
+ context 'when all attributes are valid' do
+ it_behaves_like 'contract is valid'
+ end
+ end
+end
diff --git a/modules/storages/spec/contracts/storages/create_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/create_contract_spec.rb
similarity index 100%
rename from modules/storages/spec/contracts/storages/create_contract_spec.rb
rename to modules/storages/spec/contracts/storages/storages/create_contract_spec.rb
diff --git a/modules/storages/spec/contracts/storages/delete_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/delete_contract_spec.rb
similarity index 100%
rename from modules/storages/spec/contracts/storages/delete_contract_spec.rb
rename to modules/storages/spec/contracts/storages/storages/delete_contract_spec.rb
diff --git a/modules/storages/spec/contracts/storages/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb
similarity index 100%
rename from modules/storages/spec/contracts/storages/shared_contract_examples.rb
rename to modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb
diff --git a/modules/storages/spec/contracts/storages/update_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/update_contract_spec.rb
similarity index 100%
rename from modules/storages/spec/contracts/storages/update_contract_spec.rb
rename to modules/storages/spec/contracts/storages/storages/update_contract_spec.rb
diff --git a/modules/storages/spec/factories/project_storage_factory.rb b/modules/storages/spec/factories/project_storage_factory.rb
index e95a3759c67..3f1637f2490 100644
--- a/modules/storages/spec/factories/project_storage_factory.rb
+++ b/modules/storages/spec/factories/project_storage_factory.rb
@@ -30,5 +30,7 @@
FactoryBot.define do
factory :project_storage, class: '::Storages::ProjectStorage' do
creator factory: :user
+ storage factory: :storage
+ project factory: :project
end
end
diff --git a/modules/storages/spec/features/create_and_delete_project_storage_spec.rb b/modules/storages/spec/features/create_and_delete_project_storage_spec.rb
index d3bae09db9a..986c3455556 100644
--- a/modules/storages/spec/features/create_and_delete_project_storage_spec.rb
+++ b/modules/storages/spec/features/create_and_delete_project_storage_spec.rb
@@ -30,23 +30,30 @@
require_relative '../spec_helper'
-# Setup storages within a project.
+# Setup storages in Project -> Settings -> File Storages
# This tests assumes that a Storage has already been setup
# in the Admin section, tested by admin_storage_spec.rb.
describe 'Activation of storages in projects', type: :feature, js: true do
- let(:admin) { create(:admin) }
+ let(:user) { create(:user) }
+ # The first page is the Project -> Settings -> General page, so we need
+ # to provide the user with the edit_project permission in the role.
+ let(:role) do
+ create(:role,
+ permissions: %i[manage_storages_in_project
+ select_project_modules
+ edit_project])
+ end
let(:storage) { create(:storage, name: "Storage 1") }
let(:project) do
create(:project,
- name: 'Project 1',
- identifier: 'demo-project',
+ members: { user => role },
enabled_module_names: %i[storages work_package_tracking])
end
- let(:work_package) { create(:work_package, project: project) }
before do
storage
- login_as admin
+ project
+ login_as user
end
it 'adds and removes storages to projects' do
@@ -65,6 +72,12 @@ describe 'Activation of storages in projects', type: :feature, js: true do
expect(page).to have_text('Enable a file storage')
page.find('button[type=submit]').click
+ # ToDo: Error message here (in the browser):
+ # Project was attempted to be written but is not writable.
+ # Storage was attempted to be written but is not writable.
+ # Creator was attempted to be written but is not writable.
+ # Also appears when executing as :admin
+
# The list of enabled file storages should now contain Storage 1
expect(page).to have_text('File storages available in this project')
expect(page).to have_text('Storage 1')
diff --git a/modules/storages/spec/features/delete_project_storage_and_file_links_spec.rb b/modules/storages/spec/features/delete_project_storage_and_file_links_spec.rb
new file mode 100644
index 00000000000..84227bb0278
--- /dev/null
+++ b/modules/storages/spec/features/delete_project_storage_and_file_links_spec.rb
@@ -0,0 +1,81 @@
+#-- 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'
+
+# Test if the deletion of a ProjectStorage actually deletes related FileLink
+# objects.
+describe 'Delete ProjectStorage with FileLinks', type: :feature, js: true do
+ let(:user) { create(:user) }
+ let(:role) { create(:existing_role, permissions: [:manage_storages_in_project]) }
+ let(:project) do
+ create(:project,
+ name: 'Project 1',
+ identifier: 'demo-project',
+ members: { user => role },
+ enabled_module_names: %i[storages work_package_tracking])
+ end
+ let(:storage) { create(:storage, name: "Storage 1") }
+ let(:work_package) { create(:work_package, project: project) }
+ let(:project_storage) { create(:project_storage, storage: storage, project: project) }
+ let(:file_link) { create(:file_link, storage: storage, container: work_package) }
+ let(:second_file_link) { create(:file_link, container: work_package, storage: storage) }
+
+ before do
+ # The objects defined by let(...) above are lazy instantiated, so we need
+ # to "use" (just write their name) below to really create them.
+ project_storage
+ file_link
+ second_file_link
+ # Make sure our user has access to the GUI
+ login_as user
+ end
+
+ it 'deletes ProjectStorage with dependent FileLinks' do
+ # Go to Projects -> Settings -> File Storages
+ visit project_settings_projects_storages_path(project)
+
+ # The list of enabled file storages should now contain Storage 1
+ expect(page).to have_text('File storages available in this project')
+ expect(page).to have_text('Storage 1')
+
+ # Press Delete icon to remove the storage from the project
+ page.find('.icon.icon-delete').click
+ alert_text = page.driver.browser.switch_to.alert.text
+ expect(alert_text).to have_text 'Are you sure'
+ page.driver.browser.switch_to.alert.accept
+
+ # List of ProjectStorages empty again
+ expect(page).to have_current_path project_settings_projects_storages_path(project)
+ expect(page).to have_text('No storage setup, yet.')
+
+ # Also check in the database that ProjectStorage and dependent FileLinks are gone
+ expect(Storages::ProjectStorage.count).to be 0
+ expect(Storages::FileLink.count).to be 0
+ end
+end
diff --git a/modules/storages/spec/services/storages/project_storages/delete_service_spec.rb b/modules/storages/spec/services/storages/project_storages/delete_service_spec.rb
new file mode 100644
index 00000000000..a6552df0c6f
--- /dev/null
+++ b/modules/storages/spec/services/storages/project_storages/delete_service_spec.rb
@@ -0,0 +1,70 @@
+#-- 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::ProjectStorages::DeleteService, type: :model do
+ context 'with records written to DB' do
+ let(:user) { create(:user) }
+ let(:role) { create(:existing_role, permissions: [:manage_storages_in_project]) }
+ let(:project) { create(:project, members: { user => role }) }
+ let(:other_project) { create(:project) }
+ let(:project_storage) { create(:project_storage, project: project) }
+ let(:work_package) { create(:work_package, project: project) }
+ let(:other_work_package) { create(:work_package, project: other_project) }
+ let(:file_link) { create(:file_link, container: work_package, storage: project_storage.storage) }
+ let(:other_file_link) { create(:file_link, container: other_work_package, storage: project_storage.storage) }
+
+ it 'destroys the record' do
+ project_storage
+ described_class.new(model: project_storage, user: user).call
+
+ expect(::Storages::ProjectStorage.where(id: project_storage.id))
+ .not_to exist
+ end
+
+ it 'deletes all FileLinks that belong to containers of the related project' do
+ file_link
+ other_file_link
+
+ described_class.new(model: project_storage, user: user).call
+
+ expect(::Storages::FileLink.where(id: file_link.id))
+ .not_to exist
+ expect(::Storages::FileLink.where(id: other_file_link.id))
+ .to exist
+ end
+ end
+
+ # Includes many specs that are common for every DeleteService that inherits from ::BaseServices::Delete.
+ # Collected tests on DeleteContracts from last 15 years.
+ it_behaves_like 'BaseServices delete service' do
+ let(:factory) { :project_storage }
+ end
+end
diff --git a/modules/storages/spec/services/storages/create_service_spec.rb b/modules/storages/spec/services/storages/storages/create_service_spec.rb
similarity index 100%
rename from modules/storages/spec/services/storages/create_service_spec.rb
rename to modules/storages/spec/services/storages/storages/create_service_spec.rb
diff --git a/modules/storages/spec/services/storages/delete_service_spec.rb b/modules/storages/spec/services/storages/storages/delete_service_spec.rb
similarity index 100%
rename from modules/storages/spec/services/storages/delete_service_spec.rb
rename to modules/storages/spec/services/storages/storages/delete_service_spec.rb
diff --git a/modules/storages/spec/services/storages/set_attributes_service_spec.rb b/modules/storages/spec/services/storages/storages/set_attributes_service_spec.rb
similarity index 100%
rename from modules/storages/spec/services/storages/set_attributes_service_spec.rb
rename to modules/storages/spec/services/storages/storages/set_attributes_service_spec.rb
diff --git a/modules/storages/spec/services/storages/update_service_spec.rb b/modules/storages/spec/services/storages/storages/update_service_spec.rb
similarity index 100%
rename from modules/storages/spec/services/storages/update_service_spec.rb
rename to modules/storages/spec/services/storages/storages/update_service_spec.rb