2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-13 13:57:16 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 14:08:28 -04:00
|
|
|
RSpec.describe Projects::Ci::LintsController do
|
2019-04-21 06:03:26 -04:00
|
|
|
include StubRequests
|
|
|
|
|
2018-03-13 13:57:16 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #show' do
|
|
|
|
context 'with enough privileges' do
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
get :show, params: { namespace_id: project.namespace, project_id: project }
|
2018-03-13 13:57:16 -04:00
|
|
|
end
|
|
|
|
|
2019-08-19 05:55:20 -04:00
|
|
|
it { expect(response).to be_successful }
|
2018-03-13 13:57:16 -04:00
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'renders show page' do
|
2018-03-13 13:57:16 -04:00
|
|
|
expect(response).to render_template :show
|
|
|
|
end
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'retrieves project' do
|
2018-03-13 13:57:16 -04:00
|
|
|
expect(assigns(:project)).to eq(project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without enough privileges' do
|
|
|
|
before do
|
|
|
|
project.add_guest(user)
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
get :show, params: { namespace_id: project.namespace, project_id: project }
|
2018-03-13 13:57:16 -04:00
|
|
|
end
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'responds with 404' do
|
2020-02-04 22:08:49 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2018-03-13 13:57:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST #create' do
|
2020-08-12 11:10:02 -04:00
|
|
|
subject { post :create, params: params }
|
|
|
|
|
|
|
|
let(:params) { { namespace_id: project.namespace, project_id: project, content: content } }
|
2019-09-18 10:02:45 -04:00
|
|
|
let(:remote_file_path) { 'https://gitlab.com/gitlab-org/gitlab-foss/blob/1234/.gitlab-ci-1.yml' }
|
2018-03-13 13:57:16 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
context 'with a valid gitlab-ci.yml' do
|
|
|
|
before do
|
2019-04-21 06:03:26 -04:00
|
|
|
stub_full_request(remote_file_path).to_return(body: remote_file_content)
|
2018-03-13 13:57:16 -04:00
|
|
|
project.add_developer(user)
|
2020-08-12 11:10:02 -04:00
|
|
|
end
|
2018-03-13 13:57:16 -04:00
|
|
|
|
2020-08-12 11:10:02 -04:00
|
|
|
shared_examples 'returns a successful validation' do
|
|
|
|
it 'returns successfully' do
|
|
|
|
subject
|
|
|
|
expect(response).to be_successful
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'render show page' do
|
|
|
|
subject
|
|
|
|
expect(response).to render_template :show
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retrieves project' do
|
|
|
|
subject
|
|
|
|
expect(assigns(:project)).to eq(project)
|
|
|
|
end
|
2018-03-13 13:57:16 -04:00
|
|
|
end
|
|
|
|
|
2020-08-12 11:10:02 -04:00
|
|
|
context 'using legacy validation (YamlProcessor)' do
|
|
|
|
it_behaves_like 'returns a successful validation'
|
2018-03-13 13:57:16 -04:00
|
|
|
|
2020-08-12 11:10:02 -04:00
|
|
|
it 'runs validations through YamlProcessor' do
|
|
|
|
expect(Gitlab::Ci::YamlProcessor).to receive(:new_with_validation_errors).and_call_original
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2018-03-13 13:57:16 -04:00
|
|
|
end
|
|
|
|
|
2020-08-12 11:10:02 -04:00
|
|
|
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
|
|
|
|
expect(Gitlab::Ci::YamlProcessor).to receive(:new_with_validation_errors).and_call_original
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
2018-03-13 13:57:16 -04:00
|
|
|
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)
|
|
|
|
end
|
|
|
|
|
2020-08-24 11:10:11 -04:00
|
|
|
it 'assigns result with errors' do
|
2020-08-12 11:10:02 -04:00
|
|
|
subject
|
|
|
|
|
2020-08-26 08:10:53 -04:00
|
|
|
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'
|
|
|
|
])
|
2018-03-13 13:57:16 -04:00
|
|
|
end
|
2020-08-12 11:10:02 -04:00
|
|
|
|
|
|
|
context 'with dry_run mode' do
|
|
|
|
subject { post :create, params: params.merge(dry_run: 'true') }
|
|
|
|
|
2020-08-24 11:10:11 -04:00
|
|
|
it 'assigns result with errors' do
|
2020-08-12 11:10:02 -04:00
|
|
|
subject
|
|
|
|
|
2020-08-26 08:10:53 -04:00
|
|
|
expect(assigns[:result].errors).to eq(['jobs rubocop config should implement a script: or a trigger: keyword'])
|
2020-08-12 11:10:02 -04:00
|
|
|
end
|
|
|
|
end
|
2018-03-13 13:57:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'without enough privileges' do
|
|
|
|
before do
|
|
|
|
project.add_guest(user)
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post :create, params: { namespace_id: project.namespace, project_id: project, content: content }
|
2018-03-13 13:57:16 -04:00
|
|
|
end
|
|
|
|
|
2019-04-05 04:43:27 -04:00
|
|
|
it 'responds with 404' do
|
2020-02-04 22:08:49 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2018-03-13 13:57:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|