summaryrefslogtreecommitdiff
path: root/spec/workers/create_gpg_signature_worker_spec.rb
blob: f5479e5726054e9dd99d2b13d890e24b2cadef51 (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
require 'spec_helper'

describe CreateGpgSignatureWorker do
  let(:project) { create(:project, :repository) }
  let(:commits) { project.repository.commits('HEAD', limit: 3).commits }
  let(:commit_shas) { commits.map(&:id) }
  let(:gpg_commit) { instance_double(Gitlab::Gpg::Commit) }

  context 'when GpgKey is found' do
    before do
      allow(Project).to receive(:find_by).with(id: project.id).and_return(project)
      allow(project).to receive(:commits_by).with(oids: commit_shas).and_return(commits)
    end

    subject { described_class.new.perform(commit_shas, project.id) }

    it 'calls Gitlab::Gpg::Commit#signature' do
      commits.each do |commit|
        expect(Gitlab::Gpg::Commit).to receive(:new).with(commit).and_return(gpg_commit).once
      end

      expect(gpg_commit).to receive(:signature).exactly(commits.size).times

      subject
    end

    it 'can recover from exception and continue the signature process' do
      allow(gpg_commit).to receive(:signature)
      allow(Gitlab::Gpg::Commit).to receive(:new).and_return(gpg_commit)
      allow(Gitlab::Gpg::Commit).to receive(:new).with(commits.first).and_raise(StandardError)

      expect(gpg_commit).to receive(:signature).exactly(2).times

      subject
    end
  end

  context 'handles when a string is passed in for the commit SHA' do
    it 'creates a signature once' do
      allow(Gitlab::Gpg::Commit).to receive(:new).with(commits.first).and_return(gpg_commit)

      expect(gpg_commit).to receive(:signature).once

      described_class.new.perform(commit_shas.first, project.id)
    end
  end

  context 'when Commit is not found' do
    let(:nonexisting_commit_sha) { '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a34' }

    it 'does not raise errors' do
      expect { described_class.new.perform([nonexisting_commit_sha], project.id) }.not_to raise_error
    end
  end

  context 'when Project is not found' do
    let(:nonexisting_project_id) { -1 }

    it 'does not raise errors' do
      expect { described_class.new.perform(commit_shas, nonexisting_project_id) }.not_to raise_error
    end

    it 'does not call Gitlab::Gpg::Commit#signature' do
      expect_any_instance_of(Gitlab::Gpg::Commit).not_to receive(:signature)

      described_class.new.perform(commit_shas, nonexisting_project_id)
    end
  end
end