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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::GoProxy do
include PackagesManagerApiSpecHelpers
include HttpBasicAuthHelpers
let_it_be(:user) { create :user }
let_it_be(:project) { create :project_empty_repo, creator: user, path: 'my-go-lib' }
let_it_be(:base) { "#{Settings.build_gitlab_go_url}/#{project.full_path}" }
let_it_be(:oauth) { create :oauth_access_token, scopes: 'api', resource_owner: user }
let_it_be(:job) { create :ci_build, user: user, status: :running }
let_it_be(:pa_token) { create :personal_access_token, user: user }
let_it_be(:modules) do
commits = [
create(:go_module_commit, :files, project: project, tag: 'v1.0.0', files: { 'README.md' => 'Hi' } ),
create(:go_module_commit, :module, project: project, tag: 'v1.0.1' ),
create(:go_module_commit, :package, project: project, tag: 'v1.0.2', path: 'pkg' ),
create(:go_module_commit, :module, project: project, tag: 'v1.0.3', name: 'mod' ),
create(:go_module_commit, :files, project: project, files: { 'y.go' => "package a\n" } ),
create(:go_module_commit, :module, project: project, name: 'v2' ),
create(:go_module_commit, :files, project: project, tag: 'v2.0.0', files: { 'v2/x.go' => "package a\n" })
]
{ sha: [commits[4].sha, commits[5].sha] }
end
before do
project.add_developer(user)
stub_feature_flags(go_proxy_disable_gomod_validation: false)
modules
end
shared_examples 'an unavailable resource' do
it 'returns not found' do
get_resource(user)
expect(response).to have_gitlab_http_status(:not_found)
end
end
shared_examples 'a module version list resource' do |*versions, path: ''|
let(:module_name) { "#{base}#{path}" }
let(:resource) { "list" }
it "returns #{versions.empty? ? 'nothing' : versions.join(', ')}" do
get_resource(user)
expect(response).to have_gitlab_http_status(:ok)
expect(response.body.split("\n").to_set).to eq(versions.to_set)
end
end
shared_examples 'a missing module version list resource' do |path: ''|
let(:module_name) { "#{base}#{path}" }
let(:resource) { "list" }
it_behaves_like 'an unavailable resource'
end
shared_examples 'a module version information resource' do |version, path: ''|
let(:module_name) { "#{base}#{path}" }
let(:resource) { "#{version}.info" }
it "returns information for #{version}" do
get_resource(user)
time = project.repository.find_tag(version).dereferenced_target.committed_date
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to be_kind_of(Hash)
expect(json_response['Version']).to eq(version)
expect(json_response['Time']).to eq(time.strftime('%Y-%m-%dT%H:%M:%S.%L%:z'))
end
end
shared_examples 'a missing module version information resource' do |version, path: ''|
let(:module_name) { "#{base}#{path}" }
let(:resource) { "#{version}.info" }
it_behaves_like 'an unavailable resource'
end
shared_examples 'a module pseudo-version information resource' do |prefix, path: ''|
let(:module_name) { "#{base}#{path}" }
let(:commit) { project.repository.commit_by(oid: sha) }
let(:version) { fmt_pseudo_version prefix, commit }
let(:resource) { "#{version}.info" }
it "returns information for #{prefix}yyyymmddhhmmss-abcdefabcdef" do
get_resource(user)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to be_kind_of(Hash)
expect(json_response['Version']).to eq(version)
expect(json_response['Time']).to eq(commit.committed_date.strftime('%Y-%m-%dT%H:%M:%S.%L%:z'))
end
end
shared_examples 'a missing module pseudo-version information resource' do |path: ''|
let(:module_name) { "#{base}#{path}" }
let(:commit) do
raise "tried to reference :commit without defining :sha" unless defined?(sha)
project.repository.commit_by(oid: sha)
end
let(:resource) { "#{version}.info" }
it_behaves_like 'an unavailable resource'
end
shared_examples 'a module file resource' do |version, path: ''|
let(:module_name) { "#{base}#{path}" }
let(:resource) { "#{version}.mod" }
it "returns #{path}/go.mod from the repo" do
get_resource(user)
expect(response).to have_gitlab_http_status(:ok)
expect(response.body.split("\n", 2).first).to eq("module #{module_name}")
end
end
shared_examples 'a missing module file resource' do |version, path: ''|
let(:module_name) { "#{base}#{path}" }
let(:resource) { "#{version}.mod" }
it_behaves_like 'an unavailable resource'
end
shared_examples 'a module archive resource' do |version, entries, path: ''|
let(:module_name) { "#{base}#{path}" }
let(:resource) { "#{version}.zip" }
it "returns an archive of #{path.empty? ? '/' : path} @ #{version} from the repo" do
get_resource(user)
expect(response).to have_gitlab_http_status(:ok)
entries = entries.map { |e| "#{module_name}@#{version}/#{e}" }.to_set
actual = Set[]
Zip::InputStream.open(StringIO.new(response.body)) do |zip|
while (entry = zip.get_next_entry)
actual.add(entry.name)
end
end
expect(actual).to eq(entries)
end
end
describe 'GET /projects/:id/packages/go/*module_name/@v/list' do
context 'for the root module' do
it_behaves_like 'a module version list resource', 'v1.0.1', 'v1.0.2', 'v1.0.3'
end
context 'for the package' do
it_behaves_like 'a module version list resource', path: '/pkg'
end
context 'for the submodule' do
it_behaves_like 'a module version list resource', 'v1.0.3', path: '/mod'
end
context 'for the root module v2' do
it_behaves_like 'a module version list resource', 'v2.0.0', path: '/v2'
end
context 'with a URL encoded relative path component' do
it_behaves_like 'a missing module version list resource', path: '/%2E%2E%2Fxyz'
end
context 'with the feature disabled' do
before do
stub_feature_flags(go_proxy: false)
end
it_behaves_like 'a missing module version list resource'
end
end
describe 'GET /projects/:id/packages/go/*module_name/@v/:module_version.info' do
context 'with the root module v1.0.1' do
it_behaves_like 'a module version information resource', 'v1.0.1'
end
context 'with the submodule v1.0.3' do
it_behaves_like 'a module version information resource', 'v1.0.3', path: '/mod'
end
context 'with the root module v2.0.0' do
it_behaves_like 'a module version information resource', 'v2.0.0', path: '/v2'
end
context 'with an invalid path' do
it_behaves_like 'a missing module version information resource', 'v1.0.3', path: '/pkg'
end
context 'with an invalid version' do
it_behaves_like 'a missing module version information resource', 'v1.0.1', path: '/mod'
end
context 'with a pseudo-version for v1' do
it_behaves_like 'a module pseudo-version information resource', 'v1.0.4-0.' do
let(:sha) { modules[:sha][0] }
end
end
context 'with a pseudo-version for v2' do
it_behaves_like 'a module pseudo-version information resource', 'v2.0.0-', path: '/v2' do
let(:sha) { modules[:sha][1] }
end
end
context 'with a pseudo-version with an invalid timestamp' do
it_behaves_like 'a missing module pseudo-version information resource' do
let(:version) { "v1.0.4-0.00000000000000-#{modules[:sha][0][0..11]}" }
end
end
context 'with a pseudo-version with an invalid commit sha' do
it_behaves_like 'a missing module pseudo-version information resource' do
let(:sha) { modules[:sha][0] }
let(:version) { "v1.0.4-0.#{commit.committed_date.strftime('%Y%m%d%H%M%S')}-000000000000" }
end
end
context 'with a pseudo-version with a short commit sha' do
it_behaves_like 'a missing module pseudo-version information resource' do
let(:sha) { modules[:sha][0] }
let(:version) { "v1.0.4-0.#{commit.committed_date.strftime('%Y%m%d%H%M%S')}-#{modules[:sha][0][0..10]}" }
end
end
end
describe 'GET /projects/:id/packages/go/*module_name/@v/:module_version.mod' do
context 'with the root module v1.0.1' do
it_behaves_like 'a module file resource', 'v1.0.1'
end
context 'with the submodule v1.0.3' do
it_behaves_like 'a module file resource', 'v1.0.3', path: '/mod'
end
context 'with the root module v2.0.0' do
it_behaves_like 'a module file resource', 'v2.0.0', path: '/v2'
end
context 'with an invalid path' do
it_behaves_like 'a missing module file resource', 'v1.0.3', path: '/pkg'
end
context 'with an invalid version' do
it_behaves_like 'a missing module file resource', 'v1.0.1', path: '/mod'
end
end
describe 'GET /projects/:id/packages/go/*module_name/@v/:module_version.zip' do
context 'with the root module v1.0.1' do
it_behaves_like 'a module archive resource', 'v1.0.1', ['README.md', 'go.mod', 'a.go']
end
context 'with the root module v1.0.2' do
it_behaves_like 'a module archive resource', 'v1.0.2', ['README.md', 'go.mod', 'a.go', 'pkg/b.go']
end
context 'with the root module v1.0.3' do
it_behaves_like 'a module archive resource', 'v1.0.3', ['README.md', 'go.mod', 'a.go', 'pkg/b.go']
end
context 'with the submodule v1.0.3' do
it_behaves_like 'a module archive resource', 'v1.0.3', ['go.mod', 'a.go'], path: '/mod'
end
context 'with the root module v2.0.0' do
it_behaves_like 'a module archive resource', 'v2.0.0', ['go.mod', 'a.go', 'x.go'], path: '/v2'
end
end
context 'with an invalid module directive' do
let_it_be(:project) { create :project_empty_repo, :public, creator: user }
let_it_be(:base) { "#{Settings.build_gitlab_go_url}/#{project.full_path}" }
let_it_be(:modules) do
create(:go_module_commit, :files, project: project, files: { 'a.go' => "package\a" } )
create(:go_module_commit, :files, project: project, tag: 'v1.0.0', files: { 'go.mod' => "module not/a/real/module\n" })
create(:go_module_commit, :files, project: project, files: { 'v2/a.go' => "package a\n" } )
create(:go_module_commit, :files, project: project, tag: 'v2.0.0', files: { 'v2/go.mod' => "module #{base}\n" } )
end
describe 'GET /projects/:id/packages/go/*module_name/@v/list' do
context 'with a completely wrong directive for v1' do
it_behaves_like 'a module version list resource'
end
context 'with a directive omitting the suffix for v2' do
it_behaves_like 'a module version list resource', path: '/v2'
end
end
describe 'GET /projects/:id/packages/go/*module_name/@v/:module_version.info' do
context 'with a completely wrong directive for v1' do
it_behaves_like 'a missing module version information resource', 'v1.0.0'
end
context 'with a directive omitting the suffix for v2' do
it_behaves_like 'a missing module version information resource', 'v2.0.0', path: '/v2'
end
end
end
context 'with a case sensitive project and versions' do
let_it_be(:project) { create :project_empty_repo, :public, creator: user, path: 'MyGoLib' }
let_it_be(:base) { "#{Settings.build_gitlab_go_url}/#{project.full_path}" }
let_it_be(:base_encoded) { base.gsub(/[A-Z]/) { |s| "!#{s.downcase}"} }
let_it_be(:modules) do
create(:go_module_commit, :files, project: project, files: { 'README.md' => "Hi" })
create(:go_module_commit, :module, project: project, tag: 'v1.0.1-prerelease')
create(:go_module_commit, :package, project: project, tag: 'v1.0.1-Prerelease', path: 'pkg')
end
describe 'GET /projects/:id/packages/go/*module_name/@v/list' do
let(:resource) { "list" }
context 'with a case encoded path' do
it_behaves_like 'a module version list resource', 'v1.0.1-prerelease', 'v1.0.1-Prerelease' do
let(:module_name) { base_encoded }
end
end
context 'without a case encoded path' do
it_behaves_like 'a missing module version list resource' do
let(:module_name) { base.downcase }
end
end
end
describe 'GET /projects/:id/packages/go/*module_name/@v/:module_version.info' do
context 'with a case encoded path' do
it_behaves_like 'a module version information resource', 'v1.0.1-Prerelease' do
let(:module_name) { base_encoded }
let(:resource) { "v1.0.1-!prerelease.info" }
end
end
context 'without a case encoded path' do
it_behaves_like 'a module version information resource', 'v1.0.1-prerelease' do
let(:module_name) { base_encoded }
let(:resource) { "v1.0.1-prerelease.info" }
end
end
end
end
context 'with a private project' do
let(:module_name) { base }
before do
project.update(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
end
describe 'GET /projects/:id/packages/go/*module_name/@v/list' do
let(:resource) { "list" }
it 'returns ok with an oauth token' do
get_resource(oauth_access_token: oauth)
expect(response).to have_gitlab_http_status(:ok)
end
it 'returns ok with a job token' do
get_resource(oauth_access_token: job)
expect(response).to have_gitlab_http_status(:ok)
end
it 'returns ok with a personal access token' do
get_resource(personal_access_token: pa_token)
expect(response).to have_gitlab_http_status(:ok)
end
it 'returns ok with a personal access token and basic authentication' do
get_resource(headers: basic_auth_header(user.username, pa_token.token))
expect(response).to have_gitlab_http_status(:ok)
end
it 'returns unauthorized with a failed job token' do
job.update!(status: :failed)
get_resource(oauth_access_token: job)
expect(response).to have_gitlab_http_status(:unauthorized)
end
it 'returns unauthorized with no authentication' do
get_resource
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
end
context 'with a public project' do
let(:module_name) { base }
before do
project.update(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
end
describe 'GET /projects/:id/packages/go/*module_name/@v/list' do
let(:resource) { "list" }
it 'returns ok with no authentication' do
get_resource
expect(response).to have_gitlab_http_status(:ok)
end
end
end
context 'with a non-existent project' do
def get_resource(user = nil, **params)
get api("/projects/not%2fa%2fproject/packages/go/#{base}/@v/list", user, params)
end
describe 'GET /projects/:id/packages/go/*module_name/@v/list' do
it 'returns not found with a user' do
get_resource(user)
expect(response).to have_gitlab_http_status(:not_found)
end
it 'returns not found with an oauth token' do
get_resource(oauth_access_token: oauth)
expect(response).to have_gitlab_http_status(:not_found)
end
it 'returns not found with a job token' do
get_resource(oauth_access_token: job)
expect(response).to have_gitlab_http_status(:not_found)
end
it 'returns not found with a personal access token' do
get_resource(personal_access_token: pa_token)
expect(response).to have_gitlab_http_status(:not_found)
end
it 'returns unauthorized with no authentication' do
get_resource
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
end
def get_resource(user = nil, headers: {}, **params)
get api("/projects/#{project.id}/packages/go/#{module_name}/@v/#{resource}", user, params), headers: headers
end
def fmt_pseudo_version(prefix, commit)
"#{prefix}#{commit.committed_date.strftime('%Y%m%d%H%M%S')}-#{commit.sha[0..11]}"
end
end
|