1 require File.dirname(__FILE__) + '/../test_helper'
2 require_dependency 'user_controller'
5 # Raise errors beyond the default web-based presentation
6 class UserController; def rescue_action(e) raise e end; end
8 class UserControllerTest < Test::Unit::TestCase
10 # load the fixture into the developer-specified table using the custom
12 fixture :users, :table_name => LoginEngine.config(:user_table), :class_name => "User"
16 LoginEngine::CONFIG[:salt] = "test-salt"
18 @controller = UserController.new
19 @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
20 @request.host = "localhost"
25 #==========================================================================
29 #==========================================================================
31 def test_home_without_login
33 assert_redirected_to :action => "login"
36 def test_invalid_login
37 post :login, :user => { :login => "bob", :password => "wrong_password" }
38 assert_response :success
40 assert_session_has_no :user
41 assert_template "login"
45 @request.session['return-to'] = "/bogus/location"
47 post :login, :user => { :login => "bob", :password => "atest" }
49 assert_response 302 # redirect
50 assert_session_has :user
51 assert_equal users(:bob), session[:user]
53 assert_redirect_url "http://#{@request.host}/bogus/location"
58 post :login, :user => { :login => "bob", :password => "atest" }
59 assert_session_has :user
62 assert_session_has_no :user
67 #==========================================================================
71 #==========================================================================
74 LoginEngine::CONFIG[:use_email_notification] = true
76 ActionMailer::Base.deliveries = []
78 @request.session['return-to'] = "/bogus/location"
80 assert_equal 5, User.count
81 post :signup, :user => { :login => "newbob", :password => "newpassword", :password_confirmation => "newpassword", :email => "newbob@test.com" }
82 assert_session_has_no :user
84 assert_redirect_url(@controller.url_for(:action => "login"))
85 assert_equal 1, ActionMailer::Base.deliveries.size
86 mail = ActionMailer::Base.deliveries[0]
87 assert_equal "newbob@test.com", mail.to_addrs[0].to_s
88 assert_match /login:\s+\w+\n/, mail.encoded
89 assert_match /password:\s+\w+\n/, mail.encoded
90 #mail.encoded =~ /user_id=(.*?)&key=(.*?)"/
91 user_id = /user_id=(\d+)/.match(mail.encoded)[1]
92 key = /key=([a-z0-9]+)/.match(mail.encoded)[1]
94 assert_not_nil user_id
97 user = User.find_by_email("newbob@test.com")
99 assert_equal 0, user.verified
101 # First past the expiration.
102 Time.advance_by_days = 1
103 get :home, :user_id => "#{user_id}", :key => "#{key}"
104 Time.advance_by_days = 0
105 user = User.find_by_email("newbob@test.com")
106 assert_equal 0, user.verified
109 get :home, :user_id => "#{user_id}", :key => "boguskey"
110 user = User.find_by_email("newbob@test.com")
111 assert_equal 0, user.verified
114 get :home, :user_id => "#{user_id}", :key => "#{key}"
115 user = User.find_by_email("newbob@test.com")
116 assert_equal 1, user.verified
118 post :login, :user => { :login => "newbob", :password => "newpassword" }
119 assert_session_has :user
124 def test_signup_bad_password
125 LoginEngine::CONFIG[:use_email_notification] = true
126 ActionMailer::Base.deliveries = []
128 @request.session['return-to'] = "/bogus/location"
129 post :signup, :user => { :login => "newbob", :password => "bad", :password_confirmation => "bad", :email => "newbob@test.com" }
130 assert_session_has_no :user
131 assert_invalid_column_on_record "user", "password"
133 assert_equal 0, ActionMailer::Base.deliveries.size
136 def test_signup_bad_email
137 LoginEngine::CONFIG[:use_email_notification] = true
138 ActionMailer::Base.deliveries = []
140 @request.session['return-to'] = "/bogus/location"
142 ActionMailer::Base.inject_one_error = true
143 post :signup, :user => { :login => "newbob", :password => "newpassword", :password_confirmation => "newpassword", :email => "newbob@test.com" }
144 assert_session_has_no :user
145 assert_equal 0, ActionMailer::Base.deliveries.size
148 def test_signup_without_email
149 LoginEngine::CONFIG[:use_email_notification] = false
151 @request.session['return-to'] = "/bogus/location"
153 post :signup, :user => { :login => "newbob", :password => "newpassword", :password_confirmation => "newpassword", :email => "newbob@test.com" }
155 assert_redirect_url(@controller.url_for(:action => "login"))
156 assert_session_has_no :user
157 assert_match /Signup successful/, flash[:notice]
159 assert_not_nil User.find_by_login("newbob")
161 user = User.find_by_email("newbob@test.com")
164 post :login, :user => { :login => "newbob", :password => "newpassword" }
165 assert_session_has :user
169 def test_signup_bad_details
170 @request.session['return-to'] = "/bogus/location"
172 # mismatched password
173 post :signup, :user => { :login => "newbob", :password => "newpassword", :password_confirmation => "wrong" }
174 assert_invalid_column_on_record "user", "password"
177 # login not long enough
178 post :signup, :user => { :login => "yo", :password => "newpassword", :password_confirmation => "newpassword" }
179 assert_invalid_column_on_record "user", "login"
183 post :signup, :user => { :login => "yo", :password => "newpassword", :password_confirmation => "wrong" }
184 assert_invalid_column_on_record "user", ["login", "password"]
188 post :signup, :user => { :login => "bob", :password => "doesnt_matter", :password_confirmation => "doesnt_matter" }
189 assert_invalid_column_on_record "user", "login"
193 post :signup, :user => { :login => "newbob", :email => "longbob@test.com", :password => "doesnt_matter", :password_confirmation => "doesnt_matter" }
194 assert_invalid_column_on_record "user", "email"
200 #==========================================================================
204 #==========================================================================
207 post :login, :user => { :login => "bob", :password => "atest" }
208 assert_session_has :user
210 post :edit, :user => { "firstname" => "Bob", "form" => "edit" }
211 assert_equal @response.session[:user].firstname, "Bob"
213 post :edit, :user => { "firstname" => "", "form" => "edit" }
214 assert_equal @response.session[:user].firstname, ""
221 #==========================================================================
225 #==========================================================================
228 LoginEngine::CONFIG[:use_email_notification] = true
230 post :login, :user => { :login => "deletebob1", :password => "alongtest" }
231 assert_session_has :user
233 LoginEngine.config :delayed_delete, false, :force
235 assert_equal 1, ActionMailer::Base.deliveries.size
236 assert_session_has_no :user
238 # try and login in again, we should fail.
239 post :login, :user => { :login => "deletebob1", :password => "alongtest" }
240 assert_session_has_no :user
241 assert_template_has "login"
244 # Now try delayed delete
245 ActionMailer::Base.deliveries = []
247 post :login, :user => { :login => "deletebob2", :password => "alongtest" }
248 assert_session_has :user
250 LoginEngine.config :delayed_delete, true, :force
252 assert_equal 1, ActionMailer::Base.deliveries.size
253 mail = ActionMailer::Base.deliveries[0]
254 user_id = /user_id=(\d+)/.match(mail.encoded)[1]
255 key = /key=([a-z0-9]+)/.match(mail.encoded)[1]
257 post :restore_deleted, :user_id => "#{user_id}", "key" => "badkey"
258 assert_session_has_no :user
260 # Advance the time past the delete date
261 Time.advance_by_days = LoginEngine.config :delayed_delete_days
262 post :restore_deleted, :user_id => "#{user_id}", "key" => "#{key}"
263 assert_session_has_no :user
264 Time.advance_by_days = 0
266 post :restore_deleted, :user_id => "#{user_id}", "key" => "#{key}"
267 assert_session_has :user
270 def test_delete_without_email
271 LoginEngine::CONFIG[:use_email_notification] = false
272 ActionMailer::Base.deliveries = []
275 post :login, :user => { :login => "deletebob1", :password => "alongtest" }
276 assert_session_has :user
278 LoginEngine.config :delayed_delete, false, :force
280 assert_session_has_no :user
281 assert_nil User.find_by_login("deletebob1")
283 # try and login in again, we should fail.
284 post :login, :user => { :login => "deletebob1", :password => "alongtest" }
285 assert_session_has_no :user
286 assert_template_has "login"
289 # Now try delayed delete
290 ActionMailer::Base.deliveries = []
292 post :login, :user => { :login => "deletebob2", :password => "alongtest" }
293 assert_session_has :user
295 # delayed delete is not really relevant currently without email.
296 LoginEngine.config :delayed_delete, true, :force
298 assert_equal 1, User.find_by_login("deletebob2").deleted
303 #==========================================================================
307 #==========================================================================
309 def test_change_valid_password
311 LoginEngine::CONFIG[:use_email_notification] = true
313 ActionMailer::Base.deliveries = []
315 post :login, :user => { :login => "bob", :password => "atest" }
316 assert_session_has :user
318 post :change_password, :user => { :password => "changed_password", :password_confirmation => "changed_password" }
320 assert_equal 1, ActionMailer::Base.deliveries.size
321 mail = ActionMailer::Base.deliveries[0]
322 assert_equal "bob@test.com", mail.to_addrs[0].to_s
323 assert_match /login:\s+\w+\n/, mail.encoded
324 assert_match /password:\s+\w+\n/, mail.encoded
326 post :login, :user => { :login => "bob", :password => "changed_password" }
327 assert_session_has :user
328 post :change_password, :user => { :password => "atest", :password_confirmation => "atest" }
331 post :login, :user => { :login => "bob", :password => "atest" }
332 assert_session_has :user
337 def test_change_valid_password_without_email
339 LoginEngine::CONFIG[:use_email_notification] = false
341 ActionMailer::Base.deliveries = []
343 post :login, :user => { :login => "bob", :password => "atest" }
344 assert_session_has :user
346 post :change_password, :user => { :password => "changed_password", :password_confirmation => "changed_password" }
348 assert_redirected_to :action => "change_password"
350 post :login, :user => { :login => "bob", :password => "changed_password" }
351 assert_session_has :user
352 post :change_password, :user => { :password => "atest", :password_confirmation => "atest" }
355 post :login, :user => { :login => "bob", :password => "atest" }
356 assert_session_has :user
361 def test_change_short_password
362 LoginEngine::CONFIG[:use_email_notification] = true
363 ActionMailer::Base.deliveries = []
365 post :login, :user => { :login => "bob", :password => "atest" }
366 assert_session_has :user
368 post :change_password, :user => { :password => "bad", :password_confirmation => "bad" }
369 assert_invalid_column_on_record "user", "password"
371 assert_equal 0, ActionMailer::Base.deliveries.size
373 post :login, :user => { :login => "bob", :password => "atest" }
374 assert_session_has :user
379 def test_change_short_password_without_email
380 LoginEngine::CONFIG[:use_email_notification] = false
381 post :login, :user => { :login => "bob", :password => "atest" }
382 assert_session_has :user
384 post :change_password, :user => { :password => "bad", :password_confirmation => "bad" }
385 assert_invalid_column_on_record "user", "password"
388 post :login, :user => { :login => "bob", :password => "atest" }
389 assert_session_has :user
395 def test_change_password_with_bad_email
396 LoginEngine::CONFIG[:use_email_notification] = true
397 ActionMailer::Base.deliveries = []
400 post :login, :user => { :login => "bob", :password => "atest" }
401 assert_session_has :user
403 # change the password, but the email delivery will fail
404 ActionMailer::Base.inject_one_error = true
405 post :change_password, :user => { :password => "changed_password", :password_confirmation => "changed_password" }
406 assert_equal 0, ActionMailer::Base.deliveries.size
407 assert_match /Password could not be changed/, flash[:warning]
411 assert_session_has_no :user
413 # ensure we can log in with our original password
414 # TODO: WHY DOES THIS FAIL!! It looks like the transaction stuff in UserController#change_password isn't actually rolling back changes.
415 post :login, :user => { :login => "bob", :password => "atest" }
416 assert_session_has :user
424 #==========================================================================
428 #==========================================================================
430 def test_forgot_password
431 LoginEngine::CONFIG[:use_email_notification] = true
433 do_forgot_password(false, false, false)
434 do_forgot_password(false, false, true)
435 do_forgot_password(true, false, false)
436 do_forgot_password(false, true, false)
439 def do_forgot_password(bad_address, bad_email, logged_in)
440 ActionMailer::Base.deliveries = []
443 post :login, :user => { :login => "bob", :password => "atest" }
444 assert_session_has :user
447 @request.session['return-to'] = "/bogus/location"
448 if not bad_address and not bad_email
449 post :forgot_password, :user => { :email => "bob@test.com" }
450 password = "anewpassword"
452 assert_equal 0, ActionMailer::Base.deliveries.size
453 assert_redirect_url(@controller.url_for(:action => "change_password"))
454 post :change_password, :user => { :password => "#{password}", :password_confirmation => "#{password}" }
456 assert_equal 1, ActionMailer::Base.deliveries.size
457 mail = ActionMailer::Base.deliveries[0]
458 assert_equal "bob@test.com", mail.to_addrs[0].to_s
459 user_id = /user_id=(\d+)/.match(mail.encoded)[1]
460 key = /key=([a-z0-9]+)/.match(mail.encoded)[1]
461 post :change_password, :user => { :password => "#{password}", :password_confirmation => "#{password}"}, :user_id => "#{user_id}", :key => "#{key}"
462 assert_session_has :user
466 post :forgot_password, :user => { :email => "bademail@test.com" }
467 assert_equal 0, ActionMailer::Base.deliveries.size
469 ActionMailer::Base.inject_one_error = true
470 post :forgot_password, :user => { :email => "bob@test.com" }
471 assert_equal 0, ActionMailer::Base.deliveries.size
477 if not bad_address and not bad_email
481 assert_redirect_url(@controller.url_for(:action => "login"))
483 post :login, :user => { :login => "bob", :password => "#{password}" }
485 # Okay, make sure the database did not get changed
489 post :login, :user => { :login => "bob", :password => "atest" }
492 assert_session_has :user
494 # Put the old settings back
495 if not bad_address and not bad_email
496 post :change_password, :user => { :password => "atest", :password_confirmation => "atest" }
502 def test_forgot_password_without_email_and_logged_in
503 LoginEngine::CONFIG[:use_email_notification] = false
505 post :login, :user => { :login => "bob", :password => "atest" }
506 assert_session_has :user
508 @request.session['return-to'] = "/bogus/location"
509 post :forgot_password, :user => { :email => "bob@test.com" }
510 password = "anewpassword"
511 assert_redirect_url(@controller.url_for(:action => "change_password"))
512 post :change_password, :user => { :password => "#{password}", :password_confirmation => "#{password}" }
516 post :login, :user => { :login => "bob", :password => "#{password}" }
518 assert_session_has :user
523 def forgot_password_without_email_and_not_logged_in
524 LoginEngine::CONFIG[:use_email_notification] = false
526 @request.session['return-to'] = "/bogus/location"
527 post :forgot_password, :user => { :email => "bob@test.com" }
528 password = "anewpassword"
530 # wothout email, you can't retrieve your forgotten password...
531 assert_match /Please contact the system admin/, flash[:message]
532 assert_session_has_no :user
534 assert_redirect_url "http://#{@request.host}/bogus/location"