debian-mirror-gitlab/spec/serializers/pipeline_entity_spec.rb

221 lines
6.5 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
require 'spec_helper'
describe PipelineEntity do
2019-07-07 11:18:12 +05:30
include Gitlab::Routing
set(:project) { create(:project) }
2017-09-10 17:25:29 +05:30
set(:user) { create(:user) }
2019-07-07 11:18:12 +05:30
set(:project) { create(:project) }
2017-08-17 22:00:37 +05:30
let(:request) { double('request') }
before do
2017-09-10 17:25:29 +05:30
stub_not_protect_default_branch
2017-08-17 22:00:37 +05:30
allow(request).to receive(:current_user).and_return(user)
2019-07-07 11:18:12 +05:30
allow(request).to receive(:project).and_return(project)
2017-08-17 22:00:37 +05:30
end
let(:entity) do
described_class.represent(pipeline, request: request)
end
describe '#as_json' do
subject { entity.as_json }
context 'when pipeline is empty' do
let(:pipeline) { create(:ci_empty_pipeline) }
it 'contains required fields' do
2017-09-10 17:25:29 +05:30
expect(subject).to include :id, :user, :path, :coverage, :source
2017-08-17 22:00:37 +05:30
expect(subject).to include :ref, :commit
expect(subject).to include :updated_at, :created_at
end
2018-11-08 19:23:39 +05:30
it 'excludes coverage data when disabled' do
entity = described_class
.represent(pipeline, request: request, disable_coverage: true)
expect(entity.as_json).not_to include(:coverage)
end
2017-08-17 22:00:37 +05:30
it 'contains details' do
expect(subject).to include :details
expect(subject[:details])
2019-12-04 20:38:33 +05:30
.to include :duration, :finished_at, :name
2018-05-09 12:01:36 +05:30
expect(subject[:details][:status]).to include :icon, :favicon, :text, :label, :tooltip
2017-08-17 22:00:37 +05:30
end
it 'contains flags' do
expect(subject).to include :flags
expect(subject[:flags])
2019-09-04 21:01:54 +05:30
.to include :stuck, :auto_devops, :yaml_errors,
:retryable, :cancelable, :merge_request
2017-08-17 22:00:37 +05:30
end
end
context 'when pipeline is retryable' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project) }
2017-08-17 22:00:37 +05:30
let(:pipeline) do
create(:ci_pipeline, status: :success, project: project)
end
before do
create(:ci_build, :failed, pipeline: pipeline)
end
2019-09-04 21:01:54 +05:30
it 'does not serialize stage builds' do
subject.with_indifferent_access.dig(:details, :stages, 0).tap do |stage|
expect(stage).not_to include(:groups, :latest_statuses, :retries)
end
end
2017-08-17 22:00:37 +05:30
context 'user has ability to retry pipeline' do
2017-09-10 17:25:29 +05:30
before do
project.add_developer(user)
2017-08-17 22:00:37 +05:30
end
it 'contains retry path' do
expect(subject[:retry_path]).to be_present
end
end
context 'user does not have ability to retry pipeline' do
it 'does not contain retry path' do
expect(subject).not_to have_key(:retry_path)
end
end
end
context 'when pipeline is cancelable' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project) }
2017-08-17 22:00:37 +05:30
let(:pipeline) do
create(:ci_pipeline, status: :running, project: project)
end
before do
create(:ci_build, :pending, pipeline: pipeline)
end
2019-09-04 21:01:54 +05:30
it 'does not serialize stage builds' do
subject.with_indifferent_access.dig(:details, :stages, 0).tap do |stage|
expect(stage).not_to include(:groups, :latest_statuses, :retries)
end
end
2017-08-17 22:00:37 +05:30
context 'user has ability to cancel pipeline' do
2017-09-10 17:25:29 +05:30
before do
project.add_developer(user)
2017-08-17 22:00:37 +05:30
end
it 'contains cancel path' do
expect(subject[:cancel_path]).to be_present
end
end
context 'user does not have ability to cancel pipeline' do
it 'does not contain cancel path' do
expect(subject).not_to have_key(:cancel_path)
end
end
end
context 'when pipeline ref is empty' do
let(:pipeline) { create(:ci_empty_pipeline) }
before do
allow(pipeline).to receive(:ref).and_return(nil)
end
it 'does not generate branch path' do
expect(subject[:ref][:path]).to be_nil
end
end
2018-03-17 18:26:18 +05:30
context 'when pipeline has a failure reason set' do
let(:pipeline) { create(:ci_empty_pipeline) }
before do
pipeline.drop!(:config_error)
end
it 'has a correct failure reason' do
expect(subject[:failure_reason])
.to eq 'CI/CD YAML configuration error!'
end
end
2019-07-07 11:18:12 +05:30
context 'when pipeline is detached merge request pipeline' do
let(:merge_request) { create(:merge_request, :with_detached_merge_request_pipeline) }
let(:project) { merge_request.target_project }
2019-07-31 22:56:46 +05:30
let(:pipeline) { merge_request.pipelines_for_merge_request.first }
2019-07-07 11:18:12 +05:30
it 'makes detached flag true' do
expect(subject[:flags][:detached_merge_request_pipeline]).to be_truthy
end
it 'does not expose source sha and target sha' do
expect(subject[:source_sha]).to be_nil
expect(subject[:target_sha]).to be_nil
end
context 'when user is a developer' do
before do
project.add_developer(user)
end
it 'has merge request information' do
expect(subject[:merge_request][:iid]).to eq(merge_request.iid)
expect(project_merge_request_path(project, merge_request))
.to include(subject[:merge_request][:path])
expect(subject[:merge_request][:title]).to eq(merge_request.title)
expect(subject[:merge_request][:source_branch])
.to eq(merge_request.source_branch)
expect(project_commits_path(project, merge_request.source_branch))
.to include(subject[:merge_request][:source_branch_path])
expect(subject[:merge_request][:target_branch])
.to eq(merge_request.target_branch)
expect(project_commits_path(project, merge_request.target_branch))
.to include(subject[:merge_request][:target_branch_path])
end
end
context 'when user is an external user' do
it 'has no merge request information' do
expect(subject[:merge_request]).to be_nil
end
end
end
context 'when pipeline is merge request pipeline' do
let(:merge_request) { create(:merge_request, :with_merge_request_pipeline, merge_sha: 'abc') }
let(:project) { merge_request.target_project }
2019-07-31 22:56:46 +05:30
let(:pipeline) { merge_request.pipelines_for_merge_request.first }
2019-07-07 11:18:12 +05:30
it 'makes detached flag false' do
expect(subject[:flags][:detached_merge_request_pipeline]).to be_falsy
end
it 'makes atached flag true' do
expect(subject[:flags][:merge_request_pipeline]).to be_truthy
end
it 'exposes source sha and target sha' do
expect(subject[:source_sha]).to be_present
expect(subject[:target_sha]).to be_present
end
2019-12-04 20:38:33 +05:30
it 'exposes merge request event type' do
expect(subject[:merge_request_event_type]).to be_present
end
2019-07-07 11:18:12 +05:30
end
2017-08-17 22:00:37 +05:30
end
end