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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'package details', feature_category: :package_registry do
include GraphqlHelpers
let_it_be_with_reload(:group) { create(:group) }
let_it_be_with_reload(:project) { create(:project, group: group) }
let_it_be_with_reload(:composer_package) { create(:composer_package, :last_downloaded_at, project: project) }
let_it_be(:user) { create(:user) }
let_it_be(:composer_json) { { name: 'name', type: 'type', license: 'license', version: 1 } }
let_it_be(:composer_metadatum) do
# we are forced to manually create the metadatum, without using the factory to force the sha to be a string
# and avoid an error where gitaly can't find the repository
create(:composer_metadatum, package: composer_package, target_sha: 'foo_sha', composer_json: composer_json)
end
let(:depth) { 3 }
let(:excluded) { %w[metadata apiFuzzingCiConfiguration pipeline packageFiles] }
let(:metadata) { query_graphql_fragment('ComposerMetadata') }
let(:package_files) { all_graphql_fields_for('PackageFile') }
let(:package_global_id) { global_id_of(composer_package) }
let(:package_details) { graphql_data_at(:package) }
let(:query) do
graphql_query_for(:package, { id: package_global_id }, <<~FIELDS)
#{all_graphql_fields_for('PackageDetailsType', max_depth: depth, excluded: excluded)}
metadata {
#{metadata}
}
packageFiles {
nodes {
#{package_files}
}
}
FIELDS
end
subject { post_graphql(query, current_user: user) }
context 'with unauthorized user' do
before do
project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
project.add_guest(user)
end
it 'returns no packages' do
subject
expect(graphql_data_at(:package)).to be_nil
end
context 'with access to package registry for everyone' do
before do
project.project_feature.update!(package_registry_access_level: ProjectFeature::PUBLIC)
subject
end
it_behaves_like 'a working graphql query' do
it 'matches the JSON schema' do
expect(package_details).to match_schema('graphql/packages/package_details')
end
end
it '`public_package` returns true' do
expect(graphql_data_at(:package, :public_package)).to eq(true)
end
end
end
context 'when project is public' do
let_it_be(:public_project) { create(:project, :public, group: group) }
let_it_be(:composer_package) { create(:composer_package, project: public_project) }
let(:package_global_id) { global_id_of(composer_package) }
before do
subject
end
it_behaves_like 'a working graphql query' do
before do
subject
end
it 'matches the JSON schema' do
expect(package_details).to match_schema('graphql/packages/package_details')
end
end
it '`public_package` returns true' do
expect(graphql_data_at(:package, :public_package)).to eq(true)
end
end
context 'with authorized user' do
before do
project.add_developer(user)
end
it_behaves_like 'a working graphql query' do
before do
subject
end
it 'matches the JSON schema' do
expect(package_details).to match_schema('graphql/packages/package_details')
end
end
context 'with package without last_downloaded_at' do
before do
composer_package.update!(last_downloaded_at: nil)
subject
end
it 'matches the JSON schema' do
expect(package_details).to match_schema('graphql/packages/package_details')
end
end
context 'with package files pending destruction' do
let_it_be(:package_file) { create(:package_file, package: composer_package) }
let_it_be(:package_file_pending_destruction) { create(:package_file, :pending_destruction, package: composer_package) }
let(:package_file_ids) { graphql_data_at(:package, :package_files, :nodes).map { |node| node["id"] } }
it 'does not return them' do
subject
expect(package_file_ids).to contain_exactly(package_file.to_global_id.to_s)
end
end
context 'with a batched query' do
let_it_be(:conan_package) { create(:conan_package, project: project) }
let(:batch_query) do
<<~QUERY
{
a: package(id: "#{global_id_of(composer_package)}") { name }
b: package(id: "#{global_id_of(conan_package)}") { name }
}
QUERY
end
let(:a_packages_names) { graphql_data_at(:a, :packages, :nodes, :name) }
it 'returns an error for the second package and data for the first' do
post_graphql(batch_query, current_user: user)
expect(graphql_data_at(:a, :name)).to eq(composer_package.name)
expect_graphql_errors_to_include [/"package" field can be requested only for 1 Query\(s\) at a time./]
expect(graphql_data_at(:b)).to be(nil)
end
end
context 'versions field', :aggregate_failures do
let_it_be(:composer_package2) { create(:composer_package, project: project, name: composer_package.name) }
let_it_be(:composer_package3) { create(:composer_package, :error, project: project, name: composer_package.name) }
let_it_be(:pending_destruction) { create(:composer_package, :pending_destruction, project: project, name: composer_package.name) }
def run_query
versions_nodes = <<~QUERY
nodes { id }
QUERY
query = graphql_query_for(:package, { id: package_global_id }, query_graphql_field("versions", {}, versions_nodes))
post_graphql(query, current_user: user)
end
it 'returns other versions' do
run_query
versions_ids = graphql_data.dig('package', 'versions', 'nodes').pluck('id')
expected_ids = [composer_package2, composer_package3].map(&:to_gid).map(&:to_s)
expect(versions_ids).to contain_exactly(*expected_ids)
end
end
context 'pipelines field', :aggregate_failures do
let(:pipelines) { create_list(:ci_pipeline, 6, project: project) }
let(:pipeline_gids) { pipelines.sort_by(&:id).map(&:to_gid).map(&:to_s).reverse }
before do
pipelines.each do |pipeline|
create(:package_build_info, package: composer_package, pipeline: pipeline)
end
end
def run_query(args)
pipelines_nodes = <<~QUERY
nodes {
id
}
pageInfo {
startCursor
endCursor
}
QUERY
query = graphql_query_for(:package, { id: package_global_id }, query_graphql_field("pipelines", args, pipelines_nodes))
post_graphql(query, current_user: user)
end
it 'loads the second page with pagination first correctly' do
run_query(first: 2)
pipeline_ids = graphql_data.dig('package', 'pipelines', 'nodes').pluck('id')
expect(pipeline_ids).to eq(pipeline_gids[0..1])
cursor = graphql_data.dig('package', 'pipelines', 'pageInfo', 'endCursor')
run_query(first: 2, after: cursor)
pipeline_ids = graphql_data.dig('package', 'pipelines', 'nodes').pluck('id')
expect(pipeline_ids).to eq(pipeline_gids[2..3])
end
it 'loads the second page with pagination last correctly' do
run_query(last: 2)
pipeline_ids = graphql_data.dig('package', 'pipelines', 'nodes').pluck('id')
expect(pipeline_ids).to eq(pipeline_gids[4..5])
cursor = graphql_data.dig('package', 'pipelines', 'pageInfo', 'startCursor')
run_query(last: 2, before: cursor)
pipeline_ids = graphql_data.dig('package', 'pipelines', 'nodes').pluck('id')
expect(pipeline_ids).to eq(pipeline_gids[2..3])
end
end
context 'package managers paths' do
before do
subject
end
it 'returns npm_url correctly' do
expect(graphql_data_at(:package, :npm_url)).to eq("http://localhost/api/v4/projects/#{project.id}/packages/npm")
end
it 'returns maven_url correctly' do
expect(graphql_data_at(:package, :maven_url)).to eq("http://localhost/api/v4/projects/#{project.id}/packages/maven")
end
it 'returns conan_url correctly' do
expect(graphql_data_at(:package, :conan_url)).to eq("http://localhost/api/v4/projects/#{project.id}/packages/conan")
end
it 'returns nuget_url correctly' do
expect(graphql_data_at(:package, :nuget_url)).to eq("http://localhost/api/v4/projects/#{project.id}/packages/nuget/index.json")
end
it 'returns pypi_url correctly' do
expect(graphql_data_at(:package, :pypi_url)).to eq("http://__token__:<your_personal_token>@localhost/api/v4/projects/#{project.id}/packages/pypi/simple")
end
it 'returns pypi_setup_url correctly' do
expect(graphql_data_at(:package, :pypi_setup_url)).to eq("http://localhost/api/v4/projects/#{project.id}/packages/pypi")
end
it 'returns composer_url correctly' do
expect(graphql_data_at(:package, :composer_url)).to eq("http://localhost/api/v4/group/#{group.id}/-/packages/composer/packages.json")
end
it 'returns composer_config_repository_url correctly' do
expect(graphql_data_at(:package, :composer_config_repository_url)).to eq("localhost/#{group.id}")
end
context 'with access to package registry for everyone' do
before do
project.project_feature.update!(package_registry_access_level: ProjectFeature::PUBLIC)
subject
end
it 'returns pypi_url correctly' do
expect(graphql_data_at(:package, :pypi_url)).to eq("http://__token__:<your_personal_token>@localhost/api/v4/projects/#{project.id}/packages/pypi/simple")
end
end
context 'when project is public' do
let_it_be(:public_project) { create(:project, :public, group: group) }
let_it_be(:composer_package) { create(:composer_package, project: public_project) }
let(:package_global_id) { global_id_of(composer_package) }
before do
subject
end
it 'returns pypi_url correctly' do
expect(graphql_data_at(:package, :pypi_url)).to eq("http://localhost/api/v4/projects/#{public_project.id}/packages/pypi/simple")
end
end
end
context 'web_path' do
before do
subject
end
it 'returns web_path correctly' do
expect(graphql_data_at(:package, :_links, :web_path)).to eq("/#{project.full_path}/-/packages/#{composer_package.id}")
end
context 'with terraform module' do
let_it_be(:terraform_package) { create(:terraform_module_package, project: project) }
let(:package_global_id) { global_id_of(terraform_package) }
it 'returns web_path correctly' do
expect(graphql_data_at(:package, :_links, :web_path)).to eq("/#{project.full_path}/-/infrastructure_registry/#{terraform_package.id}")
end
end
end
context 'public_package' do
context 'when project is private' do
let_it_be(:private_project) { create(:project, :private, group: group) }
let_it_be(:composer_package) { create(:composer_package, project: private_project) }
let(:package_global_id) { global_id_of(composer_package) }
before do
private_project.add_developer(user)
end
it 'returns false' do
subject
expect(graphql_data_at(:package, :public_package)).to eq(false)
end
context 'with access to package registry for everyone' do
before do
private_project.project_feature.update!(package_registry_access_level: ProjectFeature::PUBLIC)
subject
end
it 'returns true' do
expect(graphql_data_at(:package, :public_package)).to eq(true)
end
end
end
context 'when project is public' do
let_it_be(:public_project) { create(:project, :public, group: group) }
let_it_be(:composer_package) { create(:composer_package, project: public_project) }
let(:package_global_id) { global_id_of(composer_package) }
before do
subject
end
it 'returns true' do
expect(graphql_data_at(:package, :public_package)).to eq(true)
end
end
end
context 'with package that has no default status' do
before do
composer_package.update!(status: :error)
subject
end
it "does not return package's details" do
expect(package_details).to be_nil
end
end
end
end
|