3 class UsersControllerTest < ActionController::TestCase
11 # test all routes which lead to this controller
14 { :path => "/login", :method => :get },
15 { :controller => "users", :action => "login" }
18 { :path => "/login", :method => :post },
19 { :controller => "users", :action => "login" }
22 { :controller => "users", :action => "login", :format => "html" },
23 { :path => "/login.html", :method => :get }
27 { :path => "/logout", :method => :get },
28 { :controller => "users", :action => "logout" }
31 { :path => "/logout", :method => :post },
32 { :controller => "users", :action => "logout" }
35 { :controller => "users", :action => "logout", :format => "html" },
36 { :path => "/logout.html", :method => :get }
40 { :path => "/user/new", :method => :get },
41 { :controller => "users", :action => "new" }
45 { :path => "/user/new", :method => :post },
46 { :controller => "users", :action => "create" }
50 { :path => "/user/terms", :method => :get },
51 { :controller => "users", :action => "terms" }
55 { :path => "/user/save", :method => :post },
56 { :controller => "users", :action => "save" }
60 { :path => "/user/username/confirm", :method => :get },
61 { :controller => "users", :action => "confirm", :display_name => "username" }
64 { :path => "/user/username/confirm", :method => :post },
65 { :controller => "users", :action => "confirm", :display_name => "username" }
68 { :path => "/user/username/confirm/resend", :method => :get },
69 { :controller => "users", :action => "confirm_resend", :display_name => "username" }
73 { :path => "/user/confirm", :method => :get },
74 { :controller => "users", :action => "confirm" }
77 { :path => "/user/confirm", :method => :post },
78 { :controller => "users", :action => "confirm" }
81 { :path => "/user/confirm-email", :method => :get },
82 { :controller => "users", :action => "confirm_email" }
85 { :path => "/user/confirm-email", :method => :post },
86 { :controller => "users", :action => "confirm_email" }
90 { :path => "/user/go_public", :method => :post },
91 { :controller => "users", :action => "go_public" }
95 { :path => "/user/forgot-password", :method => :get },
96 { :controller => "users", :action => "lost_password" }
99 { :path => "/user/forgot-password", :method => :post },
100 { :controller => "users", :action => "lost_password" }
103 { :path => "/user/reset-password", :method => :get },
104 { :controller => "users", :action => "reset_password" }
107 { :path => "/user/reset-password", :method => :post },
108 { :controller => "users", :action => "reset_password" }
112 { :path => "/user/suspended", :method => :get },
113 { :controller => "users", :action => "suspended" }
117 { :path => "/user/username", :method => :get },
118 { :controller => "users", :action => "show", :display_name => "username" }
122 { :path => "/user/username/account", :method => :get },
123 { :controller => "users", :action => "account", :display_name => "username" }
126 { :path => "/user/username/account", :method => :post },
127 { :controller => "users", :action => "account", :display_name => "username" }
131 { :path => "/user/username/make_friend", :method => :get },
132 { :controller => "users", :action => "make_friend", :display_name => "username" }
135 { :path => "/user/username/make_friend", :method => :post },
136 { :controller => "users", :action => "make_friend", :display_name => "username" }
139 { :path => "/user/username/remove_friend", :method => :get },
140 { :controller => "users", :action => "remove_friend", :display_name => "username" }
143 { :path => "/user/username/remove_friend", :method => :post },
144 { :controller => "users", :action => "remove_friend", :display_name => "username" }
148 { :path => "/user/username/set_status", :method => :get },
149 { :controller => "users", :action => "set_status", :display_name => "username" }
152 { :path => "/user/username/delete", :method => :get },
153 { :controller => "users", :action => "delete", :display_name => "username" }
157 { :path => "/users", :method => :get },
158 { :controller => "users", :action => "index" }
161 { :path => "/users", :method => :post },
162 { :controller => "users", :action => "index" }
165 { :path => "/users/status", :method => :get },
166 { :controller => "users", :action => "index", :status => "status" }
169 { :path => "/users/status", :method => :post },
170 { :controller => "users", :action => "index", :status => "status" }
174 # The user creation page loads
177 assert_response :redirect
178 assert_redirected_to user_new_path(:cookie_test => "true")
180 get :new, :params => { :cookie_test => "true" }, :session => { :cookie_test => true }
181 assert_response :success
183 assert_select "html", :count => 1 do
184 assert_select "head", :count => 1 do
185 assert_select "title", :text => /Sign Up/, :count => 1
187 assert_select "body", :count => 1 do
188 assert_select "div#content", :count => 1 do
189 assert_select "form[action='/user/new'][method='post']", :count => 1 do
190 assert_select "input[id='user_email']", :count => 1
191 assert_select "input[id='user_email_confirmation']", :count => 1
192 assert_select "input[id='user_display_name']", :count => 1
193 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
194 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
195 assert_select "input[type='submit'][value='Sign Up']", :count => 1
202 def test_new_view_logged_in
203 session[:user] = create(:user).id
206 assert_response :redirect
207 assert_redirected_to user_new_path(:cookie_test => "true")
208 get :new, :params => { :cookie_test => "true" }
209 assert_response :redirect
210 assert_redirected_to root_path
212 get :new, :params => { :referer => "/test" }
213 assert_response :redirect
214 assert_redirected_to user_new_path(:referer => "/test", :cookie_test => "true")
215 get :new, :params => { :referer => "/test", :cookie_test => "true" }
216 assert_response :redirect
217 assert_redirected_to "/test"
221 user = build(:user, :pending)
223 assert_difference "User.count", 1 do
224 assert_difference "ActionMailer::Base.deliveries.size", 1 do
225 perform_enqueued_jobs do
226 post :save, :session => { :new_user => user }, :params => { :read_ct => 1, :read_tou => 1 }
232 register_email = ActionMailer::Base.deliveries.first
234 assert_equal register_email.to[0], user.email
235 assert_match(/#{@url}/, register_email.body.to_s)
238 assert_redirected_to :action => "confirm", :display_name => user.display_name
240 ActionMailer::Base.deliveries.clear
243 def test_new_duplicate_email
244 user = build(:user, :pending)
245 user.email = create(:user).email
247 assert_no_difference "User.count" do
248 assert_no_difference "ActionMailer::Base.deliveries.size" do
249 perform_enqueued_jobs do
250 post :save, :session => { :new_user => user }, :params => { :read_ct => 1, :read_tou => 1 }
255 assert_response :success
256 assert_template "new"
257 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
260 def test_new_duplicate_email_uppercase
261 user = build(:user, :pending)
262 user.email = create(:user).email.upcase
264 assert_no_difference "User.count" do
265 assert_no_difference "ActionMailer::Base.deliveries.size" do
266 perform_enqueued_jobs do
267 post :save, :session => { :new_user => user }, :params => { :read_ct => 1, :read_tou => 1 }
272 assert_response :success
273 assert_template "new"
274 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
277 def test_new_duplicate_name
278 user = build(:user, :pending)
279 user.display_name = create(:user).display_name
281 assert_no_difference "User.count" do
282 assert_no_difference "ActionMailer::Base.deliveries.size" do
283 perform_enqueued_jobs do
284 post :save, :session => { :new_user => user }, :params => { :read_ct => 1, :read_tou => 1 }
289 assert_response :success
290 assert_template "new"
291 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
294 def test_new_duplicate_name_uppercase
295 user = build(:user, :pending)
296 user.display_name = create(:user).display_name.upcase
298 assert_no_difference "User.count" do
299 assert_no_difference "ActionMailer::Base.deliveries.size" do
300 perform_enqueued_jobs do
301 post :save, :session => { :new_user => user }, :params => { :read_ct => 1, :read_tou => 1 }
306 assert_response :success
307 assert_template "new"
308 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
311 def test_new_blocked_domain
312 user = build(:user, :pending, :email => "user@example.net")
313 create(:acl, :domain => "example.net", :k => "no_account_creation")
315 assert_no_difference "User.count" do
316 assert_no_difference "ActionMailer::Base.deliveries.size" do
317 perform_enqueued_jobs do
318 post :save, :session => { :new_user => user }, :params => { :read_ct => 1, :read_tou => 1 }
323 assert_response :success
324 assert_template "blocked"
327 def test_save_referer_params
328 user = build(:user, :pending)
330 assert_difference "User.count", 1 do
331 assert_difference "ActionMailer::Base.deliveries.size", 1 do
332 perform_enqueued_jobs do
333 post :save, :session => { :new_user => user,
334 :referer => "/edit?editor=id#map=1/2/3" },
335 :params => { :read_ct => 1, :read_tou => 1 }
340 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
341 user.tokens.order("id DESC").first.referer
343 ActionMailer::Base.deliveries.clear
346 def test_logout_without_referer
348 assert_response :redirect
349 assert_redirected_to root_path
352 def test_logout_with_referer
353 post :logout, :params => { :referer => "/test" }
354 assert_response :redirect
355 assert_redirected_to "/test"
358 def test_logout_with_token
359 token = create(:user).tokens.create
361 session[:token] = token.token
364 assert_response :redirect
365 assert_redirected_to root_path
366 assert_nil session[:token]
367 assert_nil UserToken.where(:id => token.id).first
371 user = create(:user, :pending)
372 confirm_string = user.tokens.create.token
374 @request.cookies["_osm_session"] = user.display_name
375 get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
376 assert_response :success
377 assert_template :confirm
380 def test_confirm_get_already_confirmed
382 confirm_string = user.tokens.create.token
384 @request.cookies["_osm_session"] = user.display_name
385 get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
386 assert_response :redirect
387 assert_redirected_to root_path
390 def test_confirm_success_no_token_no_referer
391 user = create(:user, :pending)
392 stub_gravatar_request(user.email)
393 confirm_string = user.tokens.create.token
395 @request.cookies["_osm_session"] = user.display_name
396 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
397 assert_redirected_to login_path
398 assert_match(/Confirmed your account/, flash[:notice])
401 def test_confirm_success_good_token_no_referer
402 user = create(:user, :pending)
403 stub_gravatar_request(user.email)
404 confirm_string = user.tokens.create.token
405 token = user.tokens.create.token
407 @request.cookies["_osm_session"] = user.display_name
408 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
409 assert_redirected_to welcome_path
412 def test_confirm_success_bad_token_no_referer
413 user = create(:user, :pending)
414 stub_gravatar_request(user.email)
415 confirm_string = user.tokens.create.token
416 token = create(:user).tokens.create.token
418 @request.cookies["_osm_session"] = user.display_name
419 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
420 assert_redirected_to login_path
421 assert_match(/Confirmed your account/, flash[:notice])
424 def test_confirm_success_no_token_with_referer
425 user = create(:user, :pending)
426 stub_gravatar_request(user.email)
427 confirm_string = user.tokens.create(:referer => new_diary_entry_path).token
429 @request.cookies["_osm_session"] = user.display_name
430 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
431 assert_redirected_to login_path(:referer => new_diary_entry_path)
432 assert_match(/Confirmed your account/, flash[:notice])
435 def test_confirm_success_good_token_with_referer
436 user = create(:user, :pending)
437 stub_gravatar_request(user.email)
438 confirm_string = user.tokens.create(:referer => new_diary_entry_path).token
439 token = user.tokens.create.token
441 @request.cookies["_osm_session"] = user.display_name
442 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
443 assert_redirected_to new_diary_entry_path
446 def test_confirm_success_bad_token_with_referer
447 user = create(:user, :pending)
448 stub_gravatar_request(user.email)
449 confirm_string = user.tokens.create(:referer => new_diary_entry_path).token
450 token = create(:user).tokens.create.token
452 @request.cookies["_osm_session"] = user.display_name
453 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
454 assert_redirected_to login_path(:referer => new_diary_entry_path)
455 assert_match(/Confirmed your account/, flash[:notice])
458 def test_confirm_expired_token
459 user = create(:user, :pending)
460 confirm_string = user.tokens.create(:expiry => 1.day.ago).token
462 @request.cookies["_osm_session"] = user.display_name
463 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
464 assert_redirected_to :action => "confirm"
465 assert_match(/confirmation code has expired/, flash[:error])
468 def test_confirm_already_confirmed
470 confirm_string = user.tokens.create(:referer => new_diary_entry_path).token
472 @request.cookies["_osm_session"] = user.display_name
473 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
474 assert_redirected_to :action => "login"
475 assert_match(/already been confirmed/, flash[:error])
478 def test_confirm_resend_success
479 user = create(:user, :pending)
480 session[:token] = user.tokens.create.token
482 assert_difference "ActionMailer::Base.deliveries.size", 1 do
483 perform_enqueued_jobs do
484 get :confirm_resend, :params => { :display_name => user.display_name }
488 assert_response :redirect
489 assert_redirected_to login_path
490 assert_match(/sent a new confirmation/, flash[:notice])
492 email = ActionMailer::Base.deliveries.last
494 assert_equal user.email, email.to.first
496 ActionMailer::Base.deliveries.clear
499 def test_confirm_resend_no_token
500 user = create(:user, :pending)
501 assert_no_difference "ActionMailer::Base.deliveries.size" do
502 perform_enqueued_jobs do
503 get :confirm_resend, :params => { :display_name => user.display_name }
507 assert_response :redirect
508 assert_redirected_to login_path
509 assert_match "User #{user.display_name} not found.", flash[:error]
512 def test_confirm_resend_unknown_user
513 assert_no_difference "ActionMailer::Base.deliveries.size" do
514 perform_enqueued_jobs do
515 get :confirm_resend, :params => { :display_name => "No Such User" }
519 assert_response :redirect
520 assert_redirected_to login_path
521 assert_match "User No Such User not found.", flash[:error]
524 def test_confirm_email_get
526 confirm_string = user.tokens.create.token
528 get :confirm_email, :params => { :confirm_string => confirm_string }
529 assert_response :success
530 assert_template :confirm_email
533 def test_confirm_email_success
534 user = create(:user, :new_email => "test-new@example.com")
535 stub_gravatar_request(user.new_email)
536 confirm_string = user.tokens.create.token
538 post :confirm_email, :params => { :confirm_string => confirm_string }
539 assert_response :redirect
540 assert_redirected_to :action => :account, :display_name => user.display_name
541 assert_match(/Confirmed your change of email address/, flash[:notice])
544 def test_confirm_email_already_confirmed
546 confirm_string = user.tokens.create.token
548 post :confirm_email, :params => { :confirm_string => confirm_string }
549 assert_response :redirect
550 assert_redirected_to :action => :account, :display_name => user.display_name
551 assert_match(/already been confirmed/, flash[:error])
554 def test_confirm_email_bad_token
555 post :confirm_email, :params => { :confirm_string => "XXXXX" }
556 assert_response :success
557 assert_template :confirm_email
558 assert_match(/confirmation code has expired or does not exist/, flash[:error])
562 # test if testing for a gravatar works
563 # this happens when the email is actually changed
564 # which is triggered by the confirmation mail
565 def test_gravatar_auto_enable
566 # switch to email that has a gravatar
567 user = create(:user, :new_email => "test-new@example.com")
568 stub_gravatar_request(user.new_email, 200)
569 confirm_string = user.tokens.create.token
570 # precondition gravatar should be turned off
571 assert_not user.image_use_gravatar
572 post :confirm_email, :params => { :confirm_string => confirm_string }
573 assert_response :redirect
574 assert_redirected_to :action => :account, :display_name => user.display_name
575 assert_match(/Confirmed your change of email address/, flash[:notice])
576 # gravatar use should now be enabled
577 assert User.find(user.id).image_use_gravatar
580 def test_gravatar_auto_disable
581 # switch to email without a gravatar
582 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
583 stub_gravatar_request(user.new_email, 404)
584 confirm_string = user.tokens.create.token
585 # precondition gravatar should be turned on
586 assert user.image_use_gravatar
587 post :confirm_email, :params => { :confirm_string => confirm_string }
588 assert_response :redirect
589 assert_redirected_to :action => :account, :display_name => user.display_name
590 assert_match(/Confirmed your change of email address/, flash[:notice])
591 # gravatar use should now be disabled
592 assert_not User.find(user.id).image_use_gravatar
595 def test_terms_new_user
596 get :terms, :session => { :new_user => User.new }
597 assert_response :success
598 assert_template :terms
601 def test_terms_agreed
602 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
604 session[:user] = user.id
607 assert_response :redirect
608 assert_redirected_to :action => :account, :display_name => user.display_name
611 def test_terms_not_seen_without_referer
612 user = create(:user, :terms_seen => false, :terms_agreed => nil)
614 session[:user] = user.id
617 assert_response :success
618 assert_template :terms
620 post :save, :params => { :user => { :consider_pd => true }, :read_ct => 1, :read_tou => 1 }
621 assert_response :redirect
622 assert_redirected_to :action => :account, :display_name => user.display_name
623 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
627 assert_equal true, user.consider_pd
628 assert_not_nil user.terms_agreed
629 assert_equal true, user.terms_seen
632 def test_terms_not_seen_with_referer
633 user = create(:user, :terms_seen => false, :terms_agreed => nil)
635 session[:user] = user.id
637 get :terms, :params => { :referer => "/test" }
638 assert_response :success
639 assert_template :terms
641 post :save, :params => { :user => { :consider_pd => true }, :referer => "/test", :read_ct => 1, :read_tou => 1 }
642 assert_response :redirect
643 assert_redirected_to "/test"
644 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
648 assert_equal true, user.consider_pd
649 assert_not_nil user.terms_agreed
650 assert_equal true, user.terms_seen
653 # Check that if you haven't seen the terms, and make a request that requires authentication,
654 # that your request is redirected to view the terms
655 def test_terms_not_seen_redirection
656 user = create(:user, :terms_seen => false, :terms_agreed => nil)
657 session[:user] = user.id
659 get :account, :params => { :display_name => user.display_name }
660 assert_response :redirect
661 assert_redirected_to :action => :terms, :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
665 user = create(:user, :data_public => false)
666 post :go_public, :session => { :user => user }
667 assert_response :redirect
668 assert_redirected_to :action => :account, :display_name => user.display_name
669 assert_equal true, User.find(user.id).data_public
672 def test_lost_password
673 # Test fetching the lost password page
675 assert_response :success
676 assert_template :lost_password
677 assert_select "div#notice", false
679 # Test resetting using the address as recorded for a user that has an
680 # address which is duplicated in a different case by another user
682 uppercase_user = build(:user, :email => user.email.upcase).tap { |u| u.save(:validate => false) }
684 assert_difference "ActionMailer::Base.deliveries.size", 1 do
685 perform_enqueued_jobs do
686 post :lost_password, :params => { :user => { :email => user.email } }
689 assert_response :redirect
690 assert_redirected_to :action => :login
691 assert_match(/^Sorry you lost it/, flash[:notice])
692 email = ActionMailer::Base.deliveries.first
693 assert_equal 1, email.to.count
694 assert_equal user.email, email.to.first
695 ActionMailer::Base.deliveries.clear
697 # Test resetting using an address that matches a different user
698 # that has the same address in a different case
699 assert_difference "ActionMailer::Base.deliveries.size", 1 do
700 perform_enqueued_jobs do
701 post :lost_password, :params => { :user => { :email => user.email.upcase } }
704 assert_response :redirect
705 assert_redirected_to :action => :login
706 assert_match(/^Sorry you lost it/, flash[:notice])
707 email = ActionMailer::Base.deliveries.first
708 assert_equal 1, email.to.count
709 assert_equal uppercase_user.email, email.to.first
710 ActionMailer::Base.deliveries.clear
712 # Test resetting using an address that is a case insensitive match
713 # for more than one user but not an exact match for either
714 assert_no_difference "ActionMailer::Base.deliveries.size" do
715 perform_enqueued_jobs do
716 post :lost_password, :params => { :user => { :email => user.email.titlecase } }
719 assert_response :success
720 assert_template :lost_password
721 assert_select ".error", /^Could not find that email address/
723 # Test resetting using the address as recorded for a user that has an
724 # address which is case insensitively unique
725 third_user = create(:user)
726 assert_difference "ActionMailer::Base.deliveries.size", 1 do
727 perform_enqueued_jobs do
728 post :lost_password, :params => { :user => { :email => third_user.email } }
731 assert_response :redirect
732 assert_redirected_to :action => :login
733 assert_match(/^Sorry you lost it/, flash[:notice])
734 email = ActionMailer::Base.deliveries.first
735 assert_equal 1, email.to.count
736 assert_equal third_user.email, email.to.first
737 ActionMailer::Base.deliveries.clear
739 # Test resetting using an address that matches a user that has the
740 # same (case insensitively unique) address in a different case
741 assert_difference "ActionMailer::Base.deliveries.size", 1 do
742 perform_enqueued_jobs do
743 post :lost_password, :params => { :user => { :email => third_user.email.upcase } }
746 assert_response :redirect
747 assert_redirected_to :action => :login
748 assert_match(/^Sorry you lost it/, flash[:notice])
749 email = ActionMailer::Base.deliveries.first
750 assert_equal 1, email.to.count
751 assert_equal third_user.email, email.to.first
752 ActionMailer::Base.deliveries.clear
755 def test_reset_password
756 user = create(:user, :pending)
757 # Test a request with no token
759 assert_response :bad_request
761 # Test a request with a bogus token
762 get :reset_password, :params => { :token => "made_up_token" }
763 assert_response :redirect
764 assert_redirected_to :action => :lost_password
766 # Create a valid token for a user
767 token = user.tokens.create
769 # Test a request with a valid token
770 get :reset_password, :params => { :token => token.token }
771 assert_response :success
772 assert_template :reset_password
774 # Test that errors are reported for erroneous submissions
775 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "different_password" } }
776 assert_response :success
777 assert_template :reset_password
778 assert_select "div#errorExplanation"
780 # Test setting a new password
781 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" } }
782 assert_response :redirect
783 assert_redirected_to root_path
784 assert_equal user.id, session[:user]
786 assert_equal "active", user.status
787 assert_equal true, user.email_valid
788 assert_equal user, User.authenticate(:username => user.email, :password => "new_password")
792 # Get a user to work with - note that this user deliberately
793 # conflicts with uppercase_user in the email and display name
794 # fields to test that we can change other fields without any
795 # validation errors being reported
796 user = create(:user, :languages => [])
797 _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
799 # Make sure that you are redirected to the login page when
800 # you are not logged in
801 get :account, :params => { :display_name => user.display_name }
802 assert_response :redirect
803 assert_redirected_to :action => "login", :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
805 # Make sure that you are blocked when not logged in as the right user
806 get :account, :params => { :display_name => user.display_name }, :session => { :user => create(:user) }
807 assert_response :forbidden
809 # Make sure we get the page when we are logged in as the right user
810 get :account, :params => { :display_name => user.display_name }, :session => { :user => user }
811 assert_response :success
812 assert_template :account
813 assert_select "form#accountForm" do |form|
814 assert_equal "post", form.attr("method").to_s
815 assert_select "input[name='_method']", false
816 assert_equal "/user/#{ERB::Util.u(user.display_name)}/account", form.attr("action").to_s
819 # Updating the description should work
820 user.description = "new description"
821 user.preferred_editor = "default"
822 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
823 assert_response :success
824 assert_template :account
825 assert_select "div#errorExplanation", false
826 assert_select ".notice", /^User information updated successfully/
827 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
829 # Changing to a invalid editor should fail
830 user.preferred_editor = "unknown"
831 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
832 assert_response :success
833 assert_template :account
834 assert_select ".notice", false
835 assert_select "div#errorExplanation"
836 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
838 # Changing to a valid editor should work
839 user.preferred_editor = "potlatch2"
840 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
841 assert_response :success
842 assert_template :account
843 assert_select "div#errorExplanation", false
844 assert_select ".notice", /^User information updated successfully/
845 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
847 # Changing to the default editor should work
848 user.preferred_editor = "default"
849 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
850 assert_response :success
851 assert_template :account
852 assert_select "div#errorExplanation", false
853 assert_select ".notice", /^User information updated successfully/
854 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
856 # Changing to an uploaded image should work
857 image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif")
858 post :account, :params => { :display_name => user.display_name, :avatar_action => "new", :user => user.attributes.merge(:avatar => image) }, :session => { :user => user }
859 assert_response :success
860 assert_template :account
861 assert_select "div#errorExplanation", false
862 assert_select ".notice", /^User information updated successfully/
863 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=avatar_action][checked][value=?]", "keep"
865 # Changing to a gravatar image should work
866 post :account, :params => { :display_name => user.display_name, :avatar_action => "gravatar", :user => user.attributes }, :session => { :user => user }
867 assert_response :success
868 assert_template :account
869 assert_select "div#errorExplanation", false
870 assert_select ".notice", /^User information updated successfully/
871 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=avatar_action][checked][value=?]", "gravatar"
873 # Removing the image should work
874 post :account, :params => { :display_name => user.display_name, :avatar_action => "delete", :user => user.attributes }, :session => { :user => user }
875 assert_response :success
876 assert_template :account
877 assert_select "div#errorExplanation", false
878 assert_select ".notice", /^User information updated successfully/
879 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=avatar_action][checked]", false
881 # Adding external authentication should redirect to the auth provider
882 post :account, :params => { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, :session => { :user => user }
883 assert_response :redirect
884 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")
886 # Changing name to one that exists should fail
887 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
888 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
889 assert_response :success
890 assert_template :account
891 assert_select ".notice", false
892 assert_select "div#errorExplanation"
893 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
895 # Changing name to one that exists should fail, regardless of case
896 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
897 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
898 assert_response :success
899 assert_template :account
900 assert_select ".notice", false
901 assert_select "div#errorExplanation"
902 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
904 # Changing name to one that doesn't exist should work
905 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
906 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
907 assert_response :success
908 assert_template :account
909 assert_select "div#errorExplanation", false
910 assert_select ".notice", /^User information updated successfully/
911 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
913 # Record the change of name
914 user.display_name = "new tester"
916 # Changing email to one that exists should fail
917 user.new_email = create(:user).email
918 assert_no_difference "ActionMailer::Base.deliveries.size" do
919 perform_enqueued_jobs do
920 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
923 assert_response :success
924 assert_template :account
925 assert_select ".notice", false
926 assert_select "div#errorExplanation"
927 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
929 # Changing email to one that exists should fail, regardless of case
930 user.new_email = create(:user).email.upcase
931 assert_no_difference "ActionMailer::Base.deliveries.size" do
932 perform_enqueued_jobs do
933 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
936 assert_response :success
937 assert_template :account
938 assert_select ".notice", false
939 assert_select "div#errorExplanation"
940 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
942 # Changing email to one that doesn't exist should work
943 user.new_email = "new_tester@example.com"
944 assert_difference "ActionMailer::Base.deliveries.size", 1 do
945 perform_enqueued_jobs do
946 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
949 assert_response :success
950 assert_template :account
951 assert_select "div#errorExplanation", false
952 assert_select ".notice", /^User information updated successfully/
953 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
954 email = ActionMailer::Base.deliveries.first
955 assert_equal 1, email.to.count
956 assert_equal user.new_email, email.to.first
957 ActionMailer::Base.deliveries.clear
960 # Check that the user account page will display and contains some relevant
961 # information for the user
963 # Test a non-existent user
964 get :show, :params => { :display_name => "unknown" }
965 assert_response :not_found
968 user = create(:user, :home_lon => 1.1, :home_lat => 1.1)
969 friend_user = create(:user, :home_lon => 1.2, :home_lat => 1.2)
970 create(:friendship, :befriender => user, :befriendee => friend_user)
971 create(:changeset, :user => friend_user)
973 get :show, :params => { :display_name => user.display_name }
974 assert_response :success
975 assert_select "div#userinformation" do
976 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
977 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
978 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
979 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
980 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
981 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
982 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
983 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
986 # Friends shouldn't be visible as we're not logged in
987 assert_select "div#friends-container", :count => 0
989 # Test a user who has been blocked
990 blocked_user = create(:user)
991 create(:user_block, :user => blocked_user)
992 get :show, :params => { :display_name => blocked_user.display_name }
993 assert_response :success
994 assert_select "div#userinformation" do
995 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
996 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
997 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
998 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
999 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
1000 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
1001 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
1002 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
1005 # Test a moderator who has applied blocks
1006 moderator_user = create(:moderator_user)
1007 create(:user_block, :creator => moderator_user)
1008 get :show, :params => { :display_name => moderator_user.display_name }
1009 assert_response :success
1010 assert_select "div#userinformation" do
1011 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
1012 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
1013 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
1014 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
1015 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
1016 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
1017 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
1018 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
1021 # Login as a normal user
1022 session[:user] = user.id
1024 # Test the normal user
1025 get :show, :params => { :display_name => user.display_name }
1026 assert_response :success
1027 assert_select "div#userinformation" do
1028 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1029 assert_select "a[href='/traces/mine']", 1
1030 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1031 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1032 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 1
1033 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1034 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1035 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1038 # Friends should be visible as we're now logged in
1039 assert_select "div#friends-container" do
1040 assert_select "div.contact-activity", :count => 1
1043 # Login as a moderator
1044 session[:user] = create(:moderator_user).id
1046 # Test the normal user
1047 get :show, :params => { :display_name => user.display_name }
1048 assert_response :success
1049 assert_select "div#userinformation" do
1050 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1051 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1052 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1053 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1054 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1055 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1056 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1057 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
1061 # Test whether information about contributor terms is shown for users who haven't agreed
1062 def test_terms_not_agreed
1063 agreed_user = create(:user, :terms_agreed => 3.days.ago)
1064 seen_user = create(:user, :terms_seen => true, :terms_agreed => nil)
1065 not_seen_user = create(:user, :terms_seen => false, :terms_agreed => nil)
1067 get :show, :params => { :display_name => agreed_user.display_name }
1068 assert_response :success
1069 assert_select "div#userinformation" do
1070 assert_select "p", :count => 0, :text => /Contributor terms/
1073 get :show, :params => { :display_name => seen_user.display_name }
1074 assert_response :success
1075 # put @response.body
1076 assert_select "div#userinformation" do
1077 assert_select "p", :count => 1, :text => /Contributor terms/
1078 assert_select "p", /Declined/
1081 get :show, :params => { :display_name => not_seen_user.display_name }
1082 assert_response :success
1083 assert_select "div#userinformation" do
1084 assert_select "p", :count => 1, :text => /Contributor terms/
1085 assert_select "p", /Undecided/
1089 def test_make_friend
1090 # Get users to work with
1091 user = create(:user)
1092 friend = create(:user)
1094 # Check that the users aren't already friends
1095 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1097 # When not logged in a GET should ask us to login
1098 get :make_friend, :params => { :display_name => friend.display_name }
1099 assert_redirected_to :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1101 # When not logged in a POST should error
1102 post :make_friend, :params => { :display_name => friend.display_name }
1103 assert_response :forbidden
1104 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1106 # When logged in a GET should get a confirmation page
1107 get :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1108 assert_response :success
1109 assert_template :make_friend
1110 assert_select "form" do
1111 assert_select "input[type='hidden'][name='referer']", 0
1112 assert_select "input[type='submit']", 1
1114 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1116 # When logged in a POST should add the friendship
1117 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1118 perform_enqueued_jobs do
1119 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1122 assert_redirected_to user_path(friend)
1123 assert_match(/is now your friend/, flash[:notice])
1124 assert Friendship.where(:befriender => user, :befriendee => friend).first
1125 email = ActionMailer::Base.deliveries.first
1126 assert_equal 1, email.to.count
1127 assert_equal friend.email, email.to.first
1128 ActionMailer::Base.deliveries.clear
1130 # A second POST should report that the friendship already exists
1131 assert_no_difference "ActionMailer::Base.deliveries.size" do
1132 perform_enqueued_jobs do
1133 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1136 assert_redirected_to user_path(friend)
1137 assert_match(/You are already friends with/, flash[:warning])
1138 assert Friendship.where(:befriender => user, :befriendee => friend).first
1141 def test_make_friend_with_referer
1142 # Get users to work with
1143 user = create(:user)
1144 friend = create(:user)
1146 # Check that the users aren't already friends
1147 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1149 # The GET should preserve any referer
1150 get :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1151 assert_response :success
1152 assert_template :make_friend
1153 assert_select "form" do
1154 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1155 assert_select "input[type='submit']", 1
1157 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1159 # When logged in a POST should add the friendship and refer us
1160 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1161 perform_enqueued_jobs do
1162 post :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1165 assert_redirected_to "/test"
1166 assert_match(/is now your friend/, flash[:notice])
1167 assert Friendship.where(:befriender => user, :befriendee => friend).first
1168 email = ActionMailer::Base.deliveries.first
1169 assert_equal 1, email.to.count
1170 assert_equal friend.email, email.to.first
1171 ActionMailer::Base.deliveries.clear
1174 def test_make_friend_unkown_user
1175 # Should error when a bogus user is specified
1176 get :make_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1177 assert_response :not_found
1178 assert_template :no_such_user
1181 def test_remove_friend
1182 # Get users to work with
1183 user = create(:user)
1184 friend = create(:user)
1185 create(:friendship, :befriender => user, :befriendee => friend)
1187 # Check that the users are friends
1188 assert Friendship.where(:befriender => user, :befriendee => friend).first
1190 # When not logged in a GET should ask us to login
1191 get :remove_friend, :params => { :display_name => friend.display_name }
1192 assert_redirected_to :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1194 # When not logged in a POST should error
1195 post :remove_friend, :params => { :display_name => friend.display_name }
1196 assert_response :forbidden
1197 assert Friendship.where(:befriender => user, :befriendee => friend).first
1199 # When logged in a GET should get a confirmation page
1200 get :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1201 assert_response :success
1202 assert_template :remove_friend
1203 assert_select "form" do
1204 assert_select "input[type='hidden'][name='referer']", 0
1205 assert_select "input[type='submit']", 1
1207 assert Friendship.where(:befriender => user, :befriendee => friend).first
1209 # When logged in a POST should remove the friendship
1210 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1211 assert_redirected_to user_path(friend)
1212 assert_match(/was removed from your friends/, flash[:notice])
1213 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1215 # A second POST should report that the friendship does not exist
1216 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1217 assert_redirected_to user_path(friend)
1218 assert_match(/is not one of your friends/, flash[:error])
1219 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1222 def test_remove_friend_with_referer
1223 # Get users to work with
1224 user = create(:user)
1225 friend = create(:user)
1226 create(:friendship, :befriender => user, :befriendee => friend)
1228 # Check that the users are friends
1229 assert Friendship.where(:befriender => user, :befriendee => friend).first
1231 # The GET should preserve any referer
1232 get :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1233 assert_response :success
1234 assert_template :remove_friend
1235 assert_select "form" do
1236 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1237 assert_select "input[type='submit']", 1
1239 assert Friendship.where(:befriender => user, :befriendee => friend).first
1241 # When logged in a POST should remove the friendship and refer
1242 post :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1243 assert_redirected_to "/test"
1244 assert_match(/was removed from your friends/, flash[:notice])
1245 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1248 def test_remove_friend_unkown_user
1249 # Should error when a bogus user is specified
1250 get :remove_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1251 assert_response :not_found
1252 assert_template :no_such_user
1256 user = create(:user)
1258 # Try without logging in
1259 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }
1260 assert_response :redirect
1261 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1263 # Now try as a normal user
1264 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1265 assert_response :redirect
1266 assert_redirected_to :controller => :errors, :action => :forbidden
1268 # Finally try as an administrator
1269 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1270 assert_response :redirect
1271 assert_redirected_to :action => :show, :display_name => user.display_name
1272 assert_equal "suspended", User.find(user.id).status
1276 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1278 # Try without logging in
1279 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }
1280 assert_response :redirect
1281 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1283 # Now try as a normal user
1284 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1285 assert_response :redirect
1286 assert_redirected_to :controller => :errors, :action => :forbidden
1288 # Finally try as an administrator
1289 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1290 assert_response :redirect
1291 assert_redirected_to :action => :show, :display_name => user.display_name
1293 # Check that the user was deleted properly
1295 assert_equal "user_#{user.id}", user.display_name
1296 assert_equal "", user.description
1297 assert_nil user.home_lat
1298 assert_nil user.home_lon
1299 assert_equal false, user.avatar.attached?
1300 assert_equal false, user.email_valid
1301 assert_nil user.new_email
1302 assert_nil user.auth_provider
1303 assert_nil user.auth_uid
1304 assert_equal "deleted", user.status
1308 user = create(:user)
1309 moderator_user = create(:moderator_user)
1310 administrator_user = create(:administrator_user)
1311 _suspended_user = create(:user, :suspended)
1312 _ip_user = create(:user, :creation_ip => "1.2.3.4")
1314 # There are now 7 users - the five above, plus two extra "granters" for the
1315 # moderator_user and administrator_user
1316 assert_equal 7, User.count
1318 # Shouldn't work when not logged in
1320 assert_response :redirect
1321 assert_redirected_to :action => :login, :referer => users_path
1323 session[:user] = user.id
1325 # Shouldn't work when logged in as a normal user
1327 assert_response :redirect
1328 assert_redirected_to :controller => :errors, :action => :forbidden
1330 session[:user] = moderator_user.id
1332 # Shouldn't work when logged in as a moderator
1334 assert_response :redirect
1335 assert_redirected_to :controller => :errors, :action => :forbidden
1337 session[:user] = administrator_user.id
1339 # Note there is a header row, so all row counts are users + 1
1340 # Should work when logged in as an administrator
1342 assert_response :success
1343 assert_template :index
1344 assert_select "table#user_list tr", :count => 7 + 1
1346 # Should be able to limit by status
1347 get :index, :params => { :status => "suspended" }
1348 assert_response :success
1349 assert_template :index
1350 assert_select "table#user_list tr", :count => 1 + 1
1352 # Should be able to limit by IP address
1353 get :index, :params => { :ip => "1.2.3.4" }
1354 assert_response :success
1355 assert_template :index
1356 assert_select "table#user_list tr", :count => 1 + 1
1359 def test_index_get_paginated
1360 1.upto(100).each do |n|
1361 User.create(:display_name => "extra_#{n}",
1362 :email => "extra#{n}@example.com",
1363 :pass_crypt => "extraextra")
1366 session[:user] = create(:administrator_user).id
1368 # 100 examples, an administrator, and a granter for the admin.
1369 assert_equal 102, User.count
1372 assert_response :success
1373 assert_template :index
1374 assert_select "table#user_list tr", :count => 51
1376 get :index, :params => { :page => 2 }
1377 assert_response :success
1378 assert_template :index
1379 assert_select "table#user_list tr", :count => 51
1381 get :index, :params => { :page => 3 }
1382 assert_response :success
1383 assert_template :index
1384 assert_select "table#user_list tr", :count => 3
1387 def test_index_post_confirm
1388 inactive_user = create(:user, :pending)
1389 suspended_user = create(:user, :suspended)
1391 # Shouldn't work when not logged in
1392 assert_no_difference "User.active.count" do
1393 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1395 assert_response :forbidden
1397 assert_equal "pending", inactive_user.reload.status
1398 assert_equal "suspended", suspended_user.reload.status
1400 session[:user] = create(:user).id
1402 # Shouldn't work when logged in as a normal user
1403 assert_no_difference "User.active.count" do
1404 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1406 assert_response :redirect
1407 assert_redirected_to :controller => :errors, :action => :forbidden
1408 assert_equal "pending", inactive_user.reload.status
1409 assert_equal "suspended", suspended_user.reload.status
1411 session[:user] = create(:moderator_user).id
1413 # Shouldn't work when logged in as a moderator
1414 assert_no_difference "User.active.count" do
1415 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1417 assert_response :redirect
1418 assert_redirected_to :controller => :errors, :action => :forbidden
1419 assert_equal "pending", inactive_user.reload.status
1420 assert_equal "suspended", suspended_user.reload.status
1422 session[:user] = create(:administrator_user).id
1424 # Should work when logged in as an administrator
1425 assert_difference "User.active.count", 2 do
1426 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1428 assert_response :redirect
1429 assert_redirected_to :action => :index
1430 assert_equal "confirmed", inactive_user.reload.status
1431 assert_equal "confirmed", suspended_user.reload.status
1434 def test_index_post_hide
1435 normal_user = create(:user)
1436 confirmed_user = create(:user, :confirmed)
1438 # Shouldn't work when not logged in
1439 assert_no_difference "User.active.count" do
1440 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1442 assert_response :forbidden
1444 assert_equal "active", normal_user.reload.status
1445 assert_equal "confirmed", confirmed_user.reload.status
1447 session[:user] = create(:user).id
1449 # Shouldn't work when logged in as a normal user
1450 assert_no_difference "User.active.count" do
1451 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1453 assert_response :redirect
1454 assert_redirected_to :controller => :errors, :action => :forbidden
1455 assert_equal "active", normal_user.reload.status
1456 assert_equal "confirmed", confirmed_user.reload.status
1458 session[:user] = create(:moderator_user).id
1460 # Shouldn't work when logged in as a moderator
1461 assert_no_difference "User.active.count" do
1462 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1464 assert_response :redirect
1465 assert_redirected_to :controller => :errors, :action => :forbidden
1466 assert_equal "active", normal_user.reload.status
1467 assert_equal "confirmed", confirmed_user.reload.status
1469 session[:user] = create(:administrator_user).id
1471 # Should work when logged in as an administrator
1472 assert_difference "User.active.count", -2 do
1473 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1475 assert_response :redirect
1476 assert_redirected_to :action => :index
1477 assert_equal "deleted", normal_user.reload.status
1478 assert_equal "deleted", confirmed_user.reload.status