Commit 97423a0bed1148f65f9ed2bd8bf540e764d9576c

Authored by Robert Speicher
1 parent 2bdea865

Add more coverage for model validations and associations

spec/factories.rb
@@ -18,11 +18,15 @@ FactoryGirl.define do @@ -18,11 +18,15 @@ FactoryGirl.define do
18 Faker::Lorem.sentence 18 Faker::Lorem.sentence
19 end 19 end
20 20
  21 + sequence :name, aliases: [:file_name] do
  22 + Faker::Name.name
  23 + end
  24 +
21 sequence(:url) { Faker::Internet.uri('http') } 25 sequence(:url) { Faker::Internet.uri('http') }
22 26
23 factory :user, aliases: [:author, :assignee, :owner] do 27 factory :user, aliases: [:author, :assignee, :owner] do
24 email { Faker::Internet.email } 28 email { Faker::Internet.email }
25 - name { Faker::Name.name } 29 + name
26 password "123456" 30 password "123456"
27 password_confirmation "123456" 31 password_confirmation "123456"
28 32
@@ -116,6 +120,11 @@ FactoryGirl.define do @@ -116,6 +120,11 @@ FactoryGirl.define do
116 author 120 author
117 title 121 title
118 content 122 content
119 - file_name { Faker::Lorem.sentence } 123 + file_name
  124 + end
  125 +
  126 + factory :protected_branch do
  127 + name
  128 + project
120 end 129 end
121 end 130 end
spec/models/event_spec.rb
@@ -3,6 +3,7 @@ require 'spec_helper' @@ -3,6 +3,7 @@ require 'spec_helper'
3 describe Event do 3 describe Event do
4 describe "Associations" do 4 describe "Associations" do
5 it { should belong_to(:project) } 5 it { should belong_to(:project) }
  6 + it { should belong_to(:target) }
6 end 7 end
7 8
8 describe "Respond to" do 9 describe "Respond to" do
spec/models/issue_spec.rb
@@ -2,21 +2,11 @@ require 'spec_helper' @@ -2,21 +2,11 @@ require 'spec_helper'
2 2
3 describe Issue do 3 describe Issue do
4 describe "Associations" do 4 describe "Associations" do
5 - it { should belong_to(:project) }  
6 - it { should belong_to(:author) }  
7 - it { should belong_to(:assignee) }  
8 it { should belong_to(:milestone) } 5 it { should belong_to(:milestone) }
9 end 6 end
10 7
11 describe "Validation" do 8 describe "Validation" do
12 - it { should validate_presence_of(:title) }  
13 - it { should validate_presence_of(:author_id) }  
14 - it { should validate_presence_of(:project_id) }  
15 - end  
16 -  
17 - describe "Scope" do  
18 - it { Issue.should respond_to :closed }  
19 - it { Issue.should respond_to :opened } 9 + it { should ensure_length_of(:description).is_within(0..2000) }
20 end 10 end
21 11
22 describe 'modules' do 12 describe 'modules' do
spec/models/key_spec.rb
@@ -2,12 +2,15 @@ require 'spec_helper' @@ -2,12 +2,15 @@ require 'spec_helper'
2 2
3 describe Key do 3 describe Key do
4 describe "Associations" do 4 describe "Associations" do
5 - it { should belong_to(:user) or belong_to(:project) } 5 + it { should belong_to(:user) }
  6 + it { should belong_to(:project) }
6 end 7 end
7 8
8 describe "Validation" do 9 describe "Validation" do
9 it { should validate_presence_of(:title) } 10 it { should validate_presence_of(:title) }
10 it { should validate_presence_of(:key) } 11 it { should validate_presence_of(:key) }
  12 + it { should ensure_length_of(:title).is_within(0..255) }
  13 + it { should ensure_length_of(:key).is_within(0..5000) }
