3 class UserControllerTest < ActionController::TestCase
9 # test all routes which lead to this controller
12 { :path => "/api/0.6/user/1", :method => :get },
13 { :controller => "user", :action => "api_read", :id => "1" }
16 { :path => "/api/0.6/user/details", :method => :get },
17 { :controller => "user", :action => "api_details" }
20 { :path => "/api/0.6/user/gpx_files", :method => :get },
21 { :controller => "user", :action => "api_gpx_files" }
25 { :path => "/login", :method => :get },
26 { :controller => "user", :action => "login" }
29 { :path => "/login", :method => :post },
30 { :controller => "user", :action => "login" }
33 { :controller => "user", :action => "login", :format => "html" },
34 { :path => "/login.html", :method => :get }
38 { :path => "/logout", :method => :get },
39 { :controller => "user", :action => "logout" }
42 { :path => "/logout", :method => :post },
43 { :controller => "user", :action => "logout" }
46 { :controller => "user", :action => "logout", :format => "html" },
47 { :path => "/logout.html", :method => :get }
51 { :path => "/user/new", :method => :get },
52 { :controller => "user", :action => "new" }
56 { :path => "/user/new", :method => :post },
57 { :controller => "user", :action => "create" }
61 { :path => "/user/terms", :method => :get },
62 { :controller => "user", :action => "terms" }
66 { :path => "/user/save", :method => :post },
67 { :controller => "user", :action => "save" }
71 { :path => "/user/username/confirm", :method => :get },
72 { :controller => "user", :action => "confirm", :display_name => "username" }
75 { :path => "/user/username/confirm", :method => :post },
76 { :controller => "user", :action => "confirm", :display_name => "username" }
79 { :path => "/user/username/confirm/resend", :method => :get },
80 { :controller => "user", :action => "confirm_resend", :display_name => "username" }
84 { :path => "/user/confirm", :method => :get },
85 { :controller => "user", :action => "confirm" }
88 { :path => "/user/confirm", :method => :post },
89 { :controller => "user", :action => "confirm" }
92 { :path => "/user/confirm-email", :method => :get },
93 { :controller => "user", :action => "confirm_email" }
96 { :path => "/user/confirm-email", :method => :post },
97 { :controller => "user", :action => "confirm_email" }
101 { :path => "/user/go_public", :method => :post },
102 { :controller => "user", :action => "go_public" }
106 { :path => "/user/forgot-password", :method => :get },
107 { :controller => "user", :action => "lost_password" }
110 { :path => "/user/forgot-password", :method => :post },
111 { :controller => "user", :action => "lost_password" }
114 { :path => "/user/reset-password", :method => :get },
115 { :controller => "user", :action => "reset_password" }
118 { :path => "/user/reset-password", :method => :post },
119 { :controller => "user", :action => "reset_password" }
123 { :path => "/user/suspended", :method => :get },
124 { :controller => "user", :action => "suspended" }
128 { :path => "/user/username", :method => :get },
129 { :controller => "user", :action => "view", :display_name => "username" }
133 { :path => "/user/username/account", :method => :get },
134 { :controller => "user", :action => "account", :display_name => "username" }
137 { :path => "/user/username/account", :method => :post },
138 { :controller => "user", :action => "account", :display_name => "username" }
142 { :path => "/user/username/make_friend", :method => :get },
143 { :controller => "user", :action => "make_friend", :display_name => "username" }
146 { :path => "/user/username/make_friend", :method => :post },
147 { :controller => "user", :action => "make_friend", :display_name => "username" }
150 { :path => "/user/username/remove_friend", :method => :get },
151 { :controller => "user", :action => "remove_friend", :display_name => "username" }
154 { :path => "/user/username/remove_friend", :method => :post },
155 { :controller => "user", :action => "remove_friend", :display_name => "username" }
159 { :path => "/user/username/set_status", :method => :get },
160 { :controller => "user", :action => "set_status", :display_name => "username" }
163 { :path => "/user/username/delete", :method => :get },
164 { :controller => "user", :action => "delete", :display_name => "username" }
168 { :path => "/users", :method => :get },
169 { :controller => "user", :action => "list" }
172 { :path => "/users", :method => :post },
173 { :controller => "user", :action => "list" }
176 { :path => "/users/status", :method => :get },
177 { :controller => "user", :action => "list", :status => "status" }
180 { :path => "/users/status", :method => :post },
181 { :controller => "user", :action => "list", :status => "status" }
185 # The user creation page loads
188 assert_response :redirect
189 assert_redirected_to user_new_path(:cookie_test => "true")
191 get :new, :params => { :cookie_test => "true" }, :session => { :cookie_test => true }
192 assert_response :success
194 assert_select "html", :count => 1 do
195 assert_select "head", :count => 1 do
196 assert_select "title", :text => /Sign Up/, :count => 1
198 assert_select "body", :count => 1 do
199 assert_select "div#content", :count => 1 do
200 assert_select "form[action='/user/new'][method='post']", :count => 1 do
201 assert_select "input[id='user_email']", :count => 1
202 assert_select "input[id='user_email_confirmation']", :count => 1
203 assert_select "input[id='user_display_name']", :count => 1
204 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
205 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
206 assert_select "input[type='submit'][value='Sign Up']", :count => 1
213 def test_new_view_logged_in
214 session[:user] = create(:user).id
217 assert_response :redirect
218 assert_redirected_to user_new_path(:cookie_test => "true")
219 get :new, :params => { :cookie_test => "true" }
220 assert_response :redirect
221 assert_redirected_to root_path
223 get :new, :params => { :referer => "/test" }
224 assert_response :redirect
225 assert_redirected_to user_new_path(:referer => "/test", :cookie_test => "true")
226 get :new, :params => { :referer => "/test", :cookie_test => "true" }
227 assert_response :redirect
228 assert_redirected_to "/test"
232 user = build(:user, :pending)
234 assert_difference "User.count", 1 do
235 assert_difference "ActionMailer::Base.deliveries.size", 1 do
236 post :save, :session => { :new_user => user }
241 register_email = ActionMailer::Base.deliveries.first
243 assert_equal register_email.to[0], user.email
244 assert_match /#{@url}/, register_email.body.to_s
247 assert_redirected_to :action => "confirm", :display_name => user.display_name
249 ActionMailer::Base.deliveries.clear
252 def test_new_duplicate_email
253 user = build(:user, :pending)
254 user.email = create(:user).email
256 assert_no_difference "User.count" do
257 assert_no_difference "ActionMailer::Base.deliveries.size" do
258 post :save, :session => { :new_user => user }
262 assert_response :success
263 assert_template "new"
264 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
267 def test_new_duplicate_email_uppercase
268 user = build(:user, :pending)
269 user.email = create(:user).email.upcase
271 assert_no_difference "User.count" do
272 assert_no_difference "ActionMailer::Base.deliveries.size" do
273 post :save, :session => { :new_user => user }
277 assert_response :success
278 assert_template "new"
279 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
282 def test_new_duplicate_name
283 user = build(:user, :pending)
284 user.display_name = create(:user).display_name
286 assert_no_difference "User.count" do
287 assert_no_difference "ActionMailer::Base.deliveries.size" do
288 post :save, :session => { :new_user => user }
292 assert_response :success
293 assert_template "new"
294 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
297 def test_new_duplicate_name_uppercase
298 user = build(:user, :pending)
299 user.display_name = create(:user).display_name.upcase
301 assert_no_difference "User.count" do
302 assert_no_difference "ActionMailer::Base.deliveries.size" do
303 post :save, :session => { :new_user => user }
307 assert_response :success
308 assert_template "new"
309 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
312 def test_save_referer_params
313 user = build(:user, :pending)
315 assert_difference "User.count", 1 do
316 assert_difference "ActionMailer::Base.deliveries.size", 1 do
317 post :save, :session => { :new_user => user,
318 :referer => "/edit?editor=id#map=1/2/3" }
322 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
323 user.tokens.order("id DESC").first.referer
325 ActionMailer::Base.deliveries.clear
328 def test_logout_without_referer
330 assert_response :success
331 assert_template :logout
332 assert_select "input[name=referer][value=?]", ""
334 session_id = assert_select("input[name=session]").first["value"]
336 get :logout, :params => { :session => session_id }
337 assert_response :redirect
338 assert_redirected_to root_path
341 def test_logout_with_referer
342 get :logout, :params => { :referer => "/test" }
343 assert_response :success
344 assert_template :logout
345 assert_select "input[name=referer][value=?]", "/test"
347 session_id = assert_select("input[name=session]").first["value"]
349 get :logout, :params => { :session => session_id, :referer => "/test" }
350 assert_response :redirect
351 assert_redirected_to "/test"
354 def test_logout_with_token
355 token = create(:user).tokens.create
357 session[:token] = token.token
360 assert_response :success
361 assert_template :logout
362 assert_select "input[name=referer][value=?]", ""
363 assert_equal token.token, session[:token]
364 assert_not_nil UserToken.where(:id => token.id).first
366 session_id = assert_select("input[name=session]").first["value"]
368 get :logout, :params => { :session => session_id }
369 assert_response :redirect
370 assert_redirected_to root_path
371 assert_nil session[:token]
372 assert_nil UserToken.where(:id => token.id).first
376 user = create(:user, :pending)
377 confirm_string = user.tokens.create.token
379 @request.cookies["_osm_session"] = user.display_name
380 get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
381 assert_response :success
382 assert_template :confirm
385 def test_confirm_get_already_confirmed
387 confirm_string = user.tokens.create.token
389 @request.cookies["_osm_session"] = user.display_name
390 get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
391 assert_response :redirect
392 assert_redirected_to root_path
395 def test_confirm_success_no_token_no_referer
396 user = create(:user, :pending)
397 stub_gravatar_request(user.email)
398 confirm_string = user.tokens.create.token
400 @request.cookies["_osm_session"] = user.display_name
401 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
402 assert_redirected_to login_path
403 assert_match /Confirmed your account/, flash[:notice]
406 def test_confirm_success_good_token_no_referer
407 user = create(:user, :pending)
408 stub_gravatar_request(user.email)
409 confirm_string = user.tokens.create.token
410 token = user.tokens.create.token
412 @request.cookies["_osm_session"] = user.display_name
413 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
414 assert_redirected_to welcome_path
417 def test_confirm_success_bad_token_no_referer
418 user = create(:user, :pending)
419 stub_gravatar_request(user.email)
420 confirm_string = user.tokens.create.token
421 token = create(:user).tokens.create.token
423 @request.cookies["_osm_session"] = user.display_name
424 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
425 assert_redirected_to login_path
426 assert_match /Confirmed your account/, flash[:notice]
429 def test_confirm_success_no_token_with_referer
430 user = create(:user, :pending)
431 stub_gravatar_request(user.email)
432 confirm_string = user.tokens.create(:referer => diary_new_path).token
434 @request.cookies["_osm_session"] = user.display_name
435 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
436 assert_redirected_to login_path(:referer => diary_new_path)
437 assert_match /Confirmed your account/, flash[:notice]
440 def test_confirm_success_good_token_with_referer
441 user = create(:user, :pending)
442 stub_gravatar_request(user.email)
443 confirm_string = user.tokens.create(:referer => diary_new_path).token
444 token = user.tokens.create.token
446 @request.cookies["_osm_session"] = user.display_name
447 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
448 assert_redirected_to diary_new_path
451 def test_confirm_success_bad_token_with_referer
452 user = create(:user, :pending)
453 stub_gravatar_request(user.email)
454 confirm_string = user.tokens.create(:referer => diary_new_path).token
455 token = create(:user).tokens.create.token
457 @request.cookies["_osm_session"] = user.display_name
458 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
459 assert_redirected_to login_path(:referer => diary_new_path)
460 assert_match /Confirmed your account/, flash[:notice]
463 def test_confirm_expired_token
464 user = create(:user, :pending)
465 confirm_string = user.tokens.create(:expiry => 1.day.ago).token
467 @request.cookies["_osm_session"] = user.display_name
468 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
469 assert_redirected_to :action => "confirm"
470 assert_match /confirmation code has expired/, flash[:error]
473 def test_confirm_already_confirmed
475 confirm_string = user.tokens.create(:referer => diary_new_path).token
477 @request.cookies["_osm_session"] = user.display_name
478 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
479 assert_redirected_to :action => "login"
480 assert_match /already been confirmed/, flash[:error]
483 def test_confirm_resend_success
484 user = create(:user, :pending)
485 session[:token] = user.tokens.create.token
487 assert_difference "ActionMailer::Base.deliveries.size", 1 do
488 get :confirm_resend, :params => { :display_name => user.display_name }
491 assert_response :redirect
492 assert_redirected_to login_path
493 assert_match /sent a new confirmation/, flash[:notice]
495 email = ActionMailer::Base.deliveries.last
497 assert_equal user.email, email.to.first
499 ActionMailer::Base.deliveries.clear
502 def test_confirm_resend_no_token
503 user = create(:user, :pending)
504 assert_no_difference "ActionMailer::Base.deliveries.size" do
505 get :confirm_resend, :params => { :display_name => user.display_name }
508 assert_response :redirect
509 assert_redirected_to login_path
510 assert_match "User #{user.display_name} not found.", flash[:error]
513 def test_confirm_resend_unknown_user
514 assert_no_difference "ActionMailer::Base.deliveries.size" do
515 get :confirm_resend, :params => { :display_name => "No Such User" }
518 assert_response :redirect
519 assert_redirected_to login_path
520 assert_match "User No Such User not found.", flash[:error]
523 def test_confirm_email_get
525 confirm_string = user.tokens.create.token
527 get :confirm_email, :params => { :confirm_string => confirm_string }
528 assert_response :success
529 assert_template :confirm_email
532 def test_confirm_email_success
533 user = create(:user, :new_email => "test-new@example.com")
534 stub_gravatar_request(user.new_email)
535 confirm_string = user.tokens.create.token
537 post :confirm_email, :params => { :confirm_string => confirm_string }
538 assert_response :redirect
539 assert_redirected_to :action => :account, :display_name => user.display_name
540 assert_match /Confirmed your change of email address/, flash[:notice]
543 def test_confirm_email_already_confirmed
545 confirm_string = user.tokens.create.token
547 post :confirm_email, :params => { :confirm_string => confirm_string }
548 assert_response :redirect
549 assert_redirected_to :action => :account, :display_name => user.display_name
550 assert_match /already been confirmed/, flash[:error]
553 def test_confirm_email_bad_token
554 post :confirm_email, :params => { :confirm_string => "XXXXX" }
555 assert_response :success
556 assert_template :confirm_email
557 assert_match /confirmation code has expired or does not exist/, flash[:error]
561 # test if testing for a gravatar works
562 # this happens when the email is actually changed
563 # which is triggered by the confirmation mail
564 def test_gravatar_auto_enable
565 # switch to email that has a gravatar
566 user = create(:user, :new_email => "test-new@example.com")
567 stub_gravatar_request(user.new_email, 200)
568 confirm_string = user.tokens.create.token
569 # precondition gravatar should be turned off
570 assert_not user.image_use_gravatar
571 post :confirm_email, :params => { :confirm_string => confirm_string }
572 assert_response :redirect
573 assert_redirected_to :action => :account, :display_name => user.display_name
574 assert_match /Confirmed your change of email address/, flash[:notice]
575 # gravatar use should now be enabled
576 assert User.find(user.id).image_use_gravatar
579 def test_gravatar_auto_disable
580 # switch to email without a gravatar
581 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
582 stub_gravatar_request(user.new_email, 404)
583 confirm_string = user.tokens.create.token
584 # precondition gravatar should be turned on
585 assert user.image_use_gravatar
586 post :confirm_email, :params => { :confirm_string => confirm_string }
587 assert_response :redirect
588 assert_redirected_to :action => :account, :display_name => user.display_name
589 assert_match /Confirmed your change of email address/, flash[:notice]
590 # gravatar use should now be disabled
591 assert_not User.find(user.id).image_use_gravatar
594 def test_terms_new_user
595 get :terms, :session => { :new_user => User.new }
596 assert_response :success
597 assert_template :terms
600 def test_terms_agreed
601 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
603 session[:user] = user.id
606 assert_response :redirect
607 assert_redirected_to :action => :account, :display_name => user.display_name
610 def test_terms_not_seen_without_referer
611 user = create(:user, :terms_seen => false)
613 session[:user] = user.id
616 assert_response :success
617 assert_template :terms
619 post :save, :params => { :user => { :consider_pd => true } }
620 assert_response :redirect
621 assert_redirected_to :action => :account, :display_name => user.display_name
622 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
626 assert_equal true, user.consider_pd
627 assert_not_nil user.terms_agreed
628 assert_equal true, user.terms_seen
631 def test_terms_not_seen_with_referer
632 user = create(:user, :terms_seen => false)
634 session[:user] = user.id
636 get :terms, :params => { :referer => "/test" }
637 assert_response :success
638 assert_template :terms
640 post :save, :params => { :user => { :consider_pd => true }, :referer => "/test" }
641 assert_response :redirect
642 assert_redirected_to "/test"
643 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
647 assert_equal true, user.consider_pd
648 assert_not_nil user.terms_agreed
649 assert_equal true, user.terms_seen
653 user = create(:user, :data_public => false)
654 post :go_public, :session => { :user => user }
655 assert_response :redirect
656 assert_redirected_to :action => :account, :display_name => user.display_name
657 assert_equal true, User.find(user.id).data_public
660 def test_lost_password
661 # Test fetching the lost password page
663 assert_response :success
664 assert_template :lost_password
665 assert_select "div#notice", false
667 # Test resetting using the address as recorded for a user that has an
668 # address which is duplicated in a different case by another user
670 uppercase_user = build(:user, :email => user.email.upcase).tap { |u| u.save(:validate => false) }
672 assert_difference "ActionMailer::Base.deliveries.size", 1 do
673 post :lost_password, :params => { :user => { :email => user.email } }
675 assert_response :redirect
676 assert_redirected_to :action => :login
677 assert_match /^Sorry you lost it/, flash[:notice]
678 email = ActionMailer::Base.deliveries.first
679 assert_equal 1, email.to.count
680 assert_equal user.email, email.to.first
681 ActionMailer::Base.deliveries.clear
683 # Test resetting using an address that matches a different user
684 # that has the same address in a different case
685 assert_difference "ActionMailer::Base.deliveries.size", 1 do
686 post :lost_password, :params => { :user => { :email => user.email.upcase } }
688 assert_response :redirect
689 assert_redirected_to :action => :login
690 assert_match /^Sorry you lost it/, flash[:notice]
691 email = ActionMailer::Base.deliveries.first
692 assert_equal 1, email.to.count
693 assert_equal uppercase_user.email, email.to.first
694 ActionMailer::Base.deliveries.clear
696 # Test resetting using an address that is a case insensitive match
697 # for more than one user but not an exact match for either
698 assert_no_difference "ActionMailer::Base.deliveries.size" do
699 post :lost_password, :params => { :user => { :email => user.email.titlecase } }
701 assert_response :success
702 assert_template :lost_password
703 assert_select ".error", /^Could not find that email address/
705 # Test resetting using the address as recorded for a user that has an
706 # address which is case insensitively unique
707 third_user = create(:user)
708 assert_difference "ActionMailer::Base.deliveries.size", 1 do
709 post :lost_password, :params => { :user => { :email => third_user.email } }
711 assert_response :redirect
712 assert_redirected_to :action => :login
713 assert_match /^Sorry you lost it/, flash[:notice]
714 email = ActionMailer::Base.deliveries.first
715 assert_equal 1, email.to.count
716 assert_equal third_user.email, email.to.first
717 ActionMailer::Base.deliveries.clear
719 # Test resetting using an address that matches a user that has the
720 # same (case insensitively unique) address in a different case
721 assert_difference "ActionMailer::Base.deliveries.size", 1 do
722 post :lost_password, :params => { :user => { :email => third_user.email.upcase } }
724 assert_response :redirect
725 assert_redirected_to :action => :login
726 assert_match /^Sorry you lost it/, flash[:notice]
727 email = ActionMailer::Base.deliveries.first
728 assert_equal 1, email.to.count
729 assert_equal third_user.email, email.to.first
730 ActionMailer::Base.deliveries.clear
733 def test_reset_password
734 user = create(:user, :pending)
735 # Test a request with no token
737 assert_response :bad_request
739 # Test a request with a bogus token
740 get :reset_password, :params => { :token => "made_up_token" }
741 assert_response :redirect
742 assert_redirected_to :action => :lost_password
744 # Create a valid token for a user
745 token = user.tokens.create
747 # Test a request with a valid token
748 get :reset_password, :params => { :token => token.token }
749 assert_response :success
750 assert_template :reset_password
752 # Test that errors are reported for erroneous submissions
753 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "different_password" } }
754 assert_response :success
755 assert_template :reset_password
756 assert_select "div#errorExplanation"
758 # Test setting a new password
759 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" } }
760 assert_response :redirect
761 assert_redirected_to root_path
762 assert_equal user.id, session[:user]
764 assert_equal "active", user.status
765 assert_equal true, user.email_valid
766 assert_equal user, User.authenticate(:username => user.email, :password => "new_password")
770 # Get a user to work with - note that this user deliberately
771 # conflicts with uppercase_user in the email and display name
772 # fields to test that we can change other fields without any
773 # validation errors being reported
774 user = create(:user, :languages => [])
775 _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
777 # Make sure that you are redirected to the login page when
778 # you are not logged in
779 get :account, :params => { :display_name => user.display_name }
780 assert_response :redirect
781 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
783 # Make sure that you are blocked when not logged in as the right user
784 get :account, :params => { :display_name => user.display_name }, :session => { :user => create(:user) }
785 assert_response :forbidden
787 # Make sure we get the page when we are logged in as the right user
788 get :account, :params => { :display_name => user.display_name }, :session => { :user => user }
789 assert_response :success
790 assert_template :account
791 assert_select "form#accountForm" do |form|
792 assert_equal "post", form.attr("method").to_s
793 assert_select "input[name='_method']", false
794 assert_equal "/user/#{ERB::Util.u(user.display_name)}/account", form.attr("action").to_s
797 # Updating the description should work
798 user.description = "new description"
799 user.preferred_editor = "default"
800 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
801 assert_response :success
802 assert_template :account
803 assert_select "div#errorExplanation", false
804 assert_select ".notice", /^User information updated successfully/
805 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
807 # Changing to a invalid editor should fail
808 user.preferred_editor = "unknown"
809 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
810 assert_response :success
811 assert_template :account
812 assert_select ".notice", false
813 assert_select "div#errorExplanation"
814 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
816 # Changing to a valid editor should work
817 user.preferred_editor = "potlatch2"
818 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
819 assert_response :success
820 assert_template :account
821 assert_select "div#errorExplanation", false
822 assert_select ".notice", /^User information updated successfully/
823 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
825 # Changing to the default editor should work
826 user.preferred_editor = "default"
827 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
828 assert_response :success
829 assert_template :account
830 assert_select "div#errorExplanation", false
831 assert_select ".notice", /^User information updated successfully/
832 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
834 # Changing to an uploaded image should work
835 image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif")
836 post :account, :params => { :display_name => user.display_name, :image_action => "new", :user => user.attributes.merge(:image => image) }, :session => { :user => user }
837 assert_response :success
838 assert_template :account
839 assert_select "div#errorExplanation", false
840 assert_select ".notice", /^User information updated successfully/
841 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "keep"
843 # Changing to a gravatar image should work
844 post :account, :params => { :display_name => user.display_name, :image_action => "gravatar", :user => user.attributes }, :session => { :user => user }
845 assert_response :success
846 assert_template :account
847 assert_select "div#errorExplanation", false
848 assert_select ".notice", /^User information updated successfully/
849 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "gravatar"
851 # Removing the image should work
852 post :account, :params => { :display_name => user.display_name, :image_action => "delete", :user => user.attributes }, :session => { :user => user }
853 assert_response :success
854 assert_template :account
855 assert_select "div#errorExplanation", false
856 assert_select ".notice", /^User information updated successfully/
857 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked]", false
859 # Adding external authentication should redirect to the auth provider
860 post :account, :params => { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, :session => { :user => user }
861 assert_response :redirect
862 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")
864 # Changing name to one that exists should fail
865 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
866 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
867 assert_response :success
868 assert_template :account
869 assert_select ".notice", false
870 assert_select "div#errorExplanation"
871 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
873 # Changing name to one that exists should fail, regardless of case
874 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
875 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
876 assert_response :success
877 assert_template :account
878 assert_select ".notice", false
879 assert_select "div#errorExplanation"
880 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
882 # Changing name to one that doesn't exist should work
883 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
884 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
885 assert_response :success
886 assert_template :account
887 assert_select "div#errorExplanation", false
888 assert_select ".notice", /^User information updated successfully/
889 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
891 # Record the change of name
892 user.display_name = "new tester"
894 # Changing email to one that exists should fail
895 user.new_email = create(:user).email
896 assert_no_difference "ActionMailer::Base.deliveries.size" do
897 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
899 assert_response :success
900 assert_template :account
901 assert_select ".notice", false
902 assert_select "div#errorExplanation"
903 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
905 # Changing email to one that exists should fail, regardless of case
906 user.new_email = create(:user).email.upcase
907 assert_no_difference "ActionMailer::Base.deliveries.size" do
908 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
910 assert_response :success
911 assert_template :account
912 assert_select ".notice", false
913 assert_select "div#errorExplanation"
914 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
916 # Changing email to one that doesn't exist should work
917 user.new_email = "new_tester@example.com"
918 assert_difference "ActionMailer::Base.deliveries.size", 1 do
919 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
921 assert_response :success
922 assert_template :account
923 assert_select "div#errorExplanation", false
924 assert_select ".notice", /^User information updated successfully/
925 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
926 email = ActionMailer::Base.deliveries.first
927 assert_equal 1, email.to.count
928 assert_equal user.new_email, email.to.first
929 ActionMailer::Base.deliveries.clear
932 # Check that the user account page will display and contains some relevant
933 # information for the user
935 # Test a non-existent user
936 get :view, :params => { :display_name => "unknown" }
937 assert_response :not_found
941 get :view, :params => { :display_name => user.display_name }
942 assert_response :success
943 assert_select "div#userinformation" do
944 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
945 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
946 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
947 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
948 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
949 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
950 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
951 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
954 # Test a user who has been blocked
955 blocked_user = create(:user)
956 create(:user_block, :user => blocked_user)
957 get :view, :params => { :display_name => blocked_user.display_name }
958 assert_response :success
959 assert_select "div#userinformation" do
960 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
961 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
962 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
963 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
964 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
965 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
966 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
967 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
970 # Test a moderator who has applied blocks
971 moderator_user = create(:moderator_user)
972 create(:user_block, :creator => moderator_user)
973 get :view, :params => { :display_name => moderator_user.display_name }
974 assert_response :success
975 assert_select "div#userinformation" do
976 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
977 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
978 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
979 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
980 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
981 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
982 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
983 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
986 # Login as a normal user
987 session[:user] = user.id
989 # Test the normal user
990 get :view, :params => { :display_name => user.display_name }
991 assert_response :success
992 assert_select "div#userinformation" do
993 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
994 assert_select "a[href='/traces/mine']", 1
995 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
996 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
997 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 1
998 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
999 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1000 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1003 # Login as a moderator
1004 session[:user] = create(:moderator_user).id
1006 # Test the normal user
1007 get :view, :params => { :display_name => user.display_name }
1008 assert_response :success
1009 assert_select "div#userinformation" do
1010 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1011 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1012 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1013 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1014 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1015 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1016 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1017 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
1021 # Test whether information about contributor terms is shown for users who haven't agreed
1022 def test_terms_not_agreed
1023 agreed_user = create(:user, :terms_agreed => 3.days.ago)
1024 seen_user = create(:user, :terms_seen => true)
1025 not_seen_user = create(:user, :terms_seen => false)
1027 get :view, :params => { :display_name => agreed_user.display_name }
1028 assert_response :success
1029 assert_select "div#userinformation" do
1030 assert_select "p", :count => 0, :text => /Contributor terms/
1033 get :view, :params => { :display_name => seen_user.display_name }
1034 assert_response :success
1035 # put @response.body
1036 assert_select "div#userinformation" do
1037 assert_select "p", :count => 1, :text => /Contributor terms/
1038 assert_select "p", /Declined/
1041 get :view, :params => { :display_name => not_seen_user.display_name }
1042 assert_response :success
1043 assert_select "div#userinformation" do
1044 assert_select "p", :count => 1, :text => /Contributor terms/
1045 assert_select "p", /Undecided/
1050 user = create(:user, :description => "test", :terms_agreed => Date.yesterday)
1051 # check that a visible user is returned properly
1052 get :api_read, :params => { :id => user.id }
1053 assert_response :success
1054 assert_equal "text/xml", response.content_type
1056 # check the data that is returned
1057 assert_select "description", :count => 1, :text => "test"
1058 assert_select "contributor-terms", :count => 1 do
1059 assert_select "[agreed='true']"
1061 assert_select "img", :count => 0
1062 assert_select "roles", :count => 1 do
1063 assert_select "role", :count => 0
1065 assert_select "changesets", :count => 1 do
1066 assert_select "[count='0']"
1068 assert_select "traces", :count => 1 do
1069 assert_select "[count='0']"
1071 assert_select "blocks", :count => 1 do
1072 assert_select "received", :count => 1 do
1073 assert_select "[count='0'][active='0']"
1075 assert_select "issued", :count => 0
1078 # check that we aren't revealing private information
1079 assert_select "contributor-terms[pd]", false
1080 assert_select "home", false
1081 assert_select "languages", false
1082 assert_select "messages", false
1084 # check that a suspended user is not returned
1085 get :api_read, :params => { :id => create(:user, :suspended).id }
1086 assert_response :gone
1088 # check that a deleted user is not returned
1089 get :api_read, :params => { :id => create(:user, :deleted).id }
1090 assert_response :gone
1092 # check that a non-existent user is not returned
1093 get :api_read, :params => { :id => 0 }
1094 assert_response :not_found
1097 def test_api_details
1098 user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"])
1099 create(:message, :read, :recipient => user)
1100 create(:message, :sender => user)
1102 # check that nothing is returned when not logged in
1104 assert_response :unauthorized
1106 # check that we get a response when logged in
1107 basic_authorization user.email, "test"
1109 assert_response :success
1110 assert_equal "text/xml", response.content_type
1112 # check the data that is returned
1113 assert_select "description", :count => 1, :text => "test"
1114 assert_select "contributor-terms", :count => 1 do
1115 assert_select "[agreed='true'][pd='false']"
1117 assert_select "img", :count => 0
1118 assert_select "roles", :count => 1 do
1119 assert_select "role", :count => 0
1121 assert_select "changesets", :count => 1 do
1122 assert_select "[count='0']", :count => 1
1124 assert_select "traces", :count => 1 do
1125 assert_select "[count='0']", :count => 1
1127 assert_select "blocks", :count => 1 do
1128 assert_select "received", :count => 1 do
1129 assert_select "[count='0'][active='0']"
1131 assert_select "issued", :count => 0
1133 assert_select "home", :count => 1 do
1134 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
1136 assert_select "languages", :count => 1 do
1137 assert_select "lang", :count => 1, :text => "en"
1139 assert_select "messages", :count => 1 do
1140 assert_select "received", :count => 1 do
1141 assert_select "[count='1'][unread='0']"
1143 assert_select "sent", :count => 1 do
1144 assert_select "[count='1']"
1149 def test_api_gpx_files
1150 user = create(:user)
1151 trace1 = create(:trace, :user => user) do |trace|
1152 create(:tracetag, :trace => trace, :tag => "London")
1154 trace2 = create(:trace, :user => user) do |trace|
1155 create(:tracetag, :trace => trace, :tag => "Birmingham")
1157 # check that nothing is returned when not logged in
1159 assert_response :unauthorized
1161 # check that we get a response when logged in
1162 basic_authorization user.email, "test"
1164 assert_response :success
1165 assert_equal "application/xml", response.content_type
1167 # check the data that is returned
1168 assert_select "gpx_file[id='#{trace1.id}']", 1 do
1169 assert_select "tag", "London"
1171 assert_select "gpx_file[id='#{trace2.id}']", 1 do
1172 assert_select "tag", "Birmingham"
1176 def test_make_friend
1177 # Get users to work with
1178 user = create(:user)
1179 friend = create(:user)
1181 # Check that the users aren't already friends
1182 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1184 # When not logged in a GET should ask us to login
1185 get :make_friend, :params => { :display_name => friend.display_name }
1186 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1188 # When not logged in a POST should error
1189 post :make_friend, :params => { :display_name => friend.display_name }
1190 assert_response :forbidden
1191 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1193 # When logged in a GET should get a confirmation page
1194 get :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1195 assert_response :success
1196 assert_template :make_friend
1197 assert_select "form" do
1198 assert_select "input[type='hidden'][name='referer']", 0
1199 assert_select "input[type='submit']", 1
1201 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1203 # When logged in a POST should add the friendship
1204 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1205 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1207 assert_redirected_to user_path(:display_name => friend.display_name)
1208 assert_match /is now your friend/, flash[:notice]
1209 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1210 email = ActionMailer::Base.deliveries.first
1211 assert_equal 1, email.to.count
1212 assert_equal friend.email, email.to.first
1213 ActionMailer::Base.deliveries.clear
1215 # A second POST should report that the friendship already exists
1216 assert_no_difference "ActionMailer::Base.deliveries.size" do
1217 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1219 assert_redirected_to user_path(:display_name => friend.display_name)
1220 assert_match /You are already friends with/, flash[:warning]
1221 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1224 def test_make_friend_with_referer
1225 # Get users to work with
1226 user = create(:user)
1227 friend = create(:user)
1229 # Check that the users aren't already friends
1230 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1232 # The GET should preserve any referer
1233 get :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1234 assert_response :success
1235 assert_template :make_friend
1236 assert_select "form" do
1237 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1238 assert_select "input[type='submit']", 1
1240 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1242 # When logged in a POST should add the friendship and refer us
1243 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1244 post :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1246 assert_redirected_to "/test"
1247 assert_match /is now your friend/, flash[:notice]
1248 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1249 email = ActionMailer::Base.deliveries.first
1250 assert_equal 1, email.to.count
1251 assert_equal friend.email, email.to.first
1252 ActionMailer::Base.deliveries.clear
1255 def test_make_friend_unkown_user
1256 # Should error when a bogus user is specified
1257 get :make_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1258 assert_response :not_found
1259 assert_template :no_such_user
1262 def test_remove_friend
1263 # Get users to work with
1264 user = create(:user)
1265 friend = create(:user)
1266 create(:friend, :befriender => user, :befriendee => friend)
1268 # Check that the users are friends
1269 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1271 # When not logged in a GET should ask us to login
1272 get :remove_friend, :params => { :display_name => friend.display_name }
1273 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1275 # When not logged in a POST should error
1276 post :remove_friend, :params => { :display_name => friend.display_name }
1277 assert_response :forbidden
1278 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1280 # When logged in a GET should get a confirmation page
1281 get :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1282 assert_response :success
1283 assert_template :remove_friend
1284 assert_select "form" do
1285 assert_select "input[type='hidden'][name='referer']", 0
1286 assert_select "input[type='submit']", 1
1288 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1290 # When logged in a POST should remove the friendship
1291 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1292 assert_redirected_to user_path(:display_name => friend.display_name)
1293 assert_match /was removed from your friends/, flash[:notice]
1294 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1296 # A second POST should report that the friendship does not exist
1297 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1298 assert_redirected_to user_path(:display_name => friend.display_name)
1299 assert_match /is not one of your friends/, flash[:error]
1300 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1303 def test_remove_friend_with_referer
1304 # Get users to work with
1305 user = create(:user)
1306 friend = create(:user)
1307 create(:friend, :user_id => user.id, :friend_user_id => friend.id)
1309 # Check that the users are friends
1310 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1312 # The GET should preserve any referer
1313 get :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1314 assert_response :success
1315 assert_template :remove_friend
1316 assert_select "form" do
1317 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1318 assert_select "input[type='submit']", 1
1320 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1322 # When logged in a POST should remove the friendship and refer
1323 post :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1324 assert_redirected_to "/test"
1325 assert_match /was removed from your friends/, flash[:notice]
1326 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1329 def test_remove_friend_unkown_user
1330 # Should error when a bogus user is specified
1331 get :remove_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1332 assert_response :not_found
1333 assert_template :no_such_user
1337 user = create(:user)
1339 # Try without logging in
1340 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }
1341 assert_response :redirect
1342 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1344 # Now try as a normal user
1345 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1346 assert_response :redirect
1347 assert_redirected_to :action => :view, :display_name => user.display_name
1349 # Finally try as an administrator
1350 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1351 assert_response :redirect
1352 assert_redirected_to :action => :view, :display_name => user.display_name
1353 assert_equal "suspended", User.find(user.id).status
1357 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1359 # Try without logging in
1360 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }
1361 assert_response :redirect
1362 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1364 # Now try as a normal user
1365 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1366 assert_response :redirect
1367 assert_redirected_to :action => :view, :display_name => user.display_name
1369 # Finally try as an administrator
1370 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1371 assert_response :redirect
1372 assert_redirected_to :action => :view, :display_name => user.display_name
1374 # Check that the user was deleted properly
1376 assert_equal "user_#{user.id}", user.display_name
1377 assert_equal "", user.description
1378 assert_nil user.home_lat
1379 assert_nil user.home_lon
1380 assert_equal false, user.image.file?
1381 assert_equal false, user.email_valid
1382 assert_nil user.new_email
1383 assert_nil user.auth_provider
1384 assert_nil user.auth_uid
1385 assert_equal "deleted", user.status
1389 user = create(:user)
1390 moderator_user = create(:moderator_user)
1391 administrator_user = create(:administrator_user)
1392 _suspended_user = create(:user, :suspended)
1393 _ip_user = create(:user, :creation_ip => "1.2.3.4")
1395 # There are now 7 users - the five above, plus two extra "granters" for the
1396 # moderator_user and administrator_user
1397 assert_equal 7, User.count
1399 # Shouldn't work when not logged in
1401 assert_response :redirect
1402 assert_redirected_to :action => :login, :referer => users_path
1404 session[:user] = user.id
1406 # Shouldn't work when logged in as a normal user
1408 assert_response :redirect
1409 assert_redirected_to :action => :login, :referer => users_path
1411 session[:user] = moderator_user.id
1413 # Shouldn't work when logged in as a moderator
1415 assert_response :redirect
1416 assert_redirected_to :action => :login, :referer => users_path
1418 session[:user] = administrator_user.id
1420 # Note there is a header row, so all row counts are users + 1
1421 # Should work when logged in as an administrator
1423 assert_response :success
1424 assert_template :list
1425 assert_select "table#user_list tr", :count => 7 + 1
1427 # Should be able to limit by status
1428 get :list, :params => { :status => "suspended" }
1429 assert_response :success
1430 assert_template :list
1431 assert_select "table#user_list tr", :count => 1 + 1
1433 # Should be able to limit by IP address
1434 get :list, :params => { :ip => "1.2.3.4" }
1435 assert_response :success
1436 assert_template :list
1437 assert_select "table#user_list tr", :count => 1 + 1
1440 def test_list_get_paginated
1441 1.upto(100).each do |n|
1442 User.create(:display_name => "extra_#{n}",
1443 :email => "extra#{n}@example.com",
1444 :pass_crypt => "extraextra")
1447 session[:user] = create(:administrator_user).id
1449 # 100 examples, an administrator, and a granter for the admin.
1450 assert_equal 102, User.count
1453 assert_response :success
1454 assert_template :list
1455 assert_select "table#user_list tr", :count => 51
1457 get :list, :params => { :page => 2 }
1458 assert_response :success
1459 assert_template :list
1460 assert_select "table#user_list tr", :count => 51
1462 get :list, :params => { :page => 3 }
1463 assert_response :success
1464 assert_template :list
1465 assert_select "table#user_list tr", :count => 3
1468 def test_list_post_confirm
1469 inactive_user = create(:user, :pending)
1470 suspended_user = create(:user, :suspended)
1472 # Shouldn't work when not logged in
1473 assert_no_difference "User.active.count" do
1474 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1476 assert_response :redirect
1477 assert_redirected_to :action => :login, :referer => users_path
1478 assert_equal "pending", inactive_user.reload.status
1479 assert_equal "suspended", suspended_user.reload.status
1481 session[:user] = create(:user).id
1483 # Shouldn't work when logged in as a normal user
1484 assert_no_difference "User.active.count" do
1485 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1487 assert_response :redirect
1488 assert_redirected_to :action => :login, :referer => users_path
1489 assert_equal "pending", inactive_user.reload.status
1490 assert_equal "suspended", suspended_user.reload.status
1492 session[:user] = create(:moderator_user).id
1494 # Shouldn't work when logged in as a moderator
1495 assert_no_difference "User.active.count" do
1496 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1498 assert_response :redirect
1499 assert_redirected_to :action => :login, :referer => users_path
1500 assert_equal "pending", inactive_user.reload.status
1501 assert_equal "suspended", suspended_user.reload.status
1503 session[:user] = create(:administrator_user).id
1505 # Should work when logged in as an administrator
1506 assert_difference "User.active.count", 2 do
1507 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1509 assert_response :redirect
1510 assert_redirected_to :action => :list
1511 assert_equal "confirmed", inactive_user.reload.status
1512 assert_equal "confirmed", suspended_user.reload.status
1515 def test_list_post_hide
1516 normal_user = create(:user)
1517 confirmed_user = create(:user, :confirmed)
1519 # Shouldn't work when not logged in
1520 assert_no_difference "User.active.count" do
1521 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1523 assert_response :redirect
1524 assert_redirected_to :action => :login, :referer => users_path
1525 assert_equal "active", normal_user.reload.status
1526 assert_equal "confirmed", confirmed_user.reload.status
1528 session[:user] = create(:user).id
1530 # Shouldn't work when logged in as a normal user
1531 assert_no_difference "User.active.count" do
1532 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1534 assert_response :redirect
1535 assert_redirected_to :action => :login, :referer => users_path
1536 assert_equal "active", normal_user.reload.status
1537 assert_equal "confirmed", confirmed_user.reload.status
1539 session[:user] = create(:moderator_user).id
1541 # Shouldn't work when logged in as a moderator
1542 assert_no_difference "User.active.count" do
1543 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1545 assert_response :redirect
1546 assert_redirected_to :action => :login, :referer => users_path
1547 assert_equal "active", normal_user.reload.status
1548 assert_equal "confirmed", confirmed_user.reload.status
1550 session[:user] = create(:administrator_user).id
1552 # Should work when logged in as an administrator
1553 assert_difference "User.active.count", -2 do
1554 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1556 assert_response :redirect
1557 assert_redirected_to :action => :list
1558 assert_equal "deleted", normal_user.reload.status
1559 assert_equal "deleted", confirmed_user.reload.status