3 class UserControllerTest < ActionController::TestCase
9 # test all routes which lead to this controller
12 { :path => "/api/0.6/user/1", :method => :get },
13 { :controller => "user", :action => "api_read", :id => "1" }
16 { :path => "/api/0.6/user/details", :method => :get },
17 { :controller => "user", :action => "api_details" }
20 { :path => "/api/0.6/user/gpx_files", :method => :get },
21 { :controller => "user", :action => "api_gpx_files" }
25 { :path => "/login", :method => :get },
26 { :controller => "user", :action => "login" }
29 { :path => "/login", :method => :post },
30 { :controller => "user", :action => "login" }
33 { :controller => "user", :action => "login", :format => "html" },
34 { :path => "/login.html", :method => :get }
38 { :path => "/logout", :method => :get },
39 { :controller => "user", :action => "logout" }
42 { :path => "/logout", :method => :post },
43 { :controller => "user", :action => "logout" }
46 { :controller => "user", :action => "logout", :format => "html" },
47 { :path => "/logout.html", :method => :get }
51 { :path => "/user/new", :method => :get },
52 { :controller => "user", :action => "new" }
56 { :path => "/user/new", :method => :post },
57 { :controller => "user", :action => "create" }
61 { :path => "/user/terms", :method => :get },
62 { :controller => "user", :action => "terms" }
66 { :path => "/user/save", :method => :post },
67 { :controller => "user", :action => "save" }
71 { :path => "/user/username/confirm", :method => :get },
72 { :controller => "user", :action => "confirm", :display_name => "username" }
75 { :path => "/user/username/confirm", :method => :post },
76 { :controller => "user", :action => "confirm", :display_name => "username" }
79 { :path => "/user/username/confirm/resend", :method => :get },
80 { :controller => "user", :action => "confirm_resend", :display_name => "username" }
84 { :path => "/user/confirm", :method => :get },
85 { :controller => "user", :action => "confirm" }
88 { :path => "/user/confirm", :method => :post },
89 { :controller => "user", :action => "confirm" }
92 { :path => "/user/confirm-email", :method => :get },
93 { :controller => "user", :action => "confirm_email" }
96 { :path => "/user/confirm-email", :method => :post },
97 { :controller => "user", :action => "confirm_email" }
101 { :path => "/user/go_public", :method => :post },
102 { :controller => "user", :action => "go_public" }
106 { :path => "/user/forgot-password", :method => :get },
107 { :controller => "user", :action => "lost_password" }
110 { :path => "/user/forgot-password", :method => :post },
111 { :controller => "user", :action => "lost_password" }
114 { :path => "/user/reset-password", :method => :get },
115 { :controller => "user", :action => "reset_password" }
118 { :path => "/user/reset-password", :method => :post },
119 { :controller => "user", :action => "reset_password" }
123 { :path => "/user/suspended", :method => :get },
124 { :controller => "user", :action => "suspended" }
128 { :path => "/user/username", :method => :get },
129 { :controller => "user", :action => "view", :display_name => "username" }
133 { :path => "/user/username/account", :method => :get },
134 { :controller => "user", :action => "account", :display_name => "username" }
137 { :path => "/user/username/account", :method => :post },
138 { :controller => "user", :action => "account", :display_name => "username" }
142 { :path => "/user/username/make_friend", :method => :get },
143 { :controller => "user", :action => "make_friend", :display_name => "username" }
146 { :path => "/user/username/make_friend", :method => :post },
147 { :controller => "user", :action => "make_friend", :display_name => "username" }
150 { :path => "/user/username/remove_friend", :method => :get },
151 { :controller => "user", :action => "remove_friend", :display_name => "username" }
154 { :path => "/user/username/remove_friend", :method => :post },
155 { :controller => "user", :action => "remove_friend", :display_name => "username" }
159 { :path => "/user/username/set_status", :method => :get },
160 { :controller => "user", :action => "set_status", :display_name => "username" }
163 { :path => "/user/username/delete", :method => :get },
164 { :controller => "user", :action => "delete", :display_name => "username" }
168 { :path => "/users", :method => :get },
169 { :controller => "user", :action => "list" }
172 { :path => "/users", :method => :post },
173 { :controller => "user", :action => "list" }
176 { :path => "/users/status", :method => :get },
177 { :controller => "user", :action => "list", :status => "status" }
180 { :path => "/users/status", :method => :post },
181 { :controller => "user", :action => "list", :status => "status" }
185 # The user creation page loads
188 assert_response :redirect
189 assert_redirected_to user_new_path(:cookie_test => "true")
191 get :new, { :cookie_test => "true" }, { :cookie_test => true }
192 assert_response :success
194 assert_select "html", :count => 1 do
195 assert_select "head", :count => 1 do
196 assert_select "title", :text => /Sign Up/, :count => 1
198 assert_select "body", :count => 1 do
199 assert_select "div#content", :count => 1 do
200 assert_select "form[action='/user/new'][method='post']", :count => 1 do
201 assert_select "input[id='user_email']", :count => 1
202 assert_select "input[id='user_email_confirmation']", :count => 1
203 assert_select "input[id='user_display_name']", :count => 1
204 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
205 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
206 assert_select "input[type='submit'][value='Sign Up']", :count => 1
213 def test_new_view_logged_in
214 session[:user] = create(:user).id
217 assert_response :redirect
218 assert_redirected_to user_new_path(:cookie_test => "true")
219 get :new, :cookie_test => "true"
220 assert_response :redirect
221 assert_redirected_to root_path
223 get :new, :referer => "/test"
224 assert_response :redirect
225 assert_redirected_to user_new_path(:referer => "/test", :cookie_test => "true")
226 get :new, :referer => "/test", :cookie_test => "true"
227 assert_response :redirect
228 assert_redirected_to "/test"
232 user = build(:user, :pending)
234 assert_difference "User.count", 1 do
235 assert_difference "ActionMailer::Base.deliveries.size", 1 do
236 post :save, {}, { :new_user => user }
241 register_email = ActionMailer::Base.deliveries.first
243 assert_equal register_email.to[0], user.email
244 assert_match /#{@url}/, register_email.body.to_s
247 assert_redirected_to :action => "confirm", :display_name => user.display_name
249 ActionMailer::Base.deliveries.clear
252 def test_new_duplicate_email
253 user = build(:user, :pending)
254 user.email = create(:user).email
256 assert_no_difference "User.count" do
257 assert_no_difference "ActionMailer::Base.deliveries.size" do
258 post :save, {}, { :new_user => user }
262 assert_response :success
263 assert_template "new"
264 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
267 def test_new_duplicate_email_uppercase
268 user = build(:user, :pending)
269 user.email = create(:user).email.upcase
271 assert_no_difference "User.count" do
272 assert_no_difference "ActionMailer::Base.deliveries.size" do
273 post :save, {}, { :new_user => user }
277 assert_response :success
278 assert_template "new"
279 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
282 def test_new_duplicate_name
283 user = build(:user, :pending)
284 user.display_name = create(:user).display_name
286 assert_no_difference "User.count" do
287 assert_no_difference "ActionMailer::Base.deliveries.size" do
288 post :save, {}, { :new_user => user }
292 assert_response :success
293 assert_template "new"
294 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
297 def test_new_duplicate_name_uppercase
298 user = build(:user, :pending)
299 user.display_name = create(:user).display_name.upcase
301 assert_no_difference "User.count" do
302 assert_no_difference "ActionMailer::Base.deliveries.size" do
303 post :save, {}, { :new_user => user }
307 assert_response :success
308 assert_template "new"
309 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
312 def test_save_referer_params
313 user = build(:user, :pending)
315 assert_difference "User.count", 1 do
316 assert_difference "ActionMailer::Base.deliveries.size", 1 do
317 post :save, {}, { :new_user => user,
318 :referer => "/edit?editor=id#map=1/2/3" }
322 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
323 user.tokens.order("id DESC").first.referer
325 ActionMailer::Base.deliveries.clear
328 def test_logout_without_referer
330 assert_response :success
331 assert_template :logout
332 assert_select "input[name=referer][value=?]", ""
334 session_id = assert_select("input[name=session]").first["value"]
336 get :logout, :session => session_id
337 assert_response :redirect
338 assert_redirected_to root_path
341 def test_logout_with_referer
342 get :logout, :referer => "/test"
343 assert_response :success
344 assert_template :logout
345 assert_select "input[name=referer][value=?]", "/test"
347 session_id = assert_select("input[name=session]").first["value"]
349 get :logout, :session => session_id, :referer => "/test"
350 assert_response :redirect
351 assert_redirected_to "/test"
354 def test_logout_with_token
355 token = create(:user).tokens.create
357 session[:token] = token.token
360 assert_response :success
361 assert_template :logout
362 assert_select "input[name=referer][value=?]", ""
363 assert_equal token.token, session[:token]
364 assert_not_nil UserToken.where(:id => token.id).first
366 session_id = assert_select("input[name=session]").first["value"]
368 get :logout, :session => session_id
369 assert_response :redirect
370 assert_redirected_to root_path
371 assert_nil session[:token]
372 assert_nil UserToken.where(:id => token.id).first
376 user = create(:user, :pending)
377 confirm_string = user.tokens.create.token
379 @request.cookies["_osm_session"] = user.display_name
380 get :confirm, :display_name => user.display_name, :confirm_string => confirm_string
381 assert_response :success
382 assert_template :confirm
385 def test_confirm_get_already_confirmed
387 confirm_string = user.tokens.create.token
389 @request.cookies["_osm_session"] = user.display_name
390 get :confirm, :display_name => user.display_name, :confirm_string => confirm_string
391 assert_response :redirect
392 assert_redirected_to root_path
395 def test_confirm_success_no_token_no_referer
396 user = create(:user, :pending)
397 stub_gravatar_request(user.email)
398 confirm_string = user.tokens.create.token
400 @request.cookies["_osm_session"] = user.display_name
401 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
402 assert_redirected_to login_path
403 assert_match /Confirmed your account/, flash[:notice]
406 def test_confirm_success_good_token_no_referer
407 user = create(:user, :pending)
408 stub_gravatar_request(user.email)
409 confirm_string = user.tokens.create.token
410 token = user.tokens.create.token
412 @request.cookies["_osm_session"] = user.display_name
413 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
414 assert_redirected_to welcome_path
417 def test_confirm_success_bad_token_no_referer
418 user = create(:user, :pending)
419 stub_gravatar_request(user.email)
420 confirm_string = user.tokens.create.token
421 token = create(:user).tokens.create.token
423 @request.cookies["_osm_session"] = user.display_name
424 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
425 assert_redirected_to login_path
426 assert_match /Confirmed your account/, flash[:notice]
429 def test_confirm_success_no_token_with_referer
430 user = create(:user, :pending)
431 stub_gravatar_request(user.email)
432 confirm_string = user.tokens.create(:referer => diary_new_path).token
434 @request.cookies["_osm_session"] = user.display_name
435 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
436 assert_redirected_to login_path(:referer => diary_new_path)
437 assert_match /Confirmed your account/, flash[:notice]
440 def test_confirm_success_good_token_with_referer
441 user = create(:user, :pending)
442 stub_gravatar_request(user.email)
443 confirm_string = user.tokens.create(:referer => diary_new_path).token
444 token = user.tokens.create.token
446 @request.cookies["_osm_session"] = user.display_name
447 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
448 assert_redirected_to diary_new_path
451 def test_confirm_success_bad_token_with_referer
452 user = create(:user, :pending)
453 stub_gravatar_request(user.email)
454 confirm_string = user.tokens.create(:referer => diary_new_path).token
455 token = create(:user).tokens.create.token
457 @request.cookies["_osm_session"] = user.display_name
458 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
459 assert_redirected_to login_path(:referer => diary_new_path)
460 assert_match /Confirmed your account/, flash[:notice]
463 def test_confirm_expired_token
464 user = create(:user, :pending)
465 confirm_string = user.tokens.create(:expiry => 1.day.ago).token
467 @request.cookies["_osm_session"] = user.display_name
468 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
469 assert_redirected_to :action => "confirm"
470 assert_match /confirmation code has expired/, flash[:error]
473 def test_confirm_already_confirmed
475 confirm_string = user.tokens.create(:referer => diary_new_path).token
477 @request.cookies["_osm_session"] = user.display_name
478 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
479 assert_redirected_to :action => "login"
480 assert_match /already been confirmed/, flash[:error]
483 def test_confirm_resend_success
484 user = create(:user, :pending)
485 session[:token] = user.tokens.create.token
487 assert_difference "ActionMailer::Base.deliveries.size", 1 do
488 get :confirm_resend, :display_name => user.display_name
491 assert_response :redirect
492 assert_redirected_to login_path
493 assert_match /sent a new confirmation/, flash[:notice]
495 email = ActionMailer::Base.deliveries.last
497 assert_equal user.email, email.to.first
499 ActionMailer::Base.deliveries.clear
502 def test_confirm_resend_no_token
503 user = create(:user, :pending)
504 assert_no_difference "ActionMailer::Base.deliveries.size" do
505 get :confirm_resend, :display_name => user.display_name
508 assert_response :redirect
509 assert_redirected_to login_path
510 assert_match "User #{user.display_name} not found.", flash[:error]
513 def test_confirm_resend_unknown_user
514 assert_no_difference "ActionMailer::Base.deliveries.size" do
515 get :confirm_resend, :display_name => "No Such User"
518 assert_response :redirect
519 assert_redirected_to login_path
520 assert_match "User No Such User not found.", flash[:error]
523 def test_confirm_email_get
525 confirm_string = user.tokens.create.token
527 get :confirm_email, :confirm_string => confirm_string
528 assert_response :success
529 assert_template :confirm_email
532 def test_confirm_email_success
533 user = create(:user, :new_email => "test-new@example.com")
534 stub_gravatar_request(user.new_email)
535 confirm_string = user.tokens.create.token
537 post :confirm_email, :confirm_string => confirm_string
538 assert_response :redirect
539 assert_redirected_to :action => :account, :display_name => user.display_name
540 assert_match /Confirmed your change of email address/, flash[:notice]
543 def test_confirm_email_already_confirmed
545 confirm_string = user.tokens.create.token
547 post :confirm_email, :confirm_string => confirm_string
548 assert_response :redirect
549 assert_redirected_to :action => :account, :display_name => user.display_name
550 assert_match /already been confirmed/, flash[:error]
553 def test_confirm_email_bad_token
554 post :confirm_email, :confirm_string => "XXXXX"
555 assert_response :success
556 assert_template :confirm_email
557 assert_match /confirmation code has expired or does not exist/, flash[:error]
561 # test if testing for a gravatar works
562 # this happens when the email is actually changed
563 # which is triggered by the confirmation mail
564 def test_gravatar_auto_enable
565 # switch to email that has a gravatar
566 user = create(:user, :new_email => "test-new@example.com")
567 stub_gravatar_request(user.new_email, 200)
568 confirm_string = user.tokens.create.token
569 # precondition gravatar should be turned off
570 assert !user.image_use_gravatar
571 post :confirm_email, :confirm_string => confirm_string
572 assert_response :redirect
573 assert_redirected_to :action => :account, :display_name => user.display_name
574 assert_match /Confirmed your change of email address/, flash[:notice]
575 # gravatar use should now be enabled
576 assert User.find(user.id).image_use_gravatar
579 def test_gravatar_auto_disable
580 # switch to email without a gravatar
581 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
582 stub_gravatar_request(user.new_email, 404)
583 confirm_string = user.tokens.create.token
584 # precondition gravatar should be turned on
585 assert user.image_use_gravatar
586 post :confirm_email, :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 disabled
591 assert !User.find(user.id).image_use_gravatar
594 def test_terms_new_user
595 get :terms, {}, { :new_user => User.new }
596 assert_response :success
597 assert_template :terms
600 def test_terms_agreed
601 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
603 session[:user] = user.id
606 assert_response :redirect
607 assert_redirected_to :action => :account, :display_name => user.display_name
610 def test_terms_not_seen_without_referer
611 user = create(:user, :terms_seen => false)
613 session[:user] = user.id
616 assert_response :success
617 assert_template :terms
619 post :save, :user => { :consider_pd => true }
620 assert_response :redirect
621 assert_redirected_to :action => :account, :display_name => user.display_name
622 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
626 assert_equal true, user.consider_pd
627 assert_not_nil user.terms_agreed
628 assert_equal true, user.terms_seen
631 def test_terms_not_seen_with_referer
632 user = create(:user, :terms_seen => false)
634 session[:user] = user.id
636 get :terms, :referer => "/test"
637 assert_response :success
638 assert_template :terms
640 post :save, :user => { :consider_pd => true }, :referer => "/test"
641 assert_response :redirect
642 assert_redirected_to "/test"
643 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
647 assert_equal true, user.consider_pd
648 assert_not_nil user.terms_agreed
649 assert_equal true, user.terms_seen
653 user = create(:user, :data_public => false)
654 post :go_public, {}, { :user => user }
655 assert_response :redirect
656 assert_redirected_to :action => :account, :display_name => user.display_name
657 assert_equal true, User.find(user.id).data_public
660 def test_lost_password
661 # Test fetching the lost password page
663 assert_response :success
664 assert_template :lost_password
665 assert_select "div#notice", false
667 # Test resetting using the address as recorded for a user that has an
668 # address which is duplicated in a different case by another user
670 uppercase_user = build(:user, :email => user.email.upcase).tap { |u| u.save(:validate => false) }
672 assert_difference "ActionMailer::Base.deliveries.size", 1 do
673 post :lost_password, :user => { :email => user.email }
675 assert_response :redirect
676 assert_redirected_to :action => :login
677 assert_match /^Sorry you lost it/, flash[:notice]
678 email = ActionMailer::Base.deliveries.first
679 assert_equal 1, email.to.count
680 assert_equal user.email, email.to.first
681 ActionMailer::Base.deliveries.clear
683 # Test resetting using an address that matches a different user
684 # that has the same address in a different case
685 assert_difference "ActionMailer::Base.deliveries.size", 1 do
686 post :lost_password, :user => { :email => user.email.upcase }
688 assert_response :redirect
689 assert_redirected_to :action => :login
690 assert_match /^Sorry you lost it/, flash[:notice]
691 email = ActionMailer::Base.deliveries.first
692 assert_equal 1, email.to.count
693 assert_equal uppercase_user.email, email.to.first
694 ActionMailer::Base.deliveries.clear
696 # Test resetting using an address that is a case insensitive match
697 # for more than one user but not an exact match for either
698 assert_no_difference "ActionMailer::Base.deliveries.size" do
699 post :lost_password, :user => { :email => user.email.titlecase }
701 assert_response :success
702 assert_template :lost_password
703 assert_select ".error", /^Could not find that email address/
705 # Test resetting using the address as recorded for a user that has an
706 # address which is case insensitively unique
707 third_user = create(:user)
708 assert_difference "ActionMailer::Base.deliveries.size", 1 do
709 post :lost_password, :user => { :email => third_user.email }
711 assert_response :redirect
712 assert_redirected_to :action => :login
713 assert_match /^Sorry you lost it/, flash[:notice]
714 email = ActionMailer::Base.deliveries.first
715 assert_equal 1, email.to.count
716 assert_equal third_user.email, email.to.first
717 ActionMailer::Base.deliveries.clear
719 # Test resetting using an address that matches a user that has the
720 # same (case insensitively unique) address in a different case
721 assert_difference "ActionMailer::Base.deliveries.size", 1 do
722 post :lost_password, :user => { :email => third_user.email.upcase }
724 assert_response :redirect
725 assert_redirected_to :action => :login
726 assert_match /^Sorry you lost it/, flash[:notice]
727 email = ActionMailer::Base.deliveries.first
728 assert_equal 1, email.to.count
729 assert_equal third_user.email, email.to.first
730 ActionMailer::Base.deliveries.clear
733 def test_reset_password
734 user = create(:user, :pending)
735 # Test a request with no token
737 assert_response :bad_request
739 # Test a request with a bogus token
740 get :reset_password, :token => "made_up_token"
741 assert_response :redirect
742 assert_redirected_to :action => :lost_password
744 # Create a valid token for a user
745 token = user.tokens.create
747 # Test a request with a valid token
748 get :reset_password, :token => token.token
749 assert_response :success
750 assert_template :reset_password
752 # Test setting a new password
753 post :reset_password, :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" }
754 assert_response :redirect
755 assert_redirected_to root_path
756 assert_equal user.id, session[:user]
758 assert_equal "active", user.status
759 assert_equal true, user.email_valid
760 assert_equal user, User.authenticate(:username => user.email, :password => "new_password")
764 # Get a user to work with - note that this user deliberately
765 # conflicts with uppercase_user in the email and display name
766 # fields to test that we can change other fields without any
767 # validation errors being reported
768 user = create(:user, :languages => [])
769 _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
771 # Make sure that you are redirected to the login page when
772 # you are not logged in
773 get :account, :display_name => user.display_name
774 assert_response :redirect
775 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/#{URI.encode(user.display_name)}/account"
777 # Make sure that you are blocked when not logged in as the right user
778 get :account, { :display_name => user.display_name }, { :user => create(:user) }
779 assert_response :forbidden
781 # Make sure we get the page when we are logged in as the right user
782 get :account, { :display_name => user.display_name }, { :user => user }
783 assert_response :success
784 assert_template :account
786 # Updating the description should work
787 user.description = "new description"
788 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user }
789 assert_response :success
790 assert_template :account
791 assert_select "div#errorExplanation", false
792 assert_select ".notice", /^User information updated successfully/
793 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
795 # Changing to a invalid editor should fail
796 user.preferred_editor = "unknown"
797 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user }
798 assert_response :success
799 assert_template :account
800 assert_select ".notice", false
801 assert_select "div#errorExplanation"
802 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
804 # Changing to a valid editor should work
805 user.preferred_editor = "potlatch2"
806 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user }
807 assert_response :success
808 assert_template :account
809 assert_select "div#errorExplanation", false
810 assert_select ".notice", /^User information updated successfully/
811 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
813 # Changing to the default editor should work
814 user.preferred_editor = "default"
815 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user }
816 assert_response :success
817 assert_template :account
818 assert_select "div#errorExplanation", false
819 assert_select ".notice", /^User information updated successfully/
820 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
822 # Changing to an uploaded image should work
823 image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif")
824 post :account, { :display_name => user.display_name, :image_action => "new", :user => user.attributes.merge(:image => image) }, { :user => user }
825 assert_response :success
826 assert_template :account
827 assert_select "div#errorExplanation", false
828 assert_select ".notice", /^User information updated successfully/
829 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "keep"
831 # Changing to a gravatar image should work
832 post :account, { :display_name => user.display_name, :image_action => "gravatar", :user => user.attributes }, { :user => user }
833 assert_response :success
834 assert_template :account
835 assert_select "div#errorExplanation", false
836 assert_select ".notice", /^User information updated successfully/
837 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "gravatar"
839 # Removing the image should work
840 post :account, { :display_name => user.display_name, :image_action => "delete", :user => user.attributes }, { :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.accountImage input[name=image_action][checked]", false
847 # Adding external authentication should redirect to the auth provider
848 post :account, { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, { :user => user }
849 assert_response :redirect
850 assert_redirected_to auth_path(:provider => "openid", :openid_url => "https://www.google.com/accounts/o8/id", :origin => "/user/#{URI.encode(user.display_name)}/account")
852 # Changing name to one that exists should fail
853 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
854 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user }
855 assert_response :success
856 assert_template :account
857 assert_select ".notice", false
858 assert_select "div#errorExplanation"
859 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
861 # Changing name to one that exists should fail, regardless of case
862 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
863 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user }
864 assert_response :success
865 assert_template :account
866 assert_select ".notice", false
867 assert_select "div#errorExplanation"
868 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
870 # Changing name to one that doesn't exist should work
871 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
872 post :account, { :display_name => user.display_name, :user => new_attributes }, { :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 > input#user_display_name[value=?]", "new tester"
879 # Record the change of name
880 user.display_name = "new tester"
882 # Changing email to one that exists should fail
883 user.new_email = create(:user).email
884 assert_no_difference "ActionMailer::Base.deliveries.size" do
885 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user }
887 assert_response :success
888 assert_template :account
889 assert_select ".notice", false
890 assert_select "div#errorExplanation"
891 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
893 # Changing email to one that exists should fail, regardless of case
894 user.new_email = create(:user).email.upcase
895 assert_no_difference "ActionMailer::Base.deliveries.size" do
896 post :account, { :display_name => user.display_name, :user => user.attributes }, { :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_new_email"
904 # Changing email to one that doesn't exist should work
905 user.new_email = "new_tester@example.com"
906 assert_difference "ActionMailer::Base.deliveries.size", 1 do
907 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user }
909 assert_response :success
910 assert_template :account
911 assert_select "div#errorExplanation", false
912 assert_select ".notice", /^User information updated successfully/
913 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
914 email = ActionMailer::Base.deliveries.first
915 assert_equal 1, email.to.count
916 assert_equal user.new_email, email.to.first
917 ActionMailer::Base.deliveries.clear
920 # Check that the user account page will display and contains some relevant
921 # information for the user
923 # Test a non-existent user
924 get :view, :display_name => "unknown"
925 assert_response :not_found
929 get :view, :display_name => user.display_name
930 assert_response :success
931 assert_select "div#userinformation" do
932 assert_select "a[href^='/user/#{URI.encode(user.display_name)}/history']", 1
933 assert_select "a[href='/user/#{URI.encode(user.display_name)}/traces']", 1
934 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary']", 1
935 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary/comments']", 1
936 assert_select "a[href='/user/#{URI.encode(user.display_name)}/account']", 0
937 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks']", 0
938 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks_by']", 0
939 assert_select "a[href='/blocks/new/#{URI.encode(user.display_name)}']", 0
942 # Test a user who has been blocked
943 blocked_user = create(:user)
944 create(:user_block, :user => blocked_user)
945 get :view, :display_name => blocked_user.display_name
946 assert_response :success
947 assert_select "div#userinformation" do
948 assert_select "a[href^='/user/#{URI.encode(blocked_user.display_name)}/history']", 1
949 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/traces']", 1
950 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/diary']", 1
951 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/diary/comments']", 1
952 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/account']", 0
953 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/blocks']", 1
954 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/blocks_by']", 0
955 assert_select "a[href='/blocks/new/#{URI.encode(blocked_user.display_name)}']", 0
958 # Test a moderator who has applied blocks
959 moderator_user = create(:moderator_user)
960 create(:user_block, :creator => moderator_user)
961 get :view, :display_name => moderator_user.display_name
962 assert_response :success
963 assert_select "div#userinformation" do
964 assert_select "a[href^='/user/#{URI.encode(moderator_user.display_name)}/history']", 1
965 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/traces']", 1
966 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/diary']", 1
967 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/diary/comments']", 1
968 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/account']", 0
969 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/blocks']", 0
970 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/blocks_by']", 1
971 assert_select "a[href='/blocks/new/#{URI.encode(moderator_user.display_name)}']", 0
974 # Login as a normal user
975 session[:user] = user.id
977 # Test the normal user
978 get :view, :display_name => user.display_name
979 assert_response :success
980 assert_select "div#userinformation" do
981 assert_select "a[href^='/user/#{URI.encode(user.display_name)}/history']", 1
982 assert_select "a[href='/traces/mine']", 1
983 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary']", 1
984 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary/comments']", 1
985 assert_select "a[href='/user/#{URI.encode(user.display_name)}/account']", 1
986 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks']", 0
987 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks_by']", 0
988 assert_select "a[href='/blocks/new/#{URI.encode(user.display_name)}']", 0
991 # Login as a moderator
992 session[:user] = create(:moderator_user).id
994 # Test the normal user
995 get :view, :display_name => user.display_name
996 assert_response :success
997 assert_select "div#userinformation" do
998 assert_select "a[href^='/user/#{URI.encode(user.display_name)}/history']", 1
999 assert_select "a[href='/user/#{URI.encode(user.display_name)}/traces']", 1
1000 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary']", 1
1001 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary/comments']", 1
1002 assert_select "a[href='/user/#{URI.encode(user.display_name)}/account']", 0
1003 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks']", 0
1004 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks_by']", 0
1005 assert_select "a[href='/blocks/new/#{URI.encode(user.display_name)}']", 1
1010 user = create(:user, :description => "test", :terms_agreed => Date.yesterday)
1011 # check that a visible user is returned properly
1012 get :api_read, :id => user.id
1013 assert_response :success
1014 assert_equal "text/xml", response.content_type
1016 # check the data that is returned
1017 assert_select "description", :count => 1, :text => "test"
1018 assert_select "contributor-terms", :count => 1 do
1019 assert_select "[agreed='true']"
1021 assert_select "img", :count => 0
1022 assert_select "roles", :count => 1 do
1023 assert_select "role", :count => 0
1025 assert_select "changesets", :count => 1 do
1026 assert_select "[count='0']"
1028 assert_select "traces", :count => 1 do
1029 assert_select "[count='0']"
1031 assert_select "blocks", :count => 1 do
1032 assert_select "received", :count => 1 do
1033 assert_select "[count='0'][active='0']"
1035 assert_select "issued", :count => 0
1038 # check that we aren't revealing private information
1039 assert_select "contributor-terms[pd]", false
1040 assert_select "home", false
1041 assert_select "languages", false
1042 assert_select "messages", false
1044 # check that a suspended user is not returned
1045 get :api_read, :id => create(:user, :suspended).id
1046 assert_response :gone
1048 # check that a deleted user is not returned
1049 get :api_read, :id => create(:user, :deleted).id
1050 assert_response :gone
1052 # check that a non-existent user is not returned
1053 get :api_read, :id => 0
1054 assert_response :not_found
1057 def test_api_details
1058 user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"])
1059 create(:message, :read, :recipient => user)
1060 create(:message, :sender => user)
1062 # check that nothing is returned when not logged in
1064 assert_response :unauthorized
1066 # check that we get a response when logged in
1067 basic_authorization(user.email, "test")
1069 assert_response :success
1070 assert_equal "text/xml", response.content_type
1072 # check the data that is returned
1073 assert_select "description", :count => 1, :text => "test"
1074 assert_select "contributor-terms", :count => 1 do
1075 assert_select "[agreed='true'][pd='false']"
1077 assert_select "img", :count => 0
1078 assert_select "roles", :count => 1 do
1079 assert_select "role", :count => 0
1081 assert_select "changesets", :count => 1 do
1082 assert_select "[count='0']", :count => 1
1084 assert_select "traces", :count => 1 do
1085 assert_select "[count='0']", :count => 1
1087 assert_select "blocks", :count => 1 do
1088 assert_select "received", :count => 1 do
1089 assert_select "[count='0'][active='0']"
1091 assert_select "issued", :count => 0
1093 assert_select "home", :count => 1 do
1094 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
1096 assert_select "languages", :count => 1 do
1097 assert_select "lang", :count => 1, :text => "en"
1099 assert_select "messages", :count => 1 do
1100 assert_select "received", :count => 1 do
1101 assert_select "[count='1'][unread='0']"
1103 assert_select "sent", :count => 1 do
1104 assert_select "[count='1']"
1109 def test_api_gpx_files
1110 user = create(:user)
1111 trace1 = create(:trace, :user => user) do |trace|
1112 create(:tracetag, :trace => trace, :tag => "London")
1114 trace2 = create(:trace, :user => user) do |trace|
1115 create(:tracetag, :trace => trace, :tag => "Birmingham")
1117 # check that nothing is returned when not logged in
1119 assert_response :unauthorized
1121 # check that we get a response when logged in
1122 basic_authorization(user.email, "test")
1124 assert_response :success
1125 assert_equal "text/xml", response.content_type
1127 # check the data that is returned
1128 assert_select "gpx_file[id='#{trace1.id}']", 1 do
1129 assert_select "tag", "London"
1131 assert_select "gpx_file[id='#{trace2.id}']", 1 do
1132 assert_select "tag", "Birmingham"
1136 def test_make_friend
1137 # Get users to work with
1138 user = create(:user)
1139 friend = create(:user)
1141 # Check that the users aren't already friends
1142 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1144 # When not logged in a GET should ask us to login
1145 get :make_friend, :display_name => friend.display_name
1146 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1148 # When not logged in a POST should error
1149 post :make_friend, :display_name => friend.display_name
1150 assert_response :forbidden
1151 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1153 # When logged in a GET should get a confirmation page
1154 get :make_friend, { :display_name => friend.display_name }, { :user => user }
1155 assert_response :success
1156 assert_template :make_friend
1157 assert_select "form" do
1158 assert_select "input[type='hidden'][name='referer']", 0
1159 assert_select "input[type='submit']", 1
1161 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1163 # When logged in a POST should add the friendship
1164 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1165 post :make_friend, { :display_name => friend.display_name }, { :user => user }
1167 assert_redirected_to user_path(:display_name => friend.display_name)
1168 assert_match /is now your friend/, flash[:notice]
1169 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1170 email = ActionMailer::Base.deliveries.first
1171 assert_equal 1, email.to.count
1172 assert_equal friend.email, email.to.first
1173 ActionMailer::Base.deliveries.clear
1175 # A second POST should report that the friendship already exists
1176 assert_no_difference "ActionMailer::Base.deliveries.size" do
1177 post :make_friend, { :display_name => friend.display_name }, { :user => user }
1179 assert_redirected_to user_path(:display_name => friend.display_name)
1180 assert_match /You are already friends with/, flash[:warning]
1181 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1184 def test_make_friend_with_referer
1185 # Get users to work with
1186 user = create(:user)
1187 friend = create(:user)
1189 # Check that the users aren't already friends
1190 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1192 # The GET should preserve any referer
1193 get :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user }
1194 assert_response :success
1195 assert_template :make_friend
1196 assert_select "form" do
1197 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1198 assert_select "input[type='submit']", 1
1200 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1202 # When logged in a POST should add the friendship and refer us
1203 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1204 post :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user }
1206 assert_redirected_to "/test"
1207 assert_match /is now your friend/, flash[:notice]
1208 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1209 email = ActionMailer::Base.deliveries.first
1210 assert_equal 1, email.to.count
1211 assert_equal friend.email, email.to.first
1212 ActionMailer::Base.deliveries.clear
1215 def test_make_friend_unkown_user
1216 # Should error when a bogus user is specified
1217 get :make_friend, { :display_name => "No Such User" }, { :user => create(:user) }
1218 assert_response :not_found
1219 assert_template :no_such_user
1222 def test_remove_friend
1223 # Get users to work with
1224 user = create(:user)
1225 friend = create(:user)
1226 create(:friend, :befriender => user, :befriendee => friend)
1228 # Check that the users are friends
1229 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1231 # When not logged in a GET should ask us to login
1232 get :remove_friend, :display_name => friend.display_name
1233 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1235 # When not logged in a POST should error
1236 post :remove_friend, :display_name => friend.display_name
1237 assert_response :forbidden
1238 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1240 # When logged in a GET should get a confirmation page
1241 get :remove_friend, { :display_name => friend.display_name }, { :user => user }
1242 assert_response :success
1243 assert_template :remove_friend
1244 assert_select "form" do
1245 assert_select "input[type='hidden'][name='referer']", 0
1246 assert_select "input[type='submit']", 1
1248 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1250 # When logged in a POST should remove the friendship
1251 post :remove_friend, { :display_name => friend.display_name }, { :user => user }
1252 assert_redirected_to user_path(:display_name => friend.display_name)
1253 assert_match /was removed from your friends/, flash[:notice]
1254 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1256 # A second POST should report that the friendship does not exist
1257 post :remove_friend, { :display_name => friend.display_name }, { :user => user }
1258 assert_redirected_to user_path(:display_name => friend.display_name)
1259 assert_match /is not one of your friends/, flash[:error]
1260 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1263 def test_remove_friend_with_referer
1264 # Get users to work with
1265 user = create(:user)
1266 friend = create(:user)
1267 create(:friend, :user_id => user.id, :friend_user_id => friend.id)
1269 # Check that the users are friends
1270 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1272 # The GET should preserve any referer
1273 get :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user }
1274 assert_response :success
1275 assert_template :remove_friend
1276 assert_select "form" do
1277 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1278 assert_select "input[type='submit']", 1
1280 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1282 # When logged in a POST should remove the friendship and refer
1283 post :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user }
1284 assert_redirected_to "/test"
1285 assert_match /was removed from your friends/, flash[:notice]
1286 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1289 def test_remove_friend_unkown_user
1290 # Should error when a bogus user is specified
1291 get :remove_friend, { :display_name => "No Such User" }, { :user => create(:user) }
1292 assert_response :not_found
1293 assert_template :no_such_user
1297 user = create(:user)
1299 # Try without logging in
1300 get :set_status, :display_name => user.display_name, :status => "suspended"
1301 assert_response :redirect
1302 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1304 # Now try as a normal user
1305 get :set_status, { :display_name => user.display_name, :status => "suspended" }, { :user => user }
1306 assert_response :redirect
1307 assert_redirected_to :action => :view, :display_name => user.display_name
1309 # Finally try as an administrator
1310 get :set_status, { :display_name => user.display_name, :status => "suspended" }, { :user => create(:administrator_user) }
1311 assert_response :redirect
1312 assert_redirected_to :action => :view, :display_name => user.display_name
1313 assert_equal "suspended", User.find(user.id).status
1317 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1319 # Try without logging in
1320 get :delete, :display_name => user.display_name, :status => "suspended"
1321 assert_response :redirect
1322 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1324 # Now try as a normal user
1325 get :delete, { :display_name => user.display_name, :status => "suspended" }, { :user => user }
1326 assert_response :redirect
1327 assert_redirected_to :action => :view, :display_name => user.display_name
1329 # Finally try as an administrator
1330 get :delete, { :display_name => user.display_name, :status => "suspended" }, { :user => create(:administrator_user) }
1331 assert_response :redirect
1332 assert_redirected_to :action => :view, :display_name => user.display_name
1334 # Check that the user was deleted properly
1336 assert_equal "user_#{user.id}", user.display_name
1337 assert_equal "", user.description
1338 assert_nil user.home_lat
1339 assert_nil user.home_lon
1340 assert_equal false, user.image.file?
1341 assert_equal false, user.email_valid
1342 assert_nil user.new_email
1343 assert_nil user.auth_provider
1344 assert_nil user.auth_uid
1345 assert_equal "deleted", user.status
1349 user = create(:user)
1350 moderator_user = create(:moderator_user)
1351 administrator_user = create(:administrator_user)
1352 suspended_user = create(:user, :suspended)
1353 ip_user = create(:user, :creation_ip => "1.2.3.4")
1355 # There are now 7 users - the five above, plus two extra "granters" for the
1356 # moderator_user and administrator_user
1357 assert_equal 7, User.count
1359 # Shouldn't work when not logged in
1361 assert_response :redirect
1362 assert_redirected_to :action => :login, :referer => users_path
1364 session[:user] = user.id
1366 # Shouldn't work when logged in as a normal user
1368 assert_response :redirect
1369 assert_redirected_to :action => :login, :referer => users_path
1371 session[:user] = moderator_user.id
1373 # Shouldn't work when logged in as a moderator
1375 assert_response :redirect
1376 assert_redirected_to :action => :login, :referer => users_path
1378 session[:user] = administrator_user.id
1380 # Note there is a header row, so all row counts are users + 1
1381 # Should work when logged in as an administrator
1383 assert_response :success
1384 assert_template :list
1385 assert_select "table#user_list tr", :count => 7 + 1
1387 # Should be able to limit by status
1388 get :list, :status => "suspended"
1389 assert_response :success
1390 assert_template :list
1391 assert_select "table#user_list tr", :count => 1 + 1
1393 # Should be able to limit by IP address
1394 get :list, :ip => "1.2.3.4"
1395 assert_response :success
1396 assert_template :list
1397 assert_select "table#user_list tr", :count => 1 + 1
1400 def test_list_get_paginated
1401 1.upto(100).each do |n|
1402 User.create(:display_name => "extra_#{n}",
1403 :email => "extra#{n}@example.com",
1404 :pass_crypt => "extraextra")
1407 session[:user] = create(:administrator_user).id
1409 # 100 examples, an administrator, and a granter for the admin.
1410 assert_equal 102, User.count
1413 assert_response :success
1414 assert_template :list
1415 assert_select "table#user_list tr", :count => 51
1417 get :list, :page => 2
1418 assert_response :success
1419 assert_template :list
1420 assert_select "table#user_list tr", :count => 51
1422 get :list, :page => 3
1423 assert_response :success
1424 assert_template :list
1425 assert_select "table#user_list tr", :count => 3
1428 def test_list_post_confirm
1429 inactive_user = create(:user, :pending)
1430 suspended_user = create(:user, :suspended)
1432 # Shouldn't work when not logged in
1433 assert_no_difference "User.active.count" do
1434 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1436 assert_response :redirect
1437 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1438 assert_equal "pending", inactive_user.reload.status
1439 assert_equal "suspended", suspended_user.reload.status
1441 session[:user] = create(:user).id
1443 # Shouldn't work when logged in as a normal user
1444 assert_no_difference "User.active.count" do
1445 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1447 assert_response :redirect
1448 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1449 assert_equal "pending", inactive_user.reload.status
1450 assert_equal "suspended", suspended_user.reload.status
1452 session[:user] = create(:moderator_user).id
1454 # Shouldn't work when logged in as a moderator
1455 assert_no_difference "User.active.count" do
1456 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1458 assert_response :redirect
1459 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1460 assert_equal "pending", inactive_user.reload.status
1461 assert_equal "suspended", suspended_user.reload.status
1463 session[:user] = create(:administrator_user).id
1465 # Should work when logged in as an administrator
1466 assert_difference "User.active.count", 2 do
1467 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1469 assert_response :redirect
1470 assert_redirected_to :action => :list
1471 assert_equal "confirmed", inactive_user.reload.status
1472 assert_equal "confirmed", suspended_user.reload.status
1475 def test_list_post_hide
1476 normal_user = create(:user)
1477 confirmed_user = create(:user, :confirmed)
1479 # Shouldn't work when not logged in
1480 assert_no_difference "User.active.count" do
1481 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1483 assert_response :redirect
1484 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1485 assert_equal "active", normal_user.reload.status
1486 assert_equal "confirmed", confirmed_user.reload.status
1488 session[:user] = create(:user).id
1490 # Shouldn't work when logged in as a normal user
1491 assert_no_difference "User.active.count" do
1492 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1494 assert_response :redirect
1495 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1496 assert_equal "active", normal_user.reload.status
1497 assert_equal "confirmed", confirmed_user.reload.status
1499 session[:user] = create(:moderator_user).id
1501 # Shouldn't work when logged in as a moderator
1502 assert_no_difference "User.active.count" do
1503 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1505 assert_response :redirect
1506 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1507 assert_equal "active", normal_user.reload.status
1508 assert_equal "confirmed", confirmed_user.reload.status
1510 session[:user] = create(:administrator_user).id
1512 # Should work when logged in as an administrator
1513 assert_difference "User.active.count", -2 do
1514 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1516 assert_response :redirect
1517 assert_redirected_to :action => :list
1518 assert_equal "deleted", normal_user.reload.status
1519 assert_equal "deleted", confirmed_user.reload.status