File: npm_packages_shared_examples.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (806 lines) | stat: -rw-r--r-- 25,363 bytes parent folder | download
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# frozen_string_literal: true

RSpec.shared_examples 'handling get metadata requests' do |scope: :project|
  include PackagesManagerApiSpecHelpers

  using RSpec::Parameterized::TableSyntax

  let_it_be(:package_dependency_link1) { create(:packages_dependency_link, package: package, dependency_type: :dependencies) }
  let_it_be(:package_dependency_link2) { create(:packages_dependency_link, package: package, dependency_type: :devDependencies) }
  let_it_be(:package_dependency_link3) { create(:packages_dependency_link, package: package, dependency_type: :bundleDependencies) }
  let_it_be(:package_dependency_link4) { create(:packages_dependency_link, package: package, dependency_type: :peerDependencies) }

  let_it_be(:package_metadatum) { create(:npm_metadatum, package: package) }

  let(:headers) { {} }

  subject { get(url, headers: headers) }

  shared_examples 'accept metadata request' do |status:|
    it 'accepts the metadata request' do
      subject

      expect(response).to have_gitlab_http_status(status)
      expect(response.media_type).to eq('application/json')
      expect(response).to match_response_schema('public_api/v4/packages/npm_package')
      expect(json_response['name']).to eq(package.name)
      expect(json_response['versions'][package.version]).to match_schema('public_api/v4/packages/npm_package_version')
      ::Packages::DependencyLink.dependency_types.keys.each do |dependency_type|
        expect(json_response.dig('versions', package.version, dependency_type.to_s)).to be_any
      end
      expect(json_response['dist-tags']).to match_schema('public_api/v4/packages/npm_package_tags')
    end
  end

  shared_examples 'reject metadata request' do |status:|
    it_behaves_like 'returning response status', status
  end

  shared_examples 'redirect metadata request' do |status:|
    it 'redirects metadata request' do
      subject

      expect(response).to have_gitlab_http_status(:found)
      expect(response.headers['Location']).to eq("https://registry.npmjs.org/#{package_name}")
    end
  end

  shared_examples 'handles authentication' do
    include_context 'dependency proxy helpers context'

    let(:package_name) { 'unscoped-package' }

    before do
      package.update!(name: package_name)

      set_npm_package_requests_forwarding(true, scope)
    end

    # No need to test the instance scope here.
    # Instance scope handling of unscoped packages is already tested in L180
    context 'with project or group scope', if: scope.in?(%i[project group]) do
      context 'when unauthenticated' do
        let(:headers) { {} }

        where(:visibility, :expected_result, :expected_status) do
          'public'   | 'accept metadata request' | :ok
          'internal' | 'reject metadata request' | :unauthorized
          'private'  | 'reject metadata request' | :unauthorized
        end

        with_them do
          before do
            set_visibility(visibility, scope)
          end

          it_behaves_like params[:expected_result], status: params[:expected_status]
        end
      end

      context 'when authenticated' do
        let(:headers) { build_token_auth_header(token.plaintext_token) }

        context 'with guest user' do
          before do
            set_user_role('guest', scope)
          end

          context 'with a non-private project' do
            before do
              set_visibility('internal', scope)
            end

            it_behaves_like 'accept metadata request', status: :ok
          end

          context 'with a private project' do
            before do
              set_visibility('private', scope)
            end

            it_behaves_like 'reject metadata request', status: :forbidden
          end
        end

        context 'with reporter user' do
          before do
            set_visibility('private', scope)
            set_user_role('reporter', scope)
          end

          it_behaves_like 'accept metadata request', status: :ok
        end

        context 'with authentication methods' do
          %i[oauth personal_access_token job_token deploy_token].each do |auth|
            context "with #{auth}" do
              let(:headers) do
                build_headers_for_auth_type(auth)
              end

              before do
                set_visibility('private', scope)
                set_user_role('reporter', scope)
              end

              it_behaves_like 'accept metadata request', status: :ok
            end
          end
        end
      end
    end
  end

  context 'with a group namespace' do
    it_behaves_like 'handles authentication'
  end

  context 'with a user namespace', if: scope != :project do
    let_it_be(:namespace) { user.namespace }

    it_behaves_like 'handles authentication'
  end

  context 'with a developer' do
    let(:headers) { build_token_auth_header(personal_access_token.token) }

    before do
      group.add_developer(user) if scope == :group
      project.add_developer(user)
    end

    context 'project path with a dot' do
      before do
        project.update!(path: 'foo.bar')
      end

      it_behaves_like 'accept metadata request', status: :ok
    end

    context 'with a job token for a completed job' do
      let(:headers) { build_token_auth_header(job.token) }

      before do
        job.update!(status: :success)
      end

      it_behaves_like 'reject metadata request', status: :unauthorized
    end
  end

  context 'with naming conventions', if: scope == :instance do
    where(:package_name_type, :expected_result, :expected_status) do
      :scoped_naming_convention    | 'accept metadata request'   | :ok
      :scoped_no_naming_convention | 'redirect metadata request' | :redirected
      :unscoped                    | 'redirect metadata request' | :redirected
    end

    with_them do
      include_context 'set package name from package name type'

      before do
        package.update!(name: package_name)
      end

      it_behaves_like params[:expected_result], status: params[:expected_status]
    end
  end

  context 'when the package does not exist' do
    include_context 'dependency proxy helpers context'

    let(:package_name) { 'non-existing-package' }
    let(:headers) do
      user_role ? build_token_auth_header(personal_access_token.token) : {}
    end

    before do
      set_npm_package_requests_forwarding(request_forward, scope)
      set_user_role(user_role, scope) if user_role
      set_visibility(visibility.to_s, scope) if visibility
    end

    context 'with project scope', if: scope == :project do
      where(:request_forward, :visibility, :user_role, :expected_result, :expected_status) do
        true  | :public  | nil       | 'redirect metadata request'            | :redirected
        false | :public  | nil       | 'returning response status with error' | :not_found
        false | :private | nil       | 'reject metadata request'              | :unauthorized
        false | :private | :guest    | 'reject metadata request'              | :forbidden
        false | :private | :reporter | 'returning response status with error' | :not_found
      end

      with_them do
        it_behaves_like params[:expected_result], status: params[:expected_status]
      end
    end

    context 'with group scope', if: scope == :group do
      where(:request_forward, :visibility, :user_role, :expected_result, :expected_status) do
        true  | :public  | nil    | 'redirect metadata request'            | :redirected
        true  | :public  | nil    | 'redirect metadata request'            | :redirected
        false | :private | nil    | 'reject metadata request'              | :unauthorized
        false | :private | :guest | 'returning response status with error' | :not_found
      end

      with_them do
        it_behaves_like params[:expected_result], status: params[:expected_status]
      end
    end

    context 'with instance scope', if: scope == :instance do
      where(:request_forward, :expected_result, :expected_status) do
        true  | 'redirect metadata request'            | :redirected
        false | 'returning response status with error' | :not_found
      end

      with_them do
        let(:user_role) { nil }
        let(:visibility) { nil }

        it_behaves_like params[:expected_result], status: params[:expected_status]
      end
    end
  end

  def set_user_role(user_role, scope)
    project.send(:"add_#{user_role}", user)
    group.send(:"add_#{user_role}", user) if scope == :group
  end

  def set_visibility(visibility, scope)
    project.update!(visibility: visibility)
    group.update!(visibility: visibility) if scope == :group
  end
