BigW Consortium Gitlab

gpg_key_spec.rb 7.91 KB
Newer Older
Alexis Reigel committed
1 2 3 4 5
require 'rails_helper'

describe GpgKey do
  describe "associations" do
    it { is_expected.to belong_to(:user) }
6
    it { is_expected.to have_many(:subkeys) }
Alexis Reigel committed
7 8 9
  end

  describe "validation" do
10
    it { is_expected.to validate_presence_of(:user) }
11

Alexis Reigel committed
12 13
    it { is_expected.to validate_presence_of(:key) }
    it { is_expected.to validate_uniqueness_of(:key) }
14 15 16 17

    it { is_expected.to allow_value("-----BEGIN PGP PUBLIC KEY BLOCK-----\nkey\n-----END PGP PUBLIC KEY BLOCK-----").for(:key) }

    it { is_expected.not_to allow_value("-----BEGIN PGP PUBLIC KEY BLOCK-----\nkey").for(:key) }
Alexis Reigel committed
18
    it { is_expected.not_to allow_value("-----BEGIN PGP PUBLIC KEY BLOCK-----\nkey\n-----BEGIN PGP PUBLIC KEY BLOCK-----").for(:key) }
19 20 21 22
    it { is_expected.not_to allow_value("-----BEGIN PGP PUBLIC KEY BLOCK----------END PGP PUBLIC KEY BLOCK-----").for(:key) }
    it { is_expected.not_to allow_value("-----BEGIN PGP PUBLIC KEY BLOCK-----").for(:key) }
    it { is_expected.not_to allow_value("-----END PGP PUBLIC KEY BLOCK-----").for(:key) }
    it { is_expected.not_to allow_value("key\n-----END PGP PUBLIC KEY BLOCK-----").for(:key) }
Alexis Reigel committed
23 24 25
    it { is_expected.not_to allow_value('BEGIN PGP').for(:key) }
  end

26
  context 'callbacks' do
Alexis Reigel committed
27
    describe 'extract_fingerprint' do
28
      it 'extracts the fingerprint from the gpg key' do
29
        gpg_key = described_class.new(key: GpgHelpers::User1.public_key)
Alexis Reigel committed
30
        gpg_key.valid?
31
        expect(gpg_key.fingerprint).to eq GpgHelpers::User1.fingerprint
Alexis Reigel committed
32 33
      end
    end
34 35 36 37 38 39 40 41

    describe 'extract_primary_keyid' do
      it 'extracts the primary keyid from the gpg key' do
        gpg_key = described_class.new(key: GpgHelpers::User1.public_key)
        gpg_key.valid?
        expect(gpg_key.primary_keyid).to eq GpgHelpers::User1.primary_keyid
      end
    end
42 43 44 45 46 47 48 49

    describe 'generate_subkeys' do
      it 'extracts the subkeys from the gpg key' do
        gpg_key = create(:gpg_key, key: GpgHelpers::User1.public_key_with_extra_signing_key)

        expect(gpg_key.subkeys.count).to eq(2)
      end
    end
Alexis Reigel committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63
  end

  describe '#key=' do
    it 'strips white spaces' do
      key = <<~KEY.strip
        -----BEGIN PGP PUBLIC KEY BLOCK-----
        Version: GnuPG v1

        mQENBFMOSOgBCADFCYxmnXFbrDhfvlf03Q/bQuT+nZu46BFGbo7XkUjDowFXJQhP
        -----END PGP PUBLIC KEY BLOCK-----
      KEY

      expect(described_class.new(key: " #{key} ").key).to eq(key)
    end
Alexis Reigel committed
64 65 66 67

    it 'does not strip when the key is nil' do
      expect(described_class.new(key: nil).key).to be_nil
    end
Alexis Reigel committed
68
  end
69

70 71
  describe '#user_infos' do
    it 'returns the user infos from the gpg key' do
72
      gpg_key = create :gpg_key, key: GpgHelpers::User1.public_key
73
      expect(Gitlab::Gpg).to receive(:user_infos_from_key).with(gpg_key.key)
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      gpg_key.user_infos
    end
  end

  describe '#verified_user_infos' do
    it 'returns the user infos if it is verified' do
      user = create :user, email: GpgHelpers::User1.emails.first
      gpg_key = create :gpg_key, key: GpgHelpers::User1.public_key, user: user

      expect(gpg_key.verified_user_infos).to eq([{
        name: GpgHelpers::User1.names.first,
        email: GpgHelpers::User1.emails.first
      }])
    end

    it 'returns an empty array if the user info is not verified' do
      user = create :user, email: 'unrelated@example.com'
      gpg_key = create :gpg_key, key: GpgHelpers::User1.public_key, user: user

      expect(gpg_key.verified_user_infos).to eq([])
95 96
    end
  end
Alexis Reigel committed
97

98
  describe '#emails_with_verified_status' do
99
    it 'email is verified if the user has the matching email' do
Brett Walker committed
100
      user = create :user, email: 'bette.cartwright@example.com'
