debian-mirror-gitlab/spec/models/commit_signatures/gpg_signature_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

96 lines
2.9 KiB
Ruby
Raw Permalink Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2019-12-04 20:38:33 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
2022-01-26 12:08:38 +05:30
RSpec.describe CommitSignatures::GpgSignature do
2022-07-23 23:45:48 +05:30
# This commit is seeded from https://gitlab.com/gitlab-org/gitlab-test
# For instructions on how to add more seed data, see the project README
2022-10-11 01:57:18 +05:30
let_it_be(:commit_sha) { '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' }
let_it_be(:project) { create(:project, :repository, path: 'sample-project') }
let_it_be(:commit) { create(:commit, project: project, sha: commit_sha) }
let_it_be(:gpg_key) { create(:gpg_key) }
let_it_be(:gpg_key_subkey) { create(:gpg_key_subkey, gpg_key: gpg_key) }
let(:signature) { create(:gpg_signature, commit_sha: commit_sha, gpg_key: gpg_key) }
2022-07-23 23:45:48 +05:30
let(:attributes) do
{
commit_sha: commit_sha,
project: project,
gpg_key_primary_keyid: gpg_key.keyid
}
end
2018-03-17 18:26:18 +05:30
2019-02-15 15:39:39 +05:30
it_behaves_like 'having unique enum values'
2022-07-23 23:45:48 +05:30
it_behaves_like 'commit signature'
2023-03-04 22:38:38 +05:30
it_behaves_like 'signature with type checking', :gpg
2019-02-15 15:39:39 +05:30
2017-09-10 17:25:29 +05:30
describe 'associations' do
it { is_expected.to belong_to(:gpg_key) }
2018-03-17 18:26:18 +05:30
it { is_expected.to belong_to(:gpg_key_subkey) }
2017-09-10 17:25:29 +05:30
end
describe 'validation' do
subject { described_class.new }
2019-12-21 20:55:43 +05:30
2017-09-10 17:25:29 +05:30
it { is_expected.to validate_presence_of(:commit_sha) }
it { is_expected.to validate_presence_of(:gpg_key_primary_keyid) }
end
2019-12-21 20:55:43 +05:30
describe '.by_commit_sha scope' do
2022-10-11 01:57:18 +05:30
let_it_be(:another_gpg_signature) { create(:gpg_signature, gpg_key: gpg_key) }
2019-12-21 20:55:43 +05:30
it 'returns all gpg signatures by sha' do
2022-07-23 23:45:48 +05:30
expect(described_class.by_commit_sha(commit_sha)).to match_array([signature])
2019-12-21 20:55:43 +05:30
expect(
described_class.by_commit_sha([commit_sha, another_gpg_signature.commit_sha])
2022-07-23 23:45:48 +05:30
).to contain_exactly(signature, another_gpg_signature)
2017-09-10 17:25:29 +05:30
end
end
2018-03-17 18:26:18 +05:30
describe '#gpg_key=' do
it 'supports the assignment of a GpgKey' do
2022-07-23 23:45:48 +05:30
signature = create(:gpg_signature, gpg_key: gpg_key)
2018-03-17 18:26:18 +05:30
2022-07-23 23:45:48 +05:30
expect(signature.gpg_key).to be_an_instance_of(GpgKey)
2018-03-17 18:26:18 +05:30
end
it 'supports the assignment of a GpgKeySubkey' do
2022-07-23 23:45:48 +05:30
signature = create(:gpg_signature, gpg_key: gpg_key_subkey)
2018-03-17 18:26:18 +05:30
2022-07-23 23:45:48 +05:30
expect(signature.gpg_key).to be_an_instance_of(GpgKeySubkey)
2018-03-17 18:26:18 +05:30
end
it 'clears gpg_key and gpg_key_subkey_id when passing nil' do
2022-07-23 23:45:48 +05:30
signature.update_attribute(:gpg_key, nil)
2018-03-17 18:26:18 +05:30
2022-07-23 23:45:48 +05:30
expect(signature.gpg_key_id).to be_nil
expect(signature.gpg_key_subkey_id).to be_nil
2018-03-17 18:26:18 +05:30
end
end
describe '#gpg_commit' do
context 'when commit does not exist' do
it 'returns nil' do
2022-07-23 23:45:48 +05:30
allow(signature).to receive(:commit).and_return(nil)
2018-03-17 18:26:18 +05:30
2022-07-23 23:45:48 +05:30
expect(signature.gpg_commit).to be_nil
2018-03-17 18:26:18 +05:30
end
end
context 'when commit exists' do
it 'returns an instance of Gitlab::Gpg::Commit' do
2022-07-23 23:45:48 +05:30
allow(signature).to receive(:commit).and_return(commit)
2018-03-17 18:26:18 +05:30
2022-07-23 23:45:48 +05:30
expect(signature.gpg_commit).to be_an_instance_of(Gitlab::Gpg::Commit)
2018-03-17 18:26:18 +05:30
end
end
end
2023-01-13 00:05:48 +05:30
2023-03-04 22:38:38 +05:30
describe '#signed_by_user' do
2023-01-13 00:05:48 +05:30
it 'retrieves the gpg_key user' do
2023-03-04 22:38:38 +05:30
expect(signature.signed_by_user).to eq(gpg_key.user)
2023-01-13 00:05:48 +05:30
end
end
2017-09-10 17:25:29 +05:30
end