end

RSpec.shared_examples 'handling audit request' do |path:, scope: :project|
  using RSpec::Parameterized::TableSyntax

  let(:headers) { {} }
  let(:params) do
    ActiveSupport::Gzip.compress(
      Gitlab::Json.dump({
                          '@gitlab-org/npm-test': ['1.0.6'],
                          'call-bind': ['1.0.2']
                        })
    )
  end

  let(:default_headers) do
    { 'HTTP_CONTENT_ENCODING' => 'gzip', 'CONTENT_TYPE' => 'application/json' }
  end

  subject { post(url, headers: headers.merge(default_headers), params: params) }

  shared_examples 'accept audit request' do |status:|
    it 'accepts the audit request' do
      subject

      expect(response).to have_gitlab_http_status(status)
      expect(response.media_type).to eq('application/json')
      expect(json_response).to eq([])
    end
  end

  shared_examples 'reject audit request' do |status:|
    it_behaves_like 'returning response status', status
  end

  shared_examples 'reject audit request with error' do |status:|
    it_behaves_like 'returning response status with error', status: status, error: 'Project not found'
  end

  shared_examples 'redirect audit request' do |status:|
    it 'redirects audit request' do
      subject

      expect(response).to have_gitlab_http_status(status)
      expect(response.headers['Location']).to eq("https://registry.npmjs.org/-/npm/v1/security/#{path}")
    end
  end

  context 'authentication' do
    include_context 'dependency proxy helpers context'

    context 'when unauthenticated' do
      let(:auth) { nil }
      let(:headers) { {} }

      it_behaves_like 'reject audit request', status: :unauthorized
    end

    context 'when authenticated' do
      let(:headers) { build_headers_for_auth_type(auth) }

      context 'with request_forward enabled' do
        let(:auth) { :oauth }

        before do
          set_npm_package_requests_forwarding(true, scope)
        end

        it_behaves_like 'redirect audit request', status: :temporary_redirect
      end

      context 'with request_forward disabled' do
        before do
          set_npm_package_requests_forwarding(false, scope)
        end

        context 'with project scope', if: scope == :project do
          before do
            project.update!(visibility: 'private')
          end

          context 'with guest user' do
            let(:auth) { :oauth }

            before do
              project.add_guest(user)
            end

            it_behaves_like 'reject audit request', status: :forbidden
          end

          %i[oauth personal_access_token job_token deploy_token].each do |auth|
            context "with #{auth}" do
              let(:auth) { auth }

              before do
                project.add_reporter(user)
              end

              it_behaves_like 'accept audit request', status: :ok
            end
          end
        end

        context 'with group or instance scope', if: %i[group instance].include?(scope) do
          %i[oauth personal_access_token job_token deploy_token].each do |auth|
            context "with #{auth}" do
              let(:auth) { auth }

              before do
                project.add_reporter(user)
              end

              it_behaves_like 'reject audit request with error', status: :not_found
            end
          end
        end
      end
    end
  end

  context 'with a developer' do
    let(:headers) { build_token_auth_header(personal_access_token.token) }

    before do
      project.add_developer(user)
    end

    context 'with a job token for a completed job' do
      let(:headers) { build_token_auth_header(job.token) }

      before do
        job.update!(status: :success)
      end

      it_behaves_like 'reject audit request', status: :unauthorized
    end
  end
