2020-05-28 23:08:11 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Import::GithubService do
|
2020-05-28 23:08:11 -04:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:token) { 'complex-token' }
|
|
|
|
let_it_be(:access_params) { { github_access_token: 'github-complex-token' } }
|
|
|
|
let_it_be(:params) { { repo_id: 123, new_name: 'new_repo', target_namespace: 'root' } }
|
|
|
|
|
2021-12-09 01:11:13 -05:00
|
|
|
subject(:github_importer) { described_class.new(client, user, params) }
|
2020-05-28 23:08:11 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
allow(subject).to receive(:authorized?).and_return(true)
|
|
|
|
end
|
|
|
|
|
2020-08-11 08:09:55 -04:00
|
|
|
shared_examples 'handles errors' do |klass|
|
|
|
|
let(:client) { klass.new(token) }
|
2020-05-28 23:08:11 -04:00
|
|
|
|
2020-08-11 08:09:55 -04:00
|
|
|
context 'do not raise an exception on input error' do
|
|
|
|
let(:exception) { Octokit::ClientError.new(status: 404, body: 'Not Found') }
|
|
|
|
|
|
|
|
before do
|
|
|
|
expect(client).to receive(:repository).and_raise(exception)
|
|
|
|
end
|
2020-05-28 23:08:11 -04:00
|
|
|
|
2020-08-11 08:09:55 -04:00
|
|
|
it 'logs the original error' do
|
|
|
|
expect(Gitlab::Import::Logger).to receive(:error).with({
|
|
|
|
message: 'Import failed due to a GitHub error',
|
|
|
|
status: 404,
|
|
|
|
error: 'Not Found'
|
|
|
|
}).and_call_original
|
2020-05-28 23:08:11 -04:00
|
|
|
|
2020-08-11 08:09:55 -04:00
|
|
|
subject.execute(access_params, :github)
|
|
|
|
end
|
|
|
|
|
2022-03-29 17:08:04 -04:00
|
|
|
it 'returns an error with message and code' do
|
2020-08-11 08:09:55 -04:00
|
|
|
result = subject.execute(access_params, :github)
|
|
|
|
|
|
|
|
expect(result).to include(
|
2022-03-29 17:08:04 -04:00
|
|
|
message: 'Import failed due to a GitHub error: Not Found (HTTP 404)',
|
2020-08-11 08:09:55 -04:00
|
|
|
status: :error,
|
|
|
|
http_status: :unprocessable_entity
|
|
|
|
)
|
|
|
|
end
|
2020-05-28 23:08:11 -04:00
|
|
|
end
|
|
|
|
|
2020-08-11 08:09:55 -04:00
|
|
|
it 'raises an exception for unknown error causes' do
|
|
|
|
exception = StandardError.new('Not Implemented')
|
|
|
|
|
|
|
|
expect(client).to receive(:repository).and_raise(exception)
|
2020-05-28 23:08:11 -04:00
|
|
|
|
2020-08-11 08:09:55 -04:00
|
|
|
expect(Gitlab::Import::Logger).not_to receive(:error)
|
|
|
|
|
|
|
|
expect { subject.execute(access_params, :github) }.to raise_error(exception)
|
2020-05-28 23:08:11 -04:00
|
|
|
end
|
2021-02-23 01:11:16 -05:00
|
|
|
|
|
|
|
context 'repository size validation' do
|
|
|
|
let(:repository_double) { double(name: 'repository', size: 99) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
expect(client).to receive(:repository).and_return(repository_double)
|
|
|
|
|
|
|
|
allow_next_instance_of(Gitlab::LegacyGithubImport::ProjectCreator) do |creator|
|
|
|
|
allow(creator).to receive(:execute).and_return(double(persisted?: true))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no repository size limit defined' do
|
|
|
|
it 'skips the check and succeeds' do
|
|
|
|
expect(subject.execute(access_params, :github)).to include(status: :success)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the target namespace repository size limit is defined' do
|
|
|
|
let_it_be(:group) { create(:group, repository_size_limit: 100) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
params[:target_namespace] = group.full_path
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'succeeds when the repository is smaller than the limit' do
|
|
|
|
expect(subject.execute(access_params, :github)).to include(status: :success)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns error when the repository is larger than the limit' do
|
|
|
|
allow(repository_double).to receive(:size).and_return(101)
|
|
|
|
|
|
|
|
expect(subject.execute(access_params, :github)).to include(size_limit_error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when target namespace repository limit is not defined' do
|
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_application_setting(repository_size_limit: 100)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when application size limit is defined' do
|
|
|
|
it 'succeeds when the repository is smaller than the limit' do
|
|
|
|
expect(subject.execute(access_params, :github)).to include(status: :success)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns error when the repository is larger than the limit' do
|
|
|
|
allow(repository_double).to receive(:size).and_return(101)
|
|
|
|
|
|
|
|
expect(subject.execute(access_params, :github)).to include(size_limit_error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-12-09 01:11:13 -05:00
|
|
|
|
|
|
|
context 'when a blocked/local URL is used as github_hostname' do
|
|
|
|
let(:message) { 'Error while attempting to import from GitHub' }
|
|
|
|
let(:error) { "Invalid URL: #{url}" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_application_setting(allow_local_requests_from_web_hooks_and_services: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
where(url: %w[https://localhost https://10.0.0.1])
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it 'returns and logs an error' do
|
|
|
|
allow(github_importer).to receive(:url).and_return(url)
|
|
|
|
|
|
|
|
expect(Gitlab::Import::Logger).to receive(:error).with({
|
|
|
|
message: message,
|
|
|
|
error: error
|
|
|
|
}).and_call_original
|
|
|
|
expect(github_importer.execute(access_params, :github)).to include(blocked_url_error(url))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-28 23:08:11 -04:00
|
|
|
end
|
|
|
|
|
2020-08-11 08:09:55 -04:00
|
|
|
context 'when remove_legacy_github_client feature flag is enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(remove_legacy_github_client: true)
|
|
|
|
end
|
2020-05-28 23:08:11 -04:00
|
|
|
|
2020-08-11 08:09:55 -04:00
|
|
|
include_examples 'handles errors', Gitlab::GithubImport::Client
|
|
|
|
end
|
2020-05-28 23:08:11 -04:00
|
|
|
|
2020-08-11 08:09:55 -04:00
|
|
|
context 'when remove_legacy_github_client feature flag is enabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(remove_legacy_github_client: false)
|
|
|
|
end
|
2020-05-28 23:08:11 -04:00
|
|
|
|
2020-08-11 08:09:55 -04:00
|
|
|
include_examples 'handles errors', Gitlab::LegacyGithubImport::Client
|
2020-05-28 23:08:11 -04:00
|
|
|
end
|
2021-02-23 01:11:16 -05:00
|
|
|
|
|
|
|
def size_limit_error
|
|
|
|
{
|
|
|
|
status: :error,
|
|
|
|
http_status: :unprocessable_entity,
|
|
|
|
message: '"repository" size (101 Bytes) is larger than the limit of 100 Bytes.'
|
|
|
|
}
|
|
|
|
end
|
2021-12-09 01:11:13 -05:00
|
|
|
|
|
|
|
def blocked_url_error(url)
|
|
|
|
{
|
|
|
|
status: :error,
|
|
|
|
http_status: :bad_request,
|
|
|
|
message: "Invalid URL: #{url}"
|
|
|
|
}
|
|
|
|
end
|
2020-05-28 23:08:11 -04:00
|
|
|
end
|