debian-mirror-gitlab/spec/requests/api/project_export_spec.rb

385 lines
11 KiB
Ruby
Raw Normal View History

2018-03-27 19:54:05 +05:30
require 'spec_helper'
describe API::ProjectExport do
set(:project) { create(:project) }
set(:project_none) { create(:project) }
set(: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) }
2018-03-27 19:54:05 +05:30
set(:user) { create(:user) }
set(:admin) { create(:admin) }
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
allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path)
# simulate exporting work directory
FileUtils.mkdir_p File.join(project_started.export_path, 'securerandom-hex')
2018-05-09 12:01:36 +05:30
# simulate in after export action
FileUtils.touch Gitlab::ImportExport::AfterExportStrategies::BaseAfterExportStrategy.lock_file_path(project_after_export)
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
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)
expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('public_api/v4/project/export_status')
expect(json_response['export_status']).to eq('none')
end
it 'is started' do
get api(path_started, user)
expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('public_api/v4/project/export_status')
expect(json_response['export_status']).to eq('started')
end
2018-05-09 12:01:36 +05:30
it 'is after_export' do
get api(path_after_export, user)
expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('public_api/v4/project/export_status')
expect(json_response['export_status']).to eq('after_export_action')
end
2018-03-27 19:54:05 +05:30
it 'is finished' do
get api(path_finished, user)
expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('public_api/v4/project/export_status')
expect(json_response['export_status']).to eq('finished')
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-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
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)
expect(response).to have_gitlab_http_status(200)
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
get api(download_path_export_action, user)
expect(response).to have_gitlab_http_status(200)
end
end
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
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
2018-03-27 19:54:05 +05:30
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-05-09 12:01:36 +05:30
it_behaves_like 'get project download by strategy'
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
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-05-09 12:01:36 +05:30
context 'with upload strategy' do
context 'when params invalid' do
it_behaves_like '400 response' do
2019-02-13 22:33:31 +05:30
let(:request) { post(api(path, user), 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)
2019-02-13 22:33:31 +05:30
post(api(path, user), params: { 'upload[url]' => 'http://gitlab.com' })
2018-03-27 19:54:05 +05:30
2018-05-09 12:01:36 +05:30
expect(response).to have_gitlab_http_status(202)
end
end
context 'with download strategy' do
it 'starts' do
expect_any_instance_of(Gitlab::ImportExport::AfterExportStrategies::WebUploadStrategy).not_to receive(:send_file)
post api(path, user)
expect(response).to have_gitlab_http_status(202)
end
2018-03-27 19:54:05 +05:30
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'
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
it 'starts' do
params = { description: "Foo" }
expect_any_instance_of(Projects::ImportExport::ExportService).to receive(:execute)
2019-02-13 22:33:31 +05:30
post api(path, project.owner), params: params
2018-05-09 12:01:36 +05:30
expect(response).to have_gitlab_http_status(202)
end
end
2018-03-27 19:54:05 +05:30
end
end
end