end

RSpec.shared_examples 'handling get dist tags requests' do |scope: :project|
  using RSpec::Parameterized::TableSyntax

  let_it_be(:package_tag1) { create(:packages_tag, package: package) }
  let_it_be(:package_tag2) { create(:packages_tag, package: package) }

  let(:headers) { {} }

  subject { get(url, headers: headers) }

  shared_examples 'reject package tags request' do |status:|
    before do
      package.update!(name: package_name)
    end

    it_behaves_like 'returning response status', status
  end

  shared_examples 'handles authentication' do
    context 'when unauthenticated' do
      let(:headers) { {} }

      where(:visibility, :expected_result, :expected_status) do
        'public'   | 'accept package tags request' | :ok
        'private'  | 'reject package tags request' | :unauthorized
        'internal' | 'reject package tags request' | :unauthorized
      end

      with_them do
        before do
          project.update!(visibility: visibility.to_s)
        end

        it_behaves_like params[:expected_result], status: params[:expected_status]
      end
    end

    context 'when authenticated' do
      let(:headers) { build_token_auth_header(token.plaintext_token) }

      context 'with guest user' do
        let(:user_role) { :guest }

        before do
          project.add_guest(user)
        end

        context 'when internal' do
          before do
            project.update!(visibility: 'internal')
          end

          it_behaves_like 'accept package tags request', status: :ok
        end

        context 'when private' do
          before do
            project.update!(visibility: 'private')
          end

          it_behaves_like 'reject package tags request', status: :forbidden
        end
      end

      context 'with reporter user' do
        let(:user_role) { :reporter }

        before do
          project.update!(visibility: 'private')
          project.add_reporter(user)
        end

        context 'with authentication methods' do
          %i[oauth personal_access_token job_token deploy_token].each do |auth|
            context "with #{auth}" do
              let(:auth) { auth }
              let(:headers) do
                build_headers_for_auth_type(auth)
              end

              it_behaves_like 'accept package tags request', status: :ok
            end
          end
        end
      end
    end
  end

  context 'with a group namespace' do
    it_behaves_like 'handles authentication'
  end

  context 'with a user namespace', if: scope != :project do
    let_it_be(:namespace) { user.namespace }

    it_behaves_like 'handles authentication'
  end

  context 'with naming conventions', if: scope == :instance do
    where(:package_name_type, :expected_result, :expected_status) do
      :scoped_naming_convention    | 'accept package tags request'          | :ok
      :scoped_no_naming_convention | 'returning response status with error' | :not_found
      :unscoped                    | 'returning response status with error' | :not_found
    end

    with_them do
      before do
        package.update!(name: set_package_name_from_group_and_package_type(package_name_type, group))
      end

      it_behaves_like params[:expected_result], status: params[:expected_status]
    end
  end

  context 'when the package does not exist' do
    include_context 'dependency proxy helpers context'

    let(:package_name) { 'non-existing-package' }
    let(:headers) do
      user_role ? build_token_auth_header(personal_access_token.token) : {}
    end

    before do
      project.add_role(user, user_role) if user_role
      project.update!(visibility: visibility.to_s) if visibility
    end

    context 'with project scope', if: scope == :project do
      where(:visibility, :user_role, :expected_result, :expected_status) do
        :public   | nil       | 'returning response status with error' | :not_found
        :internal | nil       | 'reject package tags request'          | :unauthorized
        :public   | :guest    | 'returning response status with error' | :not_found
        :internal | :guest    | 'returning response status with error' | :not_found
        :private  | :guest    | 'reject package tags request'          | :forbidden
        :public   | :reporter | 'returning response status with error' | :not_found
      end

      with_them do
        it_behaves_like params[:expected_result], status: params[:expected_status]
      end
    end

    context 'with group scope', if: scope == :group do
      where(:visibility, :user_role, :expected_result, :expected_status) do
        :public   | nil       | 'returning response status with error' | :not_found
        :internal | nil       | 'reject package tags request'          | :unauthorized
        :public   | :guest    | 'returning response status with error' | :not_found
      end

      with_them do
        it_behaves_like params[:expected_result], status: params[:expected_status]
      end
    end

    context 'with instance scope', if: scope == :instance do
      where(:visibility, :user_role, :expected_result, :expected_status) do
        :public   | nil       | 'returning response status with error' | :not_found
        :internal | nil       | 'returning response status with error' | :not_found
        :public   | :guest    | 'returning response status with error' | :not_found
      end

      with_them do
        it_behaves_like params[:expected_result], status: params[:expected_status]
      end
    end
  end
end

RSpec.shared_examples 'handling create dist tag requests' do |scope: :project|
  include_context 'set package name from package name type'

  let_it_be(:tag_name) { 'test' }

  let(:params) { {} }
  let(:version) { package.version }
  let(:env) { { 'api.request.body': version } }
  let(:headers) { {} }

  shared_examples 'reject create package tag request' do |status:|
    before do
      package.update!(name: package_name) unless package_name == 'non-existing-package'
    end

    it_behaves_like 'returning response status', status
  end

  shared_examples 'handling all conditions' do
    subject { put(url, env: env, headers: headers) }

    context 'with unauthenticated requests' do
      let(:package_name) { 'unscoped-package' }

      it_behaves_like "reject create package tag request", status: :unauthorized
    end

    it_behaves_like 'handles non-existent packages, for tags create or delete',
      non_guest_role: :developer, action: :create, scope: scope
    it_behaves_like 'handles authenticated requests, for tags create or delete',
      non_guest_role: :developer, action: :create, scope: scope
  end

  context 'with a group namespace' do
    it_behaves_like 'handling all conditions'
  end

  if scope != :project
    context 'with a user namespace' do
      let_it_be(:namespace) { user.namespace }

      it_behaves_like 'handling all conditions'
    end
  end
end

RSpec.shared_examples 'handling delete dist tag requests' do |scope: :project|
  include_context 'set package name from package name type'

  let_it_be(:package_tag) { create(:packages_tag, package: package) }

  let(:tag_name) { package_tag.name }
  let(:headers) { {} }

  shared_examples 'reject delete package tag request' do |status:|
    before do
      package.update!(name: package_name) unless package_name == 'non-existing-package'
    end

    it_behaves_like 'returning response status', status
  end

  shared_examples 'handling all conditions' do
    subject { delete(url, headers: headers) }

    context 'with unauthenticated requests' do
      let(:package_name) { 'unscoped-package' }

      it_behaves_like "reject delete package tag request", status: :unauthorized
    end

    it_behaves_like 'handles non-existent packages, for tags create or delete', non_guest_role: :maintainer, action: :delete, scope: scope
    it_behaves_like 'handles authenticated requests, for tags create or delete', non_guest_role: :maintainer, action: :delete, scope: scope
  end

  context 'with a group namespace' do
    it_behaves_like 'handling all conditions'
  end

  if scope != :project
    context 'with a user namespace' do
      let_it_be(:namespace) { user.namespace }

      it_behaves_like 'handling all conditions'
    end
  end
