3 class UsersControllerTest < ActionController::TestCase
11 # test all routes which lead to this controller
14 { :path => "/login", :method => :get },
15 { :controller => "users", :action => "login" }
18 { :path => "/login", :method => :post },
19 { :controller => "users", :action => "login" }
22 { :controller => "users", :action => "login", :format => "html" },
23 { :path => "/login.html", :method => :get }
27 { :path => "/logout", :method => :get },
28 { :controller => "users", :action => "logout" }
31 { :path => "/logout", :method => :post },
32 { :controller => "users", :action => "logout" }
35 { :controller => "users", :action => "logout", :format => "html" },
36 { :path => "/logout.html", :method => :get }
40 { :path => "/user/new", :method => :get },
41 { :controller => "users", :action => "new" }
45 { :path => "/user/new", :method => :post },
46 { :controller => "users", :action => "create" }
50 { :path => "/user/terms", :method => :get },
51 { :controller => "users", :action => "terms" }
55 { :path => "/user/save", :method => :post },
56 { :controller => "users", :action => "save" }
60 { :path => "/user/username/confirm", :method => :get },
61 { :controller => "users", :action => "confirm", :display_name => "username" }
64 { :path => "/user/username/confirm", :method => :post },
65 { :controller => "users", :action => "confirm", :display_name => "username" }
68 { :path => "/user/username/confirm/resend", :method => :get },
69 { :controller => "users", :action => "confirm_resend", :display_name => "username" }
73 { :path => "/user/confirm", :method => :get },
74 { :controller => "users", :action => "confirm" }
77 { :path => "/user/confirm", :method => :post },
78 { :controller => "users", :action => "confirm" }
81 { :path => "/user/confirm-email", :method => :get },
82 { :controller => "users", :action => "confirm_email" }
85 { :path => "/user/confirm-email", :method => :post },
86 { :controller => "users", :action => "confirm_email" }
90 { :path => "/user/go_public", :method => :post },
91 { :controller => "users", :action => "go_public" }
95 { :path => "/user/forgot-password", :method => :get },
96 { :controller => "users", :action => "lost_password" }
99 { :path => "/user/forgot-password", :method => :post },
100 { :controller => "users", :action => "lost_password" }
103 { :path => "/user/reset-password", :method => :get },
104 { :controller => "users", :action => "reset_password" }
107 { :path => "/user/reset-password", :method => :post },
108 { :controller => "users", :action => "reset_password" }
112 { :path => "/user/suspended", :method => :get },
113 { :controller => "users", :action => "suspended" }
117 { :path => "/user/username", :method => :get },
118 { :controller => "users", :action => "show", :display_name => "username" }
122 { :path => "/user/username/account", :method => :get },
123 { :controller => "users", :action => "account", :display_name => "username" }
126 { :path => "/user/username/account", :method => :post },
127 { :controller => "users", :action => "account", :display_name => "username" }
131 { :path => "/user/username/make_friend", :method => :get },
132 { :controller => "users", :action => "make_friend", :display_name => "username" }
135 { :path => "/user/username/make_friend", :method => :post },
136 { :controller => "users", :action => "make_friend", :display_name => "username" }
139 { :path => "/user/username/remove_friend", :method => :get },
140 { :controller => "users", :action => "remove_friend", :display_name => "username" }
143 { :path => "/user/username/remove_friend", :method => :post },
144 { :controller => "users", :action => "remove_friend", :display_name => "username" }
148 { :path => "/user/username/set_status", :method => :get },
149 { :controller => "users", :action => "set_status", :display_name => "username" }
152 { :path => "/user/username/delete", :method => :get },
153 { :controller => "users", :action => "delete", :display_name => "username" }
157 { :path => "/users", :method => :get },
158 { :controller => "users", :action => "index" }
161 { :path => "/users", :method => :post },
162 { :controller => "users", :action => "index" }
165 { :path => "/users/status", :method => :get },
166 { :controller => "users", :action => "index", :status => "status" }
169 { :path => "/users/status", :method => :post },
170 { :controller => "users", :action => "index", :status => "status" }
174 # The user creation page loads
177 assert_response :redirect
178 assert_redirected_to user_new_path(:cookie_test => "true")
180 get :new, :params => { :cookie_test => "true" }, :session => { :cookie_test => true }
181 assert_response :success
183 assert_select "html", :count => 1 do
184 assert_select "head", :count => 1 do
185 assert_select "title", :text => /Sign Up/, :count => 1
187 assert_select "body", :count => 1 do
188 assert_select "div#content", :count => 1 do
189 assert_select "form[action='/user/new'][method='post']", :count => 1 do
190 assert_select "input[id='user_email']", :count => 1
191 assert_select "input[id='user_email_confirmation']", :count => 1
192 assert_select "input[id='user_display_name']", :count => 1
193 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
194 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
195 assert_select "input[type='submit'][value='Sign Up']", :count => 1
202 def test_new_view_logged_in
203 session[:user] = create(:user).id
206 assert_response :redirect
207 assert_redirected_to user_new_path(:cookie_test => "true")
208 get :new, :params => { :cookie_test => "true" }
209 assert_response :redirect
210 assert_redirected_to root_path
212 get :new, :params => { :referer => "/test" }
213 assert_response :redirect
214 assert_redirected_to user_new_path(:referer => "/test", :cookie_test => "true")
215 get :new, :params => { :referer => "/test", :cookie_test => "true" }
216 assert_response :redirect
217 assert_redirected_to "/test"
221 user = build(:user, :pending)
223 assert_difference "User.count", 1 do
224 assert_difference "ActionMailer::Base.deliveries.size", 1 do
225 perform_enqueued_jobs do
226 post :save, :session => { :new_user => user }, :params => { :read_ct => 1, :read_tou => 1 }
232 register_email = ActionMailer::Base.deliveries.first
234 assert_equal register_email.to[0], user.email
235 assert_match(/#{@url}/, register_email.body.to_s)
238 assert_redirected_to :action => "confirm", :display_name => user.display_name
240 ActionMailer::Base.deliveries.clear
243 def test_new_duplicate_email
244 user = build(:user, :pending)
245 user.email = create(:user).email
247 assert_no_difference "User.count" do
248 assert_no_difference "ActionMailer::Base.deliveries.size" do
249 perform_enqueued_jobs do
250 post :save, :session => { :new_user => user }, :params => { :read_ct => 1, :read_tou => 1 }
255 assert_response :success
256 assert_template "new"
257 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
260 def test_new_duplicate_email_uppercase
261 user = build(:user, :pending)
262 user.email = create(:user).email.upcase
264 assert_no_difference "User.count" do
265 assert_no_difference "ActionMailer::Base.deliveries.size" do
266 perform_enqueued_jobs do
267 post :save, :session => { :new_user => user }, :params => { :read_ct => 1, :read_tou => 1 }
272 assert_response :success
273 assert_template "new"
274 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
277 def test_new_duplicate_name
278 user = build(:user, :pending)
279 user.display_name = create(:user).display_name
281 assert_no_difference "User.count" do
282 assert_no_difference "ActionMailer::Base.deliveries.size" do
283 perform_enqueued_jobs do
284 post :save, :session => { :new_user => user }, :params => { :read_ct => 1, :read_tou => 1 }
289 assert_response :success
290 assert_template "new"
291 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
294 def test_new_duplicate_name_uppercase
295 user = build(:user, :pending)
296 user.display_name = create(:user).display_name.upcase
298 assert_no_difference "User.count" do
299 assert_no_difference "ActionMailer::Base.deliveries.size" do
300 perform_enqueued_jobs do
301 post :save, :session => { :new_user => user }, :params => { :read_ct => 1, :read_tou => 1 }
306 assert_response :success
307 assert_template "new"
308 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
311 def test_new_blocked_domain
312 user = build(:user, :pending, :email => "user@example.net")
313 create(:acl, :domain => "example.net", :k => "no_account_creation")
315 assert_no_difference "User.count" do
316 assert_no_difference "ActionMailer::Base.deliveries.size" do
317 perform_enqueued_jobs do
318 post :save, :session => { :new_user => user }, :params => { :read_ct => 1, :read_tou => 1 }
323 assert_response :success
324 assert_template "blocked"
327 def test_save_referer_params
328 user = build(:user, :pending)
330 assert_difference "User.count", 1 do
331 assert_difference "ActionMailer::Base.deliveries.size", 1 do
332 perform_enqueued_jobs do
333 post :save, :session => { :new_user => user,
334 :referer => "/edit?editor=id#map=1/2/3" },
335 :params => { :read_ct => 1, :read_tou => 1 }
340 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
341 user.tokens.order("id DESC").first.referer
343 ActionMailer::Base.deliveries.clear
346 def test_logout_without_referer
348 assert_response :success
349 assert_template :logout
350 assert_select "input[name=referer][value=?]", ""
352 session_id = assert_select("input[name=session]").first["value"]
354 get :logout, :params => { :session => session_id }
355 assert_response :redirect
356 assert_redirected_to root_path
359 def test_logout_with_referer
360 get :logout, :params => { :referer => "/test" }
361 assert_response :success
362 assert_template :logout
363 assert_select "input[name=referer][value=?]", "/test"
365 session_id = assert_select("input[name=session]").first["value"]
367 get :logout, :params => { :session => session_id, :referer => "/test" }
368 assert_response :redirect
369 assert_redirected_to "/test"
372 def test_logout_with_token
373 token = create(:user).tokens.create
375 session[:token] = token.token
378 assert_response :success
379 assert_template :logout
380 assert_select "input[name=referer][value=?]", ""
381 assert_equal token.token, session[:token]
382 assert_not_nil UserToken.where(:id => token.id).first
384 session_id = assert_select("input[name=session]").first["value"]
386 get :logout, :params => { :session => session_id }
387 assert_response :redirect
388 assert_redirected_to root_path
389 assert_nil session[:token]
390 assert_nil UserToken.where(:id => token.id).first
394 user = create(:user, :pending)
395 confirm_string = user.tokens.create.token
397 @request.cookies["_osm_session"] = user.display_name
398 get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
399 assert_response :success
400 assert_template :confirm
403 def test_confirm_get_already_confirmed
405 confirm_string = user.tokens.create.token
407 @request.cookies["_osm_session"] = user.display_name
408 get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
409 assert_response :redirect
410 assert_redirected_to root_path
413 def test_confirm_success_no_token_no_referer
414 user = create(:user, :pending)
415 stub_gravatar_request(user.email)
416 confirm_string = user.tokens.create.token
418 @request.cookies["_osm_session"] = user.display_name
419 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
420 assert_redirected_to login_path
421 assert_match(/Confirmed your account/, flash[:notice])
424 def test_confirm_success_good_token_no_referer
425 user = create(:user, :pending)
426 stub_gravatar_request(user.email)
427 confirm_string = user.tokens.create.token
428 token = user.tokens.create.token
430 @request.cookies["_osm_session"] = user.display_name
431 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
432 assert_redirected_to welcome_path
435 def test_confirm_success_bad_token_no_referer
436 user = create(:user, :pending)
437 stub_gravatar_request(user.email)
438 confirm_string = user.tokens.create.token
439 token = create(:user).tokens.create.token
441 @request.cookies["_osm_session"] = user.display_name
442 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
443 assert_redirected_to login_path
444 assert_match(/Confirmed your account/, flash[:notice])
447 def test_confirm_success_no_token_with_referer
448 user = create(:user, :pending)
449 stub_gravatar_request(user.email)
450 confirm_string = user.tokens.create(:referer => new_diary_entry_path).token
452 @request.cookies["_osm_session"] = user.display_name
453 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
454 assert_redirected_to login_path(:referer => new_diary_entry_path)
455 assert_match(/Confirmed your account/, flash[:notice])
458 def test_confirm_success_good_token_with_referer
459 user = create(:user, :pending)
460 stub_gravatar_request(user.email)
461 confirm_string = user.tokens.create(:referer => new_diary_entry_path).token
462 token = user.tokens.create.token
464 @request.cookies["_osm_session"] = user.display_name
465 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
466 assert_redirected_to new_diary_entry_path
469 def test_confirm_success_bad_token_with_referer
470 user = create(:user, :pending)
471 stub_gravatar_request(user.email)
472 confirm_string = user.tokens.create(:referer => new_diary_entry_path).token
473 token = create(:user).tokens.create.token
475 @request.cookies["_osm_session"] = user.display_name
476 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
477 assert_redirected_to login_path(:referer => new_diary_entry_path)
478 assert_match(/Confirmed your account/, flash[:notice])
481 def test_confirm_expired_token
482 user = create(:user, :pending)
483 confirm_string = user.tokens.create(:expiry => 1.day.ago).token
485 @request.cookies["_osm_session"] = user.display_name
486 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
487 assert_redirected_to :action => "confirm"
488 assert_match(/confirmation code has expired/, flash[:error])
491 def test_confirm_already_confirmed
493 confirm_string = user.tokens.create(:referer => new_diary_entry_path).token
495 @request.cookies["_osm_session"] = user.display_name
496 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
497 assert_redirected_to :action => "login"
498 assert_match(/already been confirmed/, flash[:error])
501 def test_confirm_resend_success
502 user = create(:user, :pending)
503 session[:token] = user.tokens.create.token
505 assert_difference "ActionMailer::Base.deliveries.size", 1 do
506 perform_enqueued_jobs do
507 get :confirm_resend, :params => { :display_name => user.display_name }
511 assert_response :redirect
512 assert_redirected_to login_path
513 assert_match(/sent a new confirmation/, flash[:notice])
515 email = ActionMailer::Base.deliveries.last
517 assert_equal user.email, email.to.first
519 ActionMailer::Base.deliveries.clear
522 def test_confirm_resend_no_token
523 user = create(:user, :pending)
524 assert_no_difference "ActionMailer::Base.deliveries.size" do
525 perform_enqueued_jobs do
526 get :confirm_resend, :params => { :display_name => user.display_name }
530 assert_response :redirect
531 assert_redirected_to login_path
532 assert_match "User #{user.display_name} not found.", flash[:error]
535 def test_confirm_resend_unknown_user
536 assert_no_difference "ActionMailer::Base.deliveries.size" do
537 perform_enqueued_jobs do
538 get :confirm_resend, :params => { :display_name => "No Such User" }
542 assert_response :redirect
543 assert_redirected_to login_path
544 assert_match "User No Such User not found.", flash[:error]
547 def test_confirm_email_get
549 confirm_string = user.tokens.create.token
551 get :confirm_email, :params => { :confirm_string => confirm_string }
552 assert_response :success
553 assert_template :confirm_email
556 def test_confirm_email_success
557 user = create(:user, :new_email => "test-new@example.com")
558 stub_gravatar_request(user.new_email)
559 confirm_string = user.tokens.create.token
561 post :confirm_email, :params => { :confirm_string => confirm_string }
562 assert_response :redirect
563 assert_redirected_to :action => :account, :display_name => user.display_name
564 assert_match(/Confirmed your change of email address/, flash[:notice])
567 def test_confirm_email_already_confirmed
569 confirm_string = user.tokens.create.token
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(/already been confirmed/, flash[:error])
577 def test_confirm_email_bad_token
578 post :confirm_email, :params => { :confirm_string => "XXXXX" }
579 assert_response :success
580 assert_template :confirm_email
581 assert_match(/confirmation code has expired or does not exist/, flash[:error])
585 # test if testing for a gravatar works
586 # this happens when the email is actually changed
587 # which is triggered by the confirmation mail
588 def test_gravatar_auto_enable
589 # switch to email that has a gravatar
590 user = create(:user, :new_email => "test-new@example.com")
591 stub_gravatar_request(user.new_email, 200)
592 confirm_string = user.tokens.create.token
593 # precondition gravatar should be turned off
594 assert_not user.image_use_gravatar
595 post :confirm_email, :params => { :confirm_string => confirm_string }
596 assert_response :redirect
597 assert_redirected_to :action => :account, :display_name => user.display_name
598 assert_match(/Confirmed your change of email address/, flash[:notice])
599 # gravatar use should now be enabled
600 assert User.find(user.id).image_use_gravatar
603 def test_gravatar_auto_disable
604 # switch to email without a gravatar
605 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
606 stub_gravatar_request(user.new_email, 404)
607 confirm_string = user.tokens.create.token
608 # precondition gravatar should be turned on
609 assert user.image_use_gravatar
610 post :confirm_email, :params => { :confirm_string => confirm_string }
611 assert_response :redirect
612 assert_redirected_to :action => :account, :display_name => user.display_name
613 assert_match(/Confirmed your change of email address/, flash[:notice])
614 # gravatar use should now be disabled
615 assert_not User.find(user.id).image_use_gravatar
618 def test_terms_new_user
619 get :terms, :session => { :new_user => User.new }
620 assert_response :success
621 assert_template :terms
624 def test_terms_agreed
625 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
627 session[:user] = user.id
630 assert_response :redirect
631 assert_redirected_to :action => :account, :display_name => user.display_name
634 def test_terms_not_seen_without_referer
635 user = create(:user, :terms_seen => false, :terms_agreed => nil)
637 session[:user] = user.id
640 assert_response :success
641 assert_template :terms
643 post :save, :params => { :user => { :consider_pd => true }, :read_ct => 1, :read_tou => 1 }
644 assert_response :redirect
645 assert_redirected_to :action => :account, :display_name => user.display_name
646 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
650 assert_equal true, user.consider_pd
651 assert_not_nil user.terms_agreed
652 assert_equal true, user.terms_seen
655 def test_terms_not_seen_with_referer
656 user = create(:user, :terms_seen => false, :terms_agreed => nil)
658 session[:user] = user.id
660 get :terms, :params => { :referer => "/test" }
661 assert_response :success
662 assert_template :terms
664 post :save, :params => { :user => { :consider_pd => true }, :referer => "/test", :read_ct => 1, :read_tou => 1 }
665 assert_response :redirect
666 assert_redirected_to "/test"
667 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
671 assert_equal true, user.consider_pd
672 assert_not_nil user.terms_agreed
673 assert_equal true, user.terms_seen
676 # Check that if you haven't seen the terms, and make a request that requires authentication,
677 # that your request is redirected to view the terms
678 def test_terms_not_seen_redirection
679 user = create(:user, :terms_seen => false, :terms_agreed => nil)
680 session[:user] = user.id
682 get :account, :params => { :display_name => user.display_name }
683 assert_response :redirect
684 assert_redirected_to :action => :terms, :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
688 user = create(:user, :data_public => false)
689 post :go_public, :session => { :user => user }
690 assert_response :redirect
691 assert_redirected_to :action => :account, :display_name => user.display_name
692 assert_equal true, User.find(user.id).data_public
695 def test_lost_password
696 # Test fetching the lost password page
698 assert_response :success
699 assert_template :lost_password
700 assert_select "div#notice", false
702 # Test resetting using the address as recorded for a user that has an
703 # address which is duplicated in a different case by another user
705 uppercase_user = build(:user, :email => user.email.upcase).tap { |u| u.save(:validate => false) }
707 assert_difference "ActionMailer::Base.deliveries.size", 1 do
708 perform_enqueued_jobs do
709 post :lost_password, :params => { :user => { :email => user.email } }
712 assert_response :redirect
713 assert_redirected_to :action => :login
714 assert_match(/^Sorry you lost it/, flash[:notice])
715 email = ActionMailer::Base.deliveries.first
716 assert_equal 1, email.to.count
717 assert_equal user.email, email.to.first
718 ActionMailer::Base.deliveries.clear
720 # Test resetting using an address that matches a different user
721 # that has the same address in a different case
722 assert_difference "ActionMailer::Base.deliveries.size", 1 do
723 perform_enqueued_jobs do
724 post :lost_password, :params => { :user => { :email => user.email.upcase } }
727 assert_response :redirect
728 assert_redirected_to :action => :login
729 assert_match(/^Sorry you lost it/, flash[:notice])
730 email = ActionMailer::Base.deliveries.first
731 assert_equal 1, email.to.count
732 assert_equal uppercase_user.email, email.to.first
733 ActionMailer::Base.deliveries.clear
735 # Test resetting using an address that is a case insensitive match
736 # for more than one user but not an exact match for either
737 assert_no_difference "ActionMailer::Base.deliveries.size" do
738 perform_enqueued_jobs do
739 post :lost_password, :params => { :user => { :email => user.email.titlecase } }
742 assert_response :success
743 assert_template :lost_password
744 assert_select ".error", /^Could not find that email address/
746 # Test resetting using the address as recorded for a user that has an
747 # address which is case insensitively unique
748 third_user = create(:user)
749 assert_difference "ActionMailer::Base.deliveries.size", 1 do
750 perform_enqueued_jobs do
751 post :lost_password, :params => { :user => { :email => third_user.email } }
754 assert_response :redirect
755 assert_redirected_to :action => :login
756 assert_match(/^Sorry you lost it/, flash[:notice])
757 email = ActionMailer::Base.deliveries.first
758 assert_equal 1, email.to.count
759 assert_equal third_user.email, email.to.first
760 ActionMailer::Base.deliveries.clear
762 # Test resetting using an address that matches a user that has the
763 # same (case insensitively unique) address in a different case
764 assert_difference "ActionMailer::Base.deliveries.size", 1 do
765 perform_enqueued_jobs do
766 post :lost_password, :params => { :user => { :email => third_user.email.upcase } }
769 assert_response :redirect
770 assert_redirected_to :action => :login
771 assert_match(/^Sorry you lost it/, flash[:notice])
772 email = ActionMailer::Base.deliveries.first
773 assert_equal 1, email.to.count
774 assert_equal third_user.email, email.to.first
775 ActionMailer::Base.deliveries.clear
778 def test_reset_password
779 user = create(:user, :pending)
780 # Test a request with no token
782 assert_response :bad_request
784 # Test a request with a bogus token
785 get :reset_password, :params => { :token => "made_up_token" }
786 assert_response :redirect
787 assert_redirected_to :action => :lost_password
789 # Create a valid token for a user
790 token = user.tokens.create
792 # Test a request with a valid token
793 get :reset_password, :params => { :token => token.token }
794 assert_response :success
795 assert_template :reset_password
797 # Test that errors are reported for erroneous submissions
798 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "different_password" } }
799 assert_response :success
800 assert_template :reset_password
801 assert_select "div#errorExplanation"
803 # Test setting a new password
804 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" } }
805 assert_response :redirect
806 assert_redirected_to root_path
807 assert_equal user.id, session[:user]
809 assert_equal "active", user.status
810 assert_equal true, user.email_valid
811 assert_equal user, User.authenticate(:username => user.email, :password => "new_password")
815 # Get a user to work with - note that this user deliberately
816 # conflicts with uppercase_user in the email and display name
817 # fields to test that we can change other fields without any
818 # validation errors being reported
819 user = create(:user, :languages => [])
820 _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
822 # Make sure that you are redirected to the login page when
823 # you are not logged in
824 get :account, :params => { :display_name => user.display_name }
825 assert_response :redirect
826 assert_redirected_to :action => "login", :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
828 # Make sure that you are blocked when not logged in as the right user
829 get :account, :params => { :display_name => user.display_name }, :session => { :user => create(:user) }
830 assert_response :forbidden
832 # Make sure we get the page when we are logged in as the right user
833 get :account, :params => { :display_name => user.display_name }, :session => { :user => user }
834 assert_response :success
835 assert_template :account
836 assert_select "form#accountForm" do |form|
837 assert_equal "post", form.attr("method").to_s
838 assert_select "input[name='_method']", false
839 assert_equal "/user/#{ERB::Util.u(user.display_name)}/account", form.attr("action").to_s
842 # Updating the description should work
843 user.description = "new description"
844 user.preferred_editor = "default"
845 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
846 assert_response :success
847 assert_template :account
848 assert_select "div#errorExplanation", false
849 assert_select ".notice", /^User information updated successfully/
850 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
852 # Changing to a invalid editor should fail
853 user.preferred_editor = "unknown"
854 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
855 assert_response :success
856 assert_template :account
857 assert_select ".notice", false
858 assert_select "div#errorExplanation"
859 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
861 # Changing to a valid editor should work
862 user.preferred_editor = "potlatch2"
863 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
864 assert_response :success
865 assert_template :account
866 assert_select "div#errorExplanation", false
867 assert_select ".notice", /^User information updated successfully/
868 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
870 # Changing to the default editor should work
871 user.preferred_editor = "default"
872 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
873 assert_response :success
874 assert_template :account
875 assert_select "div#errorExplanation", false
876 assert_select ".notice", /^User information updated successfully/
877 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
879 # Changing to an uploaded image should work
880 image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif")
881 post :account, :params => { :display_name => user.display_name, :avatar_action => "new", :user => user.attributes.merge(:avatar => image) }, :session => { :user => user }
882 assert_response :success
883 assert_template :account
884 assert_select "div#errorExplanation", false
885 assert_select ".notice", /^User information updated successfully/
886 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=avatar_action][checked][value=?]", "keep"
888 # Changing to a gravatar image should work
889 post :account, :params => { :display_name => user.display_name, :avatar_action => "gravatar", :user => user.attributes }, :session => { :user => user }
890 assert_response :success
891 assert_template :account
892 assert_select "div#errorExplanation", false
893 assert_select ".notice", /^User information updated successfully/
894 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=avatar_action][checked][value=?]", "gravatar"
896 # Removing the image should work
897 post :account, :params => { :display_name => user.display_name, :avatar_action => "delete", :user => user.attributes }, :session => { :user => user }
898 assert_response :success
899 assert_template :account
900 assert_select "div#errorExplanation", false
901 assert_select ".notice", /^User information updated successfully/
902 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=avatar_action][checked]", false
904 # Adding external authentication should redirect to the auth provider
905 post :account, :params => { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, :session => { :user => user }
906 assert_response :redirect
907 assert_redirected_to auth_path(:provider => "openid", :openid_url => "https://www.google.com/accounts/o8/id", :origin => "/user/#{ERB::Util.u(user.display_name)}/account")
909 # Changing name to one that exists should fail
910 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
911 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
912 assert_response :success
913 assert_template :account
914 assert_select ".notice", false
915 assert_select "div#errorExplanation"
916 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
918 # Changing name to one that exists should fail, regardless of case
919 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
920 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
921 assert_response :success
922 assert_template :account
923 assert_select ".notice", false
924 assert_select "div#errorExplanation"
925 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
927 # Changing name to one that doesn't exist should work
928 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
929 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
930 assert_response :success
931 assert_template :account
932 assert_select "div#errorExplanation", false
933 assert_select ".notice", /^User information updated successfully/
934 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
936 # Record the change of name
937 user.display_name = "new tester"
939 # Changing email to one that exists should fail
940 user.new_email = create(:user).email
941 assert_no_difference "ActionMailer::Base.deliveries.size" do
942 perform_enqueued_jobs do
943 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
946 assert_response :success
947 assert_template :account
948 assert_select ".notice", false
949 assert_select "div#errorExplanation"
950 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
952 # Changing email to one that exists should fail, regardless of case
953 user.new_email = create(:user).email.upcase
954 assert_no_difference "ActionMailer::Base.deliveries.size" do
955 perform_enqueued_jobs do
956 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
959 assert_response :success
960 assert_template :account
961 assert_select ".notice", false
962 assert_select "div#errorExplanation"
963 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
965 # Changing email to one that doesn't exist should work
966 user.new_email = "new_tester@example.com"
967 assert_difference "ActionMailer::Base.deliveries.size", 1 do
968 perform_enqueued_jobs do
969 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
972 assert_response :success
973 assert_template :account
974 assert_select "div#errorExplanation", false
975 assert_select ".notice", /^User information updated successfully/
976 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
977 email = ActionMailer::Base.deliveries.first
978 assert_equal 1, email.to.count
979 assert_equal user.new_email, email.to.first
980 ActionMailer::Base.deliveries.clear
983 # Check that the user account page will display and contains some relevant
984 # information for the user
986 # Test a non-existent user
987 get :show, :params => { :display_name => "unknown" }
988 assert_response :not_found
991 user = create(:user, :home_lon => 1.1, :home_lat => 1.1)
992 friend_user = create(:user, :home_lon => 1.2, :home_lat => 1.2)
993 create(:friendship, :befriender => user, :befriendee => friend_user)
994 create(:changeset, :user => friend_user)
996 get :show, :params => { :display_name => user.display_name }
997 assert_response :success
998 assert_select "div#userinformation" do
999 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1000 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1001 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1002 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1003 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1004 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1005 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1006 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1009 # Friends shouldn't be visible as we're not logged in
1010 assert_select "div#friends-container", :count => 0
1012 # Test a user who has been blocked
1013 blocked_user = create(:user)
1014 create(:user_block, :user => blocked_user)
1015 get :show, :params => { :display_name => blocked_user.display_name }
1016 assert_response :success
1017 assert_select "div#userinformation" do
1018 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
1019 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
1020 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
1021 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
1022 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
1023 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
1024 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
1025 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
1028 # Test a moderator who has applied blocks
1029 moderator_user = create(:moderator_user)
1030 create(:user_block, :creator => moderator_user)
1031 get :show, :params => { :display_name => moderator_user.display_name }
1032 assert_response :success
1033 assert_select "div#userinformation" do
1034 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
1035 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
1036 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
1037 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
1038 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
1039 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
1040 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
1041 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
1044 # Login as a normal user
1045 session[:user] = user.id
1047 # Test the normal user
1048 get :show, :params => { :display_name => user.display_name }
1049 assert_response :success
1050 assert_select "div#userinformation" do
1051 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1052 assert_select "a[href='/traces/mine']", 1
1053 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1054 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1055 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 1
1056 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1057 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1058 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1061 # Friends should be visible as we're now logged in
1062 assert_select "div#friends-container" do
1063 assert_select "div.contact-activity", :count => 1
1066 # Login as a moderator
1067 session[:user] = create(:moderator_user).id
1069 # Test the normal user
1070 get :show, :params => { :display_name => user.display_name }
1071 assert_response :success
1072 assert_select "div#userinformation" do
1073 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1074 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1075 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1076 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1077 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1078 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1079 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1080 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
1084 # Test whether information about contributor terms is shown for users who haven't agreed
1085 def test_terms_not_agreed
1086 agreed_user = create(:user, :terms_agreed => 3.days.ago)
1087 seen_user = create(:user, :terms_seen => true, :terms_agreed => nil)
1088 not_seen_user = create(:user, :terms_seen => false, :terms_agreed => nil)
1090 get :show, :params => { :display_name => agreed_user.display_name }
1091 assert_response :success
1092 assert_select "div#userinformation" do
1093 assert_select "p", :count => 0, :text => /Contributor terms/
1096 get :show, :params => { :display_name => seen_user.display_name }
1097 assert_response :success
1098 # put @response.body
1099 assert_select "div#userinformation" do
1100 assert_select "p", :count => 1, :text => /Contributor terms/
1101 assert_select "p", /Declined/
1104 get :show, :params => { :display_name => not_seen_user.display_name }
1105 assert_response :success
1106 assert_select "div#userinformation" do
1107 assert_select "p", :count => 1, :text => /Contributor terms/
1108 assert_select "p", /Undecided/
1112 def test_make_friend
1113 # Get users to work with
1114 user = create(:user)
1115 friend = create(:user)
1117 # Check that the users aren't already friends
1118 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1120 # When not logged in a GET should ask us to login
1121 get :make_friend, :params => { :display_name => friend.display_name }
1122 assert_redirected_to :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1124 # When not logged in a POST should error
1125 post :make_friend, :params => { :display_name => friend.display_name }
1126 assert_response :forbidden
1127 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1129 # When logged in a GET should get a confirmation page
1130 get :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1131 assert_response :success
1132 assert_template :make_friend
1133 assert_select "form" do
1134 assert_select "input[type='hidden'][name='referer']", 0
1135 assert_select "input[type='submit']", 1
1137 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1139 # When logged in a POST should add the friendship
1140 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1141 perform_enqueued_jobs do
1142 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1145 assert_redirected_to user_path(friend)
1146 assert_match(/is now your friend/, flash[:notice])
1147 assert Friendship.where(:befriender => user, :befriendee => friend).first
1148 email = ActionMailer::Base.deliveries.first
1149 assert_equal 1, email.to.count
1150 assert_equal friend.email, email.to.first
1151 ActionMailer::Base.deliveries.clear
1153 # A second POST should report that the friendship already exists
1154 assert_no_difference "ActionMailer::Base.deliveries.size" do
1155 perform_enqueued_jobs do
1156 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1159 assert_redirected_to user_path(friend)
1160 assert_match(/You are already friends with/, flash[:warning])
1161 assert Friendship.where(:befriender => user, :befriendee => friend).first
1164 def test_make_friend_with_referer
1165 # Get users to work with
1166 user = create(:user)
1167 friend = create(:user)
1169 # Check that the users aren't already friends
1170 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1172 # The GET should preserve any referer
1173 get :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1174 assert_response :success
1175 assert_template :make_friend
1176 assert_select "form" do
1177 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1178 assert_select "input[type='submit']", 1
1180 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1182 # When logged in a POST should add the friendship and refer us
1183 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1184 perform_enqueued_jobs do
1185 post :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1188 assert_redirected_to "/test"
1189 assert_match(/is now your friend/, flash[:notice])
1190 assert Friendship.where(:befriender => user, :befriendee => friend).first
1191 email = ActionMailer::Base.deliveries.first
1192 assert_equal 1, email.to.count
1193 assert_equal friend.email, email.to.first
1194 ActionMailer::Base.deliveries.clear
1197 def test_make_friend_unkown_user
1198 # Should error when a bogus user is specified
1199 get :make_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1200 assert_response :not_found
1201 assert_template :no_such_user
1204 def test_remove_friend
1205 # Get users to work with
1206 user = create(:user)
1207 friend = create(:user)
1208 create(:friendship, :befriender => user, :befriendee => friend)
1210 # Check that the users are friends
1211 assert Friendship.where(:befriender => user, :befriendee => friend).first
1213 # When not logged in a GET should ask us to login
1214 get :remove_friend, :params => { :display_name => friend.display_name }
1215 assert_redirected_to :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1217 # When not logged in a POST should error
1218 post :remove_friend, :params => { :display_name => friend.display_name }
1219 assert_response :forbidden
1220 assert Friendship.where(:befriender => user, :befriendee => friend).first
1222 # When logged in a GET should get a confirmation page
1223 get :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1224 assert_response :success
1225 assert_template :remove_friend
1226 assert_select "form" do
1227 assert_select "input[type='hidden'][name='referer']", 0
1228 assert_select "input[type='submit']", 1
1230 assert Friendship.where(:befriender => user, :befriendee => friend).first
1232 # When logged in a POST should remove the friendship
1233 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1234 assert_redirected_to user_path(friend)
1235 assert_match(/was removed from your friends/, flash[:notice])
1236 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1238 # A second POST should report that the friendship does not exist
1239 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1240 assert_redirected_to user_path(friend)
1241 assert_match(/is not one of your friends/, flash[:error])
1242 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1245 def test_remove_friend_with_referer
1246 # Get users to work with
1247 user = create(:user)
1248 friend = create(:user)
1249 create(:friendship, :befriender => user, :befriendee => friend)
1251 # Check that the users are friends
1252 assert Friendship.where(:befriender => user, :befriendee => friend).first
1254 # The GET should preserve any referer
1255 get :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1256 assert_response :success
1257 assert_template :remove_friend
1258 assert_select "form" do
1259 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1260 assert_select "input[type='submit']", 1
1262 assert Friendship.where(:befriender => user, :befriendee => friend).first
1264 # When logged in a POST should remove the friendship and refer
1265 post :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1266 assert_redirected_to "/test"
1267 assert_match(/was removed from your friends/, flash[:notice])
1268 assert_nil Friendship.where(:befriender => user, :befriendee => friend).first
1271 def test_remove_friend_unkown_user
1272 # Should error when a bogus user is specified
1273 get :remove_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1274 assert_response :not_found
1275 assert_template :no_such_user
1279 user = create(:user)
1281 # Try without logging in
1282 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }
1283 assert_response :redirect
1284 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1286 # Now try as a normal user
1287 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1288 assert_response :redirect
1289 assert_redirected_to :controller => :errors, :action => :forbidden
1291 # Finally try as an administrator
1292 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1293 assert_response :redirect
1294 assert_redirected_to :action => :show, :display_name => user.display_name
1295 assert_equal "suspended", User.find(user.id).status
1299 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1301 # Try without logging in
1302 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }
1303 assert_response :redirect
1304 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1306 # Now try as a normal user
1307 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1308 assert_response :redirect
1309 assert_redirected_to :controller => :errors, :action => :forbidden
1311 # Finally try as an administrator
1312 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1313 assert_response :redirect
1314 assert_redirected_to :action => :show, :display_name => user.display_name
1316 # Check that the user was deleted properly
1318 assert_equal "user_#{user.id}", user.display_name
1319 assert_equal "", user.description
1320 assert_nil user.home_lat
1321 assert_nil user.home_lon
1322 assert_equal false, user.avatar.attached?
1323 assert_equal false, user.email_valid
1324 assert_nil user.new_email
1325 assert_nil user.auth_provider
1326 assert_nil user.auth_uid
1327 assert_equal "deleted", user.status
1331 user = create(:user)
1332 moderator_user = create(:moderator_user)
1333 administrator_user = create(:administrator_user)
1334 _suspended_user = create(:user, :suspended)
1335 _ip_user = create(:user, :creation_ip => "1.2.3.4")
1337 # There are now 7 users - the five above, plus two extra "granters" for the
1338 # moderator_user and administrator_user
1339 assert_equal 7, User.count
1341 # Shouldn't work when not logged in
1343 assert_response :redirect
1344 assert_redirected_to :action => :login, :referer => users_path
1346 session[:user] = user.id
1348 # Shouldn't work when logged in as a normal user
1350 assert_response :redirect
1351 assert_redirected_to :controller => :errors, :action => :forbidden
1353 session[:user] = moderator_user.id
1355 # Shouldn't work when logged in as a moderator
1357 assert_response :redirect
1358 assert_redirected_to :controller => :errors, :action => :forbidden
1360 session[:user] = administrator_user.id
1362 # Note there is a header row, so all row counts are users + 1
1363 # Should work when logged in as an administrator
1365 assert_response :success
1366 assert_template :index
1367 assert_select "table#user_list tr", :count => 7 + 1
1369 # Should be able to limit by status
1370 get :index, :params => { :status => "suspended" }
1371 assert_response :success
1372 assert_template :index
1373 assert_select "table#user_list tr", :count => 1 + 1
1375 # Should be able to limit by IP address
1376 get :index, :params => { :ip => "1.2.3.4" }
1377 assert_response :success
1378 assert_template :index
1379 assert_select "table#user_list tr", :count => 1 + 1
1382 def test_index_get_paginated
1383 1.upto(100).each do |n|
1384 User.create(:display_name => "extra_#{n}",
1385 :email => "extra#{n}@example.com",
1386 :pass_crypt => "extraextra")
1389 session[:user] = create(:administrator_user).id
1391 # 100 examples, an administrator, and a granter for the admin.
1392 assert_equal 102, User.count
1395 assert_response :success
1396 assert_template :index
1397 assert_select "table#user_list tr", :count => 51
1399 get :index, :params => { :page => 2 }
1400 assert_response :success
1401 assert_template :index
1402 assert_select "table#user_list tr", :count => 51
1404 get :index, :params => { :page => 3 }
1405 assert_response :success
1406 assert_template :index
1407 assert_select "table#user_list tr", :count => 3
1410 def test_index_post_confirm
1411 inactive_user = create(:user, :pending)
1412 suspended_user = create(:user, :suspended)
1414 # Shouldn't work when not logged in
1415 assert_no_difference "User.active.count" do
1416 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1418 assert_response :forbidden
1420 assert_equal "pending", inactive_user.reload.status
1421 assert_equal "suspended", suspended_user.reload.status
1423 session[:user] = create(:user).id
1425 # Shouldn't work when logged in as a normal user
1426 assert_no_difference "User.active.count" do
1427 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1429 assert_response :redirect
1430 assert_redirected_to :controller => :errors, :action => :forbidden
1431 assert_equal "pending", inactive_user.reload.status
1432 assert_equal "suspended", suspended_user.reload.status
1434 session[:user] = create(:moderator_user).id
1436 # Shouldn't work when logged in as a moderator
1437 assert_no_difference "User.active.count" do
1438 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1440 assert_response :redirect
1441 assert_redirected_to :controller => :errors, :action => :forbidden
1442 assert_equal "pending", inactive_user.reload.status
1443 assert_equal "suspended", suspended_user.reload.status
1445 session[:user] = create(:administrator_user).id
1447 # Should work when logged in as an administrator
1448 assert_difference "User.active.count", 2 do
1449 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1451 assert_response :redirect
1452 assert_redirected_to :action => :index
1453 assert_equal "confirmed", inactive_user.reload.status
1454 assert_equal "confirmed", suspended_user.reload.status
1457 def test_index_post_hide
1458 normal_user = create(:user)
1459 confirmed_user = create(:user, :confirmed)
1461 # Shouldn't work when not logged in
1462 assert_no_difference "User.active.count" do
1463 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1465 assert_response :forbidden
1467 assert_equal "active", normal_user.reload.status
1468 assert_equal "confirmed", confirmed_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 :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1476 assert_response :redirect
1477 assert_redirected_to :controller => :errors, :action => :forbidden
1478 assert_equal "active", normal_user.reload.status
1479 assert_equal "confirmed", confirmed_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 :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1487 assert_response :redirect
1488 assert_redirected_to :controller => :errors, :action => :forbidden
1489 assert_equal "active", normal_user.reload.status
1490 assert_equal "confirmed", confirmed_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 :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1498 assert_response :redirect
1499 assert_redirected_to :action => :index
1500 assert_equal "deleted", normal_user.reload.status
1501 assert_equal "deleted", confirmed_user.reload.status