BigW Consortium Gitlab

commit_spec.rb 4.05 KB
Newer Older
1 2
require 'rails_helper'

3
describe Gitlab::Gpg::Commit do
4 5
  describe '#signature' do
    let!(:project) { create :project, :repository, path: 'sample-project' }
Alexis Reigel committed
6
    let!(:commit_sha) { '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33'  }
7

8
    context 'unsigned commit' do
9
      it 'returns nil' do
10
        expect(described_class.new(project, commit_sha).signature).to be_nil
11 12 13
      end
    end

14
    context 'known and verified public key' do
15 16 17
      let!(:gpg_key) do
        create :gpg_key, key: GpgHelpers::User1.public_key, user: create(:user, email: GpgHelpers::User1.emails.first)
      end
18

19 20 21 22 23 24 25 26 27
      before do
        allow(Rugged::Commit).to receive(:extract_signature)
          .with(Rugged::Repository, commit_sha)
          .and_return(
            [
              GpgHelpers::User1.signed_commit_signature,
              GpgHelpers::User1.signed_commit_base_data
            ]
          )
28
      end
29

30
      it 'returns a valid signature' do
31
        expect(described_class.new(project, commit_sha).signature).to have_attributes(
Alexis Reigel committed
32
          commit_sha: commit_sha,
33 34 35
          project: project,
          gpg_key: gpg_key,
          gpg_key_primary_keyid: GpgHelpers::User1.primary_keyid,
36 37
          gpg_key_user_name: GpgHelpers::User1.names.first,
          gpg_key_user_email: GpgHelpers::User1.emails.first,
38 39 40
          valid_signature: true
        )
      end
41 42

      it 'returns the cached signature on second call' do
43
        gpg_commit = described_class.new(project, commit_sha)
44

45
        expect(gpg_commit).to receive(:using_keychain).and_call_original
46 47 48
        gpg_commit.signature

        # consecutive call
49
        expect(gpg_commit).not_to receive(:using_keychain).and_call_original
50 51
        gpg_commit.signature
      end
52 53
    end

54
    context 'known but unverified public key' do
55
      let!(:gpg_key) { create :gpg_key, key: GpgHelpers::User1.public_key }
56

57 58 59 60 61 62 63 64 65
      before do
        allow(Rugged::Commit).to receive(:extract_signature)
          .with(Rugged::Repository, commit_sha)
          .and_return(
            [
              GpgHelpers::User1.signed_commit_signature,
              GpgHelpers::User1.signed_commit_base_data
            ]
          )
66
      end
67

68
      it 'returns an invalid signature' do
69
        expect(described_class.new(project, commit_sha).signature).to have_attributes(
Alexis Reigel committed
70
          commit_sha: commit_sha,
71 72 73
          project: project,
          gpg_key: gpg_key,
          gpg_key_primary_keyid: GpgHelpers::User1.primary_keyid,
74 75
          gpg_key_user_name: GpgHelpers::User1.names.first,
          gpg_key_user_email: GpgHelpers::User1.emails.first,
76 77 78
          valid_signature: false
        )
      end
79 80

      it 'returns the cached signature on second call' do
81
        gpg_commit = described_class.new(project, commit_sha)
82

83
        expect(gpg_commit).to receive(:using_keychain).and_call_original
84 85 86
        gpg_commit.signature

        # consecutive call
87
        expect(gpg_commit).not_to receive(:using_keychain).and_call_original
88 89
        gpg_commit.signature
      end
90 91
    end

92
    context 'unknown public key' do
93 94 95 96 97 98 99 100 101
      before do
        allow(Rugged::Commit).to receive(:extract_signature)
          .with(Rugged::Repository, commit_sha)
          .and_return(
            [
              GpgHelpers::User1.signed_commit_signature,
              GpgHelpers::User1.signed_commit_base_data
            ]
          )
102
      end
103

104
      it 'returns an invalid signature' do
105
        expect(described_class.new(project, commit_sha).signature).to have_attributes(
Alexis Reigel committed
106
          commit_sha: commit_sha,
107 108
          project: project,
          gpg_key: nil,
109
          gpg_key_primary_keyid: GpgHelpers::User1.primary_keyid,
110 111
          gpg_key_user_name: nil,
          gpg_key_user_email: nil,
112 113 114
          valid_signature: false
        )
      end
115 116

      it 'returns the cached signature on second call' do
117
        gpg_commit = described_class.new(project, commit_sha)
118

119
        expect(gpg_commit).to receive(:using_keychain).and_call_original
120 121 122
        gpg_commit.signature

        # consecutive call
123
        expect(gpg_commit).not_to receive(:using_keychain).and_call_original
124 125
        gpg_commit.signature
      end
126 127 128
    end
  end
end