1 require File.join( File.dirname(__FILE__), "..", "spec_helper" )
2 require File.join( File.dirname(__FILE__), "..", "user_spec_helper")
3 require File.join( File.dirname(__FILE__), "..", "authenticated_system_spec_helper")
9 User.clear_database_table
10 UserMailer.stub!(:activation_notification).and_return(true)
13 it "should have a login field" do
15 user.should respond_to(:login)
17 user.errors.on(:login).should_not be_nil
20 it "should fail login if there are less than 3 chars" do
24 user.errors.on(:login).should_not be_nil
27 it "should not fail login with between 3 and 40 chars" do
30 user.login = "a" * num
32 user.errors.on(:login).should be_nil
36 it "should fail login with over 90 chars" do
40 user.errors.on(:login).should_not be_nil
43 it "should make a valid user" do
44 user = User.new(valid_user_hash)
46 user.errors.should be_empty
50 it "should make sure login is unique" do
51 user = User.new( valid_user_hash.with(:login => "Daniel") )
52 user2 = User.new( valid_user_hash.with(:login => "Daniel"))
53 user.save.should be_true
55 # see http://wm.lighthouseapp.com/projects/4819/tickets/272-invalid-resources-still-save
56 #user2.save.should be_false
57 user2.errors.on(:login).should_not be_nil
60 it "should make sure login is unique regardless of case" do
61 User.first(:login => "Daniel").should be_nil
62 user = User.new( valid_user_hash.with(:login => "Daniel") )
63 user2 = User.new( valid_user_hash.with(:login => "daniel"))
64 user.save.should be_true
66 # see http://wm.lighthouseapp.com/projects/4819/tickets/272-invalid-resources-still-save
67 #user2.save.should be_false
68 user2.errors.on(:login).should_not be_nil
71 it "should downcase logins" do
72 user = User.new( valid_user_hash.with(:login => "DaNieL"))
73 user.login.should == "daniel"
76 it "should authenticate a user using a class method" do
77 user = User.new(valid_user_hash)
80 User.authenticate(valid_user_hash[:login], valid_user_hash[:password]).should_not be_nil
83 it "should not authenticate a user using the wrong password" do
84 user = User.new(valid_user_hash)
88 User.authenticate(valid_user_hash[:login], "not_the_password").should be_nil
91 it "should not authenticate a user using the wrong login" do
92 user = User.create(valid_user_hash)
95 User.authenticate("not_the_login", valid_user_hash[:password]).should be_nil
98 it "should not authenticate a user that does not exist" do
99 User.authenticate("i_dont_exist", "password").should be_nil
103 it "should send a please activate email" do
104 user = User.new(valid_user_hash)
105 UserMailer.should_receive(:dispatch_and_deliver) do |action, mail_args, mailer_params|
106 action.should == :signup_notification
107 [:from, :to, :subject].each{ |f| mail_args.keys.should include(f)}
108 mail_args[:to].should == user.email
109 mailer_params[:user].should == user
114 it "should not send a please activate email when updating" do
115 user = User.new(valid_user_hash)
117 UserMailer.should_not_receive(:signup_notification)
118 user.login = "not in the valid hash for login"
124 describe User, "the password fields for User" do
125 include UserSpecHelper
128 User.clear_database_table
129 @user = User.new( valid_user_hash )
130 UserMailer.stub!(:activation_notification).and_return(true)
133 it "should respond to password" do
134 @user.should respond_to(:password)
137 it "should respond to password_confirmation" do
138 @user.should respond_to(:password_confirmation)
141 #it "should have a protected password_required method" do
142 # @user.protected_methods.should include("password_required?")
145 it "should respond to crypted_password" do
146 @user.should respond_to(:crypted_password)
149 it "should require password if password is required" do
150 user = User.new( valid_user_hash.without(:password))
151 user.stub!(:password_required?).and_return(true)
153 user.errors.on(:password).should_not be_nil
154 user.errors.on(:password).should_not be_empty
157 it "should set the salt" do
158 user = User.new(valid_user_hash)
159 user.salt.should be_nil
160 user.send(:encrypt_password)
161 user.salt.should_not be_nil
164 it "should require the password on create" do
165 user = User.new(valid_user_hash.without(:password))
167 user.errors.on(:password).should_not be_nil
168 user.errors.on(:password).should_not be_empty
171 it "should require password_confirmation if the password_required?" do
172 user = User.new(valid_user_hash.without(:password_confirmation))
174 (user.errors.on(:password) || user.errors.on(:password_confirmation)).should_not be_nil
177 it "should fail when password is outside 4 and 40 chars" do
179 user = User.new(valid_user_hash.with(:password => ("a" * num)))
181 user.errors.on(:password).should_not be_nil
185 it "should pass when password is within 4 and 40 chars" do
186 [4,30,40].each do |num|
187 user = User.new(valid_user_hash.with(:password => ("a" * num), :password_confirmation => ("a" * num)))
189 user.errors.on(:password).should be_nil
193 it "should authenticate against a password" do
194 user = User.new(valid_user_hash)
196 user.should be_authenticated(valid_user_hash[:password])
199 it "should not require a password when saving an existing user" do
200 user = User.create(valid_user_hash)
201 user = User.first(:login => valid_user_hash[:login])
202 user.password.should be_nil
203 user.password_confirmation.should be_nil
204 user.login = "some_different_login_to_allow_saving"
205 (user.save).should be_true
210 describe User, "activation" do
211 include UserSpecHelper
215 User.clear_database_table
216 @user = User.new(valid_user_hash)
219 it "should have an activation_code as an attribute" do
220 @user.attributes.keys.any?{|a| a.to_s == "activation_code"}.should_not be_nil
223 it "should create an activation code on create" do
224 @user.activation_code.should be_nil
226 @user.activation_code.should_not be_nil
229 it "should not be active when created" do
230 @user.should_not be_activated
232 @user.should_not be_activated
235 it "should respond to activate" do
236 @user.should respond_to(:activate)
239 it "should activate a user when activate is called" do
240 @user.should_not be_activated
243 @user.should be_activated
245 #User.first(:login => valid_user_hash[:login]).should be_activated
248 it "should should show recently activated when the instance is activated" do
249 @user.should_not be_recently_activated
251 @user.should be_recently_activated
254 it "should not show recently activated when the instance is fresh" do
258 #User.first(:login => valid_user_hash[:login]).should_not be_recently_activated
261 it "should send out a welcome email to confirm that the account is activated" do
263 UserMailer.should_receive(:dispatch_and_deliver) do |action, mail_args, mailer_params|
264 action.should == :activation_notification
265 mail_args.keys.should include(:from)
266 mail_args.keys.should include(:to)
267 mail_args.keys.should include(:subject)
268 mail_args[:to].should == @user.email
269 mailer_params[:user].should == @user
276 describe User, "remember_me" do
277 include UserSpecHelper
279 predicate_matchers[:remember_token] = :remember_token?
282 User.clear_database_table
283 @user = User.new(valid_user_hash)
286 it "should have a remember_token_expires_at attribute" do
287 @user.attributes.keys.any?{|a| a.to_s == "remember_token_expires_at"}.should_not be_nil
290 it "should respond to remember_token?" do
291 @user.should respond_to(:remember_token?)
294 it "should return true if remember_token_expires_at is set and is in the future" do
295 @user.remember_token_expires_at = DateTime.now + 3600
296 @user.should remember_token
299 it "should set remember_token_expires_at to a specific date" do
300 time = DateTime.new(2009, 12, 25)
301 @user.remember_me_until(time)
302 @user.remember_token_expires_at.should == time
305 it "should set the remember_me token when remembering" do
306 time = Time.mktime(2009,12,25)
307 @user.remember_me_until(time)
308 @user.remember_token.should_not be_nil
310 User.first(:login => valid_user_hash[:login]).remember_token.should_not be_nil
313 it "should remember me for" do
315 DateTime.stub!(:now).and_return(t)
317 remember_until = today + (2* Merb::Const::WEEK)
318 @user.remember_me_for( Merb::Const::WEEK * 2)
319 @user.remember_token_expires_at.should == (remember_until)
322 it "should remember_me for two weeks" do
324 DateTime.stub!(:now).and_return(t)
326 @user.remember_token_expires_at.should == (DateTime.now + (2 * Merb::Const::WEEK ))
329 it "should forget me" do
333 @user.remember_token.should be_nil
334 @user.remember_token_expires_at.should be_nil
337 it "should persist the forget me to the database" do
341 @user = User.first(:login => valid_user_hash[:login])
342 @user.remember_token.should_not be_nil
346 @user = User.first(:login => valid_user_hash[:login])
347 @user.remember_token.should be_nil
348 @user.remember_token_expires_at.should be_nil