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

WIP services and contracts for storages

Wieland Lindenthal1903af7ee4ea
diff --git a/modules/storages/app/cells/storages/storages_table_cell.rb b/modules/storages/app/cells/storages/storages_table_cell.rb
index 634b1648d9a..e640424c765 100644
--- a/modules/storages/app/cells/storages/storages_table_cell.rb
+++ b/modules/storages/app/cells/storages/storages_table_cell.rb
@@ -32,11 +32,11 @@ 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') }],
['host', { caption: I18n.t('storages.label_host') }],
['creator', { caption: I18n.t('storages.label_creator') }],
- ['created_at', { caption: Storages::Storage.human_attribute_name(:created_at) }]
+ ['created_at', { caption: ::Storages::Storage.human_attribute_name(:created_at) }]
]
end
end
diff --git a/modules/storages/app/contracts/storages/storages/base_contract.rb b/modules/storages/app/contracts/storages/storages/base_contract.rb
new file mode 100644
index 00000000000..205d5dd8588
--- /dev/null
+++ b/modules/storages/app/contracts/storages/storages/base_contract.rb
@@ -0,0 +1,79 @@
+#-- 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 'net/http'
+require 'uri'
+
+module Storages::Storages
+ class BaseContract < ::ModelContract
+ include ::Storages::Storages::Concerns::ManageStoragesGuarded
+ include ActiveModel::Validations
+
+ attribute :name
+ attribute :provider_type
+ attribute :creator, writable: false do
+ validate_creator_is_user
+ end
+
+ attribute :host
+ validates :host, length: { minimum: 1, maximum: 255 }, allow_nil: false
+ validates_url :host
+ # Check that a host actually is a storage server
+ validate :validate_host_reachable, if: -> { errors[:host].empty? }
+
+
+ def validate_creator_is_user
+ unless creator == user
+ errors.add(:creator, :invalid)
+ end
+ end
+
+ def validate_host_reachable
+ uri = URI.parse(File.join(host, '/ocs/v2.php/cloud/capabilities'))
+ request = Net::HTTP::Get.new(uri)
+ request["Ocs-Apirequest"] = "true"
+ request["Accept"] = "application/json"
+
+ req_options = {
+ use_ssl: uri.scheme == "https"
+ }
+
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
+ http.request(request)
+ end
+
+ errors.add(:host, :invalid) unless response.is_a? Net::HTTPSuccess
+ end
+
+ def validate_uri
+ unless host =~ /\A#{URI::regexp}\z/
+ errors.add(:host, :invalid)
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/contracts/storages/storages/concerns/manage_storages_guarded.rb b/modules/storages/app/contracts/storages/storages/concerns/manage_storages_guarded.rb
new file mode 100644
index 00000000000..614f4fb1860
--- /dev/null
+++ b/modules/storages/app/contracts/storages/storages/concerns/manage_storages_guarded.rb
@@ -0,0 +1,49 @@
+#-- 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::Storages
+ module Concerns
+ module ManageStoragesGuarded
+ extend ActiveSupport::Concern
+
+ included do
+ validate :validate_user_allowed_to_manage
+
+ private
+
+ def validate_user_allowed_to_manage
+ unless user.admin? && user.active?
+ errors.add :base, :error_unauthorized
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/contracts/storages/storages/create_contract.rb b/modules/storages/app/contracts/storages/storages/create_contract.rb
new file mode 100644
index 00000000000..ceaa8972ddd
--- /dev/null
+++ b/modules/storages/app/contracts/storages/storages/create_contract.rb
@@ -0,0 +1,35 @@
+#-- 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 'net/http'
+require 'uri'
+
+module Storages::Storages
+ class CreateContract < ::Storages::Storages::BaseContract
+ end
+end
diff --git a/modules/storages/app/contracts/storages/storages/delete_contract.rb b/modules/storages/app/contracts/storages/storages/delete_contract.rb
new file mode 100644
index 00000000000..db67b926a57
--- /dev/null
+++ b/modules/storages/app/contracts/storages/storages/delete_contract.rb
@@ -0,0 +1,35 @@
+#-- 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 Bim::Bcf
+ module Viewpoints
+ class DeleteContract < ::DeleteContract
+ include ::Storages::Storages::Concerns::ManageStoragesGuarded
+ end
+ end
+end
diff --git a/modules/storages/app/contracts/storages/storages/update_contract.rb b/modules/storages/app/contracts/storages/storages/update_contract.rb
new file mode 100644
index 00000000000..d24df162990
--- /dev/null
+++ b/modules/storages/app/contracts/storages/storages/update_contract.rb
@@ -0,0 +1,35 @@
+#-- 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 'net/http'
+require 'uri'
+
+module Storages::Storages
+ class UpdateContract < ::Storages::Storages::BaseContract
+ end
+end
diff --git a/modules/storages/app/controllers/storages/admin/storages_controller.rb b/modules/storages/app/controllers/storages/admin/storages_controller.rb
index 8d65c49d80b..32dc4beadc0 100644
--- a/modules/storages/app/controllers/storages/admin/storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/storages_controller.rb
@@ -49,17 +49,36 @@ class Storages::Admin::StoragesController < ApplicationController
end
def new
- @object = Storages::Storage.new(provider_type: 'nextcloud', name: I18n.t('storages.provider_types.nextcloud'))
+ @object = ::Storages::Storages::SetAttributesService
+ .new(user: current_user,
+ model: Storages::Storage.new,
+ contract_class: EmptyContract)
+ .call({ provider_type: 'nextcloud', name: I18n.t('storages.provider_types.nextcloud') })
+ .result
+
render 'storages/admin/new'
end
def create
- combined_params = permitted_storage_params
- .to_h
- .reverse_merge(creator_id: current_user.id)
-
- @object = Storages::Storage.create combined_params
- redirect_to storage_path(@object)
+ combined_params = permitted_storage_params.to_h.reverse_merge(creator_id: current_user.id)
+ service_result = Storages::Storages::CreateService.new(user: current_user).call(combined_params)
+ @object = service_result.result
+
+ if service_result.success?
+ respond_to do |format|
+ format.html do
+ flash[:notice] = I18n.t(:notice_successful_create)
+ redirect_to storage_path(@object)
+ end
+ end
+ else
+ @errors = service_result.errors
+ respond_to do |format|
+ format.html do
+ render 'storages/admin/new'
+ end
+ end
+ end
end
def edit
@@ -67,15 +86,39 @@ class Storages::Admin::StoragesController < ApplicationController
end
def update
- @object.update permitted_storage_params
-
- redirect_to storage_path(@object)
+ service_result = ::Storages::Storages::UpdateService
+ .new(user: current_user,
+ model: @object)
+ .call(permitted_storage_params)
+
+ if service_result.success?
+ respond_to do |format|
+ format.html do
+ flash[:notice] = I18n.t(:notice_successful_update)
+ redirect_to storage_path(@object)
+ end
+ end
+ else
+ respond_to do |format|
+ format.html do
+ render action: :edit
+ end
+ end
+ end
end
def destroy
- @object.destroy
+ Storages::Storages::DeleteService
+ .new(user: User.current, model: @object)
+ .call
- redirect_to storages_path
+ flash[:info] = I18n.t(:notice_successful_delete)
+
+ respond_to do |format|
+ format.html do
+ redirect_to storages_path
+ end
+ end
end
def default_breadcrumb
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index b40b749146a..74873fb938c 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -35,10 +35,9 @@ class Storages::Storage < ApplicationRecord
PROVIDER_TYPES = %w[nextcloud].freeze
validates_uniqueness_of :host
- validates_url :host
- validates_length_of :host, minimum: 1, maximum: 255
+ validates :name, length: { minimum: 1, maximum: 255 }, allow_nil: false
validates_uniqueness_of :name
- validates_length_of :name, minimum: 1, maximum: 255
+ validates :provider_type, inclusion: { in: ->(*) { PROVIDER_TYPES } }, allow_nil: false
def visible_to?(user)
user.allowed_to_globally?(:view_file_links)
diff --git a/modules/storages/app/services/storages/storages/create_service.rb b/modules/storages/app/services/storages/storages/create_service.rb
new file mode 100644
index 00000000000..1b40ba909b4
--- /dev/null
+++ b/modules/storages/app/services/storages/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::Storages
+ class CreateService < ::BaseServices::Create
+ end
+end
diff --git a/modules/storages/app/services/storages/storages/delete_service.rb b/modules/storages/app/services/storages/storages/delete_service.rb
new file mode 100644
index 00000000000..197fb733d49
--- /dev/null
+++ b/modules/storages/app/services/storages/storages/delete_service.rb
@@ -0,0 +1,34 @@
+#-- 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::Storages
+ class DeleteService < ::BaseServices::Delete
+ end
+end
diff --git a/modules/storages/app/services/storages/storages/set_attributes_service.rb b/modules/storages/app/services/storages/storages/set_attributes_service.rb
new file mode 100644
index 00000000000..6739655112f
--- /dev/null
+++ b/modules/storages/app/services/storages/storages/set_attributes_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::Storages
+ class SetAttributesService < ::BaseServices::SetAttributes
+ end
+end
diff --git a/modules/storages/app/services/storages/storages/update_service.rb b/modules/storages/app/services/storages/storages/update_service.rb
new file mode 100644
index 00000000000..2770466c2df
--- /dev/null
+++ b/modules/storages/app/services/storages/storages/update_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::Storages
+ class UpdateService < ::BaseServices::Update
+ end
+end
diff --git a/modules/storages/spec/contracts/storages/create_contract_spec.rb b/modules/storages/spec/contracts/storages/create_contract_spec.rb
new file mode 100644
index 00000000000..d5a9a8f7d83
--- /dev/null
+++ b/modules/storages/spec/contracts/storages/create_contract_spec.rb
@@ -0,0 +1,48 @@
+#-- 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::Storages::CreateContract do
+ include_context 'ModelContract shared context'
+
+ it_behaves_like 'storage contract' do
+ let(:current_user) { create(:admin) }
+ let(:storage) do
+ ::Storages::Storage.new(name: storage_name,
+ provider_type: storage_provider_type,
+ host: storage_host,
+ creator: current_user)
+ end
+ let(:contract) { described_class.new(storage, current_user) }
+ end
+end
diff --git a/modules/storages/spec/contracts/storages/delete_contract_spec.rb b/modules/storages/spec/contracts/storages/delete_contract_spec.rb
new file mode 100644
index 00000000000..f11699d8ce7
--- /dev/null
+++ b/modules/storages/spec/contracts/storages/delete_contract_spec.rb
@@ -0,0 +1,64 @@
+#-- 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 PlaceholderUsers::DeleteContract do
+ include_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) }
+
+ it_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
+ end
+end
diff --git a/modules/storages/spec/contracts/storages/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/shared_contract_examples.rb
new file mode 100644
index 00000000000..e57223ea013
--- /dev/null
+++ b/modules/storages/spec/contracts/storages/shared_contract_examples.rb
@@ -0,0 +1,149 @@
+#-- 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'
+
+shared_examples_for 'storage contract', webmock: true do
+ let(:storage_name) { 'Storage 1' }
+ let(:storage_provider_type) { 'nextcloud' }
+ let(:storage_host) { 'http://host1.example.com' }
+ let(:storage_creator) { create(:admin) }
+
+ let(:host_response_body) { '{"data": "that you want to return"}' }
+ let(:host_response_code) { '404' }
+ let(:host_response_message) { 'OK' }
+ let(:host_response_major_version) { 23 }
+ let(:host_response_body) do
+ %{
+ {
+ "ocs": {
+ "data": {
+ "version": {
+ "major": #{host_response_major_version},
+ "minor": 0,
+ "micro": 0,
+ "string": "23.0.0",
+ "edition": "",
+ "extendedSupport": false
+ }
+ }
+ }
+ }
+ }
+ end
+
+ before do
+ unless storage_host.nil?
+ stub_request(:get,
+ File.join(storage_host.sub("https://", ""),
+ '/ocs/v2.php/cloud/capabilities'))
+ end
+ # to_return(
+ # status: host_response_code,
+ # body: host_response_body
+ # )
+ end
+
+ it_behaves_like 'contract is valid for active admins and invalid for regular users'
+
+ describe 'validations' do
+ let(:current_user) { build_stubbed :admin }
+
+
+ context 'when all attributes are valid' do
+ it_behaves_like 'contract is valid'
+ end
+
+ context 'when name is invalid' do
+ context 'as it is too long' do
+ let(:storage_name) { 'X' * 257 }
+
+ it_behaves_like 'contract is invalid'
+ end
+
+ context 'as it is empty' do
+ let(:storage_name) { ''}
+
+ it_behaves_like 'contract is invalid'
+ end
+
+ context 'as it is not unique' do
+ before do
+ ::Storages::Storage.create(name: storage_name,
+ provider_type: storage_provider_type,
+ host: storage_host,
+ creator: storage_creator)
+ end
+
+ it_behaves_like 'contract is invalid'
+ end
+ end
+
+ context 'when provider_type is invalid' do
+ context 'as it is unknown' do
+ let(:storage_provider_type) { 'unkwown_provider_type' }
+
+ it_behaves_like 'contract is invalid'
+ end
+
+ context 'as it is empty' do
+ let(:storage_provider_type) { '' }
+
+ it_behaves_like 'contract is invalid'
+ end
+
+ context 'as it is nil' do
+ let(:storage_provider_type) { nil }
+
+ it_behaves_like 'contract is invalid'
+ end
+ end
+
+ context 'when host is invalid' do
+ context 'as host is not a URL' do
+ let(:storage_host) { '---invalid-url---' }
+
+ it_behaves_like 'contract is invalid'
+ end
+
+ context 'as host is nil' do
+ let(:storage_host) { nil }
+
+ it_behaves_like 'contract is invalid'
+ end
+ #
+ # context 'when provider_type is nextcloud' do
+ # context 'with host not reachable' do
+ #
+ # end
+ # end
+ end
+ end
+end
diff --git a/modules/storages/spec/contracts/storages/update_contract_spec.rb b/modules/storages/spec/contracts/storages/update_contract_spec.rb
new file mode 100644
index 00000000000..6000fabf59b
--- /dev/null
+++ b/modules/storages/spec/contracts/storages/update_contract_spec.rb
@@ -0,0 +1,41 @@
+#-- 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 PlaceholderUsers::UpdateContract do
+ include_context 'ModelContract shared context'
+
+ it_behaves_like 'placeholder user contract' do
+ let(:placeholder_user) { build_stubbed(:placeholder_user, name: placeholder_user_name) }
+ let(:contract) { described_class.new(placeholder_user, current_user) }
+ end
+end
diff --git a/modules/storages/spec/services/storages/create_service_spec.rb b/modules/storages/spec/services/storages/create_service_spec.rb
new file mode 100644
index 00000000000..6162871f77d
--- /dev/null
+++ b/modules/storages/spec/services/storages/create_service_spec.rb
@@ -0,0 +1,36 @@
+#-- 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 'services/base_services/behaves_like_create_service'
+
+describe ::Storages::Storages::CreateService, type: :model do
+ it_behaves_like 'BaseServices create service'
+end
diff --git a/modules/storages/spec/services/storages/delete_service_spec.rb b/modules/storages/spec/services/storages/delete_service_spec.rb
new file mode 100644
index 00000000000..95c8834f427
--- /dev/null
+++ b/modules/storages/spec/services/storages/delete_service_spec.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.
+#++
+
+require 'spec_helper'
+require 'services/base_services/behaves_like_delete_service'
+
+describe ::Storages::Storages::DeleteService, type: :model do
+ it_behaves_like 'BaseServices delete service'
+end
diff --git a/modules/storages/spec/services/storages/set_attributes_service_spec.rb b/modules/storages/spec/services/storages/set_attributes_service_spec.rb
new file mode 100644
index 00000000000..4e0c43bb189
--- /dev/null
+++ b/modules/storages/spec/services/storages/set_attributes_service_spec.rb
@@ -0,0 +1,114 @@
+#-- 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::Storages::SetAttributesService, type: :model do
+ let(:current_user) { build_stubbed(:admin) }
+
+ let(:contract_instance) do
+ contract = double('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) { double('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::Storages.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
+ is_expected
+ .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 }
+ let(:expect_time_instance_save) do
+ expect(model_instance)
+ .not_to receive(:save)
+ end
+
+ it 'returns failure' do
+ is_expected.not_to be_success
+ end
+
+ it "returns the contract's errors" do
+ expect(subject.errors)
+ .to eql(contract_errors)
+ end
+ end
+end
diff --git a/modules/storages/spec/services/storages/update_service_spec.rb b/modules/storages/spec/services/storages/update_service_spec.rb
new file mode 100644
index 00000000000..ba033283089
--- /dev/null
+++ b/modules/storages/spec/services/storages/update_service_spec.rb
@@ -0,0 +1,36 @@
+#-- 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 'services/base_services/behaves_like_update_service'
+
+describe PlaceholderUsers::UpdateService, type: :model do
+ it_behaves_like 'BaseServices update service'
+end