2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-29 03:56:52 -04:00
|
|
|
require 'spec_helper'
|
2017-06-14 03:17:34 -04:00
|
|
|
|
2021-12-13 07:12:59 -05:00
|
|
|
RSpec.describe CommitSignatures::GpgSignature do
|
2017-10-07 11:47:53 -04:00
|
|
|
let(:commit_sha) { '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' }
|
|
|
|
let!(:project) { create(:project, :repository, path: 'sample-project') }
|
|
|
|
let!(:commit) { create(:commit, project: project, sha: commit_sha) }
|
|
|
|
let(:gpg_signature) { create(:gpg_signature, commit_sha: commit_sha) }
|
2017-10-04 19:44:49 -04:00
|
|
|
let(:gpg_key) { create(:gpg_key) }
|
|
|
|
let(:gpg_key_subkey) { create(:gpg_key_subkey) }
|
|
|
|
|
2018-12-04 05:24:21 -05:00
|
|
|
it_behaves_like 'having unique enum values'
|
|
|
|
|
2017-06-14 03:17:34 -04:00
|
|
|
describe 'associations' do
|
2021-12-13 07:12:59 -05:00
|
|
|
it { is_expected.to belong_to(:project).required }
|
2017-06-14 03:17:34 -04:00
|
|
|
it { is_expected.to belong_to(:gpg_key) }
|
2017-09-28 20:57:49 -04:00
|
|
|
it { is_expected.to belong_to(:gpg_key_subkey) }
|
2017-06-14 03:17:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validation' do
|
|
|
|
subject { described_class.new }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2017-06-14 03:17:34 -04:00
|
|
|
it { is_expected.to validate_presence_of(:commit_sha) }
|
2017-07-25 15:20:48 -04:00
|
|
|
it { is_expected.to validate_presence_of(:project_id) }
|
2017-06-15 06:43:04 -04:00
|
|
|
it { is_expected.to validate_presence_of(:gpg_key_primary_keyid) }
|
2017-06-14 03:17:34 -04:00
|
|
|
end
|
2017-07-06 04:17:09 -04:00
|
|
|
|
2019-02-05 12:22:25 -05:00
|
|
|
describe '.safe_create!' do
|
|
|
|
let(:attributes) do
|
|
|
|
{
|
|
|
|
commit_sha: commit_sha,
|
|
|
|
project: project,
|
|
|
|
gpg_key_primary_keyid: gpg_key.keyid
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds a signature by commit sha if it existed' do
|
|
|
|
gpg_signature
|
|
|
|
|
|
|
|
expect(described_class.safe_create!(commit_sha: commit_sha)).to eq(gpg_signature)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new signature if it was not found' do
|
|
|
|
expect { described_class.safe_create!(attributes) }.to change { described_class.count }.by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns the correct attributes when creating' do
|
|
|
|
signature = described_class.safe_create!(attributes)
|
|
|
|
|
|
|
|
expect(signature.project).to eq(project)
|
|
|
|
expect(signature.commit_sha).to eq(commit_sha)
|
|
|
|
expect(signature.gpg_key_primary_keyid).to eq(gpg_key.keyid)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise an error in case of a race condition' do
|
2021-08-18 17:08:44 -04:00
|
|
|
expect(described_class).to receive(:find_by).and_return(nil, double(described_class, persisted?: true))
|
|
|
|
|
|
|
|
expect(described_class).to receive(:create).and_raise(ActiveRecord::RecordNotUnique)
|
|
|
|
allow(described_class).to receive(:create).and_call_original
|
2019-02-05 12:22:25 -05:00
|
|
|
|
|
|
|
described_class.safe_create!(attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-17 08:07:33 -04:00
|
|
|
describe '.by_commit_sha scope' do
|
|
|
|
let(:gpg_key) { create(:gpg_key, key: GpgHelpers::User2.public_key) }
|
|
|
|
let!(:another_gpg_signature) { create(:gpg_signature, gpg_key: gpg_key) }
|
|
|
|
|
|
|
|
it 'returns all gpg signatures by sha' do
|
|
|
|
expect(described_class.by_commit_sha(commit_sha)).to eq([gpg_signature])
|
|
|
|
expect(
|
|
|
|
described_class.by_commit_sha([commit_sha, another_gpg_signature.commit_sha])
|
|
|
|
).to contain_exactly(gpg_signature, another_gpg_signature)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-06 04:17:09 -04:00
|
|
|
describe '#commit' do
|
|
|
|
it 'fetches the commit through the project' do
|
2019-11-20 01:06:16 -05:00
|
|
|
expect_next_instance_of(Project) do |instance|
|
|
|
|
expect(instance).to receive(:commit).with(commit_sha).and_return(commit)
|
|
|
|
end
|
2017-07-06 04:17:09 -04:00
|
|
|
|
|
|
|
gpg_signature.commit
|
|
|
|
end
|
|
|
|
end
|
2017-10-04 19:44:49 -04:00
|
|
|
|
|
|
|
describe '#gpg_key=' do
|
|
|
|
it 'supports the assignment of a GpgKey' do
|
|
|
|
gpg_signature = create(:gpg_signature, gpg_key: gpg_key)
|
|
|
|
|
|
|
|
expect(gpg_signature.gpg_key).to be_an_instance_of(GpgKey)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports the assignment of a GpgKeySubkey' do
|
|
|
|
gpg_signature = create(:gpg_signature, gpg_key: gpg_key_subkey)
|
|
|
|
|
|
|
|
expect(gpg_signature.gpg_key).to be_an_instance_of(GpgKeySubkey)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'clears gpg_key and gpg_key_subkey_id when passing nil' do
|
|
|
|
gpg_signature.update_attribute(:gpg_key, nil)
|
|
|
|
|
|
|
|
expect(gpg_signature.gpg_key_id).to be_nil
|
|
|
|
expect(gpg_signature.gpg_key_subkey_id).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2017-10-07 11:47:53 -04:00
|
|
|
|
|
|
|
describe '#gpg_commit' do
|
|
|
|
context 'when commit does not exist' do
|
|
|
|
it 'returns nil' do
|
|
|
|
allow(gpg_signature).to receive(:commit).and_return(nil)
|
|
|
|
|
|
|
|
expect(gpg_signature.gpg_commit).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when commit exists' do
|
|
|
|
it 'returns an instance of Gitlab::Gpg::Commit' do
|
|
|
|
allow(gpg_signature).to receive(:commit).and_return(commit)
|
|
|
|
|
|
|
|
expect(gpg_signature.gpg_commit).to be_an_instance_of(Gitlab::Gpg::Commit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-14 03:17:34 -04:00
|
|
|
end
|