summaryrefslogtreecommitdiff
path: root/spec/workers/jira_connect/sync_branch_worker_spec.rb
blob: c8453064b0d88da86595f6bb39d76e58bab03e18 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe JiraConnect::SyncBranchWorker do
  include AfterNextHelpers

  describe '#perform' do
    let_it_be(:group) { create(:group) }
    let_it_be(:project) { create(:project, :repository, group: group) }
    let_it_be(:subscription) { create(:jira_connect_subscription, installation: create(:jira_connect_installation), namespace: group) }

    let(:project_id) { project.id }
    let(:branch_name) { 'master' }
    let(:commit_shas) { %w(b83d6e3 5a62481) }

    subject { described_class.new.perform(project_id, branch_name, commit_shas) }

    def expect_jira_sync_service_execute(args)
      expect_next_instance_of(JiraConnect::SyncService) do |instance|
        expect(instance).to receive(:execute).with(args.merge(update_sequence_id: nil))
      end
    end

    it 'calls JiraConnect::SyncService#execute' do
      expect_jira_sync_service_execute(
        branches: [instance_of(Gitlab::Git::Branch)],
        commits: project.commits_by(oids: commit_shas)
      )

      subject
    end

    context 'without branch name' do
      let(:branch_name) { nil }

      it 'calls JiraConnect::SyncService#execute' do
        expect_jira_sync_service_execute(
          branches: nil,
          commits: project.commits_by(oids: commit_shas)
        )

        subject
      end
    end

    context 'without commits' do
      let(:commit_shas) { nil }

      it 'calls JiraConnect::SyncService#execute' do
        expect_jira_sync_service_execute(
          branches: [instance_of(Gitlab::Git::Branch)],
          commits: nil
        )

        subject
      end
    end

    context 'when project no longer exists' do
      let(:project_id) { non_existing_record_id }

      it 'does not call JiraConnect::SyncService' do
        expect(JiraConnect::SyncService).not_to receive(:new)

        subject
      end
    end

    context 'with update_sequence_id' do
      let(:update_sequence_id) { 1 }
      let(:request_path) { '/rest/devinfo/0.10/bulk' }
      let(:request_body) do
        {
          repositories: [
            Atlassian::JiraConnect::Serializers::RepositoryEntity.represent(
              project,
              commits: project.commits_by(oids: commit_shas),
              branches: [project.repository.find_branch(branch_name)],
              update_sequence_id: update_sequence_id
            )
          ]
        }
      end

      subject { described_class.new.perform(project_id, branch_name, commit_shas, update_sequence_id) }

      it 'sends the reqeust with custom update_sequence_id' do
        expect_next(Atlassian::JiraConnect::Client).to receive(:post).with(request_path, request_body)

        subject
      end
    end
  end
end