2017-02-22 06:49:17 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe GpgKey do
|
|
|
|
describe "associations" do
|
|
|
|
it { is_expected.to belong_to(:user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "validation" do
|
2017-07-05 08:03:36 -04:00
|
|
|
it { is_expected.to validate_presence_of(:user) }
|
2017-07-26 09:47:00 -04:00
|
|
|
|
2017-02-22 06:49:17 -05:00
|
|
|
it { is_expected.to validate_presence_of(:key) }
|
|
|
|
it { is_expected.to validate_uniqueness_of(:key) }
|
2017-07-26 09:47:00 -04:00
|
|
|
|
|
|
|
it { is_expected.to allow_value("-----BEGIN PGP PUBLIC KEY BLOCK-----\nkey\n-----END PGP PUBLIC KEY BLOCK-----").for(:key) }
|
|
|
|
|
|
|
|
it { is_expected.not_to allow_value("-----BEGIN PGP PUBLIC KEY BLOCK-----\nkey").for(:key) }
|
2017-02-22 06:49:17 -05:00
|
|
|
it { is_expected.not_to allow_value("-----BEGIN PGP PUBLIC KEY BLOCK-----\nkey\n-----BEGIN PGP PUBLIC KEY BLOCK-----").for(:key) }
|
2017-07-26 09:47:00 -04:00
|
|
|
it { is_expected.not_to allow_value("-----BEGIN PGP PUBLIC KEY BLOCK----------END PGP PUBLIC KEY BLOCK-----").for(:key) }
|
|
|
|
it { is_expected.not_to allow_value("-----BEGIN PGP PUBLIC KEY BLOCK-----").for(:key) }
|
|
|
|
it { is_expected.not_to allow_value("-----END PGP PUBLIC KEY BLOCK-----").for(:key) }
|
|
|
|
it { is_expected.not_to allow_value("key\n-----END PGP PUBLIC KEY BLOCK-----").for(:key) }
|
2017-02-22 06:49:17 -05:00
|
|
|
it { is_expected.not_to allow_value('BEGIN PGP').for(:key) }
|
|
|
|
end
|
|
|
|
|
2017-06-28 07:08:29 -04:00
|
|
|
context 'callbacks' do
|
2017-02-22 06:49:17 -05:00
|
|
|
describe 'extract_fingerprint' do
|
2017-02-22 12:36:25 -05:00
|
|
|
it 'extracts the fingerprint from the gpg key' do
|
2017-02-23 06:02:36 -05:00
|
|
|
gpg_key = described_class.new(key: GpgHelpers::User1.public_key)
|
2017-02-22 06:49:17 -05:00
|
|
|
gpg_key.valid?
|
2017-02-23 08:07:51 -05:00
|
|
|
expect(gpg_key.fingerprint).to eq GpgHelpers::User1.fingerprint
|
2017-02-22 06:49:17 -05:00
|
|
|
end
|
|
|
|
end
|
2017-06-13 07:46:43 -04:00
|
|
|
|
|
|
|
describe 'extract_primary_keyid' do
|
|
|
|
it 'extracts the primary keyid from the gpg key' do
|
|
|
|
gpg_key = described_class.new(key: GpgHelpers::User1.public_key)
|
|
|
|
gpg_key.valid?
|
|
|
|
expect(gpg_key.primary_keyid).to eq GpgHelpers::User1.primary_keyid
|
|
|
|
end
|
|
|
|
end
|
2017-02-22 06:49:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#key=' do
|
|
|
|
it 'strips white spaces' do
|
|
|
|
key = <<~KEY.strip
|
|
|
|
-----BEGIN PGP PUBLIC KEY BLOCK-----
|
|
|
|
Version: GnuPG v1
|
|
|
|
|
|
|
|
mQENBFMOSOgBCADFCYxmnXFbrDhfvlf03Q/bQuT+nZu46BFGbo7XkUjDowFXJQhP
|
|
|
|
-----END PGP PUBLIC KEY BLOCK-----
|
|
|
|
KEY
|
|
|
|
|
|
|
|
expect(described_class.new(key: " #{key} ").key).to eq(key)
|
|
|
|
end
|
2017-07-25 14:35:44 -04:00
|
|
|
|
|
|
|
it 'does not strip when the key is nil' do
|
|
|
|
expect(described_class.new(key: nil).key).to be_nil
|
|
|
|
end
|
2017-02-22 06:49:17 -05:00
|
|
|
end
|
2017-02-22 09:37:49 -05:00
|
|
|
|
2017-07-13 09:22:15 -04:00
|
|
|
describe '#user_infos' do
|
|
|
|
it 'returns the user infos from the gpg key' do
|
2017-02-23 08:07:51 -05:00
|
|
|
gpg_key = create :gpg_key, key: GpgHelpers::User1.public_key
|
2017-07-13 09:22:15 -04:00
|
|
|
expect(Gitlab::Gpg).to receive(:user_infos_from_key).with(gpg_key.key)
|
2017-02-22 09:37:49 -05:00
|
|
|
|
2017-07-13 09:22:15 -04:00
|
|
|
gpg_key.user_infos
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#verified_user_infos' do
|
|
|
|
it 'returns the user infos if it is verified' do
|
|
|
|
user = create :user, email: GpgHelpers::User1.emails.first
|
|
|
|
gpg_key = create :gpg_key, key: GpgHelpers::User1.public_key, user: user
|
|
|
|
|
|
|
|
expect(gpg_key.verified_user_infos).to eq([{
|
|
|
|
name: GpgHelpers::User1.names.first,
|
|
|
|
email: GpgHelpers::User1.emails.first
|
|
|
|
}])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty array if the user info is not verified' do
|
|
|
|
user = create :user, email: 'unrelated@example.com'
|
|
|
|
gpg_key = create :gpg_key, key: GpgHelpers::User1.public_key, user: user
|
|
|
|
|
|
|
|
expect(gpg_key.verified_user_infos).to eq([])
|
2017-02-22 09:37:49 -05:00
|
|
|
end
|
|
|
|
end
|
2017-02-24 15:28:26 -05:00
|
|
|
|
2017-06-28 07:08:29 -04:00
|
|
|
describe '#emails_with_verified_status' do
|
2017-06-12 10:16:33 -04:00
|
|
|
it 'email is verified if the user has the matching email' do
|
|
|
|
user = create :user, email: 'bette.cartwright@example.com'
|
|
|
|
gpg_key = create :gpg_key, key: GpgHelpers::User2.public_key, user: user
|
|
|
|
|
2017-07-05 07:16:50 -04:00
|
|
|
expect(gpg_key.emails_with_verified_status).to eq(
|
|
|
|
'bette.cartwright@example.com' => true,
|
|
|
|
'bette.cartwright@example.net' => false
|
|
|
|
)
|
2017-02-28 09:25:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-15 03:57:50 -04:00
|
|
|
describe '#verified?' do
|
|
|
|
it 'returns true one of the email addresses in the key belongs to the user' do
|
|
|
|
user = create :user, email: 'bette.cartwright@example.com'
|
|
|
|
gpg_key = create :gpg_key, key: GpgHelpers::User2.public_key, user: user
|
|
|
|
|
|
|
|
expect(gpg_key.verified?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if one of the email addresses in the key does not belong to the user' do
|
|
|
|
user = create :user, email: 'someone.else@example.com'
|
|
|
|
gpg_key = create :gpg_key, key: GpgHelpers::User2.public_key, user: user
|
|
|
|
|
|
|
|
expect(gpg_key.verified?).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-02 12:43:51 -04:00
|
|
|
describe 'notification', :mailer do
|
2017-02-28 04:49:59 -05:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it 'sends a notification' do
|
|
|
|
perform_enqueued_jobs do
|
|
|
|
create(:gpg_key, user: user)
|
|
|
|
end
|
|
|
|
|
|
|
|
should_email(user)
|
|
|
|
end
|
|
|
|
end
|
2017-07-12 01:59:28 -04:00
|
|
|
|
|
|
|
describe '#revoke' do
|
|
|
|
it 'invalidates all associated gpg signatures and destroys the key' do
|
|
|
|
gpg_key = create :gpg_key
|
|
|
|
gpg_signature = create :gpg_signature, valid_signature: true, gpg_key: gpg_key
|
|
|
|
|
|
|
|
unrelated_gpg_key = create :gpg_key, key: GpgHelpers::User2.public_key
|
|
|
|
unrelated_gpg_signature = create :gpg_signature, valid_signature: true, gpg_key: unrelated_gpg_key
|
|
|
|
|
|
|
|
gpg_key.revoke
|
|
|
|
|
|
|
|
expect(gpg_signature.reload).to have_attributes(
|
|
|
|
valid_signature: false,
|
|
|
|
gpg_key: nil
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(gpg_key.destroyed?).to be true
|
|
|
|
|
|
|
|
# unrelated signature is left untouched
|
|
|
|
expect(unrelated_gpg_signature.reload).to have_attributes(
|
|
|
|
valid_signature: true,
|
|
|
|
gpg_key: unrelated_gpg_key
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(unrelated_gpg_key.destroyed?).to be false
|
|
|
|
end
|
|
|
|
end
|
2017-02-22 06:49:17 -05:00
|
|
|
end
|