| 1 |
require File.dirname(__FILE__) + '/../spec_helper' |
| 2 |
|
| 3 |
describe UsersController do |
| 4 |
|
| 5 |
def create_user(options = {}) |
| 6 |
post :create, :user => { :login => 'quire', :email => 'quire@example.com', |
| 7 |
:password => 'quire', :password_confirmation => 'quire' }.merge(options) |
| 8 |
end |
| 9 |
|
| 10 |
it "should allow signups" do |
| 11 |
proc{ |
| 12 |
create_user |
| 13 |
response.should be_redirect |
| 14 |
}.should change(User, :count) |
| 15 |
end |
| 16 |
|
| 17 |
it "should require login on signup" do |
| 18 |
proc{ |
| 19 |
create_user(:login => nil) |
| 20 |
assigns(:user).errors_on(:login).should_not == nil |
| 21 |
response.should render_template("users/new") |
| 22 |
}.should_not change(User, :count) |
| 23 |
end |
| 24 |
|
| 25 |
it "should require password on signup" do |
| 26 |
proc{ |
| 27 |
create_user(:password => nil) |
| 28 |
assigns(:user).errors.on(:password).should_not be_empty |
| 29 |
response.should render_template("users/new") |
| 30 |
}.should_not change(User, :count) |
| 31 |
end |
| 32 |
|
| 33 |
it "should require password confirmation on signup" do |
| 34 |
proc { |
| 35 |
create_user(:password_confirmation => nil) |
| 36 |
assigns(:user).errors.on(:password_confirmation).should_not be_empty |
| 37 |
response.should render_template("users/new") |
| 38 |
}.should_not change(User, :count) |
| 39 |
end |
| 40 |
|
| 41 |
it "should require email on signup" do |
| 42 |
proc{ |
| 43 |
create_user(:email => nil) |
| 44 |
assigns(:user).errors.on(:email).should_not be_empty |
| 45 |
response.should render_template("users/new") |
| 46 |
}.should_not change(User, :count) |
| 47 |
end |
| 48 |
|
| 49 |
it "should be successful with valid data" do |
| 50 |
proc { |
| 51 |
create_user |
| 52 |
}.should change(User, :count) |
| 53 |
end |
| 54 |
|
| 55 |
it "requires the user to activate himself after posting valid data" do |
| 56 |
create_user |
| 57 |
User.authenticate('quire@example.com', 'quire').should == nil |
| 58 |
controller.send(:logged_in?).should == false |
| 59 |
end |
| 60 |
|
| 61 |
it "should activate user" do |
| 62 |
User.authenticate('moe', 'test').should be(nil) |
| 63 |
get :activate, :activation_code => users(:moe).activation_code |
| 64 |
response.should redirect_to('/') |
| 65 |
flash[:notice].should_not be(nil) |
| 66 |
User.authenticate('moe@example.com', 'test').should == users(:moe) |
| 67 |
end |
| 68 |
|
| 69 |
it "flashes a message when the activation code is invalid" do |
| 70 |
get :activate, :activation_code => "fubar" |
| 71 |
response.should redirect_to('/') |
| 72 |
flash[:notice].should be(nil) |
| 73 |
flash[:error].should == "Invalid activation code" |
| 74 |
User.authenticate('moe@example.com', 'test').should == nil |
| 75 |
end |
| 76 |
|
| 77 |
it "shows the user" do |
| 78 |
get :show, :id => users(:johan).login |
| 79 |
response.should be_success |
| 80 |
assigns[:user].should == users(:johan) |
| 81 |
end |
| 82 |
|
| 83 |
it "recognizes routing with dots in it" do |
| 84 |
params_from(:get, "/users/j.s")[:id].should == "j.s" |
| 85 |
end |
| 86 |
|
| 87 |
it "recognizes activate routes" do |
| 88 |
p = params_from(:get, "/users/activate/abc123") |
| 89 |
p[:controller].should == "users" |
| 90 |
p[:action].should == "activate" |
| 91 |
p[:activation_code].should == "abc123" |
| 92 |
end |
| 93 |
|
| 94 |
it "counts the number of commits in the last week" do |
| 95 |
get :show, :id => users(:johan).login |
| 96 |
response.should be_success |
| 97 |
assigns[:commits_last_week].kind_of?(Fixnum).should == true |
| 98 |
(assigns[:commits_last_week] >= 0).should == true |
| 99 |
end |
| 100 |
|
| 101 |
it "#show sets atom feed autodiscovery" do |
| 102 |
user = users(:johan) |
| 103 |
get :show, :id => user.login |
| 104 |
assigns[:atom_auto_discovery_url].should == formatted_feed_user_path(user, :atom) |
| 105 |
end |
| 106 |
|
| 107 |
it "has an atom feed" do |
| 108 |
user = users(:johan) |
| 109 |
get :feed, :id => user.login, :format => "atom" |
| 110 |
response.should be_success |
| 111 |
assigns[:user].should == user |
| 112 |
assigns[:events].should == user.events.find(:all, :limit => 30, :order => "created_at desc") |
| 113 |
end |
| 114 |
|
| 115 |
describe "#forgot_password" do |
| 116 |
it "GETs the page fine for everyone" do |
| 117 |
get :forgot_password |
| 118 |
response.should be_success |
| 119 |
response.should render_template("forgot_password") |
| 120 |
end |
| 121 |
end |
| 122 |
|
| 123 |
describe "#reset_password" do |
| 124 |
it "redirects to forgot_password if nothing was found" do |
| 125 |
post :reset_password, :user => {:email => "xxx"} |
| 126 |
response.should redirect_to(forgot_password_users_path) |
| 127 |
flash[:error].should match(/invalid email/i) |
| 128 |
end |
| 129 |
|
| 130 |
it "sends a new password if email was found" do |
| 131 |
u = users(:johan) |
| 132 |
User.should_receive(:generate_random_password).and_return("secret") |
| 133 |
Mailer.should_receive(:deliver_forgotten_password).with(u, "secret") |
| 134 |
post :reset_password, :user => {:email => u.email} |
| 135 |
response.should redirect_to(root_path) |
| 136 |
flash[:notice].should == "A new password has been sent to your email" |
| 137 |
|
| 138 |
User.authenticate(u.email, "secret").should_not be_nil |
| 139 |
end |
| 140 |
end |
| 141 |
end |