summaryrefslogtreecommitdiff
path: root/spec/requests/api/lint_spec.rb
blob: 73bc4a5d1f3e8c6efbfb43553cc0cfb883cb1d83 (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe API::Lint do
  describe 'POST /ci/lint' do
    context 'when signup settings are disabled' do
      before do
        Gitlab::CurrentSettings.signup_enabled = false
      end

      context 'when unauthenticated' do
        it 'returns authentication error' do
          post api('/ci/lint'), params: { content: 'content' }

          expect(response).to have_gitlab_http_status(:unauthorized)
        end
      end

      context 'when authenticated' do
        let_it_be(:api_user) { create(:user) }

        it 'returns authorized' do
          post api('/ci/lint', api_user), params: { content: 'content' }

          expect(response).to have_gitlab_http_status(:ok)
        end
      end

      context 'when authenticated as external user' do
        let(:project) { create(:project) }
        let(:api_user) { create(:user, :external) }

        context 'when reporter in a project' do
          before do
            project.add_reporter(api_user)
          end

          it 'returns authorization failure' do
            post api('/ci/lint', api_user), params: { content: 'content' }

            expect(response).to have_gitlab_http_status(:unauthorized)
          end
        end

        context 'when developer in a project' do
          before do
            project.add_developer(api_user)
          end

          it 'returns authorization success' do
            post api('/ci/lint', api_user), params: { content: 'content' }

            expect(response).to have_gitlab_http_status(:ok)
          end
        end
      end
    end

    context 'when signup is enabled and not limited' do
      before do
        Gitlab::CurrentSettings.signup_enabled = true
        stub_application_setting(domain_allowlist: [], email_restrictions_enabled: false, require_admin_approval_after_user_signup: false)
      end

      context 'when unauthenticated' do
        it 'returns authorized success' do
          post api('/ci/lint'), params: { content: 'content' }

          expect(response).to have_gitlab_http_status(:ok)
        end
      end

      context 'when authenticated' do
        let_it_be(:api_user) { create(:user) }

        it 'returns authentication success' do
          post api('/ci/lint', api_user), params: { content: 'content' }

          expect(response).to have_gitlab_http_status(:ok)
        end
      end
    end

    context 'when limited signup is enabled' do
      before do
        stub_application_setting(domain_allowlist: ['www.gitlab.com'])
        Gitlab::CurrentSettings.signup_enabled = true
      end

      context 'when unauthenticated' do
        it 'returns unauthorized' do
          post api('/ci/lint'), params: { content: 'content' }

          expect(response).to have_gitlab_http_status(:unauthorized)
        end
      end

      context 'when authenticated' do
        let_it_be(:api_user) { create(:user) }

        it 'returns authentication success' do
          post api('/ci/lint', api_user), params: { content: 'content' }

          expect(response).to have_gitlab_http_status(:ok)
        end
      end
    end

    context 'when authenticated' do
      let_it_be(:api_user) { create(:user) }

      context 'with valid .gitlab-ci.yml content' do
        let(:yaml_content) do
          File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
        end

        it 'passes validation without warnings or errors' do
          post api('/ci/lint', api_user), params: { content: yaml_content }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response).to be_an Hash
          expect(json_response['status']).to eq('valid')
          expect(json_response['warnings']).to match_array([])
          expect(json_response['errors']).to match_array([])
        end

        it 'outputs expanded yaml content' do
          post api('/ci/lint', api_user), params: { content: yaml_content, include_merged_yaml: true }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response).to have_key('merged_yaml')
        end

        it 'outputs jobs' do
          post api('/ci/lint', api_user), params: { content: yaml_content, include_jobs: true }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response).to have_key('jobs')
        end
      end

      context 'with valid .gitlab-ci.yml with warnings' do
        let(:yaml_content) { { job: { script: 'ls', rules: [{ when: 'always' }] } }.to_yaml }

        it 'passes validation but returns warnings' do
          post api('/ci/lint', api_user), params: { content: yaml_content }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response['status']).to eq('valid')
          expect(json_response['warnings']).not_to be_empty
          expect(json_response['errors']).to match_array([])
        end
      end

      context 'with valid .gitlab-ci.yml using deprecated keywords' do
        let(:yaml_content) { { job: { script: 'ls', type: 'test' }, types: ['test'] }.to_yaml }

        it 'passes validation but returns warnings' do
          post api('/ci/lint', api_user), params: { content: yaml_content }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response['status']).to eq('valid')
          expect(json_response['warnings']).not_to be_empty
          expect(json_response['errors']).to match_array([])
        end
      end

      context 'with an invalid .gitlab-ci.yml' do
        context 'with invalid syntax' do
          let(:yaml_content) { 'invalid content' }

          it 'responds with errors about invalid syntax' do
            post api('/ci/lint', api_user), params: { content: yaml_content }

            expect(response).to have_gitlab_http_status(:ok)
            expect(json_response['status']).to eq('invalid')
            expect(json_response['warnings']).to eq([])
            expect(json_response['errors']).to eq(['Invalid configuration format'])
          end

          it 'outputs expanded yaml content' do
            post api('/ci/lint', api_user), params: { content: yaml_content, include_merged_yaml: true }

            expect(response).to have_gitlab_http_status(:ok)
            expect(json_response).to have_key('merged_yaml')
          end

          it 'outputs jobs' do
            post api('/ci/lint', api_user), params: { content: yaml_content, include_jobs: true }

            expect(response).to have_gitlab_http_status(:ok)
            expect(json_response).to have_key('jobs')
          end
        end

        context 'with invalid configuration' do
          let(:yaml_content) { '{ image: "ruby:2.7",  services: ["postgres"] }' }

          it 'responds with errors about invalid configuration' do
            post api('/ci/lint', api_user), params: { content: yaml_content }

            expect(response).to have_gitlab_http_status(:ok)
            expect(json_response['status']).to eq('invalid')
            expect(json_response['warnings']).to eq([])
            expect(json_response['errors']).to eq(['jobs config should contain at least one visible job'])
          end

          it 'outputs expanded yaml content' do
            post api('/ci/lint', api_user), params: { content: yaml_content, include_merged_yaml: true }

            expect(response).to have_gitlab_http_status(:ok)
            expect(json_response).to have_key('merged_yaml')
          end

          it 'outputs jobs' do
            post api('/ci/lint', api_user), params: { content: yaml_content, include_jobs: true }

            expect(response).to have_gitlab_http_status(:ok)
            expect(json_response).to have_key('jobs')
          end
        end
      end

      context 'without the content parameter' do
        it 'responds with validation error about missing content' do
          post api('/ci/lint', api_user)

          expect(response).to have_gitlab_http_status(:bad_request)
          expect(json_response['error']).to eq('content is missing')
        end
      end
    end
  end

  describe 'GET /projects/:id/ci/lint' do
    subject(:ci_lint) { get api("/projects/#{project.id}/ci/lint", api_user), params: { dry_run: dry_run, include_jobs: include_jobs } }

    let(:project) { create(:project, :repository) }
    let(:dry_run) { nil }
    let(:include_jobs) { nil }

    RSpec.shared_examples 'valid config with warnings' do
      it 'passes validation with warnings' do
        ci_lint

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response['valid']).to eq(true)
        expect(json_response['errors']).to eq([])
        expect(json_response['warnings']).not_to be_empty
      end
    end

    RSpec.shared_examples 'valid config without warnings' do
      it 'passes validation' do
        ci_lint

        included_config = YAML.safe_load(included_content, [Symbol])
        root_config = YAML.safe_load(yaml_content, [Symbol])
        expected_yaml = included_config.merge(root_config).except(:include).deep_stringify_keys.to_yaml

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).to be_an Hash
        expect(json_response['merged_yaml']).to eq(expected_yaml)
        expect(json_response['valid']).to eq(true)
        expect(json_response['warnings']).to eq([])
        expect(json_response['errors']).to eq([])
      end
    end

    RSpec.shared_examples 'invalid config' do
      it 'responds with errors about invalid configuration' do
        ci_lint

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response['merged_yaml']).to eq(yaml_content)
        expect(json_response['valid']).to eq(false)
        expect(json_response['warnings']).to eq([])
        expect(json_response['errors']).to eq(['jobs config should contain at least one visible job'])
      end
    end

    context 'when unauthenticated' do
      let_it_be(:api_user) { nil }

      it 'returns authentication error' do
        ci_lint

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end

    context 'when authenticated as non-member' do
      let_it_be(:api_user) { create(:user) }

      let(:yaml_content) do
        { include: { local: 'another-gitlab-ci.yml' }, test: { stage: 'test', script: 'echo 1' } }.to_yaml
      end

      context 'when project is private' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
          stub_ci_pipeline_yaml_file(yaml_content)
        end

        it 'returns authentication error' do
          ci_lint

          expect(response).to have_gitlab_http_status(:not_found)
        end
      end

      context 'when project is public' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
        end

        context 'when running as dry run' do
          let(:dry_run) { true }

          before do
            stub_ci_pipeline_yaml_file(yaml_content)
          end

          it 'returns pipeline creation error' do
            ci_lint

            expect(response).to have_gitlab_http_status(:ok)
            expect(json_response['merged_yaml']).to eq(nil)
            expect(json_response['valid']).to eq(false)
            expect(json_response['warnings']).to eq([])
            expect(json_response['errors']).to eq(['Insufficient permissions to create a new pipeline'])
          end
        end

        context 'when running static validation' do
          let(:dry_run) { false }

          let(:included_content) do
            { another_test: { stage: 'test', script: 'echo 1' } }.deep_stringify_keys.to_yaml
          end

          before do
            project.repository.create_file(
              project.creator,
              '.gitlab-ci.yml',
              yaml_content,
              message: 'Automatically created .gitlab-ci.yml',
              branch_name: 'master'
            )

            project.repository.create_file(
              project.creator,
              'another-gitlab-ci.yml',
              included_content,
              message: 'Automatically created another-gitlab-ci.yml',
              branch_name: 'master'
            )
          end

          it_behaves_like 'valid config without warnings'
        end
      end
    end

    context 'when authenticated as project guest' do
      let_it_be(:api_user) { create(:user) }

      before do
        project.add_guest(api_user)
      end

      it 'returns authentication error' do
        ci_lint

        expect(response).to have_gitlab_http_status(:forbidden)
      end
    end

    context 'when authenticated as project developer' do
      let_it_be(:api_user) { create(:user) }

      before do
        project.add_developer(api_user)
      end

      context 'with no commit' do
        it 'returns error about providing content' do
          ci_lint

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response['errors']).to match_array(['Please provide content of .gitlab-ci.yml'])
        end
      end

      context 'with valid .gitlab-ci.yml content' do
        let(:yaml_content) do
          { include: { local: 'another-gitlab-ci.yml' }, test: { stage: 'test', script: 'echo 1' } }.to_yaml
        end

        let(:included_content) do
          { another_test: { stage: 'test', script: 'echo 1' } }.deep_stringify_keys.to_yaml
        end

        before do
          project.repository.create_file(
            project.creator,
            '.gitlab-ci.yml',
            yaml_content,
            message: 'Automatically created .gitlab-ci.yml',
            branch_name: 'master'
          )

          project.repository.create_file(
            project.creator,
            'another-gitlab-ci.yml',
            included_content,
            message: 'Automatically created another-gitlab-ci.yml',
            branch_name: 'master'
          )
        end

        context 'when running as dry run' do
          let(:dry_run) { true }

          it_behaves_like 'valid config without warnings'
        end

        context 'when running static validation' do
          let(:dry_run) { false }

          it_behaves_like 'valid config without warnings'
        end

        context 'when running with include jobs' do
          let(:include_jobs) { true }

          it_behaves_like 'valid config without warnings'

          it 'returns jobs key' do
            ci_lint

            expect(json_response).to have_key('jobs')
          end
        end

        context 'when running without include jobs' do
          let(:include_jobs) { false }

          it_behaves_like 'valid config without warnings'

          it 'does not return jobs key' do
            ci_lint

            expect(json_response).not_to have_key('jobs')
          end
        end

        context 'With warnings' do
          let(:yaml_content) { { job: { script: 'ls', rules: [{ when: 'always' }] } }.to_yaml }

          it_behaves_like 'valid config with warnings'
        end
      end

      context 'with invalid .gitlab-ci.yml content' do
        let(:yaml_content) do
          { image: 'ruby:2.7', services: ['postgres'] }.deep_stringify_keys.to_yaml
        end

        before do
          stub_ci_pipeline_yaml_file(yaml_content)
        end

        context 'when running as dry run' do
          let(:dry_run) { true }

          it_behaves_like 'invalid config'
        end

        context 'when running static validation' do
          let(:dry_run) { false }

          it_behaves_like 'invalid config'
        end

        context 'when running with include jobs' do
          let(:include_jobs) { true }

          it_behaves_like 'invalid config'

          it 'returns jobs key' do
            ci_lint

            expect(json_response).to have_key('jobs')
          end
        end

        context 'when running without include jobs' do
          let(:include_jobs) { false }

          it_behaves_like 'invalid config'

          it 'does not return jobs key' do
            ci_lint

            expect(json_response).not_to have_key('jobs')
          end
        end
      end
    end
  end

  describe 'POST /projects/:id/ci/lint' do
    subject(:ci_lint) { post api("/projects/#{project.id}/ci/lint", api_user), params: { dry_run: dry_run, content: yaml_content, include_jobs: include_jobs } }

    let(:project) { create(:project, :repository) }
    let(:dry_run) { nil }
    let(:include_jobs) { nil }

    let_it_be(:api_user) { create(:user) }

    let_it_be(:yaml_content) do
      { include: { local: 'another-gitlab-ci.yml' }, test: { stage: 'test', script: 'echo 1' } }.to_yaml
    end

    let_it_be(:included_content) do
      { another_test: { stage: 'test', script: 'echo 1' } }.to_yaml
    end

    RSpec.shared_examples 'valid project config' do
      it 'passes validation' do
        ci_lint

        included_config = YAML.safe_load(included_content, [Symbol])
        root_config = YAML.safe_load(yaml_content, [Symbol])
        expected_yaml = included_config.merge(root_config).except(:include).deep_stringify_keys.to_yaml

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response).to be_an Hash
        expect(json_response['merged_yaml']).to eq(expected_yaml)
        expect(json_response['valid']).to eq(true)
        expect(json_response['errors']).to eq([])
      end
    end

    RSpec.shared_examples 'invalid project config' do
      it 'responds with errors about invalid configuration' do
        ci_lint

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response['merged_yaml']).to eq(yaml_content)
        expect(json_response['valid']).to eq(false)
        expect(json_response['errors']).to eq(['jobs config should contain at least one visible job'])
      end
    end

    context 'with an empty repository' do
      let_it_be(:empty_project) { create(:project_empty_repo) }
      let_it_be(:yaml_content) do
        File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
      end

      before do
        empty_project.add_developer(api_user)
      end

      it 'passes validation without errors' do
        post api("/projects/#{empty_project.id}/ci/lint", api_user), params: { content: yaml_content }
        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response['valid']).to eq(true)
        expect(json_response['errors']).to eq([])
      end
    end

    context 'when unauthenticated' do
      let_it_be(:api_user) { nil }

      it 'returns authentication error' do
        ci_lint

        expect(response).to have_gitlab_http_status(:not_found)
      end

      context 'when project is public' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
        end

        it 'returns authentication error' do
          ci_lint

          expect(response).to have_gitlab_http_status(:forbidden)
        end
      end
    end

    context 'when authenticated as non-member' do
      context 'when project is private' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
        end

        it 'returns authentication error' do
          ci_lint

          expect(response).to have_gitlab_http_status(:not_found)
        end
      end

      context 'when project is public' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
        end

        context 'when running as dry run' do
          let(:dry_run) { true }

          it 'returns authentication error' do
            ci_lint

            expect(response).to have_gitlab_http_status(:forbidden)
          end
        end

        context 'when running static validation' do
          let(:dry_run) { false }

          before do
            project.repository.create_file(
              project.creator,
              'another-gitlab-ci.yml',
              included_content,
              message: 'Automatically created another-gitlab-ci.yml',
              branch_name: 'master'
            )
          end

          it 'returns authentication error' do
            ci_lint

            expect(response).to have_gitlab_http_status(:forbidden)
          end
        end
      end
    end

    context 'when authenticated as project guest' do
      before do
        project.add_guest(api_user)
      end

      it 'returns authentication error' do
        ci_lint

        expect(response).to have_gitlab_http_status(:forbidden)
      end
    end

    context 'when authenticated as project developer' do
      before do
        project.add_developer(api_user)
      end

      context 'with valid .gitlab-ci.yml content' do
        before do
          project.repository.create_file(
            project.creator,
            'another-gitlab-ci.yml',
            included_content,
            message: 'Automatically created another-gitlab-ci.yml',
            branch_name: 'master'
          )
        end

        context 'when running as dry run' do
          let(:dry_run) { true }

          it_behaves_like 'valid project config'
        end

        context 'when running static validation' do
          let(:dry_run) { false }

          it_behaves_like 'valid project config'
        end

        context 'when running with include jobs param' do
          let(:include_jobs) { true }

          it_behaves_like 'valid project config'

          it 'contains jobs key' do
            ci_lint

            expect(json_response).to have_key('jobs')
          end
        end

        context 'when running without include jobs param' do
          let(:include_jobs) { false }

          it_behaves_like 'valid project config'

          it 'does not contain jobs key' do
            ci_lint

            expect(json_response).not_to have_key('jobs')
          end
        end
      end

      context 'with invalid .gitlab-ci.yml content' do
        let(:yaml_content) do
          { image: 'ruby:2.7', services: ['postgres'] }.deep_stringify_keys.to_yaml
        end

        context 'when running as dry run' do
          let(:dry_run) { true }

          it_behaves_like 'invalid project config'
        end

        context 'when running static validation' do
          let(:dry_run) { false }

          it_behaves_like 'invalid project config'
        end

        context 'when running with include jobs set to false' do
          let(:include_jobs) { false }

          it_behaves_like 'invalid project config'

          it 'does not contain jobs key' do
            ci_lint

            expect(json_response).not_to have_key('jobs')
          end
        end

        context 'when running with param include jobs' do
          let(:include_jobs) { true }

          it_behaves_like 'invalid project config'

          it 'contains jobs key' do
            ci_lint

            expect(json_response).to have_key('jobs')
          end
        end
      end
    end
  end
end