2021-02-22 17:27:13 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Resolvers::Ci::ConfigResolver do
|
|
|
|
include GraphqlHelpers
|
|
|
|
|
|
|
|
describe '#resolve' do
|
2022-05-07 20:08:51 +05:30
|
|
|
let_it_be(:user) { create(:user) }
|
2022-07-01 11:34:44 +05:30
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
2022-05-07 20:08:51 +05:30
|
|
|
let_it_be(:sha) { nil }
|
|
|
|
|
|
|
|
let_it_be(:content) do
|
|
|
|
File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci_includes.yml'))
|
|
|
|
end
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
subject(:response) do
|
|
|
|
resolve(described_class,
|
2021-09-30 23:02:18 +05:30
|
|
|
args: { project_path: project.full_path, content: content, sha: sha },
|
2021-03-08 18:12:59 +05:30
|
|
|
ctx: { current_user: user })
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
shared_examples 'a valid config file' do
|
2021-02-22 17:27:13 +05:30
|
|
|
let(:fake_result) do
|
2021-03-08 18:12:59 +05:30
|
|
|
::Gitlab::Ci::Lint::Result.new(
|
|
|
|
merged_yaml: content,
|
|
|
|
jobs: [],
|
2021-02-22 17:27:13 +05:30
|
|
|
errors: [],
|
|
|
|
warnings: []
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'lints the ci config file and returns the merged yaml file' do
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(response[:status]).to eq(:valid)
|
2021-09-30 23:02:18 +05:30
|
|
|
expect(response[:merged_yaml]).to eq(content)
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(response[:errors]).to be_empty
|
2021-09-30 23:02:18 +05:30
|
|
|
expect(::Gitlab::Ci::Lint).to have_received(:new).with(current_user: user, project: project, sha: sha)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-01 11:34:44 +05:30
|
|
|
context 'when the user can create a pipeline' do
|
|
|
|
let(:ci_lint) do
|
|
|
|
ci_lint_double = instance_double(::Gitlab::Ci::Lint)
|
|
|
|
allow(ci_lint_double).to receive(:validate).and_return(fake_result)
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2022-07-01 11:34:44 +05:30
|
|
|
ci_lint_double
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
2022-07-01 11:34:44 +05:30
|
|
|
before do
|
|
|
|
allow(::Gitlab::Ci::Lint).to receive(:new).and_return(ci_lint)
|
|
|
|
|
|
|
|
project.add_developer(user)
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
2022-07-01 11:34:44 +05:30
|
|
|
context 'with a valid .gitlab-ci.yml' do
|
|
|
|
context 'with a sha' do
|
|
|
|
let(:sha) { '1231231' }
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2022-07-01 11:34:44 +05:30
|
|
|
it_behaves_like 'a valid config file'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without a sha' do
|
|
|
|
it_behaves_like 'a valid config file'
|
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
2022-07-01 11:34:44 +05:30
|
|
|
context 'with an invalid .gitlab-ci.yml' do
|
|
|
|
let(:content) { 'invalid' }
|
|
|
|
|
|
|
|
let(:fake_result) do
|
|
|
|
Gitlab::Ci::Lint::Result.new(
|
|
|
|
jobs: [],
|
|
|
|
merged_yaml: content,
|
|
|
|
errors: ['Invalid configuration format'],
|
|
|
|
warnings: []
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with errors about invalid syntax' do
|
|
|
|
expect(response[:status]).to eq(:invalid)
|
|
|
|
expect(response[:errors]).to match_array(['Invalid configuration format'])
|
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
2022-05-07 20:08:51 +05:30
|
|
|
|
2022-07-01 11:34:44 +05:30
|
|
|
context 'with an invalid SHA' do
|
|
|
|
let_it_be(:sha) { ':' }
|
2022-05-07 20:08:51 +05:30
|
|
|
|
2022-07-01 11:34:44 +05:30
|
|
|
let(:ci_lint) do
|
|
|
|
ci_lint_double = instance_double(::Gitlab::Ci::Lint)
|
|
|
|
allow(ci_lint_double).to receive(:validate).and_raise(GRPC::InvalidArgument)
|
2022-05-07 20:08:51 +05:30
|
|
|
|
2022-07-01 11:34:44 +05:30
|
|
|
ci_lint_double
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs the invalid SHA to Sentry' do
|
|
|
|
expect(Gitlab::ErrorTracking).to receive(:track_and_raise_exception)
|
|
|
|
.with(GRPC::InvalidArgument, sha: ':')
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
2022-05-07 20:08:51 +05:30
|
|
|
end
|
2022-07-01 11:34:44 +05:30
|
|
|
end
|
2022-05-07 20:08:51 +05:30
|
|
|
|
2022-07-01 11:34:44 +05:30
|
|
|
context 'when the user cannot create a pipeline' do
|
|
|
|
before do
|
|
|
|
project.add_guest(user)
|
|
|
|
end
|
2022-05-07 20:08:51 +05:30
|
|
|
|
2022-07-01 11:34:44 +05:30
|
|
|
it 'returns an error stating that the user cannot access the linting' do
|
|
|
|
expect { response }.to raise_error(::Gitlab::Graphql::Errors::ResourceNotAvailable)
|
2022-05-07 20:08:51 +05:30
|
|
|
end
|
|
|
|
end
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
end
|