2017-02-22 16:20:42 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Gpg do
|
|
|
|
describe '.fingerprints_from_key' do
|
2017-07-06 09:55:56 +00:00
|
|
|
before do
|
|
|
|
# make sure that each method is using the temporary keychain
|
|
|
|
expect(described_class).to receive(:using_tmp_keychain).and_call_original
|
2017-02-22 16:20:42 +00:00
|
|
|
end
|
|
|
|
|
2017-07-06 09:55:56 +00:00
|
|
|
it 'returns CurrentKeyChain.fingerprints_from_key' do
|
|
|
|
expect(Gitlab::Gpg::CurrentKeyChain).to receive(:fingerprints_from_key).with(GpgHelpers::User1.public_key)
|
|
|
|
|
|
|
|
described_class.fingerprints_from_key(GpgHelpers::User1.public_key)
|
2017-02-22 16:20:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-13 11:46:43 +00:00
|
|
|
describe '.primary_keyids_from_key' do
|
|
|
|
it 'returns the keyid' do
|
|
|
|
expect(
|
|
|
|
described_class.primary_keyids_from_key(GpgHelpers::User1.public_key)
|
|
|
|
).to eq [GpgHelpers::User1.primary_keyid]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty array when the key is invalid' do
|
|
|
|
expect(
|
|
|
|
described_class.primary_keyids_from_key('bogus')
|
|
|
|
).to eq []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-13 13:22:15 +00:00
|
|
|
describe '.user_infos_from_key' do
|
|
|
|
it 'returns the names and emails' do
|
|
|
|
user_infos = described_class.user_infos_from_key(GpgHelpers::User1.public_key)
|
|
|
|
expect(user_infos).to eq([{
|
|
|
|
name: GpgHelpers::User1.names.first,
|
|
|
|
email: GpgHelpers::User1.emails.first
|
|
|
|
}])
|
2017-02-24 19:07:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty array when the key is invalid' do
|
|
|
|
expect(
|
2017-07-13 13:22:15 +00:00
|
|
|
described_class.user_infos_from_key('bogus')
|
2017-02-24 19:07:57 +00:00
|
|
|
).to eq []
|
|
|
|
end
|
|
|
|
end
|
2017-08-09 14:43:10 +00:00
|
|
|
|
|
|
|
describe '.current_home_dir' do
|
|
|
|
let(:default_home_dir) { GPGME::Engine.dirinfo('homedir') }
|
|
|
|
|
|
|
|
it 'returns the default value when no explicit home dir has been set' do
|
|
|
|
expect(described_class.current_home_dir).to eq default_home_dir
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the explicitely set home dir' do
|
|
|
|
GPGME::Engine.home_dir = '/tmp/gpg'
|
|
|
|
|
|
|
|
expect(described_class.current_home_dir).to eq '/tmp/gpg'
|
|
|
|
|
|
|
|
GPGME::Engine.home_dir = GPGME::Engine.dirinfo('homedir')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the default value when explicitely setting the home dir to nil' do
|
|
|
|
GPGME::Engine.home_dir = nil
|
|
|
|
|
|
|
|
expect(described_class.current_home_dir).to eq default_home_dir
|
|
|
|
end
|
|
|
|
end
|
2017-02-24 19:16:04 +00:00
|
|
|
end
|
2017-06-13 12:26:42 +00:00
|
|
|
|
2017-06-28 11:08:29 +00:00
|
|
|
describe Gitlab::Gpg::CurrentKeyChain do
|
|
|
|
around do |example|
|
|
|
|
Gitlab::Gpg.using_tmp_keychain do
|
|
|
|
example.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.add' do
|
2017-06-13 12:26:42 +00:00
|
|
|
it 'stores the key in the keychain' do
|
|
|
|
expect(GPGME::Key.find(:public, GpgHelpers::User1.fingerprint)).to eq []
|
|
|
|
|
|
|
|
described_class.add(GpgHelpers::User1.public_key)
|
|
|
|
|
|
|
|
keys = GPGME::Key.find(:public, GpgHelpers::User1.fingerprint)
|
|
|
|
expect(keys.count).to eq 1
|
|
|
|
expect(keys.first).to have_attributes(
|
|
|
|
email: GpgHelpers::User1.emails.first,
|
|
|
|
fingerprint: GpgHelpers::User1.fingerprint
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2017-07-06 09:55:56 +00:00
|
|
|
|
|
|
|
describe '.fingerprints_from_key' do
|
|
|
|
it 'returns the fingerprint' do
|
|
|
|
expect(
|
|
|
|
described_class.fingerprints_from_key(GpgHelpers::User1.public_key)
|
|
|
|
).to eq [GpgHelpers::User1.fingerprint]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty array when the key is invalid' do
|
|
|
|
expect(
|
|
|
|
described_class.fingerprints_from_key('bogus')
|
|
|
|
).to eq []
|
|
|
|
end
|
|
|
|
end
|
2017-06-13 12:26:42 +00:00
|
|
|
end
|