11 end 14 end
12 15
13 describe "Methods" do 16 describe "Methods" do
spec/models/merge_request_spec.rb
1 require 'spec_helper' 1 require 'spec_helper'
2 2
3 describe MergeRequest do 3 describe MergeRequest do
4 - describe "Associations" do  
5 - it { should belong_to(:project) }  
6 - it { should belong_to(:author) }  
7 - it { should belong_to(:assignee) }  
8 - end  
9 -  
10 describe "Validation" do 4 describe "Validation" do
11 it { should validate_presence_of(:target_branch) } 5 it { should validate_presence_of(:target_branch) }
12 it { should validate_presence_of(:source_branch) } 6 it { should validate_presence_of(:source_branch) }
13 - it { should validate_presence_of(:title) }  
14 - it { should validate_presence_of(:author_id) }  
15 - it { should validate_presence_of(:project_id) }  
16 - end  
17 -  
18 - describe "Scope" do  
19 - it { MergeRequest.should respond_to :closed }  
20 - it { MergeRequest.should respond_to :opened }  
21 end 7 end
22 8
23 describe 'modules' do 9 describe 'modules' do
spec/models/note_spec.rb
@@ -3,6 +3,8 @@ require 'spec_helper' @@ -3,6 +3,8 @@ require 'spec_helper'
3 describe Note do 3 describe Note do
4 describe "Associations" do 4 describe "Associations" do
5 it { should belong_to(:project) } 5 it { should belong_to(:project) }
  6 + it { should belong_to(:noteable) }
  7 + it { should belong_to(:author).class_name('User') }
6 end 8 end
7 9
8 describe "Validation" do 10 describe "Validation" do
spec/models/project_spec.rb
@@ -2,23 +2,52 @@ require 'spec_helper' @@ -2,23 +2,52 @@ require 'spec_helper'
2 2
3 describe Project do 3 describe Project do
4 describe "Associations" do 4 describe "Associations" do
  5 + it { should belong_to(:owner).class_name('User') }
5 it { should have_many(:users) } 6 it { should have_many(:users) }
6 - it { should have_many(:protected_branches).dependent(:destroy) }  
7 it { should have_many(:events).dependent(:destroy) } 7 it { should have_many(:events).dependent(:destroy) }
8 - it { should have_many(:wikis).dependent(:destroy) }  
9 it { should have_many(:merge_requests).dependent(:destroy) } 8 it { should have_many(:merge_requests).dependent(:destroy) }
10 - it { should have_many(:users_projects).dependent(:destroy) }  
11 it { should have_many(:issues).dependent(:destroy) } 9 it { should have_many(:issues).dependent(:destroy) }
  10 + it { should have_many(:milestones).dependent(:destroy) }
  11 + it { should have_many(:users_projects).dependent(:destroy) }
12 it { should have_many(:notes).dependent(:destroy) } 12 it { should have_many(:notes).dependent(:destroy) }
13 it { should have_many(:snippets).dependent(:destroy) } 13 it { should have_many(:snippets).dependent(:destroy) }
14 - it { should have_many(:hooks).dependent(:destroy) }  
15 it { should have_many(:deploy_keys).dependent(:destroy) } 14 it { should have_many(:deploy_keys).dependent(:destroy) }
  15 + it { should have_many(:hooks).dependent(:destroy) }
  16 + it { should have_many(:wikis).dependent(:destroy) }
  17 + it { should have_many(:protected_branches).dependent(:destroy) }
16 end 18 end
17 19
18 describe "Validation" do 20 describe "Validation" do
  21 + let!(:project) { create(:project) }
  22 +
19 it { should validate_presence_of(:name) } 23 it { should validate_presence_of(:name) }
  24 + it { should validate_uniqueness_of(:name) }
  25 + it { should ensure_length_of(:name).is_within(0..255) }
  26 +
20 it { should validate_presence_of(:path) } 27 it { should validate_presence_of(:path) }
  28 + it { should validate_uniqueness_of(:path) }
  29 + it { should ensure_length_of(:path).is_within(0..255) }
  30 + # TODO: Formats
  31 +
  32 + it { should ensure_length_of(:description).is_within(0..2000) }
  33 +
21 it { should validate_presence_of(:code) } 34 it { should validate_presence_of(:code) }
  35 + it { should validate_uniqueness_of(:code) }
  36 + it { should ensure_length_of(:code).is_within(1..255) }
  37 + # TODO: Formats
  38 +
  39 + it { should validate_presence_of(:owner) }
  40 +
  41 + it "should not allow new projects beyond user limits" do
  42 + project.stub(:owner).and_return(double(can_create_project?: false, projects_limit: 1))
  43 + project.should_not be_valid
  44 + project.errors[:base].first.should match(/Your own projects limit is 1/)
  45 + end
  46 +
  47 + it "should not allow 'gitolite-admin' as repo name" do
  48 + should allow_value("blah").for(:path)
  49 + should_not allow_value("gitolite-admin").for(:path)
  50 + end
22 end 51 end
23 52
24 describe "Respond to" do 53 describe "Respond to" do
@@ -73,9 +102,11 @@ describe Project do @@ -73,9 +102,11 @@ describe Project do
73 it { should respond_to(:trigger_post_receive) } 102 it { should respond_to(:trigger_post_receive) }
74 end 103 end
75 104
76 - it "should not allow 'gitolite-admin' as repo name" do  
77 - should allow_value("blah").for(:path)  
78 - should_not allow_value("gitolite-admin").for(:path) 105 + describe 'modules' do
  106 + it { should include_module(Repository) }
  107 + it { should include_module(PushObserver) }
  108 + it { should include_module(Authority) }
  109 + it { should include_module(Team) }
79 end 110 end
80 111
81 it "should return valid url to repo" do 112 it "should return valid url to repo" do
spec/models/protected_branch_spec.rb
1 require 'spec_helper' 1 require 'spec_helper'
2 2
3 describe ProtectedBranch do 3 describe ProtectedBranch do
4 - let(:project) { Factory(:project) }  
5 -  
6 describe 'Associations' do 4 describe 'Associations' do
7 it { should belong_to(:project) } 5 it { should belong_to(:project) }
8 end 6 end
@@ -13,26 +11,26 @@ describe ProtectedBranch do @@ -13,26 +11,26 @@ describe ProtectedBranch do
13 end 11 end
14 12
15 describe 'Callbacks' do 13 describe 'Callbacks' do
16 - subject { ProtectedBranch.new(project: project, name: 'branch_name') } 14 + let(:branch) { build(:protected_branch) }
17 15
18 it 'call update_repository after save' do 16 it 'call update_repository after save' do
19 - subject.should_receive(:update_repository)  
20 - subject.save 17 + branch.should_receive(:update_repository)
  18 + branch.save
21 end 19 end
22 20
23 it 'call update_repository after destroy' do 21 it 'call update_repository after destroy' do
24 - subject.should_receive(:update_repository)  
25 - subject.destroy 22 + branch.save
  23 + branch.should_receive(:update_repository)
  24 + branch.destroy
26 end 25 end
27 end 26 end
28 27
29 describe '#commit' do 28 describe '#commit' do
30 - subject { ProtectedBranch.new(project: project, name: 'cant_touch_this') } 29 + let(:branch) { create(:protected_branch) }
31 30
32 it 'commits itself to its project' do 31 it 'commits itself to its project' do
33 - project.should_receive(:commit).with('cant_touch_this')  
34 -  
35 - subject.commit 32 + branch.project.should_receive(:commit).with(branch.name)
  33 + branch.commit
36 end 34 end
37 end 35 end
38 end 36 end
spec/models/snippet_spec.rb
@@ -3,14 +3,21 @@ require 'spec_helper' @@ -3,14 +3,21 @@ require 'spec_helper'
3 describe Snippet do 3 describe Snippet do
4 describe "Associations" do 4 describe "Associations" do
5 it { should belong_to(:project) } 5 it { should belong_to(:project) }
6 - it { should belong_to(:author) } 6 + it { should belong_to(:author).class_name('User') }
  7 + it { should have_many(:notes).dependent(:destroy) }
7 end 8 end
8 9
9 describe "Validation" do 10 describe "Validation" do
10 - it { should validate_presence_of(:title) }  
11 it { should validate_presence_of(:author_id) } 11 it { should validate_presence_of(:author_id) }
12 it { should validate_presence_of(:project_id) } 12 it { should validate_presence_of(:project_id) }
  13 +
  14 + it { should validate_presence_of(:title) }
  15 + it { should ensure_length_of(:title).is_within(0..255) }
  16 +
