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_fallback_without_referer
360 assert_response :success
361 assert_template :logout
362 assert_select "input[name=referer][value=?]", ""
365 def test_logout_fallback_with_referer
366 get :logout, :params => { :referer => "/test" }
367 assert_response :success
368 assert_template :logout
369 assert_select "input[name=referer][value=?]", "/test"
372 def test_logout_with_token
373 token = create(:user).tokens.create
375 session[:token] = token.token
378 assert_response :redirect
379 assert_redirected_to root_path
380 assert_nil session[:token]
381 assert_nil UserToken.where(:id => token.id).first
385 user = create(:user, :pending)
386 confirm_string = user.tokens.create.token
388 @request.cookies["_osm_session"] = user.display_name
389 get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
390 assert_response :success
391 assert_template :confirm
394 def test_confirm_get_already_confirmed
396 confirm_string = user.tokens.create.token
398 @request.cookies["_osm_session"] = user.display_name
399 get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
400 assert_response :redirect
401 assert_redirected_to root_path
404 def test_confirm_success_no_token_no_referer
405 user = create(:user, :pending)
406 stub_gravatar_request(user.email)
407 confirm_string = user.tokens.create.token
409 @request.cookies["_osm_session"] = user.display_name
410 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
411 assert_redirected_to login_path
412 assert_match(/Confirmed your account/, flash[:notice])
415 def test_confirm_success_good_token_no_referer
416 user = create(:user, :pending)
417 stub_gravatar_request(user.email)
418 confirm_string = user.tokens.create.token
419 token = user.tokens.create.token
421 @request.cookies["_osm_session"] = user.display_name
422 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
423 assert_redirected_to welcome_path
426 def test_confirm_success_bad_token_no_referer
427 user = create(:user, :pending)
428 stub_gravatar_request(user.email)
429 confirm_string = user.tokens.create.token
430 token = create(:user).tokens.create.token
432 @request.cookies["_osm_session"] = user.display_name
433 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
434 assert_redirected_to login_path
435 assert_match(/Confirmed your account/, flash[:notice])
438 def test_confirm_success_no_token_with_referer
439 user = create(:user, :pending)
440 stub_gravatar_request(user.email)
441 confirm_string = user.tokens.create(:referer => new_diary_entry_path).token
443 @request.cookies["_osm_session"] = user.display_name
444 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
445 assert_redirected_to login_path(:referer => new_diary_entry_path)
446 assert_match(/Confirmed your account/, flash[:notice])
449 def test_confirm_success_good_token_with_referer
450 user = create(:user, :pending)
451 stub_gravatar_request(user.email)
452 confirm_string = user.tokens.create(:referer => new_diary_entry_path).token
453 token = user.tokens.create.token
455 @request.cookies["_osm_session"] = user.display_name
456 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
457 assert_redirected_to new_diary_entry_path
460 def test_confirm_success_bad_token_with_referer
461 user = create(:user, :pending)
462 stub_gravatar_request(user.email)
463 confirm_string = user.tokens.create(:referer => new_diary_entry_path).token
464 token = create(:user).tokens.create.token
466 @request.cookies["_osm_session"] = user.display_name
467 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
468 assert_redirected_to login_path(:referer => new_diary_entry_path)
469 assert_match(/Confirmed your account/, flash[:notice])
472 def test_confirm_expired_token
473 user = create(:user, :pending)
474 confirm_string = user.tokens.create(:expiry => 1.day.ago).token
476 @request.cookies["_osm_session"] = user.display_name
477 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
478 assert_redirected_to :action => "confirm"
479 assert_match(/confirmation code has expired/, flash[:error])
482 def test_confirm_already_confirmed
484 confirm_string = user.tokens.create(:referer => new_diary_entry_path).token
486 @request.cookies["_osm_session"] = user.display_name
487 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
488 assert_redirected_to :action => "login"
489 assert_match(/already been confirmed/, flash[:error])
492 def test_confirm_resend_success
493 user = create(:user, :pending)
494 session[:token] = user.tokens.create.token
496 assert_difference "ActionMailer::Base.deliveries.size", 1 do
497 perform_enqueued_jobs do
498 get :confirm_resend, :params => { :display_name => user.display_name }
502 assert_response :redirect
503 assert_redirected_to login_path
504 assert_match(/sent a new confirmation/, flash[:notice])
506 email = ActionMailer::Base.deliveries.last
508 assert_equal user.email, email.to.first
510 ActionMailer::Base.deliveries.clear
513 def test_confirm_resend_no_token
514 user = create(:user, :pending)
515 assert_no_difference "ActionMailer::Base.deliveries.size" do
516 perform_enqueued_jobs do
517 get :confirm_resend, :params => { :display_name => user.display_name }
521 assert_response :redirect
522 assert_redirected_to login_path
523 assert_match "User #{user.display_name} not found.", flash[:error]
526 def test_confirm_resend_unknown_user
527 assert_no_difference "ActionMailer::Base.deliveries.size" do
528 perform_enqueued_jobs do
529 get :confirm_resend, :params => { :display_name => "No Such User" }
533 assert_response :redirect
534 assert_redirected_to login_path
535 assert_match "User No Such User not found.", flash[:error]
538 def test_confirm_email_get
540 confirm_string = user.tokens.create.token
542 get :confirm_email, :params => { :confirm_string => confirm_string }
543 assert_response :success
544 assert_template :confirm_email
547 def test_confirm_email_success
548 user = create(:user, :new_email => "test-new@example.com")
549 stub_gravatar_request(user.new_email)
550 confirm_string = user.tokens.create.token
552 post :confirm_email, :params => { :confirm_string => confirm_string }
553 assert_response :redirect
554 assert_redirected_to :action => :account, :display_name => user.display_name
555 assert_match(/Confirmed your change of email address/, flash[:notice])
558 def test_confirm_email_already_confirmed
560 confirm_string = user.tokens.create.token
562 post :confirm_email, :params => { :confirm_string => confirm_string }
563 assert_response :redirect
564 assert_redirected_to :action => :account, :display_name => user.display_name
565 assert_match(/already been confirmed/, flash[:error])
568 def test_confirm_email_bad_token
569 post :confirm_email, :params => { :confirm_string => "XXXXX" }
570 assert_response :success
571 assert_template :confirm_email
572 assert_match(/confirmation code has expired or does not exist/, flash[:error])
576 # test if testing for a gravatar works
577 # this happens when the email is actually changed
578 # which is triggered by the confirmation mail
579 def test_gravatar_auto_enable
580 # switch to email that has a gravatar
581 user = create(:user, :new_email => "test-new@example.com")
582 stub_gravatar_request(user.new_email, 200)
583 confirm_string = user.tokens.create.token
584 # precondition gravatar should be turned off
585 assert_not user.image_use_gravatar
586 post :confirm_email, :params => { :confirm_string => confirm_string }
587 assert_response :redirect
588 assert_redirected_to :action => :account, :display_name => user.display_name
589 assert_match(/Confirmed your change of email address/, flash[:notice])
590 # gravatar use should now be enabled
591 assert User.find(user.id).image_use_gravatar
594 def test_gravatar_auto_disable
595 # switch to email without a gravatar
596 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
597 stub_gravatar_request(user.new_email, 404)
598 confirm_string = user.tokens.create.token
599 # precondition gravatar should be turned on
600 assert user.image_use_gravatar
601 post :confirm_email, :params => { :confirm_string => confirm_string }
602 assert_response :redirect
603 assert_redirected_to :action => :account, :display_name => user.display_name
604 assert_match(/Confirmed your change of email address/, flash[:notice])
605 # gravatar use should now be disabled
606 assert_not User.find(user.id).image_use_gravatar
609 def test_terms_new_user
610 get :terms, :session => { :new_user => User.new }
611 assert_response :success
612 assert_template :terms
615 def test_terms_agreed
616 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
618 session[:user] = user.id
621 assert_response :redirect
622 assert_redirected_to :action => :account, :display_name => user.display_name
625 def test_terms_not_seen_without_referer
626 user = create(:user, :terms_seen => false, :terms_agreed => nil)
628 session[:user] = user.id
631 assert_response :success
632 assert_template :terms
634 post :save, :params => { :user => { :consider_pd => true }, :read_ct => 1, :read_tou => 1 }
635 assert_response :redirect
636 assert_redirected_to :action => :account, :display_name => user.display_name
637 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
641 assert_equal true, user.consider_pd
642 assert_not_nil user.terms_agreed
643 assert_equal true, user.terms_seen
646 def test_terms_not_seen_with_referer
647 user = create(:user, :terms_seen => false, :terms_agreed => nil)
649 session[:user] = user.id
651 get :terms, :params => { :referer => "/test" }
652 assert_response :success
653 assert_template :terms
655 post :save, :params => { :user => { :consider_pd => true }, :referer => "/test", :read_ct => 1, :read_tou => 1 }
656 assert_response :redirect
657 assert_redirected_to "/test"
658 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
662 assert_equal true, user.consider_pd
663 assert_not_nil user.terms_agreed
664 assert_equal true, user.terms_seen
667 # Check that if you haven't seen the terms, and make a request that requires authentication,
668 # that your request is redirected to view the terms
669 def test_terms_not_seen_redirection
670 user = create(:user, :terms_seen => false, :terms_agreed => nil)
671 session[:user] = user.id
673 get :account, :params => { :display_name => user.display_name }
674 assert_response :redirect
675 assert_redirected_to :action => :terms, :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
679 user = create(:user, :data_public => false)
680 post :go_public, :session => { :user => user }
681 assert_response :redirect
682 assert_redirected_to :action => :account, :display_name => user.display_name
683 assert_equal true, User.find(user.id).data_public
686 def test_lost_password
687 # Test fetching the lost password page
689 assert_response :success
690 assert_template :lost_password
691 assert_select "div#notice", false
693 # Test resetting using the address as recorded for a user that has an
694 # address which is duplicated in a different case by another user
696 uppercase_user = build(:user, :email => user.email.upcase).tap { |u| u.save(:validate => false) }
698 assert_difference "ActionMailer::Base.deliveries.size", 1 do
699 perform_enqueued_jobs do
700 post :lost_password, :params => { :user => { :email => user.email } }
703 assert_response :redirect
704 assert_redirected_to :action => :login
705 assert_match(/^Sorry you lost it/, flash[:notice])
706 email = ActionMailer::Base.deliveries.first
707 assert_equal 1, email.to.count
708 assert_equal user.email, email.to.first
709 ActionMailer::Base.deliveries.clear
711 # Test resetting using an address that matches a different user
712 # that has the same address in a different case
713 assert_difference "ActionMailer::Base.deliveries.size", 1 do
714 perform_enqueued_jobs do
715 post :lost_password, :params => { :user => { :email => user.email.upcase } }
718 assert_response :redirect
719 assert_redirected_to :action => :login
720 assert_match(/^Sorry you lost it/, flash[:notice])
721 email = ActionMailer::Base.deliveries.first
722 assert_equal 1, email.to.count
723 assert_equal uppercase_user.email, email.to.first
724 ActionMailer::Base.deliveries.clear
726 # Test resetting using an address that is a case insensitive match
727 # for more than one user but not an exact match for either
728 assert_no_difference "ActionMailer::Base.deliveries.size" do
729 perform_enqueued_jobs do
730 post :lost_password, :params => { :user => { :email => user.email.titlecase } }
733 assert_response :success
734 assert_template :lost_password
735 assert_select ".error", /^Could not find that email address/
737 # Test resetting using the address as recorded for a user that has an
738 # address which is case insensitively unique
739 third_user = create(:user)
740 assert_difference "ActionMailer::Base.deliveries.size", 1 do
741 perform_enqueued_jobs do
742 post :lost_password, :params => { :user => { :email => third_user.email } }
745 assert_response :redirect
746 assert_redirected_to :action => :login
747 assert_match(/^Sorry you lost it/, flash[:notice])
748 email = ActionMailer::Base.deliveries.first
749 assert_equal 1, email.to.count
750 assert_equal third_user.email, email.to.first
751 ActionMailer::Base.deliveries.clear
753 # Test resetting using an address that matches a user that has the
754 # same (case insensitively unique) address in a different case
755 assert_difference "ActionMailer::Base.deliveries.size", 1 do
756 perform_enqueued_jobs do
757 post :lost_password, :params => { :user => { :email => third_user.email.upcase } }
760 assert_response :redirect
761 assert_redirected_to :action => :login
762 assert_match(/^Sorry you lost it/, flash[:notice])
763 email = ActionMailer::Base.deliveries.first
764 assert_equal 1, email.to.count
765 assert_equal third_user.email, email.to.first
766 ActionMailer::Base.deliveries.clear
769 def test_reset_password
770 user = create(:user, :pending)
771 # Test a request with no token
773 assert_response :bad_request
775 # Test a request with a bogus token
776 get :reset_password, :params => { :token => "made_up_token" }
777 assert_response :redirect
778 assert_redirected_to :action => :lost_password
780 # Create a valid token for a user
781 token = user.tokens.create
783 # Test a request with a valid token
784 get :reset_password, :params => { :token => token.token }
785 assert_response :success
786 assert_template :reset_password
788 # Test that errors are reported for erroneous submissions
789 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "different_password" } }
790 assert_response :success
791 assert_template :reset_password
792 assert_select "div#errorExplanation"
794 # Test setting a new password
795 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" } }
796 assert_response :redirect
797 assert_redirected_to root_path
798 assert_equal user.id, session[:user]
800 assert_equal "active", user.status
801 assert_equal true, user.email_valid
802 assert_equal user, User.authenticate(:username => user.email, :password => "new_password")
806 # Get a user to work with - note that this user deliberately
807 # conflicts with uppercase_user in the email and display name
808 # fields to test that we can change other fields without any
809 # validation errors being reported
810 user = create(:user, :languages => [])
811 _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
813 # Make sure that you are redirected to the login page when
814 # you are not logged in
815 get :account, :params => { :display_name => user.display_name }
816 assert_response :redirect
817 assert_redirected_to :action => "login", :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
819 # Make sure that you are blocked when not logged in as the right user
820 get :account, :params => { :display_name => user.display_name }, :session => { :user => create(:user) }
821 assert_response :forbidden
823 # Make sure we get the page when we are logged in as the right user
824 get :account, :params => { :display_name => user.display_name }, :session => { :user => user }
825 assert_response :success
826 assert_template :account
827 assert_select "form#accountForm" do |form|
828 assert_equal "post", form.attr("method").to_s
829 assert_select "input[name='_method']", false
830 assert_equal "/user/#{ERB::Util.u(user.display_name)}/account", form.attr("action").to_s
833 # Updating the description should work
834 user.description = "new description"
835 user.preferred_editor = "default"
836 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
837 assert_response :success
838 assert_template :account
839 assert_select "div#errorExplanation", false
840 assert_select ".notice", /^User information updated successfully/
841 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
843 # Changing to a invalid editor should fail
844 user.preferred_editor = "unknown"
845 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
846 assert_response :success
847 assert_template :account
848 assert_select ".notice", false
849 assert_select "div#errorExplanation"
850 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
852 # Changing to a valid editor should work
853 user.preferred_editor = "potlatch2"
854 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
855 assert_response :success
856 assert_template :account
857 assert_select "div#errorExplanation", false
858 assert_select ".notice", /^User information updated successfully/
859 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
861 # Changing to the default editor should work
862 user.preferred_editor = "default"
863 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
864 assert_response :success
865 assert_template :account
866 assert_select "div#errorExplanation", false
867 assert_select ".notice", /^User information updated successfully/
868 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
870 # Changing to an uploaded image should work
871 image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif")
872 post :account, :params => { :display_name => user.display_name, :avatar_action => "new", :user => user.attributes.merge(:avatar => image) }, :session => { :user => user }
873 assert_response :success
874 assert_template :account
875 assert_select "div#errorExplanation", false
876 assert_select ".notice", /^User information updated successfully/
877 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=avatar_action][checked][value=?]", "keep"
879 # Changing to a gravatar image should work
880 post :account, :params => { :display_name => user.display_name, :avatar_action => "gravatar", :user => user.attributes }, :session => { :user => user }
881 assert_response :success
882 assert_template :account
883 assert_select "div#errorExplanation", false
884 assert_select ".notice", /^User information updated successfully/
885 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=avatar_action][checked][value=?]", "gravatar"
887 # Removing the image should work
888 post :account, :params => { :display_name => user.display_name, :avatar_action => "delete", :user => user.attributes }, :session => { :user => user }
889 assert_response :success
890 assert_template :account
891 assert_select "div#errorExplanation", false
892 assert_select ".notice", /^User information updated successfully/
893 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=avatar_action][checked]", false
895 # Adding external authentication should redirect to the auth provider
896 post :account, :params => { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, :session => { :user => user }
897 assert_response :redirect
898 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")
900 # Changing name to one that exists should fail
901 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
902 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
903 assert_response :success
904 assert_template :account
905 assert_select ".notice", false
906 assert_select "div#errorExplanation"
907 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
909 # Changing name to one that exists should fail, regardless of case
910 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
911 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
912 assert_response :success
913 assert_template :account
914 assert_select ".notice", false
915 assert_select "div#errorExplanation"
916 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
918 # Changing name to one that doesn't exist should work
919 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
920 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
921 assert_response :success
922 assert_template :account
923 assert_select "div#errorExplanation", false
924 assert_select ".notice", /^User information updated successfully/
925 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
927 # Record the change of name
928 user.display_name = "new tester"
930 # Changing email to one that exists should fail
931 user.new_email = create(:user).email
932 assert_no_difference "ActionMailer::Base.deliveries.size" do
933 perform_enqueued_jobs do
934 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
937 assert_response :success
938 assert_template :account
939 assert_select ".notice", false
940 assert_select "div#errorExplanation"
941 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
943 # Changing email to one that exists should fail, regardless of case
944 user.new_email = create(:user).email.upcase
945 assert_no_difference "ActionMailer::Base.deliveries.size" do
946 perform_enqueued_jobs do
947 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
950 assert_response :success
951 assert_template :account
952 assert_select ".notice", false
953 assert_select "div#errorExplanation"
954 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
956 # Changing email to one that doesn't exist should work
957 user.new_email = "new_tester@example.com"
958 assert_difference "ActionMailer::Base.deliveries.size", 1 do
959 perform_enqueued_jobs do
960 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
963 assert_response :success
964 assert_template :account
965 assert_select "div#errorExplanation", false
966 assert_select ".notice", /^User information updated successfully/
967 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
968 email = ActionMailer::Base.deliveries.first
969 assert_equal 1, email.to.count
970 assert_equal user.new_email, email.to.first
971 ActionMailer::Base.deliveries.clear
974 # Check that the user account page will display and contains some relevant
975 # information for the user
977 # Test a non-existent user
978 get :show, :params => { :display_name => "unknown" }
979 assert_response :not_found
982 user = create(:user, :home_lon => 1.1, :home_lat => 1.1)
983 friend_user = create(:user, :home_lon => 1.2, :home_lat => 1.2)
984 create(:friendship, :befriender => user, :befriendee => friend_user)
985 create(:changeset, :user => friend_user)
987 get :show, :params => { :display_name => user.display_name }
988 assert_response :success
989 assert_select "div#userinformation" do
990 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
991 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
992 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
993 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
994 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
995 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
996 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
997 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1000 # Friends shouldn't be visible as we're not logged in
1001 assert_select "div#friends-container", :count => 0
1003 # Test a user who has been blocked
1004 blocked_user = create(:user)
1005 create(:user_block, :user => blocked_user)
1006 get :show, :params => { :display_name => blocked_user.display_name }
1007 assert_response :success
1008 assert_select "div#userinformation" do
1009 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
1010 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
1011 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
1012 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
1013 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
1014 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
1015 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
1016 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
1019 # Test a moderator who has applied blocks
1020 moderator_user = create(:moderator_user)
1021 create(:user_block, :creator => moderator_user)
1022 get :show, :params => { :display_name => moderator_user.display_name }
1023 assert_response :success
1024 assert_select "div#userinformation" do
1025 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
1026 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
1027 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
1028 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
1029 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
1030 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
1031 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
1032 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
1035 # Login as a normal user
1036 session[:user] = user.id
1038 # Test the normal user
1039 get :show, :params => { :display_name => user.display_name }
1040 assert_response :success
1041 assert_select "div#userinformation" do
1042 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1043 assert_select "a[href='/traces/mine']", 1
1044 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1045 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1046 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 1
1047 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1048 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1049 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1052 # Friends should be visible as we're now logged in
1053 assert_select "div#friends-container" do
1054 assert_select "div.contact-activity", :count => 1
1057 # Login as a moderator
1058 session[:user] = create(:moderator_user).id
1060 # Test the normal user
1061 get :show, :params => { :display_name => user.display_name }
1062 assert_response :success
1063 assert_select "div#userinformation" do
1064 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1065 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1066 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1067 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1068 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1069 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1070 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1071 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
1075 # Test whether information about contributor terms is shown for users who haven't agreed
1076 def test_terms_not_agreed
1077 agreed_user = create(:user, :terms_agreed => 3.days.ago)
1078 seen_user = create(:user, :terms_seen => true, :terms_agreed => nil)
1079 not_seen_user = create(:user, :terms_seen => false, :terms_agreed => nil)
1081 get :show, :params => { :display_name => agreed_user.display_name }
1082 assert_response :success
1083 assert_select "div#userinformation" do
1084 assert_select "p", :count => 0, :text => /Contributor terms/
1087 get :show, :params => { :display_name => seen_user.display_name }
1088 assert_response :success
1089 # put @response.body
1090 assert_select "div#userinformation" do
1091 assert_select "p", :count => 1, :text => /Contributor terms/
1092 assert_select "p", /Declined/
1095 get :show, :params => { :display_name => not_seen_user.display_name }
1096 assert_response :success
1097 assert_select "div#userinformation" do
1098 assert_select "p", :count => 1, :text => /Contributor terms/
1099 assert_select "p", /Undecided/
1103 def test_make_friend
1104 # Get users to work with
1105 user = create(:user)
1106 friend = create(:user)
1108 # Check that the users aren't already friends
1109 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1111 # When not logged in a GET should ask us to login
1112 get :make_friend, :params => { :display_name => friend.display_name }
1113 assert_redirected_to :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1115 # When not logged in a POST should error
1116 post :make_friend, :params => { :display_name => friend.display_name }
1117 assert_response :forbidden
1118 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1120 # When logged in a GET should get a confirmation page
1121 get :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1122 assert_response :success
1123 assert_template :make_friend
1124 assert_select "form" do
1125 assert_select "input[type='hidden'][name='referer']", 0
1126 assert_select "input[type='submit']", 1
1128 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1130 # When logged in a POST should add the friendship
1131 assert_difference "ActionMailer::Base.deliveries.size", 1 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(/is now your friend/, flash[:notice])
1138 assert Friendship.where(:befriender => user, :befriendee => friend).first
1139 email = ActionMailer::Base.deliveries.first
1140 assert_equal 1, email.to.count
1141 assert_equal friend.email, email.to.first
1142 ActionMailer::Base.deliveries.clear
1144 # A second POST should report that the friendship already exists
1145 assert_no_difference "ActionMailer::Base.deliveries.size" do
1146 perform_enqueued_jobs do
1147 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1150 assert_redirected_to user_path(friend)
1151 assert_match(/You are already friends with/, flash[:warning])
1152 assert Friendship.where(:befriender => user, :befriendee => friend).first
1155 def test_make_friend_with_referer
1156 # Get users to work with
1157 user = create(:user)
1158 friend = create(:user)
1160 # Check that the users aren't already friends
1161 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1163 # The GET should preserve any referer
1164 get :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1165 assert_response :success
1166 assert_template :make_friend
1167 assert_select "form" do
1168 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1169 assert_select "input[type='submit']", 1
1171 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1173 # When logged in a POST should add the friendship and refer us
1174 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1175 perform_enqueued_jobs do
1176 post :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1179 assert_redirected_to "/test"
1180 assert_match(/is now your friend/, flash[:notice])
1181 assert Friendship.where(:befriender => user, :befriendee => friend).first
1182 email = ActionMailer::Base.deliveries.first
1183 assert_equal 1, email.to.count
1184 assert_equal friend.email, email.to.first
1185 ActionMailer::Base.deliveries.clear
1188 def test_make_friend_unkown_user
1189 # Should error when a bogus user is specified
1190 get :make_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1191 assert_response :not_found
1192 assert_template :no_such_user
1195 def test_remove_friend
1196 # Get users to work with
1197 user = create(:user)
1198 friend = create(:user)
1199 create(:friendship, :befriender => user, :befriendee => friend)
1201 # Check that the users are friends
1202 assert Friendship.where(:befriender => user, :befriendee => friend).first
1204 # When not logged in a GET should ask us to login
1205 get :remove_friend, :params => { :display_name => friend.display_name }
1206 assert_redirected_to :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1208 # When not logged in a POST should error
1209 post :remove_friend, :params => { :display_name => friend.display_name }
1210 assert_response :forbidden
1211 assert Friendship.where(:befriender => user, :befriendee => friend).first
1213 # When logged in a GET should get a confirmation page
1214 get :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1215 assert_response :success
1216 assert_template :remove_friend
1217 assert_select "form" do
1218 assert_select "input[type='hidden'][name='referer']", 0
1219 assert_select "input[type='submit']", 1
1221 assert Friendship.where(:befriender => user, :befriendee => friend).first
1223 # When logged in a POST should remove the friendship
1224 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1225 assert_redirected_to user_path(friend)
1226 assert_match(/was removed from your friends/, flash[:notice])
1227 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1229 # A second POST should report that the friendship does not exist
1230 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1231 assert_redirected_to user_path(friend)
1232 assert_match(/is not one of your friends/, flash[:error])
1233 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1236 def test_remove_friend_with_referer
1237 # Get users to work with
1238 user = create(:user)
1239 friend = create(:user)
1240 create(:friendship, :befriender => user, :befriendee => friend)
1242 # Check that the users are friends
1243 assert Friendship.where(:befriender => user, :befriendee => friend).first
1245 # The GET should preserve any referer
1246 get :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1247 assert_response :success
1248 assert_template :remove_friend
1249 assert_select "form" do
1250 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1251 assert_select "input[type='submit']", 1
1253 assert Friendship.where(:befriender => user, :befriendee => friend).first
1255 # When logged in a POST should remove the friendship and refer
1256 post :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1257 assert_redirected_to "/test"
1258 assert_match(/was removed from your friends/, flash[:notice])
1259 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1262 def test_remove_friend_unkown_user
1263 # Should error when a bogus user is specified
1264 get :remove_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1265 assert_response :not_found
1266 assert_template :no_such_user
1270 user = create(:user)
1272 # Try without logging in
1273 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }
1274 assert_response :redirect
1275 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1277 # Now try as a normal user
1278 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1279 assert_response :redirect
1280 assert_redirected_to :controller => :errors, :action => :forbidden
1282 # Finally try as an administrator
1283 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1284 assert_response :redirect
1285 assert_redirected_to :action => :show, :display_name => user.display_name
1286 assert_equal "suspended", User.find(user.id).status
1290 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1292 # Try without logging in
1293 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }
1294 assert_response :redirect
1295 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1297 # Now try as a normal user
1298 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1299 assert_response :redirect
1300 assert_redirected_to :controller => :errors, :action => :forbidden
1302 # Finally try as an administrator
1303 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1304 assert_response :redirect
1305 assert_redirected_to :action => :show, :display_name => user.display_name
1307 # Check that the user was deleted properly
1309 assert_equal "user_#{user.id}", user.display_name
1310 assert_equal "", user.description
1311 assert_nil user.home_lat
1312 assert_nil user.home_lon
1313 assert_equal false, user.avatar.attached?
1314 assert_equal false, user.email_valid
1315 assert_nil user.new_email
1316 assert_nil user.auth_provider
1317 assert_nil user.auth_uid
1318 assert_equal "deleted", user.status
1322 user = create(:user)
1323 moderator_user = create(:moderator_user)
1324 administrator_user = create(:administrator_user)
1325 _suspended_user = create(:user, :suspended)
1326 _ip_user = create(:user, :creation_ip => "1.2.3.4")
1328 # There are now 7 users - the five above, plus two extra "granters" for the
1329 # moderator_user and administrator_user
1330 assert_equal 7, User.count
1332 # Shouldn't work when not logged in
1334 assert_response :redirect
1335 assert_redirected_to :action => :login, :referer => users_path
1337 session[:user] = user.id
1339 # Shouldn't work when logged in as a normal user
1341 assert_response :redirect
1342 assert_redirected_to :controller => :errors, :action => :forbidden
1344 session[:user] = moderator_user.id
1346 # Shouldn't work when logged in as a moderator
1348 assert_response :redirect
1349 assert_redirected_to :controller => :errors, :action => :forbidden
1351 session[:user] = administrator_user.id
1353 # Note there is a header row, so all row counts are users + 1
1354 # Should work when logged in as an administrator
1356 assert_response :success
1357 assert_template :index
1358 assert_select "table#user_list tr", :count => 7 + 1
1360 # Should be able to limit by status
1361 get :index, :params => { :status => "suspended" }
1362 assert_response :success
1363 assert_template :index
1364 assert_select "table#user_list tr", :count => 1 + 1
1366 # Should be able to limit by IP address
1367 get :index, :params => { :ip => "1.2.3.4" }
1368 assert_response :success
1369 assert_template :index
1370 assert_select "table#user_list tr", :count => 1 + 1
1373 def test_index_get_paginated
1374 1.upto(100).each do |n|
1375 User.create(:display_name => "extra_#{n}",
1376 :email => "extra#{n}@example.com",
1377 :pass_crypt => "extraextra")
1380 session[:user] = create(:administrator_user).id
1382 # 100 examples, an administrator, and a granter for the admin.
1383 assert_equal 102, User.count
1386 assert_response :success
1387 assert_template :index
1388 assert_select "table#user_list tr", :count => 51
1390 get :index, :params => { :page => 2 }
1391 assert_response :success
1392 assert_template :index
1393 assert_select "table#user_list tr", :count => 51
1395 get :index, :params => { :page => 3 }
1396 assert_response :success
1397 assert_template :index
1398 assert_select "table#user_list tr", :count => 3
1401 def test_index_post_confirm
1402 inactive_user = create(:user, :pending)
1403 suspended_user = create(:user, :suspended)
1405 # Shouldn't work when not logged in
1406 assert_no_difference "User.active.count" do
1407 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1409 assert_response :forbidden
1411 assert_equal "pending", inactive_user.reload.status
1412 assert_equal "suspended", suspended_user.reload.status
1414 session[:user] = create(:user).id
1416 # Shouldn't work when logged in as a normal user
1417 assert_no_difference "User.active.count" do
1418 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1420 assert_response :redirect
1421 assert_redirected_to :controller => :errors, :action => :forbidden
1422 assert_equal "pending", inactive_user.reload.status
1423 assert_equal "suspended", suspended_user.reload.status
1425 session[:user] = create(:moderator_user).id
1427 # Shouldn't work when logged in as a moderator
1428 assert_no_difference "User.active.count" do
1429 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1431 assert_response :redirect
1432 assert_redirected_to :controller => :errors, :action => :forbidden
1433 assert_equal "pending", inactive_user.reload.status
1434 assert_equal "suspended", suspended_user.reload.status
1436 session[:user] = create(:administrator_user).id
1438 # Should work when logged in as an administrator
1439 assert_difference "User.active.count", 2 do
1440 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1442 assert_response :redirect
1443 assert_redirected_to :action => :index
1444 assert_equal "confirmed", inactive_user.reload.status
1445 assert_equal "confirmed", suspended_user.reload.status
1448 def test_index_post_hide
1449 normal_user = create(:user)
1450 confirmed_user = create(:user, :confirmed)
1452 # Shouldn't work when not logged in
1453 assert_no_difference "User.active.count" do
1454 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1456 assert_response :forbidden
1458 assert_equal "active", normal_user.reload.status
1459 assert_equal "confirmed", confirmed_user.reload.status
1461 session[:user] = create(:user).id
1463 # Shouldn't work when logged in as a normal user
1464 assert_no_difference "User.active.count" do
1465 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1467 assert_response :redirect
1468 assert_redirected_to :controller => :errors, :action => :forbidden
1469 assert_equal "active", normal_user.reload.status
1470 assert_equal "confirmed", confirmed_user.reload.status
1472 session[:user] = create(:moderator_user).id
1474 # Shouldn't work when logged in as a moderator
1475 assert_no_difference "User.active.count" do
1476 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1478 assert_response :redirect
1479 assert_redirected_to :controller => :errors, :action => :forbidden
1480 assert_equal "active", normal_user.reload.status
1481 assert_equal "confirmed", confirmed_user.reload.status
1483 session[:user] = create(:administrator_user).id
1485 # Should work when logged in as an administrator
1486 assert_difference "User.active.count", -2 do
1487 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1489 assert_response :redirect
1490 assert_redirected_to :action => :index
1491 assert_equal "deleted", normal_user.reload.status
1492 assert_equal "deleted", confirmed_user.reload.status