debian-mirror-gitlab/spec/workers/create_commit_signature_worker_spec.rb

163 lines
5 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
require 'spec_helper'
2020-03-13 15:44:24 +05:30
describe CreateCommitSignatureWorker do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :repository) }
2018-11-18 11:00:15 +05:30
let(:commits) { project.repository.commits('HEAD', limit: 3).commits }
let(:commit_shas) { commits.map(&:id) }
let(:gpg_commit) { instance_double(Gitlab::Gpg::Commit) }
2020-03-13 15:44:24 +05:30
let(:x509_commit) { instance_double(Gitlab::X509::Commit) }
2017-09-10 17:25:29 +05:30
2020-04-22 19:07:51 +05:30
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
2018-11-18 11:00:15 +05:30
2020-04-22 19:07:51 +05:30
subject { described_class.new.perform(commit_shas, project.id) }
2017-09-10 17:25:29 +05:30
2020-04-22 19:07:51 +05:30
context 'when a signature is found' do
2020-05-24 23:13:21 +05:30
it_behaves_like 'an idempotent worker' do
let(:job_args) { [commit_shas, project.id] }
before do
# Removing the stub which can cause bugs for multiple calls to
# Project#commits_by.
allow(project).to receive(:commits_by).and_call_original
# Making sure it still goes through all the perform execution.
allow_next_instance_of(::Commit) do |commit|
allow(commit).to receive(:signature_type).and_return(:PGP)
end
allow_next_instance_of(::Gitlab::Gpg::Commit) do |gpg|
expect(gpg).to receive(:signature).once.and_call_original
end
end
end
2017-09-10 17:25:29 +05:30
it 'calls Gitlab::Gpg::Commit#signature' do
2018-11-18 11:00:15 +05:30
commits.each do |commit|
2020-03-13 15:44:24 +05:30
allow(commit).to receive(:signature_type).and_return(:PGP)
2018-11-18 11:00:15 +05:30
expect(Gitlab::Gpg::Commit).to receive(:new).with(commit).and_return(gpg_commit).once
end
2017-09-10 17:25:29 +05:30
2018-11-18 11:00:15 +05:30
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)
2020-03-13 15:44:24 +05:30
allow(commits[1]).to receive(:signature_type).and_return(:PGP)
allow(commits[2]).to receive(:signature_type).and_return(:PGP)
expect(gpg_commit).to receive(:signature).twice
subject
end
it 'calls Gitlab::X509::Commit#signature' do
commits.each do |commit|
allow(commit).to receive(:signature_type).and_return(:X509)
expect(Gitlab::X509::Commit).to receive(:new).with(commit).and_return(x509_commit).once
end
expect(x509_commit).to receive(:signature).exactly(commits.size).times
subject
end
it 'can recover from exception and continue the X509 signature process' do
allow(x509_commit).to receive(:signature)
allow(Gitlab::X509::Commit).to receive(:new).and_return(x509_commit)
allow(Gitlab::X509::Commit).to receive(:new).with(commits.first).and_raise(StandardError)
allow(commits[1]).to receive(:signature_type).and_return(:X509)
allow(commits[2]).to receive(:signature_type).and_return(:X509)
expect(x509_commit).to receive(:signature).twice
2018-11-18 11:00:15 +05:30
subject
end
end
context 'handles when a string is passed in for the commit SHA' do
2020-04-22 19:07:51 +05:30
let(:commit_shas) { super().first }
2020-03-13 15:44:24 +05:30
before do
2020-04-22 19:07:51 +05:30
allow(project).to receive(:commits_by).with(oids: [commit_shas]).and_return(commits)
2020-03-13 15:44:24 +05:30
allow(commits.first).to receive(:signature_type).and_return(:PGP)
end
2018-11-18 11:00:15 +05:30
it 'creates a signature once' do
allow(Gitlab::Gpg::Commit).to receive(:new).with(commits.first).and_return(gpg_commit)
2018-03-17 18:26:18 +05:30
2018-11-18 11:00:15 +05:30
expect(gpg_commit).to receive(:signature).once
2017-09-10 17:25:29 +05:30
2020-04-22 19:07:51 +05:30
subject
2017-09-10 17:25:29 +05:30
end
end
context 'when Commit is not found' do
let(:nonexisting_commit_sha) { '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a34' }
2020-04-22 19:07:51 +05:30
let(:commit_shas) { [nonexisting_commit_sha] }
2017-09-10 17:25:29 +05:30
it 'does not raise errors' do
2020-04-22 19:07:51 +05:30
expect { described_class.new.perform(commit_shas, project.id) }.not_to raise_error
2017-09-10 17:25:29 +05:30
end
end
context 'when Project is not found' do
2020-04-22 19:07:51 +05:30
let(:commits) { [] }
let(:project) { double(id: non_existing_record_id) }
2017-09-10 17:25:29 +05:30
it 'does not raise errors' do
2020-04-22 19:07:51 +05:30
expect { subject }.not_to raise_error
2017-09-10 17:25:29 +05:30
end
it 'does not call Gitlab::Gpg::Commit#signature' do
expect_any_instance_of(Gitlab::Gpg::Commit).not_to receive(:signature)
2020-04-22 19:07:51 +05:30
subject
2017-09-10 17:25:29 +05:30
end
2020-03-13 15:44:24 +05:30
it 'does not call Gitlab::X509::Commit#signature' do
expect_any_instance_of(Gitlab::X509::Commit).not_to receive(:signature)
2020-04-22 19:07:51 +05:30
subject
end
end
context 'fetching signatures' do
before do
commits.each do |commit|
allow(commit).to receive(:signature_type).and_return(type)
end
end
context 'X509' do
let(:type) { :X509 }
it 'performs a single query for commit signatures' do
expect(X509CommitSignature).to receive(:by_commit_sha).with(commit_shas).once.and_return([])
subject
end
end
context 'PGP' do
let(:type) { :PGP }
it 'performs a single query for commit signatures' do
expect(GpgSignature).to receive(:by_commit_sha).with(commit_shas).once.and_return([])
subject
end
2020-03-13 15:44:24 +05:30
end
2017-09-10 17:25:29 +05:30
end
end