debian-mirror-gitlab/spec/controllers/projects/ci/lints_controller_spec.rb

176 lines
4.5 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2018-05-09 12:01:36 +05:30
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe Projects::Ci::LintsController do
2019-06-05 12:25:43 +05:30
include StubRequests
2020-11-24 15:15:51 +05:30
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) }
2018-05-09 12:01:36 +05:30
before do
sign_in(user)
end
describe 'GET #show' do
context 'with enough privileges' do
before do
project.add_developer(user)
2019-02-15 15:39:39 +05:30
get :show, params: { namespace_id: project.namespace, project_id: project }
2018-05-09 12:01:36 +05:30
end
2020-11-24 15:15:51 +05:30
it { expect(response).to have_gitlab_http_status(:ok) }
2018-05-09 12:01:36 +05:30
2019-07-07 11:18:12 +05:30
it 'renders show page' do
2018-05-09 12:01:36 +05:30
expect(response).to render_template :show
end
2019-07-07 11:18:12 +05:30
it 'retrieves project' do
2018-05-09 12:01:36 +05:30
expect(assigns(:project)).to eq(project)
end
end
context 'without enough privileges' do
before do
project.add_guest(user)
2019-02-15 15:39:39 +05:30
get :show, params: { namespace_id: project.namespace, project_id: project }
2018-05-09 12:01:36 +05:30
end
2019-07-07 11:18:12 +05:30
it 'responds with 404' do
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-05-09 12:01:36 +05:30
end
end
end
describe 'POST #create' do
2020-10-24 23:57:45 +05:30
subject { post :create, params: params }
2021-01-29 00:20:46 +05:30
let(:params) { { namespace_id: project.namespace, project_id: project, content: content, format: :json } }
2019-12-04 20:38:33 +05:30
let(:remote_file_path) { 'https://gitlab.com/gitlab-org/gitlab-foss/blob/1234/.gitlab-ci-1.yml' }
2021-01-29 00:20:46 +05:30
let(:parsed_body) { Gitlab::Json.parse(response.body) }
2018-05-09 12:01:36 +05:30
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
2021-01-29 00:20:46 +05:30
shared_examples 'returns a successful validation' do
before do
subject
end
2020-11-24 15:15:51 +05:30
2021-01-29 00:20:46 +05:30
it 'returns successfully' do
expect(response).to have_gitlab_http_status :ok
end
it 'renders json' do
2021-02-22 17:27:13 +05:30
expect(response.media_type).to eq 'application/json'
2021-01-29 00:20:46 +05:30
expect(parsed_body).to include('errors', 'warnings', 'jobs', 'valid')
expect(parsed_body).to match_schema('entities/lint_result_entity')
end
it 'retrieves project' do
expect(assigns(:project)).to eq(project)
2020-11-24 15:15:51 +05:30
end
end
2018-05-09 12:01:36 +05:30
context 'with a valid gitlab-ci.yml' do
before do
2019-06-05 12:25:43 +05:30
stub_full_request(remote_file_path).to_return(body: remote_file_content)
2018-05-09 12:01:36 +05:30
project.add_developer(user)
2020-10-24 23:57:45 +05:30
end
2018-05-09 12:01:36 +05:30
2021-01-29 00:20:46 +05:30
it_behaves_like 'returns a successful validation'
2018-05-09 12:01:36 +05:30
2020-10-24 23:57:45 +05:30
context 'using legacy validation (YamlProcessor)' do
it_behaves_like 'returns a successful validation'
2018-05-09 12:01:36 +05:30
2020-10-24 23:57:45 +05:30
it 'runs validations through YamlProcessor' do
2020-11-24 15:15:51 +05:30
expect(Gitlab::Ci::YamlProcessor).to receive(:new).and_call_original
2020-10-24 23:57:45 +05:30
subject
end
2018-05-09 12:01:36 +05:30
end
2020-10-24 23:57:45 +05:30
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
2018-05-09 12:01:36 +05:30
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)
2020-11-24 15:15:51 +05:30
subject
2018-05-09 12:01:36 +05:30
end
2021-01-29 00:20:46 +05:30
it_behaves_like 'returns a successful validation'
2020-11-24 15:15:51 +05:30
it 'assigns result with errors' do
2021-01-29 00:20:46 +05:30
expect(parsed_body['errors']).to match_array([
2020-11-24 15:15:51 +05:30
'jobs rubocop config should implement a script: or a trigger: keyword',
'jobs config should contain at least one visible job'
])
end
2020-10-24 23:57:45 +05:30
context 'with dry_run mode' do
subject { post :create, params: params.merge(dry_run: 'true') }
2020-11-24 15:15:51 +05:30
it 'assigns result with errors' do
2021-01-29 00:20:46 +05:30
expect(parsed_body['errors']).to eq(['jobs rubocop config should implement a script: or a trigger: keyword'])
2020-10-24 23:57:45 +05:30
end
2020-11-24 15:15:51 +05:30
2021-01-29 00:20:46 +05:30
it_behaves_like 'returns a successful validation'
2020-10-24 23:57:45 +05:30
end
2018-05-09 12:01:36 +05:30
end
context 'without enough privileges' do
before do
project.add_guest(user)
2019-02-15 15:39:39 +05:30
post :create, params: { namespace_id: project.namespace, project_id: project, content: content }
2018-05-09 12:01:36 +05:30
end
2019-07-07 11:18:12 +05:30
it 'responds with 404' do
2020-03-13 15:44:24 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-05-09 12:01:36 +05:30
end
end
end
end