end

RSpec.shared_examples 'handles non-existent packages, for tags create or delete' do |non_guest_role:, action:, scope: :project|
  using RSpec::Parameterized::TableSyntax

  let(:package_name) { 'non-existing-package' }
  let(:headers) do
    user_role ? build_token_auth_header(personal_access_token.token) : {}
  end

  before do
    project.add_role(user, user_role)
  end

  context 'with project scope', if: scope == :project do
    where(:user_role, :expected_result, :expected_status) do
      :guest         | "reject #{action} package tag request" | :forbidden
      non_guest_role | "reject #{action} package tag request" | :not_found
    end

    with_them do
      it_behaves_like params[:expected_result], status: params[:expected_status]
    end
  end

  context 'with group scope', if: scope == :group do
    let(:user_role) { :guest }

    it_behaves_like 'returning response status with error', status: :not_found
  end

  context 'with instance scope', if: scope == :instance do
    let(:user_role) { :guest }

    it_behaves_like "reject #{action} package tag request", status: :not_found
  end
end

RSpec.shared_examples 'handles authenticated requests, for tags create or delete' do |non_guest_role:, action:, scope:|
  let(:package_name_type) { :scoped_naming_convention }

  before do
    package.update!(name: set_package_name_from_group_and_package_type(package_name_type, group))
  end

  context 'with guest user' do
    let(:headers) { build_token_auth_header(token.plaintext_token) }
    let(:user_role) { :guest }

    %i[public internal private].each do |visibility|
      context "with #{visibility} project" do
        before do
          project.add_guest(user)
          project.update!(visibility: visibility.to_s)
        end

        it_behaves_like "reject #{action} package tag request", status: :forbidden
      end
    end
  end

  context 'with user having required role' do
    let(:headers) { build_token_auth_header(token.plaintext_token) }
    let(:user_role) { non_guest_role }

    before do
      project.send(:"add_#{non_guest_role}", user)
      project.update!(visibility: 'private')
    end

    context 'with authentication methods' do
      %i[oauth personal_access_token job_token deploy_token].each do |auth|
        context "with #{auth}" do
          let(:auth) { auth }
          let(:headers) do
            build_headers_for_auth_type(auth)
          end

          it_behaves_like "accept #{action} package tag request", status: :ok
        end
      end
    end
  end
end

RSpec.shared_examples 'rejects invalid package names' do
  let(:package_name) { "%0d%0ahttp:/%2fexample.com" }

  it do
    subject

    expect(response).to have_gitlab_http_status(:bad_request)
    expect(Gitlab::Json.parse(response.body)).to eq({ 'error' => 'package_name should be a valid file path' })
  end
end

RSpec.shared_examples 'handling get metadata requests for packages in multiple projects' do
  let_it_be(:project2) { create(:project, namespace: namespace) }
  let_it_be(:package2) do
    create(:npm_package,
      project: project2,
      name: "@#{group.path}/scoped_package",
      version: '1.2.0')
  end

  let(:headers) { build_token_auth_header(personal_access_token.token) }

  subject { get(url, headers: headers) }

  before_all do
    project.update!(visibility: 'private')

    group.add_guest(user)
    project.add_reporter(user)
    project2.add_reporter(user)
  end

  it 'includes all matching package versions in the response' do
    subject

    expect(json_response['versions'].keys).to match_array([package.version, package2.version])
  end

  context 'with the feature flag disabled' do
    before do
      stub_feature_flags(npm_allow_packages_in_multiple_projects: false)
    end

    it 'returns matching package versions from only one project' do
      subject

      expect(json_response['versions'].keys).to match_array([package2.version])
    end
  end

  context 'with limited access to the project with the last package version' do
    before_all do
      project2.add_guest(user)
    end

    it 'includes matching package versions from authorized projects in the response' do
      subject

      expect(json_response['versions'].keys).to contain_exactly(package.version)
    end
  end

  context 'with limited access to the project with the first package version' do
    before do
      project.add_guest(user)
    end

    it 'includes matching package versions from authorized projects in the response' do
      subject

      expect(json_response['versions'].keys).to contain_exactly(package2.version)
    end
  end
end