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/username/confirm", :method => :get },
29 { :controller => "users", :action => "confirm", :display_name => "username" }
32 { :path => "/user/username/confirm", :method => :post },
33 { :controller => "users", :action => "confirm", :display_name => "username" }
36 { :path => "/user/username/confirm/resend", :method => :get },
37 { :controller => "users", :action => "confirm_resend", :display_name => "username" }
41 { :path => "/user/confirm", :method => :get },
42 { :controller => "users", :action => "confirm" }
45 { :path => "/user/confirm", :method => :post },
46 { :controller => "users", :action => "confirm" }
49 { :path => "/user/confirm-email", :method => :get },
50 { :controller => "users", :action => "confirm_email" }
53 { :path => "/user/confirm-email", :method => :post },
54 { :controller => "users", :action => "confirm_email" }
58 { :path => "/user/go_public", :method => :post },
59 { :controller => "users", :action => "go_public" }
63 { :path => "/user/forgot-password", :method => :get },
64 { :controller => "users", :action => "lost_password" }
67 { :path => "/user/forgot-password", :method => :post },
68 { :controller => "users", :action => "lost_password" }
71 { :path => "/user/reset-password", :method => :get },
72 { :controller => "users", :action => "reset_password" }
75 { :path => "/user/reset-password", :method => :post },
76 { :controller => "users", :action => "reset_password" }
80 { :path => "/user/suspended", :method => :get },
81 { :controller => "users", :action => "suspended" }
85 { :path => "/user/username", :method => :get },
86 { :controller => "users", :action => "show", :display_name => "username" }
90 { :path => "/user/username/account", :method => :get },
91 { :controller => "users", :action => "account", :display_name => "username" }
94 { :path => "/user/username/account", :method => :post },
95 { :controller => "users", :action => "account", :display_name => "username" }
99 { :path => "/user/username/set_status", :method => :post },
100 { :controller => "users", :action => "set_status", :display_name => "username" }
103 { :path => "/user/username", :method => :delete },
104 { :controller => "users", :action => "destroy", :display_name => "username" }
108 { :path => "/users", :method => :get },
109 { :controller => "users", :action => "index" }
112 { :path => "/users", :method => :post },
113 { :controller => "users", :action => "index" }
116 { :path => "/users/status", :method => :get },
117 { :controller => "users", :action => "index", :status => "status" }
120 { :path => "/users/status", :method => :post },
121 { :controller => "users", :action => "index", :status => "status" }
125 # The user creation page loads
128 assert_response :redirect
129 assert_redirected_to user_new_path(:cookie_test => "true")
131 get user_new_path, :params => { :cookie_test => "true" }
132 assert_response :success
134 assert_select "html", :count => 1 do
135 assert_select "head", :count => 1 do
136 assert_select "title", :text => /Sign Up/, :count => 1
138 assert_select "body", :count => 1 do
139 assert_select "div#content", :count => 1 do
140 assert_select "form[action='/user/new'][method='post']", :count => 1 do
141 assert_select "input[id='user_email']", :count => 1
142 assert_select "input[id='user_email_confirmation']", :count => 1
143 assert_select "input[id='user_display_name']", :count => 1
144 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
145 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
146 assert_select "input[type='submit'][value='Sign Up']", :count => 1
153 def test_new_view_logged_in
154 session_for(create(:user))
157 assert_response :redirect
158 assert_redirected_to root_path
160 get user_new_path, :params => { :referer => "/test" }
161 assert_response :redirect
162 assert_redirected_to "/test"
166 user = build(:user, :pending)
168 assert_no_difference "User.count" do
169 assert_no_difference "ActionMailer::Base.deliveries.size" do
170 perform_enqueued_jobs do
171 post user_new_path, :params => { :user => user.attributes }
176 assert_difference "User.count", 1 do
177 assert_difference "ActionMailer::Base.deliveries.size", 1 do
178 perform_enqueued_jobs do
179 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
185 register_email = ActionMailer::Base.deliveries.first
187 assert_equal register_email.to[0], user.email
188 assert_match(/#{@url}/, register_email.body.to_s)
191 assert_redirected_to :action => "confirm", :display_name => user.display_name
193 ActionMailer::Base.deliveries.clear
196 def test_new_duplicate_email
197 user = build(:user, :pending)
198 create(:user, :email => user.email)
200 assert_no_difference "User.count" do
201 assert_no_difference "ActionMailer::Base.deliveries.size" do
202 perform_enqueued_jobs do
203 post user_new_path, :params => { :user => user.attributes }
208 assert_response :success
209 assert_template "new"
210 assert_select "form > div.form-group > input.is-invalid#user_email"
213 def test_save_duplicate_email
214 user = build(:user, :pending)
216 # Set up our user as being half-way through registration
217 assert_no_difference "User.count" do
218 assert_no_difference "ActionMailer::Base.deliveries.size" do
219 perform_enqueued_jobs do
220 post user_new_path, :params => { :user => user.attributes }
225 # Now create another user with that email
226 create(:user, :email => user.email)
228 # Check that the second half of registration fails
229 assert_no_difference "User.count" do
230 assert_no_difference "ActionMailer::Base.deliveries.size" do
231 perform_enqueued_jobs do
232 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
237 assert_response :success
238 assert_template "new"
239 assert_select "form > div.form-group > input.is-invalid#user_email"
242 def test_save_duplicate_email_uppercase
243 user = build(:user, :pending)
245 # Set up our user as being half-way through registration
246 assert_no_difference "User.count" do
247 assert_no_difference "ActionMailer::Base.deliveries.size" do
248 perform_enqueued_jobs do
249 post user_new_path, :params => { :user => user.attributes }
254 # Now create another user with that email, but uppercased
255 create(:user, :email => user.email.upcase)
257 # Check that the second half of registration fails
258 assert_no_difference "User.count" do
259 assert_no_difference "ActionMailer::Base.deliveries.size" do
260 perform_enqueued_jobs do
261 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
266 assert_response :success
267 assert_template "new"
268 assert_select "form > div.form-group > input.is-invalid#user_email"
271 def test_save_duplicate_name
272 user = build(:user, :pending)
274 # Set up our user as being half-way through registration
275 assert_no_difference "User.count" do
276 assert_no_difference "ActionMailer::Base.deliveries.size" do
277 perform_enqueued_jobs do
278 post user_new_path, :params => { :user => user.attributes }
283 # Now create another user with that display name
284 create(:user, :display_name => user.display_name)
286 # Check that the second half of registration fails
287 assert_no_difference "User.count" do
288 assert_no_difference "ActionMailer::Base.deliveries.size" do
289 perform_enqueued_jobs do
290 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
295 assert_response :success
296 assert_template "new"
297 assert_select "form > div.form-group > input.is-invalid#user_display_name"
300 def test_save_duplicate_name_uppercase
301 user = build(:user, :pending)
303 # Set up our user as being half-way through registration
304 assert_no_difference "User.count" do
305 assert_no_difference "ActionMailer::Base.deliveries.size" do
306 perform_enqueued_jobs do
307 post user_new_path, :params => { :user => user.attributes }
312 # Now create another user with that display_name, but uppercased
313 create(:user, :display_name => user.display_name.upcase)
315 # Check that the second half of registration fails
316 assert_no_difference "User.count" do
317 assert_no_difference "ActionMailer::Base.deliveries.size" do
318 perform_enqueued_jobs do
319 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
324 assert_response :success
325 assert_template "new"
326 assert_select "form > div.form-group > input.is-invalid#user_display_name"
329 def test_save_blocked_domain
330 user = build(:user, :pending, :email => "user@example.net")
332 # Set up our user as being half-way through registration
333 assert_no_difference "User.count" do
334 assert_no_difference "ActionMailer::Base.deliveries.size" do
335 perform_enqueued_jobs do
336 post user_new_path, :params => { :user => user.attributes }
341 # Now block that domain
342 create(:acl, :domain => "example.net", :k => "no_account_creation")
344 # Check that the second half of registration fails
345 assert_no_difference "User.count" do
346 assert_no_difference "ActionMailer::Base.deliveries.size" do
347 perform_enqueued_jobs do
348 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
353 assert_response :success
354 assert_template "blocked"
357 def test_save_referer_params
358 user = build(:user, :pending)
360 # Set up our user as being half-way through registration
361 assert_no_difference "User.count" do
362 assert_no_difference "ActionMailer::Base.deliveries.size" do
363 perform_enqueued_jobs do
364 post user_new_path, :params => { :user => user.attributes, :referer => "/edit?editor=id#map=1/2/3" }
369 assert_difference "User.count", 1 do
370 assert_difference "ActionMailer::Base.deliveries.size", 1 do
371 perform_enqueued_jobs do
372 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
377 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
378 User.find_by(:email => user.email).tokens.order("id DESC").first.referer
380 ActionMailer::Base.deliveries.clear
384 user = build(:user, :pending)
385 post user_new_path, :params => { :user => user.attributes }
386 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
387 confirm_string = User.find_by(:email => user.email).tokens.create.token
389 get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
390 assert_response :success
391 assert_template :confirm
394 def test_confirm_get_already_confirmed
395 user = build(:user, :pending)
396 stub_gravatar_request(user.email)
397 post user_new_path, :params => { :user => user.attributes }
398 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
399 confirm_string = User.find_by(:email => user.email).tokens.create.token
401 # Get the confirmation page
402 get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
403 assert_response :success
404 assert_template :confirm
407 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
408 assert_redirected_to welcome_path
410 # Now try to get the confirmation page again
411 get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
412 assert_response :redirect
413 assert_redirected_to root_path
416 def test_confirm_success_no_token_no_referer
417 user = build(:user, :pending)
418 stub_gravatar_request(user.email)
419 post user_new_path, :params => { :user => user.attributes }
420 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
421 confirm_string = User.find_by(:email => user.email).tokens.create.token
425 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
426 assert_redirected_to login_path
427 assert_match(/Confirmed your account/, flash[:notice])
430 def test_confirm_success_good_token_no_referer
431 user = build(:user, :pending)
432 stub_gravatar_request(user.email)
433 post user_new_path, :params => { :user => user.attributes }
434 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
435 confirm_string = User.find_by(:email => user.email).tokens.create.token
437 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
438 assert_redirected_to welcome_path
441 def test_confirm_success_bad_token_no_referer
442 user = build(:user, :pending)
443 stub_gravatar_request(user.email)
444 post user_new_path, :params => { :user => user.attributes }
445 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
446 confirm_string = User.find_by(:email => user.email).tokens.create.token
449 session_for(create(:user))
451 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
452 assert_redirected_to login_path
453 assert_match(/Confirmed your account/, flash[:notice])
456 def test_confirm_success_no_token_with_referer
457 user = build(:user, :pending)
458 stub_gravatar_request(user.email)
459 post user_new_path, :params => { :user => user.attributes }
460 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
461 confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token
465 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
466 assert_redirected_to login_path(:referer => new_diary_entry_path)
467 assert_match(/Confirmed your account/, flash[:notice])
470 def test_confirm_success_good_token_with_referer
471 user = build(:user, :pending)
472 stub_gravatar_request(user.email)
473 post user_new_path, :params => { :user => user.attributes }
474 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
475 confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token
477 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
478 assert_redirected_to new_diary_entry_path
481 def test_confirm_success_bad_token_with_referer
482 user = build(:user, :pending)
483 stub_gravatar_request(user.email)
484 post user_new_path, :params => { :user => user.attributes }
485 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
486 confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token
489 session_for(create(:user))
491 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
492 assert_redirected_to login_path(:referer => new_diary_entry_path)
493 assert_match(/Confirmed your account/, flash[:notice])
496 def test_confirm_expired_token
497 user = build(:user, :pending)
498 stub_gravatar_request(user.email)
499 post user_new_path, :params => { :user => user.attributes }
500 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
501 confirm_string = User.find_by(:email => user.email).tokens.create(:expiry => 1.day.ago).token
503 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
504 assert_redirected_to :action => "confirm"
505 assert_match(/confirmation code has expired/, flash[:error])
508 def test_confirm_already_confirmed
509 user = build(:user, :pending)
510 stub_gravatar_request(user.email)
511 post user_new_path, :params => { :user => user.attributes }
512 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
513 confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token
515 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
516 assert_redirected_to new_diary_entry_path
520 confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token
521 post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
522 assert_redirected_to login_path
523 assert_match(/already been confirmed/, flash[:error])
526 def test_confirm_resend_success
527 user = build(:user, :pending)
528 post user_new_path, :params => { :user => user.attributes }
529 post user_save_path, :params => { :read_ct => 1, :read_tou => 1 }
531 assert_difference "ActionMailer::Base.deliveries.size", 1 do
532 perform_enqueued_jobs do
533 get user_confirm_resend_path(user)
537 assert_response :redirect
538 assert_redirected_to login_path
539 assert_match(/sent a new confirmation/, flash[:notice])
541 email = ActionMailer::Base.deliveries.last
543 assert_equal user.email, email.to.first
545 ActionMailer::Base.deliveries.clear
548 def test_confirm_resend_no_token
549 user = build(:user, :pending)
550 # only complete first half of registration
551 post user_new_path, :params => { :user => user.attributes }
553 assert_no_difference "ActionMailer::Base.deliveries.size" do
554 perform_enqueued_jobs do
555 get user_confirm_resend_path(user)
559 assert_response :redirect
560 assert_redirected_to login_path
561 assert_match "User #{user.display_name} not found.", flash[:error]
564 def test_confirm_resend_unknown_user
565 assert_no_difference "ActionMailer::Base.deliveries.size" do
566 perform_enqueued_jobs do
567 get user_confirm_resend_path(:display_name => "No Such User")
571 assert_response :redirect
572 assert_redirected_to login_path
573 assert_match "User No Such User not found.", flash[:error]
576 def test_confirm_email_get
578 confirm_string = user.tokens.create.token
580 get user_confirm_email_path, :params => { :confirm_string => confirm_string }
581 assert_response :success
582 assert_template :confirm_email
585 def test_confirm_email_success
586 user = create(:user, :new_email => "test-new@example.com")
587 stub_gravatar_request(user.new_email)
588 confirm_string = user.tokens.create.token
590 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
591 assert_response :redirect
592 assert_redirected_to :action => :account, :display_name => user.display_name
593 assert_match(/Confirmed your change of email address/, flash[:notice])
596 def test_confirm_email_already_confirmed
598 confirm_string = user.tokens.create.token
600 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
601 assert_response :redirect
602 assert_redirected_to :action => :account, :display_name => user.display_name
603 assert_match(/already been confirmed/, flash[:error])
606 def test_confirm_email_bad_token
607 post user_confirm_email_path, :params => { :confirm_string => "XXXXX" }
608 assert_response :success
609 assert_template :confirm_email
610 assert_match(/confirmation code has expired or does not exist/, flash[:error])
614 # test if testing for a gravatar works
615 # this happens when the email is actually changed
616 # which is triggered by the confirmation mail
617 def test_gravatar_auto_enable
618 # switch to email that has a gravatar
619 user = create(:user, :new_email => "test-new@example.com")
620 stub_gravatar_request(user.new_email, 200)
621 confirm_string = user.tokens.create.token
622 # precondition gravatar should be turned off
623 assert_not user.image_use_gravatar
624 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
625 assert_response :redirect
626 assert_redirected_to :action => :account, :display_name => user.display_name
627 assert_match(/Confirmed your change of email address/, flash[:notice])
628 # gravatar use should now be enabled
629 assert User.find(user.id).image_use_gravatar
632 def test_gravatar_auto_disable
633 # switch to email without a gravatar
634 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
635 stub_gravatar_request(user.new_email, 404)
636 confirm_string = user.tokens.create.token
637 # precondition gravatar should be turned on
638 assert user.image_use_gravatar
639 post user_confirm_email_path, :params => { :confirm_string => confirm_string }
640 assert_response :redirect
641 assert_redirected_to :action => :account, :display_name => user.display_name
642 assert_match(/Confirmed your change of email address/, flash[:notice])
643 # gravatar use should now be disabled
644 assert_not User.find(user.id).image_use_gravatar
647 def test_terms_new_user
648 user = build(:user, :pending)
650 # Set up our user as being half-way through registration
651 assert_no_difference "User.count" do
652 assert_no_difference "ActionMailer::Base.deliveries.size" do
653 perform_enqueued_jobs do
654 post user_new_path, :params => { :user => user.attributes }
661 assert_response :success
662 assert_template :terms
665 def test_terms_agreed
666 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
671 assert_response :redirect
672 assert_redirected_to :action => :account, :display_name => user.display_name
675 def test_terms_not_seen_without_referer
676 user = create(:user, :terms_seen => false, :terms_agreed => nil)
681 assert_response :success
682 assert_template :terms
684 post user_save_path, :params => { :user => { :consider_pd => true }, :read_ct => 1, :read_tou => 1 }
685 assert_response :redirect
686 assert_redirected_to :action => :account, :display_name => user.display_name
687 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
691 assert user.consider_pd
692 assert_not_nil user.terms_agreed
693 assert user.terms_seen
696 def test_terms_not_seen_with_referer
697 user = create(:user, :terms_seen => false, :terms_agreed => nil)
701 get user_terms_path, :params => { :referer => "/test" }
702 assert_response :success
703 assert_template :terms
705 post user_save_path, :params => { :user => { :consider_pd => true }, :referer => "/test", :read_ct => 1, :read_tou => 1 }
706 assert_response :redirect
707 assert_redirected_to "/test"
708 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
712 assert user.consider_pd
713 assert_not_nil user.terms_agreed
714 assert user.terms_seen
717 # Check that if you haven't seen the terms, and make a request that requires authentication,
718 # that your request is redirected to view the terms
719 def test_terms_not_seen_redirection
720 user = create(:user, :terms_seen => false, :terms_agreed => nil)
723 get user_account_path(user)
724 assert_response :redirect
725 assert_redirected_to :action => :terms, :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
729 user = create(:user, :data_public => false)
732 post user_go_public_path
734 assert_response :redirect
735 assert_redirected_to :action => :account, :display_name => user.display_name
736 assert User.find(user.id).data_public
739 def test_lost_password
740 # Test fetching the lost password page
741 get user_forgot_password_path
742 assert_response :success
743 assert_template :lost_password
744 assert_select "div#notice", false
746 # Test resetting using the address as recorded for a user that has an
747 # address which is duplicated in a different case by another user
749 uppercase_user = build(:user, :email => user.email.upcase).tap { |u| u.save(:validate => false) }
751 # Resetting with GET should fail
752 assert_no_difference "ActionMailer::Base.deliveries.size" do
753 perform_enqueued_jobs do
754 get user_forgot_password_path, :params => { :email => user.email }
757 assert_response :success
758 assert_template :lost_password
760 # Resetting with POST should work
761 assert_difference "ActionMailer::Base.deliveries.size", 1 do
762 perform_enqueued_jobs do
763 post user_forgot_password_path, :params => { :email => user.email }
766 assert_response :redirect
767 assert_redirected_to login_path
768 assert_match(/^Sorry you lost it/, flash[:notice])
769 email = ActionMailer::Base.deliveries.first
770 assert_equal 1, email.to.count
771 assert_equal user.email, email.to.first
772 ActionMailer::Base.deliveries.clear
774 # Test resetting using an address that matches a different user
775 # that has the same address in a different case
776 assert_difference "ActionMailer::Base.deliveries.size", 1 do
777 perform_enqueued_jobs do
778 post user_forgot_password_path, :params => { :email => user.email.upcase }
781 assert_response :redirect
782 assert_redirected_to login_path
783 assert_match(/^Sorry you lost it/, flash[:notice])
784 email = ActionMailer::Base.deliveries.first
785 assert_equal 1, email.to.count
786 assert_equal uppercase_user.email, email.to.first
787 ActionMailer::Base.deliveries.clear
789 # Test resetting using an address that is a case insensitive match
790 # for more than one user but not an exact match for either
791 assert_no_difference "ActionMailer::Base.deliveries.size" do
792 perform_enqueued_jobs do
793 post user_forgot_password_path, :params => { :email => user.email.titlecase }
796 assert_response :success
797 assert_template :lost_password
798 assert_select ".error", /^Could not find that email address/
800 # Test resetting using the address as recorded for a user that has an
801 # address which is case insensitively unique
802 third_user = create(:user)
803 assert_difference "ActionMailer::Base.deliveries.size", 1 do
804 perform_enqueued_jobs do
805 post user_forgot_password_path, :params => { :email => third_user.email }
808 assert_response :redirect
809 assert_redirected_to login_path
810 assert_match(/^Sorry you lost it/, flash[:notice])
811 email = ActionMailer::Base.deliveries.first
812 assert_equal 1, email.to.count
813 assert_equal third_user.email, email.to.first
814 ActionMailer::Base.deliveries.clear
816 # Test resetting using an address that matches a user that has the
817 # same (case insensitively unique) address in a different case
818 assert_difference "ActionMailer::Base.deliveries.size", 1 do
819 perform_enqueued_jobs do
820 post user_forgot_password_path, :params => { :email => third_user.email.upcase }
823 assert_response :redirect
824 assert_redirected_to login_path
825 assert_match(/^Sorry you lost it/, flash[:notice])
826 email = ActionMailer::Base.deliveries.first
827 assert_equal 1, email.to.count
828 assert_equal third_user.email, email.to.first
829 ActionMailer::Base.deliveries.clear
832 def test_reset_password
833 user = create(:user, :pending)
834 # Test a request with no token
835 get user_reset_password_path
836 assert_response :bad_request
838 # Test a request with a bogus token
839 get user_reset_password_path, :params => { :token => "made_up_token" }
840 assert_response :redirect
841 assert_redirected_to :action => :lost_password
843 # Create a valid token for a user
844 token = user.tokens.create
846 # Test a request with a valid token
847 get user_reset_password_path, :params => { :token => token.token }
848 assert_response :success
849 assert_template :reset_password
851 # Test that errors are reported for erroneous submissions
852 post user_reset_password_path, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "different_password" } }
853 assert_response :success
854 assert_template :reset_password
855 assert_select "div.invalid-feedback"
857 # Test setting a new password
858 post user_reset_password_path, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" } }
859 assert_response :redirect
860 assert_redirected_to root_path
861 assert_equal user.id, session[:user]
863 assert_equal "active", user.status
864 assert user.email_valid
865 assert_equal user, User.authenticate(:username => user.email, :password => "new_password")
869 # Get a user to work with - note that this user deliberately
870 # conflicts with uppercase_user in the email and display name
871 # fields to test that we can change other fields without any
872 # validation errors being reported
873 user = create(:user, :languages => [])
874 _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
876 # Make sure that you are redirected to the login page when
877 # you are not logged in
878 get user_account_path(user)
879 assert_response :redirect
880 assert_redirected_to login_path(:referer => "/user/#{ERB::Util.u(user.display_name)}/account")
882 # Make sure that you are blocked when not logged in as the right user
883 session_for(create(:user))
884 get user_account_path(user)
885 assert_response :forbidden
887 # Make sure we get the page when we are logged in as the right user
889 get user_account_path(user)
890 assert_response :success
891 assert_template :account
892 assert_select "form#accountForm" do |form|
893 assert_equal "post", form.attr("method").to_s
894 assert_select "input[name='_method']", false
895 assert_equal "/user/#{ERB::Util.u(user.display_name)}/account", form.attr("action").to_s
898 # Updating the description using GET should fail
899 user.description = "new description"
900 user.preferred_editor = "default"
901 get user_account_path(user), :params => { :user => user.attributes }
902 assert_response :success
903 assert_template :account
904 assert_not_equal user.description, User.find(user.id).description
906 # Updating the description should work
907 user.description = "new description"
908 user.preferred_editor = "default"
909 post user_account_path(user), :params => { :user => user.attributes }
910 assert_response :redirect
911 assert_redirected_to user_account_url(user)
912 get user_account_path(user)
913 assert_response :success
914 assert_template :account
915 assert_select ".notice", /^User information updated successfully/
916 assert_select "form#accountForm > div.form-group > div#user_description_container > div#user_description_content > textarea#user_description", user.description
918 # Changing to a invalid editor should fail
919 user.preferred_editor = "unknown"
920 post user_account_path(user), :params => { :user => user.attributes }
921 assert_response :success
922 assert_template :account
923 assert_select ".notice", false
924 assert_select "form#accountForm > div.form-group > select#user_preferred_editor > option[selected]", false
926 # Changing to a valid editor should work
927 user.preferred_editor = "id"
928 post user_account_path(user), :params => { :user => user.attributes }
929 assert_response :redirect
930 assert_redirected_to user_account_url(user)
931 get user_account_path(user)
932 assert_response :success
933 assert_template :account
934 assert_select ".notice", /^User information updated successfully/
935 assert_select "form#accountForm > div.form-group > select#user_preferred_editor > option[selected][value=?]", "id"
937 # Changing to the default editor should work
938 user.preferred_editor = "default"
939 post user_account_path(user), :params => { :user => user.attributes }
940 assert_response :redirect
941 assert_redirected_to user_account_url(user)
942 get user_account_path(user)
943 assert_response :success
944 assert_template :account
945 assert_select ".notice", /^User information updated successfully/
946 assert_select "form#accountForm > div.form-group > select#user_preferred_editor > option[selected]", false
948 # Changing to an uploaded image should work
949 image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif")
950 post user_account_path(user), :params => { :avatar_action => "new", :user => user.attributes.merge(:avatar => image) }
951 assert_response :redirect
952 assert_redirected_to user_account_url(user)
953 get user_account_path(user)
954 assert_response :success
955 assert_template :account
956 assert_select ".notice", /^User information updated successfully/
957 assert_select "form#accountForm > fieldset.form-group > div.form-row > div.col-sm-10 > div.form-check > input[name=avatar_action][checked][value=?]", "keep"
959 # Changing to a gravatar image should work
960 post user_account_path(user), :params => { :avatar_action => "gravatar", :user => user.attributes }
961 assert_response :redirect
962 assert_redirected_to user_account_url(user)
963 get user_account_path(user)
964 assert_response :success
965 assert_template :account
966 assert_select ".notice", /^User information updated successfully/
967 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"
969 # Removing the image should work
970 post user_account_path(user), :params => { :avatar_action => "delete", :user => user.attributes }
971 assert_response :redirect
972 assert_redirected_to user_account_url(user)
973 get user_account_path(user)
974 assert_response :success
975 assert_template :account
976 assert_select ".notice", /^User information updated successfully/
977 assert_select "form#accountForm > fieldset.form-group > div.form-row > div.col-sm-10 > div.form-check > input[name=avatar_action][checked]", false
978 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
980 # Adding external authentication should redirect to the auth provider
981 post user_account_path(user), :params => { :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }
982 assert_response :redirect
983 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")
985 # Changing name to one that exists should fail
986 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
987 post user_account_path(user), :params => { :user => new_attributes }
988 assert_response :success
989 assert_template :account
990 assert_select ".notice", false
991 assert_select "form#accountForm > div.form-group > input.is-invalid#user_display_name"
993 # Changing name to one that exists should fail, regardless of case
994 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
995 post user_account_path(user), :params => { :user => new_attributes }
996 assert_response :success
997 assert_template :account
998 assert_select ".notice", false
999 assert_select "form#accountForm > div.form-group > input.is-invalid#user_display_name"
1001 # Changing name to one that doesn't exist should work
1002 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
1003 post user_account_path(user), :params => { :user => new_attributes }
1004 assert_response :redirect
1005 assert_redirected_to user_account_url(:display_name => "new tester")
1006 get user_account_path(:display_name => "new tester")
1007 assert_response :success
1008 assert_template :account
1009 assert_select ".notice", /^User information updated successfully/
1010 assert_select "form#accountForm > div.form-group > input#user_display_name[value=?]", "new tester"
1012 # Record the change of name
1013 user.display_name = "new tester"
1015 # Changing email to one that exists should fail
1016 user.new_email = create(:user).email
1017 assert_no_difference "ActionMailer::Base.deliveries.size" do
1018 perform_enqueued_jobs do
1019 post user_account_path(user), :params => { :user => user.attributes }
1022 assert_response :success
1023 assert_template :account
1024 assert_select ".notice", false
1025 assert_select "form#accountForm > div.form-group > input.is-invalid#user_new_email"
1027 # Changing email to one that exists should fail, regardless of case
1028 user.new_email = create(:user).email.upcase
1029 assert_no_difference "ActionMailer::Base.deliveries.size" do
1030 perform_enqueued_jobs do
1031 post user_account_path(user), :params => { :user => user.attributes }
1034 assert_response :success
1035 assert_template :account
1036 assert_select ".notice", false
1037 assert_select "form#accountForm > div.form-group > input.is-invalid#user_new_email"
1039 # Changing email to one that doesn't exist should work
1040 user.new_email = "new_tester@example.com"
1041 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1042 perform_enqueued_jobs do
1043 post user_account_path(user), :params => { :user => user.attributes }
1046 assert_response :redirect
1047 assert_redirected_to user_account_url(user)
1048 get user_account_path(user)
1049 assert_response :success
1050 assert_template :account
1051 assert_select ".notice", /^User information updated successfully/
1052 assert_select "form#accountForm > div.form-group > input#user_new_email[value=?]", user.new_email
1053 email = ActionMailer::Base.deliveries.first
1054 assert_equal 1, email.to.count
1055 assert_equal user.new_email, email.to.first
1056 ActionMailer::Base.deliveries.clear
1059 # Check that the user account page will display and contains some relevant
1060 # information for the user
1062 # Test a non-existent user
1063 get user_path(:display_name => "unknown")
1064 assert_response :not_found
1066 # Test a normal user
1067 user = create(:user, :home_lon => 1.1, :home_lat => 1.1)
1068 friend_user = create(:user, :home_lon => 1.2, :home_lat => 1.2)
1069 create(:friendship, :befriender => user, :befriendee => friend_user)
1070 create(:changeset, :user => friend_user)
1073 assert_response :success
1074 assert_select "div#userinformation" do
1075 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1076 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1077 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1078 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1079 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1080 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1081 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1082 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1085 # Friends shouldn't be visible as we're not logged in
1086 assert_select "div#friends-container", :count => 0
1088 # Test a user who has been blocked
1089 blocked_user = create(:user)
1090 create(:user_block, :user => blocked_user)
1091 get user_path(blocked_user)
1092 assert_response :success
1093 assert_select "div#userinformation" do
1094 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
1095 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
1096 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
1097 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
1098 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
1099 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
1100 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
1101 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
1104 # Test a moderator who has applied blocks
1105 moderator_user = create(:moderator_user)
1106 create(:user_block, :creator => moderator_user)
1107 get user_path(moderator_user)
1108 assert_response :success
1109 assert_select "div#userinformation" do
1110 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
1111 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
1112 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
1113 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
1114 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
1115 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
1116 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
1117 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
1120 # Login as a normal user
1123 # Test the normal user
1125 assert_response :success
1126 assert_select "div#userinformation" do
1127 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1128 assert_select "a[href='/traces/mine']", 1
1129 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1130 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1131 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 1
1132 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1133 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1134 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1137 # Friends should be visible as we're now logged in
1138 assert_select "div#friends-container" do
1139 assert_select "div.contact-activity", :count => 1
1142 # Login as a moderator
1143 session_for(create(:moderator_user))
1145 # Test the normal user
1147 assert_response :success
1148 assert_select "div#userinformation" do
1149 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1150 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1151 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1152 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1153 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1154 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1155 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1156 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
1160 # Test whether information about contributor terms is shown for users who haven't agreed
1161 def test_terms_not_agreed
1162 agreed_user = create(:user, :terms_agreed => 3.days.ago)
1163 seen_user = create(:user, :terms_seen => true, :terms_agreed => nil)
1164 not_seen_user = create(:user, :terms_seen => false, :terms_agreed => nil)
1166 get user_path(agreed_user)
1167 assert_response :success
1168 assert_select "div#userinformation" do
1169 assert_select "p", :count => 0, :text => /Contributor terms/
1172 get user_path(seen_user)
1173 assert_response :success
1174 # put @response.body
1175 assert_select "div#userinformation" do
1176 assert_select "p", :count => 1, :text => /Contributor terms/
1177 assert_select "p", /Declined/
1180 get user_path(not_seen_user)
1181 assert_response :success
1182 assert_select "div#userinformation" do
1183 assert_select "p", :count => 1, :text => /Contributor terms/
1184 assert_select "p", /Undecided/
1189 user = create(:user)
1191 # Try without logging in
1192 post set_status_user_path(user), :params => { :status => "suspended" }
1193 assert_response :forbidden
1195 # Now try as a normal user
1197 post set_status_user_path(user), :params => { :status => "suspended" }
1198 assert_response :redirect
1199 assert_redirected_to :controller => :errors, :action => :forbidden
1201 # Finally try as an administrator
1202 session_for(create(:administrator_user))
1203 post set_status_user_path(user), :params => { :status => "suspended" }
1204 assert_response :redirect
1205 assert_redirected_to :action => :show, :display_name => user.display_name
1206 assert_equal "suspended", User.find(user.id).status
1210 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1212 # Try without logging in
1213 delete user_path(user), :params => { :status => "suspended" }
1214 assert_response :forbidden
1216 # Now try as a normal user
1218 delete user_path(user), :params => { :status => "suspended" }
1219 assert_response :redirect
1220 assert_redirected_to :controller => :errors, :action => :forbidden
1222 # Finally try as an administrator
1223 session_for(create(:administrator_user))
1224 delete user_path(user), :params => { :status => "suspended" }
1225 assert_response :redirect
1226 assert_redirected_to :action => :show, :display_name => user.display_name
1228 # Check that the user was deleted properly
1230 assert_equal "user_#{user.id}", user.display_name
1231 assert_equal "", user.description
1232 assert_nil user.home_lat
1233 assert_nil user.home_lon
1234 assert_not user.avatar.attached?
1235 assert_not user.email_valid
1236 assert_nil user.new_email
1237 assert_nil user.auth_provider
1238 assert_nil user.auth_uid
1239 assert_equal "deleted", user.status
1243 user = create(:user)
1244 moderator_user = create(:moderator_user)
1245 administrator_user = create(:administrator_user)
1246 _suspended_user = create(:user, :suspended)
1247 _ip_user = create(:user, :creation_ip => "1.2.3.4")
1249 # There are now 7 users - the five above, plus two extra "granters" for the
1250 # moderator_user and administrator_user
1251 assert_equal 7, User.count
1253 # Shouldn't work when not logged in
1255 assert_response :redirect
1256 assert_redirected_to login_path(:referer => users_path)
1260 # Shouldn't work when logged in as a normal user
1262 assert_response :redirect
1263 assert_redirected_to :controller => :errors, :action => :forbidden
1265 session_for(moderator_user)
1267 # Shouldn't work when logged in as a moderator
1269 assert_response :redirect
1270 assert_redirected_to :controller => :errors, :action => :forbidden
1272 session_for(administrator_user)
1274 # Note there is a header row, so all row counts are users + 1
1275 # Should work when logged in as an administrator
1277 assert_response :success
1278 assert_template :index
1279 assert_select "table#user_list tr", :count => 7 + 1
1281 # Should be able to limit by status
1282 get users_path, :params => { :status => "suspended" }
1283 assert_response :success
1284 assert_template :index
1285 assert_select "table#user_list tr", :count => 1 + 1
1287 # Should be able to limit by IP address
1288 get users_path, :params => { :ip => "1.2.3.4" }
1289 assert_response :success
1290 assert_template :index
1291 assert_select "table#user_list tr", :count => 1 + 1
1294 def test_index_get_paginated
1295 1.upto(100).each do |n|
1296 User.create(:display_name => "extra_#{n}",
1297 :email => "extra#{n}@example.com",
1298 :pass_crypt => "extraextra")
1301 session_for(create(:administrator_user))
1303 # 100 examples, an administrator, and a granter for the admin.
1304 assert_equal 102, User.count
1307 assert_response :success
1308 assert_template :index
1309 assert_select "table#user_list tr", :count => 51
1311 get users_path, :params => { :page => 2 }
1312 assert_response :success
1313 assert_template :index
1314 assert_select "table#user_list tr", :count => 51
1316 get users_path, :params => { :page => 3 }
1317 assert_response :success
1318 assert_template :index
1319 assert_select "table#user_list tr", :count => 3
1322 def test_index_post_confirm
1323 inactive_user = create(:user, :pending)
1324 suspended_user = create(:user, :suspended)
1326 # Shouldn't work when not logged in
1327 assert_no_difference "User.active.count" do
1328 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1330 assert_response :forbidden
1332 assert_equal "pending", inactive_user.reload.status
1333 assert_equal "suspended", suspended_user.reload.status
1335 session_for(create(:user))
1337 # Shouldn't work when logged in as a normal user
1338 assert_no_difference "User.active.count" do
1339 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1341 assert_response :redirect
1342 assert_redirected_to :controller => :errors, :action => :forbidden
1343 assert_equal "pending", inactive_user.reload.status
1344 assert_equal "suspended", suspended_user.reload.status
1346 session_for(create(:moderator_user))
1348 # Shouldn't work when logged in as a moderator
1349 assert_no_difference "User.active.count" do
1350 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1352 assert_response :redirect
1353 assert_redirected_to :controller => :errors, :action => :forbidden
1354 assert_equal "pending", inactive_user.reload.status
1355 assert_equal "suspended", suspended_user.reload.status
1357 session_for(create(:administrator_user))
1359 # Should work when logged in as an administrator
1360 assert_difference "User.active.count", 2 do
1361 post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1363 assert_response :redirect
1364 assert_redirected_to :action => :index
1365 assert_equal "confirmed", inactive_user.reload.status
1366 assert_equal "confirmed", suspended_user.reload.status
1369 def test_index_post_hide
1370 normal_user = create(:user)
1371 confirmed_user = create(:user, :confirmed)
1373 # Shouldn't work when not logged in
1374 assert_no_difference "User.active.count" do
1375 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1377 assert_response :forbidden
1379 assert_equal "active", normal_user.reload.status
1380 assert_equal "confirmed", confirmed_user.reload.status
1382 session_for(create(:user))
1384 # Shouldn't work when logged in as a normal user
1385 assert_no_difference "User.active.count" do
1386 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1388 assert_response :redirect
1389 assert_redirected_to :controller => :errors, :action => :forbidden
1390 assert_equal "active", normal_user.reload.status
1391 assert_equal "confirmed", confirmed_user.reload.status
1393 session_for(create(:moderator_user))
1395 # Shouldn't work when logged in as a moderator
1396 assert_no_difference "User.active.count" do
1397 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1399 assert_response :redirect
1400 assert_redirected_to :controller => :errors, :action => :forbidden
1401 assert_equal "active", normal_user.reload.status
1402 assert_equal "confirmed", confirmed_user.reload.status
1404 session_for(create(:administrator_user))
1406 # Should work when logged in as an administrator
1407 assert_difference "User.active.count", -2 do
1408 post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1410 assert_response :redirect
1411 assert_redirected_to :action => :index
1412 assert_equal "deleted", normal_user.reload.status
1413 assert_equal "deleted", confirmed_user.reload.status