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/set_status", :method => :post },
44 { :controller => "users", :action => "set_status", :display_name => "username" }
47 { :path => "/user/username", :method => :delete },
48 { :controller => "users", :action => "destroy", :display_name => "username" }
52 { :path => "/users", :method => :get },
53 { :controller => "users", :action => "index" }
56 { :path => "/users", :method => :post },
57 { :controller => "users", :action => "index" }
60 { :path => "/users/status", :method => :get },
61 { :controller => "users", :action => "index", :status => "status" }
64 { :path => "/users/status", :method => :post },
65 { :controller => "users", :action => "index", :status => "status" }
69 # The user creation page loads
72 assert_response :redirect
73 assert_redirected_to user_new_path(:cookie_test => "true")
75 get user_new_path, :params => { :cookie_test => "true" }
76 assert_response :success
78 assert_select "html", :count => 1 do
79 assert_select "head", :count => 1 do
80 assert_select "title", :text => /Sign Up/, :count => 1
82 assert_select "body", :count => 1 do
83 assert_select "div#content", :count => 1 do
84 assert_select "form[action='/user/new'][method='post']", :count => 1 do
85 assert_select "input[id='user_email']", :count => 1
86 assert_select "input[id='user_email_confirmation']", :count => 1
87 assert_select "input[id='user_display_name']", :count => 1
88 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
89 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
90 assert_select "input[type='submit'][value='Sign Up']", :count => 1
97 def test_new_view_logged_in
98 session_for(create(:user))
101 assert_response :redirect
102 assert_redirected_to root_path
104 get user_new_path, :params => { :referer => "/test" }
105 assert_response :redirect
106 assert_redirected_to "/test"
110 user = build(:user, :pending)
112 assert_no_difference "User.count" do
113 assert_no_difference "ActionMailer::Base.deliveries.size" do
114 perform_enqueued_jobs do
115 post user_new_path, :params => { :user => user.attributes }
120 assert_difference "User.count", 1 do
121 assert_difference "ActionMailer::Base.deliveries.size", 1 do
122 perform_enqueued_jobs do
123 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
129 register_email = ActionMailer::Base.deliveries.first
131 assert_equal register_email.to[0], user.email
132 assert_match(/#{@url}/, register_email.body.to_s)
135 assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => user.display_name
137 ActionMailer::Base.deliveries.clear
140 def test_new_duplicate_email
141 user = build(:user, :pending)
142 create(:user, :email => user.email)
144 assert_no_difference "User.count" do
145 assert_no_difference "ActionMailer::Base.deliveries.size" do
146 perform_enqueued_jobs do
147 post user_new_path, :params => { :user => user.attributes }
152 assert_response :success
153 assert_template "new"
154 assert_select "form > div > input.is-invalid#user_email"
157 def test_save_duplicate_email
158 user = build(:user, :pending)
160 # Set up our user as being half-way through registration
161 assert_no_difference "User.count" do
162 assert_no_difference "ActionMailer::Base.deliveries.size" do
163 perform_enqueued_jobs do
164 post user_new_path, :params => { :user => user.attributes }
169 # Now create another user with that email
170 create(:user, :email => user.email)
172 # Check that the second half of registration fails
173 assert_no_difference "User.count" do
174 assert_no_difference "ActionMailer::Base.deliveries.size" do
175 perform_enqueued_jobs do
176 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
181 assert_response :success
182 assert_template "new"
183 assert_select "form > div > input.is-invalid#user_email"
186 def test_save_duplicate_email_uppercase
187 user = build(:user, :pending)
189 # Set up our user as being half-way through registration
190 assert_no_difference "User.count" do
191 assert_no_difference "ActionMailer::Base.deliveries.size" do
192 perform_enqueued_jobs do
193 post user_new_path, :params => { :user => user.attributes }
198 # Now create another user with that email, but uppercased
199 create(:user, :email => user.email.upcase)
201 # Check that the second half of registration fails
202 assert_no_difference "User.count" do
203 assert_no_difference "ActionMailer::Base.deliveries.size" do
204 perform_enqueued_jobs do
205 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
210 assert_response :success
211 assert_template "new"
212 assert_select "form > div > input.is-invalid#user_email"
215 def test_save_duplicate_name
216 user = build(:user, :pending)
218 # Set up our user as being half-way through registration
219 assert_no_difference "User.count" do
220 assert_no_difference "ActionMailer::Base.deliveries.size" do
221 perform_enqueued_jobs do
222 post user_new_path, :params => { :user => user.attributes }
227 # Now create another user with that display name
228 create(:user, :display_name => user.display_name)
230 # Check that the second half of registration fails
231 assert_no_difference "User.count" do
232 assert_no_difference "ActionMailer::Base.deliveries.size" do
233 perform_enqueued_jobs do
234 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
239 assert_response :success
240 assert_template "new"
241 assert_select "form > div > input.is-invalid#user_display_name"
244 def test_save_duplicate_name_uppercase
245 user = build(:user, :pending)
247 # Set up our user as being half-way through registration
248 assert_no_difference "User.count" do
249 assert_no_difference "ActionMailer::Base.deliveries.size" do
250 perform_enqueued_jobs do
251 post user_new_path, :params => { :user => user.attributes }
256 # Now create another user with that display_name, but uppercased
257 create(:user, :display_name => user.display_name.upcase)
259 # Check that the second half of registration fails
260 assert_no_difference "User.count" do
261 assert_no_difference "ActionMailer::Base.deliveries.size" do
262 perform_enqueued_jobs do
263 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
268 assert_response :success
269 assert_template "new"
270 assert_select "form > div > input.is-invalid#user_display_name"
273 def test_save_blocked_domain
274 user = build(:user, :pending, :email => "user@example.net")
276 # Set up our user as being half-way through registration
277 assert_no_difference "User.count" do
278 assert_no_difference "ActionMailer::Base.deliveries.size" do
279 perform_enqueued_jobs do
280 post user_new_path, :params => { :user => user.attributes }
285 # Now block that domain
286 create(:acl, :domain => "example.net", :k => "no_account_creation")
288 # Check that the second half of registration fails
289 assert_no_difference "User.count" do
290 assert_no_difference "ActionMailer::Base.deliveries.size" do
291 perform_enqueued_jobs do
292 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
297 assert_response :success
298 assert_template "blocked"
301 def test_save_referer_params
302 user = build(:user, :pending)
304 # Set up our user as being half-way through registration
305 assert_no_difference "User.count" do
306 assert_no_difference "ActionMailer::Base.deliveries.size" do
307 perform_enqueued_jobs do
308 post user_new_path, :params => { :user => user.attributes, :referer => "/edit?editor=id#map=1/2/3" }
313 assert_difference "User.count", 1 do
314 assert_difference "ActionMailer::Base.deliveries.size", 1 do
315 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
316 assert_enqueued_with :job => ActionMailer::MailDeliveryJob,
317 :args => proc { |args| args[3][:args][2] == welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3) }
318 perform_enqueued_jobs
322 ActionMailer::Base.deliveries.clear
325 def test_terms_new_user
326 user = build(:user, :pending)
328 # Set up our user as being half-way through registration
329 assert_no_difference "User.count" do
330 assert_no_difference "ActionMailer::Base.deliveries.size" do
331 perform_enqueued_jobs do
332 post user_new_path, :params => { :user => user.attributes }
339 assert_response :success
340 assert_template :terms
343 def test_terms_agreed
344 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
349 assert_response :redirect
350 assert_redirected_to edit_account_path
353 def test_terms_not_seen_without_referer
354 user = create(:user, :terms_seen => false, :terms_agreed => nil)
359 assert_response :success
360 assert_template :terms
362 post user_save_path, :params => { :user => { :consider_pd => true }, :read_ct => 1, :read_tou => 1 }
363 assert_response :redirect
364 assert_redirected_to edit_account_path
365 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
369 assert user.consider_pd
370 assert_not_nil user.terms_agreed
371 assert user.terms_seen
374 def test_terms_not_seen_with_referer
375 user = create(:user, :terms_seen => false, :terms_agreed => nil)
379 get user_terms_path, :params => { :referer => "/test" }
380 assert_response :success
381 assert_template :terms
383 post user_save_path, :params => { :user => { :consider_pd => true }, :referer => "/test", :read_ct => 1, :read_tou => 1 }
384 assert_response :redirect
385 assert_redirected_to "/test"
386 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
390 assert user.consider_pd
391 assert_not_nil user.terms_agreed
392 assert user.terms_seen
395 # Check that if you haven't seen the terms, and make a request that requires authentication,
396 # that your request is redirected to view the terms
397 def test_terms_not_seen_redirection
398 user = create(:user, :terms_seen => false, :terms_agreed => nil)
401 get edit_account_path
402 assert_response :redirect
403 assert_redirected_to :controller => :users, :action => :terms, :referer => "/account/edit"
406 def test_terms_not_logged_in
409 assert_redirected_to login_path(:referer => "/user/terms")
413 user = create(:user, :data_public => false)
416 post user_go_public_path
418 assert_response :redirect
419 assert_redirected_to edit_account_path
420 assert User.find(user.id).data_public
423 # Check that the user account page will display and contains some relevant
424 # information for the user
426 # Test a non-existent user
427 get user_path(:display_name => "unknown")
428 assert_response :not_found
434 assert_response :success
435 assert_select "div.content-heading" do
436 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
437 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
438 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
439 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
440 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
441 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
442 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
443 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
446 # Friends shouldn't be visible as we're not logged in
447 assert_select "div#friends-container", :count => 0
449 # Test a user who has been blocked
450 blocked_user = create(:user)
451 create(:user_block, :user => blocked_user)
452 get user_path(blocked_user)
453 assert_response :success
454 assert_select "div.content-heading" do
455 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
456 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
457 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
458 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
459 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
460 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
461 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
462 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
465 # Test a moderator who has applied blocks
466 moderator_user = create(:moderator_user)
467 create(:user_block, :creator => moderator_user)
468 get user_path(moderator_user)
469 assert_response :success
470 assert_select "div.content-heading" do
471 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
472 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
473 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
474 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
475 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
476 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
477 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
478 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
481 # Login as a normal user
484 # Test the normal user
486 assert_response :success
487 assert_select "div.content-heading" do
488 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
489 assert_select "a[href='/traces/mine']", 1
490 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
491 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
492 assert_select "a[href='/account/edit']", 1
493 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
494 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
495 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
496 assert_select "a[href='/api/0.6/user/#{ERB::Util.u(user.id)}']", 0
499 # Login as a moderator
500 session_for(create(:moderator_user))
502 # Test the normal user
504 assert_response :success
505 assert_select "div.content-heading" do
506 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
507 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
508 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
509 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
510 assert_select "a[href='/account/edit']", 0
511 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
512 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
513 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
514 assert_select "a[href='/api/0.6/user/#{ERB::Util.u(user.id)}']", 1
518 # Test whether information about contributor terms is shown for users who haven't agreed
519 def test_terms_not_agreed
520 agreed_user = create(:user, :terms_agreed => 3.days.ago)
521 seen_user = create(:user, :terms_seen => true, :terms_agreed => nil)
522 not_seen_user = create(:user, :terms_seen => false, :terms_agreed => nil)
524 get user_path(agreed_user)
525 assert_response :success
526 assert_select "div.content-heading" do
527 assert_select "dt", :count => 0, :text => /Contributor terms/
530 get user_path(seen_user)
531 assert_response :success
532 assert_select "div.content-heading" do
533 assert_select "dt", :count => 1, :text => /Contributor terms/
534 assert_select "dd", /Declined/
537 get user_path(not_seen_user)
538 assert_response :success
539 assert_select "div.content-heading" do
540 assert_select "dt", :count => 1, :text => /Contributor terms/
541 assert_select "dd", /Undecided/
548 # Try without logging in
549 post set_status_user_path(user), :params => { :event => "confirm" }
550 assert_response :forbidden
552 # Now try as a normal user
554 post set_status_user_path(user), :params => { :event => "confirm" }
555 assert_response :redirect
556 assert_redirected_to :controller => :errors, :action => :forbidden
558 # Finally try as an administrator
559 session_for(create(:administrator_user))
560 post set_status_user_path(user), :params => { :event => "confirm" }
561 assert_response :redirect
562 assert_redirected_to :action => :show, :display_name => user.display_name
563 assert_equal "confirmed", User.find(user.id).status
567 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
569 # Try without logging in
570 delete user_path(user)
571 assert_response :forbidden
573 # Now try as a normal user
575 delete user_path(user)
576 assert_response :redirect
577 assert_redirected_to :controller => :errors, :action => :forbidden
579 # Finally try as an administrator
580 session_for(create(:administrator_user))
581 delete user_path(user)
582 assert_response :redirect
583 assert_redirected_to :action => :show, :display_name => user.display_name
585 # Check that the user was deleted properly
587 assert_equal "user_#{user.id}", user.display_name
588 assert_equal "", user.description
589 assert_nil user.home_lat
590 assert_nil user.home_lon
591 assert_not user.avatar.attached?
592 assert_not user.email_valid
593 assert_nil user.new_email
594 assert_nil user.auth_provider
595 assert_nil user.auth_uid
596 assert_equal "deleted", user.status
601 moderator_user = create(:moderator_user)
602 administrator_user = create(:administrator_user)
603 _suspended_user = create(:user, :suspended)
604 _ip_user = create(:user, :creation_ip => "1.2.3.4")
606 # There are now 7 users - the five above, plus two extra "granters" for the
607 # moderator_user and administrator_user
608 assert_equal 7, User.count
610 # Shouldn't work when not logged in
612 assert_response :redirect
613 assert_redirected_to login_path(:referer => users_path)
617 # Shouldn't work when logged in as a normal user
619 assert_response :redirect
620 assert_redirected_to :controller => :errors, :action => :forbidden
622 session_for(moderator_user)
624 # Shouldn't work when logged in as a moderator
626 assert_response :redirect
627 assert_redirected_to :controller => :errors, :action => :forbidden
629 session_for(administrator_user)
631 # Note there is a header row, so all row counts are users + 1
632 # Should work when logged in as an administrator
634 assert_response :success
635 assert_template :index
636 assert_select "table#user_list tr", :count => 7 + 1
638 # Should be able to limit by status
639 get users_path, :params => { :status => "suspended" }
640 assert_response :success
641 assert_template :index
642 assert_select "table#user_list tr", :count => 1 + 1
644 # Should be able to limit by IP address
645 get users_path, :params => { :ip => "1.2.3.4" }
646 assert_response :success
647 assert_template :index
648 assert_select "table#user_list tr", :count => 1 + 1
651 def test_index_get_paginated
652 1.upto(100).each do |n|
653 User.create(:display_name => "extra_#{n}",
654 :email => "extra#{n}@example.com",
655 :pass_crypt => "extraextra")
658 session_for(create(:administrator_user))
660 # 100 examples, an administrator, and a granter for the admin.
661 assert_equal 102, User.count
664 assert_response :success
665 assert_template :index
666 assert_select "table#user_list tr", :count => 51
668 get users_path, :params => { :page => 2 }
669 assert_response :success
670 assert_template :index
671 assert_select "table#user_list tr", :count => 51
673 get users_path, :params => { :page => 3 }
674 assert_response :success
675 assert_template :index
676 assert_select "table#user_list tr", :count => 3
679 def test_index_post_confirm
680 inactive_user = create(:user, :pending)
681 suspended_user = create(:user, :suspended)
683 # Shouldn't work when not logged in
684 assert_no_difference "User.active.count" do
685 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
687 assert_response :forbidden
689 assert_equal "pending", inactive_user.reload.status
690 assert_equal "suspended", suspended_user.reload.status
692 session_for(create(:user))
694 # Shouldn't work when logged in as a normal user
695 assert_no_difference "User.active.count" do
696 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
698 assert_response :redirect
699 assert_redirected_to :controller => :errors, :action => :forbidden
700 assert_equal "pending", inactive_user.reload.status
701 assert_equal "suspended", suspended_user.reload.status
703 session_for(create(:moderator_user))
705 # Shouldn't work when logged in as a moderator
706 assert_no_difference "User.active.count" do
707 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
709 assert_response :redirect
710 assert_redirected_to :controller => :errors, :action => :forbidden
711 assert_equal "pending", inactive_user.reload.status
712 assert_equal "suspended", suspended_user.reload.status
714 session_for(create(:administrator_user))
716 # Should work when logged in as an administrator
717 assert_difference "User.active.count", 2 do
718 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
720 assert_response :redirect
721 assert_redirected_to :action => :index
722 assert_equal "confirmed", inactive_user.reload.status
723 assert_equal "confirmed", suspended_user.reload.status
726 def test_index_post_hide
727 normal_user = create(:user)
728 confirmed_user = create(:user, :confirmed)
730 # Shouldn't work when not logged in
731 assert_no_difference "User.active.count" do
732 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
734 assert_response :forbidden
736 assert_equal "active", normal_user.reload.status
737 assert_equal "confirmed", confirmed_user.reload.status
739 session_for(create(:user))
741 # Shouldn't work when logged in as a normal user
742 assert_no_difference "User.active.count" do
743 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
745 assert_response :redirect
746 assert_redirected_to :controller => :errors, :action => :forbidden
747 assert_equal "active", normal_user.reload.status
748 assert_equal "confirmed", confirmed_user.reload.status
750 session_for(create(:moderator_user))
752 # Shouldn't work when logged in as a moderator
753 assert_no_difference "User.active.count" do
754 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
756 assert_response :redirect
757 assert_redirected_to :controller => :errors, :action => :forbidden
758 assert_equal "active", normal_user.reload.status
759 assert_equal "confirmed", confirmed_user.reload.status
761 session_for(create(:administrator_user))
763 # Should work when logged in as an administrator
764 assert_difference "User.active.count", -2 do
765 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
767 assert_response :redirect
768 assert_redirected_to :action => :index
769 assert_equal "deleted", normal_user.reload.status
770 assert_equal "deleted", confirmed_user.reload.status
773 def test_auth_failure_callback
774 get auth_failure_path
775 assert_response :redirect
776 assert_redirected_to login_path
778 get auth_failure_path, :params => { :origin => "/" }
779 assert_response :redirect
780 assert_redirected_to root_path
782 get auth_failure_path, :params => { :origin => "http://www.google.com" }
783 assert_response :redirect
784 assert_redirected_to login_path