2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-09 09:58:28 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe API::ProjectImport do
|
2020-02-27 04:09:01 -05:00
|
|
|
include WorkhorseHelpers
|
|
|
|
|
2018-02-09 09:58:28 -05:00
|
|
|
let(:user) { create(:user) }
|
2018-06-06 17:26:00 -04:00
|
|
|
let(:file) { File.join('spec', 'features', 'projects', 'import_export', 'test_project_export.tar.gz') }
|
2018-02-12 04:13:08 -05:00
|
|
|
let(:namespace) { create(:group) }
|
2019-12-17 13:07:48 -05:00
|
|
|
|
2020-02-27 04:09:01 -05:00
|
|
|
let(:workhorse_token) { JWT.encode({ 'iss' => 'gitlab-workhorse' }, Gitlab::Workhorse.secret, 'HS256') }
|
|
|
|
let(:workhorse_headers) { { 'GitLab-Workhorse' => '1.0', Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER => workhorse_token } }
|
|
|
|
|
2018-02-09 09:58:28 -05:00
|
|
|
before do
|
2018-02-12 09:26:59 -05:00
|
|
|
namespace.add_owner(user)
|
2018-02-09 09:58:28 -05:00
|
|
|
end
|
|
|
|
|
2018-02-12 04:13:08 -05:00
|
|
|
describe 'POST /projects/import' do
|
2020-03-13 14:09:39 -04:00
|
|
|
subject { upload_archive(file_upload, workhorse_headers, params) }
|
|
|
|
|
|
|
|
let(:file_upload) { fixture_file_upload(file) }
|
|
|
|
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
path: 'test-import',
|
|
|
|
'file.size' => file_upload.size
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(ImportExportUploader).to receive(:workhorse_upload_path).and_return('/')
|
|
|
|
end
|
|
|
|
|
2018-02-13 04:54:04 -05:00
|
|
|
it 'schedules an import using a namespace' do
|
2018-02-14 08:55:11 -05:00
|
|
|
stub_import(namespace)
|
2020-03-13 14:09:39 -04:00
|
|
|
params[:namespace] = namespace.id
|
2018-02-09 09:58:28 -05:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
subject
|
2018-02-13 05:24:14 -05:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2018-02-13 05:24:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'schedules an import using the namespace path' do
|
2018-02-14 08:55:11 -05:00
|
|
|
stub_import(namespace)
|
2020-03-13 14:09:39 -04:00
|
|
|
params[:namespace] = namespace.full_path
|
2018-02-13 05:24:14 -05:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
subject
|
2018-02-12 04:13:08 -05:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2018-02-12 04:13:08 -05:00
|
|
|
end
|
2018-02-13 04:54:04 -05:00
|
|
|
|
2019-09-19 20:05:59 -04: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)
|
2020-03-13 14:09:39 -04:00
|
|
|
params[:name] = expected_name
|
|
|
|
params[:namespace] = namespace.id
|
2019-09-19 20:05:59 -04:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
subject
|
2019-09-19 20:05:59 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2019-09-19 20:05:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'schedules an import using the namespace path and a different name' do
|
|
|
|
stub_import(namespace)
|
2020-03-13 14:09:39 -04:00
|
|
|
params[:name] = expected_name
|
|
|
|
params[:namespace] = namespace.full_path
|
2019-09-19 20:05:59 -04:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
subject
|
2019-09-19 20:05:59 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2019-09-19 20:05:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets name correctly' do
|
|
|
|
stub_import(namespace)
|
2020-03-13 14:09:39 -04:00
|
|
|
params[:name] = expected_name
|
|
|
|
params[:namespace] = namespace.full_path
|
2019-09-19 20:05:59 -04:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
subject
|
2019-09-19 20:05:59 -04:00
|
|
|
|
|
|
|
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)
|
2020-03-13 14:09:39 -04:00
|
|
|
params[:name] = 'new project name'
|
|
|
|
params[:namespace] = namespace.full_path
|
|
|
|
params[:overwrite] = true
|
2019-09-19 20:05:59 -04:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
subject
|
2019-09-19 20:05:59 -04:00
|
|
|
|
|
|
|
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)
|
2020-03-13 14:09:39 -04:00
|
|
|
params[:name] = nil
|
|
|
|
params[:namespace] = namespace.full_path
|
2019-09-19 20:05:59 -04:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
subject
|
2019-09-19 20:05:59 -04:00
|
|
|
|
|
|
|
project = Project.find(json_response['id'])
|
|
|
|
expect(project.name).to eq('test-import')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-13 04:54:04 -05:00
|
|
|
it 'schedules an import at the user namespace level' do
|
2018-02-14 08:46:40 -05:00
|
|
|
stub_import(user.namespace)
|
2020-03-13 14:09:39 -04:00
|
|
|
params[:path] = 'test-import2'
|
2018-02-13 04:54:04 -05:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
subject
|
2018-02-13 04:54:04 -05:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2018-02-13 04:54:04 -05:00
|
|
|
end
|
|
|
|
|
2018-08-16 07:40:03 -04:00
|
|
|
it 'does not schedule an import for a namespace that does not exist' do
|
2018-11-27 04:41:27 -05:00
|
|
|
expect_any_instance_of(ProjectImportState).not_to receive(:schedule)
|
2018-02-16 12:03:34 -05:00
|
|
|
expect(::Projects::CreateService).not_to receive(:new)
|
2018-02-14 08:46:40 -05:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
params[:namespace] = 'nonexistent'
|
|
|
|
params[:path] = 'test-import2'
|
|
|
|
|
|
|
|
subject
|
2018-02-14 08:46:40 -05:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2018-02-14 08:46:40 -05:00
|
|
|
expect(json_response['message']).to eq('404 Namespace Not Found')
|
|
|
|
end
|
|
|
|
|
2018-02-13 04:54:04 -05:00
|
|
|
it 'does not schedule an import if the user has no permission to the namespace' do
|
2018-11-27 04:41:27 -05:00
|
|
|
expect_any_instance_of(ProjectImportState).not_to receive(:schedule)
|
2018-02-13 04:54:04 -05:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
new_namespace = create(:group)
|
|
|
|
params[:path] = 'test-import3'
|
|
|
|
params[:namespace] = new_namespace.full_path
|
|
|
|
|
|
|
|
subject
|
2018-02-13 04:54:04 -05:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2018-02-14 08:46:40 -05:00
|
|
|
expect(json_response['message']).to eq('404 Namespace Not Found')
|
2018-02-13 04:54:04 -05:00
|
|
|
end
|
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
context 'if user uploads no valid file' do
|
|
|
|
let(:file) { 'README.md' }
|
2018-02-13 04:54:04 -05:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
it 'does not schedule an import if the user uploads no valid file' do
|
|
|
|
expect_any_instance_of(ProjectImportState).not_to receive(:schedule)
|
|
|
|
|
|
|
|
params[:path] = 'test-import3'
|
|
|
|
|
|
|
|
subject
|
2018-02-13 04:54:04 -05:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
|
|
|
expect(json_response['message']['error']).to eq('You need to upload a GitLab project export archive (ending in .gz).')
|
|
|
|
end
|
2018-02-13 04:54:04 -05:00
|
|
|
end
|
2018-02-14 08:46:40 -05:00
|
|
|
|
2018-03-29 09:08:31 -04:00
|
|
|
it 'stores params that can be overridden' do
|
2018-03-29 07:57:21 -04:00
|
|
|
stub_import(namespace)
|
|
|
|
override_params = { 'description' => 'Hello world' }
|
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
params[:namespace] = namespace.id
|
|
|
|
params[:override_params] = override_params
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
2018-03-29 07:57:21 -04:00
|
|
|
import_project = Project.find(json_response['id'])
|
|
|
|
|
|
|
|
expect(import_project.import_data.data['override_params']).to eq(override_params)
|
|
|
|
end
|
|
|
|
|
2018-03-29 09:08:31 -04:00
|
|
|
it 'does not store params that are not allowed' do
|
2018-03-29 07:57:21 -04:00
|
|
|
stub_import(namespace)
|
|
|
|
override_params = { 'not_allowed' => 'Hello world' }
|
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
params[:namespace] = namespace.id
|
|
|
|
params[:override_params] = override_params
|
2018-03-29 07:57:21 -04:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
subject
|
2018-03-29 09:08:31 -04:00
|
|
|
|
|
|
|
import_project = Project.find(json_response['id'])
|
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
expect(import_project.import_data.data['override_params']).to be_empty
|
2018-03-29 09:08:31 -04:00
|
|
|
end
|
|
|
|
|
2018-04-06 11:23:49 -04: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 04:41:27 -05:00
|
|
|
expect_any_instance_of(ProjectImportState).not_to receive(:schedule)
|
2018-04-06 11:23:49 -04:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
params[:path] = existing_project.path
|
|
|
|
|
|
|
|
subject
|
2018-04-06 11:23:49 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
2018-04-06 11:23:49 -04:00
|
|
|
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)
|
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
params[:path] = existing_project.path
|
|
|
|
params[:overwrite] = true
|
|
|
|
|
|
|
|
subject
|
2018-04-06 11:23:49 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2018-04-06 11:23:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-27 04:08:32 -05: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
|
2020-03-13 14:09:39 -04:00
|
|
|
params[:namespace] = namespace.id
|
|
|
|
|
|
|
|
subject
|
2020-01-27 04:08:32 -05:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:too_many_requests)
|
2020-01-27 04:08:32 -05:00
|
|
|
expect(json_response['message']['error']).to eq('This endpoint has been requested too many times. Try again later.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
context 'when using remote storage' do
|
2020-02-27 04:09:01 -05:00
|
|
|
let(:file_name) { 'project_export.tar.gz' }
|
|
|
|
|
|
|
|
let!(:fog_connection) do
|
|
|
|
stub_uploads_object_storage(ImportExportUploader, direct_upload: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:tmp_object) do
|
|
|
|
fog_connection.directories.new(key: 'uploads').files.create(
|
|
|
|
key: "tmp/uploads/#{file_name}",
|
|
|
|
body: fixture_file_upload(file)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:file_upload) { fog_to_uploaded_file(tmp_object) }
|
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
it 'schedules an import' do
|
|
|
|
stub_import(namespace)
|
|
|
|
params[:namespace] = namespace.id
|
2020-02-27 04:09:01 -05:00
|
|
|
|
2020-03-13 14:09:39 -04:00
|
|
|
subject
|
2020-02-27 04:09:01 -05:00
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_archive(file, headers = {}, params = {})
|
|
|
|
workhorse_finalize(
|
|
|
|
api("/projects/import", user),
|
|
|
|
method: :post,
|
|
|
|
file_key: :file,
|
2020-03-13 14:09:39 -04:00
|
|
|
params: params.merge(file: file),
|
2020-02-27 04:09:01 -05:00
|
|
|
headers: headers,
|
|
|
|
send_rewritten_field: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-02-14 08:46:40 -05:00
|
|
|
def stub_import(namespace)
|
2018-11-27 04:41:27 -05:00
|
|
|
expect_any_instance_of(ProjectImportState).to receive(:schedule)
|
2018-02-16 12:03:34 -05:00
|
|
|
expect(::Projects::CreateService).to receive(:new).with(user, hash_including(namespace_id: namespace.id)).and_call_original
|
2018-02-14 08:46:40 -05:00
|
|
|
end
|
2018-02-12 04:13:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/import' do
|
|
|
|
it 'returns the import status' do
|
2018-05-02 09:35:04 -04:00
|
|
|
project = create(:project, :import_started)
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2018-02-12 04:13:08 -05:00
|
|
|
|
|
|
|
get api("/projects/#{project.id}/import", user)
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-02-12 06:40:55 -05:00
|
|
|
expect(json_response).to include('import_status' => 'started')
|
2018-02-12 04:13:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the import status and the error if failed' do
|
2018-05-02 09:35:04 -04:00
|
|
|
project = create(:project, :import_failed)
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2018-07-02 06:43:06 -04:00
|
|
|
project.import_state.update(last_error: 'error')
|
2018-02-12 04:13:08 -05:00
|
|
|
|
|
|
|
get api("/projects/#{project.id}/import", user)
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-02-12 06:40:55 -05:00
|
|
|
expect(json_response).to include('import_status' => 'failed',
|
2018-02-12 08:46:47 -05:00
|
|
|
'import_error' => 'error')
|
2018-02-12 04:13:08 -05:00
|
|
|
end
|
2018-02-09 09:58:28 -05:00
|
|
|
end
|
2020-02-27 04:09:01 -05:00
|
|
|
|
|
|
|
describe 'POST /projects/import/authorize' do
|
|
|
|
subject { post api('/projects/import/authorize', user), headers: workhorse_headers }
|
|
|
|
|
|
|
|
it 'authorizes importing project with workhorse header' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.content_type.to_s).to eq(Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE)
|
2020-03-13 14:09:39 -04:00
|
|
|
expect(json_response['TempPath']).to eq(ImportExportUploader.workhorse_local_upload_path)
|
2020-02-27 04:09:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'rejects requests that bypassed gitlab-workhorse' do
|
|
|
|
workhorse_headers.delete(Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when using remote storage' do
|
|
|
|
context 'when direct upload is enabled' do
|
|
|
|
before do
|
|
|
|
stub_uploads_object_storage(ImportExportUploader, enabled: true, direct_upload: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with status 200, location of file remote store and object details' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.content_type.to_s).to eq(Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE)
|
|
|
|
expect(json_response).not_to have_key('TempPath')
|
|
|
|
expect(json_response['RemoteObject']).to have_key('ID')
|
|
|
|
expect(json_response['RemoteObject']).to have_key('GetURL')
|
|
|
|
expect(json_response['RemoteObject']).to have_key('StoreURL')
|
|
|
|
expect(json_response['RemoteObject']).to have_key('DeleteURL')
|
|
|
|
expect(json_response['RemoteObject']).to have_key('MultipartUpload')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when direct upload is disabled' do
|
|
|
|
before do
|
|
|
|
stub_uploads_object_storage(ImportExportUploader, enabled: true, direct_upload: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles as a local file' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.content_type.to_s).to eq(Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE)
|
|
|
|
expect(json_response['TempPath']).to eq(ImportExportUploader.workhorse_local_upload_path)
|
|
|
|
expect(json_response['RemoteObject']).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-02-09 09:58:28 -05:00
|
|
|
end
|