2021-01-03 14:25:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
RSpec.describe BulkImports::Clients::HTTP do
|
2021-01-03 14:25:43 +05:30
|
|
|
include ImportSpecHelper
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
let(:url) { 'http://gitlab.example' }
|
2021-01-03 14:25:43 +05:30
|
|
|
let(:token) { 'token' }
|
|
|
|
let(:resource) { 'resource' }
|
2021-09-30 23:02:18 +05:30
|
|
|
let(:version) { "#{BulkImport::MINIMUM_GITLAB_MAJOR_VERSION}.0.0" }
|
2021-06-08 01:23:25 +05:30
|
|
|
let(:response_double) { double(code: 200, success?: true, parsed_response: {}) }
|
2021-09-30 23:02:18 +05:30
|
|
|
let(:version_response) { double(code: 200, success?: true, parsed_response: { 'version' => version }) }
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
before do
|
|
|
|
allow(Gitlab::HTTP).to receive(:get)
|
|
|
|
.with('http://gitlab.example/api/v4/version', anything)
|
|
|
|
.and_return(version_response)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.new(url: url, token: token) }
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
shared_examples 'performs network request' do
|
|
|
|
it 'performs network request' do
|
|
|
|
expect(Gitlab::HTTP).to receive(method).with(*expected_args).and_return(response_double)
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
subject.public_send(method, resource)
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'error handling' do
|
|
|
|
context 'when error occurred' do
|
2021-09-30 23:02:18 +05:30
|
|
|
it 'raises BulkImports::Error' do
|
2021-06-08 01:23:25 +05:30
|
|
|
allow(Gitlab::HTTP).to receive(method).and_raise(Errno::ECONNREFUSED)
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
expect { subject.public_send(method, resource) }.to raise_exception(BulkImports::Error)
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when response is not success' do
|
2021-09-30 23:02:18 +05:30
|
|
|
it 'raises BulkImports::Error' do
|
2021-01-03 14:25:43 +05:30
|
|
|
response_double = double(code: 503, success?: false)
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
allow(Gitlab::HTTP).to receive(method).and_return(response_double)
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
expect { subject.public_send(method, resource) }.to raise_exception(BulkImports::Error)
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#get' do
|
|
|
|
let(:method) { :get }
|
|
|
|
|
|
|
|
include_examples 'performs network request' do
|
|
|
|
let(:expected_args) do
|
|
|
|
[
|
2021-09-30 23:02:18 +05:30
|
|
|
'http://gitlab.example/api/v4/resource',
|
2021-06-08 01:23:25 +05:30
|
|
|
hash_including(
|
2021-09-04 01:27:46 +05:30
|
|
|
follow_redirects: false,
|
2021-06-08 01:23:25 +05:30
|
|
|
query: {
|
|
|
|
page: described_class::DEFAULT_PAGE,
|
|
|
|
per_page: described_class::DEFAULT_PER_PAGE
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'Authorization' => "Bearer #{token}"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
describe '#each_page' do
|
|
|
|
let(:objects1) { [{ object: 1 }, { object: 2 }] }
|
|
|
|
let(:objects2) { [{ object: 3 }, { object: 4 }] }
|
|
|
|
let(:response1) { double(success?: true, headers: { 'x-next-page' => 2 }, parsed_response: objects1) }
|
|
|
|
let(:response2) { double(success?: true, headers: {}, parsed_response: objects2) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_http_get('groups', { page: 1, per_page: 30 }, response1)
|
|
|
|
stub_http_get('groups', { page: 2, per_page: 30 }, response2)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a block' do
|
|
|
|
it 'yields every retrieved page to the supplied block' do
|
|
|
|
pages = []
|
|
|
|
|
|
|
|
subject.each_page(:get, 'groups') { |page| pages << page }
|
|
|
|
|
|
|
|
expect(pages[0]).to be_an_instance_of(Array)
|
|
|
|
expect(pages[1]).to be_an_instance_of(Array)
|
|
|
|
|
|
|
|
expect(pages[0]).to eq(objects1)
|
|
|
|
expect(pages[1]).to eq(objects2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without a block' do
|
|
|
|
it 'returns an Enumerator' do
|
|
|
|
expect(subject.each_page(:get, :foo)).to be_an_instance_of(Enumerator)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def stub_http_get(path, query, response)
|
2021-09-30 23:02:18 +05:30
|
|
|
uri = "http://gitlab.example/api/v4/#{path}"
|
2021-01-29 00:20:46 +05:30
|
|
|
params = {
|
|
|
|
follow_redirects: false,
|
|
|
|
headers: {
|
|
|
|
"Authorization" => "Bearer token",
|
|
|
|
"Content-Type" => "application/json"
|
|
|
|
}
|
|
|
|
}.merge(query: query)
|
|
|
|
|
|
|
|
allow(Gitlab::HTTP).to receive(:get).with(uri, params).and_return(response)
|
|
|
|
end
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
describe '#post' do
|
|
|
|
let(:method) { :post }
|
|
|
|
|
|
|
|
include_examples 'performs network request' do
|
|
|
|
let(:expected_args) do
|
|
|
|
[
|
2021-09-30 23:02:18 +05:30
|
|
|
'http://gitlab.example/api/v4/resource',
|
2021-06-08 01:23:25 +05:30
|
|
|
hash_including(
|
|
|
|
body: {},
|
2021-09-04 01:27:46 +05:30
|
|
|
follow_redirects: false,
|
2021-06-08 01:23:25 +05:30
|
|
|
headers: {
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'Authorization' => "Bearer #{token}"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
describe '#head' do
|
|
|
|
let(:method) { :head }
|
|
|
|
|
|
|
|
include_examples 'performs network request' do
|
|
|
|
let(:expected_args) do
|
|
|
|
[
|
2021-09-30 23:02:18 +05:30
|
|
|
'http://gitlab.example/api/v4/resource',
|
2021-09-04 01:27:46 +05:30
|
|
|
hash_including(
|
|
|
|
follow_redirects: false,
|
|
|
|
headers: {
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'Authorization' => "Bearer #{token}"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#stream' do
|
|
|
|
it 'performs network request with stream_body option' do
|
|
|
|
expected_args = [
|
2021-09-30 23:02:18 +05:30
|
|
|
'http://gitlab.example/api/v4/resource',
|
2021-09-04 01:27:46 +05:30
|
|
|
hash_including(
|
|
|
|
stream_body: true,
|
|
|
|
headers: {
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'Authorization' => "Bearer #{token}"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
expect(Gitlab::HTTP).to receive(:get).with(*expected_args).and_return(response_double)
|
|
|
|
|
|
|
|
subject.stream(resource)
|
|
|
|
end
|
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
|
|
|
|
context 'when source instance is incompatible' do
|
|
|
|
let(:version) { '13.0.0' }
|
|
|
|
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { subject.get(resource) }.to raise_error(::BulkImports::Error, "Unsupported GitLab Version. Minimum Supported Gitlab Version #{BulkImport::MINIMUM_GITLAB_MAJOR_VERSION}.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when url is relative' do
|
|
|
|
let(:url) { 'http://website.example/gitlab' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::HTTP).to receive(:get)
|
|
|
|
.with('http://website.example/gitlab/api/v4/version', anything)
|
|
|
|
.and_return(version_response)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'performs network request to a relative gitlab url' do
|
|
|
|
expect(Gitlab::HTTP).to receive(:get).with('http://website.example/gitlab/api/v4/resource', anything).and_return(response_double)
|
|
|
|
|
|
|
|
subject.get(resource)
|
|
|
|
end
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|