2019-10-29 00:06:10 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-09 14:58:28 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe API::ProjectImport do
|
|
|
|
let(:export_path) { "#{Dir.tmpdir}/project_export_spec" }
|
|
|
|
let(:user) { create(:user) }
|
2018-06-06 21:26:00 +00:00
|
|
|
let(:file) { File.join('spec', 'features', 'projects', 'import_export', 'test_project_export.tar.gz') }
|
2018-02-12 09:13:08 +00:00
|
|
|
let(:namespace) { create(:group) }
|
2019-12-17 18:07:48 +00:00
|
|
|
|
2018-02-09 14:58:28 +00:00
|
|
|
before do
|
|
|
|
allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path)
|
2018-07-26 10:55:21 +00:00
|
|
|
stub_uploads_object_storage(FileUploader)
|
2018-02-09 14:58:28 +00:00
|
|
|
|
2018-02-12 14:26:59 +00:00
|
|
|
namespace.add_owner(user)
|
2018-02-09 14:58:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
FileUtils.rm_rf(export_path, secure: true)
|
|
|
|
end
|
|
|
|
|
2018-02-12 09:13:08 +00:00
|
|
|
describe 'POST /projects/import' do
|
2018-02-13 09:54:04 +00:00
|
|
|
it 'schedules an import using a namespace' do
|
2018-02-14 13:55:11 +00:00
|
|
|
stub_import(namespace)
|
2018-02-09 14:58:28 +00:00
|
|
|
|
2018-12-17 22:52:17 +00:00
|
|
|
post api('/projects/import', user), params: { path: 'test-import', file: fixture_file_upload(file), namespace: namespace.id }
|
2018-02-13 10:24:14 +00:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(201)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'schedules an import using the namespace path' do
|
2018-02-14 13:55:11 +00:00
|
|
|
stub_import(namespace)
|
2018-02-13 10:24:14 +00:00
|
|
|
|
2018-12-17 22:52:17 +00:00
|
|
|
post api('/projects/import', user), params: { path: 'test-import', file: fixture_file_upload(file), namespace: namespace.full_path }
|
2018-02-12 09:13:08 +00:00
|
|
|
|
2018-02-12 15:02:15 +00:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2018-02-12 09:13:08 +00:00
|
|
|
end
|
2018-02-13 09:54:04 +00:00
|
|
|
|
2019-09-20 00:05:59 +00:00
|
|
|
context 'when a name is explicitly set' do
|
|
|
|
let(:expected_name) { 'test project import' }
|
|
|
|
|
|
|
|
it 'schedules an import using a namespace and a different name' do
|
|
|
|
stub_import(namespace)
|
|
|
|
|
|
|
|
post api('/projects/import', user), params: { path: 'test-import', file: fixture_file_upload(file), namespace: namespace.id, name: expected_name }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(201)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'schedules an import using the namespace path and a different name' do
|
|
|
|
stub_import(namespace)
|
|
|
|
|
|
|
|
post api('/projects/import', user), params: { path: 'test-import', file: fixture_file_upload(file), namespace: namespace.full_path, name: expected_name }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(201)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets name correctly' do
|
|
|
|
stub_import(namespace)
|
|
|
|
|
|
|
|
post api('/projects/import', user), params: { path: 'test-import', file: fixture_file_upload(file), namespace: namespace.full_path, name: expected_name }
|
|
|
|
|
|
|
|
project = Project.find(json_response['id'])
|
|
|
|
expect(project.name).to eq(expected_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets name correctly with an overwrite' do
|
|
|
|
stub_import(namespace)
|
|
|
|
|
|
|
|
post api('/projects/import', user), params: { path: 'test-import', file: fixture_file_upload(file), namespace: namespace.full_path, name: 'new project name', overwrite: true }
|
|
|
|
|
|
|
|
project = Project.find(json_response['id'])
|
|
|
|
expect(project.name).to eq('new project name')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'schedules an import using the path and name explicitly set to nil' do
|
|
|
|
stub_import(namespace)
|
|
|
|
|
|
|
|
post api('/projects/import', user), params: { path: 'test-import', file: fixture_file_upload(file), namespace: namespace.full_path, name: nil }
|
|
|
|
|
|
|
|
project = Project.find(json_response['id'])
|
|
|
|
expect(project.name).to eq('test-import')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-13 09:54:04 +00:00
|
|
|
it 'schedules an import at the user namespace level' do
|
2018-02-14 13:46:40 +00:00
|
|
|
stub_import(user.namespace)
|
2018-02-13 09:54:04 +00:00
|
|
|
|
2018-12-17 22:52:17 +00:00
|
|
|
post api('/projects/import', user), params: { path: 'test-import2', file: fixture_file_upload(file) }
|
2018-02-13 09:54:04 +00:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(201)
|
|
|
|
end
|
|
|
|
|
2018-08-16 11:40:03 +00:00
|
|
|
it 'does not schedule an import for a namespace that does not exist' do
|
2018-11-27 09:41:27 +00:00
|
|
|
expect_any_instance_of(ProjectImportState).not_to receive(:schedule)
|
2018-02-16 17:03:34 +00:00
|
|
|
expect(::Projects::CreateService).not_to receive(:new)
|
2018-02-14 13:46:40 +00:00
|
|
|
|
2018-12-17 22:52:17 +00:00
|
|
|
post api('/projects/import', user), params: { namespace: 'nonexistent', path: 'test-import2', file: fixture_file_upload(file) }
|
2018-02-14 13:46:40 +00:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(404)
|
|
|
|
expect(json_response['message']).to eq('404 Namespace Not Found')
|
|
|
|
end
|
|
|
|
|
2018-02-13 09:54:04 +00:00
|
|
|
it 'does not schedule an import if the user has no permission to the namespace' do
|
2018-11-27 09:41:27 +00:00
|
|
|
expect_any_instance_of(ProjectImportState).not_to receive(:schedule)
|
2018-02-13 09:54:04 +00:00
|
|
|
|
|
|
|
post(api('/projects/import', create(:user)),
|
2018-12-17 22:52:17 +00:00
|
|
|
params: {
|
|
|
|
path: 'test-import3',
|
|
|
|
file: fixture_file_upload(file),
|
|
|
|
namespace: namespace.full_path
|
|
|
|
})
|
2018-02-13 09:54:04 +00:00
|
|
|
|
2018-02-14 13:46:40 +00:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
|
|
|
expect(json_response['message']).to eq('404 Namespace Not Found')
|
2018-02-13 09:54:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not schedule an import if the user uploads no valid file' do
|
2018-11-27 09:41:27 +00:00
|
|
|
expect_any_instance_of(ProjectImportState).not_to receive(:schedule)
|
2018-02-13 09:54:04 +00:00
|
|
|
|
2018-12-17 22:52:17 +00:00
|
|
|
post api('/projects/import', user), params: { path: 'test-import3', file: './random/test' }
|
2018-02-13 09:54:04 +00:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(400)
|
|
|
|
expect(json_response['error']).to eq('file is invalid')
|
|
|
|
end
|
2018-02-14 13:46:40 +00:00
|
|
|
|
2018-03-29 13:08:31 +00:00
|
|
|
it 'stores params that can be overridden' do
|
2018-03-29 11:57:21 +00:00
|
|
|
stub_import(namespace)
|
|
|
|
override_params = { 'description' => 'Hello world' }
|
|
|
|
|
|
|
|
post api('/projects/import', user),
|
2018-12-17 22:52:17 +00:00
|
|
|
params: {
|
|
|
|
path: 'test-import',
|
|
|
|
file: fixture_file_upload(file),
|
|
|
|
namespace: namespace.id,
|
|
|
|
override_params: override_params
|
|
|
|
}
|
2018-03-29 11:57:21 +00:00
|
|
|
import_project = Project.find(json_response['id'])
|
|
|
|
|
|
|
|
expect(import_project.import_data.data['override_params']).to eq(override_params)
|
|
|
|
end
|
|
|
|
|
2018-03-29 13:08:31 +00:00
|
|
|
it 'does not store params that are not allowed' do
|
2018-03-29 11:57:21 +00:00
|
|
|
stub_import(namespace)
|
|
|
|
override_params = { 'not_allowed' => 'Hello world' }
|
|
|
|
|
|
|
|
post api('/projects/import', user),
|
2018-12-17 22:52:17 +00:00
|
|
|
params: {
|
|
|
|
path: 'test-import',
|
|
|
|
file: fixture_file_upload(file),
|
|
|
|
namespace: namespace.id,
|
|
|
|
override_params: override_params
|
|
|
|
}
|
2018-03-29 11:57:21 +00:00
|
|
|
import_project = Project.find(json_response['id'])
|
|
|
|
|
|
|
|
expect(import_project.import_data.data['override_params']).to be_empty
|
|
|
|
end
|
|
|
|
|
2019-10-23 09:06:03 +00:00
|
|
|
it 'correctly overrides params during the import', :sidekiq_might_not_need_inline do
|
2018-03-29 13:08:31 +00:00
|
|
|
override_params = { 'description' => 'Hello world' }
|
|
|
|
|
2018-07-23 04:34:54 +00:00
|
|
|
perform_enqueued_jobs do
|
2018-03-29 13:08:31 +00:00
|
|
|
post api('/projects/import', user),
|
2018-12-17 22:52:17 +00:00
|
|
|
params: {
|
|
|
|
path: 'test-import',
|
|
|
|
file: fixture_file_upload(file),
|
|
|
|
namespace: namespace.id,
|
|
|
|
override_params: override_params
|
|
|
|
}
|
2018-03-29 13:08:31 +00:00
|
|
|
end
|
|
|
|
import_project = Project.find(json_response['id'])
|
|
|
|
|
|
|
|
expect(import_project.description).to eq('Hello world')
|
|
|
|
end
|
|
|
|
|
2018-04-06 15:23:49 +00:00
|
|
|
context 'when target path already exists in namespace' do
|
|
|
|
let(:existing_project) { create(:project, namespace: user.namespace) }
|
|
|
|
|
|
|
|
it 'does not schedule an import' do
|
2018-11-27 09:41:27 +00:00
|
|
|
expect_any_instance_of(ProjectImportState).not_to receive(:schedule)
|
2018-04-06 15:23:49 +00:00
|
|
|
|
2018-12-17 22:52:17 +00:00
|
|
|
post api('/projects/import', user), params: { path: existing_project.path, file: fixture_file_upload(file) }
|
2018-04-06 15:23:49 +00:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(400)
|
|
|
|
expect(json_response['message']).to eq('Name has already been taken')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when param overwrite is true' do
|
|
|
|
it 'schedules an import' do
|
|
|
|
stub_import(user.namespace)
|
|
|
|
|
2018-12-17 22:52:17 +00:00
|
|
|
post api('/projects/import', user), params: { path: existing_project.path, file: fixture_file_upload(file), overwrite: true }
|
2018-04-06 15:23:49 +00:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(201)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-27 09:08:32 +00:00
|
|
|
context 'when request exceeds the rate limit' do
|
|
|
|
before do
|
|
|
|
allow(::Gitlab::ApplicationRateLimiter).to receive(:throttled?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'prevents users from importing projects' do
|
|
|
|
post api('/projects/import', user), params: { path: 'test-import', file: fixture_file_upload(file), namespace: namespace.id }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(429)
|
|
|
|
expect(json_response['message']['error']).to eq('This endpoint has been requested too many times. Try again later.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-14 13:46:40 +00:00
|
|
|
def stub_import(namespace)
|
2018-11-27 09:41:27 +00:00
|
|
|
expect_any_instance_of(ProjectImportState).to receive(:schedule)
|
2018-02-16 17:03:34 +00:00
|
|
|
expect(::Projects::CreateService).to receive(:new).with(user, hash_including(namespace_id: namespace.id)).and_call_original
|
2018-02-14 13:46:40 +00:00
|
|
|
end
|
2018-02-12 09:13:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/import' do
|
|
|
|
it 'returns the import status' do
|
2018-05-02 13:35:04 +00:00
|
|
|
project = create(:project, :import_started)
|
2018-07-11 14:36:08 +00:00
|
|
|
project.add_maintainer(user)
|
2018-02-12 09:13:08 +00:00
|
|
|
|
|
|
|
get api("/projects/#{project.id}/import", user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2018-02-12 11:40:55 +00:00
|
|
|
expect(json_response).to include('import_status' => 'started')
|
2018-02-12 09:13:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the import status and the error if failed' do
|
2018-05-02 13:35:04 +00:00
|
|
|
project = create(:project, :import_failed)
|
2018-07-11 14:36:08 +00:00
|
|
|
project.add_maintainer(user)
|
2018-07-02 10:43:06 +00:00
|
|
|
project.import_state.update(last_error: 'error')
|
2018-02-12 09:13:08 +00:00
|
|
|
|
|
|
|
get api("/projects/#{project.id}/import", user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2018-02-12 11:40:55 +00:00
|
|
|
expect(json_response).to include('import_status' => 'failed',
|
2018-02-12 13:46:47 +00:00
|
|
|
'import_error' => 'error')
|
2018-02-12 09:13:08 +00:00
|
|
|
end
|
2018-02-09 14:58:28 +00:00
|
|
|
end
|
|
|
|
end
|