1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe BulkImports::Clients::HTTP, feature_category: :importers do
include ImportSpecHelper
let(:url) { 'http://gitlab.example' }
let(:token) { 'token' }
let(:resource) { 'resource' }
let(:version) { "#{BulkImport::MIN_MAJOR_VERSION}.0.0" }
let(:enterprise) { false }
let(:sidekiq_request_timeout) { described_class::SIDEKIQ_REQUEST_TIMEOUT }
let(:response_double) { double(code: 200, success?: true, parsed_response: {}) }
let(:metadata_response) do
double(
code: 200,
success?: true,
parsed_response: {
'version' => version,
'enterprise' => enterprise
}
)
end
subject { described_class.new(url: url, token: token) }
shared_examples 'performs network request' do
it 'performs network request' do
expect(Gitlab::HTTP).to receive(method).with(*expected_args).and_return(response_double)
subject.public_send(method, resource)
end
context 'error handling' do
context 'when error occurred' do
it 'raises BulkImports::NetworkError' do
allow(Gitlab::HTTP).to receive(method).and_raise(Errno::ECONNREFUSED)
expect { subject.public_send(method, resource) }.to raise_exception(BulkImports::NetworkError)
end
end
context 'when response is not success' do
it 'raises BulkImports::NetworkError' do
response_double = double(code: 503, success?: false, parsed_response: 'Error', request: double(path: double(path: '/test')))
allow(Gitlab::HTTP).to receive(method).and_return(response_double)
expect { subject.public_send(method, resource) }.to raise_exception(BulkImports::NetworkError, 'Unsuccessful response 503 from /test. Body: Error')
end
end
end
end
describe '#get' do
let(:method) { :get }
include_examples 'performs network request' do
let(:expected_args) do
[
'http://gitlab.example/api/v4/resource',
hash_including(
query: {
page: described_class::DEFAULT_PAGE,
per_page: described_class::DEFAULT_PER_PAGE,
private_token: token
},
headers: {
'Content-Type' => 'application/json'
},
follow_redirects: true,
resend_on_redirect: false,
limit: 2
)
]
end
end
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)
uri = "http://gitlab.example/api/v4/#{path}"
params = {
headers: { "Content-Type" => "application/json" },
query: { private_token: token },
follow_redirects: true,
resend_on_redirect: false,
limit: 2
}
params[:query] = params[:query].merge(query)
allow(Gitlab::HTTP).to receive(:get).with(uri, params).and_return(response)
end
end
context 'when the request is asynchronous' do
let(:expected_args) do
[
'http://gitlab.example/api/v4/resource',
hash_including(
query: {
page: described_class::DEFAULT_PAGE,
per_page: described_class::DEFAULT_PER_PAGE,
private_token: token
},
headers: {
'Content-Type' => 'application/json'
},
follow_redirects: true,
resend_on_redirect: false,
limit: 2,
timeout: sidekiq_request_timeout
)
]
end
it 'sets a timeout that is double the default read timeout' do
allow(Gitlab::Runtime).to receive(:sidekiq?).and_return(true)
expect(Gitlab::HTTP).to receive(method).with(*expected_args).and_return(response_double)
subject.public_send(method, resource)
end
end
end
describe '#post' do
let(:method) { :post }
include_examples 'performs network request' do
let(:expected_args) do
[
'http://gitlab.example/api/v4/resource',
hash_including(
body: {},
headers: {
'Content-Type' => 'application/json'
},
query: {
page: described_class::DEFAULT_PAGE,
per_page: described_class::DEFAULT_PER_PAGE,
private_token: token
},
follow_redirects: true,
resend_on_redirect: false,
limit: 2
)
]
end
end
end
describe '#head' do
let(:method) { :head }
include_examples 'performs network request' do
let(:expected_args) do
[
'http://gitlab.example/api/v4/resource',
hash_including(
headers: {
'Content-Type' => 'application/json'
},
query: {
page: described_class::DEFAULT_PAGE,
per_page: described_class::DEFAULT_PER_PAGE,
private_token: token
},
follow_redirects: true,
resend_on_redirect: false,
limit: 2
)
]
end
end
end
describe '#stream' do
it 'performs network request with stream_body option' do
expected_args = [
'http://gitlab.example/api/v4/resource',
hash_including(
stream_body: true,
headers: {
'Content-Type' => 'application/json'
},
query: {
page: described_class::DEFAULT_PAGE,
per_page: described_class::DEFAULT_PER_PAGE,
private_token: token
},
follow_redirects: true,
resend_on_redirect: false,
limit: 2
)
]
expect(Gitlab::HTTP).to receive(:get).with(*expected_args).and_return(response_double)
subject.stream(resource)
end
end
describe '#instance_version' do
it 'returns version as an instance of Gitlab::VersionInfo' do
response = { version: version }
stub_request(:get, 'http://gitlab.example/api/v4/version?private_token=token')
.to_return(status: 200, body: response.to_json, headers: { 'Content-Type' => 'application/json' })
expect(subject.instance_version).to eq(Gitlab::VersionInfo.parse(version))
end
context 'when /version endpoint is not available' do
it 'requests /metadata endpoint' do
response = { version: version }
stub_request(:get, 'http://gitlab.example/api/v4/version?private_token=token').to_return(status: 404)
stub_request(:get, 'http://gitlab.example/api/v4/metadata?private_token=token')
.to_return(status: 200, body: response.to_json, headers: { 'Content-Type' => 'application/json' })
expect(subject.instance_version).to eq(Gitlab::VersionInfo.parse(version))
end
context 'when /metadata endpoint returns a 401' do
it 'raises a BulkImports:Error' do
stub_request(:get, 'http://gitlab.example/api/v4/version?private_token=token').to_return(status: 404)
stub_request(:get, 'http://gitlab.example/api/v4/metadata?private_token=token')
.to_return(status: 401, body: "", headers: { 'Content-Type' => 'application/json' })
expect { subject.instance_version }.to raise_exception(BulkImports::Error,
"Import aborted as the provided personal access token does not have the required 'api' scope or " \
"is no longer valid.")
end
end
context 'when /metadata endpoint returns a 403' do
it 'raises a BulkImports:Error' do
stub_request(:get, 'http://gitlab.example/api/v4/version?private_token=token').to_return(status: 404)
stub_request(:get, 'http://gitlab.example/api/v4/metadata?private_token=token')
.to_return(status: 403, body: "", headers: { 'Content-Type' => 'application/json' })
expect { subject.instance_version }.to raise_exception(BulkImports::Error,
"Import aborted as the provided personal access token does not have the required 'api' scope or " \
"is no longer valid.")
end
end
context 'when /metadata endpoint returns a 404' do
it 'raises a BulkImports:Error' do
stub_request(:get, 'http://gitlab.example/api/v4/version?private_token=token').to_return(status: 404)
stub_request(:get, 'http://gitlab.example/api/v4/metadata?private_token=token')
.to_return(status: 404, body: "", headers: { 'Content-Type' => 'application/json' })
expect { subject.instance_version }.to raise_exception(BulkImports::Error, 'Invalid source URL. Enter only the base URL of the source GitLab instance.')
end
end
context 'when /metadata endpoint returns any other BulkImports::NetworkError' do
it 'raises a BulkImports:NetworkError' do
stub_request(:get, 'http://gitlab.example/api/v4/version?private_token=token').to_return(status: 404)
stub_request(:get, 'http://gitlab.example/api/v4/metadata?private_token=token')
.to_return(status: 418, body: "", headers: { 'Content-Type' => 'application/json' })
expect { subject.instance_version }.to raise_exception(BulkImports::NetworkError)
end
end
end
end
describe '#validate_instance_version!' do
before do
allow(subject).to receive(:instance_version).and_return(source_version)
end
context 'when instance version is greater than or equal to the minimum major version' do
let(:source_version) { Gitlab::VersionInfo.new(14) }
it { expect(subject.validate_instance_version!).to eq(true) }
end
context 'when instance version is less than the minimum major version' do
let(:source_version) { Gitlab::VersionInfo.new(13, 10, 0) }
it { expect { subject.validate_instance_version! }.to raise_exception(BulkImports::Error) }
end
end
describe '#validate_import_scopes!' do
context 'when the source_version is < 15.5' do
let(:source_version) { Gitlab::VersionInfo.new(15, 0) }
it 'skips validation' do
allow(subject).to receive(:instance_version).and_return(source_version)
expect(subject.validate_import_scopes!).to eq(true)
end
end
context 'when source version is 15.5 or higher' do
let(:source_version) { Gitlab::VersionInfo.new(15, 6) }
before do
allow(subject).to receive(:instance_version).and_return(source_version)
end
context 'when an HTTP error is raised' do
let(:response) { { enterprise: false } }
it 'raises BulkImports::NetworkError' do
stub_request(:get, 'http://gitlab.example/api/v4/personal_access_tokens/self?private_token=token')
.to_return(status: 404)
expect { subject.validate_import_scopes! }.to raise_exception(BulkImports::NetworkError)
end
end
context 'when scopes are valid' do
it 'returns true' do
stub_request(:get, 'http://gitlab.example/api/v4/personal_access_tokens/self?private_token=token')
.to_return(status: 200, body: { 'scopes' => ['api'] }.to_json, headers: { 'Content-Type' => 'application/json' })
expect(subject.validate_import_scopes!).to eq(true)
end
end
context 'when scopes are invalid' do
it 'raises a BulkImports error' do
stub_request(:get, 'http://gitlab.example/api/v4/personal_access_tokens/self?private_token=token')
.to_return(status: 200, body: { 'scopes' => ['read_user'] }.to_json, headers: { 'Content-Type' => 'application/json' })
expect(subject.instance_version).to eq(Gitlab::VersionInfo.parse(source_version))
expect { subject.validate_import_scopes! }.to raise_exception(BulkImports::Error)
end
end
end
end
describe '#instance_enterprise' do
let(:response) { { enterprise: false } }
before do
stub_request(:get, 'http://gitlab.example/api/v4/version?private_token=token')
.to_return(status: 200, body: response.to_json, headers: { 'Content-Type' => 'application/json' })
end
it 'returns source instance enterprise information' do
expect(subject.instance_enterprise).to eq(false)
end
context 'when enterprise information is missing' do
let(:response) { {} }
it 'defaults to true' do
expect(subject.instance_enterprise).to eq(true)
end
end
end
describe '#compatible_for_project_migration?' do
before do
allow(subject).to receive(:instance_version).and_return(Gitlab::VersionInfo.parse(version))
end
context 'when instance version is lower the the expected minimum' do
let(:version) { '14.3.0' }
it 'returns false' do
expect(subject.compatible_for_project_migration?).to be false
end
end
context 'when instance version is at least the expected minimum' do
let(:version) { '14.4.4' }
it 'returns true' do
expect(subject.compatible_for_project_migration?).to be true
end
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(metadata_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
end
|