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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::NugetProjectPackages, feature_category: :package_registry do
include_context 'nuget api setup'
using RSpec::Parameterized::TableSyntax
let_it_be_with_reload(:project) { create(:project, :public) }
let_it_be(:deploy_token) { create(:deploy_token, read_package_registry: true, write_package_registry: true) }
let_it_be(:project_deploy_token) { create(:project_deploy_token, deploy_token: deploy_token, project: project) }
let_it_be(:package_name) { 'Dummy.Package' }
let(:target) { project }
let(:target_type) { 'projects' }
let(:snowplow_gitlab_standard_context) { snowplow_context }
def snowplow_context(user_role: :developer)
if user_role == :anonymous
{ project: target, namespace: target.namespace, property: 'i_package_nuget_user' }
else
{ project: target, namespace: target.namespace, property: 'i_package_nuget_user', user: user }
end
end
shared_examples 'accept get request on private project with access to package registry for everyone' do
subject { get api(url) }
before do
update_visibility_to(Gitlab::VisibilityLevel::PRIVATE)
project.project_feature.update!(package_registry_access_level: ProjectFeature::PUBLIC)
end
it_behaves_like 'returning response status', :ok
end
shared_examples 'nuget serialize odata package endpoint' do
subject { get api(url), params: params }
it { is_expected.to have_request_urgency(:low) }
it_behaves_like 'returning response status', :success
it 'returns a valid xml response and invokes OdataPackageEntryService' do
expect(Packages::Nuget::OdataPackageEntryService).to receive(:new).with(target, service_params).and_call_original
subject
expect(response.media_type).to eq('application/xml')
end
[nil, '', '%20', '..%2F..', '../..'].each do |value|
context "with invalid package name #{value}" do
let(:package_name) { value }
it_behaves_like 'returning response status', :bad_request
end
end
context 'with missing required params' do
let(:params) { {} }
let(:package_version) { nil }
it_behaves_like 'returning response status', :bad_request
end
end
describe 'GET /api/v4/projects/:id/packages/nuget' do
let(:url) { "/projects/#{target.id}/packages/nuget/index.json" }
it_behaves_like 'handling nuget service requests'
it_behaves_like 'accept get request on private project with access to package registry for everyone'
end
describe 'GET /api/v4/projects/:id/packages/nuget/v2' do
let(:url) { "/projects/#{target.id}/packages/nuget/v2" }
it_behaves_like 'handling nuget service requests', v2: true
it_behaves_like 'accept get request on private project with access to package registry for everyone'
end
describe 'GET /api/v4/projects/:id/packages/nuget/v2/$metadata' do
let(:url) { "/projects/#{target.id}/packages/nuget/v2/$metadata" }
subject(:api_request) { get api(url) }
it { is_expected.to have_request_urgency(:low) }
context 'with valid target' do
using RSpec::Parameterized::TableSyntax
where(:visibility_level, :user_role, :member, :expected_status) do
'PUBLIC' | :developer | true | :success
'PUBLIC' | :guest | true | :success
'PUBLIC' | :developer | false | :success
'PUBLIC' | :guest | false | :success
'PUBLIC' | :anonymous | false | :success
'PRIVATE' | :developer | true | :success
'PRIVATE' | :guest | true | :success
'PRIVATE' | :developer | false | :success
'PRIVATE' | :guest | false | :success
'PRIVATE' | :anonymous | false | :success
'INTERNAL' | :developer | true | :success
'INTERNAL' | :guest | true | :success
'INTERNAL' | :developer | false | :success
'INTERNAL' | :guest | false | :success
'INTERNAL' | :anonymous | false | :success
end
with_them do
before do
update_visibility_to(Gitlab::VisibilityLevel.const_get(visibility_level, false))
end
it_behaves_like 'process nuget v2 $metadata service request', params[:user_role], params[:expected_status], params[:member]
end
end
end
describe 'GET /api/v4/projects/:id/packages/nuget/metadata/*package_name/index' do
let(:url) { "/projects/#{target.id}/packages/nuget/metadata/#{package_name}/index.json" }
it_behaves_like 'handling nuget metadata requests with package name'
it_behaves_like 'accept get request on private project with access to package registry for everyone' do
let_it_be(:packages) { create_list(:nuget_package, 5, :with_metadatum, name: package_name, project: project) }
end
end
describe 'GET /api/v4/projects/:id/packages/nuget/metadata/*package_name/*package_version' do
let(:url) { "/projects/#{target.id}/packages/nuget/metadata/#{package_name}/#{package.version}.json" }
it_behaves_like 'handling nuget metadata requests with package name and package version'
it_behaves_like 'accept get request on private project with access to package registry for everyone' do
let_it_be(:package) { create(:nuget_package, :with_metadatum, name: package_name, project: project) }
end
end
describe 'GET /api/v4/projects/:id/packages/nuget/query' do
let(:url) { "/projects/#{target.id}/packages/nuget/query?#{query_parameters.to_query}" }
it_behaves_like 'handling nuget search requests'
it_behaves_like 'accept get request on private project with access to package registry for everyone' do
let_it_be(:query_parameters) { { q: 'query', take: 5, skip: 0, prerelease: true } }
end
end
describe 'GET /api/v4/projects/:id/packages/nuget/download/*package_name/index' do
let_it_be(:packages) { create_list(:nuget_package, 5, name: package_name, project: project) }
let(:url) { "/projects/#{target.id}/packages/nuget/download/#{package_name}/index.json" }
subject { get api(url) }
context 'with valid target' do
where(:visibility_level, :user_role, :member, :user_token, :shared_examples_name, :expected_status) do
'PUBLIC' | :developer | true | true | 'process nuget download versions request' | :success
'PUBLIC' | :guest | true | true | 'process nuget download versions request' | :success
'PUBLIC' | :developer | true | false | 'rejects nuget packages access' | :unauthorized
'PUBLIC' | :guest | true | false | 'rejects nuget packages access' | :unauthorized
'PUBLIC' | :developer | false | true | 'process nuget download versions request' | :success
'PUBLIC' | :guest | false | true | 'process nuget download versions request' | :success
'PUBLIC' | :developer | false | false | 'rejects nuget packages access' | :unauthorized
'PUBLIC' | :guest | false | false | 'rejects nuget packages access' | :unauthorized
'PUBLIC' | :anonymous | false | true | 'process nuget download versions request' | :success
'PRIVATE' | :developer | true | true | 'process nuget download versions request' | :success
'PRIVATE' | :guest | true | true | 'rejects nuget packages access' | :forbidden
'PRIVATE' | :developer | true | false | 'rejects nuget packages access' | :unauthorized
'PRIVATE' | :guest | true | false | 'rejects nuget packages access' | :unauthorized
'PRIVATE' | :developer | false | true | 'rejects nuget packages access' | :not_found
'PRIVATE' | :guest | false | true | 'rejects nuget packages access' | :not_found
'PRIVATE' | :developer | false | false | 'rejects nuget packages access' | :unauthorized
'PRIVATE' | :guest | false | false | 'rejects nuget packages access' | :unauthorized
'PRIVATE' | :anonymous | false | true | 'rejects nuget packages access' | :unauthorized
end
with_them do
let(:token) { user_token ? personal_access_token.token : 'wrong' }
let(:headers) { user_role == :anonymous ? {} : basic_auth_header(user.username, token) }
subject { get api(url), headers: headers }
before do
update_visibility_to(Gitlab::VisibilityLevel.const_get(visibility_level, false))
end
it_behaves_like params[:shared_examples_name], params[:user_role], params[:expected_status], params[:member]
end
end
it_behaves_like 'deploy token for package GET requests'
it_behaves_like 'rejects nuget access with unknown target id'
it_behaves_like 'rejects nuget access with invalid target id'
it_behaves_like 'accept get request on private project with access to package registry for everyone'
end
describe 'GET /api/v4/projects/:id/packages/nuget/download/*package_name/*package_version/*package_filename' do
let_it_be(:package) { create(:nuget_package, :with_symbol_package, :with_metadatum, project: project, name: package_name, version: '0.1') }
let_it_be(:package_version) { package.version }
let(:format) { 'nupkg' }
let(:url) { "/projects/#{target.id}/packages/nuget/download/#{package.name}/#{package_version}/#{package.name}.#{package_version}.#{format}" }
subject { get api(url), headers: headers }
context 'with valid target' do
where(:visibility_level, :user_role, :member, :user_token, :shared_examples_name, :expected_status) do
'PUBLIC' | :developer | true | true | 'process nuget download content request' | :success
'PUBLIC' | :guest | true | true | 'process nuget download content request' | :success
'PUBLIC' | :developer | true | false | 'rejects nuget packages access' | :unauthorized
'PUBLIC' | :guest | true | false | 'rejects nuget packages access' | :unauthorized
'PUBLIC' | :developer | false | true | 'process nuget download content request' | :success
'PUBLIC' | :guest | false | true | 'process nuget download content request' | :success
'PUBLIC' | :developer | false | false | 'rejects nuget packages access' | :unauthorized
'PUBLIC' | :guest | false | false | 'rejects nuget packages access' | :unauthorized
'PUBLIC' | :anonymous | false | true | 'process nuget download content request' | :success
'PRIVATE' | :developer | true | true | 'process nuget download content request' | :success
'PRIVATE' | :guest | true | true | 'rejects nuget packages access' | :forbidden
'PRIVATE' | :developer | true | false | 'rejects nuget packages access' | :unauthorized
'PRIVATE' | :guest | true | false | 'rejects nuget packages access' | :unauthorized
'PRIVATE' | :developer | false | true | 'rejects nuget packages access' | :not_found
'PRIVATE' | :guest | false | true | 'rejects nuget packages access' | :not_found
'PRIVATE' | :developer | false | false | 'rejects nuget packages access' | :unauthorized
'PRIVATE' | :guest | false | false | 'rejects nuget packages access' | :unauthorized
'PRIVATE' | :anonymous | false | true | 'rejects nuget packages access' | :unauthorized
end
with_them do
let(:token) { user_token ? personal_access_token.token : 'wrong' }
let(:headers) { user_role == :anonymous ? {} : basic_auth_header(user.username, token) }
let(:snowplow_gitlab_standard_context) { snowplow_context(user_role: user_role) }
before do
update_visibility_to(Gitlab::VisibilityLevel.const_get(visibility_level, false))
end
it_behaves_like params[:shared_examples_name], params[:user_role], params[:expected_status], params[:member]
end
it_behaves_like 'accept get request on private project with access to package registry for everyone'
end
it_behaves_like 'deploy token for package GET requests' do
before do
update_visibility_to(Gitlab::VisibilityLevel::PRIVATE)
end
end
it_behaves_like 'rejects nuget access with unknown target id'
it_behaves_like 'rejects nuget access with invalid target id'
end
describe 'GET /api/v4/projects/:id/packages/nuget/v2/FindPackagesById()' do
it_behaves_like 'nuget serialize odata package endpoint' do
let(:url) { "/projects/#{target.id}/packages/nuget/v2/FindPackagesById()" }
let(:params) { { id: "'#{package_name}'" } }
let(:service_params) { { package_name: package_name } }
end
end
describe 'GET /api/v4/projects/:id/packages/nuget/v2/Packages()' do
it_behaves_like 'nuget serialize odata package endpoint' do
let(:url) { "/projects/#{target.id}/packages/nuget/v2/Packages()" }
let(:params) { { '$filter' => "(tolower(Id) eq '#{package_name&.downcase}')" } }
let(:service_params) { { package_name: package_name&.downcase } }
end
end
describe 'GET /api/v4/projects/:id/packages/nuget/v2/Packages(Id=\'*\',Version=\'*\')' do
let(:package_version) { '1.0.0' }
let(:url) { "/projects/#{target.id}/packages/nuget/v2/Packages(Id='#{package_name}',Version='#{package_version}')" }
let(:params) { {} }
let(:service_params) { { package_name: package_name, package_version: package_version } }
it_behaves_like 'nuget serialize odata package endpoint'
context 'with invalid package version' do
subject { get api(url) }
['', '1', '1./2.3', '%20', '..%2F..', '../..'].each do |value|
context "with invalid package version #{value}" do
let(:package_version) { value }
it_behaves_like 'returning response status', :bad_request
end
end
end
end
describe 'PUT /api/v4/projects/:id/packages/nuget/authorize' do
it_behaves_like 'nuget authorize upload endpoint' do
let(:url) { "/projects/#{target.id}/packages/nuget/authorize" }
end
end
describe 'PUT /api/v4/projects/:id/packages/nuget' do
it_behaves_like 'nuget upload endpoint' do
let(:url) { "/projects/#{target.id}/packages/nuget" }
end
end
describe 'PUT /api/v4/projects/:id/packages/nuget/symbolpackage/authorize' do
it_behaves_like 'nuget authorize upload endpoint' do
let(:url) { "/projects/#{target.id}/packages/nuget/symbolpackage/authorize" }
end
end
describe 'PUT /api/v4/projects/:id/packages/nuget/symbolpackage' do
it_behaves_like 'nuget upload endpoint', symbol_package: true do
let(:url) { "/projects/#{target.id}/packages/nuget/symbolpackage" }
end
end
describe 'PUT /api/v4/projects/:id/packages/nuget/v2/authorize' do
it_behaves_like 'nuget authorize upload endpoint' do
let(:url) { "/projects/#{target.id}/packages/nuget/v2/authorize" }
end
end
describe 'PUT /api/v4/projects/:id/packages/nuget/v2' do
it_behaves_like 'nuget upload endpoint' do
let(:url) { "/projects/#{target.id}/packages/nuget/v2" }
end
end
def update_visibility_to(visibility)
project.update!(visibility_level: visibility)
end
end
|