OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/spec/workers/copy_project_job_spec.rb

275 lines
9.3 KiB

#-- 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 docs/COPYRIGHT.rdoc for more details.
#++
require 'spec_helper'
describe CopyProjectJob, type: :model do
let(:project) { FactoryBot.create(:project, public: false) }
let(:user) { FactoryBot.create(:user) }
let(:role) { FactoryBot.create(:role, permissions: [:copy_projects]) }
let(:params) { { name: 'Copy', identifier: 'copy' } }
let(:maildouble) { double('Mail::Message', deliver: true) }
before do
allow(maildouble).to receive(:deliver_now).and_return nil
end
describe 'copy localizes error message' do
let(:user_de) { FactoryBot.create(:admin, language: :de) }
let(:source_project) { FactoryBot.create(:project) }
let(:target_project) { FactoryBot.create(:project) }
let(:copy_job) do
CopyProjectJob.new
end
it 'sets locale correctly' do
expect(copy_job)
.to receive(:create_project_copy)
.and_wrap_original do |m, *args, &block|
expect(I18n.locale).to eq(:de)
m.call(*args, &block)
end
copy_job.perform user_id: user_de.id,
source_project_id: source_project.id,
target_project_params: {},
associations_to_copy: []
end
end
describe 'copy project succeeds with errors' do
let(:admin) { FactoryBot.create(:admin) }
let(:source_project) { FactoryBot.create(:project, types: [type]) }
let!(:work_package) { FactoryBot.create(:work_package, project: source_project, type: type) }
let(:type) { FactoryBot.create(:type_bug) }
let(:custom_field) do
FactoryBot.create(:work_package_custom_field,
name: 'required_field',
field_format: 'text',
is_required: true,
is_for_all: true)
end
let(:job_args) do
{
user_id: admin.id,
source_project_id: source_project.id,
target_project_params: params,
associations_to_copy: [:work_packages]
}
end
let(:copy_job) do
CopyProjectJob.new(job_args).tap(&:perform_now)
end
let(:params) { { name: 'Copy', identifier: 'copy', type_ids: [type.id], work_package_custom_field_ids: [custom_field.id] } }
let(:expected_error_message) do
"#{WorkPackage.model_name.human} '#{work_package.type.name} ##{work_package.id}: #{work_package.subject}': #{custom_field.name} #{I18n.t('errors.messages.blank')}."
end
before do
source_project.work_package_custom_fields << custom_field
type.custom_fields << custom_field
allow(User).to receive(:current).and_return(admin)
@copied_project = copy_job.target_project
@errors = copy_job.errors
end
it 'copies the project', :aggregate_failures do
expect(Project.find_by(identifier: params[:identifier])).to eq(@copied_project)
expect(@errors.first).to eq(expected_error_message)
# expect to create a status
expect(copy_job.job_status).to be_present
expect(copy_job.job_status[:status]).to eq 'success'
[34444] Projects copy APIv3 (#9149) * Create copy project endpoint * Add representers * Add _meta representer for copy module information * Add Meta payload representer * Extract parsing of copy attributes into service * Extract enqueue job for projects copy * Keep request object available in the bodied endpoint this allows us to access grape (e.g., for redirecting) * Add DelayedModify endpoint that redirects to job status * Use DelayedModify endpoint for copying projects * Add api paths for form/copy * Disable cache on ProjectCopyPayload * Add spec * Add resource spec * Extend schema for meta * Extend docs * Don't pass service result, but state as meta to forms * Use copy dependencies for naming copy options in API * Add description property for counting * Remove unused action attribute * Skip writable checks on meta property for payloads * Use prepend to allow create(...) usage with meta * Extend copy spec with custom fields usage * Add spec for copy flags * Add todos * Pass errors correctly to copy settings * Remove invalid parameters to copy that are now caught through project service * Remove duplicated validation * Linting * Make count a human readable, formattable description string * Make source count string readable * Provide the source project for generating the counts * Extract copying of attachments into sepearate dependent services This will allow the API to dynamically generate a copy association schema from each dependent service * Add spec for copy schema representer * Add payload representer spec * Also validate the model to pass the validations when copying * Make description optional and add note to schemas.apib * Add send_notifications to representer * Allow to configure sendNotifications * Allow bodied to receive a state process callback That allows us to manage the meta state on the API level, not on the service level * Make other services compatible with BaseCallable * Fix params passing from BaseCallable Wrapping a single hash into kwargs obviously loses their key indifference and we can't expect all services to use a single params object. Older services use kwargs * Copy wiki page with parent_id This will save some memory instead of memoizing the entire wiki page * Fix typo * Default to true for all copy associations That means if only a true value is passed, all other options will still be true * Do not try to copy attachments if the base dependency wasnt copied * Ensure null identifier gets rendered * Fix paths to the form and commit * Change redirect to an URL * Extend documentation on meta properties as table * Ensure we pass a default params if incoming params are nil Now that SetAttributes needs to splat params, they do no longer correctly get the no-param-default behavior of Ruby, so we need to explictly assign a default params hash * Fix expect for trailing path
4 years ago
expect(copy_job.job_status[:payload]['redirect']).to include '/projects/copy'
end
end
describe 'project has an invalid repository' do
let(:admin) { FactoryBot.create(:admin) }
let(:source_project) do
project = FactoryBot.create(:project)
# add invalid repo
repository = Repository::Git.new scm_type: :existing, project: project
repository.save!(validate: false)
project.reload
project
end
let(:copy_job) do
CopyProjectJob.new.tap do |job|
job.perform user_id: admin.id,
source_project_id: source_project.id,
target_project_params: params,
associations_to_copy: [:work_packages]
end
end
before do
allow(User).to receive(:current).and_return(admin)
end
it 'saves without the repository' do
expect(source_project).not_to be_valid
copied_project = copy_job.target_project
errors = copy_job.errors
expect(errors).to be_empty
expect(copied_project).to be_valid
expect(copied_project.repository).to be_nil
expect(copied_project.enabled_module_names).not_to include 'repository'
end
end
describe 'copy project fails with internal error' do
let(:admin) { FactoryBot.create(:admin) }
let(:source_project) { FactoryBot.create(:project) }
let(:copy_job) do
CopyProjectJob.new.tap do |job|
job.perform user_id: admin.id,
source_project_id: source_project.id,
target_project_params: params,
associations_to_copy: [:work_packages]
end
end
let(:params) { { name: 'Copy', identifier: 'copy' } }
before do
allow(User).to receive(:current).and_return(admin)
allow(ProjectMailer).to receive(:copy_project_succeeded).and_raise 'error message not meant for user'
end
it 'renders a error when unexpected errors occur' do
expect(ProjectMailer)
.to receive(:copy_project_failed)
.with(admin, source_project, 'Copy', [I18n.t('copy_project.failed_internal')])
.and_return maildouble
expect { copy_job }.not_to raise_error
# expect to create a status
expect(copy_job.job_status).to be_present
expect(copy_job.job_status[:status]).to eq 'failure'
expect(copy_job.job_status[:message]).to include "Cannot copy project #{source_project.name}"
expect(copy_job.job_status[:payload]).to eq('title' => 'Copy project')
end
end
shared_context 'copy project' do
before do
CopyProjectJob.new.tap do |job|
job.perform user_id: user.id,
source_project_id: project_to_copy.id,
target_project_params: params,
associations_to_copy: [:members]
end
end
end
describe 'perform' do
before do
login_as(user)
expect(User).to receive(:current=).with(user).at_least(:once)
end
describe 'subproject' do
let(:params) { { name: 'Copy', identifier: 'copy', parent_id: project.id } }
let(:subproject) do
FactoryBot.create(:project, parent: project).tap do |p|
FactoryBot.create(:member,
principal: user,
roles: [role],
project: p)
end
end
subject { Project.find_by(identifier: 'copy') }
describe 'user without add_subprojects permission in parent' do
include_context 'copy project' do
let(:project_to_copy) { subproject }
end
it 'copies the project without the parent being set' do
expect(subject).not_to be_nil
expect(subject.parent).to be_nil
expect(subproject.reload.enabled_module_names).not_to be_empty
end
it "notifies the user of the success" do
mail = ActionMailer::Base.deliveries
.find { |m| m.message_id.start_with? "openproject.project-#{user.id}-#{subject.id}" }
expect(mail).to be_present
expect(mail.subject).to eq "Created project #{subject.name}"
end
end
describe 'user with add_subprojects permission in parent' do
let(:role_add_subproject) { FactoryBot.create(:role, permissions: [:add_subprojects]) }
let(:member_add_subproject) do
FactoryBot.create(:member,
user: user,
project: project,
roles: [role_add_subproject])
end
before do
member_add_subproject
end
include_context 'copy project' do
let(:project_to_copy) { subproject }
end
it 'copies the project' do
expect(subject).not_to be_nil
expect(subject.parent).to eql(project)
expect(subproject.reload.enabled_module_names).not_to be_empty
end
it "notifies the user of the success" do
mail = ActionMailer::Base.deliveries
.find { |m| m.message_id.start_with? "openproject.project-#{user.id}-#{subject.id}" }
expect(mail).to be_present
expect(mail.subject).to eq "Created project #{subject.name}"
end
end
end
end
end