diff options
Diffstat (limited to 'spec/serializers/ci')
-rw-r--r-- | spec/serializers/ci/job_entity_spec.rb | 237 | ||||
-rw-r--r-- | spec/serializers/ci/job_serializer_spec.rb | 63 | ||||
-rw-r--r-- | spec/serializers/ci/pipeline_entity_spec.rb | 12 |
3 files changed, 312 insertions, 0 deletions
diff --git a/spec/serializers/ci/job_entity_spec.rb b/spec/serializers/ci/job_entity_spec.rb new file mode 100644 index 00000000000..ba68b9a6c16 --- /dev/null +++ b/spec/serializers/ci/job_entity_spec.rb @@ -0,0 +1,237 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Ci::JobEntity do + let(:user) { create(:user) } + let(:job) { create(:ci_build) } + let(:project) { job.project } + let(:request) { double('request') } + + before do + stub_not_protect_default_branch + allow(request).to receive(:current_user).and_return(user) + + project.add_developer(user) + end + + let(:entity) do + described_class.new(job, request: request) + end + + subject { entity.as_json } + + it 'contains complete to indicate if a pipeline is completed' do + expect(subject).to include(:complete) + end + + it 'contains paths to job page action' do + expect(subject).to include(:build_path) + end + + it 'does not contain sensitive information' do + expect(subject).not_to include(/token/) + expect(subject).not_to include(/variables/) + end + + it 'contains whether it is playable' do + expect(subject[:playable]).to eq job.playable? + end + + it 'contains timestamps' do + expect(subject).to include(:created_at, :updated_at) + end + + it 'contains details' do + expect(subject).to include :status + expect(subject[:status]).to include :icon, :favicon, :text, :label, :tooltip + end + + context 'when job is retryable' do + before do + job.update!(status: :failed) + end + + it 'contains cancel path' do + expect(subject).to include(:retry_path) + end + end + + context 'when job is cancelable' do + before do + job.update!(status: :running) + end + + it 'contains cancel path' do + expect(subject).to include(:cancel_path) + end + end + + context 'when job is a regular job' do + it 'does not contain path to play action' do + expect(subject).not_to include(:play_path) + end + + it 'is not a playable build' do + expect(subject[:playable]).to be false + end + end + + context 'when job is a manual action' do + let(:job) { create(:ci_build, :manual) } + + context 'when user is allowed to trigger action' do + before do + project.add_developer(user) + + create(:protected_branch, :developers_can_merge, + name: job.ref, project: job.project) + end + + it 'contains path to play action' do + expect(subject).to include(:play_path) + end + + it 'is a playable action' do + expect(subject[:playable]).to be true + end + end + + context 'when user is not allowed to trigger action' do + before do + allow(job.project).to receive(:empty_repo?).and_return(false) + + create(:protected_branch, :no_one_can_push, + name: job.ref, project: job.project) + end + + it 'does not contain path to play action' do + expect(subject).not_to include(:play_path) + end + + it 'is not a playable action' do + expect(subject[:playable]).to be false + end + end + end + + context 'when job is scheduled' do + let(:job) { create(:ci_build, :scheduled) } + + it 'contains path to unschedule action' do + expect(subject).to include(:unschedule_path) + end + + it 'contains scheduled_at' do + expect(subject[:scheduled]).to be_truthy + expect(subject[:scheduled_at]).to eq(job.scheduled_at) + end + end + + context 'when job is generic commit status' do + let(:job) { create(:generic_commit_status, target_url: 'http://google.com') } + + it 'contains paths to target action' do + expect(subject).to include(:build_path) + end + + it 'does not contain paths to other action paths' do + expect(subject).not_to include(:retry_path, :cancel_path, :play_path) + end + + it 'contains timestamps' do + expect(subject).to include(:created_at, :updated_at) + end + + it 'contains details' do + expect(subject).to include :status + expect(subject[:status]).to include :icon, :favicon, :text, :label, :tooltip + end + end + + context 'when job failed' do + let(:job) { create(:ci_build, :api_failure) } + + it 'contains details' do + expect(subject[:status]).to include :icon, :favicon, :text, :label, :tooltip + end + + it 'states that it failed' do + expect(subject[:status][:label]).to eq(s_('CiStatusLabel|failed')) + end + + it 'indicates the failure reason on tooltip' do + expect(subject[:status][:tooltip]).to eq("#{s_('CiStatusLabel|failed')} - (API failure)") + end + + it 'includes a callout message with a verbose output' do + expect(subject[:callout_message]).to eq('There has been an API failure, please try again') + end + + it 'states that it is not recoverable' do + expect(subject[:recoverable]).to be_truthy + end + end + + context 'when job is allowed to fail' do + let(:job) { create(:ci_build, :allowed_to_fail, :api_failure) } + + it 'contains details' do + expect(subject[:status]).to include :icon, :favicon, :text, :label, :tooltip + end + + it 'states that it failed' do + expect(subject[:status][:label]).to eq('failed (allowed to fail)') + end + + it 'indicates the failure reason on tooltip' do + expect(subject[:status][:tooltip]).to eq("#{s_('CiStatusLabel|failed')} - (API failure) (allowed to fail)") + end + + it 'includes a callout message with a verbose output' do + expect(subject[:callout_message]).to eq('There has been an API failure, please try again') + end + + it 'states that it is not recoverable' do + expect(subject[:recoverable]).to be_truthy + end + end + + context 'when the job failed with a script failure' do + let(:job) { create(:ci_build, :failed, :script_failure) } + + it 'does not include callout message or recoverable keys' do + expect(subject).not_to include('callout_message') + expect(subject).not_to include('recoverable') + end + end + + context 'when job failed and is recoverable' do + let(:job) { create(:ci_build, :api_failure) } + + it 'states it is recoverable' do + expect(subject[:recoverable]).to be_truthy + end + end + + context 'when job passed' do + let(:job) { create(:ci_build, :success) } + + it 'does not include callout message or recoverable keys' do + expect(subject).not_to include('callout_message') + expect(subject).not_to include('recoverable') + end + end + + context 'when job is a bridge' do + let(:job) { create(:ci_bridge) } + + it 'does not include build path' do + expect(subject).not_to include(:build_path) + end + + it 'does not include cancel path' do + expect(subject).not_to include(:cancel_path) + end + end +end diff --git a/spec/serializers/ci/job_serializer_spec.rb b/spec/serializers/ci/job_serializer_spec.rb new file mode 100644 index 00000000000..d47c9fdbf24 --- /dev/null +++ b/spec/serializers/ci/job_serializer_spec.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Ci::JobSerializer do + let(:user) { create(:user) } + + let(:serializer) do + described_class.new(current_user: user) + end + + subject { serializer.represent(resource) } + + describe '#represent' do + context 'when a single object is being serialized' do + let(:resource) { create(:ci_build) } + + it 'serializers the pipeline object' do + expect(subject[:id]).to eq resource.id + end + end + + context 'when multiple objects are being serialized' do + let(:resource) { create_list(:ci_build, 2) } + + it 'serializers the array of pipelines' do + expect(subject).not_to be_empty + end + end + end + + describe '#represent_status' do + context 'for a failed build' do + let(:resource) { create(:ci_build, :failed) } + let(:status) { resource.detailed_status(double('user')) } + + subject { serializer.represent_status(resource) } + + it 'serializes only status' do + expect(subject[:text]).to eq(status.text) + expect(subject[:label]).to eq('failed') + expect(subject[:tooltip]).to eq('failed - (unknown failure)') + expect(subject[:icon]).to eq(status.icon) + expect(subject[:favicon]).to match_asset_path("/assets/ci_favicons/#{status.favicon}.png") + end + end + + context 'for any other type of build' do + let(:resource) { create(:ci_build, :success) } + let(:status) { resource.detailed_status(double('user')) } + + subject { serializer.represent_status(resource) } + + it 'serializes only status' do + expect(subject[:text]).to eq(status.text) + expect(subject[:label]).to eq('passed') + expect(subject[:tooltip]).to eq('passed') + expect(subject[:icon]).to eq(status.icon) + expect(subject[:favicon]).to match_asset_path("/assets/ci_favicons/#{status.favicon}.png") + end + end + end +end diff --git a/spec/serializers/ci/pipeline_entity_spec.rb b/spec/serializers/ci/pipeline_entity_spec.rb index f79bbd91a0a..4d9ed9fc22f 100644 --- a/spec/serializers/ci/pipeline_entity_spec.rb +++ b/spec/serializers/ci/pipeline_entity_spec.rb @@ -260,5 +260,17 @@ RSpec.describe Ci::PipelineEntity do end end end + + context 'when pipeline has coverage' do + let_it_be(:pipeline) { create(:ci_pipeline, project: project, user: user) } + + before do + allow(pipeline).to receive(:coverage).and_return(35.0) + end + + it 'exposes the coverage' do + expect(subject[:coverage]).to eq('35.00') + end + end end end |