2017-06-14 05:51:34 -04:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Gitlab::Gpg::Commit do
|
|
|
|
describe '#signature' do
|
|
|
|
let!(:project) { create :project, :repository, path: 'sample-project' }
|
2017-06-26 03:13:36 -04:00
|
|
|
let!(:commit_sha) { '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' }
|
2017-06-14 05:51:34 -04:00
|
|
|
|
2017-06-15 03:16:50 -04:00
|
|
|
context 'unisgned commit' do
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(described_class.new(project.commit).signature).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-15 03:57:50 -04:00
|
|
|
context 'known and verified public key' do
|
2017-06-15 04:28:28 -04:00
|
|
|
let!(:gpg_key) do
|
|
|
|
create :gpg_key, key: GpgHelpers::User1.public_key, user: create(:user, email: GpgHelpers::User1.emails.first)
|
|
|
|
end
|
2017-06-14 05:51:34 -04:00
|
|
|
|
2017-06-15 04:28:28 -04:00
|
|
|
let!(:commit) do
|
2017-06-14 05:51:34 -04:00
|
|
|
raw_commit = double(:raw_commit, signature: [
|
|
|
|
GpgHelpers::User1.signed_commit_signature,
|
|
|
|
GpgHelpers::User1.signed_commit_base_data
|
2017-06-26 03:13:36 -04:00
|
|
|
], sha: commit_sha)
|
2017-06-14 05:51:34 -04:00
|
|
|
allow(raw_commit).to receive :save!
|
|
|
|
|
2017-06-15 04:28:28 -04:00
|
|
|
create :commit, git_commit: raw_commit, project: project
|
|
|
|
end
|
2017-06-14 05:51:34 -04:00
|
|
|
|
2017-06-15 04:28:28 -04:00
|
|
|
it 'returns a valid signature' do
|
2017-06-14 05:51:34 -04:00
|
|
|
expect(described_class.new(commit).signature).to have_attributes(
|
2017-06-26 03:13:36 -04:00
|
|
|
commit_sha: commit_sha,
|
2017-06-14 05:51:34 -04:00
|
|
|
project: project,
|
|
|
|
gpg_key: gpg_key,
|
|
|
|
gpg_key_primary_keyid: GpgHelpers::User1.primary_keyid,
|
2017-07-13 09:22:15 -04:00
|
|
|
gpg_key_user_name: GpgHelpers::User1.names.first,
|
|
|
|
gpg_key_user_email: GpgHelpers::User1.emails.first,
|
2017-06-14 05:51:34 -04:00
|
|
|
valid_signature: true
|
|
|
|
)
|
|
|
|
end
|
2017-06-15 04:28:28 -04:00
|
|
|
|
|
|
|
it 'returns the cached signature on second call' do
|
|
|
|
gpg_commit = described_class.new(commit)
|
|
|
|
|
2017-06-15 07:37:03 -04:00
|
|
|
expect(gpg_commit).to receive(:using_keychain).and_call_original
|
2017-06-15 04:28:28 -04:00
|
|
|
gpg_commit.signature
|
|
|
|
|
|
|
|
# consecutive call
|
2017-06-15 07:37:03 -04:00
|
|
|
expect(gpg_commit).not_to receive(:using_keychain).and_call_original
|
2017-06-15 04:28:28 -04:00
|
|
|
gpg_commit.signature
|
|
|
|
end
|
2017-06-14 05:51:34 -04:00
|
|
|
end
|
|
|
|
|
2017-06-15 03:57:50 -04:00
|
|
|
context 'known but unverified public key' do
|
2017-06-15 04:28:28 -04:00
|
|
|
let!(:gpg_key) { create :gpg_key, key: GpgHelpers::User1.public_key }
|
2017-06-15 03:57:50 -04:00
|
|
|
|
2017-06-15 04:28:28 -04:00
|
|
|
let!(:commit) do
|
2017-06-15 03:57:50 -04:00
|
|
|
raw_commit = double(:raw_commit, signature: [
|
|
|
|
GpgHelpers::User1.signed_commit_signature,
|
|
|
|
GpgHelpers::User1.signed_commit_base_data
|
2017-06-26 03:13:36 -04:00
|
|
|
], sha: commit_sha)
|
2017-06-15 03:57:50 -04:00
|
|
|
allow(raw_commit).to receive :save!
|
|
|
|
|
2017-06-15 04:28:28 -04:00
|
|
|
create :commit, git_commit: raw_commit, project: project
|
|
|
|
end
|
2017-06-15 03:57:50 -04:00
|
|
|
|
2017-06-15 04:28:28 -04:00
|
|
|
it 'returns an invalid signature' do
|
2017-06-15 03:57:50 -04:00
|
|
|
expect(described_class.new(commit).signature).to have_attributes(
|
2017-06-26 03:13:36 -04:00
|
|
|
commit_sha: commit_sha,
|
2017-06-15 03:57:50 -04:00
|
|
|
project: project,
|
|
|
|
gpg_key: gpg_key,
|
|
|
|
gpg_key_primary_keyid: GpgHelpers::User1.primary_keyid,
|
2017-07-13 09:22:15 -04:00
|
|
|
gpg_key_user_name: GpgHelpers::User1.names.first,
|
|
|
|
gpg_key_user_email: GpgHelpers::User1.emails.first,
|
2017-06-15 03:57:50 -04:00
|
|
|
valid_signature: false
|
|
|
|
)
|
|
|
|
end
|
2017-06-15 04:28:28 -04:00
|
|
|
|
|
|
|
it 'returns the cached signature on second call' do
|
|
|
|
gpg_commit = described_class.new(commit)
|
|
|
|
|
2017-06-15 07:37:03 -04:00
|
|
|
expect(gpg_commit).to receive(:using_keychain).and_call_original
|
2017-06-15 04:28:28 -04:00
|
|
|
gpg_commit.signature
|
|
|
|
|
|
|
|
# consecutive call
|
2017-06-15 07:37:03 -04:00
|
|
|
expect(gpg_commit).not_to receive(:using_keychain).and_call_original
|
2017-06-15 04:28:28 -04:00
|
|
|
gpg_commit.signature
|
|
|
|
end
|
2017-06-15 03:57:50 -04:00
|
|
|
end
|
|
|
|
|
2017-06-14 05:51:34 -04:00
|
|
|
context 'unknown public key' do
|
2017-06-15 04:28:28 -04:00
|
|
|
let!(:commit) do
|
2017-06-14 05:51:34 -04:00
|
|
|
raw_commit = double(:raw_commit, signature: [
|
|
|
|
GpgHelpers::User1.signed_commit_signature,
|
|
|
|
GpgHelpers::User1.signed_commit_base_data
|
2017-06-26 03:13:36 -04:00
|
|
|
], sha: commit_sha)
|
2017-06-14 05:51:34 -04:00
|
|
|
allow(raw_commit).to receive :save!
|
|
|
|
|
2017-06-15 04:28:28 -04:00
|
|
|
create :commit,
|
2017-06-14 05:51:34 -04:00
|
|
|
git_commit: raw_commit,
|
|
|
|
project: project
|
2017-06-15 04:28:28 -04:00
|
|
|
end
|
2017-06-14 05:51:34 -04:00
|
|
|
|
2017-06-15 04:28:28 -04:00
|
|
|
it 'returns an invalid signature' do
|
2017-06-14 05:51:34 -04:00
|
|
|
expect(described_class.new(commit).signature).to have_attributes(
|
2017-06-26 03:13:36 -04:00
|
|
|
commit_sha: commit_sha,
|
2017-06-14 05:51:34 -04:00
|
|
|
project: project,
|
|
|
|
gpg_key: nil,
|
2017-06-15 06:43:04 -04:00
|
|
|
gpg_key_primary_keyid: GpgHelpers::User1.primary_keyid,
|
2017-07-13 09:22:15 -04:00
|
|
|
gpg_key_user_name: nil,
|
|
|
|
gpg_key_user_email: nil,
|
2017-06-14 05:51:34 -04:00
|
|
|
valid_signature: false
|
|
|
|
)
|
|
|
|
end
|
2017-06-15 04:28:28 -04:00
|
|
|
|
|
|
|
it 'returns the cached signature on second call' do
|
|
|
|
gpg_commit = described_class.new(commit)
|
|
|
|
|
2017-06-15 07:37:03 -04:00
|
|
|
expect(gpg_commit).to receive(:using_keychain).and_call_original
|
2017-06-15 04:28:28 -04:00
|
|
|
gpg_commit.signature
|
|
|
|
|
|
|
|
# consecutive call
|
2017-06-15 07:37:03 -04:00
|
|
|
expect(gpg_commit).not_to receive(:using_keychain).and_call_original
|
2017-06-15 04:28:28 -04:00
|
|
|
gpg_commit.signature
|
|
|
|
end
|
2017-06-14 05:51:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|