2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
RSpec.describe API::ImportGithub, feature_category: :importers do
|
2019-03-02 22:35:43 +05:30
|
|
|
let(:token) { "asdasd12345" }
|
|
|
|
let(:provider) { :github }
|
|
|
|
let(:access_params) { { github_access_token: token } }
|
2023-03-17 16:20:25 +05:30
|
|
|
let(:provider_username) { user.username }
|
|
|
|
let(:provider_user) { double('provider', login: provider_username).as_null_object }
|
|
|
|
let(:provider_repo) do
|
|
|
|
{
|
|
|
|
name: 'vim',
|
|
|
|
full_name: "#{provider_username}/vim",
|
|
|
|
owner: double('provider', login: provider_username),
|
|
|
|
description: 'provider',
|
|
|
|
private: false,
|
|
|
|
clone_url: 'https://fake.url/vim.git',
|
|
|
|
has_wiki: true
|
|
|
|
}
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
let(:client) { double('client', user: provider_user, repository: provider_repo) }
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
before do
|
|
|
|
Grape::Endpoint.before_each do |endpoint|
|
|
|
|
allow(endpoint).to receive(:client).and_return(client)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
after do
|
|
|
|
Grape::Endpoint.before_each nil
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "POST /import/github" do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project) { create(:project) }
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2020-07-02 01:45:43 +05:30
|
|
|
it 'rejects requests when Github Importer is disabled' do
|
|
|
|
stub_application_setting(import_sources: nil)
|
|
|
|
|
|
|
|
post api("/import/github", user), params: {
|
|
|
|
target_namespace: user.namespace_path,
|
|
|
|
personal_access_token: token,
|
|
|
|
repo_id: non_existing_record_id
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
it 'returns 201 response when the project is imported successfully' do
|
|
|
|
allow(Gitlab::LegacyGithubImport::ProjectCreator)
|
2022-10-11 01:57:18 +05:30
|
|
|
.to receive(:new).with(provider_repo, provider_repo[:name], user.namespace, user, type: provider, **access_params)
|
2019-03-02 22:35:43 +05:30
|
|
|
.and_return(double(execute: project))
|
|
|
|
|
|
|
|
post api("/import/github", user), params: {
|
|
|
|
target_namespace: user.namespace_path,
|
|
|
|
personal_access_token: token,
|
2020-04-22 19:07:51 +05:30
|
|
|
repo_id: non_existing_record_id
|
2019-03-02 22:35:43 +05:30
|
|
|
}
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2019-03-02 22:35:43 +05:30
|
|
|
expect(json_response).to be_a Hash
|
|
|
|
expect(json_response['name']).to eq(project.name)
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it 'returns 201 response when the project is imported successfully from GHE' do
|
|
|
|
allow(Gitlab::LegacyGithubImport::ProjectCreator)
|
2022-10-11 01:57:18 +05:30
|
|
|
.to receive(:new).with(provider_repo, provider_repo[:name], user.namespace, user, type: provider, **access_params)
|
2021-01-29 00:20:46 +05:30
|
|
|
.and_return(double(execute: project))
|
|
|
|
|
|
|
|
post api("/import/github", user), params: {
|
|
|
|
target_namespace: user.namespace_path,
|
|
|
|
personal_access_token: token,
|
|
|
|
repo_id: non_existing_record_id,
|
2022-11-25 23:54:43 +05:30
|
|
|
github_hostname: "https://github.somecompany.com/",
|
|
|
|
optional_stages: { attachments_import: true }
|
2021-01-29 00:20:46 +05:30
|
|
|
}
|
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
|
|
|
expect(json_response).to be_a Hash
|
|
|
|
expect(json_response['name']).to eq(project.name)
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
it 'returns 422 response when user can not create projects in the chosen namespace' do
|
|
|
|
other_namespace = create(:group, name: 'other_namespace')
|
|
|
|
|
|
|
|
post api("/import/github", user), params: {
|
|
|
|
target_namespace: other_namespace.name,
|
|
|
|
personal_access_token: token,
|
2020-04-22 19:07:51 +05:30
|
|
|
repo_id: non_existing_record_id
|
2019-03-02 22:35:43 +05:30
|
|
|
}
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
context 'when target_namespace is blank' do
|
|
|
|
it 'returns 400 response' do
|
|
|
|
allow(Gitlab::LegacyGithubImport::ProjectCreator)
|
|
|
|
.to receive(:new).with(provider_repo, provider_repo[:name], user.namespace, user, type: provider, **access_params)
|
|
|
|
.and_return(double(execute: project))
|
|
|
|
|
|
|
|
post api("/import/github", user), params: {
|
|
|
|
target_namespace: '',
|
|
|
|
personal_access_token: token,
|
|
|
|
repo_id: non_existing_record_id
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
expect(json_response['error']).to eq 'target_namespace is empty'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
context 'when unauthenticated user' do
|
|
|
|
it 'returns 403 response' do
|
|
|
|
post api("/import/github"), params: {
|
|
|
|
target_namespace: user.namespace_path,
|
|
|
|
personal_access_token: token,
|
|
|
|
repo_id: non_existing_record_id
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
2022-11-25 23:54:43 +05:30
|
|
|
|
|
|
|
describe "POST /import/github/cancel" do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project) { create(:project, :import_started, import_type: 'github', import_url: 'https://fake.url') }
|
|
|
|
|
|
|
|
context 'when project import was canceled' do
|
|
|
|
before do
|
|
|
|
allow(Import::Github::CancelProjectImportService)
|
|
|
|
.to receive(:new).with(project, user)
|
|
|
|
.and_return(double(execute: { status: :success, project: project }))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns success' do
|
|
|
|
post api("/import/github/cancel", user), params: {
|
|
|
|
project_id: project.id
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when project import was not canceled' do
|
|
|
|
before do
|
|
|
|
allow(Import::Github::CancelProjectImportService)
|
|
|
|
.to receive(:new).with(project, user)
|
|
|
|
.and_return(double(execute: { status: :error, message: 'The import cannot be canceled because it is finished', http_status: :bad_request }))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns error' do
|
|
|
|
post api("/import/github/cancel", user), params: {
|
|
|
|
project_id: project.id
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
expect(json_response['message']).to eq('The import cannot be canceled because it is finished')
|
|
|
|
end
|
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
context 'when unauthenticated user' do
|
|
|
|
it 'returns 403 response' do
|
|
|
|
post api("/import/github/cancel"), params: {
|
|
|
|
project_id: project.id
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
|
|
|
|
describe 'POST /import/github/gists' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let(:params) { { personal_access_token: token } }
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
context 'when gists import was started' do
|
2023-03-17 16:20:25 +05:30
|
|
|
before do
|
2023-06-20 00:43:36 +05:30
|
|
|
allow(Import::Github::GistsImportService)
|
|
|
|
.to receive(:new).with(user, client, access_params)
|
|
|
|
.and_return(double(execute: { status: :success }))
|
2023-03-17 16:20:25 +05:30
|
|
|
end
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
it 'returns 202' do
|
|
|
|
post api('/import/github/gists', user), params: params
|
2023-03-17 16:20:25 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:accepted)
|
2023-03-17 16:20:25 +05:30
|
|
|
end
|
2023-06-20 00:43:36 +05:30
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
context 'when gists import is in progress' do
|
|
|
|
before do
|
|
|
|
allow(Import::Github::GistsImportService)
|
|
|
|
.to receive(:new).with(user, client, access_params)
|
|
|
|
.and_return(double(execute: { status: :error, message: 'Import already in progress', http_status: :unprocessable_entity }))
|
2023-03-17 16:20:25 +05:30
|
|
|
end
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
it 'returns 422 error' do
|
|
|
|
post api('/import/github/gists', user), params: params
|
2023-03-17 16:20:25 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unprocessable_entity)
|
|
|
|
expect(json_response['errors']).to eq('Import already in progress')
|
2023-03-17 16:20:25 +05:30
|
|
|
end
|
2023-06-20 00:43:36 +05:30
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
context 'when unauthenticated user' do
|
|
|
|
it 'returns 403 error' do
|
|
|
|
post api('/import/github/gists'), params: params
|
2023-03-17 16:20:25 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2023-03-17 16:20:25 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
context 'when rate limit reached' do
|
2023-03-17 16:20:25 +05:30
|
|
|
before do
|
2023-06-20 00:43:36 +05:30
|
|
|
allow(Import::Github::GistsImportService)
|
|
|
|
.to receive(:new).with(user, client, access_params)
|
|
|
|
.and_raise(Gitlab::GithubImport::RateLimitError)
|
2023-03-17 16:20:25 +05:30
|
|
|
end
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
it 'returns 429 error' do
|
2023-03-17 16:20:25 +05:30
|
|
|
post api('/import/github/gists', user), params: params
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:too_many_requests)
|
2023-03-17 16:20:25 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|