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

require 'spec_helper'

RSpec.describe API::Lint do
  describe 'POST /ci/lint' do
    context 'with valid .gitlab-ci.yaml content' do
      let(:yaml_content) do
        File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
      end

      it 'passes validation' do
        post api('/ci/lint'), 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['errors']).to eq([])
      end

      it 'outputs expanded yaml content' do
        post api('/ci/lint'), 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
    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'), params: { content: yaml_content }

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

        it 'outputs expanded yaml content' do
          post api('/ci/lint'), 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
      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'), params: { content: yaml_content }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response['status']).to eq('invalid')
          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'), 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
      end
    end

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

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