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, :params => { :cookie_test => "true" }, :session => { :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, :params => { :cookie_test => "true" }
220 assert_response :redirect
221 assert_redirected_to root_path
223 get :new, :params => { :referer => "/test" }
224 assert_response :redirect
225 assert_redirected_to user_new_path(:referer => "/test", :cookie_test => "true")
226 get :new, :params => { :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, :session => { :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, :session => { :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, :session => { :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, :session => { :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, :session => { :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, :session => { :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, :params => { :session => session_id }
337 assert_response :redirect
338 assert_redirected_to root_path
341 def test_logout_with_referer
342 get :logout, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :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, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :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, :params => { :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, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :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, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :confirm_string => confirm_string }
587 assert_response :redirect
588 assert_redirected_to :action => :account, :display_name => user.display_name
589 assert_match /Confirmed your change of email address/, flash[:notice]
590 # gravatar use should now be disabled
591 assert !User.find(user.id).image_use_gravatar
594 def test_terms_new_user
595 get :terms, :session => { :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, :params => { :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, :params => { :referer => "/test" }
637 assert_response :success
638 assert_template :terms
640 post :save, :params => { :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, :session => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :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, :params => { :token => token.token }
749 assert_response :success
750 assert_template :reset_password
752 # Test setting a new password
753 post :reset_password, :params => { :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, :params => { :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, :params => { :display_name => user.display_name }, :session => { :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, :params => { :display_name => user.display_name }, :session => { :user => user }
783 assert_response :success
784 assert_template :account
786 # Updating the description should work
787 user.description = "new description"
788 user.preferred_editor = "default"
789 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
790 assert_response :success
791 assert_template :account
792 assert_select "div#errorExplanation", false
793 assert_select ".notice", /^User information updated successfully/
794 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
796 # Changing to a invalid editor should fail
797 user.preferred_editor = "unknown"
798 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
799 assert_response :success
800 assert_template :account
801 assert_select ".notice", false
802 assert_select "div#errorExplanation"
803 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
805 # Changing to a valid editor should work
806 user.preferred_editor = "potlatch2"
807 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
808 assert_response :success
809 assert_template :account
810 assert_select "div#errorExplanation", false
811 assert_select ".notice", /^User information updated successfully/
812 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
814 # Changing to the default editor should work
815 user.preferred_editor = "default"
816 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
817 assert_response :success
818 assert_template :account
819 assert_select "div#errorExplanation", false
820 assert_select ".notice", /^User information updated successfully/
821 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
823 # Changing to an uploaded image should work
824 image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif")
825 post :account, :params => { :display_name => user.display_name, :image_action => "new", :user => user.attributes.merge(:image => image) }, :session => { :user => user }
826 assert_response :success
827 assert_template :account
828 assert_select "div#errorExplanation", false
829 assert_select ".notice", /^User information updated successfully/
830 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "keep"
832 # Changing to a gravatar image should work
833 post :account, :params => { :display_name => user.display_name, :image_action => "gravatar", :user => user.attributes }, :session => { :user => user }
834 assert_response :success
835 assert_template :account
836 assert_select "div#errorExplanation", false
837 assert_select ".notice", /^User information updated successfully/
838 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "gravatar"
840 # Removing the image should work
841 post :account, :params => { :display_name => user.display_name, :image_action => "delete", :user => user.attributes }, :session => { :user => user }
842 assert_response :success
843 assert_template :account
844 assert_select "div#errorExplanation", false
845 assert_select ".notice", /^User information updated successfully/
846 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked]", false
848 # Adding external authentication should redirect to the auth provider
849 post :account, :params => { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, :session => { :user => user }
850 assert_response :redirect
851 assert_redirected_to auth_path(:provider => "openid", :openid_url => "https://www.google.com/accounts/o8/id", :origin => "/user/#{URI.encode(user.display_name)}/account")
853 # Changing name to one that exists should fail
854 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
855 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
856 assert_response :success
857 assert_template :account
858 assert_select ".notice", false
859 assert_select "div#errorExplanation"
860 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
862 # Changing name to one that exists should fail, regardless of case
863 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
864 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
865 assert_response :success
866 assert_template :account
867 assert_select ".notice", false
868 assert_select "div#errorExplanation"
869 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
871 # Changing name to one that doesn't exist should work
872 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
873 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
874 assert_response :success
875 assert_template :account
876 assert_select "div#errorExplanation", false
877 assert_select ".notice", /^User information updated successfully/
878 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
880 # Record the change of name
881 user.display_name = "new tester"
883 # Changing email to one that exists should fail
884 user.new_email = create(:user).email
885 assert_no_difference "ActionMailer::Base.deliveries.size" do
886 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
888 assert_response :success
889 assert_template :account
890 assert_select ".notice", false
891 assert_select "div#errorExplanation"
892 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
894 # Changing email to one that exists should fail, regardless of case
895 user.new_email = create(:user).email.upcase
896 assert_no_difference "ActionMailer::Base.deliveries.size" do
897 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
899 assert_response :success
900 assert_template :account
901 assert_select ".notice", false
902 assert_select "div#errorExplanation"
903 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
905 # Changing email to one that doesn't exist should work
906 user.new_email = "new_tester@example.com"
907 assert_difference "ActionMailer::Base.deliveries.size", 1 do
908 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
910 assert_response :success
911 assert_template :account
912 assert_select "div#errorExplanation", false
913 assert_select ".notice", /^User information updated successfully/
914 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
915 email = ActionMailer::Base.deliveries.first
916 assert_equal 1, email.to.count
917 assert_equal user.new_email, email.to.first
918 ActionMailer::Base.deliveries.clear
921 # Check that the user account page will display and contains some relevant
922 # information for the user
924 # Test a non-existent user
925 get :view, :params => { :display_name => "unknown" }
926 assert_response :not_found
930 get :view, :params => { :display_name => user.display_name }
931 assert_response :success
932 assert_select "div#userinformation" do
933 assert_select "a[href^='/user/#{URI.encode(user.display_name)}/history']", 1
934 assert_select "a[href='/user/#{URI.encode(user.display_name)}/traces']", 1
935 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary']", 1
936 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary/comments']", 1
937 assert_select "a[href='/user/#{URI.encode(user.display_name)}/account']", 0
938 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks']", 0
939 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks_by']", 0
940 assert_select "a[href='/blocks/new/#{URI.encode(user.display_name)}']", 0
943 # Test a user who has been blocked
944 blocked_user = create(:user)
945 create(:user_block, :user => blocked_user)
946 get :view, :params => { :display_name => blocked_user.display_name }
947 assert_response :success
948 assert_select "div#userinformation" do
949 assert_select "a[href^='/user/#{URI.encode(blocked_user.display_name)}/history']", 1
950 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/traces']", 1
951 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/diary']", 1
952 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/diary/comments']", 1
953 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/account']", 0
954 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/blocks']", 1
955 assert_select "a[href='/user/#{URI.encode(blocked_user.display_name)}/blocks_by']", 0
956 assert_select "a[href='/blocks/new/#{URI.encode(blocked_user.display_name)}']", 0
959 # Test a moderator who has applied blocks
960 moderator_user = create(:moderator_user)
961 create(:user_block, :creator => moderator_user)
962 get :view, :params => { :display_name => moderator_user.display_name }
963 assert_response :success
964 assert_select "div#userinformation" do
965 assert_select "a[href^='/user/#{URI.encode(moderator_user.display_name)}/history']", 1
966 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/traces']", 1
967 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/diary']", 1
968 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/diary/comments']", 1
969 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/account']", 0
970 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/blocks']", 0
971 assert_select "a[href='/user/#{URI.encode(moderator_user.display_name)}/blocks_by']", 1
972 assert_select "a[href='/blocks/new/#{URI.encode(moderator_user.display_name)}']", 0
975 # Login as a normal user
976 session[:user] = user.id
978 # Test the normal user
979 get :view, :params => { :display_name => user.display_name }
980 assert_response :success
981 assert_select "div#userinformation" do
982 assert_select "a[href^='/user/#{URI.encode(user.display_name)}/history']", 1
983 assert_select "a[href='/traces/mine']", 1
984 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary']", 1
985 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary/comments']", 1
986 assert_select "a[href='/user/#{URI.encode(user.display_name)}/account']", 1
987 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks']", 0
988 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks_by']", 0
989 assert_select "a[href='/blocks/new/#{URI.encode(user.display_name)}']", 0
992 # Login as a moderator
993 session[:user] = create(:moderator_user).id
995 # Test the normal user
996 get :view, :params => { :display_name => user.display_name }
997 assert_response :success
998 assert_select "div#userinformation" do
999 assert_select "a[href^='/user/#{URI.encode(user.display_name)}/history']", 1
1000 assert_select "a[href='/user/#{URI.encode(user.display_name)}/traces']", 1
1001 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary']", 1
1002 assert_select "a[href='/user/#{URI.encode(user.display_name)}/diary/comments']", 1
1003 assert_select "a[href='/user/#{URI.encode(user.display_name)}/account']", 0
1004 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks']", 0
1005 assert_select "a[href='/user/#{URI.encode(user.display_name)}/blocks_by']", 0
1006 assert_select "a[href='/blocks/new/#{URI.encode(user.display_name)}']", 1
1010 # Test whether information about contributor terms is shown for users who haven't agreed
1011 def test_terms_not_agreed
1012 agreed_user = create(:user, :terms_agreed => 3.days.ago)
1013 seen_user = create(:user, :terms_seen => true)
1014 not_seen_user = create(:user, :terms_seen => false)
1016 get :view, :params => { :display_name => agreed_user.display_name }
1017 assert_response :success
1018 assert_select "div#userinformation" do
1019 assert_select "p", :count => 0, :text => /Contributor terms/
1022 get :view, :params => { :display_name => seen_user.display_name }
1023 assert_response :success
1024 # put @response.body
1025 assert_select "div#userinformation" do
1026 assert_select "p", :count => 1, :text => /Contributor terms/
1027 assert_select "p", /Declined/
1030 get :view, :params => { :display_name => not_seen_user.display_name }
1031 assert_response :success
1032 assert_select "div#userinformation" do
1033 assert_select "p", :count => 1, :text => /Contributor terms/
1034 assert_select "p", /Undecided/
1039 user = create(:user, :description => "test", :terms_agreed => Date.yesterday)
1040 # check that a visible user is returned properly
1041 get :api_read, :params => { :id => user.id }
1042 assert_response :success
1043 assert_equal "text/xml", response.content_type
1045 # check the data that is returned
1046 assert_select "description", :count => 1, :text => "test"
1047 assert_select "contributor-terms", :count => 1 do
1048 assert_select "[agreed='true']"
1050 assert_select "img", :count => 0
1051 assert_select "roles", :count => 1 do
1052 assert_select "role", :count => 0
1054 assert_select "changesets", :count => 1 do
1055 assert_select "[count='0']"
1057 assert_select "traces", :count => 1 do
1058 assert_select "[count='0']"
1060 assert_select "blocks", :count => 1 do
1061 assert_select "received", :count => 1 do
1062 assert_select "[count='0'][active='0']"
1064 assert_select "issued", :count => 0
1067 # check that we aren't revealing private information
1068 assert_select "contributor-terms[pd]", false
1069 assert_select "home", false
1070 assert_select "languages", false
1071 assert_select "messages", false
1073 # check that a suspended user is not returned
1074 get :api_read, :params => { :id => create(:user, :suspended).id }
1075 assert_response :gone
1077 # check that a deleted user is not returned
1078 get :api_read, :params => { :id => create(:user, :deleted).id }
1079 assert_response :gone
1081 # check that a non-existent user is not returned
1082 get :api_read, :params => { :id => 0 }
1083 assert_response :not_found
1086 def test_api_details
1087 user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"])
1088 create(:message, :read, :recipient => user)
1089 create(:message, :sender => user)
1091 # check that nothing is returned when not logged in
1093 assert_response :unauthorized
1095 # check that we get a response when logged in
1096 basic_authorization user.email, "test"
1098 assert_response :success
1099 assert_equal "text/xml", response.content_type
1101 # check the data that is returned
1102 assert_select "description", :count => 1, :text => "test"
1103 assert_select "contributor-terms", :count => 1 do
1104 assert_select "[agreed='true'][pd='false']"
1106 assert_select "img", :count => 0
1107 assert_select "roles", :count => 1 do
1108 assert_select "role", :count => 0
1110 assert_select "changesets", :count => 1 do
1111 assert_select "[count='0']", :count => 1
1113 assert_select "traces", :count => 1 do
1114 assert_select "[count='0']", :count => 1
1116 assert_select "blocks", :count => 1 do
1117 assert_select "received", :count => 1 do
1118 assert_select "[count='0'][active='0']"
1120 assert_select "issued", :count => 0
1122 assert_select "home", :count => 1 do
1123 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
1125 assert_select "languages", :count => 1 do
1126 assert_select "lang", :count => 1, :text => "en"
1128 assert_select "messages", :count => 1 do
1129 assert_select "received", :count => 1 do
1130 assert_select "[count='1'][unread='0']"
1132 assert_select "sent", :count => 1 do
1133 assert_select "[count='1']"
1138 def test_api_gpx_files
1139 user = create(:user)
1140 trace1 = create(:trace, :user => user) do |trace|
1141 create(:tracetag, :trace => trace, :tag => "London")
1143 trace2 = create(:trace, :user => user) do |trace|
1144 create(:tracetag, :trace => trace, :tag => "Birmingham")
1146 # check that nothing is returned when not logged in
1148 assert_response :unauthorized
1150 # check that we get a response when logged in
1151 basic_authorization user.email, "test"
1153 assert_response :success
1154 assert_equal "application/xml", response.content_type
1156 # check the data that is returned
1157 assert_select "gpx_file[id='#{trace1.id}']", 1 do
1158 assert_select "tag", "London"
1160 assert_select "gpx_file[id='#{trace2.id}']", 1 do
1161 assert_select "tag", "Birmingham"
1165 def test_make_friend
1166 # Get users to work with
1167 user = create(:user)
1168 friend = create(:user)
1170 # Check that the users aren't already friends
1171 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1173 # When not logged in a GET should ask us to login
1174 get :make_friend, :params => { :display_name => friend.display_name }
1175 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1177 # When not logged in a POST should error
1178 post :make_friend, :params => { :display_name => friend.display_name }
1179 assert_response :forbidden
1180 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1182 # When logged in a GET should get a confirmation page
1183 get :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1184 assert_response :success
1185 assert_template :make_friend
1186 assert_select "form" do
1187 assert_select "input[type='hidden'][name='referer']", 0
1188 assert_select "input[type='submit']", 1
1190 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1192 # When logged in a POST should add the friendship
1193 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1194 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1196 assert_redirected_to user_path(:display_name => friend.display_name)
1197 assert_match /is now your friend/, flash[:notice]
1198 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1199 email = ActionMailer::Base.deliveries.first
1200 assert_equal 1, email.to.count
1201 assert_equal friend.email, email.to.first
1202 ActionMailer::Base.deliveries.clear
1204 # A second POST should report that the friendship already exists
1205 assert_no_difference "ActionMailer::Base.deliveries.size" do
1206 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1208 assert_redirected_to user_path(:display_name => friend.display_name)
1209 assert_match /You are already friends with/, flash[:warning]
1210 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1213 def test_make_friend_with_referer
1214 # Get users to work with
1215 user = create(:user)
1216 friend = create(:user)
1218 # Check that the users aren't already friends
1219 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1221 # The GET should preserve any referer
1222 get :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1223 assert_response :success
1224 assert_template :make_friend
1225 assert_select "form" do
1226 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1227 assert_select "input[type='submit']", 1
1229 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1231 # When logged in a POST should add the friendship and refer us
1232 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1233 post :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1235 assert_redirected_to "/test"
1236 assert_match /is now your friend/, flash[:notice]
1237 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1238 email = ActionMailer::Base.deliveries.first
1239 assert_equal 1, email.to.count
1240 assert_equal friend.email, email.to.first
1241 ActionMailer::Base.deliveries.clear
1244 def test_make_friend_unkown_user
1245 # Should error when a bogus user is specified
1246 get :make_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1247 assert_response :not_found
1248 assert_template :no_such_user
1251 def test_remove_friend
1252 # Get users to work with
1253 user = create(:user)
1254 friend = create(:user)
1255 create(:friend, :befriender => user, :befriendee => friend)
1257 # Check that the users are friends
1258 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1260 # When not logged in a GET should ask us to login
1261 get :remove_friend, :params => { :display_name => friend.display_name }
1262 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1264 # When not logged in a POST should error
1265 post :remove_friend, :params => { :display_name => friend.display_name }
1266 assert_response :forbidden
1267 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1269 # When logged in a GET should get a confirmation page
1270 get :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1271 assert_response :success
1272 assert_template :remove_friend
1273 assert_select "form" do
1274 assert_select "input[type='hidden'][name='referer']", 0
1275 assert_select "input[type='submit']", 1
1277 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1279 # When logged in a POST should remove the friendship
1280 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1281 assert_redirected_to user_path(:display_name => friend.display_name)
1282 assert_match /was removed from your friends/, flash[:notice]
1283 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1285 # A second POST should report that the friendship does not exist
1286 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1287 assert_redirected_to user_path(:display_name => friend.display_name)
1288 assert_match /is not one of your friends/, flash[:error]
1289 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1292 def test_remove_friend_with_referer
1293 # Get users to work with
1294 user = create(:user)
1295 friend = create(:user)
1296 create(:friend, :user_id => user.id, :friend_user_id => friend.id)
1298 # Check that the users are friends
1299 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1301 # The GET should preserve any referer
1302 get :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1303 assert_response :success
1304 assert_template :remove_friend
1305 assert_select "form" do
1306 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1307 assert_select "input[type='submit']", 1
1309 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1311 # When logged in a POST should remove the friendship and refer
1312 post :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1313 assert_redirected_to "/test"
1314 assert_match /was removed from your friends/, flash[:notice]
1315 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1318 def test_remove_friend_unkown_user
1319 # Should error when a bogus user is specified
1320 get :remove_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1321 assert_response :not_found
1322 assert_template :no_such_user
1326 user = create(:user)
1328 # Try without logging in
1329 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }
1330 assert_response :redirect
1331 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1333 # Now try as a normal user
1334 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1335 assert_response :redirect
1336 assert_redirected_to :action => :view, :display_name => user.display_name
1338 # Finally try as an administrator
1339 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1340 assert_response :redirect
1341 assert_redirected_to :action => :view, :display_name => user.display_name
1342 assert_equal "suspended", User.find(user.id).status
1346 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1348 # Try without logging in
1349 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }
1350 assert_response :redirect
1351 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1353 # Now try as a normal user
1354 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1355 assert_response :redirect
1356 assert_redirected_to :action => :view, :display_name => user.display_name
1358 # Finally try as an administrator
1359 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1360 assert_response :redirect
1361 assert_redirected_to :action => :view, :display_name => user.display_name
1363 # Check that the user was deleted properly
1365 assert_equal "user_#{user.id}", user.display_name
1366 assert_equal "", user.description
1367 assert_nil user.home_lat
1368 assert_nil user.home_lon
1369 assert_equal false, user.image.file?
1370 assert_equal false, user.email_valid
1371 assert_nil user.new_email
1372 assert_nil user.auth_provider
1373 assert_nil user.auth_uid
1374 assert_equal "deleted", user.status
1378 user = create(:user)
1379 moderator_user = create(:moderator_user)
1380 administrator_user = create(:administrator_user)
1381 _suspended_user = create(:user, :suspended)
1382 _ip_user = create(:user, :creation_ip => "1.2.3.4")
1384 # There are now 7 users - the five above, plus two extra "granters" for the
1385 # moderator_user and administrator_user
1386 assert_equal 7, User.count
1388 # Shouldn't work when not logged in
1390 assert_response :redirect
1391 assert_redirected_to :action => :login, :referer => users_path
1393 session[:user] = user.id
1395 # Shouldn't work when logged in as a normal user
1397 assert_response :redirect
1398 assert_redirected_to :action => :login, :referer => users_path
1400 session[:user] = moderator_user.id
1402 # Shouldn't work when logged in as a moderator
1404 assert_response :redirect
1405 assert_redirected_to :action => :login, :referer => users_path
1407 session[:user] = administrator_user.id
1409 # Note there is a header row, so all row counts are users + 1
1410 # Should work when logged in as an administrator
1412 assert_response :success
1413 assert_template :list
1414 assert_select "table#user_list tr", :count => 7 + 1
1416 # Should be able to limit by status
1417 get :list, :params => { :status => "suspended" }
1418 assert_response :success
1419 assert_template :list
1420 assert_select "table#user_list tr", :count => 1 + 1
1422 # Should be able to limit by IP address
1423 get :list, :params => { :ip => "1.2.3.4" }
1424 assert_response :success
1425 assert_template :list
1426 assert_select "table#user_list tr", :count => 1 + 1
1429 def test_list_get_paginated
1430 1.upto(100).each do |n|
1431 User.create(:display_name => "extra_#{n}",
1432 :email => "extra#{n}@example.com",
1433 :pass_crypt => "extraextra")
1436 session[:user] = create(:administrator_user).id
1438 # 100 examples, an administrator, and a granter for the admin.
1439 assert_equal 102, User.count
1442 assert_response :success
1443 assert_template :list
1444 assert_select "table#user_list tr", :count => 51
1446 get :list, :params => { :page => 2 }
1447 assert_response :success
1448 assert_template :list
1449 assert_select "table#user_list tr", :count => 51
1451 get :list, :params => { :page => 3 }
1452 assert_response :success
1453 assert_template :list
1454 assert_select "table#user_list tr", :count => 3
1457 def test_list_post_confirm
1458 inactive_user = create(:user, :pending)
1459 suspended_user = create(:user, :suspended)
1461 # Shouldn't work when not logged in
1462 assert_no_difference "User.active.count" do
1463 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1465 assert_response :redirect
1466 assert_redirected_to :action => :login, :referer => users_path
1467 assert_equal "pending", inactive_user.reload.status
1468 assert_equal "suspended", suspended_user.reload.status
1470 session[:user] = create(:user).id
1472 # Shouldn't work when logged in as a normal user
1473 assert_no_difference "User.active.count" do
1474 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1476 assert_response :redirect
1477 assert_redirected_to :action => :login, :referer => users_path
1478 assert_equal "pending", inactive_user.reload.status
1479 assert_equal "suspended", suspended_user.reload.status
1481 session[:user] = create(:moderator_user).id
1483 # Shouldn't work when logged in as a moderator
1484 assert_no_difference "User.active.count" do
1485 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1487 assert_response :redirect
1488 assert_redirected_to :action => :login, :referer => users_path
1489 assert_equal "pending", inactive_user.reload.status
1490 assert_equal "suspended", suspended_user.reload.status
1492 session[:user] = create(:administrator_user).id
1494 # Should work when logged in as an administrator
1495 assert_difference "User.active.count", 2 do
1496 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1498 assert_response :redirect
1499 assert_redirected_to :action => :list
1500 assert_equal "confirmed", inactive_user.reload.status
1501 assert_equal "confirmed", suspended_user.reload.status
1504 def test_list_post_hide
1505 normal_user = create(:user)
1506 confirmed_user = create(:user, :confirmed)
1508 # Shouldn't work when not logged in
1509 assert_no_difference "User.active.count" do
1510 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1512 assert_response :redirect
1513 assert_redirected_to :action => :login, :referer => users_path
1514 assert_equal "active", normal_user.reload.status
1515 assert_equal "confirmed", confirmed_user.reload.status
1517 session[:user] = create(:user).id
1519 # Shouldn't work when logged in as a normal user
1520 assert_no_difference "User.active.count" do
1521 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1523 assert_response :redirect
1524 assert_redirected_to :action => :login, :referer => users_path
1525 assert_equal "active", normal_user.reload.status
1526 assert_equal "confirmed", confirmed_user.reload.status
1528 session[:user] = create(:moderator_user).id
1530 # Shouldn't work when logged in as a moderator
1531 assert_no_difference "User.active.count" do
1532 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1534 assert_response :redirect
1535 assert_redirected_to :action => :login, :referer => users_path
1536 assert_equal "active", normal_user.reload.status
1537 assert_equal "confirmed", confirmed_user.reload.status
1539 session[:user] = create(:administrator_user).id
1541 # Should work when logged in as an administrator
1542 assert_difference "User.active.count", -2 do
1543 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1545 assert_response :redirect
1546 assert_redirected_to :action => :list
1547 assert_equal "deleted", normal_user.reload.status
1548 assert_equal "deleted", confirmed_user.reload.status