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

gitlabhq's avatar
gitlabhq committed
15 16 17 18
require 'spec_helper'

describe Key do
  describe "Associations" do
19
    it { should belong_to(:user) }
gitlabhq's avatar
gitlabhq committed
20 21
  end

22 23 24 25 26
  describe "Mass assignment" do
    it { should_not allow_mass_assignment_of(:project_id) }
    it { should_not allow_mass_assignment_of(:user_id) }
  end

gitlabhq's avatar
gitlabhq committed
27 28 29
  describe "Validation" do
    it { should validate_presence_of(:title) }
    it { should validate_presence_of(:key) }
30 31
    it { should ensure_length_of(:title).is_within(0..255) }
    it { should ensure_length_of(:key).is_within(0..5000) }
gitlabhq's avatar
gitlabhq committed
32 33
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
34
  describe "Methods" do
gitlabhq's avatar
gitlabhq committed
35 36 37
    it { should respond_to :projects }
  end

38
  context "validation of uniqueness" do
39
    let(:user) { create(:user) }
40

41 42
    it "accepts the key once" do
      build(:key, user: user).should be_valid
43 44
    end

45
    it "does not accept the exact same key twice" do
46 47
      create(:key, user: user)
      build(:key, user: user).should_not be_valid
48
    end
49 50 51 52 53 54 55

    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'
      duplicate.should_not be_valid
    end
56
  end
57 58 59

  context "validate it is a fingerprintable key" do
    it "accepts the fingerprintable key" do
60
      build(:key).should be_valid
61 62
    end

63
    it "rejects the unfingerprintable key (contains space in middle)" do
64 65
      build(:key_with_a_space_in_the_middle).should_not be_valid
    end
66 67 68 69

    it "rejects the unfingerprintable key (not a key)" do
      build(:invalid_key).should_not be_valid
    end
70
  end
gitlabhq's avatar
gitlabhq committed
71
end