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 # Updating the description should work
467 user.description = "new description"
468 user.preferred_editor = "default"
469 post user_account_path(user), :params => { :user => user.attributes }
470 assert_response :redirect
471 assert_redirected_to user_account_url(user)
472 get user_account_path(user)
473 assert_response :success
474 assert_template :account
475 assert_select ".notice", /^User information updated successfully/
476 assert_select "form#accountForm > div.form-group > div#user_description_container > div#user_description_content > textarea#user_description", user.description
478 # Changing to a invalid editor should fail
479 user.preferred_editor = "unknown"
480 post user_account_path(user), :params => { :user => user.attributes }
481 assert_response :success
482 assert_template :account
483 assert_select ".notice", false
484 assert_select "form#accountForm > div.form-group > select#user_preferred_editor > option[selected]", false
486 # Changing to a valid editor should work
487 user.preferred_editor = "id"
488 post user_account_path(user), :params => { :user => user.attributes }
489 assert_response :redirect
490 assert_redirected_to user_account_url(user)
491 get user_account_path(user)
492 assert_response :success
493 assert_template :account
494 assert_select ".notice", /^User information updated successfully/
495 assert_select "form#accountForm > div.form-group > select#user_preferred_editor > option[selected][value=?]", "id"
497 # Changing to the default editor should work
498 user.preferred_editor = "default"
499 post user_account_path(user), :params => { :user => user.attributes }
500 assert_response :redirect
501 assert_redirected_to user_account_url(user)
502 get user_account_path(user)
503 assert_response :success
504 assert_template :account
505 assert_select ".notice", /^User information updated successfully/
506 assert_select "form#accountForm > div.form-group > select#user_preferred_editor > option[selected]", false
508 # Changing to an uploaded image should work
509 image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif")
510 post user_account_path(user), :params => { :avatar_action => "new", :user => user.attributes.merge(:avatar => image) }
511 assert_response :redirect
512 assert_redirected_to user_account_url(user)
513 get user_account_path(user)
514 assert_response :success
515 assert_template :account
516 assert_select ".notice", /^User information updated successfully/
517 assert_select "form#accountForm > fieldset.form-group > div.form-row > div.col-sm-10 > div.form-check > input[name=avatar_action][checked][value=?]", "keep"
519 # Changing to a gravatar image should work
520 post user_account_path(user), :params => { :avatar_action => "gravatar", :user => user.attributes }
521 assert_response :redirect
522 assert_redirected_to user_account_url(user)
523 get user_account_path(user)
524 assert_response :success
525 assert_template :account
526 assert_select ".notice", /^User information updated successfully/
527 assert_select "form#accountForm > fieldset.form-group > div.form-row > div.col-sm-10 > div.form-group > div.form-check > input[name=avatar_action][checked][value=?]", "gravatar"
529 # Removing the image should work
530 post user_account_path(user), :params => { :avatar_action => "delete", :user => user.attributes }
531 assert_response :redirect
532 assert_redirected_to user_account_url(user)
533 get user_account_path(user)
534 assert_response :success
535 assert_template :account
536 assert_select ".notice", /^User information updated successfully/
537 assert_select "form#accountForm > fieldset.form-group > div.form-row > div.col-sm-10 > div.form-check > input[name=avatar_action][checked]", false
538 assert_select "form#accountForm > fieldset.form-group > div.form-row > div.col-sm-10 > div.form-group > div.form-check > input[name=avatar_action][checked]", false
540 # Adding external authentication should redirect to the auth provider
541 post user_account_path(user), :params => { :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }
542 assert_response :redirect
543 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")
545 # Changing name to one that exists should fail
546 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
547 post user_account_path(user), :params => { :user => new_attributes }
548 assert_response :success
549 assert_template :account
550 assert_select ".notice", false
551 assert_select "form#accountForm > div.form-group > input.is-invalid#user_display_name"
553 # Changing name to one that exists should fail, regardless of case
554 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
555 post user_account_path(user), :params => { :user => new_attributes }
556 assert_response :success
557 assert_template :account
558 assert_select ".notice", false
559 assert_select "form#accountForm > div.form-group > input.is-invalid#user_display_name"
561 # Changing name to one that doesn't exist should work
562 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
563 post user_account_path(user), :params => { :user => new_attributes }
564 assert_response :redirect
565 assert_redirected_to user_account_url(:display_name => "new tester")
566 get user_account_path(:display_name => "new tester")
567 assert_response :success
568 assert_template :account
569 assert_select ".notice", /^User information updated successfully/
570 assert_select "form#accountForm > div.form-group > input#user_display_name[value=?]", "new tester"
572 # Record the change of name
573 user.display_name = "new tester"
575 # Changing email to one that exists should fail
576 user.new_email = create(:user).email
577 assert_no_difference "ActionMailer::Base.deliveries.size" do
578 perform_enqueued_jobs do
579 post user_account_path(user), :params => { :user => user.attributes }
582 assert_response :success
583 assert_template :account
584 assert_select ".notice", false
585 assert_select "form#accountForm > div.form-group > input.is-invalid#user_new_email"
587 # Changing email to one that exists should fail, regardless of case
588 user.new_email = create(:user).email.upcase
589 assert_no_difference "ActionMailer::Base.deliveries.size" do
590 perform_enqueued_jobs do
591 post user_account_path(user), :params => { :user => user.attributes }
594 assert_response :success
595 assert_template :account
596 assert_select ".notice", false
597 assert_select "form#accountForm > div.form-group > input.is-invalid#user_new_email"
599 # Changing email to one that doesn't exist should work
600 user.new_email = "new_tester@example.com"
601 assert_difference "ActionMailer::Base.deliveries.size", 1 do
602 perform_enqueued_jobs do
603 post user_account_path(user), :params => { :user => user.attributes }
606 assert_response :redirect
607 assert_redirected_to user_account_url(user)
608 get user_account_path(user)
609 assert_response :success
610 assert_template :account
611 assert_select ".notice", /^User information updated successfully/
612 assert_select "form#accountForm > div.form-group > input#user_new_email[value=?]", user.new_email
613 email = ActionMailer::Base.deliveries.first
614 assert_equal 1, email.to.count
615 assert_equal user.new_email, email.to.first
616 ActionMailer::Base.deliveries.clear
619 # Check that the user account page will display and contains some relevant
620 # information for the user
622 # Test a non-existent user
623 get user_path(:display_name => "unknown")
624 assert_response :not_found
627 user = create(:user, :home_lon => 1.1, :home_lat => 1.1)
628 friend_user = create(:user, :home_lon => 1.2, :home_lat => 1.2)
629 create(:friendship, :befriender => user, :befriendee => friend_user)
630 create(:changeset, :user => friend_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)}']", 0
645 # Friends shouldn't be visible as we're not logged in
646 assert_select "div#friends-container", :count => 0
648 # Test a user who has been blocked
649 blocked_user = create(:user)
650 create(:user_block, :user => blocked_user)
651 get user_path(blocked_user)
652 assert_response :success
653 assert_select "div#userinformation" do
654 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
655 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
656 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
657 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
658 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
659 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
660 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
661 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
664 # Test a moderator who has applied blocks
665 moderator_user = create(:moderator_user)
666 create(:user_block, :creator => moderator_user)
667 get user_path(moderator_user)
668 assert_response :success
669 assert_select "div#userinformation" do
670 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
671 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
672 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
673 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
674 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
675 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
676 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
677 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
680 # Login as a normal user
683 # Test the normal user
685 assert_response :success
686 assert_select "div#userinformation" do
687 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
688 assert_select "a[href='/traces/mine']", 1
689 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
690 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
691 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 1
692 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
693 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
694 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
697 # Friends should be visible as we're now logged in
698 assert_select "div#friends-container" do
699 assert_select "div.contact-activity", :count => 1
702 # Login as a moderator
703 session_for(create(:moderator_user))
705 # Test the normal user
707 assert_response :success
708 assert_select "div#userinformation" do
709 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
710 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
711 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
712 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
713 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
714 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
715 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
716 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
720 # Test whether information about contributor terms is shown for users who haven't agreed
721 def test_terms_not_agreed
722 agreed_user = create(:user, :terms_agreed => 3.days.ago)
723 seen_user = create(:user, :terms_seen => true, :terms_agreed => nil)
724 not_seen_user = create(:user, :terms_seen => false, :terms_agreed => nil)
726 get user_path(agreed_user)
727 assert_response :success
728 assert_select "div#userinformation" do
729 assert_select "p", :count => 0, :text => /Contributor terms/
732 get user_path(seen_user)
733 assert_response :success
735 assert_select "div#userinformation" do
736 assert_select "p", :count => 1, :text => /Contributor terms/
737 assert_select "p", /Declined/
740 get user_path(not_seen_user)
741 assert_response :success
742 assert_select "div#userinformation" do
743 assert_select "p", :count => 1, :text => /Contributor terms/
744 assert_select "p", /Undecided/
751 # Try without logging in
752 post set_status_user_path(user), :params => { :status => "suspended" }
753 assert_response :forbidden
755 # Now try as a normal user
757 post set_status_user_path(user), :params => { :status => "suspended" }
758 assert_response :redirect
759 assert_redirected_to :controller => :errors, :action => :forbidden
761 # Finally try as an administrator
762 session_for(create(:administrator_user))
763 post set_status_user_path(user), :params => { :status => "suspended" }
764 assert_response :redirect
765 assert_redirected_to :action => :show, :display_name => user.display_name
766 assert_equal "suspended", User.find(user.id).status
770 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
772 # Try without logging in
773 delete user_path(user), :params => { :status => "suspended" }
774 assert_response :forbidden
776 # Now try as a normal user
778 delete user_path(user), :params => { :status => "suspended" }
779 assert_response :redirect
780 assert_redirected_to :controller => :errors, :action => :forbidden
782 # Finally try as an administrator
783 session_for(create(:administrator_user))
784 delete user_path(user), :params => { :status => "suspended" }
785 assert_response :redirect
786 assert_redirected_to :action => :show, :display_name => user.display_name
788 # Check that the user was deleted properly
790 assert_equal "user_#{user.id}", user.display_name
791 assert_equal "", user.description
792 assert_nil user.home_lat
793 assert_nil user.home_lon
794 assert_not user.avatar.attached?
795 assert_not user.email_valid
796 assert_nil user.new_email
797 assert_nil user.auth_provider
798 assert_nil user.auth_uid
799 assert_equal "deleted", user.status
804 moderator_user = create(:moderator_user)
805 administrator_user = create(:administrator_user)
806 _suspended_user = create(:user, :suspended)
807 _ip_user = create(:user, :creation_ip => "1.2.3.4")
809 # There are now 7 users - the five above, plus two extra "granters" for the
810 # moderator_user and administrator_user
811 assert_equal 7, User.count
813 # Shouldn't work when not logged in
815 assert_response :redirect
816 assert_redirected_to login_path(:referer => users_path)
820 # Shouldn't work when logged in as a normal user
822 assert_response :redirect
823 assert_redirected_to :controller => :errors, :action => :forbidden
825 session_for(moderator_user)
827 # Shouldn't work when logged in as a moderator
829 assert_response :redirect
830 assert_redirected_to :controller => :errors, :action => :forbidden
832 session_for(administrator_user)
834 # Note there is a header row, so all row counts are users + 1
835 # Should work when logged in as an administrator
837 assert_response :success
838 assert_template :index
839 assert_select "table#user_list tr", :count => 7 + 1
841 # Should be able to limit by status
842 get users_path, :params => { :status => "suspended" }
843 assert_response :success
844 assert_template :index
845 assert_select "table#user_list tr", :count => 1 + 1
847 # Should be able to limit by IP address
848 get users_path, :params => { :ip => "1.2.3.4" }
849 assert_response :success
850 assert_template :index
851 assert_select "table#user_list tr", :count => 1 + 1
854 def test_index_get_paginated
855 1.upto(100).each do |n|
856 User.create(:display_name => "extra_#{n}",
857 :email => "extra#{n}@example.com",
858 :pass_crypt => "extraextra")
861 session_for(create(:administrator_user))
863 # 100 examples, an administrator, and a granter for the admin.
864 assert_equal 102, User.count
867 assert_response :success
868 assert_template :index
869 assert_select "table#user_list tr", :count => 51
871 get users_path, :params => { :page => 2 }
872 assert_response :success
873 assert_template :index
874 assert_select "table#user_list tr", :count => 51
876 get users_path, :params => { :page => 3 }
877 assert_response :success
878 assert_template :index
879 assert_select "table#user_list tr", :count => 3
882 def test_index_post_confirm
883 inactive_user = create(:user, :pending)
884 suspended_user = create(:user, :suspended)
886 # Shouldn't work when not logged in
887 assert_no_difference "User.active.count" do
888 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
890 assert_response :forbidden
892 assert_equal "pending", inactive_user.reload.status
893 assert_equal "suspended", suspended_user.reload.status
895 session_for(create(:user))
897 # Shouldn't work when logged in as a normal user
898 assert_no_difference "User.active.count" do
899 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
901 assert_response :redirect
902 assert_redirected_to :controller => :errors, :action => :forbidden
903 assert_equal "pending", inactive_user.reload.status
904 assert_equal "suspended", suspended_user.reload.status
906 session_for(create(:moderator_user))
908 # Shouldn't work when logged in as a moderator
909 assert_no_difference "User.active.count" do
910 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
912 assert_response :redirect
913 assert_redirected_to :controller => :errors, :action => :forbidden
914 assert_equal "pending", inactive_user.reload.status
915 assert_equal "suspended", suspended_user.reload.status
917 session_for(create(:administrator_user))
919 # Should work when logged in as an administrator
920 assert_difference "User.active.count", 2 do
921 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
923 assert_response :redirect
924 assert_redirected_to :action => :index
925 assert_equal "confirmed", inactive_user.reload.status
926 assert_equal "confirmed", suspended_user.reload.status
929 def test_index_post_hide
930 normal_user = create(:user)
931 confirmed_user = create(:user, :confirmed)
933 # Shouldn't work when not logged in
934 assert_no_difference "User.active.count" do
935 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
937 assert_response :forbidden
939 assert_equal "active", normal_user.reload.status
940 assert_equal "confirmed", confirmed_user.reload.status
942 session_for(create(:user))
944 # Shouldn't work when logged in as a normal user
945 assert_no_difference "User.active.count" do
946 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
948 assert_response :redirect
949 assert_redirected_to :controller => :errors, :action => :forbidden
950 assert_equal "active", normal_user.reload.status
951 assert_equal "confirmed", confirmed_user.reload.status
953 session_for(create(:moderator_user))
955 # Shouldn't work when logged in as a moderator
956 assert_no_difference "User.active.count" do
957 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
959 assert_response :redirect
960 assert_redirected_to :controller => :errors, :action => :forbidden
961 assert_equal "active", normal_user.reload.status
962 assert_equal "confirmed", confirmed_user.reload.status
964 session_for(create(:administrator_user))
966 # Should work when logged in as an administrator
967 assert_difference "User.active.count", -2 do
968 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
970 assert_response :redirect
971 assert_redirected_to :action => :index
972 assert_equal "deleted", normal_user.reload.status
973 assert_equal "deleted", confirmed_user.reload.status