Skip to content
Snippets Groups Projects
Commit b972cc69 authored by Alexis Reigel's avatar Alexis Reigel :coffee:
Browse files

extract common method

parent 3091d196
No related branches found
No related tags found
Loading
This commit is part of merge request !9546. Comments created here will be created in the context of that merge request.
......@@ -8,10 +8,8 @@ module CurrentKeyChain
def add(key)
GPGME::Key.import(key)
end
end
def fingerprints_from_key(key)
using_tmp_keychain do
def fingerprints_from_key(key)
import = GPGME::Key.import(key)
return [] if import.imported == 0
......@@ -20,13 +18,15 @@ def fingerprints_from_key(key)
end
end
def primary_keyids_from_key(key)
def fingerprints_from_key(key)
using_tmp_keychain do
import = GPGME::Key.import(key)
return [] if import.imported == 0
CurrentKeyChain.fingerprints_from_key(key)
end
end
fingerprints = import.imports.map(&:fingerprint)
def primary_keyids_from_key(key)
using_tmp_keychain do
fingerprints = CurrentKeyChain.fingerprints_from_key(key)
GPGME::Key.find(:public, fingerprints).map { |raw_key| raw_key.primary_subkey.keyid }
end
......@@ -34,11 +34,7 @@ def primary_keyids_from_key(key)
def emails_from_key(key)
using_tmp_keychain do
import = GPGME::Key.import(key)
return [] if import.imported == 0
fingerprints = import.imports.map(&:fingerprint)
fingerprints = CurrentKeyChain.fingerprints_from_key(key)
GPGME::Key.find(:public, fingerprints).flat_map { |raw_key| raw_key.uids.map(&:email) }
end
......
......@@ -2,16 +2,15 @@
describe Gitlab::Gpg do
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]
before do
# make sure that each method is using the temporary keychain
expect(described_class).to receive(:using_tmp_keychain).and_call_original
end
it 'returns an empty array when the key is invalid' do
expect(
described_class.fingerprints_from_key('bogus')
).to eq []
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)
end
end
......@@ -65,4 +64,18 @@
)
end
end
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
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment