lints_controller_spec.rb 5.6 KB
Newer Older
G
gfyoung 已提交
1 2
# frozen_string_literal: true

M
Mayra Cabrera 已提交
3 4
require 'spec_helper'

5
RSpec.describe Projects::Ci::LintsController do
6 7
  include StubRequests

8 9
  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:user) { create(:user) }
M
Mayra Cabrera 已提交
10 11 12 13 14 15 16 17 18 19

  before do
    sign_in(user)
  end

  describe 'GET #show' do
    context 'with enough privileges' do
      before do
        project.add_developer(user)

B
blackst0ne 已提交
20
        get :show, params: { namespace_id: project.namespace, project_id: project }
M
Mayra Cabrera 已提交
21 22
      end

23
      it { expect(response).to have_gitlab_http_status(:ok) }
M
Mayra Cabrera 已提交
24

25
      it 'renders show page' do
M
Mayra Cabrera 已提交
26 27 28
        expect(response).to render_template :show
      end

29
      it 'retrieves project' do
M
Mayra Cabrera 已提交
30 31 32 33 34 35 36 37
        expect(assigns(:project)).to eq(project)
      end
    end

    context 'without enough privileges' do
      before do
        project.add_guest(user)

B
blackst0ne 已提交
38
        get :show, params: { namespace_id: project.namespace, project_id: project }
M
Mayra Cabrera 已提交
39 40
      end

41
      it 'responds with 404' do
42
        expect(response).to have_gitlab_http_status(:not_found)
M
Mayra Cabrera 已提交
43 44 45 46 47
      end
    end
  end

  describe 'POST #create' do
48 49
    subject { post :create, params: params }

50 51
    let(:format) { :html }
    let(:params) { { namespace_id: project.namespace, project_id: project, content: content, format: format } }
52
    let(:remote_file_path) { 'https://gitlab.com/gitlab-org/gitlab-foss/blob/1234/.gitlab-ci-1.yml' }
M
Mayra Cabrera 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    let(:remote_file_content) do
      <<~HEREDOC
      before_script:
        - apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs
        - ruby -v
        - which ruby
        - bundle install --jobs $(nproc)  "${FLAGS[@]}"
      HEREDOC
    end

    let(:content) do
      <<~HEREDOC
      include:
        - #{remote_file_path}

      rubocop:
        script:
          - bundle exec rubocop
      HEREDOC
    end

75 76 77 78 79 80 81 82 83 84 85 86 87 88
    shared_examples 'successful request with format json' do
      context 'with format json' do
        let(:format) { :json }
        let(:parsed_body) { Gitlab::Json.parse(response.body) }

        it 'renders json' do
          expect(response).to have_gitlab_http_status :ok
          expect(response.content_type).to eq 'application/json'
          expect(parsed_body).to include('errors', 'warnings', 'jobs', 'valid')
          expect(parsed_body).to match_schema('entities/lint_result_entity')
        end
      end
    end

M
Mayra Cabrera 已提交
89 90
    context 'with a valid gitlab-ci.yml' do
      before do
91
        stub_full_request(remote_file_path).to_return(body: remote_file_content)
M
Mayra Cabrera 已提交
92
        project.add_developer(user)
93
      end
M
Mayra Cabrera 已提交
94

95
      shared_examples 'returns a successful validation' do
96
        before do
97 98 99
          subject
        end

100 101 102 103 104
        it 'returns successfully' do
          expect(response).to have_gitlab_http_status :ok
        end

        it 'renders show page' do
105 106 107 108 109 110
          expect(response).to render_template :show
        end

        it 'retrieves project' do
          expect(assigns(:project)).to eq(project)
        end
111 112

        it_behaves_like 'successful request with format json'
M
Mayra Cabrera 已提交
113 114
      end

115 116
      context 'using legacy validation (YamlProcessor)' do
        it_behaves_like 'returns a successful validation'
M
Mayra Cabrera 已提交
117

118
        it 'runs validations through YamlProcessor' do
119
          expect(Gitlab::Ci::YamlProcessor).to receive(:new).and_call_original
120 121 122

          subject
        end
M
Mayra Cabrera 已提交
123 124
      end

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
      context 'using dry_run mode' do
        subject { post :create, params: params.merge(dry_run: 'true') }

        it_behaves_like 'returns a successful validation'

        it 'runs validations through Ci::CreatePipelineService' do
          expect(Ci::CreatePipelineService)
            .to receive(:new)
            .with(project, user, ref: 'master')
            .and_call_original

          subject
        end

        context 'when dry_run feature flag is disabled' do
          before do
            stub_feature_flags(ci_lint_creates_pipeline_with_dry_run: false)
          end

          it_behaves_like 'returns a successful validation'

          it 'runs validations through YamlProcessor' do
147
            expect(Gitlab::Ci::YamlProcessor).to receive(:new).and_call_original
148 149 150 151

            subject
          end
        end
M
Mayra Cabrera 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165
      end
    end

    context 'with an invalid gitlab-ci.yml' do
      let(:content) do
        <<~HEREDOC
        rubocop:
          scriptt:
            - bundle exec rubocop
        HEREDOC
      end

      before do
        project.add_developer(user)
166
        subject
M
Mayra Cabrera 已提交
167 168
      end

169
      it 'assigns result with errors' do
170 171 172 173
        expect(assigns[:result].errors).to match_array([
          'jobs rubocop config should implement a script: or a trigger: keyword',
          'jobs config should contain at least one visible job'
        ])
M
Mayra Cabrera 已提交
174
      end
175

176 177 178 179 180 181
      it 'render show page' do
        expect(response).to render_template :show
      end

      it_behaves_like 'successful request with format json'

182 183 184
      context 'with dry_run mode' do
        subject { post :create, params: params.merge(dry_run: 'true') }

185
        it 'assigns result with errors' do
186
          expect(assigns[:result].errors).to eq(['jobs rubocop config should implement a script: or a trigger: keyword'])
187
        end
188 189

        it_behaves_like 'successful request with format json'
190
      end
M
Mayra Cabrera 已提交
191 192 193 194 195 196
    end

    context 'without enough privileges' do
      before do
        project.add_guest(user)

B
blackst0ne 已提交
197
        post :create, params: { namespace_id: project.namespace, project_id: project, content: content }
M
Mayra Cabrera 已提交
198 199
      end

200
      it 'responds with 404' do
201
        expect(response).to have_gitlab_http_status(:not_found)
M
Mayra Cabrera 已提交
202
      end
203 204 205 206 207 208 209 210

      context 'with format json' do
        let(:format) { :json }

        it 'responds with 404' do
          expect(response).to have_gitlab_http_status :not_found
        end
      end
M
Mayra Cabrera 已提交
211 212 213
    end
  end
end