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
Fixed storage contract specs
Andreas Pfohlfa4b6fe162d4
diff --git a/modules/storages/spec/contracts/storages/storages/base_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/nextcloud_contract_spec.rbsimilarity index 52%rename from modules/storages/spec/contracts/storages/storages/base_contract_spec.rbrename to modules/storages/spec/contracts/storages/storages/nextcloud_contract_spec.rbindex f767d549510..bf1bf944686 100644--- a/modules/storages/spec/contracts/storages/storages/base_contract_spec.rb+++ b/modules/storages/spec/contracts/storages/storages/nextcloud_contract_spec.rb@@ -31,22 +31,25 @@require 'spec_helper'require_module_spec_helper-RSpec.describe Storages::Storages::BaseContract, :storage_server_helpers, webmock: true do+RSpec.describe Storages::Storages::NextcloudContract, :storage_server_helpers, webmock: true dolet(:current_user) { create(:admin) }let(:storage_host) { 'https://host1.example.com' }let(:storage) { build(:nextcloud_storage, host: storage_host) }- let(:contract) { described_class.new(storage, current_user) }++ # As the NextcloudContract is selected by the BaseContract to make writable attributes available,+ # the BaseContract needs to be instantiated here.+ subject { Storages::Storages::BaseContract.new(storage, current_user) }it 'checks the storage url only when changed' docapabilities_request = mock_server_capabilities_response(storage_host)host_request = mock_server_config_check_response(storage_host)- contract.valid?+ subject.validateexpect(capabilities_request).to have_been_made.onceexpect(host_request).to have_been_made.onceWebMock.reset_executed_requests!storage.save- contract.valid?+ subject.validateexpect(capabilities_request).not_to have_been_madeexpect(host_request).not_to have_been_madeend@@ -62,63 +65,58 @@ RSpec.describe Storages::Storages::BaseContract, :storage_server_helpers, webmocit 'passes validation' docredentials_request = mock_nextcloud_application_credentials_validation(storage.host)- contract = described_class.new(storage, current_user)- expect(contract).to be_valid+ expect(subject).to be_validexpect(credentials_request).to have_been_made.onceend- end- context 'with invalid credentials' do- let(:storage) { build(:nextcloud_storage, :as_automatically_managed) }+ context 'with invalid credentials' do+ let(:storage) { build(:nextcloud_storage, :as_automatically_managed) }- it 'fails validation' do- credentials_request = mock_nextcloud_application_credentials_validation(storage.host, response_code: 401)- contract = described_class.new(storage, current_user)+ it 'fails validation' do+ credentials_request = mock_nextcloud_application_credentials_validation(storage.host, response_code: 401)- expect(contract).not_to be_valid- expect(contract.errors.to_hash).to eq({ password: ["is not valid."] })+ expect(subject).not_to be_valid+ expect(subject.errors.to_hash).to eq({ password: ["is not valid."] })- expect(credentials_request).to have_been_made.once+ expect(credentials_request).to have_been_made.once+ endend- end- context 'with unknown error' do- let(:storage) { build(:nextcloud_storage, :as_automatically_managed) }+ context 'with unknown error' do+ let(:storage) { build(:nextcloud_storage, :as_automatically_managed) }- it 'fails validation' do- credentials_request = mock_nextcloud_application_credentials_validation(storage.host, response_code: 500)- contract = described_class.new(storage, current_user)+ it 'fails validation' do+ credentials_request = mock_nextcloud_application_credentials_validation(storage.host, response_code: 500)- expect(contract).not_to be_valid- expect(contract.errors.to_hash)- .to eq({ password: ["could not be validated. Please check your storage connection and try again."] })+ expect(subject).not_to be_valid+ expect(subject.errors.to_hash)+ .to eq({ password: ["could not be validated. Please check your storage connection and try again."] })- expect(credentials_request).to have_been_made.once+ expect(credentials_request).to have_been_made.once+ endend- end- context 'when the storage is not automatically managed' do- let(:storage) { build(:nextcloud_storage, :as_not_automatically_managed) }+ context 'when the storage is not automatically managed' do+ let(:storage) { build(:nextcloud_storage, :as_not_automatically_managed) }- it 'skips credentials validation' do- credentials_request = mock_nextcloud_application_credentials_validation(storage.host)- contract = described_class.new(storage, current_user)+ it 'skips credentials validation' do+ credentials_request = mock_nextcloud_application_credentials_validation(storage.host)- expect(contract).to be_valid- expect(credentials_request).not_to have_been_made+ expect(subject).to be_valid+ expect(credentials_request).not_to have_been_made+ endend- end- context 'when the storage host has a subpath' do- let(:storage) { build(:nextcloud_storage, :as_automatically_managed, host: 'https://host1.example.com/api') }+ context 'when the storage host has a subpath' do+ let(:storage) { build(:nextcloud_storage, :as_automatically_managed, host: 'https://host1.example.com/api') }- it 'passes validation' do- credentials_request = mock_nextcloud_application_credentials_validation(storage.host)- contract = described_class.new(storage, current_user)+ it 'passes validation' do+ credentials_request = mock_nextcloud_application_credentials_validation(storage.host)- expect(contract).to be_valid- expect(credentials_request).to have_been_made.once+ expect(subject).to be_valid+ expect(credentials_request).to have_been_made.once+ endendendenddiff --git a/modules/storages/spec/contracts/storages/storages/create_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/nextcloud_create_contract_spec.rbsimilarity index 96%rename from modules/storages/spec/contracts/storages/storages/create_contract_spec.rbrename to modules/storages/spec/contracts/storages/storages/nextcloud_create_contract_spec.rbindex 72a7c5f5ec2..5b6dffd79ac 100644--- a/modules/storages/spec/contracts/storages/storages/create_contract_spec.rb+++ b/modules/storages/spec/contracts/storages/storages/nextcloud_create_contract_spec.rb@@ -34,8 +34,7 @@ require_relative 'shared_contract_examples'RSpec.describe Storages::Storages::CreateContract doinclude_context 'ModelContract shared context'- it_behaves_like 'storage contract' do- let(:current_user) { create(:admin) }+ it_behaves_like 'nextcloud storage contract' dolet(:storage) dobuild_stubbed(:nextcloud_storage,creator: storage_creator,diff --git a/modules/storages/spec/contracts/storages/storages/delete_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/nextcloud_delete_contract_spec.rbsimilarity index 97%rename from modules/storages/spec/contracts/storages/storages/delete_contract_spec.rbrename to modules/storages/spec/contracts/storages/storages/nextcloud_delete_contract_spec.rbindex f68a3565088..1dfc1e5a6ff 100644--- a/modules/storages/spec/contracts/storages/storages/delete_contract_spec.rb+++ b/modules/storages/spec/contracts/storages/storages/nextcloud_delete_contract_spec.rb@@ -33,7 +33,7 @@ require 'contracts/shared/model_contract_shared_context'RSpec.describe Storages::Storages::DeleteContract doinclude_context 'ModelContract shared context'- let(:storage) { create(:storage) }+ let(:storage) { create(:nextcloud_storage) }let(:contract) { described_class.new(storage, current_user) }# Generic checks that the contract is valid for valid admin, but invalid otherwisediff --git a/modules/storages/spec/contracts/storages/storages/update_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/nextcloud_update_contract_spec.rbsimilarity index 97%rename from modules/storages/spec/contracts/storages/storages/update_contract_spec.rbrename to modules/storages/spec/contracts/storages/storages/nextcloud_update_contract_spec.rbindex 337343d76b2..e3ab4abddcb 100644--- a/modules/storages/spec/contracts/storages/storages/update_contract_spec.rb+++ b/modules/storages/spec/contracts/storages/storages/nextcloud_update_contract_spec.rb@@ -34,7 +34,7 @@ require_relative 'shared_contract_examples'RSpec.describe Storages::Storages::UpdateContract doinclude_context 'ModelContract shared context'- it_behaves_like 'storage contract' do+ it_behaves_like 'nextcloud storage contract' dolet(:storage) dobuild_stubbed(:nextcloud_storage,creator: storage_creator,diff --git a/modules/storages/spec/contracts/storages/storages/one_drive_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/one_drive_contract_spec.rbnew file mode 100644index 00000000000..254d0e33685--- /dev/null+++ b/modules/storages/spec/contracts/storages/storages/one_drive_contract_spec.rb@@ -0,0 +1,51 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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_module_spec_helper++RSpec.describe Storages::Storages::NextcloudContract, :storage_server_helpers, webmock: true do+ let(:current_user) { create(:admin) }+ let(:storage) { build(:one_drive_storage) }++ # As the OneDriveContract is selected by the BaseContract to make writable attributes available,+ # the BaseContract needs to be instantiated here.+ subject { Storages::Storages::BaseContract.new(storage, current_user) }++ describe 'when a host is set' do+ before do+ storage.host = "https://exmaple.com/"+ end++ it 'must be invalid' do+ expect(subject).not_to be_valid+ end+ end+enddiff --git a/modules/storages/spec/contracts/storages/storages/one_drive_create_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/one_drive_create_contract_spec.rbnew file mode 100644index 00000000000..fa74ab9a45f--- /dev/null+++ b/modules/storages/spec/contracts/storages/storages/one_drive_create_contract_spec.rb@@ -0,0 +1,52 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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_module_spec_helper+require 'contracts/shared/model_contract_shared_context'+require_relative 'shared_contract_examples'++RSpec.describe Storages::Storages::CreateContract do+ include_context 'ModelContract shared context'++ let(:storage) do+ build_stubbed(:one_drive_storage,+ creator: storage_creator,+ name: storage_name,+ provider_type: storage_provider_type)+ end+ let(:contract) { described_class.new(storage, current_user) }++ it_behaves_like 'onedrive storage contract' do+ context 'when creator is not the current user' do+ let(:storage_creator) { build_stubbed(:user) }++ include_examples 'contract is invalid', creator: :invalid+ end+ end+enddiff --git a/modules/storages/spec/contracts/storages/storages/one_drive_delete_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/one_drive_delete_contract_spec.rbnew file mode 100644index 00000000000..428bda2f052--- /dev/null+++ b/modules/storages/spec/contracts/storages/storages/one_drive_delete_contract_spec.rb@@ -0,0 +1,41 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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_module_spec_helper+require 'contracts/shared/model_contract_shared_context'++RSpec.describe Storages::Storages::DeleteContract do+ include_context 'ModelContract shared context'++ let(:storage) { create(:one_drive_storage) }+ let(:contract) { described_class.new(storage, current_user) }++ # 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/storages/one_drive_update_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/one_drive_update_contract_spec.rbnew file mode 100644index 00000000000..b1a0ca9a04e--- /dev/null+++ b/modules/storages/spec/contracts/storages/storages/one_drive_update_contract_spec.rb@@ -0,0 +1,52 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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_module_spec_helper+require 'contracts/shared/model_contract_shared_context'+require_relative 'shared_contract_examples'++RSpec.describe Storages::Storages::UpdateContract do+ include_context 'ModelContract shared context'++ it_behaves_like 'onedrive storage contract' do+ let(:storage) do+ build_stubbed(:one_drive_storage,+ creator: storage_creator,+ name: storage_name,+ provider_type: storage_provider_type)+ end+ let(:contract) { described_class.new(storage, current_user) }++ context 'when current user is not the initial storage creator' do+ let(:storage_creator) { build_stubbed(:user) }++ include_examples 'contract is valid'+ end+ end+enddiff --git a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rbindex c993d46d030..93635807ebd 100644--- a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb+++ b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb@@ -31,24 +31,7 @@require 'spec_helper'require_module_spec_helper-RSpec.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) { Storages::Storage::PROVIDER_TYPE_NEXTCLOUD }- let(:storage_host) { 'https://host1.example.com' }- let(:storage_creator) { current_user }-- before do- if storage_host.present?- mock_server_capabilities_response(storage_host)- mock_server_config_check_response(storage_host)- mock_nextcloud_application_credentials_validation(storage_host)- end- end-- it_behaves_like 'contract is valid for active admins and invalid for regular users'-+RSpec.shared_examples_for 'storage contract' dodescribe 'validations' docontext 'when all attributes are valid' doinclude_examples 'contract is valid'@@ -87,7 +70,41 @@ RSpec.shared_examples_for 'storage contract', :storage_server_helpers, webmock:include_examples 'contract is invalid', provider_type: :inclusionendend+ end+end++RSpec.shared_examples_for 'onedrive storage contract' do+ let(:current_user) { create(:admin) }+ let(:storage_name) { 'Storage 1' }+ let(:storage_provider_type) { Storages::Storage::PROVIDER_TYPE_ONE_DRIVE }+ let(:storage_creator) { current_user }++ it_behaves_like 'contract is valid for active admins and invalid for regular users'++ it_behaves_like 'storage contract'+end++RSpec.shared_examples_for 'nextcloud 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) { Storages::Storage::PROVIDER_TYPE_NEXTCLOUD }+ let(:storage_host) { 'https://host1.example.com' }+ let(:storage_creator) { current_user }++ before do+ if storage_host.present?+ mock_server_capabilities_response(storage_host)+ mock_server_config_check_response(storage_host)+ mock_nextcloud_application_credentials_validation(storage_host)+ end+ end++ it_behaves_like 'contract is valid for active admins and invalid for regular users'+ it_behaves_like 'storage contract'++ describe 'validations' docontext 'when host is invalid' docontext 'as host is not a URL' dolet(:storage_host) { '---invalid-url---' }diff --git a/modules/storages/spec/factories/storage_factory.rb b/modules/storages/spec/factories/storage_factory.rbindex 72c93ac6991..c32255198a1 100644--- a/modules/storages/spec/factories/storage_factory.rb+++ b/modules/storages/spec/factories/storage_factory.rb@@ -28,13 +28,12 @@FactoryBot.define dofactory :storage, class: '::Storages::Storage' do- provider_type { Storages::Storage::PROVIDER_TYPE_NEXTCLOUD }sequence(:name) { |n| "Storage #{n}" }- sequence(:host) { |n| "https://host#{n}.example.com" }creator factory: :userfactory :nextcloud_storage, class: '::Storages::NextcloudStorage' doprovider_type { Storages::Storage::PROVIDER_TYPE_NEXTCLOUD }+ sequence(:host) { |n| "https://host#{n}.example.com" }trait :as_automatically_managed doautomatically_managed { true }@@ -46,5 +45,9 @@ FactoryBot.define doautomatically_managed { false }endend++ factory :one_drive_storage, class: '::Storages::OneDriveStorage' do+ provider_type { Storages::Storage::PROVIDER_TYPE_ONE_DRIVE }+ endendend