3 class UserControllerTest < ActionController::TestCase
11 # test all routes which lead to this controller
14 { :path => "/api/0.6/user/1", :method => :get },
15 { :controller => "user", :action => "api_read", :id => "1" }
18 { :path => "/api/0.6/user/details", :method => :get },
19 { :controller => "user", :action => "api_details" }
22 { :path => "/api/0.6/user/gpx_files", :method => :get },
23 { :controller => "user", :action => "api_gpx_files" }
27 { :path => "/login", :method => :get },
28 { :controller => "user", :action => "login" }
31 { :path => "/login", :method => :post },
32 { :controller => "user", :action => "login" }
35 { :controller => "user", :action => "login", :format => "html" },
36 { :path => "/login.html", :method => :get }
40 { :path => "/logout", :method => :get },
41 { :controller => "user", :action => "logout" }
44 { :path => "/logout", :method => :post },
45 { :controller => "user", :action => "logout" }
48 { :controller => "user", :action => "logout", :format => "html" },
49 { :path => "/logout.html", :method => :get }
53 { :path => "/user/new", :method => :get },
54 { :controller => "user", :action => "new" }
58 { :path => "/user/new", :method => :post },
59 { :controller => "user", :action => "create" }
63 { :path => "/user/terms", :method => :get },
64 { :controller => "user", :action => "terms" }
68 { :path => "/user/save", :method => :post },
69 { :controller => "user", :action => "save" }
73 { :path => "/user/username/confirm", :method => :get },
74 { :controller => "user", :action => "confirm", :display_name => "username" }
77 { :path => "/user/username/confirm", :method => :post },
78 { :controller => "user", :action => "confirm", :display_name => "username" }
81 { :path => "/user/username/confirm/resend", :method => :get },
82 { :controller => "user", :action => "confirm_resend", :display_name => "username" }
86 { :path => "/user/confirm", :method => :get },
87 { :controller => "user", :action => "confirm" }
90 { :path => "/user/confirm", :method => :post },
91 { :controller => "user", :action => "confirm" }
94 { :path => "/user/confirm-email", :method => :get },
95 { :controller => "user", :action => "confirm_email" }
98 { :path => "/user/confirm-email", :method => :post },
99 { :controller => "user", :action => "confirm_email" }
103 { :path => "/user/go_public", :method => :post },
104 { :controller => "user", :action => "go_public" }
108 { :path => "/user/forgot-password", :method => :get },
109 { :controller => "user", :action => "lost_password" }
112 { :path => "/user/forgot-password", :method => :post },
113 { :controller => "user", :action => "lost_password" }
116 { :path => "/user/reset-password", :method => :get },
117 { :controller => "user", :action => "reset_password" }
120 { :path => "/user/reset-password", :method => :post },
121 { :controller => "user", :action => "reset_password" }
125 { :path => "/user/suspended", :method => :get },
126 { :controller => "user", :action => "suspended" }
130 { :path => "/user/username", :method => :get },
131 { :controller => "user", :action => "view", :display_name => "username" }
135 { :path => "/user/username/account", :method => :get },
136 { :controller => "user", :action => "account", :display_name => "username" }
139 { :path => "/user/username/account", :method => :post },
140 { :controller => "user", :action => "account", :display_name => "username" }
144 { :path => "/user/username/make_friend", :method => :get },
145 { :controller => "user", :action => "make_friend", :display_name => "username" }
148 { :path => "/user/username/make_friend", :method => :post },
149 { :controller => "user", :action => "make_friend", :display_name => "username" }
152 { :path => "/user/username/remove_friend", :method => :get },
153 { :controller => "user", :action => "remove_friend", :display_name => "username" }
156 { :path => "/user/username/remove_friend", :method => :post },
157 { :controller => "user", :action => "remove_friend", :display_name => "username" }
161 { :path => "/user/username/set_status", :method => :get },
162 { :controller => "user", :action => "set_status", :display_name => "username" }
165 { :path => "/user/username/delete", :method => :get },
166 { :controller => "user", :action => "delete", :display_name => "username" }
170 { :path => "/users", :method => :get },
171 { :controller => "user", :action => "list" }
174 { :path => "/users", :method => :post },
175 { :controller => "user", :action => "list" }
178 { :path => "/users/status", :method => :get },
179 { :controller => "user", :action => "list", :status => "status" }
182 { :path => "/users/status", :method => :post },
183 { :controller => "user", :action => "list", :status => "status" }
187 # The user creation page loads
190 assert_response :redirect
191 assert_redirected_to user_new_path(:cookie_test => "true")
193 get :new, { :cookie_test => "true" }, { :cookie_test => true }
194 assert_response :success
196 assert_select "html", :count => 1 do
197 assert_select "head", :count => 1 do
198 assert_select "title", :text => /Sign Up/, :count => 1
200 assert_select "body", :count => 1 do
201 assert_select "div#content", :count => 1 do
202 assert_select "form[action='/user/new'][method='post']", :count => 1 do
203 assert_select "input[id='user_email']", :count => 1
204 assert_select "input[id='user_email_confirmation']", :count => 1
205 assert_select "input[id='user_display_name']", :count => 1
206 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
207 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
208 assert_select "input[type='submit'][value='Sign Up']", :count => 1
215 def test_new_view_logged_in
216 session[:user] = create(:user).id
219 assert_response :redirect
220 assert_redirected_to user_new_path(:cookie_test => "true")
221 get :new, :cookie_test => "true"
222 assert_response :redirect
223 assert_redirected_to root_path
225 get :new, :referer => "/test"
226 assert_response :redirect
227 assert_redirected_to user_new_path(:referer => "/test", :cookie_test => "true")
228 get :new, :referer => "/test", :cookie_test => "true"
229 assert_response :redirect
230 assert_redirected_to "/test"
234 user = build(:user, :pending)
236 assert_difference "User.count", 1 do
237 assert_difference "ActionMailer::Base.deliveries.size", 1 do
238 post :save, {}, { :new_user => user }
243 register_email = ActionMailer::Base.deliveries.first
245 assert_equal register_email.to[0], user.email
246 assert_match /#{@url}/, register_email.body.to_s
249 assert_redirected_to :action => "confirm", :display_name => user.display_name
251 ActionMailer::Base.deliveries.clear
254 def test_new_duplicate_email
255 user = build(:user, :pending)
256 user.email = create(:user).email
258 assert_no_difference "User.count" do
259 assert_no_difference "ActionMailer::Base.deliveries.size" do
260 post :save, {}, { :new_user => user }
264 assert_response :success
265 assert_template "new"
266 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
269 def test_new_duplicate_email_uppercase
270 user = build(:user, :pending)
271 user.email = create(:user).email.upcase
273 assert_no_difference "User.count" do
274 assert_no_difference "ActionMailer::Base.deliveries.size" do
275 post :save, {}, { :new_user => user }
279 assert_response :success
280 assert_template "new"
281 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
284 def test_new_duplicate_name
285 user = build(:user, :pending)
286 user.display_name = create(:user).display_name
288 assert_no_difference "User.count" do
289 assert_no_difference "ActionMailer::Base.deliveries.size" do
290 post :save, {}, { :new_user => user }
294 assert_response :success
295 assert_template "new"
296 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
299 def test_new_duplicate_name_uppercase
300 user = build(:user, :pending)
301 user.display_name = create(:user).display_name.upcase
303 assert_no_difference "User.count" do
304 assert_no_difference "ActionMailer::Base.deliveries.size" do
305 post :save, {}, { :new_user => user }
309 assert_response :success
310 assert_template "new"
311 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
314 def test_save_referer_params
315 user = build(:user, :pending)
317 assert_difference "User.count", 1 do
318 assert_difference "ActionMailer::Base.deliveries.size", 1 do
319 post :save, {}, { :new_user => user,
320 :referer => "/edit?editor=id#map=1/2/3" }
324 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
325 user.tokens.order("id DESC").first.referer
327 ActionMailer::Base.deliveries.clear
330 def test_logout_without_referer
332 assert_response :success
333 assert_template :logout
334 assert_select "input[name=referer][value=?]", ""
336 session_id = assert_select("input[name=session]").first["value"]
338 get :logout, :session => session_id
339 assert_response :redirect
340 assert_redirected_to root_path
343 def test_logout_with_referer
344 get :logout, :referer => "/test"
345 assert_response :success
346 assert_template :logout
347 assert_select "input[name=referer][value=?]", "/test"
349 session_id = assert_select("input[name=session]").first["value"]
351 get :logout, :session => session_id, :referer => "/test"
352 assert_response :redirect
353 assert_redirected_to "/test"
356 def test_logout_with_token
357 token = create(:user).tokens.create
359 session[:token] = token.token
362 assert_response :success
363 assert_template :logout
364 assert_select "input[name=referer][value=?]", ""
365 assert_equal token.token, session[:token]
366 assert_not_nil UserToken.where(:id => token.id).first
368 session_id = assert_select("input[name=session]").first["value"]
370 get :logout, :session => session_id
371 assert_response :redirect
372 assert_redirected_to root_path
373 assert_nil session[:token]
374 assert_nil UserToken.where(:id => token.id).first
378 user = create(:user, :pending)
379 confirm_string = user.tokens.create.token
381 @request.cookies["_osm_session"] = user.display_name
382 get :confirm, :display_name => user.display_name, :confirm_string => confirm_string
383 assert_response :success
384 assert_template :confirm
387 def test_confirm_get_already_confirmed
389 confirm_string = user.tokens.create.token
391 @request.cookies["_osm_session"] = user.display_name
392 get :confirm, :display_name => user.display_name, :confirm_string => confirm_string
393 assert_response :redirect
394 assert_redirected_to root_path
397 def test_confirm_success_no_token_no_referer
398 user = create(:user, :pending)
399 stub_gravatar_request(user.email)
400 confirm_string = user.tokens.create.token
402 @request.cookies["_osm_session"] = user.display_name
403 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
404 assert_redirected_to login_path
405 assert_match /Confirmed your account/, flash[:notice]
408 def test_confirm_success_good_token_no_referer
409 user = create(:user, :pending)
410 stub_gravatar_request(user.email)
411 confirm_string = user.tokens.create.token
412 token = user.tokens.create.token
414 @request.cookies["_osm_session"] = user.display_name
415 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
416 assert_redirected_to welcome_path
419 def test_confirm_success_bad_token_no_referer
420 user = create(:user, :pending)
421 stub_gravatar_request(user.email)
422 confirm_string = user.tokens.create.token
423 token = create(:user).tokens.create.token
425 @request.cookies["_osm_session"] = user.display_name
426 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
427 assert_redirected_to login_path
428 assert_match /Confirmed your account/, flash[:notice]
431 def test_confirm_success_no_token_with_referer
432 user = create(:user, :pending)
433 stub_gravatar_request(user.email)
434 confirm_string = user.tokens.create(:referer => diary_new_path).token
436 @request.cookies["_osm_session"] = user.display_name
437 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
438 assert_redirected_to login_path(:referer => diary_new_path)
439 assert_match /Confirmed your account/, flash[:notice]
442 def test_confirm_success_good_token_with_referer
443 user = create(:user, :pending)
444 stub_gravatar_request(user.email)
445 confirm_string = user.tokens.create(:referer => diary_new_path).token
446 token = user.tokens.create.token
448 @request.cookies["_osm_session"] = user.display_name
449 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
450 assert_redirected_to diary_new_path
453 def test_confirm_success_bad_token_with_referer
454 user = create(:user, :pending)
455 stub_gravatar_request(user.email)
456 confirm_string = user.tokens.create(:referer => diary_new_path).token
457 token = create(:user).tokens.create.token
459 @request.cookies["_osm_session"] = user.display_name
460 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
461 assert_redirected_to login_path(:referer => diary_new_path)
462 assert_match /Confirmed your account/, flash[:notice]
465 def test_confirm_expired_token
466 user = create(:user, :pending)
467 confirm_string = user.tokens.create(:expiry => 1.day.ago).token
469 @request.cookies["_osm_session"] = user.display_name
470 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
471 assert_redirected_to :action => "confirm"
472 assert_match /confirmation code has expired/, flash[:error]
475 def test_confirm_already_confirmed
477 confirm_string = user.tokens.create(:referer => diary_new_path).token
479 @request.cookies["_osm_session"] = user.display_name
480 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
481 assert_redirected_to :action => "login"
482 assert_match /already been confirmed/, flash[:error]
485 def test_confirm_resend_success
486 user = create(:user, :pending)
487 session[:token] = user.tokens.create.token
489 assert_difference "ActionMailer::Base.deliveries.size", 1 do
490 get :confirm_resend, :display_name => user.display_name
493 assert_response :redirect
494 assert_redirected_to login_path
495 assert_match /sent a new confirmation/, flash[:notice]
497 email = ActionMailer::Base.deliveries.last
499 assert_equal user.email, email.to.first
501 ActionMailer::Base.deliveries.clear
504 def test_confirm_resend_no_token
505 user = create(:user, :pending)
506 assert_no_difference "ActionMailer::Base.deliveries.size" do
507 get :confirm_resend, :display_name => user.display_name
510 assert_response :redirect
511 assert_redirected_to login_path
512 assert_match "User #{user.display_name} not found.", flash[:error]
515 def test_confirm_resend_unknown_user
516 assert_no_difference "ActionMailer::Base.deliveries.size" do
517 get :confirm_resend, :display_name => "No Such User"
520 assert_response :redirect
521 assert_redirected_to login_path
522 assert_match "User No Such User not found.", flash[:error]
525 def test_confirm_email_get
527 confirm_string = user.tokens.create.token
529 get :confirm_email, :confirm_string => confirm_string
530 assert_response :success
531 assert_template :confirm_email
534 def test_confirm_email_success
535 user = create(:user, :new_email => "test-new@example.com")
536 stub_gravatar_request(user.new_email)
537 confirm_string = user.tokens.create.token
539 post :confirm_email, :confirm_string => confirm_string
540 assert_response :redirect
541 assert_redirected_to :action => :account, :display_name => user.display_name
542 assert_match /Confirmed your change of email address/, flash[:notice]
545 def test_confirm_email_already_confirmed
547 confirm_string = user.tokens.create.token
549 post :confirm_email, :confirm_string => confirm_string
550 assert_response :redirect
551 assert_redirected_to :action => :account, :display_name => user.display_name
552 assert_match /already been confirmed/, flash[:error]
555 def test_confirm_email_bad_token
556 post :confirm_email, :confirm_string => "XXXXX"
557 assert_response :success
558 assert_template :confirm_email
559 assert_match /confirmation code has expired or does not exist/, flash[:error]
563 # test if testing for a gravatar works
564 # this happens when the email is actually changed
565 # which is triggered by the confirmation mail
566 def test_gravatar_auto_enable
567 # switch to email that has a gravatar
568 user = create(:user, :new_email => "test-new@example.com")
569 stub_gravatar_request(user.new_email, 200)
570 confirm_string = user.tokens.create.token
571 # precondition gravatar should be turned off
572 assert !user.image_use_gravatar
573 post :confirm_email, :confirm_string => confirm_string
574 assert_response :redirect
575 assert_redirected_to :action => :account, :display_name => user.display_name
576 assert_match /Confirmed your change of email address/, flash[:notice]
577 # gravatar use should now be enabled
578 assert User.find(user.id).image_use_gravatar
581 def test_gravatar_auto_disable
582 # switch to email without a gravatar
583 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
584 stub_gravatar_request(user.new_email, 404)
585 confirm_string = user.tokens.create.token
586 # precondition gravatar should be turned on
587 assert user.image_use_gravatar
588 post :confirm_email, :confirm_string => confirm_string
589 assert_response :redirect
590 assert_redirected_to :action => :account, :display_name => user.display_name
591 assert_match /Confirmed your change of email address/, flash[:notice]
592 # gravatar use should now be disabled
593 assert !User.find(user.id).image_use_gravatar
596 def test_terms_new_user
597 get :terms, {}, { :new_user => User.new }
598 assert_response :success
599 assert_template :terms
602 def test_terms_agreed
603 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
605 session[:user] = user.id
608 assert_response :redirect
609 assert_redirected_to :action => :account, :display_name => user.display_name
612 def test_terms_not_seen_without_referer
613 user = create(:user, :terms_seen => false)
615 session[:user] = user.id
618 assert_response :success
619 assert_template :terms
621 post :save, :user => { :consider_pd => true }
622 assert_response :redirect
623 assert_redirected_to :action => :account, :display_name => user.display_name
624 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
628 assert_equal true, user.consider_pd
629 assert_not_nil user.terms_agreed
630 assert_equal true, user.terms_seen
633 def test_terms_not_seen_with_referer
634 user = create(:user, :terms_seen => false)
636 session[:user] = user.id
638 get :terms, :referer => "/test"
639 assert_response :success
640 assert_template :terms
642 post :save, :user => { :consider_pd => true }, :referer => "/test"
643 assert_response :redirect
644 assert_redirected_to "/test"
645 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
649 assert_equal true, user.consider_pd
650 assert_not_nil user.terms_agreed
651 assert_equal true, user.terms_seen
655 user = create(:user, :data_public => false)
656 post :go_public, {}, { :user => user }
657 assert_response :redirect
658 assert_redirected_to :action => :account, :display_name => user.display_name
659 assert_equal true, User.find(user.id).data_public
662 def test_lost_password
663 # Test fetching the lost password page
665 assert_response :success
666 assert_template :lost_password
667 assert_select "div#notice", false
669 # Test resetting using the address as recorded for a user that has an
670 # address which is duplicated in a different case by another user
672 uppercase_user = build(:user, :email => user.email.upcase).tap { |u| u.save(:validate => false) }
674 assert_difference "ActionMailer::Base.deliveries.size", 1 do
675 post :lost_password, :user => { :email => user.email }
677 assert_response :redirect
678 assert_redirected_to :action => :login
679 assert_match /^Sorry you lost it/, flash[:notice]
680 email = ActionMailer::Base.deliveries.first
681 assert_equal 1, email.to.count
682 assert_equal user.email, email.to.first
683 ActionMailer::Base.deliveries.clear
685 # Test resetting using an address that matches a different user
686 # that has the same address in a different case
687 assert_difference "ActionMailer::Base.deliveries.size", 1 do
688 post :lost_password, :user => { :email => user.email.upcase }
690 assert_response :redirect
691 assert_redirected_to :action => :login
692 assert_match /^Sorry you lost it/, flash[:notice]
693 email = ActionMailer::Base.deliveries.first
694 assert_equal 1, email.to.count
695 assert_equal uppercase_user.email, email.to.first
696 ActionMailer::Base.deliveries.clear
698 # Test resetting using an address that is a case insensitive match
699 # for more than one user but not an exact match for either
700 assert_no_difference "ActionMailer::Base.deliveries.size" do
701 post :lost_password, :user => { :email => user.email.titlecase }
703 assert_response :success
704 assert_template :lost_password
705 assert_select ".error", /^Could not find that email address/
707 # Test resetting using the address as recorded for a user that has an
708 # address which is case insensitively unique
709 third_user = create(:user)
710 assert_difference "ActionMailer::Base.deliveries.size", 1 do
711 post :lost_password, :user => { :email => third_user.email }
713 assert_response :redirect
714 assert_redirected_to :action => :login
715 assert_match /^Sorry you lost it/, flash[:notice]
716 email = ActionMailer::Base.deliveries.first
717 assert_equal 1, email.to.count
718 assert_equal third_user.email, email.to.first
719 ActionMailer::Base.deliveries.clear
721 # Test resetting using an address that matches a user that has the
722 # same (case insensitively unique) address in a different case
723 assert_difference "ActionMailer::Base.deliveries.size", 1 do
724 post :lost_password, :user => { :email => third_user.email.upcase }
726 assert_response :redirect
727 assert_redirected_to :action => :login
728 assert_match /^Sorry you lost it/, flash[:notice]
729 email = ActionMailer::Base.deliveries.first
730 assert_equal 1, email.to.count
731 assert_equal third_user.email, email.to.first
732 ActionMailer::Base.deliveries.clear
735 def test_reset_password
736 user = create(:user, :pending)
737 # Test a request with no token
739 assert_response :bad_request
741 # Test a request with a bogus token
742 get :reset_password, :token => "made_up_token"
743 assert_response :redirect
744 assert_redirected_to :action => :lost_password
746 # Create a valid token for a user
747 token = user.tokens.create
749 # Test a request with a valid token
750 get :reset_password, :token => token.token
751 assert_response :success
752 assert_template :reset_password
754 # Test setting a new password
755 post :reset_password, :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" }
756 assert_response :redirect
757 assert_redirected_to root_path
758 assert_equal user.id, session[:user]
760 assert_equal "active", user.status
761 assert_equal true, user.email_valid
762 assert_equal user, User.authenticate(:username => user.email, :password => "new_password")
766 # Get a user to work with - note that this user deliberately
767 # conflicts with uppercase_user in the email and display name
768 # fields to test that we can change other fields without any
769 # validation errors being reported
770 user = create(:user, :languages => [])
771 _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
773 # Make sure that you are redirected to the login page when
774 # you are not logged in
775 get :account, :display_name => user.display_name
776 assert_response :redirect
777 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/#{URI.encode(user.display_name)}/account"
779 # Make sure that you are blocked when not logged in as the right user
780 get :account, { :display_name => user.display_name }, { :user => create(:user) }
781 assert_response :forbidden
783 # Make sure we get the page when we are logged in as the right user
784 get :account, { :display_name => user.display_name }, { :user => user }
785 assert_response :success
786 assert_template :account
788 # Updating the description should work
789 user.description = "new description"
790 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user }
791 assert_response :success
792 assert_template :account
793 assert_select "div#errorExplanation", false
794 assert_select ".notice", /^User information updated successfully/
795 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
797 # Changing to a invalid editor should fail
798 user.preferred_editor = "unknown"
799 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user }
800 assert_response :success
801 assert_template :account
802 assert_select ".notice", false
803 assert_select "div#errorExplanation"
804 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
806 # Changing to a valid editor should work
807 user.preferred_editor = "potlatch2"
808 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user }
809 assert_response :success
810 assert_template :account
811 assert_select "div#errorExplanation", false
812 assert_select ".notice", /^User information updated successfully/
813 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
815 # Changing to the default editor should work
816 user.preferred_editor = "default"
817 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user }
818 assert_response :success
819 assert_template :account
820 assert_select "div#errorExplanation", false
821 assert_select ".notice", /^User information updated successfully/
822 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
824 # Changing to an uploaded image should work
825 image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif")
826 post :account, { :display_name => user.display_name, :image_action => "new", :user => user.attributes.merge(:image => image) }, { :user => user }
827 assert_response :success
828 assert_template :account
829 assert_select "div#errorExplanation", false
830 assert_select ".notice", /^User information updated successfully/
831 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "keep"
833 # Changing to a gravatar image should work
834 post :account, { :display_name => user.display_name, :image_action => "gravatar", :user => user.attributes }, { :user => user }
835 assert_response :success
836 assert_template :account
837 assert_select "div#errorExplanation", false
838 assert_select ".notice", /^User information updated successfully/
839 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "gravatar"
841 # Removing the image should work
842 post :account, { :display_name => user.display_name, :image_action => "delete", :user => user.attributes }, { :user => user }
843 assert_response :success
844 assert_template :account
845 assert_select "div#errorExplanation", false
846 assert_select ".notice", /^User information updated successfully/
847 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked]", false
849 # Adding external authentication should redirect to the auth provider
850 post :account, { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, { :user => user }
851 assert_response :redirect
852 assert_redirected_to auth_path(:provider => "openid", :openid_url => "https://www.google.com/accounts/o8/id", :origin => "/user/#{URI.encode(user.display_name)}/account")
854 # Changing name to one that exists should fail
855 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
856 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user }
857 assert_response :success
858 assert_template :account
859 assert_select ".notice", false
860 assert_select "div#errorExplanation"
861 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
863 # Changing name to one that exists should fail, regardless of case
864 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
865 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user }
866 assert_response :success
867 assert_template :account
868 assert_select ".notice", false
869 assert_select "div#errorExplanation"
870 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
872 # Changing name to one that doesn't exist should work
873 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
874 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user }
875 assert_response :success
876 assert_template :account
877 assert_select "div#errorExplanation", false
878 assert_select ".notice", /^User information updated successfully/
879 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
881 # Record the change of name
882 user.display_name = "new tester"
884 # Changing email to one that exists should fail
885 user.new_email = create(:user).email
886 assert_no_difference "ActionMailer::Base.deliveries.size" do
887 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user }
889 assert_response :success
890 assert_template :account
891 assert_select ".notice", false
892 assert_select "div#errorExplanation"
893 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
895 # Changing email to one that exists should fail, regardless of case
896 user.new_email = create(:user).email.upcase
897 assert_no_difference "ActionMailer::Base.deliveries.size" do
898 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user }
900 assert_response :success
901 assert_template :account
902 assert_select ".notice", false
903 assert_select "div#errorExplanation"
904 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
906 # Changing email to one that doesn't exist should work
907 user.new_email = "new_tester@example.com"
908 assert_difference "ActionMailer::Base.deliveries.size", 1 do
909 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user }
911 assert_response :success
912 assert_template :account
913 assert_select "div#errorExplanation", false
914 assert_select ".notice", /^User information updated successfully/
915 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
916 email = ActionMailer::Base.deliveries.first
917 assert_equal 1, email.to.count
918 assert_equal user.new_email, email.to.first
919 ActionMailer::Base.deliveries.clear
922 # Check that the user account page will display and contains some relevant
923 # information for the user
925 # Test a non-existent user
926 get :view, :display_name => "unknown"
927 assert_response :not_found
931 get :view, :display_name => user.display_name
932 assert_response :success
933 assert_select "div#userinformation" do
934 assert_select "a[href^='/user/#{URI.encode(user.display_name)}/history']", 1
935 assert_select "a[href='/user/#{URI.encode(user.display_name)}/traces']", 1
936 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary']", 1
937 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary/comments']", 1
938 assert_select "a[href='/user/#{URI.encode(user.display_name)}/account']", 0
939 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks']", 0
940 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks_by']", 0
941 assert_select "a[href='/blocks/new/#{URI.encode(user.display_name)}']", 0
944 # Test a user who has been blocked
945 blocked_user = create(:user)
946 create(:user_block, :user => blocked_user)
947 get :view, :display_name => blocked_user.display_name
948 assert_response :success
949 assert_select "div#userinformation" do
950 assert_select "a[href^='/user/#{URI.encode(blocked_user.display_name)}/history']", 1
951 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/traces']", 1
952 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/diary']", 1
953 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/diary/comments']", 1
954 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/account']", 0
955 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/blocks']", 1
956 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/blocks_by']", 0
957 assert_select "a[href='/blocks/new/#{URI.encode(blocked_user.display_name)}']", 0
960 # Test a moderator who has applied blocks
961 moderator_user = create(:moderator_user)
962 create(:user_block, :creator => moderator_user)
963 get :view, :display_name => moderator_user.display_name
964 assert_response :success
965 assert_select "div#userinformation" do
966 assert_select "a[href^='/user/#{URI.encode(moderator_user.display_name)}/history']", 1
967 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/traces']", 1
968 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/diary']", 1
969 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/diary/comments']", 1
970 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/account']", 0
971 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/blocks']", 0
972 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/blocks_by']", 1
973 assert_select "a[href='/blocks/new/#{URI.encode(moderator_user.display_name)}']", 0
976 # Login as a normal user
977 session[:user] = user.id
979 # Test the normal user
980 get :view, :display_name => user.display_name
981 assert_response :success
982 assert_select "div#userinformation" do
983 assert_select "a[href^='/user/#{URI.encode(user.display_name)}/history']", 1
984 assert_select "a[href='/traces/mine']", 1
985 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary']", 1
986 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary/comments']", 1
987 assert_select "a[href='/user/#{URI.encode(user.display_name)}/account']", 1
988 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks']", 0
989 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks_by']", 0
990 assert_select "a[href='/blocks/new/#{URI.encode(user.display_name)}']", 0
993 # Login as a moderator
994 session[:user] = create(:moderator_user).id
996 # Test the normal user
997 get :view, :display_name => user.display_name
998 assert_response :success
999 assert_select "div#userinformation" do
1000 assert_select "a[href^='/user/#{URI.encode(user.display_name)}/history']", 1
1001 assert_select "a[href='/user/#{URI.encode(user.display_name)}/traces']", 1
1002 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary']", 1
1003 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary/comments']", 1
1004 assert_select "a[href='/user/#{URI.encode(user.display_name)}/account']", 0
1005 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks']", 0
1006 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks_by']", 0
1007 assert_select "a[href='/blocks/new/#{URI.encode(user.display_name)}']", 1
1012 user = create(:user, :description => "test", :terms_agreed => Date.yesterday)
1013 # check that a visible user is returned properly
1014 get :api_read, :id => user.id
1015 assert_response :success
1016 assert_equal "text/xml", response.content_type
1018 # check the data that is returned
1019 assert_select "description", :count => 1, :text => "test"
1020 assert_select "contributor-terms", :count => 1 do
1021 assert_select "[agreed='true']"
1023 assert_select "img", :count => 0
1024 assert_select "roles", :count => 1 do
1025 assert_select "role", :count => 0
1027 assert_select "changesets", :count => 1 do
1028 assert_select "[count='0']"
1030 assert_select "traces", :count => 1 do
1031 assert_select "[count='0']"
1033 assert_select "blocks", :count => 1 do
1034 assert_select "received", :count => 1 do
1035 assert_select "[count='0'][active='0']"
1037 assert_select "issued", :count => 0
1040 # check that we aren't revealing private information
1041 assert_select "contributor-terms[pd]", false
1042 assert_select "home", false
1043 assert_select "languages", false
1044 assert_select "messages", false
1046 # check that a suspended user is not returned
1047 get :api_read, :id => create(:user, :suspended).id
1048 assert_response :gone
1050 # check that a deleted user is not returned
1051 get :api_read, :id => create(:user, :deleted).id
1052 assert_response :gone
1054 # check that a non-existent user is not returned
1055 get :api_read, :id => 0
1056 assert_response :not_found
1059 def test_api_details
1060 user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"])
1061 create(:message, :read, :recipient => user)
1062 create(:message, :sender => user)
1064 # check that nothing is returned when not logged in
1066 assert_response :unauthorized
1068 # check that we get a response when logged in
1069 basic_authorization(user.email, "test")
1071 assert_response :success
1072 assert_equal "text/xml", response.content_type
1074 # check the data that is returned
1075 assert_select "description", :count => 1, :text => "test"
1076 assert_select "contributor-terms", :count => 1 do
1077 assert_select "[agreed='true'][pd='false']"
1079 assert_select "img", :count => 0
1080 assert_select "roles", :count => 1 do
1081 assert_select "role", :count => 0
1083 assert_select "changesets", :count => 1 do
1084 assert_select "[count='0']", :count => 1
1086 assert_select "traces", :count => 1 do
1087 assert_select "[count='0']", :count => 1
1089 assert_select "blocks", :count => 1 do
1090 assert_select "received", :count => 1 do
1091 assert_select "[count='0'][active='0']"
1093 assert_select "issued", :count => 0
1095 assert_select "home", :count => 1 do
1096 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
1098 assert_select "languages", :count => 1 do
1099 assert_select "lang", :count => 1, :text => "en"
1101 assert_select "messages", :count => 1 do
1102 assert_select "received", :count => 1 do
1103 assert_select "[count='1'][unread='0']"
1105 assert_select "sent", :count => 1 do
1106 assert_select "[count='1']"
1111 def test_api_gpx_files
1112 user = create(:user)
1113 trace1 = create(:trace, :user => user) do |trace|
1114 create(:tracetag, :trace => trace, :tag => "London")
1116 trace2 = create(:trace, :user => user) do |trace|
1117 create(:tracetag, :trace => trace, :tag => "Birmingham")
1119 # check that nothing is returned when not logged in
1121 assert_response :unauthorized
1123 # check that we get a response when logged in
1124 basic_authorization(user.email, "test")
1126 assert_response :success
1127 assert_equal "text/xml", response.content_type
1129 # check the data that is returned
1130 assert_select "gpx_file[id='#{trace1.id}']", 1 do
1131 assert_select "tag", "London"
1133 assert_select "gpx_file[id='#{trace2.id}']", 1 do
1134 assert_select "tag", "Birmingham"
1138 def test_make_friend
1139 # Get users to work with
1140 user = create(:user)
1141 friend = create(:user)
1143 # Check that the users aren't already friends
1144 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1146 # When not logged in a GET should ask us to login
1147 get :make_friend, :display_name => friend.display_name
1148 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1150 # When not logged in a POST should error
1151 post :make_friend, :display_name => friend.display_name
1152 assert_response :forbidden
1153 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1155 # When logged in a GET should get a confirmation page
1156 get :make_friend, { :display_name => friend.display_name }, { :user => user }
1157 assert_response :success
1158 assert_template :make_friend
1159 assert_select "form" do
1160 assert_select "input[type='hidden'][name='referer']", 0
1161 assert_select "input[type='submit']", 1
1163 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1165 # When logged in a POST should add the friendship
1166 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1167 post :make_friend, { :display_name => friend.display_name }, { :user => user }
1169 assert_redirected_to user_path(:display_name => friend.display_name)
1170 assert_match /is now your friend/, flash[:notice]
1171 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1172 email = ActionMailer::Base.deliveries.first
1173 assert_equal 1, email.to.count
1174 assert_equal friend.email, email.to.first
1175 ActionMailer::Base.deliveries.clear
1177 # A second POST should report that the friendship already exists
1178 assert_no_difference "ActionMailer::Base.deliveries.size" do
1179 post :make_friend, { :display_name => friend.display_name }, { :user => user }
1181 assert_redirected_to user_path(:display_name => friend.display_name)
1182 assert_match /You are already friends with/, flash[:warning]
1183 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1186 def test_make_friend_with_referer
1187 # Get users to work with
1188 user = create(:user)
1189 friend = create(:user)
1191 # Check that the users aren't already friends
1192 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1194 # The GET should preserve any referer
1195 get :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user }
1196 assert_response :success
1197 assert_template :make_friend
1198 assert_select "form" do
1199 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1200 assert_select "input[type='submit']", 1
1202 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1204 # When logged in a POST should add the friendship and refer us
1205 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1206 post :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user }
1208 assert_redirected_to "/test"
1209 assert_match /is now your friend/, flash[:notice]
1210 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1211 email = ActionMailer::Base.deliveries.first
1212 assert_equal 1, email.to.count
1213 assert_equal friend.email, email.to.first
1214 ActionMailer::Base.deliveries.clear
1217 def test_make_friend_unkown_user
1218 # Should error when a bogus user is specified
1219 get :make_friend, { :display_name => "No Such User" }, { :user => create(:user) }
1220 assert_response :not_found
1221 assert_template :no_such_user
1224 def test_remove_friend
1225 # Get users to work with
1226 user = create(:user)
1227 friend = create(:user)
1228 create(:friend, :befriender => user, :befriendee => friend)
1230 # Check that the users are friends
1231 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1233 # When not logged in a GET should ask us to login
1234 get :remove_friend, :display_name => friend.display_name
1235 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1237 # When not logged in a POST should error
1238 post :remove_friend, :display_name => friend.display_name
1239 assert_response :forbidden
1240 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1242 # When logged in a GET should get a confirmation page
1243 get :remove_friend, { :display_name => friend.display_name }, { :user => user }
1244 assert_response :success
1245 assert_template :remove_friend
1246 assert_select "form" do
1247 assert_select "input[type='hidden'][name='referer']", 0
1248 assert_select "input[type='submit']", 1
1250 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1252 # When logged in a POST should remove the friendship
1253 post :remove_friend, { :display_name => friend.display_name }, { :user => user }
1254 assert_redirected_to user_path(:display_name => friend.display_name)
1255 assert_match /was removed from your friends/, flash[:notice]
1256 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1258 # A second POST should report that the friendship does not exist
1259 post :remove_friend, { :display_name => friend.display_name }, { :user => user }
1260 assert_redirected_to user_path(:display_name => friend.display_name)
1261 assert_match /is not one of your friends/, flash[:error]
1262 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1265 def test_remove_friend_with_referer
1266 # Get users to work with
1267 user = create(:user)
1268 friend = create(:user)
1269 create(:friend, :user_id => user.id, :friend_user_id => friend.id)
1271 # Check that the users are friends
1272 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1274 # The GET should preserve any referer
1275 get :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user }
1276 assert_response :success
1277 assert_template :remove_friend
1278 assert_select "form" do
1279 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1280 assert_select "input[type='submit']", 1
1282 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1284 # When logged in a POST should remove the friendship and refer
1285 post :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user }
1286 assert_redirected_to "/test"
1287 assert_match /was removed from your friends/, flash[:notice]
1288 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1291 def test_remove_friend_unkown_user
1292 # Should error when a bogus user is specified
1293 get :remove_friend, { :display_name => "No Such User" }, { :user => create(:user) }
1294 assert_response :not_found
1295 assert_template :no_such_user
1299 user = create(:user)
1301 # Try without logging in
1302 get :set_status, :display_name => user.display_name, :status => "suspended"
1303 assert_response :redirect
1304 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1306 # Now try as a normal user
1307 get :set_status, { :display_name => user.display_name, :status => "suspended" }, { :user => user }
1308 assert_response :redirect
1309 assert_redirected_to :action => :view, :display_name => user.display_name
1311 # Finally try as an administrator
1312 get :set_status, { :display_name => user.display_name, :status => "suspended" }, { :user => create(:administrator_user) }
1313 assert_response :redirect
1314 assert_redirected_to :action => :view, :display_name => user.display_name
1315 assert_equal "suspended", User.find(user.id).status
1319 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1321 # Try without logging in
1322 get :delete, :display_name => user.display_name, :status => "suspended"
1323 assert_response :redirect
1324 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1326 # Now try as a normal user
1327 get :delete, { :display_name => user.display_name, :status => "suspended" }, { :user => user }
1328 assert_response :redirect
1329 assert_redirected_to :action => :view, :display_name => user.display_name
1331 # Finally try as an administrator
1332 get :delete, { :display_name => user.display_name, :status => "suspended" }, { :user => create(:administrator_user) }
1333 assert_response :redirect
1334 assert_redirected_to :action => :view, :display_name => user.display_name
1336 # Check that the user was deleted properly
1338 assert_equal "user_#{user.id}", user.display_name
1339 assert_equal "", user.description
1340 assert_nil user.home_lat
1341 assert_nil user.home_lon
1342 assert_equal false, user.image.file?
1343 assert_equal false, user.email_valid
1344 assert_nil user.new_email
1345 assert_nil user.auth_provider
1346 assert_nil user.auth_uid
1347 assert_equal "deleted", user.status
1351 # Shouldn't work when not logged in
1353 assert_response :redirect
1354 assert_redirected_to :action => :login, :referer => users_path
1356 session[:user] = create(:user).id
1358 # Shouldn't work when logged in as a normal user
1360 assert_response :redirect
1361 assert_redirected_to :action => :login, :referer => users_path
1363 session[:user] = create(:moderator_user).id
1365 # Shouldn't work when logged in as a moderator
1367 assert_response :redirect
1368 assert_redirected_to :action => :login, :referer => users_path
1370 session[:user] = create(:administrator_user).id
1372 # Should work when logged in as an administrator
1374 assert_response :success
1375 assert_template :list
1376 assert_select "table#user_list tr", :count => User.count + 1
1378 # Should be able to limit by status
1379 get :list, :status => "suspended"
1380 assert_response :success
1381 assert_template :list
1382 assert_select "table#user_list tr", :count => User.where(:status => "suspended").count + 1
1384 # Should be able to limit by IP address
1385 get :list, :ip => "1.2.3.4"
1386 assert_response :success
1387 assert_template :list
1388 assert_select "table#user_list tr", :count => User.where(:creation_ip => "1.2.3.4").count + 1
1391 def test_list_get_paginated
1392 1.upto(100).each do |n|
1393 User.create(:display_name => "extra_#{n}",
1394 :email => "extra#{n}@example.com",
1395 :pass_crypt => "extraextra")
1398 session[:user] = create(:administrator_user).id
1401 assert_response :success
1402 assert_template :list
1403 assert_select "table#user_list tr", :count => 51
1405 get :list, :page => 2
1406 assert_response :success
1407 assert_template :list
1408 assert_select "table#user_list tr", :count => 51
1410 get :list, :page => 3
1411 assert_response :success
1412 assert_template :list
1413 assert_select "table#user_list tr", :count => 28
1416 def test_list_post_confirm
1417 inactive_user = create(:user, :pending)
1418 suspended_user = create(:user, :suspended)
1420 # Shouldn't work when not logged in
1421 assert_no_difference "User.active.count" do
1422 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1424 assert_response :redirect
1425 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1426 assert_equal "pending", inactive_user.reload.status
1427 assert_equal "suspended", suspended_user.reload.status
1429 session[:user] = create(:user).id
1431 # Shouldn't work when logged in as a normal user
1432 assert_no_difference "User.active.count" do
1433 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1435 assert_response :redirect
1436 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1437 assert_equal "pending", inactive_user.reload.status
1438 assert_equal "suspended", suspended_user.reload.status
1440 session[:user] = create(:moderator_user).id
1442 # Shouldn't work when logged in as a moderator
1443 assert_no_difference "User.active.count" do
1444 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1446 assert_response :redirect
1447 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1448 assert_equal "pending", inactive_user.reload.status
1449 assert_equal "suspended", suspended_user.reload.status
1451 session[:user] = create(:administrator_user).id
1453 # Should work when logged in as an administrator
1454 assert_difference "User.active.count", 2 do
1455 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1457 assert_response :redirect
1458 assert_redirected_to :action => :list
1459 assert_equal "confirmed", inactive_user.reload.status
1460 assert_equal "confirmed", suspended_user.reload.status
1463 def test_list_post_hide
1464 normal_user = create(:user)
1465 confirmed_user = create(:user, :confirmed)
1467 # Shouldn't work when not logged in
1468 assert_no_difference "User.active.count" do
1469 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1471 assert_response :redirect
1472 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1473 assert_equal "active", normal_user.reload.status
1474 assert_equal "confirmed", confirmed_user.reload.status
1476 session[:user] = create(:user).id
1478 # Shouldn't work when logged in as a normal user
1479 assert_no_difference "User.active.count" do
1480 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1482 assert_response :redirect
1483 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1484 assert_equal "active", normal_user.reload.status
1485 assert_equal "confirmed", confirmed_user.reload.status
1487 session[:user] = create(:moderator_user).id
1489 # Shouldn't work when logged in as a moderator
1490 assert_no_difference "User.active.count" do
1491 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1493 assert_response :redirect
1494 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1495 assert_equal "active", normal_user.reload.status
1496 assert_equal "confirmed", confirmed_user.reload.status
1498 session[:user] = create(:administrator_user).id
1500 # Should work when logged in as an administrator
1501 assert_difference "User.active.count", -2 do
1502 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1504 assert_response :redirect
1505 assert_redirected_to :action => :list
1506 assert_equal "deleted", normal_user.reload.status
1507 assert_equal "deleted", confirmed_user.reload.status