summaryrefslogtreecommitdiff
path: root/spec/services/ci/play_bridge_service_spec.rb
blob: d6130325b5a5bffb98c856156db67450687e6444 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::PlayBridgeService, '#execute' do
  let(:project) { create(:project) }
  let(:user) { create(:user) }
  let(:pipeline) { create(:ci_pipeline, project: project) }
  let(:downstream_project) { create(:project) }
  let(:bridge) { create(:ci_bridge, :playable, pipeline: pipeline, downstream: downstream_project) }
  let(:instance) { described_class.new(project, user) }

  subject(:execute_service) { instance.execute(bridge) }

  context 'when user can run the bridge' do
    before do
      allow(instance).to receive(:can?).with(user, :play_job, bridge).and_return(true)
    end

    it 'marks the bridge pending' do
      execute_service

      expect(bridge.reload).to be_pending
    end

    it 'enqueues Ci::CreateCrossProjectPipelineWorker' do
      expect(::Ci::CreateCrossProjectPipelineWorker).to receive(:perform_async).with(bridge.id)

      execute_service
    end

    it "updates bridge's user" do
      execute_service

      expect(bridge.reload.user).to eq(user)
    end

    context 'when a subsequent job is skipped' do
      let!(:job) { create(:ci_build, :skipped, pipeline: pipeline, stage_idx: bridge.stage_idx + 1) }

      before do
        create(:ci_build_need, build: job, name: bridge.name)
      end

      it 'marks the subsequent job as processable' do
        expect { execute_service }.to change { job.reload.status }.from('skipped').to('created')
      end

      context 'when the FF ci_fix_pipeline_status_for_dag_needs_manual is disabled' do
        before do
          stub_feature_flags(ci_fix_pipeline_status_for_dag_needs_manual: false)
        end

        it 'does not change the subsequent job' do
          expect { execute_service }.not_to change { job.reload.status }.from('skipped')
        end
      end
    end

    context 'when bridge is not playable' do
      let(:bridge) { create(:ci_bridge, :failed, pipeline: pipeline, downstream: downstream_project) }

      it 'raises StateMachines::InvalidTransition' do
        expect { execute_service }.to raise_error StateMachines::InvalidTransition
      end
    end
  end

  context 'when user can not run the bridge' do
    before do
      allow(instance).to receive(:can?).with(user, :play_job, bridge).and_return(false)
    end

    it 'allows user with developer role to play a bridge' do
      expect { execute_service }.to raise_error Gitlab::Access::AccessDeniedError
    end
  end
end