2020-04-22 19:07:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
RSpec.describe Import::GitlabProjectsController, feature_category: :importers do
|
2020-04-22 19:07:51 +05:30
|
|
|
include WorkhorseHelpers
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
include_context 'workhorse headers'
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
let_it_be(:namespace) { create(:namespace) }
|
2022-04-04 11:22:00 +05:30
|
|
|
let_it_be(:user) { namespace.first_owner }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
login_as(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST create' do
|
|
|
|
subject { upload_archive(file_upload, workhorse_headers, params) }
|
|
|
|
|
|
|
|
let(:file) { File.join('spec', 'features', 'projects', 'import_export', 'test_project_export.tar.gz') }
|
|
|
|
let(:file_upload) { fixture_file_upload(file) }
|
|
|
|
let(:params) { { namespace_id: namespace.id, path: 'test' } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(ImportExportUploader).to receive(:workhorse_upload_path).and_return('/')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a valid path' do
|
|
|
|
it 'schedules an import and redirects to the new project path' do
|
|
|
|
stub_import(namespace)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(flash[:notice]).to include('is being imported')
|
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an invalid path' do
|
|
|
|
['/test', '../test'].each do |invalid_path|
|
|
|
|
it "redirects with an error when path is `#{invalid_path}`" do
|
|
|
|
params[:path] = invalid_path
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(flash[:alert]).to start_with('Project could not be imported')
|
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(flash[:alert]).to eq('This endpoint has been requested too many times. Try again later.')
|
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_archive(file, headers = {}, params = {})
|
|
|
|
workhorse_finalize(
|
|
|
|
import_gitlab_project_path,
|
|
|
|
method: :post,
|
|
|
|
file_key: :file,
|
|
|
|
params: params.merge(file: file),
|
|
|
|
headers: headers,
|
|
|
|
send_rewritten_field: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_import(namespace)
|
|
|
|
expect_any_instance_of(ProjectImportState).to receive(:schedule)
|
|
|
|
expect(::Projects::CreateService)
|
|
|
|
.to receive(:new)
|
|
|
|
.with(user, instance_of(ActionController::Parameters))
|
|
|
|
.and_call_original
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST authorize' do
|
2021-02-22 17:27:13 +05:30
|
|
|
it_behaves_like 'handle uploads authorize request' do
|
|
|
|
let(:uploader_class) { ImportExportUploader }
|
|
|
|
let(:maximum_size) { Gitlab::CurrentSettings.max_import_size.megabytes }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
subject { post authorize_import_gitlab_project_path, headers: workhorse_headers }
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
2023-05-27 22:25:52 +05:30
|
|
|
|
|
|
|
describe 'GET new' do
|
|
|
|
context 'when the user is not allowed to import projects' do
|
|
|
|
let!(:group) { create(:group).tap { |group| group.add_developer(user) } }
|
|
|
|
|
|
|
|
it 'returns 404' do
|
|
|
|
get new_import_gitlab_project_path, params: { namespace_id: group.id }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|