gitlab-org--gitlab-foss/spec/models/gpg_key_spec.rb

68 lines
2.0 KiB
Ruby
Raw Normal View History

2017-02-22 11:49:17 +00:00
require 'rails_helper'
describe GpgKey do
describe "associations" do
it { is_expected.to belong_to(:user) }
end
describe "validation" do
it { is_expected.to validate_presence_of(:key) }
it { is_expected.to validate_uniqueness_of(:key) }
it { is_expected.to allow_value("-----BEGIN PGP PUBLIC KEY BLOCK-----\nkey").for(:key) }
it { is_expected.not_to allow_value("-----BEGIN PGP PUBLIC KEY BLOCK-----\nkey\n-----BEGIN PGP PUBLIC KEY BLOCK-----").for(:key) }
it { is_expected.not_to allow_value('BEGIN PGP').for(:key) }
end
context 'callbacks', :gpg do
2017-02-22 11:49:17 +00:00
describe 'extract_fingerprint' do
it 'extracts the fingerprint from the gpg key' do
2017-02-23 11:02:36 +00:00
gpg_key = described_class.new(key: GpgHelpers::User1.public_key)
2017-02-22 11:49:17 +00:00
gpg_key.valid?
2017-02-23 13:07:51 +00:00
expect(gpg_key.fingerprint).to eq GpgHelpers::User1.fingerprint
2017-02-22 11:49:17 +00:00
end
end
describe 'add_to_keychain' do
it 'calls add_to_keychain after create' do
expect(Gitlab::Gpg::CurrentKeyChain).to receive(:add).with(GpgHelpers::User1.public_key)
create :gpg_key
end
end
describe 'remove_from_keychain' do
it 'calls remove_from_keychain after destroy' do
allow(Gitlab::Gpg::CurrentKeyChain).to receive :add
gpg_key = create :gpg_key
expect(
Gitlab::Gpg::CurrentKeyChain
).to receive(:remove).with(GpgHelpers::User1.fingerprint)
gpg_key.destroy!
end
end
2017-02-22 11:49:17 +00: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
end
2017-02-22 14:37:49 +00:00
describe '#emails', :gpg do
2017-02-22 14:37:49 +00:00
it 'returns the emails from the gpg key' do
2017-02-23 13:07:51 +00:00
gpg_key = create :gpg_key, key: GpgHelpers::User1.public_key
2017-02-22 14:37:49 +00:00
expect(gpg_key.emails).to eq GpgHelpers::User1.emails
2017-02-22 14:37:49 +00:00
end
end
2017-02-22 11:49:17 +00:00
end