2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-05 23:28:12 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe API::ProjectExport, :clean_gitlab_redis_cache do
|
2020-02-17 10:09:01 -05:00
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:project_none) { create(:project) }
|
|
|
|
let_it_be(:project_started) { create(:project) }
|
2018-08-29 09:41:56 -04:00
|
|
|
let(:project_finished) { create(:project, :with_export) }
|
|
|
|
let(:project_after_export) { create(:project, :with_export) }
|
2020-02-17 10:09:01 -05:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:admin) { create(:admin) }
|
2017-12-05 23:28:12 -05:00
|
|
|
|
|
|
|
let(:path) { "/projects/#{project.id}/export" }
|
|
|
|
let(:path_none) { "/projects/#{project_none.id}/export" }
|
|
|
|
let(:path_started) { "/projects/#{project_started.id}/export" }
|
|
|
|
let(:path_finished) { "/projects/#{project_finished.id}/export" }
|
2018-03-30 11:45:59 -04:00
|
|
|
let(:path_after_export) { "/projects/#{project_after_export.id}/export" }
|
2017-12-05 23:28:12 -05:00
|
|
|
|
|
|
|
let(:download_path) { "/projects/#{project.id}/export/download" }
|
|
|
|
let(:download_path_none) { "/projects/#{project_none.id}/export/download" }
|
|
|
|
let(:download_path_started) { "/projects/#{project_started.id}/export/download" }
|
|
|
|
let(:download_path_finished) { "/projects/#{project_finished.id}/export/download" }
|
2018-03-30 11:45:59 -04:00
|
|
|
let(:download_path_export_action) { "/projects/#{project_after_export.id}/export/download" }
|
2017-12-05 23:28:12 -05:00
|
|
|
|
|
|
|
let(:export_path) { "#{Dir.tmpdir}/project_export_spec" }
|
|
|
|
|
|
|
|
before do
|
2020-11-02 16:09:10 -05:00
|
|
|
allow(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path)
|
2020-03-12 05:09:55 -04:00
|
|
|
allow_next_instance_of(ProjectExportWorker) do |job|
|
|
|
|
allow(job).to receive(:jid).and_return(SecureRandom.hex(8))
|
|
|
|
end
|
2017-12-05 23:28:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
FileUtils.rm_rf(export_path, secure: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'when project export is disabled' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(project_export_enabled?: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '404 response'
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:project_id/export' do
|
|
|
|
shared_examples_for 'get project export status not found' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(path, user) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'get project export status denied' do
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(path, user) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'get project export status ok' do
|
|
|
|
it 'is none' do
|
|
|
|
get api(path_none, user)
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-12-05 23:28:12 -05:00
|
|
|
expect(response).to match_response_schema('public_api/v4/project/export_status')
|
|
|
|
expect(json_response['export_status']).to eq('none')
|
|
|
|
end
|
|
|
|
|
2020-03-12 05:09:55 -04:00
|
|
|
context 'when project export has started' do
|
|
|
|
before do
|
|
|
|
create(:project_export_job, project: project_started, status: 1)
|
|
|
|
end
|
2017-12-05 23:28:12 -05:00
|
|
|
|
2020-03-12 05:09:55 -04:00
|
|
|
it 'returns status started' do
|
|
|
|
get api(path_started, user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to match_response_schema('public_api/v4/project/export_status')
|
|
|
|
expect(json_response['export_status']).to eq('started')
|
|
|
|
end
|
2017-12-05 23:28:12 -05:00
|
|
|
end
|
|
|
|
|
2020-03-12 05:09:55 -04:00
|
|
|
context 'when project export has finished' do
|
|
|
|
it 'returns status finished' do
|
|
|
|
get api(path_finished, user)
|
2018-03-30 11:45:59 -04:00
|
|
|
|
2020-03-12 05:09:55 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to match_response_schema('public_api/v4/project/export_status')
|
|
|
|
expect(json_response['export_status']).to eq('finished')
|
|
|
|
end
|
2018-03-30 11:45:59 -04:00
|
|
|
end
|
|
|
|
|
2020-03-12 05:09:55 -04:00
|
|
|
context 'when project export is being regenerated' do
|
|
|
|
before do
|
|
|
|
create(:project_export_job, project: project_finished, status: 1)
|
|
|
|
end
|
2017-12-05 23:28:12 -05:00
|
|
|
|
2020-03-12 05:09:55 -04:00
|
|
|
it 'returns status regeneration_in_progress' do
|
|
|
|
get api(path_finished, user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response).to match_response_schema('public_api/v4/project/export_status')
|
|
|
|
expect(json_response['export_status']).to eq('regeneration_in_progress')
|
|
|
|
end
|
2017-12-05 23:28:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'when project export is disabled' do
|
|
|
|
let(:request) { get api(path, admin) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project export is enabled' do
|
|
|
|
context 'when user is an admin' do
|
|
|
|
let(:user) { admin }
|
|
|
|
|
|
|
|
it_behaves_like 'get project export status ok'
|
|
|
|
end
|
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
context 'when user is a maintainer' do
|
2017-12-05 23:28:12 -05:00
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
|
|
|
project_none.add_maintainer(user)
|
|
|
|
project_started.add_maintainer(user)
|
|
|
|
project_finished.add_maintainer(user)
|
|
|
|
project_after_export.add_maintainer(user)
|
2017-12-05 23:28:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'get project export status ok'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is a developer' do
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'get project export status denied'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is a reporter' do
|
|
|
|
before do
|
|
|
|
project.add_reporter(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'get project export status denied'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is a guest' do
|
|
|
|
before do
|
|
|
|
project.add_guest(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'get project export status denied'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not a member' do
|
|
|
|
it_behaves_like 'get project export status not found'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:project_id/export/download' do
|
|
|
|
shared_examples_for 'get project export download not found' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(download_path, user) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'get project export download denied' do
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { get api(download_path, user) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'get project export download' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(download_path_none, user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(download_path_started, user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'downloads' do
|
|
|
|
get api(download_path_finished, user)
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-12-05 23:28:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-30 11:45:59 -04:00
|
|
|
shared_examples_for 'get project export upload after action' do
|
|
|
|
context 'and is uploading' do
|
|
|
|
it 'downloads' do
|
|
|
|
get api(download_path_export_action, user)
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-03-30 11:45:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when upload complete' do
|
|
|
|
before do
|
2018-08-29 09:41:56 -04:00
|
|
|
project_after_export.remove_exports
|
|
|
|
end
|
2018-06-25 09:10:26 -04:00
|
|
|
|
2018-08-29 09:41:56 -04:00
|
|
|
it 'has removed the export' do
|
2018-09-07 08:43:51 -04:00
|
|
|
expect(project_after_export.export_file_exists?).to be_falsey
|
2018-03-30 11:45:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { get api(download_path_export_action, user) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'get project download by strategy' do
|
|
|
|
context 'when upload strategy set' do
|
|
|
|
it_behaves_like 'get project export upload after action'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when download strategy set' do
|
|
|
|
it_behaves_like 'get project export download'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-05 23:28:12 -05:00
|
|
|
it_behaves_like 'when project export is disabled' do
|
|
|
|
let(:request) { get api(download_path, admin) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project export is enabled' do
|
|
|
|
context 'when user is an admin' do
|
|
|
|
let(:user) { admin }
|
|
|
|
|
2018-03-30 11:45:59 -04:00
|
|
|
it_behaves_like 'get project download by strategy'
|
2019-12-05 16:07:40 -05:00
|
|
|
|
|
|
|
context 'when rate limit is exceeded' do
|
|
|
|
let(:request) { get api(download_path, admin) }
|
|
|
|
|
2020-05-20 17:09:09 -04:00
|
|
|
before do
|
2020-06-03 08:08:21 -04:00
|
|
|
allow(Gitlab::ApplicationRateLimiter)
|
|
|
|
.to receive(:increment)
|
2020-07-07 11:08:49 -04:00
|
|
|
.and_return(Gitlab::ApplicationRateLimiter.rate_limits[:project_download_export][:threshold].call + 1)
|
2020-05-20 17:09:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'prevents requesting project export' do
|
|
|
|
request
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:too_many_requests)
|
|
|
|
expect(json_response['message']['error']).to eq('This endpoint has been requested too many times. Try again later.')
|
|
|
|
end
|
2019-12-05 16:07:40 -05:00
|
|
|
end
|
2017-12-05 23:28:12 -05:00
|
|
|
end
|
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
context 'when user is a maintainer' do
|
2017-12-05 23:28:12 -05:00
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
|
|
|
project_none.add_maintainer(user)
|
|
|
|
project_started.add_maintainer(user)
|
|
|
|
project_finished.add_maintainer(user)
|
|
|
|
project_after_export.add_maintainer(user)
|
2017-12-05 23:28:12 -05:00
|
|
|
end
|
|
|
|
|
2018-03-30 11:45:59 -04:00
|
|
|
it_behaves_like 'get project download by strategy'
|
2017-12-05 23:28:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is a developer' do
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'get project export download denied'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is a reporter' do
|
|
|
|
before do
|
|
|
|
project.add_reporter(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'get project export download denied'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is a guest' do
|
|
|
|
before do
|
|
|
|
project.add_guest(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'get project export download denied'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not a member' do
|
|
|
|
it_behaves_like 'get project export download not found'
|
|
|
|
end
|
|
|
|
end
|
2018-06-25 09:10:26 -04:00
|
|
|
|
|
|
|
context 'when an uploader is used' do
|
|
|
|
before do
|
|
|
|
stub_uploads_object_storage(ImportExportUploader)
|
|
|
|
|
2018-08-29 09:41:56 -04:00
|
|
|
project.add_maintainer(user)
|
|
|
|
project_finished.add_maintainer(user)
|
|
|
|
project_after_export.add_maintainer(user)
|
2018-06-25 09:10:26 -04:00
|
|
|
|
2018-08-29 09:41:56 -04:00
|
|
|
upload = ImportExportUpload.new(project: project)
|
|
|
|
upload.export_file = fixture_file_upload('spec/fixtures/project_export.tar.gz', "`/tar.gz")
|
|
|
|
upload.save!
|
2018-06-25 09:10:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'get project download by strategy'
|
|
|
|
end
|
2017-12-05 23:28:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /projects/:project_id/export' do
|
|
|
|
shared_examples_for 'post project export start not found' do
|
|
|
|
it_behaves_like '404 response' do
|
|
|
|
let(:request) { post api(path, user) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'post project export start denied' do
|
|
|
|
it_behaves_like '403 response' do
|
|
|
|
let(:request) { post api(path, user) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'post project export start' do
|
2018-03-30 11:45:59 -04:00
|
|
|
context 'with upload strategy' do
|
|
|
|
context 'when params invalid' do
|
|
|
|
it_behaves_like '400 response' do
|
2018-12-17 17:52:17 -05:00
|
|
|
let(:request) { post(api(path, user), params: { 'upload[url]' => 'whatever' }) }
|
2018-03-30 11:45:59 -04:00
|
|
|
end
|
|
|
|
end
|
2017-12-05 23:28:12 -05:00
|
|
|
|
2018-03-30 11:45:59 -04:00
|
|
|
it 'starts' do
|
|
|
|
allow_any_instance_of(Gitlab::ImportExport::AfterExportStrategies::WebUploadStrategy).to receive(:send_file)
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post(api(path, user), params: { 'upload[url]' => 'http://gitlab.com' })
|
2018-03-30 11:45:59 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:accepted)
|
2018-03-30 11:45:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with download strategy' do
|
2020-07-21 20:09:26 -04:00
|
|
|
before do
|
|
|
|
Grape::Endpoint.before_each do |endpoint|
|
|
|
|
allow(endpoint).to receive(:user_project).and_return(project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Grape::Endpoint.before_each nil
|
|
|
|
end
|
|
|
|
|
2018-03-30 11:45:59 -04:00
|
|
|
it 'starts' do
|
|
|
|
expect_any_instance_of(Gitlab::ImportExport::AfterExportStrategies::WebUploadStrategy).not_to receive(:send_file)
|
|
|
|
|
|
|
|
post api(path, user)
|
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:accepted)
|
2018-03-30 11:45:59 -04:00
|
|
|
end
|
2020-07-21 20:09:26 -04:00
|
|
|
|
|
|
|
it 'removes previously exported archive file' do
|
|
|
|
expect(project).to receive(:remove_exports).once
|
|
|
|
|
|
|
|
post api(path, user)
|
|
|
|
end
|
2017-12-05 23:28:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'when project export is disabled' do
|
|
|
|
let(:request) { post api(path, admin) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project export is enabled' do
|
|
|
|
context 'when user is an admin' do
|
|
|
|
let(:user) { admin }
|
|
|
|
|
|
|
|
it_behaves_like 'post project export start'
|
2019-12-05 16:07:40 -05:00
|
|
|
|
2020-05-20 17:09:09 -04:00
|
|
|
context 'when rate limit is exceeded across projects' do
|
2020-06-03 08:08:21 -04:00
|
|
|
before do
|
|
|
|
allow(Gitlab::ApplicationRateLimiter)
|
|
|
|
.to receive(:increment)
|
2020-07-07 11:08:49 -04:00
|
|
|
.and_return(Gitlab::ApplicationRateLimiter.rate_limits[:project_export][:threshold].call + 1)
|
2020-06-03 08:08:21 -04:00
|
|
|
end
|
2020-05-20 17:09:09 -04:00
|
|
|
|
2020-06-03 08:08:21 -04:00
|
|
|
it 'prevents requesting project export' do
|
2020-05-20 17:09:09 -04:00
|
|
|
post api(path, admin)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:too_many_requests)
|
|
|
|
expect(json_response['message']['error']).to eq('This endpoint has been requested too many times. Try again later.')
|
|
|
|
end
|
2019-12-05 16:07:40 -05:00
|
|
|
end
|
2017-12-05 23:28:12 -05:00
|
|
|
end
|
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
context 'when user is a maintainer' do
|
2017-12-05 23:28:12 -05:00
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
|
|
|
project_none.add_maintainer(user)
|
|
|
|
project_started.add_maintainer(user)
|
|
|
|
project_finished.add_maintainer(user)
|
|
|
|
project_after_export.add_maintainer(user)
|
2017-12-05 23:28:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'post project export start'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is a developer' do
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'post project export start denied'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is a reporter' do
|
|
|
|
before do
|
|
|
|
project.add_reporter(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'post project export start denied'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is a guest' do
|
|
|
|
before do
|
|
|
|
project.add_guest(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'post project export start denied'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not a member' do
|
|
|
|
it_behaves_like 'post project export start not found'
|
|
|
|
end
|
2018-03-14 10:17:35 -04:00
|
|
|
|
|
|
|
context 'when overriding description' do
|
2019-10-23 05:06:03 -04:00
|
|
|
it 'starts', :sidekiq_might_not_need_inline do
|
2018-03-14 10:17:35 -04:00
|
|
|
params = { description: "Foo" }
|
|
|
|
|
2020-05-13 08:07:54 -04:00
|
|
|
expect_next_instance_of(Projects::ImportExport::ExportService) do |service|
|
|
|
|
expect(service).to receive(:execute)
|
|
|
|
end
|
2018-12-17 17:52:17 -05:00
|
|
|
post api(path, project.owner), params: params
|
2018-03-14 10:17:35 -04:00
|
|
|
|
2020-02-27 16:09:17 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:accepted)
|
2018-03-14 10:17:35 -04:00
|
|
|
end
|
|
|
|
end
|
2017-12-05 23:28:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|