BigW Consortium Gitlab

key_spec.rb 2.72 KB
Newer Older
Dmitriy Zaporozhets committed
1 2 3 4
# == Schema Information
#
# Table name: keys
#
Dmitriy Zaporozhets committed
5 6
#  id          :integer          not null, primary key
#  user_id     :integer
Dmitriy Zaporozhets committed
7 8
#  created_at  :datetime
#  updated_at  :datetime
Dmitriy Zaporozhets committed
9 10 11 12
#  key         :text
#  title       :string(255)
#  type        :string(255)
#  fingerprint :string(255)
Stan Hu committed
13
#  public      :boolean          default(FALSE), not null
Dmitriy Zaporozhets committed
14 15
#

gitlabhq committed
16 17
require 'spec_helper'

Douwe Maan committed
18
describe Key, models: true do
gitlabhq committed
19
  describe "Associations" do
20
    it { is_expected.to belong_to(:user) }
gitlabhq committed
21 22
  end

23 24 25
  describe "Mass assignment" do
  end

gitlabhq committed
26
  describe "Validation" do
27 28
    it { is_expected.to validate_presence_of(:title) }
    it { is_expected.to validate_presence_of(:key) }
29 30
    it { is_expected.to validate_length_of(:title).is_within(0..255) }
    it { is_expected.to validate_length_of(:key).is_within(0..5000) }
gitlabhq committed
31 32
  end

Nihad Abbasov committed
33
  describe "Methods" do
34
    it { is_expected.to respond_to :projects }
35 36 37 38 39 40 41
    it { is_expected.to respond_to :publishable_key }

    describe "#publishable_keys" do
      it 'strips all personal information' do
        expect(build(:key).publishable_key).not_to match(/dummy@gitlab/)
      end
    end
gitlabhq committed
42 43
  end

44
  context "validation of uniqueness" do
45
    let(:user) { create(:user) }
46

47
    it "accepts the key once" do
48
      expect(build(:key, user: user)).to be_valid
49 50
    end

51
    it "does not accept the exact same key twice" do
52
      create(:key, user: user)
53
      expect(build(:key, user: user)).not_to be_valid
54
    end
55 56 57 58 59

    it "does not accept a duplicate key with a different comment" do
      create(:key, user: user)
      duplicate = build(:key, user: user)
      duplicate.key << ' extra comment'
60
      expect(duplicate).not_to be_valid
61
    end
62
  end
63 64 65

  context "validate it is a fingerprintable key" do
    it "accepts the fingerprintable key" do
66
      expect(build(:key)).to be_valid
67 68
    end

69 70 71 72
    it 'rejects an unfingerprintable key that contains a space' do
      key = build(:key)

      # Not always the middle, but close enough
73
      key.key = key.key[0..100] + ' ' + key.key[101..-1]
74 75

      expect(key).not_to be_valid
76
    end
77

78 79
    it 'rejects the unfingerprintable key (not a key)' do
      expect(build(:key, key: 'ssh-rsa an-invalid-key==')).not_to be_valid
80
    end
81 82 83

    it 'rejects the multiple line key' do
      key = build(:key)
84
      key.key.tr!(' ', "\n")
85 86
      expect(key).not_to be_valid
    end
87
  end
88 89 90 91

  context 'callbacks' do
    it 'should add new key to authorized_file' do
      @key = build(:personal_key, id: 7)
92
      expect(GitlabShellWorker).to receive(:perform_async).with(:add_key, @key.shell_id, @key.key)
93 94 95 96 97
      @key.save
    end

    it 'should remove key from authorized_file' do
      @key = create(:personal_key)
98
      expect(GitlabShellWorker).to receive(:perform_async).with(:remove_key, @key.shell_id, @key.key)
99 100 101
      @key.destroy
    end
  end
gitlabhq committed
102
end