101
      gpg_key = create :gpg_key, key: GpgHelpers::User2.public_key, user: user
Brett Walker committed
102
      create :email, user: user
103 104 105 106
      user.reload

      expect(gpg_key.emails_with_verified_status).to eq(
        'bette.cartwright@example.com' => true,
Brett Walker committed
107
        'bette.cartwright@example.net' => false
108
      )
109

Brett Walker committed
110
      create :email, :confirmed, user: user, email: 'bette.cartwright@example.net'
111
      user.reload
112 113
      expect(gpg_key.emails_with_verified_status).to eq(
        'bette.cartwright@example.com' => true,
114
        'bette.cartwright@example.net' => true
115
      )
116 117 118
    end
  end

119
  describe '#verified?' do
120
    it 'returns true if one of the email addresses in the key belongs to the user' do
121 122 123 124 125 126
      user = create :user, email: 'bette.cartwright@example.com'
      gpg_key = create :gpg_key, key: GpgHelpers::User2.public_key, user: user

      expect(gpg_key.verified?).to be_truthy
    end

127
    it 'returns false if none of the email addresses in the key does not belong to the user' do
128 129 130 131 132 133 134
      user = create :user, email: 'someone.else@example.com'
      gpg_key = create :gpg_key, key: GpgHelpers::User2.public_key, user: user

      expect(gpg_key.verified?).to be_falsey
    end
  end

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  describe 'verified_and_belongs_to_email?' do
    it 'returns false if none of the email addresses in the key does not belong to the user' do
      user = create :user, email: 'someone.else@example.com'
      gpg_key = create :gpg_key, key: GpgHelpers::User2.public_key, user: user

      expect(gpg_key.verified?).to be_falsey
      expect(gpg_key.verified_and_belongs_to_email?('someone.else@example.com')).to be_falsey
    end

    it 'returns false if one of the email addresses in the key belongs to the user and does not match the provided email' do
      user = create :user, email: 'bette.cartwright@example.com'
      gpg_key = create :gpg_key, key: GpgHelpers::User2.public_key, user: user

      expect(gpg_key.verified?).to be_truthy
      expect(gpg_key.verified_and_belongs_to_email?('bette.cartwright@example.net')).to be_falsey
    end

    it 'returns true if one of the email addresses in the key belongs to the user and matches the provided email' do
      user = create :user, email: 'bette.cartwright@example.com'
      gpg_key = create :gpg_key, key: GpgHelpers::User2.public_key, user: user

      expect(gpg_key.verified?).to be_truthy
      expect(gpg_key.verified_and_belongs_to_email?('bette.cartwright@example.com')).to be_truthy
    end
159 160 161 162 163 164 165 166

    it 'returns true if one of the email addresses in the key belongs to the user and case-insensitively matches the provided email' do
      user = create :user, email: 'bette.cartwright@example.com'
      gpg_key = create :gpg_key, key: GpgHelpers::User2.public_key, user: user

      expect(gpg_key.verified?).to be_truthy
      expect(gpg_key.verified_and_belongs_to_email?('Bette.Cartwright@example.com')).to be_truthy
    end
167 168
  end

169 170 171
  describe '#revoke' do
    it 'invalidates all associated gpg signatures and destroys the key' do
      gpg_key = create :gpg_key
172
      gpg_signature = create :gpg_signature, verification_status: :verified, gpg_key: gpg_key
173 174

      unrelated_gpg_key = create :gpg_key, key: GpgHelpers::User2.public_key
175
      unrelated_gpg_signature = create :gpg_signature, verification_status: :verified, gpg_key: unrelated_gpg_key
176 177 178 179

      gpg_key.revoke

      expect(gpg_signature.reload).to have_attributes(
180
        verification_status: 'unknown_key',
181 182 183 184 185 186 187
        gpg_key: nil
      )

      expect(gpg_key.destroyed?).to be true

      # unrelated signature is left untouched
      expect(unrelated_gpg_signature.reload).to have_attributes(
188
        verification_status: 'verified',
189 190 191 192 193
        gpg_key: unrelated_gpg_key
      )

      expect(unrelated_gpg_key.destroyed?).to be false
    end
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

    it 'deletes all the associated subkeys' do
      gpg_key = create :gpg_key, key: GpgHelpers::User3.public_key

      expect(gpg_key.subkeys).to be_present

      gpg_key.revoke

      expect(gpg_key.subkeys(true)).to be_blank
    end

    it 'invalidates all signatures associated to the subkeys' do
      gpg_key = create :gpg_key, key: GpgHelpers::User3.public_key
      gpg_key_subkey = gpg_key.subkeys.last
      gpg_signature = create :gpg_signature, verification_status: :verified, gpg_key: gpg_key_subkey

      gpg_key.revoke

      expect(gpg_signature.reload).to have_attributes(
        verification_status: 'unknown_key',
        gpg_key: nil,
        gpg_key_subkey: nil
      )
    end
218
  end
Alexis Reigel committed
219
end