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) }
|
|
|
|
let(:file) { File.join(Rails.root, 'spec', 'features', 'projects', 'import_export', 'test_project_export.tar.gz') }
|
2018-02-12 09:13:08 +00:00
|
|
|
let(:namespace) { create(:group) }
|
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-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:46:40 +00:00
|
|
|
stub_import(user.namespace)
|
2018-02-09 14:58:28 +00:00
|
|
|
|
2018-02-13 10:24:14 +00:00
|
|
|
post api('/projects/import', user), path: 'test-import', file: fixture_file_upload(file), namespace: namespace.id
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(201)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'schedules an import using the namespace path' do
|
2018-02-14 13:46:40 +00:00
|
|
|
stub_import(unamespace)
|
2018-02-13 10:24:14 +00:00
|
|
|
|
2018-02-12 15:02:15 +00:00
|
|
|
post api('/projects/import', user), 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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
post api('/projects/import', user), path: 'test-import2', file: fixture_file_upload(file)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(201)
|
|
|
|
end
|
|
|
|
|
2018-02-14 13:46:40 +00:00
|
|
|
it 'schedules an import at the user namespace level' do
|
|
|
|
expect_any_instance_of(Project).not_to receive(:import_schedule)
|
|
|
|
expect(Gitlab::ImportExport::ProjectCreator).not_to receive(:new)
|
|
|
|
|
|
|
|
post api('/projects/import', user), namespace: 'nonexistent', path: 'test-import2', file: fixture_file_upload(file)
|
|
|
|
|
|
|
|
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
|
|
|
|
expect_any_instance_of(Project).not_to receive(:import_schedule)
|
|
|
|
|
|
|
|
post(api('/projects/import', create(:user)),
|
|
|
|
path: 'test-import3',
|
|
|
|
file: fixture_file_upload(file),
|
|
|
|
namespace: namespace.full_path)
|
|
|
|
|
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
|
|
|
|
expect_any_instance_of(Project).not_to receive(:import_schedule)
|
|
|
|
|
|
|
|
post api('/projects/import', user), path: 'test-import3', file: './random/test'
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def stub_import(namespace)
|
|
|
|
expect_any_instance_of(Project).to receive(:import_schedule)
|
|
|
|
expect(Gitlab::ImportExport::ProjectCreator).to receive(:new).with(namespace.id, any_args).and_call_original
|
|
|
|
end
|
2018-02-12 09:13:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/import' do
|
|
|
|
it 'returns the import status' do
|
|
|
|
project = create(:project, import_status: 'started')
|
2018-02-13 08:24:10 +00:00
|
|
|
project.add_master(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
|
|
|
|
project = create(:project, import_status: 'failed', import_error: 'error')
|
2018-02-13 08:24:10 +00:00
|
|
|
project.add_master(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' => '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
|