2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
RSpec.describe API::ProjectExport, :aggregate_failures, :clean_gitlab_redis_cache, feature_category: :importers do
|
2020-03-13 15:44:24 +05:30
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:project_none) { create(:project) }
|
|
|
|
let_it_be(:project_started) { create(:project) }
|
2018-11-20 20:47:30 +05:30
|
|
|
let(:project_finished) { create(:project, :with_export) }
|
|
|
|
let(:project_after_export) { create(:project, :with_export) }
|
2020-03-13 15:44:24 +05:30
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:admin) { create(:admin) }
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
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-05-09 12:01:36 +05:30
|
|
|
let(:path_after_export) { "/projects/#{project_after_export.id}/export" }
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
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-05-09 12:01:36 +05:30
|
|
|
let(:download_path_export_action) { "/projects/#{project_after_export.id}/export/download" }
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
let(:export_path) { "#{Dir.tmpdir}/project_export_spec" }
|
|
|
|
|
|
|
|
before do
|
2021-01-29 00:20:46 +05:30
|
|
|
allow(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path)
|
2020-04-08 14:13:33 +05:30
|
|
|
allow_next_instance_of(ProjectExportWorker) do |job|
|
|
|
|
allow(job).to receive(:jid).and_return(SecureRandom.hex(8))
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
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
|
2023-06-20 00:43:36 +05:30
|
|
|
it_behaves_like 'GET request permissions for admin mode' do
|
|
|
|
let(:failed_status_code) { :not_found }
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
shared_examples_for 'get project export status not found' do
|
|
|
|
it_behaves_like '404 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(path, user) }
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'get project export status denied' do
|
|
|
|
it_behaves_like '403 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(path, user) }
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'get project export status ok' do
|
2023-06-20 00:43:36 +05:30
|
|
|
let_it_be(:admin_mode) { false }
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
it 'is none' do
|
2023-06-20 00:43:36 +05:30
|
|
|
get api(path_none, user, admin_mode: admin_mode)
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-03-27 19:54:05 +05:30
|
|
|
expect(response).to match_response_schema('public_api/v4/project/export_status')
|
|
|
|
expect(json_response['export_status']).to eq('none')
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'when project export has started' do
|
|
|
|
before do
|
|
|
|
create(:project_export_job, project: project_started, status: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns status started' do
|
2023-06-20 00:43:36 +05:30
|
|
|
get api(path_started, user, admin_mode: admin_mode)
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
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
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'when project export has finished' do
|
|
|
|
it 'returns status finished' do
|
2023-06-20 00:43:36 +05:30
|
|
|
get api(path_finished, user, admin_mode: admin_mode)
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
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-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'when project export is being regenerated' do
|
|
|
|
before do
|
|
|
|
create(:project_export_job, project: project_finished, status: 1)
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it 'returns status regeneration_in_progress' do
|
2023-06-20 00:43:36 +05:30
|
|
|
get api(path_finished, user, admin_mode: admin_mode)
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
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
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'when project export is disabled' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(path, admin, admin_mode: true) }
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project export is enabled' do
|
|
|
|
context 'when user is an admin' do
|
|
|
|
let(:user) { admin }
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
it_behaves_like 'get project export status ok' do
|
|
|
|
let(:admin_mode) { true }
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
context 'when user is a maintainer' do
|
2018-03-27 19:54:05 +05:30
|
|
|
before do
|
2018-11-18 11:00:15 +05:30
|
|
|
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)
|
2018-03-27 19:54:05 +05:30
|
|
|
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
|
2023-06-20 00:43:36 +05:30
|
|
|
it_behaves_like 'GET request permissions for admin mode' do
|
|
|
|
let(:path) { download_path_finished }
|
|
|
|
let(:failed_status_code) { :not_found }
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
shared_examples_for 'get project export download not found' do
|
|
|
|
it_behaves_like '404 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(download_path, user) }
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'get project export download denied' do
|
|
|
|
it_behaves_like '403 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(download_path, user) }
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'get project export download' do
|
|
|
|
it_behaves_like '404 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(download_path_none, user, admin_mode: admin_mode) }
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '404 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(download_path_started, user, admin_mode: admin_mode) }
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'downloads' do
|
2023-06-20 00:43:36 +05:30
|
|
|
get api(download_path_finished, user, admin_mode: admin_mode)
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
shared_examples_for 'get project export upload after action' do
|
|
|
|
context 'and is uploading' do
|
|
|
|
it 'downloads' do
|
2023-06-20 00:43:36 +05:30
|
|
|
get api(download_path_export_action, user, admin_mode: admin_mode)
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
context 'when export object is not present' do
|
|
|
|
before do
|
|
|
|
project_after_export.export_file.file.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 404' do
|
2023-06-20 00:43:36 +05:30
|
|
|
get api(download_path_export_action, user, admin_mode: admin_mode)
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
expect(json_response['message']).to eq('The project export file is not available yet')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
context 'when upload complete' do
|
|
|
|
before do
|
2018-11-20 20:47:30 +05:30
|
|
|
project_after_export.remove_exports
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
it 'has removed the export' do
|
|
|
|
expect(project_after_export.export_file_exists?).to be_falsey
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '404 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(download_path_export_action, user, admin_mode: admin_mode) }
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'get project download by strategy' do
|
2023-06-20 00:43:36 +05:30
|
|
|
let_it_be(:admin_mode) { false }
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
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
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
it_behaves_like 'when project export is disabled' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(download_path, admin, admin_mode: true) }
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project export is enabled' do
|
|
|
|
context 'when user is an admin' do
|
|
|
|
let(:user) { admin }
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
it_behaves_like 'get project download by strategy' do
|
|
|
|
let(:admin_mode) { true }
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
context 'when rate limit is exceeded' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(download_path, admin, admin_mode: true) }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
before do
|
2022-08-13 15:12:31 +05:30
|
|
|
allow_next_instance_of(Gitlab::ApplicationRateLimiter::BaseStrategy) do |strategy|
|
|
|
|
threshold = Gitlab::ApplicationRateLimiter.rate_limits[:project_download_export][:threshold].call
|
|
|
|
allow(strategy).to receive(:increment).and_return(threshold + 1)
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
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
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
context 'applies correct scope when throttling' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(project_download_export_limit: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'throttles downloads within same namespaces' do
|
|
|
|
# simulate prior request to the same namespace, which increments the rate limit counter for that scope
|
|
|
|
Gitlab::ApplicationRateLimiter.throttled?(:project_download_export, scope: [user, project_finished.namespace])
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
get api(download_path_finished, user, admin_mode: true)
|
2022-06-21 17:19:12 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:too_many_requests)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows downloads from different namespaces' do
|
|
|
|
# simulate prior request to a different namespace, which increments the rate limit counter for that scope
|
|
|
|
Gitlab::ApplicationRateLimiter.throttled?(:project_download_export,
|
|
|
|
scope: [user, create(:project, :with_export).namespace])
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
get api(download_path_finished, user, admin_mode: true)
|
2022-06-21 17:19:12 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
context 'when user is a maintainer' do
|
2018-03-27 19:54:05 +05:30
|
|
|
before do
|
2018-11-18 11:00:15 +05:30
|
|
|
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)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
it_behaves_like 'get project download by strategy'
|
2018-03-27 19:54:05 +05:30
|
|
|
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-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
context 'when an uploader is used' do
|
|
|
|
before do
|
|
|
|
stub_uploads_object_storage(ImportExportUploader)
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
project.add_maintainer(user)
|
|
|
|
project_finished.add_maintainer(user)
|
|
|
|
project_after_export.add_maintainer(user)
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
upload = ImportExportUpload.new(project: project)
|
|
|
|
upload.export_file = fixture_file_upload('spec/fixtures/project_export.tar.gz', "`/tar.gz")
|
|
|
|
upload.save!
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'get project download by strategy'
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /projects/:project_id/export' do
|
2023-06-20 00:43:36 +05:30
|
|
|
let(:admin_mode) { false }
|
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
it_behaves_like 'POST request permissions for admin mode' do
|
|
|
|
let(:params) { { 'upload[url]' => 'http://gitlab.com' } }
|
|
|
|
let(:failed_status_code) { :not_found }
|
|
|
|
let(:success_status_code) { :accepted }
|
|
|
|
end
|
|
|
|
|
|
|
|
subject(:request) { post api(path, user, admin_mode: admin_mode), params: params }
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
shared_examples_for 'post project export start not found' do
|
2023-06-20 00:43:36 +05:30
|
|
|
it_behaves_like '404 response'
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'post project export start denied' do
|
2023-06-20 00:43:36 +05:30
|
|
|
it_behaves_like '403 response'
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'post project export start' do
|
2023-06-20 00:43:36 +05:30
|
|
|
let_it_be(:admin_mode) { false }
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
context 'with upload strategy' do
|
|
|
|
context 'when params invalid' do
|
|
|
|
it_behaves_like '400 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
let(:params) { { 'upload[url]' => 'whatever' } }
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'starts' do
|
|
|
|
allow_any_instance_of(Gitlab::ImportExport::AfterExportStrategies::WebUploadStrategy).to receive(:send_file)
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
request do
|
|
|
|
let(:params) { { 'upload[url]' => 'http://gitlab.com' } }
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:accepted)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with download strategy' do
|
2020-10-24 23:57:45 +05:30
|
|
|
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-05-09 12:01:36 +05:30
|
|
|
it 'starts' do
|
|
|
|
expect_any_instance_of(Gitlab::ImportExport::AfterExportStrategies::WebUploadStrategy).not_to receive(:send_file)
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
request
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:accepted)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
it 'removes previously exported archive file' do
|
|
|
|
expect(project).to receive(:remove_exports).once
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
request
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
it_behaves_like 'when project export is disabled'
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
context 'when project export is enabled' do
|
|
|
|
context 'when user is an admin' do
|
|
|
|
let(:user) { admin }
|
2023-06-20 00:43:36 +05:30
|
|
|
let(:admin_mode) { true }
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
it_behaves_like 'post project export start' do
|
|
|
|
let(:admin_mode) { true }
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
context 'with project export size limit' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(max_export_size: 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'starts if limit not exceeded' do
|
2023-06-20 00:43:36 +05:30
|
|
|
request
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:accepted)
|
|
|
|
end
|
|
|
|
|
|
|
|
it '400 response if limit exceeded' do
|
|
|
|
project.statistics.update!(lfs_objects_size: 2.megabytes, repository_size: 2.megabytes)
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
request
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
expect(json_response["message"]).to include('The project size exceeds the export limit.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
context 'when rate limit is exceeded across projects' do
|
|
|
|
before do
|
2022-08-13 15:12:31 +05:30
|
|
|
allow_next_instance_of(Gitlab::ApplicationRateLimiter::BaseStrategy) do |strategy|
|
|
|
|
threshold = Gitlab::ApplicationRateLimiter.rate_limits[:project_export][:threshold].call
|
|
|
|
allow(strategy).to receive(:increment).and_return(threshold + 1)
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'prevents requesting project export' do
|
2023-06-20 00:43:36 +05:30
|
|
|
request
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
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
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
context 'when user is a maintainer' do
|
2018-03-27 19:54:05 +05:30
|
|
|
before do
|
2018-11-18 11:00:15 +05:30
|
|
|
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)
|
2018-03-27 19:54:05 +05:30
|
|
|
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-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
context 'when overriding description' do
|
2019-12-26 22:10:19 +05:30
|
|
|
it 'starts', :sidekiq_might_not_need_inline do
|
2018-05-09 12:01:36 +05:30
|
|
|
params = { description: "Foo" }
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
expect_next_instance_of(Projects::ImportExport::ExportService) do |service|
|
|
|
|
expect(service).to receive(:execute)
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
post api(path, project.first_owner), params: params
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:accepted)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
2021-11-18 22:05:49 +05:30
|
|
|
|
|
|
|
describe 'export relations' do
|
|
|
|
let(:relation) { 'labels' }
|
|
|
|
let(:download_path) { "/projects/#{project.id}/export_relations/download?relation=#{relation}" }
|
|
|
|
let(:path) { "/projects/#{project.id}/export_relations" }
|
|
|
|
|
|
|
|
let_it_be(:status_path) { "/projects/#{project.id}/export_relations/status" }
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
before do
|
|
|
|
stub_application_setting(bulk_import_enabled: true)
|
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
context 'when user is a maintainer' do
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /projects/:id/export_relations' do
|
|
|
|
it 'accepts the request' do
|
|
|
|
post api(path, user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:accepted)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when response is not success' do
|
|
|
|
it 'returns api error' do
|
|
|
|
allow_next_instance_of(BulkImports::ExportService) do |service|
|
|
|
|
allow(service).to receive(:execute).and_return(ServiceResponse.error(message: 'error', http_status: :error))
|
|
|
|
end
|
|
|
|
|
|
|
|
post api(path, user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/export_relations/download' do
|
|
|
|
let_it_be(:export) { create(:bulk_import_export, project: project, relation: 'labels') }
|
|
|
|
let_it_be(:upload) { create(:bulk_import_export_upload, export: export) }
|
|
|
|
|
|
|
|
context 'when export file exists' do
|
|
|
|
it 'downloads exported project relation archive' do
|
|
|
|
upload.update!(export_file: fixture_file_upload('spec/fixtures/bulk_imports/gz/labels.ndjson.gz'))
|
|
|
|
|
|
|
|
get api(download_path, user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(response.header['Content-Disposition']).to eq("attachment; filename=\"labels.ndjson.gz\"; filename*=UTF-8''labels.ndjson.gz")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when relation is not portable' do
|
|
|
|
let(:relation) { ::BulkImports::FileTransfer::ProjectConfig.new(project).skipped_relations.first }
|
|
|
|
|
|
|
|
it_behaves_like '400 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(download_path, user) }
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when export file does not exist' do
|
|
|
|
it 'returns 404' do
|
|
|
|
allow(upload).to receive(:export_file).and_return(nil)
|
|
|
|
|
|
|
|
get api(download_path, user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/export_relations/status' do
|
|
|
|
it 'returns a list of relation export statuses' do
|
|
|
|
create(:bulk_import_export, :started, project: project, relation: 'labels')
|
|
|
|
create(:bulk_import_export, :finished, project: project, relation: 'milestones')
|
|
|
|
create(:bulk_import_export, :failed, project: project, relation: 'project_badges')
|
|
|
|
|
|
|
|
get api(status_path, user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
expect(json_response.pluck('relation')).to contain_exactly('labels', 'milestones', 'project_badges')
|
|
|
|
expect(json_response.pluck('status')).to contain_exactly(-1, 0, 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
context 'with bulk_import is disabled' do
|
2021-11-18 22:05:49 +05:30
|
|
|
before do
|
2023-03-17 16:20:25 +05:30
|
|
|
stub_application_setting(bulk_import_enabled: false)
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /projects/:id/export_relations' do
|
|
|
|
it_behaves_like '404 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { post api(path, user) }
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/export_relations/download' do
|
|
|
|
let_it_be(:export) { create(:bulk_import_export, project: project, relation: 'labels') }
|
|
|
|
let_it_be(:upload) { create(:bulk_import_export_upload, export: export) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
upload.update!(export_file: fixture_file_upload('spec/fixtures/bulk_imports/gz/labels.ndjson.gz'))
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like '404 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { post api(path, user) }
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/export_relations/status' do
|
|
|
|
it_behaves_like '404 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(status_path, user) }
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is a developer' do
|
|
|
|
let_it_be(:developer) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(developer)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /projects/:id/export_relations' do
|
|
|
|
it_behaves_like '403 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { post api(path, developer) }
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/export_relations/download' do
|
|
|
|
it_behaves_like '403 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(download_path, developer) }
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /projects/:id/export_relations/status' do
|
|
|
|
it_behaves_like '403 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(status_path, developer) }
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
|
|
|
|
context 'when bulk import is disabled' do
|
|
|
|
it_behaves_like '404 response' do
|
2023-06-20 00:43:36 +05:30
|
|
|
subject(:request) { get api(path, user) }
|
2023-03-17 16:20:25 +05:30
|
|
|
end
|
|
|
|
end
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|