3 class UserLoginTest < ActionDispatch::IntegrationTest
4 fixtures :users, :user_blocks
7 OmniAuth.config.test_mode = true
11 OmniAuth.config.mock_auth[:openid] = nil
12 OmniAuth.config.mock_auth[:google] = nil
13 OmniAuth.config.mock_auth[:facebook] = nil
14 OmniAuth.config.mock_auth[:windowslive] = nil
15 OmniAuth.config.test_mode = false
18 def test_login_email_password_normal
19 user = users(:normal_user)
21 try_password_login user.email, "test"
23 assert_template "changeset/history"
24 assert_select "span.username", "test"
27 def test_login_email_password_normal_upcase
28 user = users(:normal_user)
30 try_password_login user.email.upcase, "test"
32 assert_template "changeset/history"
33 assert_select "span.username", "TEST"
36 def test_login_email_password_normal_titlecase
37 user = users(:normal_user)
39 try_password_login user.email.titlecase, "test"
41 assert_template "login"
42 assert_select "span.username", false
45 def test_login_email_password_public
46 user = users(:public_user)
48 try_password_login user.email, "test"
50 assert_template "changeset/history"
51 assert_select "span.username", "test2"
54 def test_login_email_password_public_upcase
55 user = users(:public_user)
57 try_password_login user.email.upcase, "test"
59 assert_template "changeset/history"
60 assert_select "span.username", "test2"
63 def test_login_email_password_public_titlecase
64 user = users(:public_user)
66 try_password_login user.email.titlecase, "test"
68 assert_template "changeset/history"
69 assert_select "span.username", "test2"
72 def test_login_email_password_inactive
73 user = users(:inactive_user)
75 try_password_login user.email, "test2"
77 assert_template "confirm"
78 assert_select "span.username", false
81 def test_login_email_password_inactive_upcase
82 user = users(:inactive_user)
84 try_password_login user.email.upcase, "test2"
86 assert_template "confirm"
87 assert_select "span.username", false
90 def test_login_email_password_inactive_titlecase
91 user = users(:inactive_user)
93 try_password_login user.email.titlecase, "test2"
95 assert_template "confirm"
96 assert_select "span.username", false
99 def test_login_email_password_suspended
100 user = users(:suspended_user)
102 try_password_login user.email, "test"
104 assert_template "login"
105 assert_select "span.username", false
106 assert_select "div.flash.error", /your account has been suspended/
109 def test_login_email_password_suspended_upcase
110 user = users(:suspended_user)
112 try_password_login user.email.upcase, "test"
114 assert_template "login"
115 assert_select "span.username", false
116 assert_select "div.flash.error", /your account has been suspended/
119 def test_login_email_password_suspended_titlecase
120 user = users(:suspended_user)
122 try_password_login user.email.titlecase, "test"
124 assert_template "login"
125 assert_select "span.username", false
126 assert_select "div.flash.error", /your account has been suspended/
129 def test_login_email_password_blocked
130 user = users(:blocked_user)
132 try_password_login user.email, "test"
134 assert_template "user_blocks/show"
135 assert_select "span.username", "blocked"
138 def test_login_email_password_blocked_upcase
139 user = users(:blocked_user)
141 try_password_login user.email.upcase, "test"
143 assert_template "user_blocks/show"
144 assert_select "span.username", "blocked"
147 def test_login_email_password_blocked_titlecase
148 user = users(:blocked_user)
150 try_password_login user.email.titlecase, "test"
152 assert_template "user_blocks/show"
153 assert_select "span.username", "blocked"
156 def test_login_username_password_normal
157 user = users(:normal_user)
159 try_password_login user.display_name, "test"
161 assert_template "changeset/history"
162 assert_select "span.username", "test"
165 def test_login_username_password_normal_upcase
166 user = users(:normal_user)
168 try_password_login user.display_name.upcase, "test"
170 assert_template "changeset/history"
171 assert_select "span.username", "TEST"
174 def test_login_username_password_normal_titlecase
175 user = users(:normal_user)
177 try_password_login user.display_name.titlecase, "test"
179 assert_template "login"
180 assert_select "span.username", false
183 def test_login_username_password_public
184 user = users(:public_user)
186 try_password_login user.display_name, "test"
188 assert_template "changeset/history"
189 assert_select "span.username", "test2"
192 def test_login_username_password_public_upcase
193 user = users(:public_user)
195 try_password_login user.display_name.upcase, "test"
197 assert_template "changeset/history"
198 assert_select "span.username", "test2"
201 def test_login_username_password_public_titlecase
202 user = users(:public_user)
204 try_password_login user.display_name.titlecase, "test"
206 assert_template "changeset/history"
207 assert_select "span.username", "test2"
210 def test_login_username_password_inactive
211 user = users(:inactive_user)
213 try_password_login user.display_name, "test2"
215 assert_template "confirm"
216 assert_select "span.username", false
219 def test_login_username_password_inactive_upcase
220 user = users(:inactive_user)
222 try_password_login user.display_name.upcase, "test2"
224 assert_template "confirm"
225 assert_select "span.username", false
228 def test_login_username_password_inactive_titlecase
229 user = users(:inactive_user)
231 try_password_login user.display_name.titlecase, "test2"
233 assert_template "confirm"
234 assert_select "span.username", false
237 def test_login_username_password_suspended
238 user = users(:suspended_user)
240 try_password_login user.display_name, "test"
242 assert_template "login"
243 assert_select "span.username", false
244 assert_select "div.flash.error", /your account has been suspended/
247 def test_login_username_password_suspended_upcase
248 user = users(:suspended_user)
250 try_password_login user.display_name.upcase, "test"
252 assert_template "login"
253 assert_select "span.username", false
254 assert_select "div.flash.error", /your account has been suspended/
257 def test_login_username_password_suspended_titlecase
258 user = users(:suspended_user)
260 try_password_login user.display_name.titlecase, "test"
262 assert_template "login"
263 assert_select "span.username", false
264 assert_select "div.flash.error", /your account has been suspended/
267 def test_login_username_password_blocked
268 user = users(:blocked_user)
270 try_password_login user.display_name.upcase, "test"
272 assert_template "user_blocks/show"
273 assert_select "span.username", "blocked"
276 def test_login_username_password_blocked_upcase
277 user = users(:blocked_user)
279 try_password_login user.display_name, "test"
281 assert_template "user_blocks/show"
282 assert_select "span.username", "blocked"
285 def test_login_username_password_blocked_titlecase
286 user = users(:blocked_user)
288 try_password_login user.display_name.titlecase, "test"
290 assert_template "user_blocks/show"
291 assert_select "span.username", "blocked"
294 def test_login_email_password_remember_me
295 user = users(:normal_user)
297 try_password_login user.email, "test", "yes"
299 assert_template "changeset/history"
300 assert_select "span.username", "test"
301 assert session.key?(:_remember_for)
304 def test_login_username_password_remember_me
305 user = users(:normal_user)
307 try_password_login user.display_name, "test", "yes"
309 assert_template "changeset/history"
310 assert_select "span.username", "test"
311 assert session.key?(:_remember_for)
314 def test_login_openid_success
315 OmniAuth.config.add_mock(:openid, :uid => "http://localhost:1123/john.doe")
317 get "/login", :referer => "/history"
318 assert_response :redirect
319 assert_redirected_to :controller => :user, :action => :login, :cookie_test => true, :referer => "/history"
321 assert_response :success
322 assert_template "user/login"
323 post "/login", :openid_url => "http://localhost:1123/john.doe", :referer => "/history"
324 assert_response :redirect
325 assert_redirected_to auth_path(:provider => "openid", :openid_url => "http://localhost:1123/john.doe", :origin => "/login?referer=%2Fhistory", :referer => "/history")
327 assert_response :redirect
328 assert_redirected_to auth_success_path(:provider => "openid", :openid_url => "http://localhost:1123/john.doe", :origin => "/login?referer=%2Fhistory", :referer => "/history")
330 assert_response :redirect
332 assert_response :success
333 assert_template "changeset/history"
334 assert_select "span.username", "openIDuser"
337 def test_login_openid_remember_me
338 OmniAuth.config.add_mock(:openid, :uid => "http://localhost:1123/john.doe")
340 get "/login", :referer => "/history"
341 assert_response :redirect
342 assert_redirected_to :controller => :user, :action => :login, :cookie_test => true, :referer => "/history"
344 assert_response :success
345 assert_template "user/login"
346 post "/login", :openid_url => "http://localhost:1123/john.doe", :remember_me_openid => true, :referer => "/history"
347 assert_response :redirect
348 assert_redirected_to auth_path(:provider => "openid", :openid_url => "http://localhost:1123/john.doe", :origin => "/login?referer=%2Fhistory", :referer => "/history")
350 assert_response :redirect
351 assert_redirected_to auth_success_path(:provider => "openid", :openid_url => "http://localhost:1123/john.doe", :origin => "/login?referer=%2Fhistory", :referer => "/history")
353 assert_response :redirect
355 assert_response :success
356 assert_template "changeset/history"
357 assert_select "span.username", "openIDuser"
358 assert session.key?(:_remember_for)
361 def test_login_openid_connection_failed
362 OmniAuth.config.mock_auth[:openid] = :connection_failed
364 get "/login", :referer => "/history"
365 assert_response :redirect
366 assert_redirected_to :controller => :user, :action => :login, :cookie_test => true, :referer => "/history"
368 assert_response :success
369 assert_template "user/login"
370 post "/login", :openid_url => "http://localhost:1123/john.doe", :referer => "/history"
371 assert_response :redirect
372 assert_redirected_to auth_path(:provider => "openid", :openid_url => "http://localhost:1123/john.doe", :origin => "/login?referer=%2Fhistory", :referer => "/history")
374 assert_response :redirect
375 assert_redirected_to auth_success_path(:provider => "openid", :openid_url => "http://localhost:1123/john.doe", :origin => "/login?referer=%2Fhistory", :referer => "/history")
377 assert_response :redirect
378 assert_redirected_to auth_failure_path(:strategy => "openid", :message => "connection_failed", :origin => "/login?referer=%2Fhistory")
380 assert_response :redirect
382 assert_response :success
383 assert_template "login"
384 assert_select "div.flash.error", "Connection to authentication provider failed"
385 assert_select "span.username", false
388 def test_login_openid_invalid_credentials
389 OmniAuth.config.mock_auth[:openid] = :invalid_credentials
391 get "/login", :referer => "/history"
392 assert_response :redirect
393 assert_redirected_to :controller => :user, :action => :login, :cookie_test => true, :referer => "/history"
395 assert_response :success
396 assert_template "user/login"
397 post "/login", :openid_url => "http://localhost:1123/john.doe", :referer => "/history"
398 assert_response :redirect
399 assert_redirected_to auth_path(:provider => "openid", :openid_url => "http://localhost:1123/john.doe", :origin => "/login?referer=%2Fhistory", :referer => "/history")
401 assert_response :redirect
402 assert_redirected_to auth_success_path(:provider => "openid", :openid_url => "http://localhost:1123/john.doe", :origin => "/login?referer=%2Fhistory", :referer => "/history")
404 assert_response :redirect
405 assert_redirected_to auth_failure_path(:strategy => "openid", :message => "invalid_credentials", :origin => "/login?referer=%2Fhistory")
407 assert_response :redirect
409 assert_response :success
410 assert_template "login"
411 assert_select "div.flash.error", "Invalid authentication credentials"
412 assert_select "span.username", false
415 def test_login_openid_unknown
416 OmniAuth.config.add_mock(:openid, :uid => "http://localhost:1123/fred.bloggs")
418 get "/login", :referer => "/history"
419 assert_response :redirect
420 assert_redirected_to :controller => :user, :action => :login, :cookie_test => true, :referer => "/history"
422 assert_response :success
423 assert_template "user/login"
424 post "/login", :openid_url => "http://localhost:1123/fred.bloggs", :referer => "/history"
425 assert_response :redirect
426 assert_redirected_to auth_path(:provider => "openid", :openid_url => "http://localhost:1123/fred.bloggs", :origin => "/login?referer=%2Fhistory", :referer => "/history")
428 assert_response :redirect
429 assert_redirected_to auth_success_path(:provider => "openid", :openid_url => "http://localhost:1123/fred.bloggs", :origin => "/login?referer=%2Fhistory", :referer => "/history")
431 assert_response :redirect
433 assert_response :success
434 assert_template "user/new"
435 assert_select "span.username", false
438 def test_login_google_success
439 OmniAuth.config.add_mock(:google, :uid => "123456789", :extra => {
440 :id_info => { "openid_id" => "http://localhost:1123/fred.bloggs" }
443 get "/login", :referer => "/history"
444 assert_response :redirect
445 assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
447 assert_response :success
448 assert_template "user/login"
449 get auth_path(:provider => "google", :origin => "/login?referer=%2Fhistory", :referer => "/history")
450 assert_response :redirect
451 assert_redirected_to auth_success_path(:provider => "google")
453 assert_response :redirect
455 assert_response :success
456 assert_template "changeset/history"
457 assert_select "span.username", "googleuser"
460 def test_login_google_connection_failed
461 OmniAuth.config.mock_auth[:google] = :connection_failed
463 get "/login", :referer => "/history"
464 assert_response :redirect
465 assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
467 assert_response :success
468 assert_template "user/login"
469 get auth_path(:provider => "google", :origin => "/login?referer=%2Fhistory", :referer => "/history")
470 assert_response :redirect
471 assert_redirected_to auth_success_path(:provider => "google")
473 assert_response :redirect
474 assert_redirected_to auth_failure_path(:strategy => "google", :message => "connection_failed", :origin => "/login?referer=%2Fhistory")
476 assert_response :redirect
478 assert_response :success
479 assert_template "login"
480 assert_select "div.flash.error", "Connection to authentication provider failed"
481 assert_select "span.username", false
484 def test_login_google_invalid_credentials
485 OmniAuth.config.mock_auth[:google] = :invalid_credentials
487 get "/login", :referer => "/history"
488 assert_response :redirect
489 assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
491 assert_response :success
492 assert_template "user/login"
493 get auth_path(:provider => "google", :origin => "/login?referer=%2Fhistory", :referer => "/history")
494 assert_response :redirect
495 assert_redirected_to auth_success_path(:provider => "google")
497 assert_response :redirect
498 assert_redirected_to auth_failure_path(:strategy => "google", :message => "invalid_credentials", :origin => "/login?referer=%2Fhistory")
500 assert_response :redirect
502 assert_response :success
503 assert_template "login"
504 assert_select "div.flash.error", "Invalid authentication credentials"
505 assert_select "span.username", false
508 def test_login_google_unknown
509 OmniAuth.config.add_mock(:google, :uid => "987654321", :extra => {
510 :id_info => { "openid_id" => "http://localhost:1123/fred.bloggs" }
513 get "/login", :referer => "/history"
514 assert_response :redirect
515 assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
517 assert_response :success
518 assert_template "user/login"
519 get auth_path(:provider => "google", :origin => "/login?referer=%2Fhistory", :referer => "/history")
520 assert_response :redirect
521 assert_redirected_to auth_success_path(:provider => "google")
523 assert_response :redirect
525 assert_response :success
526 assert_template "user/new"
527 assert_select "span.username", false
530 def test_login_google_upgrade
531 OmniAuth.config.add_mock(:google, :uid => "987654321", :extra => {
532 :id_info => { "openid_id" => "http://localhost:1123/john.doe" }
535 get "/login", :referer => "/history"
536 assert_response :redirect
537 assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
539 assert_response :success
540 assert_template "user/login"
541 get auth_path(:provider => "google", :origin => "/login?referer=%2Fhistory", :referer => "/history")
542 assert_response :redirect
543 assert_redirected_to auth_success_path(:provider => "google")
545 assert_response :redirect
547 assert_response :success
548 assert_template "changeset/history"
549 assert_select "span.username", "openIDuser"
551 user = User.find_by_display_name("openIDuser")
552 assert_equal "google", user.auth_provider
553 assert_equal "987654321", user.auth_uid
556 def test_login_facebook_success
557 OmniAuth.config.add_mock(:facebook, :uid => "123456789")
559 get "/login", :referer => "/history"
560 assert_response :redirect
561 assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
563 assert_response :success
564 assert_template "user/login"
565 get auth_path(:provider => "facebook", :origin => "/login?referer=%2Fhistory", :referer => "/history")
566 assert_response :redirect
567 assert_redirected_to auth_success_path(:provider => "facebook")
569 assert_response :redirect
571 assert_response :success
572 assert_template "changeset/history"
573 assert_select "span.username", "facebookuser"
576 def test_login_facebook_connection_failed
577 OmniAuth.config.mock_auth[:facebook] = :connection_failed
579 get "/login", :referer => "/history"
580 assert_response :redirect
581 assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
583 assert_response :success
584 assert_template "user/login"
585 get auth_path(:provider => "facebook", :origin => "/login?referer=%2Fhistory", :referer => "/history")
586 assert_response :redirect
587 assert_redirected_to auth_success_path(:provider => "facebook")
589 assert_response :redirect
590 assert_redirected_to auth_failure_path(:strategy => "facebook", :message => "connection_failed", :origin => "/login?referer=%2Fhistory")
592 assert_response :redirect
594 assert_response :success
595 assert_template "login"
596 assert_select "div.flash.error", "Connection to authentication provider failed"
597 assert_select "span.username", false
600 def test_login_facebook_invalid_credentials
601 OmniAuth.config.mock_auth[:facebook] = :invalid_credentials
603 get "/login", :referer => "/history"
604 assert_response :redirect
605 assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
607 assert_response :success
608 assert_template "user/login"
609 get auth_path(:provider => "facebook", :origin => "/login?referer=%2Fhistory", :referer => "/history")
610 assert_response :redirect
611 assert_redirected_to auth_success_path(:provider => "facebook")
613 assert_response :redirect
614 assert_redirected_to auth_failure_path(:strategy => "facebook", :message => "invalid_credentials", :origin => "/login?referer=%2Fhistory")
616 assert_response :redirect
618 assert_response :success
619 assert_template "login"
620 assert_select "div.flash.error", "Invalid authentication credentials"
621 assert_select "span.username", false
624 def test_login_facebook_unknown
625 OmniAuth.config.add_mock(:facebook, :uid => "987654321")
627 get "/login", :referer => "/history"
628 assert_response :redirect
629 assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
631 assert_response :success
632 assert_template "user/login"
633 get auth_path(:provider => "facebook", :origin => "/login?referer=%2Fhistory", :referer => "/history")
634 assert_response :redirect
635 assert_redirected_to auth_success_path(:provider => "facebook")
637 assert_response :redirect
639 assert_response :success
640 assert_template "user/new"
641 assert_select "span.username", false
644 def test_login_windowslive_success
645 OmniAuth.config.add_mock(:windowslive, :uid => "123456789")
647 get "/login", :referer => "/history"
648 assert_response :redirect
649 assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
651 assert_response :success
652 assert_template "user/login"
653 get auth_path(:provider => "windowslive", :origin => "/login?referer=%2Fhistory", :referer => "/history")
654 assert_response :redirect
655 assert_redirected_to auth_success_path(:provider => "windowslive", :origin => "/login?referer=%2Fhistory", :referer => "/history")
657 assert_response :redirect
659 assert_response :success
660 assert_template "changeset/history"
661 assert_select "span.username", "windowsliveuser"
664 def test_login_windowslive_connection_failed
665 OmniAuth.config.mock_auth[:windowslive] = :connection_failed
667 get "/login", :referer => "/history"
668 assert_response :redirect
669 assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
671 assert_response :success
672 assert_template "user/login"
673 get auth_path(:provider => "windowslive", :origin => "/login?referer=%2Fhistory", :referer => "/history")
674 assert_response :redirect
675 assert_redirected_to auth_success_path(:provider => "windowslive", :origin => "/login?referer=%2Fhistory", :referer => "/history")
677 assert_response :redirect
678 assert_redirected_to auth_failure_path(:strategy => "windowslive", :message => "connection_failed", :origin => "/login?referer=%2Fhistory")
680 assert_response :redirect
682 assert_response :success
683 assert_template "login"
684 assert_select "div.flash.error", "Connection to authentication provider failed"
685 assert_select "span.username", false
688 def test_login_windowslive_invalid_credentials
689 OmniAuth.config.mock_auth[:windowslive] = :invalid_credentials
691 get "/login", :referer => "/history"
692 assert_response :redirect
693 assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
695 assert_response :success
696 assert_template "user/login"
697 get auth_path(:provider => "windowslive", :origin => "/login?referer=%2Fhistory", :referer => "/history")
698 assert_response :redirect
699 assert_redirected_to auth_success_path(:provider => "windowslive", :origin => "/login?referer=%2Fhistory", :referer => "/history")
701 assert_response :redirect
702 assert_redirected_to auth_failure_path(:strategy => "windowslive", :message => "invalid_credentials", :origin => "/login?referer=%2Fhistory")
704 assert_response :redirect
706 assert_response :success
707 assert_template "login"
708 assert_select "div.flash.error", "Invalid authentication credentials"
709 assert_select "span.username", false
712 def test_login_windowslive_unknown
713 OmniAuth.config.add_mock(:windowslive, :uid => "987654321")
715 get "/login", :referer => "/history"
716 assert_response :redirect
717 assert_redirected_to "controller" => "user", "action" => "login", "cookie_test" => "true", "referer" => "/history"
719 assert_response :success
720 assert_template "user/login"
721 get auth_path(:provider => "windowslive", :origin => "/login?referer=%2Fhistory", :referer => "/history")
722 assert_response :redirect
723 assert_redirected_to auth_success_path(:provider => "windowslive", :origin => "/login?referer=%2Fhistory", :referer => "/history")
725 assert_response :redirect
727 assert_response :success
728 assert_template "user/new"
729 assert_select "span.username", false
734 def try_password_login(username, password, remember_me = nil)
736 assert_response :redirect
737 assert_redirected_to :controller => :user, :action => :login, :cookie_test => true
739 assert_response :success
740 assert_template "login"
741 assert_select "input#username", 1 do
742 assert_select "[value]", false
744 assert_select "input#password", 1 do
745 assert_select "[value=?]", ""
747 assert_select "input#remember_me", 1 do
748 assert_select "[checked]", false
751 post "/login", :username => username, :password => "wrong", :remember_me => remember_me, :referer => "/history"
752 assert_response :redirect
754 assert_response :success
755 assert_template "login"
756 assert_select "input#username", 1 do
757 assert_select "[value=?]", username
759 assert_select "input#password", 1 do
760 assert_select "[value=?]", ""
762 assert_select "input#remember_me", 1 do
763 assert_select "[checked]", remember_me == "yes"
766 post "/login", :username => username, :password => password, :remember_me => remember_me, :referer => "/history"
767 assert_response :redirect
769 assert_response :success