2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe API::ImportGithub do
|
2019-03-02 22:35:43 +05:30
|
|
|
let(:token) { "asdasd12345" }
|
|
|
|
let(:provider) { :github }
|
|
|
|
let(:access_params) { { github_access_token: token } }
|
|
|
|
|
|
|
|
describe "POST /import/github" do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:provider_username) { user.username }
|
2022-01-26 12:08:38 +05:30
|
|
|
let(:provider_user) { double('provider', login: provider_username) }
|
2019-03-02 22:35:43 +05:30
|
|
|
let(:provider_repo) do
|
2022-01-26 12:08:38 +05:30
|
|
|
double('provider',
|
2019-03-02 22:35:43 +05:30
|
|
|
name: 'vim',
|
|
|
|
full_name: "#{provider_username}/vim",
|
2022-01-26 12:08:38 +05:30
|
|
|
owner: double('provider', login: provider_username)
|
2019-03-02 22:35:43 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
Grape::Endpoint.before_each do |endpoint|
|
2020-10-24 23:57:45 +05:30
|
|
|
allow(endpoint).to receive(:client).and_return(double('client', user: provider_user, repository: provider_repo).as_null_object)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
after do
|
|
|
|
Grape::Endpoint.before_each nil
|
|
|
|
end
|
|
|
|
|
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)
|
2021-02-22 17:27:13 +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)
|
2021-02-22 17:27:13 +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,
|
|
|
|
github_hostname: "https://github.somecompany.com/"
|
|
|
|
}
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|