Commit 0668521b authored by Alexis Reigel's avatar Alexis Reigel

move current keychain methods to namespace

parent 0e3d3d60
...@@ -40,10 +40,10 @@ class GpgKey < ActiveRecord::Base ...@@ -40,10 +40,10 @@ class GpgKey < ActiveRecord::Base
end end
def add_to_keychain def add_to_keychain
Gitlab::Gpg.add_to_keychain(key) Gitlab::Gpg::CurrentKeyChain.add(key)
end end
def remove_from_keychain def remove_from_keychain
Gitlab::Gpg.remove_from_keychain(fingerprint) Gitlab::Gpg::CurrentKeyChain.remove(fingerprint)
end end
end end
...@@ -5,6 +5,14 @@ module Gitlab ...@@ -5,6 +5,14 @@ module Gitlab
module CurrentKeyChain module CurrentKeyChain
extend self extend self
def add(key)
GPGME::Key.import(key)
end
def remove(fingerprint)
GPGME::Key.get(fingerprint).delete!
end
def emails(fingerprint) def emails(fingerprint)
GPGME::Key.find(:public, fingerprint).flat_map { |raw_key| raw_key.uids.map(&:email) } GPGME::Key.find(:public, fingerprint).flat_map { |raw_key| raw_key.uids.map(&:email) }
end end
...@@ -32,14 +40,6 @@ module Gitlab ...@@ -32,14 +40,6 @@ module Gitlab
end end
end end
def add_to_keychain(key)
GPGME::Key.import(key)
end
def remove_from_keychain(fingerprint)
GPGME::Key.get(fingerprint).delete!
end
def using_tmp_keychain def using_tmp_keychain
Dir.mktmpdir do |dir| Dir.mktmpdir do |dir|
@original_dirs ||= [GPGME::Engine.dirinfo('homedir')] @original_dirs ||= [GPGME::Engine.dirinfo('homedir')]
......
...@@ -28,37 +28,37 @@ describe Gitlab::Gpg do ...@@ -28,37 +28,37 @@ describe Gitlab::Gpg do
).to eq [] ).to eq []
end end
end end
end
describe Gitlab::Gpg::CurrentKeyChain, :gpg do
describe '.emails' do
it 'returns the emails' do
Gitlab::Gpg::CurrentKeyChain.add(GpgHelpers::User2.public_key)
expect(
described_class.emails(GpgHelpers::User2.fingerprint)
).to match_array GpgHelpers::User2.emails
end
end
describe '.add_to_keychain', :gpg do describe '.add', :gpg do
it 'stores the key in the keychain' do it 'stores the key in the keychain' do
expect(GPGME::Key.find(:public, GpgHelpers::User1.fingerprint)).to eq [] expect(GPGME::Key.find(:public, GpgHelpers::User1.fingerprint)).to eq []
Gitlab::Gpg.add_to_keychain(GpgHelpers::User1.public_key) Gitlab::Gpg::CurrentKeyChain.add(GpgHelpers::User1.public_key)
expect(GPGME::Key.find(:public, GpgHelpers::User1.fingerprint)).not_to eq [] expect(GPGME::Key.find(:public, GpgHelpers::User1.fingerprint)).not_to eq []
end end
end end
describe '.remove_from_keychain', :gpg do describe '.remove', :gpg do
it 'removes the key from the keychain' do it 'removes the key from the keychain' do
Gitlab::Gpg.add_to_keychain(GpgHelpers::User1.public_key) Gitlab::Gpg::CurrentKeyChain.add(GpgHelpers::User1.public_key)
expect(GPGME::Key.find(:public, GpgHelpers::User1.fingerprint)).not_to eq [] expect(GPGME::Key.find(:public, GpgHelpers::User1.fingerprint)).not_to eq []
Gitlab::Gpg.remove_from_keychain(GpgHelpers::User1.fingerprint) Gitlab::Gpg::CurrentKeyChain.remove(GpgHelpers::User1.fingerprint)
expect(GPGME::Key.find(:public, GpgHelpers::User1.fingerprint)).to eq [] expect(GPGME::Key.find(:public, GpgHelpers::User1.fingerprint)).to eq []
end end
end end
end end
describe Gitlab::Gpg::CurrentKeyChain, :gpg do
describe '.emails' do
it 'returns the emails' do
Gitlab::Gpg.add_to_keychain(GpgHelpers::User2.public_key)
expect(
described_class.emails(GpgHelpers::User2.fingerprint)
).to match_array GpgHelpers::User2.emails
end
end
end
...@@ -24,17 +24,19 @@ describe GpgKey do ...@@ -24,17 +24,19 @@ describe GpgKey do
describe 'add_to_keychain' do describe 'add_to_keychain' do
it 'calls add_to_keychain after create' do it 'calls add_to_keychain after create' do
expect(Gitlab::Gpg).to receive(:add_to_keychain).with(GpgHelpers::User1.public_key) expect(Gitlab::Gpg::CurrentKeyChain).to receive(:add).with(GpgHelpers::User1.public_key)
create :gpg_key create :gpg_key
end end
end end
describe 'remove_from_keychain' do describe 'remove_from_keychain' do
it 'calls remove_from_keychain after destroy' do it 'calls remove_from_keychain after destroy' do
allow(Gitlab::Gpg).to receive :add_to_keychain allow(Gitlab::Gpg::CurrentKeyChain).to receive :add
gpg_key = create :gpg_key gpg_key = create :gpg_key
expect(Gitlab::Gpg).to receive(:remove_from_keychain).with(GpgHelpers::User1.fingerprint) expect(
Gitlab::Gpg::CurrentKeyChain
).to receive(:remove).with(GpgHelpers::User1.fingerprint)
gpg_key.destroy! gpg_key.destroy!
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment