3 class UsersControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/login", :method => :get },
9 { :controller => "users", :action => "login" }
12 { :path => "/login", :method => :post },
13 { :controller => "users", :action => "login" }
16 { :controller => "users", :action => "login", :format => "html" },
17 { :path => "/login.html", :method => :get }
21 { :path => "/logout", :method => :get },
22 { :controller => "users", :action => "logout" }
25 { :path => "/logout", :method => :post },
26 { :controller => "users", :action => "logout" }
29 { :controller => "users", :action => "logout", :format => "html" },
30 { :path => "/logout.html", :method => :get }
34 { :path => "/user/new", :method => :get },
35 { :controller => "users", :action => "new" }
39 { :path => "/user/new", :method => :post },
40 { :controller => "users", :action => "create" }
44 { :path => "/user/terms", :method => :get },
45 { :controller => "users", :action => "terms" }
49 { :path => "/user/save", :method => :post },
50 { :controller => "users", :action => "save" }
54 { :path => "/user/username/confirm", :method => :get },
55 { :controller => "users", :action => "confirm", :display_name => "username" }
58 { :path => "/user/username/confirm", :method => :post },
59 { :controller => "users", :action => "confirm", :display_name => "username" }
62 { :path => "/user/username/confirm/resend", :method => :get },
63 { :controller => "users", :action => "confirm_resend", :display_name => "username" }
67 { :path => "/user/confirm", :method => :get },
68 { :controller => "users", :action => "confirm" }
71 { :path => "/user/confirm", :method => :post },
72 { :controller => "users", :action => "confirm" }
75 { :path => "/user/confirm-email", :method => :get },
76 { :controller => "users", :action => "confirm_email" }
79 { :path => "/user/confirm-email", :method => :post },
80 { :controller => "users", :action => "confirm_email" }
84 { :path => "/user/go_public", :method => :post },
85 { :controller => "users", :action => "go_public" }
89 { :path => "/user/forgot-password", :method => :get },
90 { :controller => "users", :action => "lost_password" }
93 { :path => "/user/forgot-password", :method => :post },
94 { :controller => "users", :action => "lost_password" }
97 { :path => "/user/reset-password", :method => :get },
98 { :controller => "users", :action => "reset_password" }
101 { :path => "/user/reset-password", :method => :post },
102 { :controller => "users", :action => "reset_password" }
106 { :path => "/user/suspended", :method => :get },
107 { :controller => "users", :action => "suspended" }
111 { :path => "/user/username", :method => :get },
112 { :controller => "users", :action => "show", :display_name => "username" }
116 { :path => "/user/username/account", :method => :get },
117 { :controller => "users", :action => "account", :display_name => "username" }
120 { :path => "/user/username/account", :method => :post },
121 { :controller => "users", :action => "account", :display_name => "username" }
125 { :path => "/user/username/make_friend", :method => :get },
126 { :controller => "users", :action => "make_friend", :display_name => "username" }
129 { :path => "/user/username/make_friend", :method => :post },
130 { :controller => "users", :action => "make_friend", :display_name => "username" }
133 { :path => "/user/username/remove_friend", :method => :get },
134 { :controller => "users", :action => "remove_friend", :display_name => "username" }
137 { :path => "/user/username/remove_friend", :method => :post },
138 { :controller => "users", :action => "remove_friend", :display_name => "username" }
142 { :path => "/user/username/set_status", :method => :get },
143 { :controller => "users", :action => "set_status", :display_name => "username" }
146 { :path => "/user/username/delete", :method => :get },
147 { :controller => "users", :action => "delete", :display_name => "username" }
151 { :path => "/users", :method => :get },
152 { :controller => "users", :action => "index" }
155 { :path => "/users", :method => :post },
156 { :controller => "users", :action => "index" }
159 { :path => "/users/status", :method => :get },
160 { :controller => "users", :action => "index", :status => "status" }
163 { :path => "/users/status", :method => :post },
164 { :controller => "users", :action => "index", :status => "status" }
168 # The user creation page loads
171 assert_response :redirect
172 assert_redirected_to user_new_path(:cookie_test => "true")
174 get user_new_path, :params => { :cookie_test => "true" }
175 assert_response :success
177 assert_select "html", :count => 1 do
178 assert_select "head", :count => 1 do
179 assert_select "title", :text => /Sign Up/, :count => 1
181 assert_select "body", :count => 1 do
182 assert_select "div#content", :count => 1 do
183 assert_select "form[action='/user/new'][method='post']", :count => 1 do
184 assert_select "input[id='user_email']", :count => 1
185 assert_select "input[id='user_email_confirmation']", :count => 1
186 assert_select "input[id='user_display_name']", :count => 1
187 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
188 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
189 assert_select "input[type='submit'][value='Sign Up']", :count => 1
196 def test_new_view_logged_in
197 session_for(create(:user))
200 assert_response :redirect
201 assert_redirected_to root_path
203 get user_new_path, :params => { :referer => "/test" }
204 assert_response :redirect
205 assert_redirected_to "/test"
209 user = build(:user, :pending)
211 assert_no_difference "User.count" do
212 assert_no_difference "ActionMailer::Base.deliveries.size" do
213 perform_enqueued_jobs do
214 post user_new_path, :params => { :user => user.attributes }
219 assert_difference "User.count", 1 do
220 assert_difference "ActionMailer::Base.deliveries.size", 1 do
221 perform_enqueued_jobs do
222 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
228 register_email = ActionMailer::Base.deliveries.first
230 assert_equal register_email.to[0], user.email
231 assert_match(/#{@url}/, register_email.body.to_s)
234 assert_redirected_to :action => "confirm", :display_name => user.display_name
236 ActionMailer::Base.deliveries.clear
239 def test_new_duplicate_email
240 user = build(:user, :pending)
241 create(:user, :email => user.email)
243 assert_no_difference "User.count" do
244 assert_no_difference "ActionMailer::Base.deliveries.size" do
245 perform_enqueued_jobs do
246 post user_new_path, :params => { :user => user.attributes }
251 assert_response :success
252 assert_template "new"
253 assert_select "form > fieldset > div.standard-form-row > input.field_with_errors#user_email"
256 def test_save_duplicate_email
257 user = build(:user, :pending)
259 # Set up our user as being half-way through registration
260 assert_no_difference "User.count" do
261 assert_no_difference "ActionMailer::Base.deliveries.size" do
262 perform_enqueued_jobs do
263 post user_new_path, :params => { :user => user.attributes }
268 # Now create another user with that email
269 create(:user, :email => user.email)
271 # Check that the second half of registration fails
272 assert_no_difference "User.count" do
273 assert_no_difference "ActionMailer::Base.deliveries.size" do
274 perform_enqueued_jobs do
275 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
280 assert_response :success
281 assert_template "new"
282 assert_select "form > fieldset > div.standard-form-row > input.field_with_errors#user_email"
285 def test_save_duplicate_email_uppercase
286 user = build(:user, :pending)
288 # Set up our user as being half-way through registration
289 assert_no_difference "User.count" do
290 assert_no_difference "ActionMailer::Base.deliveries.size" do
291 perform_enqueued_jobs do
292 post user_new_path, :params => { :user => user.attributes }
297 # Now create another user with that email, but uppercased
298 create(:user, :email => user.email.upcase)
300 # Check that the second half of registration fails
301 assert_no_difference "User.count" do
302 assert_no_difference "ActionMailer::Base.deliveries.size" do
303 perform_enqueued_jobs do
304 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
309 assert_response :success
310 assert_template "new"
311 assert_select "form > fieldset > div.standard-form-row > input.field_with_errors#user_email"
314 def test_save_duplicate_name
315 user = build(:user, :pending)
317 # Set up our user as being half-way through registration
318 assert_no_difference "User.count" do
319 assert_no_difference "ActionMailer::Base.deliveries.size" do
320 perform_enqueued_jobs do
321 post user_new_path, :params => { :user => user.attributes }
326 # Now create another user with that display name
327 create(:user, :display_name => user.display_name)
329 # Check that the second half of registration fails
330 assert_no_difference "User.count" do
331 assert_no_difference "ActionMailer::Base.deliveries.size" do
332 perform_enqueued_jobs do
333 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
338 assert_response :success
339 assert_template "new"
340 assert_select "form > fieldset > div.standard-form-row > input.field_with_errors#user_display_name"
343 def test_save_duplicate_name_uppercase
344 user = build(:user, :pending)
346 # Set up our user as being half-way through registration
347 assert_no_difference "User.count" do
348 assert_no_difference "ActionMailer::Base.deliveries.size" do
349 perform_enqueued_jobs do
350 post user_new_path, :params => { :user => user.attributes }
355 # Now create another user with that display_name, but uppercased
356 create(:user, :display_name => user.display_name.upcase)
358 # Check that the second half of registration fails
359 assert_no_difference "User.count" do
360 assert_no_difference "ActionMailer::Base.deliveries.size" do
361 perform_enqueued_jobs do
362 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
367 assert_response :success
368 assert_template "new"
369 assert_select "form > fieldset > div.standard-form-row > input.field_with_errors#user_display_name"
372 def test_save_blocked_domain
373 user = build(:user, :pending, :email => "user@example.net")
375 # Set up our user as being half-way through registration
376 assert_no_difference "User.count" do
377 assert_no_difference "ActionMailer::Base.deliveries.size" do
378 perform_enqueued_jobs do
379 post user_new_path, :params => { :user => user.attributes }
384 # Now block that domain
385 create(:acl, :domain => "example.net", :k => "no_account_creation")
387 # Check that the second half of registration fails
388 assert_no_difference "User.count" do
389 assert_no_difference "ActionMailer::Base.deliveries.size" do
390 perform_enqueued_jobs do
391 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
396 assert_response :success
397 assert_template "blocked"
400 def test_save_referer_params
401 user = build(:user, :pending)
403 # Set up our user as being half-way through registration
404 assert_no_difference "User.count" do
405 assert_no_difference "ActionMailer::Base.deliveries.size" do
406 perform_enqueued_jobs do
407 post user_new_path, :params => { :user => user.attributes, :referer => "/edit?editor=id#map=1/2/3" }
412 assert_difference "User.count", 1 do
413 assert_difference "ActionMailer::Base.deliveries.size", 1 do
414 perform_enqueued_jobs do
415 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
420 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
421 User.find_by(:email => user.email).tokens.order("id DESC").first.referer
423 ActionMailer::Base.deliveries.clear
426 def test_logout_without_referer
428 assert_response :redirect
429 assert_redirected_to root_path
432 def test_logout_with_referer
433 post logout_path, :params => { :referer => "/test" }
434 assert_response :redirect
435 assert_redirected_to "/test"
438 def test_logout_fallback_without_referer
440 assert_response :success
441 assert_template :logout
442 assert_select "input[name=referer][value=?]", ""
445 def test_logout_fallback_with_referer
446 get logout_path, :params => { :referer => "/test" }
447 assert_response :success
448 assert_template :logout
449 assert_select "input[name=referer][value=?]", "/test"
452 def test_logout_removes_session_token
453 user = build(:user, :pending)
454 post user_new_path, :params => { :user => user.attributes }
455 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
457 assert_difference "User.find_by(:email => user.email).tokens.count", -1 do
460 assert_response :redirect
461 assert_redirected_to root_path
465 user = build(:user, :pending)
466 post user_new_path, :params => { :user => user.attributes }
467 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
468 confirm_string = User.find_by(:email => user.email).tokens.create.token
470 get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
471 assert_response :success
472 assert_template :confirm
475 def test_confirm_get_already_confirmed
476 user = build(:user, :pending)
477 stub_gravatar_request(user.email)
478 post user_new_path, :params => { :user => user.attributes }
479 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
480 confirm_string = User.find_by(:email => user.email).tokens.create.token
482 # Get the confirmation page
483 get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
484 assert_response :success
485 assert_template :confirm
488 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
489 assert_redirected_to welcome_path
491 # Now try to get the confirmation page again
492 get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
493 assert_response :redirect
494 assert_redirected_to root_path
497 def test_confirm_success_no_token_no_referer
498 user = build(:user, :pending)
499 stub_gravatar_request(user.email)
500 post user_new_path, :params => { :user => user.attributes }
501 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
502 confirm_string = User.find_by(:email => user.email).tokens.create.token
506 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
507 assert_redirected_to login_path
508 assert_match(/Confirmed your account/, flash[:notice])
511 def test_confirm_success_good_token_no_referer
512 user = build(:user, :pending)
513 stub_gravatar_request(user.email)
514 post user_new_path, :params => { :user => user.attributes }
515 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
516 confirm_string = User.find_by(:email => user.email).tokens.create.token
518 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
519 assert_redirected_to welcome_path
522 def test_confirm_success_bad_token_no_referer
523 user = build(:user, :pending)
524 stub_gravatar_request(user.email)
525 post user_new_path, :params => { :user => user.attributes }
526 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
527 confirm_string = User.find_by(:email => user.email).tokens.create.token
530 session_for(create(:user))
532 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
533 assert_redirected_to login_path
534 assert_match(/Confirmed your account/, flash[:notice])
537 def test_confirm_success_no_token_with_referer
538 user = build(:user, :pending)
539 stub_gravatar_request(user.email)
540 post user_new_path, :params => { :user => user.attributes }
541 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
542 confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token
546 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
547 assert_redirected_to login_path(:referer => new_diary_entry_path)
548 assert_match(/Confirmed your account/, flash[:notice])
551 def test_confirm_success_good_token_with_referer
552 user = build(:user, :pending)
553 stub_gravatar_request(user.email)
554 post user_new_path, :params => { :user => user.attributes }
555 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
556 confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token
558 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
559 assert_redirected_to new_diary_entry_path
562 def test_confirm_success_bad_token_with_referer
563 user = build(:user, :pending)
564 stub_gravatar_request(user.email)
565 post user_new_path, :params => { :user => user.attributes }
566 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
567 confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token
570 session_for(create(:user))
572 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
573 assert_redirected_to login_path(:referer => new_diary_entry_path)
574 assert_match(/Confirmed your account/, flash[:notice])
577 def test_confirm_expired_token
578 user = build(:user, :pending)
579 stub_gravatar_request(user.email)
580 post user_new_path, :params => { :user => user.attributes }
581 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
582 confirm_string = User.find_by(:email => user.email).tokens.create(:expiry => 1.day.ago).token
584 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
585 assert_redirected_to :action => "confirm"
586 assert_match(/confirmation code has expired/, flash[:error])
589 def test_confirm_already_confirmed
590 user = build(:user, :pending)
591 stub_gravatar_request(user.email)
592 post user_new_path, :params => { :user => user.attributes }
593 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
594 confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token
596 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
597 assert_redirected_to new_diary_entry_path
601 confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token
602 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
603 assert_redirected_to :action => "login"
604 assert_match(/already been confirmed/, flash[:error])
607 def test_confirm_resend_success
608 user = build(:user, :pending)
609 post user_new_path, :params => { :user => user.attributes }
610 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
612 assert_difference "ActionMailer::Base.deliveries.size", 1 do
613 perform_enqueued_jobs do
614 get user_confirm_resend_path(user)
618 assert_response :redirect
619 assert_redirected_to login_path
620 assert_match(/sent a new confirmation/, flash[:notice])
622 email = ActionMailer::Base.deliveries.last
624 assert_equal user.email, email.to.first
626 ActionMailer::Base.deliveries.clear
629 def test_confirm_resend_no_token
630 user = build(:user, :pending)
631 # only complete first half of registration
632 post user_new_path, :params => { :user => user.attributes }
634 assert_no_difference "ActionMailer::Base.deliveries.size" do
635 perform_enqueued_jobs do
636 get user_confirm_resend_path(user)
640 assert_response :redirect
641 assert_redirected_to login_path
642 assert_match "User #{user.display_name} not found.", flash[:error]
645 def test_confirm_resend_unknown_user
646 assert_no_difference "ActionMailer::Base.deliveries.size" do
647 perform_enqueued_jobs do
648 get user_confirm_resend_path(:display_name => "No Such User")
652 assert_response :redirect
653 assert_redirected_to login_path
654 assert_match "User No Such User not found.", flash[:error]
657 def test_confirm_email_get
659 confirm_string = user.tokens.create.token
661 get user_confirm_email_path, :params => { :confirm_string => confirm_string }
662 assert_response :success
663 assert_template :confirm_email
666 def test_confirm_email_success
667 user = create(:user, :new_email => "test-new@example.com")
668 stub_gravatar_request(user.new_email)
669 confirm_string = user.tokens.create.token
671 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
672 assert_response :redirect
673 assert_redirected_to :action => :account, :display_name => user.display_name
674 assert_match(/Confirmed your change of email address/, flash[:notice])
677 def test_confirm_email_already_confirmed
679 confirm_string = user.tokens.create.token
681 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
682 assert_response :redirect
683 assert_redirected_to :action => :account, :display_name => user.display_name
684 assert_match(/already been confirmed/, flash[:error])
687 def test_confirm_email_bad_token
688 post user_confirm_email_path, :params => { :confirm_string => "XXXXX" }
689 assert_response :success
690 assert_template :confirm_email
691 assert_match(/confirmation code has expired or does not exist/, flash[:error])
695 # test if testing for a gravatar works
696 # this happens when the email is actually changed
697 # which is triggered by the confirmation mail
698 def test_gravatar_auto_enable
699 # switch to email that has a gravatar
700 user = create(:user, :new_email => "test-new@example.com")
701 stub_gravatar_request(user.new_email, 200)
702 confirm_string = user.tokens.create.token
703 # precondition gravatar should be turned off
704 assert_not user.image_use_gravatar
705 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
706 assert_response :redirect
707 assert_redirected_to :action => :account, :display_name => user.display_name
708 assert_match(/Confirmed your change of email address/, flash[:notice])
709 # gravatar use should now be enabled
710 assert User.find(user.id).image_use_gravatar
713 def test_gravatar_auto_disable
714 # switch to email without a gravatar
715 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
716 stub_gravatar_request(user.new_email, 404)
717 confirm_string = user.tokens.create.token
718 # precondition gravatar should be turned on
719 assert user.image_use_gravatar
720 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
721 assert_response :redirect
722 assert_redirected_to :action => :account, :display_name => user.display_name
723 assert_match(/Confirmed your change of email address/, flash[:notice])
724 # gravatar use should now be disabled
725 assert_not User.find(user.id).image_use_gravatar
728 def test_terms_new_user
729 user = build(:user, :pending)
731 # Set up our user as being half-way through registration
732 assert_no_difference "User.count" do
733 assert_no_difference "ActionMailer::Base.deliveries.size" do
734 perform_enqueued_jobs do
735 post user_new_path, :params => { :user => user.attributes }
742 assert_response :success
743 assert_template :terms
746 def test_terms_agreed
747 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
752 assert_response :redirect
753 assert_redirected_to :action => :account, :display_name => user.display_name
756 def test_terms_not_seen_without_referer
757 user = create(:user, :terms_seen => false, :terms_agreed => nil)
762 assert_response :success
763 assert_template :terms
765 post user_save_path, :params => { :user => { :consider_pd => true }, :read_ct => 1, :read_tou => 1 }
766 assert_response :redirect
767 assert_redirected_to :action => :account, :display_name => user.display_name
768 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
772 assert user.consider_pd
773 assert_not_nil user.terms_agreed
774 assert user.terms_seen
777 def test_terms_not_seen_with_referer
778 user = create(:user, :terms_seen => false, :terms_agreed => nil)
782 get user_terms_path, :params => { :referer => "/test" }
783 assert_response :success
784 assert_template :terms
786 post user_save_path, :params => { :user => { :consider_pd => true }, :referer => "/test", :read_ct => 1, :read_tou => 1 }
787 assert_response :redirect
788 assert_redirected_to "/test"
789 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
793 assert user.consider_pd
794 assert_not_nil user.terms_agreed
795 assert user.terms_seen
798 # Check that if you haven't seen the terms, and make a request that requires authentication,
799 # that your request is redirected to view the terms
800 def test_terms_not_seen_redirection
801 user = create(:user, :terms_seen => false, :terms_agreed => nil)
804 get user_account_path(user)
805 assert_response :redirect
806 assert_redirected_to :action => :terms, :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
810 user = create(:user, :data_public => false)
813 post user_go_public_path
815 assert_response :redirect
816 assert_redirected_to :action => :account, :display_name => user.display_name
817 assert User.find(user.id).data_public
820 def test_lost_password
821 # Test fetching the lost password page
822 get user_forgot_password_path
823 assert_response :success
824 assert_template :lost_password
825 assert_select "div#notice", false
827 # Test resetting using the address as recorded for a user that has an
828 # address which is duplicated in a different case by another user
830 uppercase_user = build(:user, :email => user.email.upcase).tap { |u| u.save(:validate => false) }
832 assert_difference "ActionMailer::Base.deliveries.size", 1 do
833 perform_enqueued_jobs do
834 post user_forgot_password_path, :params => { :user => { :email => user.email } }
837 assert_response :redirect
838 assert_redirected_to :action => :login
839 assert_match(/^Sorry you lost it/, flash[:notice])
840 email = ActionMailer::Base.deliveries.first
841 assert_equal 1, email.to.count
842 assert_equal user.email, email.to.first
843 ActionMailer::Base.deliveries.clear
845 # Test resetting using an address that matches a different user
846 # that has the same address in a different case
847 assert_difference "ActionMailer::Base.deliveries.size", 1 do
848 perform_enqueued_jobs do
849 post user_forgot_password_path, :params => { :user => { :email => user.email.upcase } }
852 assert_response :redirect
853 assert_redirected_to :action => :login
854 assert_match(/^Sorry you lost it/, flash[:notice])
855 email = ActionMailer::Base.deliveries.first
856 assert_equal 1, email.to.count
857 assert_equal uppercase_user.email, email.to.first
858 ActionMailer::Base.deliveries.clear
860 # Test resetting using an address that is a case insensitive match
861 # for more than one user but not an exact match for either
862 assert_no_difference "ActionMailer::Base.deliveries.size" do
863 perform_enqueued_jobs do
864 post user_forgot_password_path, :params => { :user => { :email => user.email.titlecase } }
867 assert_response :success
868 assert_template :lost_password
869 assert_select ".error", /^Could not find that email address/
871 # Test resetting using the address as recorded for a user that has an
872 # address which is case insensitively unique
873 third_user = create(:user)
874 assert_difference "ActionMailer::Base.deliveries.size", 1 do
875 perform_enqueued_jobs do
876 post user_forgot_password_path, :params => { :user => { :email => third_user.email } }
879 assert_response :redirect
880 assert_redirected_to :action => :login
881 assert_match(/^Sorry you lost it/, flash[:notice])
882 email = ActionMailer::Base.deliveries.first
883 assert_equal 1, email.to.count
884 assert_equal third_user.email, email.to.first
885 ActionMailer::Base.deliveries.clear
887 # Test resetting using an address that matches a user that has the
888 # same (case insensitively unique) address in a different case
889 assert_difference "ActionMailer::Base.deliveries.size", 1 do
890 perform_enqueued_jobs do
891 post user_forgot_password_path, :params => { :user => { :email => third_user.email.upcase } }
894 assert_response :redirect
895 assert_redirected_to :action => :login
896 assert_match(/^Sorry you lost it/, flash[:notice])
897 email = ActionMailer::Base.deliveries.first
898 assert_equal 1, email.to.count
899 assert_equal third_user.email, email.to.first
900 ActionMailer::Base.deliveries.clear
903 def test_reset_password
904 user = create(:user, :pending)
905 # Test a request with no token
906 get user_reset_password_path
907 assert_response :bad_request
909 # Test a request with a bogus token
910 get user_reset_password_path, :params => { :token => "made_up_token" }
911 assert_response :redirect
912 assert_redirected_to :action => :lost_password
914 # Create a valid token for a user
915 token = user.tokens.create
917 # Test a request with a valid token
918 get user_reset_password_path, :params => { :token => token.token }
919 assert_response :success
920 assert_template :reset_password
922 # Test that errors are reported for erroneous submissions
923 post user_reset_password_path, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "different_password" } }
924 assert_response :success
925 assert_template :reset_password
926 assert_select "div#errorExplanation"
928 # Test setting a new password
929 post user_reset_password_path, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" } }
930 assert_response :redirect
931 assert_redirected_to root_path
932 assert_equal user.id, session[:user]
934 assert_equal "active", user.status
935 assert user.email_valid
936 assert_equal user, User.authenticate(:username => user.email, :password => "new_password")
940 # Get a user to work with - note that this user deliberately
941 # conflicts with uppercase_user in the email and display name
942 # fields to test that we can change other fields without any
943 # validation errors being reported
944 user = create(:user, :languages => [])
945 _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
947 # Make sure that you are redirected to the login page when
948 # you are not logged in
949 get user_account_path(user)
950 assert_response :redirect
951 assert_redirected_to :action => "login", :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
953 # Make sure that you are blocked when not logged in as the right user
954 session_for(create(:user))
955 get user_account_path(user)
956 assert_response :forbidden
958 # Make sure we get the page when we are logged in as the right user
960 get user_account_path(user)
961 assert_response :success
962 assert_template :account
963 assert_select "form#accountForm" do |form|
964 assert_equal "post", form.attr("method").to_s
965 assert_select "input[name='_method']", false
966 assert_equal "/user/#{ERB::Util.u(user.display_name)}/account", form.attr("action").to_s
969 # Updating the description should work
970 user.description = "new description"
971 user.preferred_editor = "default"
972 post user_account_path(user), :params => { :user => user.attributes }
973 assert_response :success
974 assert_template :account
975 assert_select "div#errorExplanation", false
976 assert_select ".notice", /^User information updated successfully/
977 assert_select "form#accountForm > fieldset > div.standard-form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
979 # Changing to a invalid editor should fail
980 user.preferred_editor = "unknown"
981 post user_account_path(user), :params => { :user => user.attributes }
982 assert_response :success
983 assert_template :account
984 assert_select ".notice", false
985 assert_select "div#errorExplanation"
986 assert_select "form#accountForm > fieldset > div.standard-form-row > select#user_preferred_editor > option[selected]", false
988 # Changing to a valid editor should work
989 user.preferred_editor = "potlatch2"
990 post user_account_path(user), :params => { :user => user.attributes }
991 assert_response :success
992 assert_template :account
993 assert_select "div#errorExplanation", false
994 assert_select ".notice", /^User information updated successfully/
995 assert_select "form#accountForm > fieldset > div.standard-form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
997 # Changing to the default editor should work
998 user.preferred_editor = "default"
999 post user_account_path(user), :params => { :user => user.attributes }
1000 assert_response :success
1001 assert_template :account
1002 assert_select "div#errorExplanation", false
1003 assert_select ".notice", /^User information updated successfully/
1004 assert_select "form#accountForm > fieldset > div.standard-form-row > select#user_preferred_editor > option[selected]", false
1006 # Changing to an uploaded image should work
1007 image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif")
1008 post user_account_path(user), :params => { :avatar_action => "new", :user => user.attributes.merge(:avatar => image) }
1009 assert_response :success
1010 assert_template :account
1011 assert_select "div#errorExplanation", false
1012 assert_select ".notice", /^User information updated successfully/
1013 assert_select "form#accountForm > fieldset > div.standard-form-row.accountImage input[name=avatar_action][checked][value=?]", "keep"
1015 # Changing to a gravatar image should work
1016 post user_account_path(user), :params => { :avatar_action => "gravatar", :user => user.attributes }
1017 assert_response :success
1018 assert_template :account
1019 assert_select "div#errorExplanation", false
1020 assert_select ".notice", /^User information updated successfully/
1021 assert_select "form#accountForm > fieldset > div.standard-form-row.accountImage input[name=avatar_action][checked][value=?]", "gravatar"
1023 # Removing the image should work
1024 post user_account_path(user), :params => { :avatar_action => "delete", :user => user.attributes }
1025 assert_response :success
1026 assert_template :account
1027 assert_select "div#errorExplanation", false
1028 assert_select ".notice", /^User information updated successfully/
1029 assert_select "form#accountForm > fieldset > div.standard-form-row.accountImage input[name=avatar_action][checked]", false
1031 # Adding external authentication should redirect to the auth provider
1032 post user_account_path(user), :params => { :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }
1033 assert_response :redirect
1034 assert_redirected_to auth_path(:provider => "openid", :openid_url => "https://www.google.com/accounts/o8/id", :origin => "/user/#{ERB::Util.u(user.display_name)}/account")
1036 # Changing name to one that exists should fail
1037 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
1038 post user_account_path(user), :params => { :user => new_attributes }
1039 assert_response :success
1040 assert_template :account
1041 assert_select ".notice", false
1042 assert_select "div#errorExplanation"
1043 assert_select "form#accountForm > fieldset > div.standard-form-row > input.field_with_errors#user_display_name"
1045 # Changing name to one that exists should fail, regardless of case
1046 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
1047 post user_account_path(user), :params => { :user => new_attributes }
1048 assert_response :success
1049 assert_template :account
1050 assert_select ".notice", false
1051 assert_select "div#errorExplanation"
1052 assert_select "form#accountForm > fieldset > div.standard-form-row > input.field_with_errors#user_display_name"
1054 # Changing name to one that doesn't exist should work
1055 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
1056 post user_account_path(user), :params => { :user => new_attributes }
1057 assert_response :success
1058 assert_template :account
1059 assert_select "div#errorExplanation", false
1060 assert_select ".notice", /^User information updated successfully/
1061 assert_select "form#accountForm > fieldset > div.standard-form-row > input#user_display_name[value=?]", "new tester"
1063 # Record the change of name
1064 user.display_name = "new tester"
1066 # Changing email to one that exists should fail
1067 user.new_email = create(:user).email
1068 assert_no_difference "ActionMailer::Base.deliveries.size" do
1069 perform_enqueued_jobs do
1070 post user_account_path(user), :params => { :user => user.attributes }
1073 assert_response :success
1074 assert_template :account
1075 assert_select ".notice", false
1076 assert_select "div#errorExplanation"
1077 assert_select "form#accountForm > fieldset > div.standard-form-row > input.field_with_errors#user_new_email"
1079 # Changing email to one that exists should fail, regardless of case
1080 user.new_email = create(:user).email.upcase
1081 assert_no_difference "ActionMailer::Base.deliveries.size" do
1082 perform_enqueued_jobs do
1083 post user_account_path(user), :params => { :user => user.attributes }
1086 assert_response :success
1087 assert_template :account
1088 assert_select ".notice", false
1089 assert_select "div#errorExplanation"
1090 assert_select "form#accountForm > fieldset > div.standard-form-row > input.field_with_errors#user_new_email"
1092 # Changing email to one that doesn't exist should work
1093 user.new_email = "new_tester@example.com"
1094 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1095 perform_enqueued_jobs do
1096 post user_account_path(user), :params => { :user => user.attributes }
1099 assert_response :success
1100 assert_template :account
1101 assert_select "div#errorExplanation", false
1102 assert_select ".notice", /^User information updated successfully/
1103 assert_select "form#accountForm > fieldset > div.standard-form-row > input#user_new_email[value=?]", user.new_email
1104 email = ActionMailer::Base.deliveries.first
1105 assert_equal 1, email.to.count
1106 assert_equal user.new_email, email.to.first
1107 ActionMailer::Base.deliveries.clear
1110 # Check that the user account page will display and contains some relevant
1111 # information for the user
1113 # Test a non-existent user
1114 get user_path(:display_name => "unknown")
1115 assert_response :not_found
1117 # Test a normal user
1118 user = create(:user, :home_lon => 1.1, :home_lat => 1.1)
1119 friend_user = create(:user, :home_lon => 1.2, :home_lat => 1.2)
1120 create(:friendship, :befriender => user, :befriendee => friend_user)
1121 create(:changeset, :user => friend_user)
1124 assert_response :success
1125 assert_select "div#userinformation" do
1126 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1127 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1128 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1129 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1130 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1131 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1132 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1133 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1136 # Friends shouldn't be visible as we're not logged in
1137 assert_select "div#friends-container", :count => 0
1139 # Test a user who has been blocked
1140 blocked_user = create(:user)
1141 create(:user_block, :user => blocked_user)
1142 get user_path(blocked_user)
1143 assert_response :success
1144 assert_select "div#userinformation" do
1145 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
1146 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
1147 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
1148 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
1149 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
1150 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
1151 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
1152 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
1155 # Test a moderator who has applied blocks
1156 moderator_user = create(:moderator_user)
1157 create(:user_block, :creator => moderator_user)
1158 get user_path(moderator_user)
1159 assert_response :success
1160 assert_select "div#userinformation" do
1161 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
1162 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
1163 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
1164 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
1165 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
1166 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
1167 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
1168 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
1171 # Login as a normal user
1174 # Test the normal user
1176 assert_response :success
1177 assert_select "div#userinformation" do
1178 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1179 assert_select "a[href='/traces/mine']", 1
1180 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1181 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1182 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 1
1183 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1184 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1185 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1188 # Friends should be visible as we're now logged in
1189 assert_select "div#friends-container" do
1190 assert_select "div.contact-activity", :count => 1
1193 # Login as a moderator
1194 session_for(create(:moderator_user))
1196 # Test the normal user
1198 assert_response :success
1199 assert_select "div#userinformation" do
1200 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1201 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1202 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1203 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1204 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1205 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1206 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1207 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
1211 # Test whether information about contributor terms is shown for users who haven't agreed
1212 def test_terms_not_agreed
1213 agreed_user = create(:user, :terms_agreed => 3.days.ago)
1214 seen_user = create(:user, :terms_seen => true, :terms_agreed => nil)
1215 not_seen_user = create(:user, :terms_seen => false, :terms_agreed => nil)
1217 get user_path(agreed_user)
1218 assert_response :success
1219 assert_select "div#userinformation" do
1220 assert_select "p", :count => 0, :text => /Contributor terms/
1223 get user_path(seen_user)
1224 assert_response :success
1225 # put @response.body
1226 assert_select "div#userinformation" do
1227 assert_select "p", :count => 1, :text => /Contributor terms/
1228 assert_select "p", /Declined/
1231 get user_path(not_seen_user)
1232 assert_response :success
1233 assert_select "div#userinformation" do
1234 assert_select "p", :count => 1, :text => /Contributor terms/
1235 assert_select "p", /Undecided/
1239 def test_make_friend
1240 # Get users to work with
1241 user = create(:user)
1242 friend = create(:user)
1244 # Check that the users aren't already friends
1245 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1247 # When not logged in a GET should ask us to login
1248 get make_friend_path(friend)
1249 assert_redirected_to :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1251 # When not logged in a POST should error
1252 post make_friend_path(friend)
1253 assert_response :forbidden
1254 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1258 # When logged in a GET should get a confirmation page
1259 get make_friend_path(friend)
1260 assert_response :success
1261 assert_template :make_friend
1262 assert_select "form" do
1263 assert_select "input[type='hidden'][name='referer']", 0
1264 assert_select "input[type='submit']", 1
1266 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1268 # When logged in a POST should add the friendship
1269 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1270 perform_enqueued_jobs do
1271 post make_friend_path(friend)
1274 assert_redirected_to user_path(friend)
1275 assert_match(/is now your friend/, flash[:notice])
1276 assert Friendship.where(:befriender => user, :befriendee => friend).first
1277 email = ActionMailer::Base.deliveries.first
1278 assert_equal 1, email.to.count
1279 assert_equal friend.email, email.to.first
1280 ActionMailer::Base.deliveries.clear
1282 # A second POST should report that the friendship already exists
1283 assert_no_difference "ActionMailer::Base.deliveries.size" do
1284 perform_enqueued_jobs do
1285 post make_friend_path(friend)
1288 assert_redirected_to user_path(friend)
1289 assert_match(/You are already friends with/, flash[:warning])
1290 assert Friendship.where(:befriender => user, :befriendee => friend).first
1293 def test_make_friend_with_referer
1294 # Get users to work with
1295 user = create(:user)
1296 friend = create(:user)
1299 # Check that the users aren't already friends
1300 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1302 # The GET should preserve any referer
1303 get make_friend_path(friend), :params => { :referer => "/test" }
1304 assert_response :success
1305 assert_template :make_friend
1306 assert_select "form" do
1307 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1308 assert_select "input[type='submit']", 1
1310 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1312 # When logged in a POST should add the friendship and refer us
1313 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1314 perform_enqueued_jobs do
1315 post make_friend_path(friend), :params => { :referer => "/test" }
1318 assert_redirected_to "/test"
1319 assert_match(/is now your friend/, flash[:notice])
1320 assert Friendship.where(:befriender => user, :befriendee => friend).first
1321 email = ActionMailer::Base.deliveries.first
1322 assert_equal 1, email.to.count
1323 assert_equal friend.email, email.to.first
1324 ActionMailer::Base.deliveries.clear
1327 def test_make_friend_unkown_user
1328 # Should error when a bogus user is specified
1329 session_for(create(:user))
1330 get make_friend_path(:display_name => "No Such User")
1331 assert_response :not_found
1332 assert_template :no_such_user
1335 def test_remove_friend
1336 # Get users to work with
1337 user = create(:user)
1338 friend = create(:user)
1339 create(:friendship, :befriender => user, :befriendee => friend)
1341 # Check that the users are friends
1342 assert Friendship.where(:befriender => user, :befriendee => friend).first
1344 # When not logged in a GET should ask us to login
1345 get remove_friend_path(friend)
1346 assert_redirected_to :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1348 # When not logged in a POST should error
1349 post remove_friend_path, :params => { :display_name => friend.display_name }
1350 assert_response :forbidden
1351 assert Friendship.where(:befriender => user, :befriendee => friend).first
1355 # When logged in a GET should get a confirmation page
1356 get remove_friend_path(friend)
1357 assert_response :success
1358 assert_template :remove_friend
1359 assert_select "form" do
1360 assert_select "input[type='hidden'][name='referer']", 0
1361 assert_select "input[type='submit']", 1
1363 assert Friendship.where(:befriender => user, :befriendee => friend).first
1365 # When logged in a POST should remove the friendship
1366 post remove_friend_path(friend)
1367 assert_redirected_to user_path(friend)
1368 assert_match(/was removed from your friends/, flash[:notice])
1369 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1371 # A second POST should report that the friendship does not exist
1372 post remove_friend_path(friend)
1373 assert_redirected_to user_path(friend)
1374 assert_match(/is not one of your friends/, flash[:error])
1375 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1378 def test_remove_friend_with_referer
1379 # Get users to work with
1380 user = create(:user)
1381 friend = create(:user)
1382 create(:friendship, :befriender => user, :befriendee => friend)
1385 # Check that the users are friends
1386 assert Friendship.where(:befriender => user, :befriendee => friend).first
1388 # The GET should preserve any referer
1389 get remove_friend_path(friend), :params => { :referer => "/test" }
1390 assert_response :success
1391 assert_template :remove_friend
1392 assert_select "form" do
1393 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1394 assert_select "input[type='submit']", 1
1396 assert Friendship.where(:befriender => user, :befriendee => friend).first
1398 # When logged in a POST should remove the friendship and refer
1399 post remove_friend_path(friend), :params => { :referer => "/test" }
1400 assert_redirected_to "/test"
1401 assert_match(/was removed from your friends/, flash[:notice])
1402 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1405 def test_remove_friend_unkown_user
1406 # Should error when a bogus user is specified
1407 session_for(create(:user))
1408 get remove_friend_path(:display_name => "No Such User")
1409 assert_response :not_found
1410 assert_template :no_such_user
1414 user = create(:user)
1416 # Try without logging in
1417 get set_status_user_path(user), :params => { :status => "suspended" }
1418 assert_response :redirect
1419 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1421 # Now try as a normal user
1423 get set_status_user_path(user), :params => { :status => "suspended" }
1424 assert_response :redirect
1425 assert_redirected_to :controller => :errors, :action => :forbidden
1427 # Finally try as an administrator
1428 session_for(create(:administrator_user))
1429 get set_status_user_path(user), :params => { :status => "suspended" }
1430 assert_response :redirect
1431 assert_redirected_to :action => :show, :display_name => user.display_name
1432 assert_equal "suspended", User.find(user.id).status
1436 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1438 # Try without logging in
1439 get delete_user_path(user), :params => { :status => "suspended" }
1440 assert_response :redirect
1441 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1443 # Now try as a normal user
1445 get delete_user_path(user), :params => { :status => "suspended" }
1446 assert_response :redirect
1447 assert_redirected_to :controller => :errors, :action => :forbidden
1449 # Finally try as an administrator
1450 session_for(create(:administrator_user))
1451 get delete_user_path(user), :params => { :status => "suspended" }
1452 assert_response :redirect
1453 assert_redirected_to :action => :show, :display_name => user.display_name
1455 # Check that the user was deleted properly
1457 assert_equal "user_#{user.id}", user.display_name
1458 assert_equal "", user.description
1459 assert_nil user.home_lat
1460 assert_nil user.home_lon
1461 assert_not user.avatar.attached?
1462 assert_not user.email_valid
1463 assert_nil user.new_email
1464 assert_nil user.auth_provider
1465 assert_nil user.auth_uid
1466 assert_equal "deleted", user.status
1470 user = create(:user)
1471 moderator_user = create(:moderator_user)
1472 administrator_user = create(:administrator_user)
1473 _suspended_user = create(:user, :suspended)
1474 _ip_user = create(:user, :creation_ip => "1.2.3.4")
1476 # There are now 7 users - the five above, plus two extra "granters" for the
1477 # moderator_user and administrator_user
1478 assert_equal 7, User.count
1480 # Shouldn't work when not logged in
1482 assert_response :redirect
1483 assert_redirected_to :action => :login, :referer => users_path
1487 # Shouldn't work when logged in as a normal user
1489 assert_response :redirect
1490 assert_redirected_to :controller => :errors, :action => :forbidden
1492 session_for(moderator_user)
1494 # Shouldn't work when logged in as a moderator
1496 assert_response :redirect
1497 assert_redirected_to :controller => :errors, :action => :forbidden
1499 session_for(administrator_user)
1501 # Note there is a header row, so all row counts are users + 1
1502 # Should work when logged in as an administrator
1504 assert_response :success
1505 assert_template :index
1506 assert_select "table#user_list tr", :count => 7 + 1
1508 # Should be able to limit by status
1509 get users_path, :params => { :status => "suspended" }
1510 assert_response :success
1511 assert_template :index
1512 assert_select "table#user_list tr", :count => 1 + 1
1514 # Should be able to limit by IP address
1515 get users_path, :params => { :ip => "1.2.3.4" }
1516 assert_response :success
1517 assert_template :index
1518 assert_select "table#user_list tr", :count => 1 + 1
1521 def test_index_get_paginated
1522 1.upto(100).each do |n|
1523 User.create(:display_name => "extra_#{n}",
1524 :email => "extra#{n}@example.com",
1525 :pass_crypt => "extraextra")
1528 session_for(create(:administrator_user))
1530 # 100 examples, an administrator, and a granter for the admin.
1531 assert_equal 102, User.count
1534 assert_response :success
1535 assert_template :index
1536 assert_select "table#user_list tr", :count => 51
1538 get users_path, :params => { :page => 2 }
1539 assert_response :success
1540 assert_template :index
1541 assert_select "table#user_list tr", :count => 51
1543 get users_path, :params => { :page => 3 }
1544 assert_response :success
1545 assert_template :index
1546 assert_select "table#user_list tr", :count => 3
1549 def test_index_post_confirm
1550 inactive_user = create(:user, :pending)
1551 suspended_user = create(:user, :suspended)
1553 # Shouldn't work when not logged in
1554 assert_no_difference "User.active.count" do
1555 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1557 assert_response :forbidden
1559 assert_equal "pending", inactive_user.reload.status
1560 assert_equal "suspended", suspended_user.reload.status
1562 session_for(create(:user))
1564 # Shouldn't work when logged in as a normal user
1565 assert_no_difference "User.active.count" do
1566 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1568 assert_response :redirect
1569 assert_redirected_to :controller => :errors, :action => :forbidden
1570 assert_equal "pending", inactive_user.reload.status
1571 assert_equal "suspended", suspended_user.reload.status
1573 session_for(create(:moderator_user))
1575 # Shouldn't work when logged in as a moderator
1576 assert_no_difference "User.active.count" do
1577 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1579 assert_response :redirect
1580 assert_redirected_to :controller => :errors, :action => :forbidden
1581 assert_equal "pending", inactive_user.reload.status
1582 assert_equal "suspended", suspended_user.reload.status
1584 session_for(create(:administrator_user))
1586 # Should work when logged in as an administrator
1587 assert_difference "User.active.count", 2 do
1588 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1590 assert_response :redirect
1591 assert_redirected_to :action => :index
1592 assert_equal "confirmed", inactive_user.reload.status
1593 assert_equal "confirmed", suspended_user.reload.status
1596 def test_index_post_hide
1597 normal_user = create(:user)
1598 confirmed_user = create(:user, :confirmed)
1600 # Shouldn't work when not logged in
1601 assert_no_difference "User.active.count" do
1602 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1604 assert_response :forbidden
1606 assert_equal "active", normal_user.reload.status
1607 assert_equal "confirmed", confirmed_user.reload.status
1609 session_for(create(:user))
1611 # Shouldn't work when logged in as a normal user
1612 assert_no_difference "User.active.count" do
1613 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1615 assert_response :redirect
1616 assert_redirected_to :controller => :errors, :action => :forbidden
1617 assert_equal "active", normal_user.reload.status
1618 assert_equal "confirmed", confirmed_user.reload.status
1620 session_for(create(:moderator_user))
1622 # Shouldn't work when logged in as a moderator
1623 assert_no_difference "User.active.count" do
1624 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1626 assert_response :redirect
1627 assert_redirected_to :controller => :errors, :action => :forbidden
1628 assert_equal "active", normal_user.reload.status
1629 assert_equal "confirmed", confirmed_user.reload.status
1631 session_for(create(:administrator_user))
1633 # Should work when logged in as an administrator
1634 assert_difference "User.active.count", -2 do
1635 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1637 assert_response :redirect
1638 assert_redirected_to :action => :index
1639 assert_equal "deleted", normal_user.reload.status
1640 assert_equal "deleted", confirmed_user.reload.status