summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ssh_public_key_spec.rb
blob: 61e6ef85a870e2abe83839cb213e891d3197a223 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require 'spec_helper'

describe Gitlab::SSHPublicKey, lib: true do
  let(:key) { attributes_for(:rsa_key_2048)[:key] }
  let(:public_key) { described_class.new(key) }

  describe '.technology_names' do
    it 'returns the available technology names' do
      expect(described_class.technology_names).to eq(%w[rsa dsa ecdsa])
    end
  end

  describe '.allowed_sizes(name)' do
    {
      'rsa' => [1024, 2048, 3072, 4096],
      'dsa' => [1024, 2048, 3072],
      'ecdsa' => [256, 384, 521]
    }.each do |name, sizes|
      it "returns '#{sizes}' for #{name}" do
        expect(described_class.allowed_sizes(name)).to eq(sizes)
      end
    end
  end

  describe '.allowed_type?' do
    it 'determines the key type' do
      expect(described_class.allowed_type?('foo')).to be(false)
    end
  end

  describe '#valid?' do
    context 'with a valid SSH key' do
      it 'returns true' do
        expect(public_key).to be_valid
      end
    end

    context 'with an invalid SSH key' do
      let(:key) { 'this is not a key' }

      it 'returns false' do
        expect(public_key).not_to be_valid
      end
    end
  end

  describe '#type' do
    context 'with a DSA key' do
      let(:key) { attributes_for(:dsa_key)[:key] }

      it 'determines the key type' do
        expect(public_key.type).to eq(:dsa)
      end
    end

    context 'with a ECDSA key' do
      let(:key) { attributes_for(:ecdsa_key)[:key] }

      it 'determines the key type' do
        expect(public_key.type).to eq(:ecdsa)
      end
    end

    context 'with a RSA key' do
      it 'determines the key type' do
        expect(public_key.type).to eq(:rsa)
      end
    end

    context 'with an invalid SSH key' do
      let(:key) { 'this is not a key' }

      it 'determines the key type' do
        expect(public_key.type).to be_nil
      end
    end
  end

  describe '#size' do
    it 'determines the key length in bits' do
      expect(public_key.size).to eq(1024)
    end

    context 'with an invalid SSH key' do
      let(:key) { 'this is not a key' }

      it 'determines the key type' do
        expect(public_key.size).to be_nil
      end
    end
  end

  describe '#fingerprint' do
    let(:fingerprint) { '3f:a2:ee:de:b5:de:53:c3:aa:2f:9c:45:24:4c:47:7b' }

    it "generates the key's fingerprint" do
      expect(public_key.fingerprint).to eq(fingerprint)
    end
  end
end