| 1 |
require File.dirname(__FILE__) + '/../spec_helper' |
| 2 |
|
| 3 |
describe Project do |
| 4 |
|
| 5 |
def create_project(options={}) |
| 6 |
Project.new({ |
| 7 |
:title => "foo project", |
| 8 |
:slug => "foo", |
| 9 |
:description => "my little project", |
| 10 |
:user => users(:johan) |
| 11 |
}.merge(options)) |
| 12 |
end |
| 13 |
|
| 14 |
it "should have valid associations" do |
| 15 |
create_project.should have_valid_associations |
| 16 |
end |
| 17 |
|
| 18 |
it "should have a title to be valid" do |
| 19 |
project = create_project(:title => nil) |
| 20 |
project.should_not be_valid |
| 21 |
project.title = "foo" |
| 22 |
project.should be_valid |
| 23 |
end |
| 24 |
|
| 25 |
it "should have a slug to be valid" do |
| 26 |
project = create_project(:slug => nil) |
| 27 |
project.should_not be_valid |
| 28 |
end |
| 29 |
|
| 30 |
it "should have a unique slug to be valid" do |
| 31 |
p1 = create_project |
| 32 |
p1.save! |
| 33 |
p2 = create_project(:slug => "FOO") |
| 34 |
p2.should_not be_valid |
| 35 |
p2.should have(1).error_on(:slug) |
| 36 |
end |
| 37 |
|
| 38 |
it "should have an alphanumeric slug" do |
| 39 |
project = create_project(:slug => "asd asd") |
| 40 |
project.valid? |
| 41 |
project.should_not be_valid |
| 42 |
end |
| 43 |
|
| 44 |
it "should downcase the slug before validation" do |
| 45 |
project = create_project(:slug => "FOO") |
| 46 |
project.valid? |
| 47 |
project.slug.should == "foo" |
| 48 |
end |
| 49 |
|
| 50 |
it "creates an initial repository for itself" do |
| 51 |
project = create_project |
| 52 |
project.save |
| 53 |
project.repositories.should_not == [] |
| 54 |
project.repositories.first.name.should == "mainline" |
| 55 |
project.repositories.first.user.should == project.user |
| 56 |
project.user.can_write_to?(project.repositories.first).should == true |
| 57 |
end |
| 58 |
|
| 59 |
it "finds a project by slug or raises" do |
| 60 |
Project.find_by_slug!(projects(:johans).slug).should == projects(:johans) |
| 61 |
proc{ |
| 62 |
Project.find_by_slug!("asdasdasd") |
| 63 |
}.should raise_error(ActiveRecord::RecordNotFound) |
| 64 |
end |
| 65 |
|
| 66 |
it "has the slug as its params" do |
| 67 |
projects(:johans).to_param.should == projects(:johans).slug |
| 68 |
end |
| 69 |
|
| 70 |
it "knows if a user is a admin on a project" do |
| 71 |
projects(:johans).admin?(users(:johan)).should == true |
| 72 |
projects(:johans).admin?(users(:moe)).should == false |
| 73 |
projects(:johans).admin?(:false).should == false |
| 74 |
end |
| 75 |
|
| 76 |
it "knows if a user can delete the project" do |
| 77 |
project = projects(:johans) |
| 78 |
project.can_be_deleted_by?(users(:moe)).should == false |
| 79 |
project.can_be_deleted_by?(users(:johan)).should == false |
| 80 |
project.repositories.last.destroy |
| 81 |
project.reload.can_be_deleted_by?(users(:johan)).should == true |
| 82 |
end |
| 83 |
|
| 84 |
it "should strip html tags" do |
| 85 |
project = create_project(:description => "<h1>Project A</h1>\n<b>Project A</b> is a....") |
| 86 |
project.stripped_description.should == "Project A\nProject A is a...." |
| 87 |
end |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
it "should have valid urls ( prepending http:// if needed )" do |
| 95 |
project = projects(:johans) |
| 96 |
[ :home_url, :mailinglist_url, :bugtracker_url ].each do |attr| |
| 97 |
project.should be_valid |
| 98 |
project.send("#{attr}=", 'http://blah.com') |
| 99 |
project.should be_valid |
| 100 |
project.send("#{attr}=", 'ftp://blah.com') |
| 101 |
project.should_not be_valid |
| 102 |
project.send("#{attr}=", 'blah.com') |
| 103 |
project.should be_valid |
| 104 |
project.send(attr).should == 'http://blah.com' |
| 105 |
end |
| 106 |
end |
| 107 |
|
| 108 |
it "should not prepend http:// to empty urls" do |
| 109 |
project = projects(:johans) |
| 110 |
[ :home_url, :mailinglist_url, :bugtracker_url ].each do |attr| |
| 111 |
project.send("#{attr}=", '') |
| 112 |
project.send(attr).should be_blank |
| 113 |
project.send("#{attr}=", nil) |
| 114 |
project.send(attr).should be_blank |
| 115 |
end |
| 116 |
end |
| 117 |
|
| 118 |
describe "Project events" do |
| 119 |
before(:each) do |
| 120 |
@project = projects(:johans) |
| 121 |
@user = users(:johan) |
| 122 |
@repository = @project.repositories.first |
| 123 |
end |
| 124 |
|
| 125 |
it "should create an event from the action name" do |
| 126 |
@project.create_event(Action::CREATE_PROJECT, @repository, @user, "", "").should_not == nil |
| 127 |
end |
| 128 |
|
| 129 |
it "should create an event even without a valid id" do |
| 130 |
@project.create_event(52342, @repository, @user).should_not == nil |
| 131 |
end |
| 132 |
|
| 133 |
it "creates valid attributes on the event" do |
| 134 |
e = @project.create_event(Action::COMMIT, @repository, @user, "somedata", "a body") |
| 135 |
e.should be_valid |
| 136 |
e.new_record?.should == false |
| 137 |
e.reload |
| 138 |
e.action.should == Action::COMMIT |
| 139 |
e.target.should == @repository |
| 140 |
e.project.should == @project |
| 141 |
e.user.should == @user |
| 142 |
e.data.should == "somedata" |
| 143 |
e.body.should == "a body" |
| 144 |
end |
| 145 |
end |
| 146 |
|
| 147 |
end |