13 it { should validate_presence_of(:file_name) } 17 it { should validate_presence_of(:file_name) }
  18 + it { should ensure_length_of(:title).is_within(0..255) }
  19 +
14 it { should validate_presence_of(:content) } 20 it { should validate_presence_of(:content) }
  21 + it { should ensure_length_of(:content).is_within(0..10_000) }
15 end 22 end
16 end 23 end
spec/models/user_spec.rb
@@ -2,13 +2,26 @@ require 'spec_helper' @@ -2,13 +2,26 @@ require 'spec_helper'
2 2
3 describe User do 3 describe User do
4 describe "Associations" do 4 describe "Associations" do
5 - it { should have_many(:projects) }  
6 it { should have_many(:users_projects).dependent(:destroy) } 5 it { should have_many(:users_projects).dependent(:destroy) }
  6 + it { should have_many(:projects) }
  7 + it { should have_many(:my_own_projects).class_name('Project') }
  8 + it { should have_many(:keys).dependent(:destroy) }
  9 + it { should have_many(:events).class_name('Event').dependent(:destroy) }
  10 + it { should have_many(:recent_events).class_name('Event') }
7 it { should have_many(:issues).dependent(:destroy) } 11 it { should have_many(:issues).dependent(:destroy) }
  12 + it { should have_many(:notes).dependent(:destroy) }
8 it { should have_many(:assigned_issues).dependent(:destroy) } 13 it { should have_many(:assigned_issues).dependent(:destroy) }
9 it { should have_many(:merge_requests).dependent(:destroy) } 14 it { should have_many(:merge_requests).dependent(:destroy) }
10 it { should have_many(:assigned_merge_requests).dependent(:destroy) } 15 it { should have_many(:assigned_merge_requests).dependent(:destroy) }
11 - it { should have_many(:notes).dependent(:destroy) } 16 + end
  17 +
  18 + describe 'validations' do
  19 + it { should validate_presence_of(:projects_limit) }
  20 + it { should validate_numericality_of(:projects_limit) }
  21 + it { should allow_value(0).for(:projects_limit) }
  22 + it { should_not allow_value(-1).for(:projects_limit) }
  23 +
  24 + it { should ensure_length_of(:bio).is_within(0..255) }
12 end 25 end
13 26
14 describe "Respond to" do 27 describe "Respond to" do
spec/models/users_project_spec.rb
@@ -7,7 +7,11 @@ describe UsersProject do @@ -7,7 +7,11 @@ describe UsersProject do
7 end 7 end
8 8
9 describe "Validation" do 9 describe "Validation" do
  10 + let!(:users_project) { create(:users_project) }
  11 +
10 it { should validate_presence_of(:user_id) } 12 it { should validate_presence_of(:user_id) }
  13 + it { should validate_uniqueness_of(:user_id).scoped_to(:project_id) }
  14 +
11 it { should validate_presence_of(:project_id) } 15 it { should validate_presence_of(:project_id) }
12 end 16 end
13 17
spec/models/wiki_spec.rb
@@ -4,10 +4,12 @@ describe Wiki do @@ -4,10 +4,12 @@ describe Wiki do
4 describe "Associations" do 4 describe "Associations" do
5 it { should belong_to(:project) } 5 it { should belong_to(:project) }
6 it { should belong_to(:user) } 6 it { should belong_to(:user) }
  7 + it { should have_many(:notes).dependent(:destroy) }
7 end 8 end
8 9
9 describe "Validation" do 10 describe "Validation" do
10 it { should validate_presence_of(:title) } 11 it { should validate_presence_of(:title) }
  12 + it { should ensure_length_of(:title).is_within(1..250) }
11 it { should validate_presence_of(:content) } 13 it { should validate_presence_of(:content) }
12 it { should validate_presence_of(:user_id) } 14 it { should validate_presence_of(:user_id) }
13 end 15 end