BigW Consortium Gitlab

build_presenter_spec.rb 3.85 KB
Newer Older
1 2
require 'spec_helper'

3
describe Ci::BuildPresenter do
4
  let(:project) { create(:project) }
5 6 7
  let(:pipeline) { create(:ci_pipeline, project: project) }
  let(:build) { create(:ci_build, pipeline: pipeline) }

8
  subject(:presenter) do
9 10 11 12 13 14 15 16 17
    described_class.new(build)
  end

  it 'inherits from Gitlab::View::Presenter::Delegated' do
    expect(described_class.superclass).to eq(Gitlab::View::Presenter::Delegated)
  end

  describe '#initialize' do
    it 'takes a build and optional params' do
18
      expect { presenter }.not_to raise_error
19 20 21
    end

    it 'exposes build' do
22
      expect(presenter.build).to eq(build)
23 24 25
    end

    it 'forwards missing methods to build' do
26
      expect(presenter.ref).to eq('master')
27 28 29 30 31
    end
  end

  describe '#erased_by_user?' do
    it 'takes a build and optional params' do
32
      expect(presenter).not_to be_erased_by_user
33 34 35 36 37 38
    end
  end

  describe '#erased_by_name' do
    context 'when build is not erased' do
      before do
39
        expect(presenter).to receive(:erased_by_user?).and_return(false)
40 41 42
      end

      it 'returns nil' do
43
        expect(presenter.erased_by_name).to be_nil
44 45
      end
    end
46

47 48
    context 'when build is erased' do
      before do
49
        expect(presenter).to receive(:erased_by_user?).and_return(true)
50 51
        expect(build).to receive(:erased_by)
          .and_return(double(:user, name: 'John Doe'))
52 53 54
      end

      it 'returns the name of the eraser' do
55
        expect(presenter.erased_by_name).to eq('John Doe')
56 57 58 59
      end
    end
  end

Lin Jen-Shin committed
60
  describe '#status_title' do
61
    context 'when build is auto-canceled' do
Lin Jen-Shin committed
62
      before do
63 64
        expect(build).to receive(:auto_canceled?).and_return(true)
        expect(build).to receive(:auto_canceled_by_id).and_return(1)
Lin Jen-Shin committed
65 66
      end

67
      it 'shows that the build is auto-canceled' do
68
        status_title = presenter.status_title
Lin Jen-Shin committed
69

70 71
        expect(status_title).to include('auto-canceled')
        expect(status_title).to include('Pipeline #1')
Lin Jen-Shin committed
72
      end
73
    end
Lin Jen-Shin committed
74

75 76 77 78
    context 'when build is not auto-canceled' do
      before do
        expect(build).to receive(:auto_canceled?).and_return(false)
      end
Lin Jen-Shin committed
79

80 81
      it 'does not have a status title' do
        expect(presenter.status_title).to be_nil
Lin Jen-Shin committed
82 83 84 85
      end
    end
  end

86 87
  describe 'quack like a Ci::Build permission-wise' do
    context 'user is not allowed' do
88
      let(:project) { create(:project, public_builds: false) }
89 90

      it 'returns false' do
91
        expect(presenter.can?(nil, :read_build)).to be_falsy
92 93 94 95
      end
    end

    context 'user is allowed' do
96
      let(:project) { create(:project, :public) }
97 98

      it 'returns true' do
99
        expect(presenter.can?(nil, :read_build)).to be_truthy
100 101 102
      end
    end
  end
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

  describe '#trigger_variables' do
    let(:build) { create(:ci_build, pipeline: pipeline, trigger_request: trigger_request) }
    let(:trigger) { create(:ci_trigger, project: project) }
    let(:trigger_request) { create(:ci_trigger_request, pipeline: pipeline, trigger: trigger) }

    context 'when variable is stored in ci_pipeline_variables' do
      let!(:pipeline_variable) { create(:ci_pipeline_variable, pipeline: pipeline) }

      context 'when pipeline is triggered by trigger API' do
        it 'returns variables' do
          expect(presenter.trigger_variables).to eq([pipeline_variable.to_runner_variable])
        end
      end

      context 'when pipeline is not triggered by trigger API' do
        let(:build) { create(:ci_build, pipeline: pipeline) }

        it 'does not return variables' do
          expect(presenter.trigger_variables).to eq([])
        end
      end
    end

    context 'when variable is stored in ci_trigger_requests.variables' do
      before do
        trigger_request.update_attribute(:variables, { 'TRIGGER_KEY_1' => 'TRIGGER_VALUE_1' } )
      end

      it 'returns variables' do
        expect(presenter.trigger_variables).to eq(trigger_request.user_variables)
      end
    end
  end
137
end