Commit 5b0a9989 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge pull request #1333 from tsigo/model_specs

Increased model spec coverage
parents 8c44789e 97423a0b
...@@ -18,11 +18,15 @@ FactoryGirl.define do ...@@ -18,11 +18,15 @@ FactoryGirl.define do
Faker::Lorem.sentence Faker::Lorem.sentence
end end
sequence :name, aliases: [:file_name] do
Faker::Name.name
end
sequence(:url) { Faker::Internet.uri('http') } sequence(:url) { Faker::Internet.uri('http') }
factory :user, aliases: [:author, :assignee, :owner] do factory :user, aliases: [:author, :assignee, :owner] do
email { Faker::Internet.email } email { Faker::Internet.email }
name { Faker::Name.name } name
password "123456" password "123456"
password_confirmation "123456" password_confirmation "123456"
...@@ -116,6 +120,11 @@ FactoryGirl.define do ...@@ -116,6 +120,11 @@ FactoryGirl.define do
author author
title title
content content
file_name { Faker::Lorem.sentence } file_name
end
factory :protected_branch do
name
project
end end
end end
# == Schema Information
#
# Table name: events
#
# id :integer(4) not null, primary key
# target_type :string(255)
# target_id :integer(4)
# title :string(255)
# data :text
# project_id :integer(4)
# created_at :datetime not null
# updated_at :datetime not null
# action :integer(4)
# author_id :integer(4)
#
require 'spec_helper' require 'spec_helper'
describe Event do describe Event do
describe "Associations" do describe "Associations" do
it { should belong_to(:project) } it { should belong_to(:project) }
it { should belong_to(:target) }
end end
describe "Respond to" do describe "Respond to" do
...@@ -29,16 +14,6 @@ describe Event do ...@@ -29,16 +14,6 @@ describe Event do
it { should respond_to(:commits) } it { should respond_to(:commits) }
end end
describe "Creation" do
before do
@event = Factory :event
end
it "should create a valid event" do
@event.should be_valid
end
end
describe "Push event" do describe "Push event" do
before do before do
project = Factory :project project = Factory :project
......
...@@ -2,21 +2,16 @@ require 'spec_helper' ...@@ -2,21 +2,16 @@ require 'spec_helper'
describe Issue do describe Issue do
describe "Associations" do describe "Associations" do
it { should belong_to(:project) }
it { should belong_to(:author) }
it { should belong_to(:assignee) }
it { should belong_to(:milestone) } it { should belong_to(:milestone) }
end end
describe "Validation" do describe "Validation" do
it { should validate_presence_of(:title) } it { should ensure_length_of(:description).is_within(0..2000) }
it { should validate_presence_of(:author_id) }
it { should validate_presence_of(:project_id) }
end end
describe "Scope" do describe 'modules' do
it { Issue.should respond_to :closed } it { should include_module(IssueCommonality) }
it { Issue.should respond_to :opened } it { should include_module(Upvote) }
end end
subject { Factory.create(:issue) } subject { Factory.create(:issue) }
...@@ -61,57 +56,4 @@ describe Issue do ...@@ -61,57 +56,4 @@ describe Issue do
subject.is_being_reopened?.should be_false subject.is_being_reopened?.should be_false
end end
end end
describe "plus 1" do
subject { Factory.create(:issue) }
it "with no notes has a 0/0 score" do
subject.upvotes.should == 0
end
it "should recognize non-+1 notes" do
subject.notes << Factory(:note, note: "No +1 here")
subject.should have(1).note
subject.notes.first.upvote?.should be_false
subject.upvotes.should == 0
end
it "should recognize a single +1 note" do
subject.notes << Factory(:note, note: "+1 This is awesome")
subject.upvotes.should == 1
end
it "should recognize a multiple +1 notes" do
subject.notes << Factory(:note, note: "+1 This is awesome")
subject.notes << Factory(:note, note: "+1 I want this")
subject.upvotes.should == 2
end
end
describe ".search" do
let!(:issue) { Factory.create(:issue, title: "Searchable issue") }
it "matches by title" do
Issue.search('able').all.should == [issue]
end
end
end end
# == Schema Information
#
# Table name: issues
#
# id :integer(4) not null, primary key
# title :string(255)
# assignee_id :integer(4)
# author_id :integer(4)
# project_id :integer(4)
# created_at :datetime not null
# updated_at :datetime not null
# closed :boolean(1) default(FALSE), not null
# position :integer(4) default(0)
# critical :boolean(1) default(FALSE), not null
# branch_name :string(255)
# description :text
# milestone_id :integer(4)
#
...@@ -2,12 +2,15 @@ require 'spec_helper' ...@@ -2,12 +2,15 @@ require 'spec_helper'
describe Key do describe Key do
describe "Associations" do describe "Associations" do
it { should belong_to(:user) or belong_to(:project) } it { should belong_to(:user) }
it { should belong_to(:project) }
end end
describe "Validation" do describe "Validation" do
it { should validate_presence_of(:title) } it { should validate_presence_of(:title) }
it { should validate_presence_of(:key) } it { should validate_presence_of(:key) }
it { should ensure_length_of(:title).is_within(0..255) }
it { should ensure_length_of(:key).is_within(0..5000) }
end end
describe "Methods" do describe "Methods" do
...@@ -44,17 +47,3 @@ describe Key do ...@@ -44,17 +47,3 @@ describe Key do
end end
end end
end end
# == Schema Information
#
# Table name: keys
#
# id :integer(4) not null, primary key
# user_id :integer(4)
# created_at :datetime not null
# updated_at :datetime not null
# key :text
# title :string(255)
# identifier :string(255)
# project_id :integer(4)
#
require 'spec_helper' require 'spec_helper'
describe MergeRequest do describe MergeRequest do
describe "Associations" do
it { should belong_to(:project) }
it { should belong_to(:author) }
it { should belong_to(:assignee) }
end
describe "Validation" do describe "Validation" do
it { should validate_presence_of(:target_branch) } it { should validate_presence_of(:target_branch) }
it { should validate_presence_of(:source_branch) } it { should validate_presence_of(:source_branch) }
it { should validate_presence_of(:title) }
it { should validate_presence_of(:author_id) }
it { should validate_presence_of(:project_id) }
end
describe "Scope" do
it { MergeRequest.should respond_to :closed }
it { MergeRequest.should respond_to :opened }
end
describe "plus 1" do
subject { Factory.create(:merge_request) }
it "with no notes has a 0/0 score" do
subject.upvotes.should == 0
end
it "should recognize non-+1 notes" do
subject.notes << Factory(:note, note: "No +1 here")
subject.should have(1).note
subject.notes.first.upvote?.should be_false
subject.upvotes.should == 0
end
it "should recognize a single +1 note" do
subject.notes << Factory(:note, note: "+1 This is awesome")
subject.upvotes.should == 1
end
it "should recognize a multiple +1 notes" do
subject.notes << Factory(:note, note: "+1 This is awesome")
subject.notes << Factory(:note, note: "+1 I want this")
subject.upvotes.should == 2
end
end end
describe ".search" do describe 'modules' do
let!(:issue) { Factory.create(:issue, title: "Searchable issue") } it { should include_module(IssueCommonality) }
it { should include_module(Upvote) }
it "matches by title" do
Issue.search('able').all.should == [issue]
end
end end
end end
# == Schema Information
#
# Table name: merge_requests
#
# id :integer(4) not null, primary key
# target_branch :string(255) not null
# source_branch :string(255) not null
# project_id :integer(4) not null
# author_id :integer(4)
# assignee_id :integer(4)
# title :string(255)
# closed :boolean(1) default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
# st_commits :text(2147483647
# st_diffs :text(2147483647
# merged :boolean(1) default(FALSE), not null
# state :integer(4) default(1), not null
#
# == Schema Information
#
# Table name: milestones
#
# id :integer(4) not null, primary key
# title :string(255) not null
# project_id :integer(4) not null
# description :text
# due_date :date
# closed :boolean(1) default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
#
require 'spec_helper' require 'spec_helper'
describe Milestone do describe Milestone do
......
...@@ -3,6 +3,8 @@ require 'spec_helper' ...@@ -3,6 +3,8 @@ require 'spec_helper'
describe Note do describe Note do
describe "Associations" do describe "Associations" do
it { should belong_to(:project) } it { should belong_to(:project) }
it { should belong_to(:noteable) }
it { should belong_to(:author).class_name('User') }
end end
describe "Validation" do describe "Validation" do
...@@ -130,19 +132,3 @@ describe Note do ...@@ -130,19 +132,3 @@ describe Note do
end end
end end
end end
# == Schema Information
#
# Table name: notes
#
# id :integer(4) not null, primary key
# note :text
# noteable_id :string(255)
# noteable_type :string(255)
# author_id :integer(4)
# created_at :datetime not null
# updated_at :datetime not null
# project_id :integer(4)
# attachment :string(255)
# line_code :string(255)
#
...@@ -2,23 +2,52 @@ require 'spec_helper' ...@@ -2,23 +2,52 @@ require 'spec_helper'
describe Project do describe Project do
describe "Associations" do describe "Associations" do
it { should belong_to(:owner).class_name('User') }
it { should have_many(:users) } it { should have_many(:users) }
it { should have_many(:protected_branches).dependent(:destroy) }
it { should have_many(:events).dependent(:destroy) } it { should have_many(:events).dependent(:destroy) }
it { should have_many(:wikis).dependent(:destroy) }
it { should have_many(:merge_requests).dependent(:destroy) } it { should have_many(:merge_requests).dependent(:destroy) }
it { should have_many(:users_projects).dependent(:destroy) }
it { should have_many(:issues).dependent(:destroy) } it { should have_many(:issues).dependent(:destroy) }
it { should have_many(:milestones).dependent(:destroy) }
it { should have_many(:users_projects).dependent(:destroy) }
it { should have_many(:notes).dependent(:destroy) } it { should have_many(:notes).dependent(:destroy) }
it { should have_many(:snippets).dependent(:destroy) } it { should have_many(:snippets).dependent(:destroy) }
it { should have_many(:hooks).dependent(:destroy) }
it { should have_many(:deploy_keys).dependent(:destroy) } it { should have_many(:deploy_keys).dependent(:destroy) }
it { should have_many(:hooks).dependent(:destroy) }
it { should have_many(:wikis).dependent(:destroy) }
it { should have_many(:protected_branches).dependent(:destroy) }
end end
describe "Validation" do describe "Validation" do
let!(:project) { create(:project) }
it { should validate_presence_of(:name) } it { should validate_presence_of(:name) }
it { should validate_uniqueness_of(:name) }
it { should ensure_length_of(:name).is_within(0..255) }
it { should validate_presence_of(:path) } it { should validate_presence_of(:path) }
it { should validate_uniqueness_of(:path) }
it { should ensure_length_of(:path).is_within(0..255) }
# TODO: Formats
it { should ensure_length_of(:description).is_within(0..2000) }
it { should validate_presence_of(:code) } it { should validate_presence_of(:code) }
it { should validate_uniqueness_of(:code) }
it { should ensure_length_of(:code).is_within(1..255) }
# TODO: Formats
it { should validate_presence_of(:owner) }
it "should not allow new projects beyond user limits" do
project.stub(:owner).and_return(double(can_create_project?: false, projects_limit: 1))
project.should_not be_valid
project.errors[:base].first.should match(/Your own projects limit is 1/)
end
it "should not allow 'gitolite-admin' as repo name" do
should allow_value("blah").for(:path)
should_not allow_value("gitolite-admin").for(:path)
end
end end
describe "Respond to" do describe "Respond to" do
...@@ -73,9 +102,11 @@ describe Project do ...@@ -73,9 +102,11 @@ describe Project do
it { should respond_to(:trigger_post_receive) } it { should respond_to(:trigger_post_receive) }
end end
it "should not allow 'gitolite-admin' as repo name" do describe 'modules' do
should allow_value("blah").for(:path) it { should include_module(Repository) }
should_not allow_value("gitolite-admin").for(:path) it { should include_module(PushObserver) }
it { should include_module(Authority) }
it { should include_module(Team) }
end end
it "should return valid url to repo" do it "should return valid url to repo" do
...@@ -236,23 +267,3 @@ describe Project do ...@@ -236,23 +267,3 @@ describe Project do
end end
end end
end end
# == Schema Information
#
# Table name: projects
#
# id :integer(4) not null, primary key
# name :string(255)
# path :string(255)
# description :text
# created_at :datetime not null
# updated_at :datetime not null
# private_flag :boolean(1) default(TRUE), not null
# code :string(255)
# owner_id :integer(4)
# default_branch :string(255) default("master"), not null
# issues_enabled :boolean(1) default(TRUE), not null
# wall_enabled :boolean(1) default(TRUE), not null
# merge_requests_enabled :boolean(1) default(TRUE), not null
# wiki_enabled :boolean(1) default(TRUE), not null
#
# == Schema Information
#
# Table name: protected_branches
#
# id :integer(4) not null, primary key
# project_id :integer(4) not null
# name :string(255) not null
# created_at :datetime not null
# updated_at :datetime not null
#
require 'spec_helper' require 'spec_helper'
describe ProtectedBranch do describe ProtectedBranch do
let(:project) { Factory(:project) }
describe 'Associations' do describe 'Associations' do
it { should belong_to(:project) } it { should belong_to(:project) }
end end
...@@ -24,26 +11,26 @@ describe ProtectedBranch do ...@@ -24,26 +11,26 @@ describe ProtectedBranch do
end end
describe 'Callbacks' do describe 'Callbacks' do
subject { ProtectedBranch.new(project: project, name: 'branch_name') } let(:branch) { build(:protected_branch) }
it 'call update_repository after save' do it 'call update_repository after save' do
subject.should_receive(:update_repository) branch.should_receive(:update_repository)
subject.save branch.save
end end
it 'call update_repository after destroy' do it 'call update_repository after destroy' do
subject.should_receive(:update_repository) branch.save
subject.destroy branch.should_receive(:update_repository)
branch.destroy
end end
end end
describe '#commit' do describe '#commit' do
subject { ProtectedBranch.new(project: project, name: 'cant_touch_this') } let(:branch) { create(:protected_branch) }
it 'commits itself to its project' do it 'commits itself to its project' do
project.should_receive(:commit).with('cant_touch_this') branch.project.should_receive(:commit).with(branch.name)
branch.commit
subject.commit
end end
end end
end end
...@@ -3,29 +3,21 @@ require 'spec_helper' ...@@ -3,29 +3,21 @@ require 'spec_helper'
describe Snippet do describe Snippet do
describe "Associations" do describe "Associations" do
it { should belong_to(:project) } it { should belong_to(:project) }
it { should belong_to(:author) } it { should belong_to(:author).class_name('User') }
it { should have_many(:notes).dependent(:destroy) }
end end
describe "Validation" do describe "Validation" do
it { should validate_presence_of(:title) }
it { should validate_presence_of(:author_id) } it { should validate_presence_of(:author_id) }
it { should validate_presence_of(:project_id) } it { should validate_presence_of(:project_id) }
it { should validate_presence_of(:title) }
it { should ensure_length_of(:title).is_within(0..255) }
it { should validate_presence_of(:file_name) } it { should validate_presence_of(:file_name) }
it { should ensure_length_of(:title).is_within(0..255) }
it { should validate_presence_of(:content) } it { should validate_presence_of(:content) }
it { should ensure_length_of(:content).is_within(0..10_000) }
end end
end end
# == Schema Information
#
# Table name: snippets
#
# id :integer(4) not null, primary key
# title :string(255)
# content :text
# author_id :integer(4) not null
# project_id :integer(4) not null
# created_at :datetime not null
# updated_at :datetime not null
# file_name :string(255)
# expires_at :datetime
#
...@@ -2,13 +2,26 @@ require 'spec_helper' ...@@ -2,13 +2,26 @@ require 'spec_helper'
describe User do describe User do
describe "Associations" do describe "Associations" do
it { should have_many(:projects) }
it { should have_many(:users_projects).dependent(:destroy) } it { should have_many(:users_projects).dependent(:destroy) }
it { should have_many(:projects) }
it { should have_many(:my_own_projects).class_name('Project') }
it { should have_many(:keys).dependent(:destroy) }
it { should have_many(:events).class_name('Event').dependent(:destroy) }
it { should have_many(:recent_events).class_name('Event') }
it { should have_many(:issues).dependent(:destroy) } it { should have_many(:issues).dependent(:destroy) }
it { should have_many(:notes).dependent(:destroy) }
it { should have_many(:assigned_issues).dependent(:destroy) } it { should have_many(:assigned_issues).dependent(:destroy) }
it { should have_many(:merge_requests).dependent(:destroy) } it { should have_many(:merge_requests).dependent(:destroy) }
it { should have_many(:assigned_merge_requests).dependent(:destroy) } it { should have_many(:assigned_merge_requests).dependent(:destroy) }
it { should have_many(:notes).dependent(:destroy) } end
describe 'validations' do
it { should validate_presence_of(:projects_limit) }
it { should validate_numericality_of(:projects_limit) }
it { should allow_value(0).for(:projects_limit) }
it { should_not allow_value(-1).for(:projects_limit) }
it { should ensure_length_of(:bio).is_within(0..255) }
end end
describe "Respond to" do describe "Respond to" do
...@@ -51,33 +64,3 @@ describe User do ...@@ -51,33 +64,3 @@ describe User do
user.authentication_token.should_not == "" user.authentication_token.should_not == ""
end end
end end
# == Schema Information
#
# Table name: users
#
# id :integer(4) not null, primary key
# email :string(255) default(""), not null
# encrypted_password :string(128) default(""), not null
# reset_password_token :string(255)
# reset_password_sent_at :datetime
# remember_created_at :datetime
# sign_in_count :integer(4) default(0)
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
# created_at :datetime not null
# updated_at :datetime not null
# name :string(255)
# admin :boolean(1) default(FALSE), not null
# projects_limit :integer(4) default(10)
# skype :string(255) default(""), not null
# linkedin :string(255) default(""), not null
# twitter :string(255) default(""), not null
# authentication_token :string(255)
# dark_scheme :boolean(1) default(FALSE), not null
# theme_id :integer(4) default(1), not null
# bio :string(255)
# blocked :boolean(1) default(FALSE), not null
#
...@@ -7,7 +7,11 @@ describe UsersProject do ...@@ -7,7 +7,11 @@ describe UsersProject do
end end
describe "Validation" do describe "Validation" do
let!(:users_project) { create(:users_project) }
it { should validate_presence_of(:user_id) } it { should validate_presence_of(:user_id) }
it { should validate_uniqueness_of(:user_id).scoped_to(:project_id) }
it { should validate_presence_of(:project_id) } it { should validate_presence_of(:project_id) }
end end
...@@ -16,15 +20,3 @@ describe UsersProject do ...@@ -16,15 +20,3 @@ describe UsersProject do
it { should respond_to(:user_email) } it { should respond_to(:user_email) }
end end
end end
# == Schema Information
#
# Table name: users_projects
#
# id :integer(4) not null, primary key
# user_id :integer(4) not null
# project_id :integer(4) not null
# created_at :datetime not null
# updated_at :datetime not null
# project_access :integer(4) default(0), not null
#
...@@ -52,14 +52,3 @@ describe ProjectHook do ...@@ -52,14 +52,3 @@ describe ProjectHook do
end end
end end
end end
# == Schema Information
#
# Table name: web_hooks
#
# id :integer(4) not null, primary key
# url :string(255)
# project_id :integer(4)
# created_at :datetime not null
# updated_at :datetime not null
#
...@@ -4,27 +4,13 @@ describe Wiki do ...@@ -4,27 +4,13 @@ describe Wiki do
describe "Associations" do describe "Associations" do
it { should belong_to(:project) } it { should belong_to(:project) }
it { should belong_to(:user) } it { should belong_to(:user) }
it { should have_many(:notes).dependent(:destroy) }
end end
describe "Validation" do describe "Validation" do
it { should validate_presence_of(:title) } it { should validate_presence_of(:title) }
it { should ensure_length_of(:title).is_within(1..250) }
it { should validate_presence_of(:content) } it { should validate_presence_of(:content) }
it { should validate_presence_of(:user_id) } it { should validate_presence_of(:user_id) }
end end
it { Factory(:wiki).should be_valid }
end end
# == Schema Information
#
# Table name: wikis
#
# id :integer(4) not null, primary key
# title :string(255)
# content :text
# project_id :integer(4)
# created_at :datetime not null
# updated_at :datetime not null
# slug :string(255)
# user_id :integer(4)
#
require 'spec_helper'
describe Issue, "IssueCommonality" do
let(:issue) { create(:issue) }
describe "Associations" do
it { should belong_to(:project) }
it { should belong_to(:author) }
it { should belong_to(:assignee) }
it { should have_many(:notes).dependent(:destroy) }
end
describe "Validation" do
it { should validate_presence_of(:project_id) }
it { should validate_presence_of(:author_id) }
it { should validate_presence_of(:title) }
it { should ensure_length_of(:title).is_at_least(0).is_at_most(255) }
end
describe "Scope" do
it { described_class.should respond_to(:opened) }
it { described_class.should respond_to(:closed) }
it { described_class.should respond_to(:assigned) }
end
it "has an :author_id_of_changes accessor" do
issue.should respond_to(:author_id_of_changes)
issue.should respond_to(:author_id_of_changes=)
end
describe ".search" do
let!(:searchable_issue) { create(:issue, title: "Searchable issue") }
it "matches by title" do
described_class.search('able').all.should == [searchable_issue]
end
end
describe "#today?" do
it "returns true when created today" do
# Avoid timezone differences and just return exactly what we want
Date.stub(:today).and_return(issue.created_at.to_date)
issue.today?.should be_true
end
it "returns false when not created today" do
Date.stub(:today).and_return(Date.yesterday)
issue.today?.should be_false
end
end
describe "#new?" do
it "returns true when created today and record hasn't been updated" do
issue.stub(:today?).and_return(true)
issue.new?.should be_true
end
it "returns false when not created today" do
issue.stub(:today?).and_return(false)
issue.new?.should be_false
end
it "returns false when record has been updated" do
issue.stub(:today?).and_return(true)
issue.touch
issue.new?.should be_false
end
end
end
require 'spec_helper'
describe Issue, "Upvote" do
let(:issue) { create(:issue) }
it "with no notes has a 0/0 score" do
issue.upvotes.should == 0
end
it "should recognize non-+1 notes" do
issue.notes << create(:note, note: "No +1 here")
issue.should have(1).note
issue.notes.first.upvote?.should be_false
issue.upvotes.should == 0
end
it "should recognize a single +1 note" do
issue.notes << create(:note, note: "+1 This is awesome")
issue.upvotes.should == 1
end
it "should recognize multiple +1 notes" do
issue.notes << create(:note, note: "+1 This is awesome")
issue.notes << create(:note, note: "+1 I want this")
issue.upvotes.should == 2
end
end
...@@ -28,6 +28,16 @@ RSpec::Matchers.define :be_404_for do |user| ...@@ -28,6 +28,16 @@ RSpec::Matchers.define :be_404_for do |user|
end end
end end
RSpec::Matchers.define :include_module do |expected|
match do
described_class.included_modules.include?(expected)
end
failure_message_for_should do
"expected #{described_class} to include the #{expected} module"
end
end
module UrlAccess module UrlAccess
def url_allowed?(user, url) def url_allowed?(user, url)
emulate_user(user) emulate_user(user)
...@@ -57,3 +67,17 @@ module UrlAccess ...@@ -57,3 +67,17 @@ module UrlAccess
login_with(user) if user login_with(user) if user
end end
end end
# Extend shoulda-matchers
module Shoulda::Matchers::ActiveModel
class EnsureLengthOfMatcher
# Shortcut for is_at_least and is_at_most
def is_within(range)
if range.exclude_end?
is_at_least(range.first) && is_at_most(range.last - 1)
else
is_at_least(range.first) && is_at_most(range.last)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment