create_gpg_signature_worker_spec.rb 2.26 KB
Newer Older
1 2 3
require 'spec_helper'

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

9
  context 'when GpgKey is found' do
10 11 12 13 14 15
    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) }
16

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

22 23 24 25 26
      expect(gpg_commit).to receive(:signature).exactly(commits.size).times

      subject
    end

27
    it 'can recover from exception and continue the signature process' do
28 29 30
      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)
31

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

34
      subject
35 36 37
    end
  end

38 39 40 41 42 43 44 45 46 47
  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

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

    it 'does not raise errors' do
52
      expect { described_class.new.perform([nonexisting_commit_sha], project.id) }.not_to raise_error
53 54 55 56 57 58 59
    end
  end

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

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

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

66
      described_class.new.perform(commit_shas, nonexisting_project_id)
67 68 69
    end
  end
end