summaryrefslogtreecommitdiff
path: root/spec/workers/ci/external_pull_requests/create_pipeline_worker_spec.rb
blob: a637ac088ff871347fcb3b9fa036e69e43cadda8 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::ExternalPullRequests::CreatePipelineWorker do
  let_it_be(:project) { create(:project, :auto_devops, :repository) }
  let_it_be(:user) { project.first_owner }
  let_it_be(:external_pull_request) do
    branch = project.repository.branches.last
    create(:external_pull_request, project: project, source_branch: branch.name, source_sha: branch.target)
  end

  let(:worker) { described_class.new }

  describe '#perform' do
    let(:project_id) { project.id }
    let(:user_id) { user.id }
    let(:external_pull_request_id) { external_pull_request.id }

    subject(:perform) { worker.perform(project_id, user_id, external_pull_request_id) }

    it 'creates the pipeline' do
      pipeline = perform.payload

      expect(pipeline).to be_valid
      expect(pipeline).to be_persisted
      expect(pipeline).to be_external_pull_request_event
      expect(pipeline.project).to eq(project)
      expect(pipeline.user).to eq(user)
      expect(pipeline.external_pull_request).to eq(external_pull_request)
      expect(pipeline.status).to eq('created')
      expect(pipeline.ref).to eq(external_pull_request.source_branch)
      expect(pipeline.sha).to eq(external_pull_request.source_sha)
      expect(pipeline.source_sha).to eq(external_pull_request.source_sha)
      expect(pipeline.target_sha).to eq(external_pull_request.target_sha)
    end

    shared_examples_for 'not calling service' do
      it 'does not call the service' do
        expect(Ci::CreatePipelineService).not_to receive(:new)
        perform
      end
    end

    context 'when the project not found' do
      let(:project_id) { non_existing_record_id }

      it_behaves_like 'not calling service'
    end

    context 'when the user not found' do
      let(:user_id) { non_existing_record_id }

      it_behaves_like 'not calling service'
    end

    context 'when the pull request not found' do
      let(:external_pull_request_id) { non_existing_record_id }

      it_behaves_like 'not calling service'
    end

    context 'when the pull request does not belong to the project' do
      let(:external_pull_request_id) { create(:external_pull_request).id }

      it_behaves_like 'not calling service'
    end
  end
end