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
1011 user = create(:user, :description => "test", :terms_agreed => Date.yesterday)
1012 # check that a visible user is returned properly
1013 get :api_read, :params => { :id => user.id }
1014 assert_response :success
1015 assert_equal "text/xml", response.content_type
1017 # check the data that is returned
1018 assert_select "description", :count => 1, :text => "test"
1019 assert_select "contributor-terms", :count => 1 do
1020 assert_select "[agreed='true']"
1022 assert_select "img", :count => 0
1023 assert_select "roles", :count => 1 do
1024 assert_select "role", :count => 0
1026 assert_select "changesets", :count => 1 do
1027 assert_select "[count='0']"
1029 assert_select "traces", :count => 1 do
1030 assert_select "[count='0']"
1032 assert_select "blocks", :count => 1 do
1033 assert_select "received", :count => 1 do
1034 assert_select "[count='0'][active='0']"
1036 assert_select "issued", :count => 0
1039 # check that we aren't revealing private information
1040 assert_select "contributor-terms[pd]", false
1041 assert_select "home", false
1042 assert_select "languages", false
1043 assert_select "messages", false
1045 # check that a suspended user is not returned
1046 get :api_read, :params => { :id => create(:user, :suspended).id }
1047 assert_response :gone
1049 # check that a deleted user is not returned
1050 get :api_read, :params => { :id => create(:user, :deleted).id }
1051 assert_response :gone
1053 # check that a non-existent user is not returned
1054 get :api_read, :params => { :id => 0 }
1055 assert_response :not_found
1058 def test_api_details
1059 user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"])
1060 create(:message, :read, :recipient => user)
1061 create(:message, :sender => user)
1063 # check that nothing is returned when not logged in
1065 assert_response :unauthorized
1067 # check that we get a response when logged in
1068 basic_authorization(user.email, "test")
1070 assert_response :success
1071 assert_equal "text/xml", response.content_type
1073 # check the data that is returned
1074 assert_select "description", :count => 1, :text => "test"
1075 assert_select "contributor-terms", :count => 1 do
1076 assert_select "[agreed='true'][pd='false']"
1078 assert_select "img", :count => 0
1079 assert_select "roles", :count => 1 do
1080 assert_select "role", :count => 0
1082 assert_select "changesets", :count => 1 do
1083 assert_select "[count='0']", :count => 1
1085 assert_select "traces", :count => 1 do
1086 assert_select "[count='0']", :count => 1
1088 assert_select "blocks", :count => 1 do
1089 assert_select "received", :count => 1 do
1090 assert_select "[count='0'][active='0']"
1092 assert_select "issued", :count => 0
1094 assert_select "home", :count => 1 do
1095 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
1097 assert_select "languages", :count => 1 do
1098 assert_select "lang", :count => 1, :text => "en"
1100 assert_select "messages", :count => 1 do
1101 assert_select "received", :count => 1 do
1102 assert_select "[count='1'][unread='0']"
1104 assert_select "sent", :count => 1 do
1105 assert_select "[count='1']"
1110 def test_api_gpx_files
1111 user = create(:user)
1112 trace1 = create(:trace, :user => user) do |trace|
1113 create(:tracetag, :trace => trace, :tag => "London")
1115 trace2 = create(:trace, :user => user) do |trace|
1116 create(:tracetag, :trace => trace, :tag => "Birmingham")
1118 # check that nothing is returned when not logged in
1120 assert_response :unauthorized
1122 # check that we get a response when logged in
1123 basic_authorization(user.email, "test")
1125 assert_response :success
1126 assert_equal "application/xml", response.content_type
1128 # check the data that is returned
1129 assert_select "gpx_file[id='#{trace1.id}']", 1 do
1130 assert_select "tag", "London"
1132 assert_select "gpx_file[id='#{trace2.id}']", 1 do
1133 assert_select "tag", "Birmingham"
1137 def test_make_friend
1138 # Get users to work with
1139 user = create(:user)
1140 friend = create(:user)
1142 # Check that the users aren't already friends
1143 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1145 # When not logged in a GET should ask us to login
1146 get :make_friend, :params => { :display_name => friend.display_name }
1147 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1149 # When not logged in a POST should error
1150 post :make_friend, :params => { :display_name => friend.display_name }
1151 assert_response :forbidden
1152 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1154 # When logged in a GET should get a confirmation page
1155 get :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1156 assert_response :success
1157 assert_template :make_friend
1158 assert_select "form" do
1159 assert_select "input[type='hidden'][name='referer']", 0
1160 assert_select "input[type='submit']", 1
1162 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1164 # When logged in a POST should add the friendship
1165 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1166 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1168 assert_redirected_to user_path(:display_name => friend.display_name)
1169 assert_match /is now your friend/, flash[:notice]
1170 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1171 email = ActionMailer::Base.deliveries.first
1172 assert_equal 1, email.to.count
1173 assert_equal friend.email, email.to.first
1174 ActionMailer::Base.deliveries.clear
1176 # A second POST should report that the friendship already exists
1177 assert_no_difference "ActionMailer::Base.deliveries.size" do
1178 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1180 assert_redirected_to user_path(:display_name => friend.display_name)
1181 assert_match /You are already friends with/, flash[:warning]
1182 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1185 def test_make_friend_with_referer
1186 # Get users to work with
1187 user = create(:user)
1188 friend = create(:user)
1190 # Check that the users aren't already friends
1191 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1193 # The GET should preserve any referer
1194 get :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1195 assert_response :success
1196 assert_template :make_friend
1197 assert_select "form" do
1198 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1199 assert_select "input[type='submit']", 1
1201 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1203 # When logged in a POST should add the friendship and refer us
1204 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1205 post :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1207 assert_redirected_to "/test"
1208 assert_match /is now your friend/, flash[:notice]
1209 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1210 email = ActionMailer::Base.deliveries.first
1211 assert_equal 1, email.to.count
1212 assert_equal friend.email, email.to.first
1213 ActionMailer::Base.deliveries.clear
1216 def test_make_friend_unkown_user
1217 # Should error when a bogus user is specified
1218 get :make_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1219 assert_response :not_found
1220 assert_template :no_such_user
1223 def test_remove_friend
1224 # Get users to work with
1225 user = create(:user)
1226 friend = create(:user)
1227 create(:friend, :befriender => user, :befriendee => friend)
1229 # Check that the users are friends
1230 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1232 # When not logged in a GET should ask us to login
1233 get :remove_friend, :params => { :display_name => friend.display_name }
1234 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1236 # When not logged in a POST should error
1237 post :remove_friend, :params => { :display_name => friend.display_name }
1238 assert_response :forbidden
1239 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1241 # When logged in a GET should get a confirmation page
1242 get :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1243 assert_response :success
1244 assert_template :remove_friend
1245 assert_select "form" do
1246 assert_select "input[type='hidden'][name='referer']", 0
1247 assert_select "input[type='submit']", 1
1249 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1251 # When logged in a POST should remove the friendship
1252 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1253 assert_redirected_to user_path(:display_name => friend.display_name)
1254 assert_match /was removed from your friends/, flash[:notice]
1255 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1257 # A second POST should report that the friendship does not exist
1258 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1259 assert_redirected_to user_path(:display_name => friend.display_name)
1260 assert_match /is not one of your friends/, flash[:error]
1261 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1264 def test_remove_friend_with_referer
1265 # Get users to work with
1266 user = create(:user)
1267 friend = create(:user)
1268 create(:friend, :user_id => user.id, :friend_user_id => friend.id)
1270 # Check that the users are friends
1271 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1273 # The GET should preserve any referer
1274 get :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1275 assert_response :success
1276 assert_template :remove_friend
1277 assert_select "form" do
1278 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1279 assert_select "input[type='submit']", 1
1281 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1283 # When logged in a POST should remove the friendship and refer
1284 post :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1285 assert_redirected_to "/test"
1286 assert_match /was removed from your friends/, flash[:notice]
1287 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1290 def test_remove_friend_unkown_user
1291 # Should error when a bogus user is specified
1292 get :remove_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1293 assert_response :not_found
1294 assert_template :no_such_user
1298 user = create(:user)
1300 # Try without logging in
1301 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }
1302 assert_response :redirect
1303 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1305 # Now try as a normal user
1306 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1307 assert_response :redirect
1308 assert_redirected_to :action => :view, :display_name => user.display_name
1310 # Finally try as an administrator
1311 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1312 assert_response :redirect
1313 assert_redirected_to :action => :view, :display_name => user.display_name
1314 assert_equal "suspended", User.find(user.id).status
1318 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1320 # Try without logging in
1321 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }
1322 assert_response :redirect
1323 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1325 # Now try as a normal user
1326 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1327 assert_response :redirect
1328 assert_redirected_to :action => :view, :display_name => user.display_name
1330 # Finally try as an administrator
1331 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1332 assert_response :redirect
1333 assert_redirected_to :action => :view, :display_name => user.display_name
1335 # Check that the user was deleted properly
1337 assert_equal "user_#{user.id}", user.display_name
1338 assert_equal "", user.description
1339 assert_nil user.home_lat
1340 assert_nil user.home_lon
1341 assert_equal false, user.image.file?
1342 assert_equal false, user.email_valid
1343 assert_nil user.new_email
1344 assert_nil user.auth_provider
1345 assert_nil user.auth_uid
1346 assert_equal "deleted", user.status
1350 user = create(:user)
1351 moderator_user = create(:moderator_user)
1352 administrator_user = create(:administrator_user)
1353 _suspended_user = create(:user, :suspended)
1354 _ip_user = create(:user, :creation_ip => "1.2.3.4")
1356 # There are now 7 users - the five above, plus two extra "granters" for the
1357 # moderator_user and administrator_user
1358 assert_equal 7, User.count
1360 # Shouldn't work when not logged in
1362 assert_response :redirect
1363 assert_redirected_to :action => :login, :referer => users_path
1365 session[:user] = user.id
1367 # Shouldn't work when logged in as a normal user
1369 assert_response :redirect
1370 assert_redirected_to :action => :login, :referer => users_path
1372 session[:user] = moderator_user.id
1374 # Shouldn't work when logged in as a moderator
1376 assert_response :redirect
1377 assert_redirected_to :action => :login, :referer => users_path
1379 session[:user] = administrator_user.id
1381 # Note there is a header row, so all row counts are users + 1
1382 # Should work when logged in as an administrator
1384 assert_response :success
1385 assert_template :list
1386 assert_select "table#user_list tr", :count => 7 + 1
1388 # Should be able to limit by status
1389 get :list, :params => { :status => "suspended" }
1390 assert_response :success
1391 assert_template :list
1392 assert_select "table#user_list tr", :count => 1 + 1
1394 # Should be able to limit by IP address
1395 get :list, :params => { :ip => "1.2.3.4" }
1396 assert_response :success
1397 assert_template :list
1398 assert_select "table#user_list tr", :count => 1 + 1
1401 def test_list_get_paginated
1402 1.upto(100).each do |n|
1403 User.create(:display_name => "extra_#{n}",
1404 :email => "extra#{n}@example.com",
1405 :pass_crypt => "extraextra")
1408 session[:user] = create(:administrator_user).id
1410 # 100 examples, an administrator, and a granter for the admin.
1411 assert_equal 102, User.count
1414 assert_response :success
1415 assert_template :list
1416 assert_select "table#user_list tr", :count => 51
1418 get :list, :params => { :page => 2 }
1419 assert_response :success
1420 assert_template :list
1421 assert_select "table#user_list tr", :count => 51
1423 get :list, :params => { :page => 3 }
1424 assert_response :success
1425 assert_template :list
1426 assert_select "table#user_list tr", :count => 3
1429 def test_list_post_confirm
1430 inactive_user = create(:user, :pending)
1431 suspended_user = create(:user, :suspended)
1433 # Shouldn't work when not logged in
1434 assert_no_difference "User.active.count" do
1435 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1437 assert_response :redirect
1438 assert_redirected_to :action => :login, :referer => users_path
1439 assert_equal "pending", inactive_user.reload.status
1440 assert_equal "suspended", suspended_user.reload.status
1442 session[:user] = create(:user).id
1444 # Shouldn't work when logged in as a normal user
1445 assert_no_difference "User.active.count" do
1446 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1448 assert_response :redirect
1449 assert_redirected_to :action => :login, :referer => users_path
1450 assert_equal "pending", inactive_user.reload.status
1451 assert_equal "suspended", suspended_user.reload.status
1453 session[:user] = create(:moderator_user).id
1455 # Shouldn't work when logged in as a moderator
1456 assert_no_difference "User.active.count" do
1457 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1459 assert_response :redirect
1460 assert_redirected_to :action => :login, :referer => users_path
1461 assert_equal "pending", inactive_user.reload.status
1462 assert_equal "suspended", suspended_user.reload.status
1464 session[:user] = create(:administrator_user).id
1466 # Should work when logged in as an administrator
1467 assert_difference "User.active.count", 2 do
1468 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1470 assert_response :redirect
1471 assert_redirected_to :action => :list
1472 assert_equal "confirmed", inactive_user.reload.status
1473 assert_equal "confirmed", suspended_user.reload.status
1476 def test_list_post_hide
1477 normal_user = create(:user)
1478 confirmed_user = create(:user, :confirmed)
1480 # Shouldn't work when not logged in
1481 assert_no_difference "User.active.count" do
1482 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1484 assert_response :redirect
1485 assert_redirected_to :action => :login, :referer => users_path
1486 assert_equal "active", normal_user.reload.status
1487 assert_equal "confirmed", confirmed_user.reload.status
1489 session[:user] = create(:user).id
1491 # Shouldn't work when logged in as a normal user
1492 assert_no_difference "User.active.count" do
1493 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1495 assert_response :redirect
1496 assert_redirected_to :action => :login, :referer => users_path
1497 assert_equal "active", normal_user.reload.status
1498 assert_equal "confirmed", confirmed_user.reload.status
1500 session[:user] = create(:moderator_user).id
1502 # Shouldn't work when logged in as a moderator
1503 assert_no_difference "User.active.count" do
1504 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1506 assert_response :redirect
1507 assert_redirected_to :action => :login, :referer => users_path
1508 assert_equal "active", normal_user.reload.status
1509 assert_equal "confirmed", confirmed_user.reload.status
1511 session[:user] = create(:administrator_user).id
1513 # Should work when logged in as an administrator
1514 assert_difference "User.active.count", -2 do
1515 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1517 assert_response :redirect
1518 assert_redirected_to :action => :list
1519 assert_equal "deleted", normal_user.reload.status
1520 assert_equal "deleted", confirmed_user.reload.status