key_spec.rb 1.35 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1 2 3 4
require 'spec_helper'

describe Key do
  describe "Associations" do
5 6
    it { should belong_to(:user) }
    it { should belong_to(:project) }
gitlabhq's avatar
gitlabhq committed
7 8
  end

9 10 11 12 13
  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
14 15 16
  describe "Validation" do
    it { should validate_presence_of(:title) }
    it { should validate_presence_of(:key) }
17 18
    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
19 20
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
21
  describe "Methods" do
gitlabhq's avatar
gitlabhq committed
22 23 24
    it { should respond_to :projects }
  end

25 26 27
  context "validation of uniqueness" do

    context "as a deploy key" do
28
      let!(:deploy_key) { create(:deploy_key) }
29 30

      it "does not accept the same key twice for a project" do
31
        key = build(:key, project: deploy_key.project)
32 33 34 35
        key.should_not be_valid
      end

      it "does accept the same key for another project" do
36
        key = build(:key, project_id: 0)
37 38 39 40 41 42 43 44
        key.should be_valid
      end
    end

    context "as a personal key" do
      let(:user) { Factory.create(:user) }

      it "accepts the key once" do
45
        build(:key, user: user).should be_valid
46 47 48
      end

      it "does not accepts the key twice" do
49 50
        create(:key, user: user)
        build(:key, user: user).should_not be_valid
51 52 53
      end
    end
  end
gitlabhq's avatar
gitlabhq committed
54
end