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/requests/api/v3/projects/copy/copy_resource_spec.rb

214 lines
6.4 KiB

[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
#-- encoding: UTF-8
#-- 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-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'
require 'rack/test'
describe ::API::V3::Projects::Copy::CopyAPI, content_type: :json do
include Rack::Test::Methods
include API::V3::Utilities::PathHelper
shared_let(:text_custom_field) do
FactoryBot.create(:text_project_custom_field)
end
shared_let(:list_custom_field) do
FactoryBot.create(:list_project_custom_field)
end
shared_let(:source_project) do
FactoryBot.create :project,
enabled_module_names: %w[work_package_tracking wiki],
custom_field_values: {
text_custom_field.id => 'source text',
list_custom_field.id => list_custom_field.custom_options.last.id
}
end
shared_let(:work_package) { FactoryBot.create :work_package, project: source_project }
shared_let(:wiki_page) { FactoryBot.create :wiki_page, wiki: source_project.wiki }
shared_let(:current_user) do
FactoryBot.create :user,
member_in_project: source_project,
member_with_permissions: %i[copy_projects view_project view_work_packages]
end
let(:path) { api_v3_paths.project_copy(source_project.id) }
let(:params) do
{
}
end
before do
login_as(current_user)
post path, params.to_json
end
subject(:response) { last_response }
describe '#POST /api/v3/projects/:id/copy' do
describe 'with empty params' do
it 'returns 422', :aggregate_failures do
expect(response.status).to eq(422)
expect(response.body)
.to be_json_eql('Error'.to_json)
.at_path('_type')
expect(response.body)
.to be_json_eql("Multiple field constraints have been violated.".to_json)
.at_path("message")
errors = JSON.parse(response.body)['_embedded']['errors']
expect(errors.collect { |el| el['message'] })
.to contain_exactly "Identifier can't be blank.", "Name can't be blank."
end
end
describe 'with attributes given' do
let(:params) do
{ name: 'My copied project',
identifier: 'my-copied-project',
"customField#{text_custom_field.id}": {
"raw": "CF text"
},
}
end
it 'returns with a redirect to job' do
aggregate_failures do
expect(response.status).to eq(302)
expect(response).to be_redirect
expect(response.location).to match /\/api\/v3\/job_statuses\/[\w-]+\z/
end
get response.location
expect(last_response.status).to eq 200
expect(last_response.body)
.to be_json_eql('in_queue'.to_json)
.at_path("status")
perform_enqueued_jobs
get response.location
expect(last_response.status).to eq 200
expect(last_response.body)
.to be_json_eql('success'.to_json)
.at_path("status")
expect(last_response.body)
.to be_json_eql("Created project My copied project".to_json)
.at_path("message")
project = Project.find_by(identifier: 'my-copied-project')
expect(project).to be_present
expect(project.custom_value_for(text_custom_field).value).to eq 'CF text'
expect(project.custom_value_for(list_custom_field).formatted_value).to eq list_custom_field.custom_options.last.value
end
end
describe 'with restricted copying' do
let(:params) do
{ name: 'My copied project',
identifier: 'my-copied-project',
_meta: {
copyWorkPackages: true,
copyWiki: false,
}
}
end
it 'does not copy the wiki' do
perform_enqueued_jobs
project = Project.find_by(identifier: 'my-copied-project')
expect(project).to be_present
expect(source_project.wiki.pages.count).to eq 1
expect(project.wiki.pages.count).to eq 0
expect(source_project.work_packages.count).to eq 1
expect(project.work_packages.count).to eq 1
end
end
describe 'sendNotifications' do
let(:params) do
{ name: 'My copied project',
identifier: 'my-copied-project',
_meta: {
sendNotifications: sendNotifications
}
}
end
context 'when false' do
let(:sendNotifications) { false }
it 'queues the job without notifications' do
expect(CopyProjectJob)
.to have_been_enqueued.with do |args|
expect(args[:send_mails]).to eq false
end
end
end
context 'when true' do
let(:sendNotifications) { true }
it 'queues the job with notifications' do
expect(CopyProjectJob)
.to have_been_enqueued.with do |args|
expect(args[:send_mails]).to eq true
end
end
end
end
context 'without the necessary permission' do
let(:current_user) do
FactoryBot.create :user,
member_in_project: source_project,
member_with_permissions: %i[view_project view_work_packages]
end
it 'returns 403 Not Authorized' do
expect(response.status).to eq(403)
end
end
end
end