3 class UsersControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/user/new", :method => :get },
9 { :controller => "users", :action => "new" }
13 { :path => "/user/new", :method => :post },
14 { :controller => "users", :action => "create" }
18 { :path => "/user/terms", :method => :get },
19 { :controller => "users", :action => "terms" }
23 { :path => "/user/save", :method => :post },
24 { :controller => "users", :action => "save" }
28 { :path => "/user/go_public", :method => :post },
29 { :controller => "users", :action => "go_public" }
33 { :path => "/user/suspended", :method => :get },
34 { :controller => "users", :action => "suspended" }
38 { :path => "/user/username", :method => :get },
39 { :controller => "users", :action => "show", :display_name => "username" }
43 { :path => "/user/username/account", :method => :get },
44 { :controller => "users", :action => "account", :display_name => "username" }
47 { :path => "/user/username/account", :method => :post },
48 { :controller => "users", :action => "account", :display_name => "username" }
52 { :path => "/user/username/set_status", :method => :post },
53 { :controller => "users", :action => "set_status", :display_name => "username" }
56 { :path => "/user/username", :method => :delete },
57 { :controller => "users", :action => "destroy", :display_name => "username" }
61 { :path => "/users", :method => :get },
62 { :controller => "users", :action => "index" }
65 { :path => "/users", :method => :post },
66 { :controller => "users", :action => "index" }
69 { :path => "/users/status", :method => :get },
70 { :controller => "users", :action => "index", :status => "status" }
73 { :path => "/users/status", :method => :post },
74 { :controller => "users", :action => "index", :status => "status" }
78 # The user creation page loads
81 assert_response :redirect
82 assert_redirected_to user_new_path(:cookie_test => "true")
84 get user_new_path, :params => { :cookie_test => "true" }
85 assert_response :success
87 assert_select "html", :count => 1 do
88 assert_select "head", :count => 1 do
89 assert_select "title", :text => /Sign Up/, :count => 1
91 assert_select "body", :count => 1 do
92 assert_select "div#content", :count => 1 do
93 assert_select "form[action='/user/new'][method='post']", :count => 1 do
94 assert_select "input[id='user_email']", :count => 1
95 assert_select "input[id='user_email_confirmation']", :count => 1
96 assert_select "input[id='user_display_name']", :count => 1
97 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
98 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
99 assert_select "input[type='submit'][value='Sign Up']", :count => 1
106 def test_new_view_logged_in
107 session_for(create(:user))
110 assert_response :redirect
111 assert_redirected_to root_path
113 get user_new_path, :params => { :referer => "/test" }
114 assert_response :redirect
115 assert_redirected_to "/test"
119 user = build(:user, :pending)
121 assert_no_difference "User.count" do
122 assert_no_difference "ActionMailer::Base.deliveries.size" do
123 perform_enqueued_jobs do
124 post user_new_path, :params => { :user => user.attributes }
129 assert_difference "User.count", 1 do
130 assert_difference "ActionMailer::Base.deliveries.size", 1 do
131 perform_enqueued_jobs do
132 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
138 register_email = ActionMailer::Base.deliveries.first
140 assert_equal register_email.to[0], user.email
141 assert_match(/#{@url}/, register_email.body.to_s)
144 assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => user.display_name
146 ActionMailer::Base.deliveries.clear
149 def test_new_duplicate_email
150 user = build(:user, :pending)
151 create(:user, :email => user.email)
153 assert_no_difference "User.count" do
154 assert_no_difference "ActionMailer::Base.deliveries.size" do
155 perform_enqueued_jobs do
156 post user_new_path, :params => { :user => user.attributes }
161 assert_response :success
162 assert_template "new"
163 assert_select "form > div.form-group > input.is-invalid#user_email"
166 def test_save_duplicate_email
167 user = build(:user, :pending)
169 # Set up our user as being half-way through registration
170 assert_no_difference "User.count" do
171 assert_no_difference "ActionMailer::Base.deliveries.size" do
172 perform_enqueued_jobs do
173 post user_new_path, :params => { :user => user.attributes }
178 # Now create another user with that email
179 create(:user, :email => user.email)
181 # Check that the second half of registration fails
182 assert_no_difference "User.count" do
183 assert_no_difference "ActionMailer::Base.deliveries.size" do
184 perform_enqueued_jobs do
185 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
190 assert_response :success
191 assert_template "new"
192 assert_select "form > div.form-group > input.is-invalid#user_email"
195 def test_save_duplicate_email_uppercase
196 user = build(:user, :pending)
198 # Set up our user as being half-way through registration
199 assert_no_difference "User.count" do
200 assert_no_difference "ActionMailer::Base.deliveries.size" do
201 perform_enqueued_jobs do
202 post user_new_path, :params => { :user => user.attributes }
207 # Now create another user with that email, but uppercased
208 create(:user, :email => user.email.upcase)
210 # Check that the second half of registration fails
211 assert_no_difference "User.count" do
212 assert_no_difference "ActionMailer::Base.deliveries.size" do
213 perform_enqueued_jobs do
214 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
219 assert_response :success
220 assert_template "new"
221 assert_select "form > div.form-group > input.is-invalid#user_email"
224 def test_save_duplicate_name
225 user = build(:user, :pending)
227 # Set up our user as being half-way through registration
228 assert_no_difference "User.count" do
229 assert_no_difference "ActionMailer::Base.deliveries.size" do
230 perform_enqueued_jobs do
231 post user_new_path, :params => { :user => user.attributes }
236 # Now create another user with that display name
237 create(:user, :display_name => user.display_name)
239 # Check that the second half of registration fails
240 assert_no_difference "User.count" do
241 assert_no_difference "ActionMailer::Base.deliveries.size" do
242 perform_enqueued_jobs do
243 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
248 assert_response :success
249 assert_template "new"
250 assert_select "form > div.form-group > input.is-invalid#user_display_name"
253 def test_save_duplicate_name_uppercase
254 user = build(:user, :pending)
256 # Set up our user as being half-way through registration
257 assert_no_difference "User.count" do
258 assert_no_difference "ActionMailer::Base.deliveries.size" do
259 perform_enqueued_jobs do
260 post user_new_path, :params => { :user => user.attributes }
265 # Now create another user with that display_name, but uppercased
266 create(:user, :display_name => user.display_name.upcase)
268 # Check that the second half of registration fails
269 assert_no_difference "User.count" do
270 assert_no_difference "ActionMailer::Base.deliveries.size" do
271 perform_enqueued_jobs do
272 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
277 assert_response :success
278 assert_template "new"
279 assert_select "form > div.form-group > input.is-invalid#user_display_name"
282 def test_save_blocked_domain
283 user = build(:user, :pending, :email => "user@example.net")
285 # Set up our user as being half-way through registration
286 assert_no_difference "User.count" do
287 assert_no_difference "ActionMailer::Base.deliveries.size" do
288 perform_enqueued_jobs do
289 post user_new_path, :params => { :user => user.attributes }
294 # Now block that domain
295 create(:acl, :domain => "example.net", :k => "no_account_creation")
297 # Check that the second half of registration fails
298 assert_no_difference "User.count" do
299 assert_no_difference "ActionMailer::Base.deliveries.size" do
300 perform_enqueued_jobs do
301 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
306 assert_response :success
307 assert_template "blocked"
310 def test_save_referer_params
311 user = build(:user, :pending)
313 # Set up our user as being half-way through registration
314 assert_no_difference "User.count" do
315 assert_no_difference "ActionMailer::Base.deliveries.size" do
316 perform_enqueued_jobs do
317 post user_new_path, :params => { :user => user.attributes, :referer => "/edit?editor=id#map=1/2/3" }
322 assert_difference "User.count", 1 do
323 assert_difference "ActionMailer::Base.deliveries.size", 1 do
324 perform_enqueued_jobs do
325 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
330 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
331 User.find_by(:email => user.email).tokens.order("id DESC").first.referer
333 ActionMailer::Base.deliveries.clear
336 def test_terms_new_user
337 user = build(:user, :pending)
339 # Set up our user as being half-way through registration
340 assert_no_difference "User.count" do
341 assert_no_difference "ActionMailer::Base.deliveries.size" do
342 perform_enqueued_jobs do
343 post user_new_path, :params => { :user => user.attributes }
350 assert_response :success
351 assert_template :terms
354 def test_terms_agreed
355 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
360 assert_response :redirect
361 assert_redirected_to :action => :account, :display_name => user.display_name
364 def test_terms_not_seen_without_referer
365 user = create(:user, :terms_seen => false, :terms_agreed => nil)
370 assert_response :success
371 assert_template :terms
373 post user_save_path, :params => { :user => { :consider_pd => true }, :read_ct => 1, :read_tou => 1 }
374 assert_response :redirect
375 assert_redirected_to :action => :account, :display_name => user.display_name
376 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
380 assert user.consider_pd
381 assert_not_nil user.terms_agreed
382 assert user.terms_seen
385 def test_terms_not_seen_with_referer
386 user = create(:user, :terms_seen => false, :terms_agreed => nil)
390 get user_terms_path, :params => { :referer => "/test" }
391 assert_response :success
392 assert_template :terms
394 post user_save_path, :params => { :user => { :consider_pd => true }, :referer => "/test", :read_ct => 1, :read_tou => 1 }
395 assert_response :redirect
396 assert_redirected_to "/test"
397 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
401 assert user.consider_pd
402 assert_not_nil user.terms_agreed
403 assert user.terms_seen
406 # Check that if you haven't seen the terms, and make a request that requires authentication,
407 # that your request is redirected to view the terms
408 def test_terms_not_seen_redirection
409 user = create(:user, :terms_seen => false, :terms_agreed => nil)
412 get user_account_path(user)
413 assert_response :redirect
414 assert_redirected_to :action => :terms, :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
418 user = create(:user, :data_public => false)
421 post user_go_public_path
423 assert_response :redirect
424 assert_redirected_to :action => :account, :display_name => user.display_name
425 assert User.find(user.id).data_public
429 # Get a user to work with - note that this user deliberately
430 # conflicts with uppercase_user in the email and display name
431 # fields to test that we can change other fields without any
432 # validation errors being reported
433 user = create(:user, :languages => [])
434 _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
436 # Make sure that you are redirected to the login page when
437 # you are not logged in
438 get user_account_path(user)
439 assert_response :redirect
440 assert_redirected_to login_path(:referer => "/user/#{ERB::Util.u(user.display_name)}/account")
442 # Make sure that you are blocked when not logged in as the right user
443 session_for(create(:user))
444 get user_account_path(user)
445 assert_response :forbidden
447 # Make sure we get the page when we are logged in as the right user
449 get user_account_path(user)
450 assert_response :success
451 assert_template :account
452 assert_select "form#accountForm" do |form|
453 assert_equal "post", form.attr("method").to_s
454 assert_select "input[name='_method']", false
455 assert_equal "/user/#{ERB::Util.u(user.display_name)}/account", form.attr("action").to_s
458 # Updating the description using GET should fail
459 user.description = "new description"
460 user.preferred_editor = "default"
461 get user_account_path(user), :params => { :user => user.attributes }
462 assert_response :success
463 assert_template :account
464 assert_not_equal user.description, User.find(user.id).description
466 # Adding external authentication should redirect to the auth provider
467 post user_account_path(user), :params => { :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }
468 assert_response :redirect
469 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")
471 # Changing name to one that exists should fail
472 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
473 post user_account_path(user), :params => { :user => new_attributes }
474 assert_response :success
475 assert_template :account
476 assert_select ".notice", false
477 assert_select "form#accountForm > div.form-group > input.is-invalid#user_display_name"
479 # Changing name to one that exists should fail, regardless of case
480 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
481 post user_account_path(user), :params => { :user => new_attributes }
482 assert_response :success
483 assert_template :account
484 assert_select ".notice", false
485 assert_select "form#accountForm > div.form-group > input.is-invalid#user_display_name"
487 # Changing name to one that doesn't exist should work
488 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
489 post user_account_path(user), :params => { :user => new_attributes }
490 assert_response :redirect
491 assert_redirected_to user_account_url(:display_name => "new tester")
492 get user_account_path(:display_name => "new tester")
493 assert_response :success
494 assert_template :account
495 assert_select ".notice", /^User information updated successfully/
496 assert_select "form#accountForm > div.form-group > input#user_display_name[value=?]", "new tester"
498 # Record the change of name
499 user.display_name = "new tester"
501 # Changing email to one that exists should fail
502 user.new_email = create(:user).email
503 assert_no_difference "ActionMailer::Base.deliveries.size" do
504 perform_enqueued_jobs do
505 post user_account_path(user), :params => { :user => user.attributes }
508 assert_response :success
509 assert_template :account
510 assert_select ".notice", false
511 assert_select "form#accountForm > div.form-group > input.is-invalid#user_new_email"
513 # Changing email to one that exists should fail, regardless of case
514 user.new_email = create(:user).email.upcase
515 assert_no_difference "ActionMailer::Base.deliveries.size" do
516 perform_enqueued_jobs do
517 post user_account_path(user), :params => { :user => user.attributes }
520 assert_response :success
521 assert_template :account
522 assert_select ".notice", false
523 assert_select "form#accountForm > div.form-group > input.is-invalid#user_new_email"
525 # Changing email to one that doesn't exist should work
526 user.new_email = "new_tester@example.com"
527 assert_difference "ActionMailer::Base.deliveries.size", 1 do
528 perform_enqueued_jobs do
529 post user_account_path(user), :params => { :user => user.attributes }
532 assert_response :redirect
533 assert_redirected_to user_account_url(user)
534 get user_account_path(user)
535 assert_response :success
536 assert_template :account
537 assert_select ".notice", /^User information updated successfully/
538 assert_select "form#accountForm > div.form-group > input#user_new_email[value=?]", user.new_email
539 email = ActionMailer::Base.deliveries.first
540 assert_equal 1, email.to.count
541 assert_equal user.new_email, email.to.first
542 ActionMailer::Base.deliveries.clear
545 # Check that the user account page will display and contains some relevant
546 # information for the user
548 # Test a non-existent user
549 get user_path(:display_name => "unknown")
550 assert_response :not_found
553 user = create(:user, :home_lon => 1.1, :home_lat => 1.1)
554 friend_user = create(:user, :home_lon => 1.2, :home_lat => 1.2)
555 create(:friendship, :befriender => user, :befriendee => friend_user)
556 create(:changeset, :user => friend_user)
559 assert_response :success
560 assert_select "div#userinformation" do
561 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
562 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
563 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
564 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
565 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
566 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
567 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
568 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
571 # Friends shouldn't be visible as we're not logged in
572 assert_select "div#friends-container", :count => 0
574 # Test a user who has been blocked
575 blocked_user = create(:user)
576 create(:user_block, :user => blocked_user)
577 get user_path(blocked_user)
578 assert_response :success
579 assert_select "div#userinformation" do
580 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
581 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
582 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
583 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
584 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
585 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
586 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
587 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
590 # Test a moderator who has applied blocks
591 moderator_user = create(:moderator_user)
592 create(:user_block, :creator => moderator_user)
593 get user_path(moderator_user)
594 assert_response :success
595 assert_select "div#userinformation" do
596 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
597 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
598 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
599 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
600 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
601 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
602 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
603 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
606 # Login as a normal user
609 # Test the normal user
611 assert_response :success
612 assert_select "div#userinformation" do
613 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
614 assert_select "a[href='/traces/mine']", 1
615 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
616 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
617 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 1
618 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
619 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
620 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
623 # Friends should be visible as we're now logged in
624 assert_select "div#friends-container" do
625 assert_select "div.contact-activity", :count => 1
628 # Login as a moderator
629 session_for(create(:moderator_user))
631 # Test the normal user
633 assert_response :success
634 assert_select "div#userinformation" do
635 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
636 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
637 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
638 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
639 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
640 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
641 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
642 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
646 # Test whether information about contributor terms is shown for users who haven't agreed
647 def test_terms_not_agreed
648 agreed_user = create(:user, :terms_agreed => 3.days.ago)
649 seen_user = create(:user, :terms_seen => true, :terms_agreed => nil)
650 not_seen_user = create(:user, :terms_seen => false, :terms_agreed => nil)
652 get user_path(agreed_user)
653 assert_response :success
654 assert_select "div#userinformation" do
655 assert_select "p", :count => 0, :text => /Contributor terms/
658 get user_path(seen_user)
659 assert_response :success
661 assert_select "div#userinformation" do
662 assert_select "p", :count => 1, :text => /Contributor terms/
663 assert_select "p", /Declined/
666 get user_path(not_seen_user)
667 assert_response :success
668 assert_select "div#userinformation" do
669 assert_select "p", :count => 1, :text => /Contributor terms/
670 assert_select "p", /Undecided/
677 # Try without logging in
678 post set_status_user_path(user), :params => { :status => "suspended" }
679 assert_response :forbidden
681 # Now try as a normal user
683 post set_status_user_path(user), :params => { :status => "suspended" }
684 assert_response :redirect
685 assert_redirected_to :controller => :errors, :action => :forbidden
687 # Finally try as an administrator
688 session_for(create(:administrator_user))
689 post set_status_user_path(user), :params => { :status => "suspended" }
690 assert_response :redirect
691 assert_redirected_to :action => :show, :display_name => user.display_name
692 assert_equal "suspended", User.find(user.id).status
696 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
698 # Try without logging in
699 delete user_path(user), :params => { :status => "suspended" }
700 assert_response :forbidden
702 # Now try as a normal user
704 delete user_path(user), :params => { :status => "suspended" }
705 assert_response :redirect
706 assert_redirected_to :controller => :errors, :action => :forbidden
708 # Finally try as an administrator
709 session_for(create(:administrator_user))
710 delete user_path(user), :params => { :status => "suspended" }
711 assert_response :redirect
712 assert_redirected_to :action => :show, :display_name => user.display_name
714 # Check that the user was deleted properly
716 assert_equal "user_#{user.id}", user.display_name
717 assert_equal "", user.description
718 assert_nil user.home_lat
719 assert_nil user.home_lon
720 assert_not user.avatar.attached?
721 assert_not user.email_valid
722 assert_nil user.new_email
723 assert_nil user.auth_provider
724 assert_nil user.auth_uid
725 assert_equal "deleted", user.status
730 moderator_user = create(:moderator_user)
731 administrator_user = create(:administrator_user)
732 _suspended_user = create(:user, :suspended)
733 _ip_user = create(:user, :creation_ip => "1.2.3.4")
735 # There are now 7 users - the five above, plus two extra "granters" for the
736 # moderator_user and administrator_user
737 assert_equal 7, User.count
739 # Shouldn't work when not logged in
741 assert_response :redirect
742 assert_redirected_to login_path(:referer => users_path)
746 # Shouldn't work when logged in as a normal user
748 assert_response :redirect
749 assert_redirected_to :controller => :errors, :action => :forbidden
751 session_for(moderator_user)
753 # Shouldn't work when logged in as a moderator
755 assert_response :redirect
756 assert_redirected_to :controller => :errors, :action => :forbidden
758 session_for(administrator_user)
760 # Note there is a header row, so all row counts are users + 1
761 # Should work when logged in as an administrator
763 assert_response :success
764 assert_template :index
765 assert_select "table#user_list tr", :count => 7 + 1
767 # Should be able to limit by status
768 get users_path, :params => { :status => "suspended" }
769 assert_response :success
770 assert_template :index
771 assert_select "table#user_list tr", :count => 1 + 1
773 # Should be able to limit by IP address
774 get users_path, :params => { :ip => "1.2.3.4" }
775 assert_response :success
776 assert_template :index
777 assert_select "table#user_list tr", :count => 1 + 1
780 def test_index_get_paginated
781 1.upto(100).each do |n|
782 User.create(:display_name => "extra_#{n}",
783 :email => "extra#{n}@example.com",
784 :pass_crypt => "extraextra")
787 session_for(create(:administrator_user))
789 # 100 examples, an administrator, and a granter for the admin.
790 assert_equal 102, User.count
793 assert_response :success
794 assert_template :index
795 assert_select "table#user_list tr", :count => 51
797 get users_path, :params => { :page => 2 }
798 assert_response :success
799 assert_template :index
800 assert_select "table#user_list tr", :count => 51
802 get users_path, :params => { :page => 3 }
803 assert_response :success
804 assert_template :index
805 assert_select "table#user_list tr", :count => 3
808 def test_index_post_confirm
809 inactive_user = create(:user, :pending)
810 suspended_user = create(:user, :suspended)
812 # Shouldn't work when not logged in
813 assert_no_difference "User.active.count" do
814 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
816 assert_response :forbidden
818 assert_equal "pending", inactive_user.reload.status
819 assert_equal "suspended", suspended_user.reload.status
821 session_for(create(:user))
823 # Shouldn't work when logged in as a normal user
824 assert_no_difference "User.active.count" do
825 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
827 assert_response :redirect
828 assert_redirected_to :controller => :errors, :action => :forbidden
829 assert_equal "pending", inactive_user.reload.status
830 assert_equal "suspended", suspended_user.reload.status
832 session_for(create(:moderator_user))
834 # Shouldn't work when logged in as a moderator
835 assert_no_difference "User.active.count" do
836 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
838 assert_response :redirect
839 assert_redirected_to :controller => :errors, :action => :forbidden
840 assert_equal "pending", inactive_user.reload.status
841 assert_equal "suspended", suspended_user.reload.status
843 session_for(create(:administrator_user))
845 # Should work when logged in as an administrator
846 assert_difference "User.active.count", 2 do
847 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
849 assert_response :redirect
850 assert_redirected_to :action => :index
851 assert_equal "confirmed", inactive_user.reload.status
852 assert_equal "confirmed", suspended_user.reload.status
855 def test_index_post_hide
856 normal_user = create(:user)
857 confirmed_user = create(:user, :confirmed)
859 # Shouldn't work when not logged in
860 assert_no_difference "User.active.count" do
861 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
863 assert_response :forbidden
865 assert_equal "active", normal_user.reload.status
866 assert_equal "confirmed", confirmed_user.reload.status
868 session_for(create(:user))
870 # Shouldn't work when logged in as a normal user
871 assert_no_difference "User.active.count" do
872 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
874 assert_response :redirect
875 assert_redirected_to :controller => :errors, :action => :forbidden
876 assert_equal "active", normal_user.reload.status
877 assert_equal "confirmed", confirmed_user.reload.status
879 session_for(create(:moderator_user))
881 # Shouldn't work when logged in as a moderator
882 assert_no_difference "User.active.count" do
883 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
885 assert_response :redirect
886 assert_redirected_to :controller => :errors, :action => :forbidden
887 assert_equal "active", normal_user.reload.status
888 assert_equal "confirmed", confirmed_user.reload.status
890 session_for(create(:administrator_user))
892 # Should work when logged in as an administrator
893 assert_difference "User.active.count", -2 do
894 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
896 assert_response :redirect
897 assert_redirected_to :action => :index
898 assert_equal "deleted", normal_user.reload.status
899 assert_equal "deleted", confirmed_user.reload.status