Commit 28bb5e3d authored by Alexis Reigel's avatar Alexis Reigel

commit signature with spec

parent 817d9558
...@@ -237,7 +237,7 @@ class Commit ...@@ -237,7 +237,7 @@ class Commit
def signature def signature
return @signature if defined?(@signature) return @signature if defined?(@signature)
sig, signed = @raw.extract_signature(project.repository.raw_repository) sig, signed = @raw.signature(project.repository)
if sig && signed if sig && signed
GPGME::Crypto.new.verify(sig, signed_text: signed) do |sign| GPGME::Crypto.new.verify(sig, signed_text: signed) do |sign|
@signature = sign @signature = sign
......
...@@ -319,7 +319,12 @@ module Gitlab ...@@ -319,7 +319,12 @@ module Gitlab
end end
end end
def extract_signature(repo) # Get the gpg signature of this commit.
#
# Ex.
# commit.signature(repo)
#
def signature(repo)
Rugged::Commit.extract_signature(repo.rugged, sha) Rugged::Commit.extract_signature(repo.rugged, sha)
end end
......
...@@ -414,4 +414,44 @@ eos ...@@ -414,4 +414,44 @@ eos
expect(described_class.valid_hash?('a' * 41)).to be false expect(described_class.valid_hash?('a' * 41)).to be false
end end
end end
describe '#signature' do
it 'returns nil if the commit is not signed' do
expect(commit.signature).to be_nil
end
context 'signed commit', :gpg do
it 'returns a valid signature if the public key is known' do
GPGME::Key.import(GpgHelpers.public_key)
raw_commit = double(:raw_commit, signature: [
GpgHelpers.signed_commit_signature,
GpgHelpers.signed_commit_base_data
])
allow(raw_commit).to receive :save!
commit = create :commit,
git_commit: raw_commit,
project: project
expect(commit.signature).to be_a GPGME::Signature
expect(commit.signature.valid?).to be_truthy
end
it 'returns an invalid signature if the public commit is unknown', :gpg do
raw_commit = double(:raw_commit, signature: [
GpgHelpers.signed_commit_signature,
GpgHelpers.signed_commit_base_data
])
allow(raw_commit).to receive :save!
commit = create :commit,
git_commit: raw_commit,
project: project
expect(commit.signature).to be_a GPGME::Signature
expect(commit.signature.valid?).to be_falsey
end
end
end
end end
...@@ -141,6 +141,18 @@ RSpec.configure do |config| ...@@ -141,6 +141,18 @@ RSpec.configure do |config|
config.around(:each, :postgresql) do |example| config.around(:each, :postgresql) do |example|
example.run if Gitlab::Database.postgresql? example.run if Gitlab::Database.postgresql?
end end
config.around(:each, :gpg) do |example|
Dir.mktmpdir do |dir|
original_dir = GPGME::Engine.dirinfo('homedir')
GPGME::Engine.home_dir = dir
example.run
GPGME::Engine.home_dir = original_dir
end
end
end end
FactoryGirl::SyntaxRunner.class_eval do FactoryGirl::SyntaxRunner.class_eval do
......
This diff is collapsed.
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