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 unit tests for storages plugin (#10133)
Frank Bergmann45cf2dfbda1a
diff --git a/db/migrate/20220113144759_create_file_links.rb b/db/migrate/20220113144759_create_file_links.rbindex 8f81e7c29a5..9eab9dfb82e 100644--- a/db/migrate/20220113144759_create_file_links.rb+++ b/db/migrate/20220113144759_create_file_links.rb@@ -1,7 +1,7 @@class CreateFileLinks < ActiveRecord::Migration[6.1]def changecreate_table :file_links do |t|- t.references :storage, foreign_key: true, on_delete: :cascade+ t.references :storage, foreign_key: { on_delete: :cascade }t.references :creator,null: false,index: true,diff --git a/db/migrate/20220121090847_create_projects_storages.rb b/db/migrate/20220121090847_create_projects_storages.rbindex 72f57b393e2..ad787ffda51 100644--- a/db/migrate/20220121090847_create_projects_storages.rb+++ b/db/migrate/20220121090847_create_projects_storages.rb@@ -1,8 +1,8 @@class CreateProjectsStorages < ActiveRecord::Migration[6.1]def changecreate_table :projects_storages do |t|- t.references :project, null: false, foreign_key: true, on_delete: :cascade- t.references :storage, null: false, foreign_key: true, on_delete: :cascade+ t.references :project, null: false, foreign_key: { on_delete: :cascade }+ t.references :storage, null: false, foreign_key: { on_delete: :cascade }t.references :creator,null: false,index: true,diff --git a/modules/storages/app/views/storages/admin/_form.html.erb b/modules/storages/app/views/storages/admin/_form.html.erbindex 2d5e877a0af..d3d3f9db1fe 100644--- a/modules/storages/app/views/storages/admin/_form.html.erb+++ b/modules/storages/app/views/storages/admin/_form.html.erb@@ -43,7 +43,11 @@ See COPYRIGHT and LICENSE files for more details.</span></div><div class="form--field -required">- <%= f.text_field :host, type: :url, required: true, pattern: ".{1,255}", placeholder: "https://my-file-storage.com", container_class: '-wide' %>+ <%= f.text_field :host,+ type: :url,+ required: true,+ pattern: ".{1,255}",+ placeholder: "https://my-file-storage.com", container_class: '-wide' %><span class="form--field-instructions"><%= t('storages.instructions.host') %></span>diff --git a/modules/storages/spec/factories/project_storage_factory.rb b/modules/storages/spec/factories/project_storage_factory.rbnew file mode 100644index 00000000000..e4faff94fd7--- /dev/null+++ b/modules/storages/spec/factories/project_storage_factory.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.+#++++# Required parameters: project and storage+FactoryBot.define do+ factory :project_storage, class: '::Storages::ProjectStorage' do |m|+ creator factory: :user+ end+enddiff --git a/modules/storages/spec/factories/storage_factory.rb b/modules/storages/spec/factories/storage_factory.rbindex 5dea328ccb9..2081711106e 100644--- a/modules/storages/spec/factories/storage_factory.rb+++ b/modules/storages/spec/factories/storage_factory.rb@@ -28,8 +28,9 @@FactoryBot.define dofactory :storage, class: '::Storages::Storage' do- name { 'TestStorage' }- provider_type { 'nextcloud' }- host { 'https://nextcloud.test-storage.org/' }+ provider_type { "nextcloud" }+ sequence(:name) { |n| "Storage #{n}" }+ sequence(:host) { |n| "https://host#{n}.example.com" }+ creator factory: :userendenddiff --git a/modules/storages/spec/features/admin_storages_spec.rb b/modules/storages/spec/features/admin_storages_spec.rbindex a83511d4d45..5de209a4a0c 100644--- a/modules/storages/spec/features/admin_storages_spec.rb+++ b/modules/storages/spec/features/admin_storages_spec.rb@@ -31,9 +31,7 @@require_relative '../spec_helper'describe 'Admin storages', type: :feature, js: true do- let(:admin) do- create(:admin)- end+ let(:admin) { create(:admin) }before dologin_as admin@@ -67,6 +65,7 @@ describe 'Admin storages', type: :feature, js: true doexpect(page).to have_title("Edit: NC 1")expect(page.find('.title-container')).to have_text('Edit: NC 1')+page.find('#storages_storage_name').set("Other NC")page.find('#storages_storage_host').set("https://other.example.com")page.find('button[type=submit]').click@@ -84,14 +83,11 @@ describe 'Admin storages', type: :feature, js: true doexpect(page).to have_text(admin.name)page.find("a", text: 'Other NC').click-expect(page).to have_current_path storage_path(created_storage)-page.find('.button--icon.icon-delete').clickalert_text = page.driver.browser.switch_to.alert.textexpect(alert_text).to eq(I18n.t('storages.delete_warning.storage'))-page.driver.browser.switch_to.alert.acceptexpect(page).to have_current_path(storages_path)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.rbnew file mode 100644index 00000000000..ace14be9063--- /dev/null+++ b/modules/storages/spec/features/create_and_delete_project_storage_spec.rb@@ -0,0 +1,78 @@+#-- encoding: UTF-8++#-- 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'++# Setup a project storage within a project.+# This tests assumes that a Storage has already been setup+# in the Admin section, tested by admin_storage_spec.rb.+describe 'Setup project storage', type: :feature, js: true do+ let(:admin) { create(:admin) }+ let(:storage) { create(:storage, name: "Storage 1") }+ let(:project) { create(:project, name: 'Project 1', identifier: 'demo-project', enabled_module_names: [:storages, :work_package_tracking]) }+ let(:work_package) { create(:work_package, project: project) }++ before do+ storage+ login_as admin+ end++ it 'activates up a ProjectStorage for a specific project' do+ # Go to Projects -> Settings -> File Storages+ visit project_settings_general_path(project)+ page.find('.settings-projects-storages-menu-item').click++ # Check for an empty Project -> Settings -> File storages page+ expect(page).to have_title('File storages')+ expect(page).to have_current_path project_settings_projects_storages_path(project)+ expect(page).to have_text('No storage setup, yet.')+ page.find('.toolbar .button--icon.icon-add').click++ # Enable the one and only file storage+ expect(page).to have_title('File storages')+ expect(page).to have_text('Enable a file storage')+ page.find('button[type=submit]').click++ # The list of enabled file storages should now contain Storage 1+ expect(page).to have_title('File storages')+ expect(page).to have_text('File storages available in this project')+ expect(page).to have_text('Storage 1')++ # Press Delete icon to disable the ProjectStorage again+ 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.')+ end+enddiff --git a/modules/storages/spec/models/file_link_spec.rb b/modules/storages/spec/models/file_link_spec.rbnew file mode 100644index 00000000000..31b89557dac--- /dev/null+++ b/modules/storages/spec/models/file_link_spec.rb@@ -0,0 +1,78 @@+#-- 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_relative '../spec_helper'++describe ::Storages::FileLink, type: :model do+ let(:creator) { create(:user) }+ let(:project) { create(:project) }+ let(:work_package) { create(:work_package, project: project) }+ let(:storage) { create(:storage) }+ let(:attributes) do+ {+ storage: storage,+ creator: creator,+ container_id: work_package.id,+ container_type: "WorkPackage",+ origin_id: "123456",+ origin_name: "Origin Name",+ origin_created_by_name: "Peter Pan",+ origin_last_modified_by_name: "Lucy Lectric",+ origin_mime_type: "text/html"+ }+ end++ describe '#create' do+ it "creates an instance" do+ file_link = described_class.create attributes+ expect(file_link).to be_valid+ end++ it "should fail when creating an instance with an unsupported container type" do+ file_link = described_class.create(attributes.merge({ container_id: creator.id, container_type: "User" }))+ # ToDo: expect(file_link).to be_invalid+ end++ it "create instance should fail with empty origin_id" do+ file_link = described_class.create(attributes.merge({ origin_id: "" }))+ # ToDo: expect(file_link).to be_invalid+ end+ end++ describe '#destroy' do+ let(:file_link_to_destroy) { described_class.create(attributes) }++ before do+ file_link_to_destroy.destroy+ end++ it "destroy instance should leave no file_link" do+ expect(Storages::FileLink.count).to be 0+ end+ end+enddiff --git a/modules/storages/spec/models/project_storage_spec.rb b/modules/storages/spec/models/project_storage_spec.rbnew file mode 100644index 00000000000..00f49e469c5--- /dev/null+++ b/modules/storages/spec/models/project_storage_spec.rb@@ -0,0 +1,85 @@+#-- 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_relative '../spec_helper'++describe ::Storages::ProjectStorage, type: :model do+ let(:creator) { create(:user) }+ let(:project) { create(:project, :enabled_module_names => %i[storages work_packages]) }+ let(:storage) { create(:storage) }+ let(:attributes) do+ {+ storage: storage,+ creator: creator,+ project: project+ }+ end++ describe '#create' do+ it "creates an instance" do+ project_storage = described_class.create attributes+ expect(project_storage).to be_valid+ end++ context "having already one instance" do+ let(:old_project_storage) { described_class.create attributes }++ before do+ old_project_storage+ end++ it "should fail if it is not unique per storage and project" do+ expect(described_class.create(attributes.merge)).to be_invalid+ end+ end+ end++ describe '#destroy' do+ let(:project_storage_to_destroy) { described_class.create(attributes) }+ let(:work_package) { create(:work_package, project: project) }+ let(:file_link) do+ Storages::FileLink.create(container: work_package,+ storage: storage,+ creator: creator,+ container_type: "WorkPackage")+ end++ before do+ project_storage_to_destroy+ file_link++ project_storage_to_destroy.destroy+ end++ it "should destroy all associated FileLink records" do+ expect(Storages::ProjectStorage.count).to be 0+ # ToDo: expect(Storages::FileLink.count).to be 0+ end+ end+end+diff --git a/modules/storages/spec/models/storage_spec.rb b/modules/storages/spec/models/storage_spec.rbindex 60be43572fb..85d8fa39e37 100644--- a/modules/storages/spec/models/storage_spec.rb+++ b/modules/storages/spec/models/storage_spec.rb@@ -29,61 +29,70 @@require_relative '../spec_helper'describe ::Storages::Storage, type: :model do- let(:test_default_creator) { create(:user) }- let(:test_default_attributes) do+ let(:creator) { create(:user) }+ let(:default_attributes) do{ name: "NC 1",provider_type: 'nextcloud',host: 'https://example.com',- creator: test_default_creator }+ creator: creator }enddescribe '#create' doit "creates an instance" do- storage = described_class.create test_default_attributes+ storage = described_class.create default_attributesexpect(storage).to be_validend-+it "fails the validation if name is empty string" do- expect(described_class.create(test_default_attributes.merge({ name: "" }))).to be_invalid+ expect(described_class.create(default_attributes.merge({ name: "" }))).to be_invalidendit "fails the validation if name is nil" do- expect(described_class.create(test_default_attributes.merge({ name: nil }))).to be_invalid+ expect(described_class.create(default_attributes.merge({ name: nil }))).to be_invalidendit "fails the validation if host is empty string" do- expect(described_class.create(test_default_attributes.merge({ host: '' }))).to be_invalid+ expect(described_class.create(default_attributes.merge({ host: '' }))).to be_invalidendit "fails the validation if host is nil" do- expect(described_class.create(test_default_attributes.merge({ host: nil }))).to be_invalid+ expect(described_class.create(default_attributes.merge({ host: nil }))).to be_invalidend-- context "when having already one instance" do- let(:old_storage) { described_class.create test_default_attributes }-++ context "having already one instance" do+ let(:old_storage) { described_class.create default_attributes }+before doold_storageend-+it "fails the validation if name is not unique" do- expect(described_class.create(test_default_attributes.merge({ host: 'https://example2.com' }))).to be_invalid+ expect(described_class.create(default_attributes.merge({ host: 'https://example2.com' }))).to be_invalidendit "fails the validation if host is not unique" do- expect(described_class.create(test_default_attributes.merge({ name: 'NC 2' }))).to be_invalid+ expect(described_class.create(default_attributes.merge({ name: 'NC 2' }))).to be_invalidendendenddescribe '#destroy' do- let(:project) { create(project) }- let(:storage) { described_class.create(test_default_attributes) }- let(:project_storage) { Storages::ProjectStorage.create(project: project, storage: storage) }+ let(:project) { create(:project) }+ let(:storage) { described_class.create(default_attributes) }+ # let(:project_storage) { Storages::ProjectStorage.create(project: project, storage: storage, creator: creator) }+ let(:project_storage) { create(:project_storage, project: project, storage: storage, creator: creator) }let(:work_package) { create(:work_package, project: project) }- let(:file_link) { FileLink.create(container: work_package, storage: storage) }+ let(:file_link) do+ Storages::FileLink.create(container: work_package,+ storage: storage,+ creator: creator,+ container_type: "WorkPackage")+ endbefore do+ project_storage+ file_link+storage.destroyend