pipeline_entity_spec.rb 2.82 KB
Newer Older
1 2 3
require 'spec_helper'

describe PipelineEntity do
4
  set(:user) { create(:user) }
5 6 7
  let(:request) { double('request') }

  before do
8 9
    stub_not_protect_default_branch

Fatih Acet's avatar
Fatih Acet committed
10
    allow(request).to receive(:current_user).and_return(user)
11 12 13 14 15 16 17 18 19 20 21 22 23
  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
24
        expect(subject).to include :id, :user, :path, :coverage, :source
25 26 27 28 29 30 31 32
        expect(subject).to include :ref, :commit
        expect(subject).to include :updated_at, :created_at
      end

      it 'contains details' do
        expect(subject).to include :details
        expect(subject[:details])
          .to include :duration, :finished_at
33
        expect(subject[:details][:status]).to include :icon, :favicon, :text, :label
34 35 36 37 38
      end

      it 'contains flags' do
        expect(subject).to include :flags
        expect(subject[:flags])
39
          .to include :latest, :stuck,
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
                      :yaml_errors, :retryable, :cancelable
      end
    end

    context 'when pipeline is retryable' do
      let(:project) { create(:empty_project) }

      let(:pipeline) do
        create(:ci_pipeline, status: :success, project: project)
      end

      before do
        create(:ci_build, :failed, pipeline: pipeline)
      end

      context 'user has ability to retry pipeline' do
56
        before do
57
          project.add_developer(user)
58
        end
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

        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
      let(:project) { create(:empty_project) }

      let(:pipeline) do
        create(:ci_pipeline, status: :running, project: project)
      end

      before do
        create(:ci_build, :pending, pipeline: pipeline)
      end

      context 'user has ability to cancel pipeline' do
84
        before do
85
          project.add_developer(user)
86
        end
87 88 89 90 91 92 93 94 95 96 97 98 99

        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

100 101 102 103 104 105 106 107 108 109 110
    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
111 112
  end
end