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 specs for instantiation

Oliver Günther0f5348b34194
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 7de246a8b40..c183b8ecc8e 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -2006,7 +2006,9 @@ en:
warning_two: Existing links to the project will no longer work.
title: Change the project's identifier
template:
- copying: 'Your project is being created from the selected template project.'
+ copying: >
+ Your project is being created from the selected template project.
+ You will be notified by mail as soon as the project is available.
use_template: 'Use template'
make_template: 'Set as template'
archive:
diff --git a/spec/contracts/projects/instantiate_template_contract_spec.rb b/spec/contracts/projects/instantiate_template_contract_spec.rb
new file mode 100644
index 00000000000..d24f88086a1
--- /dev/null
+++ b/spec/contracts/projects/instantiate_template_contract_spec.rb
@@ -0,0 +1,81 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+require_relative './shared_contract_examples'
+
+describe Projects::InstantiateTemplateContract do
+ let(:user) { FactoryBot.build_stubbed :user }
+ let(:project) { Project.new name: 'Foo Bar', identifier: 'foo' }
+ let(:template) { FactoryBot.build_stubbed :project }
+ let(:options) { { template_project: template } }
+
+ subject { described_class.new(project, user, options: options) }
+
+ before do
+ allow(user)
+ .to(receive(:allowed_to_globally?))
+ .with(:add_project)
+ .and_return(allowed_to_add)
+
+ allow(user)
+ .to(receive(:allowed_to?))
+ .with(:copy_projects, template)
+ .and_return(allowed_to_copy)
+ end
+
+ context 'when user may copy template' do
+ let(:allowed_to_copy) { true }
+ let(:allowed_to_add) { true }
+
+ it 'validates the contract' do
+ expect(subject.validate).to eq true
+ expect(subject.errors).to be_empty
+ end
+
+ context 'but may not add projects' do
+ let(:allowed_to_copy) { true }
+ let(:allowed_to_add) { false }
+
+ it 'fails the parent contract' do
+ expect(subject.validate).to eq false
+ expect(subject.errors[:base]).to include 'may not be accessed.'
+ end
+ end
+ end
+
+ context 'when user may not copy template' do
+ let(:allowed_to_copy) { false }
+ let(:allowed_to_add) { true }
+
+ it 'fails the contract' do
+ expect(subject.validate).to eq false
+ expect(subject.errors[:base]).to include 'may not be accessed.'
+ end
+ end
+end
diff --git a/spec/features/projects/instantiate_template_spec.rb b/spec/features/projects/instantiate_template_spec.rb
new file mode 100644
index 00000000000..4609c9a2c62
--- /dev/null
+++ b/spec/features/projects/instantiate_template_spec.rb
@@ -0,0 +1,81 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+describe 'Projects', 'Instantiate template', type: :feature, js: true do
+ let!(:template) {
+ FactoryBot.create(:template_project, name: 'My template', enabled_module_names: %w[wiki work_package_tracking])
+ }
+ let!(:work_package) { FactoryBot.create :work_package, project: template }
+ let!(:wiki_page) { FactoryBot.create(:wiki_page_with_content, wiki: template.wiki) }
+
+ let!(:role) { FactoryBot.create(:role, permissions: %i[view_project view_work_packages copy_projects add_project]) }
+ let!(:current_user) { FactoryBot.create(:user, member_in_project: template, member_through_role: role) }
+
+ before do
+ login_as current_user
+ end
+
+ it 'can instantiate the project with the copy permission' do
+ visit new_project_path
+
+ fill_in 'project[name]', with: 'Foo bar'
+ select 'My template', from: 'Use template'
+ click_on 'Advanced settings'
+ fill_in 'project[identifier]', with: 'foo'
+ sleep 1
+ click_on 'Create'
+
+ expect(page).to have_content I18n.t('project.template.copying')
+ expect(page).to have_current_path home_path
+
+ # Email notification should be sent
+ perform_enqueued_jobs
+
+ mail = ActionMailer::Base
+ .deliveries
+ .detect { |mail| mail.subject == 'Created project Foo bar'}
+
+ expect(mail).not_to be_nil
+
+ project = Project.find_by identifier: 'foo'
+ expect(project.name).to eq 'Foo bar'
+ expect(project.users.first).to eq current_user
+ expect(project.enabled_module_names.sort).to eq(template.enabled_module_names.sort)
+
+ wp_source = template.work_packages.first.attributes.except(*%w[id author_id project_id updated_at created_at])
+ wp_target = project.work_packages.first.attributes.except(*%w[id author_id project_id updated_at created_at])
+ expect(wp_source).to eq(wp_target)
+
+ wiki_source = template.wiki.pages.first
+ wiki_target = project.wiki.pages.first
+ expect(wiki_source.title).to eq(wiki_target.title)
+ expect(wiki_source.content.text).to eq(wiki_target.content.text)
+ end
+end