3 class UserControllerTest < ActionController::TestCase
5 fixtures :messages, :friends
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/user/1", :method => :get },
12 { :controller => "user", :action => "api_read", :id => "1" }
15 { :path => "/api/0.6/user/details", :method => :get },
16 { :controller => "user", :action => "api_details" }
19 { :path => "/api/0.6/user/gpx_files", :method => :get },
20 { :controller => "user", :action => "api_gpx_files" }
24 { :path => "/login", :method => :get },
25 { :controller => "user", :action => "login" }
28 { :path => "/login", :method => :post },
29 { :controller => "user", :action => "login" }
32 { :controller => "user", :action => "login", :format => "html" },
33 { :path => "/login.html", :method => :get }
37 { :path => "/logout", :method => :get },
38 { :controller => "user", :action => "logout" }
41 { :path => "/logout", :method => :post },
42 { :controller => "user", :action => "logout" }
45 { :controller => "user", :action => "logout", :format => "html" },
46 { :path => "/logout.html", :method => :get }
50 { :path => "/user/new", :method => :get },
51 { :controller => "user", :action => "new" }
55 { :path => "/user/new", :method => :post },
56 { :controller => "user", :action => "create" }
60 { :path => "/user/terms", :method => :get },
61 { :controller => "user", :action => "terms" }
65 { :path => "/user/save", :method => :post },
66 { :controller => "user", :action => "save" }
70 { :path => "/user/username/confirm", :method => :get },
71 { :controller => "user", :action => "confirm", :display_name => "username" }
74 { :path => "/user/username/confirm", :method => :post },
75 { :controller => "user", :action => "confirm", :display_name => "username" }
78 { :path => "/user/username/confirm/resend", :method => :get },
79 { :controller => "user", :action => "confirm_resend", :display_name => "username" }
83 { :path => "/user/confirm", :method => :get },
84 { :controller => "user", :action => "confirm" }
87 { :path => "/user/confirm", :method => :post },
88 { :controller => "user", :action => "confirm" }
91 { :path => "/user/confirm-email", :method => :get },
92 { :controller => "user", :action => "confirm_email" }
95 { :path => "/user/confirm-email", :method => :post },
96 { :controller => "user", :action => "confirm_email" }
100 { :path => "/user/go_public", :method => :post },
101 { :controller => "user", :action => "go_public" }
105 { :path => "/user/forgot-password", :method => :get },
106 { :controller => "user", :action => "lost_password" }
109 { :path => "/user/forgot-password", :method => :post },
110 { :controller => "user", :action => "lost_password" }
113 { :path => "/user/reset-password", :method => :get },
114 { :controller => "user", :action => "reset_password" }
117 { :path => "/user/reset-password", :method => :post },
118 { :controller => "user", :action => "reset_password" }
122 { :path => "/user/suspended", :method => :get },
123 { :controller => "user", :action => "suspended" }
127 { :path => "/user/username", :method => :get },
128 { :controller => "user", :action => "view", :display_name => "username" }
132 { :path => "/user/username/account", :method => :get },
133 { :controller => "user", :action => "account", :display_name => "username" }
136 { :path => "/user/username/account", :method => :post },
137 { :controller => "user", :action => "account", :display_name => "username" }
141 { :path => "/user/username/make_friend", :method => :get },
142 { :controller => "user", :action => "make_friend", :display_name => "username" }
145 { :path => "/user/username/make_friend", :method => :post },
146 { :controller => "user", :action => "make_friend", :display_name => "username" }
149 { :path => "/user/username/remove_friend", :method => :get },
150 { :controller => "user", :action => "remove_friend", :display_name => "username" }
153 { :path => "/user/username/remove_friend", :method => :post },
154 { :controller => "user", :action => "remove_friend", :display_name => "username" }
158 { :path => "/user/username/set_status", :method => :get },
159 { :controller => "user", :action => "set_status", :display_name => "username" }
162 { :path => "/user/username/delete", :method => :get },
163 { :controller => "user", :action => "delete", :display_name => "username" }
167 { :path => "/users", :method => :get },
168 { :controller => "user", :action => "list" }
171 { :path => "/users", :method => :post },
172 { :controller => "user", :action => "list" }
175 { :path => "/users/status", :method => :get },
176 { :controller => "user", :action => "list", :status => "status" }
179 { :path => "/users/status", :method => :post },
180 { :controller => "user", :action => "list", :status => "status" }
184 # The user creation page loads
187 assert_response :redirect
188 assert_redirected_to user_new_path(:cookie_test => "true")
190 get :new, { :cookie_test => "true" }, { :cookie_test => true }
191 assert_response :success
193 assert_select "html", :count => 1 do
194 assert_select "head", :count => 1 do
195 assert_select "title", :text => /Sign Up/, :count => 1
197 assert_select "body", :count => 1 do
198 assert_select "div#content", :count => 1 do
199 assert_select "form[action='/user/new'][method='post']", :count => 1 do
200 assert_select "input[id='user_email']", :count => 1
201 assert_select "input[id='user_email_confirmation']", :count => 1
202 assert_select "input[id='user_display_name']", :count => 1
203 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
204 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
205 assert_select "input[type='submit'][value='Sign Up']", :count => 1
212 def test_new_view_logged_in
213 session[:user] = users(:normal_user).id
216 assert_response :redirect
217 assert_redirected_to user_new_path(:cookie_test => "true")
218 get :new, :cookie_test => "true"
219 assert_response :redirect
220 assert_redirected_to root_path
222 get :new, :referer => "/test"
223 assert_response :redirect
224 assert_redirected_to user_new_path(:referer => "/test", :cookie_test => "true")
225 get :new, :referer => "/test", :cookie_test => "true"
226 assert_response :redirect
227 assert_redirected_to "/test"
233 assert_difference "User.count", 1 do
234 assert_difference "ActionMailer::Base.deliveries.size", 1 do
235 post :save, {}, { :new_user => user }
240 register_email = ActionMailer::Base.deliveries.first
242 assert_equal register_email.to[0], user.email
243 assert_match /#{@url}/, register_email.body.to_s
246 assert_redirected_to :action => "confirm", :display_name => user.display_name
248 ActionMailer::Base.deliveries.clear
251 def test_new_duplicate_email
253 user.email = users(:public_user).email
255 assert_no_difference "User.count" do
256 assert_no_difference "ActionMailer::Base.deliveries.size" do
257 post :save, {}, { :new_user => user }
261 assert_response :success
262 assert_template "new"
263 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
266 def test_new_duplicate_email_uppercase
268 user.email = users(:public_user).email.upcase
270 assert_no_difference "User.count" do
271 assert_no_difference "ActionMailer::Base.deliveries.size" do
272 post :save, {}, { :new_user => user }
276 assert_response :success
277 assert_template "new"
278 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
281 def test_new_duplicate_name
283 user.display_name = users(:public_user).display_name
285 assert_no_difference "User.count" do
286 assert_no_difference "ActionMailer::Base.deliveries.size" do
287 post :save, {}, { :new_user => user }
291 assert_response :success
292 assert_template "new"
293 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
296 def test_new_duplicate_name_uppercase
298 user.display_name = users(:public_user).display_name.upcase
300 assert_no_difference "User.count" do
301 assert_no_difference "ActionMailer::Base.deliveries.size" do
302 post :save, {}, { :new_user => user }
306 assert_response :success
307 assert_template "new"
308 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
311 def test_save_referer_params
314 assert_difference "User.count", 1 do
315 assert_difference "ActionMailer::Base.deliveries.size", 1 do
316 post :save, {}, { :new_user => user,
317 :referer => "/edit?editor=id#map=1/2/3" }
321 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
322 user.tokens.order("id DESC").first.referer
324 ActionMailer::Base.deliveries.clear
327 def test_logout_without_referer
329 assert_response :success
330 assert_template :logout
331 assert_select "input[name=referer][value=?]", ""
333 session_id = assert_select("input[name=session]").first["value"]
335 get :logout, :session => session_id
336 assert_response :redirect
337 assert_redirected_to root_path
340 def test_logout_with_referer
341 get :logout, :referer => "/test"
342 assert_response :success
343 assert_template :logout
344 assert_select "input[name=referer][value=?]", "/test"
346 session_id = assert_select("input[name=session]").first["value"]
348 get :logout, :session => session_id, :referer => "/test"
349 assert_response :redirect
350 assert_redirected_to "/test"
353 def test_logout_with_token
354 token = users(:normal_user).tokens.create
356 session[:token] = token.token
359 assert_response :success
360 assert_template :logout
361 assert_select "input[name=referer][value=?]", ""
362 assert_equal token.token, session[:token]
363 assert_not_nil UserToken.where(:id => token.id).first
365 session_id = assert_select("input[name=session]").first["value"]
367 get :logout, :session => session_id
368 assert_response :redirect
369 assert_redirected_to root_path
370 assert_nil session[:token]
371 assert_nil UserToken.where(:id => token.id).first
375 user = users(:inactive_user)
376 confirm_string = user.tokens.create.token
378 @request.cookies["_osm_session"] = user.display_name
379 get :confirm, :display_name => user.display_name, :confirm_string => confirm_string
380 assert_response :success
381 assert_template :confirm
384 def test_confirm_get_already_confirmed
385 user = users(:normal_user)
386 confirm_string = user.tokens.create.token
388 @request.cookies["_osm_session"] = user.display_name
389 get :confirm, :display_name => user.display_name, :confirm_string => confirm_string
390 assert_response :redirect
391 assert_redirected_to root_path
394 def test_confirm_success_no_token_no_referer
395 user = users(:inactive_user)
396 confirm_string = user.tokens.create.token
398 @request.cookies["_osm_session"] = user.display_name
399 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
400 assert_redirected_to login_path
401 assert_match /Confirmed your account/, flash[:notice]
404 def test_confirm_success_good_token_no_referer
405 user = users(:inactive_user)
406 confirm_string = user.tokens.create.token
407 token = user.tokens.create.token
409 @request.cookies["_osm_session"] = user.display_name
410 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
411 assert_redirected_to welcome_path
414 def test_confirm_success_bad_token_no_referer
415 user = users(:inactive_user)
416 confirm_string = user.tokens.create.token
417 token = users(:normal_user).tokens.create.token
419 @request.cookies["_osm_session"] = user.display_name
420 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
421 assert_redirected_to login_path
422 assert_match /Confirmed your account/, flash[:notice]
425 def test_confirm_success_no_token_with_referer
426 user = users(:inactive_user)
427 confirm_string = user.tokens.create(:referer => diary_new_path).token
429 @request.cookies["_osm_session"] = user.display_name
430 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
431 assert_redirected_to login_path(:referer => diary_new_path)
432 assert_match /Confirmed your account/, flash[:notice]
435 def test_confirm_success_good_token_with_referer
436 user = users(:inactive_user)
437 confirm_string = user.tokens.create(:referer => diary_new_path).token
438 token = user.tokens.create.token
440 @request.cookies["_osm_session"] = user.display_name
441 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
442 assert_redirected_to diary_new_path
445 def test_confirm_success_bad_token_with_referer
446 user = users(:inactive_user)
447 confirm_string = user.tokens.create(:referer => diary_new_path).token
448 token = users(:normal_user).tokens.create.token
450 @request.cookies["_osm_session"] = user.display_name
451 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
452 assert_redirected_to login_path(:referer => diary_new_path)
453 assert_match /Confirmed your account/, flash[:notice]
456 def test_confirm_expired_token
457 user = users(:inactive_user)
458 confirm_string = user.tokens.create(:expiry => 1.day.ago).token
460 @request.cookies["_osm_session"] = user.display_name
461 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
462 assert_redirected_to :action => "confirm"
463 assert_match /confirmation code has expired/, flash[:error]
466 def test_confirm_already_confirmed
467 user = users(:normal_user)
468 confirm_string = user.tokens.create(:referer => diary_new_path).token
470 @request.cookies["_osm_session"] = user.display_name
471 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
472 assert_redirected_to :action => "login"
473 assert_match /already been confirmed/, flash[:error]
476 def test_confirm_resend_success
477 assert_difference "ActionMailer::Base.deliveries.size", 1 do
478 get :confirm_resend, :display_name => users(:inactive_user).display_name
481 assert_response :redirect
482 assert_redirected_to login_path
483 assert_match /sent a new confirmation/, flash[:notice]
485 email = ActionMailer::Base.deliveries.last
487 assert_equal users(:inactive_user).email, email.to.first
489 ActionMailer::Base.deliveries.clear
492 def test_confirm_resend_failure
493 assert_no_difference "ActionMailer::Base.deliveries.size" do
494 get :confirm_resend, :display_name => "No Such User"
497 assert_response :redirect
498 assert_redirected_to login_path
499 assert_match "User No Such User not found.", flash[:error]
502 def test_confirm_email_get
503 user = users(:normal_user)
504 confirm_string = user.tokens.create.token
506 get :confirm_email, :confirm_string => confirm_string
507 assert_response :success
508 assert_template :confirm_email
511 def test_confirm_email_success
512 user = users(:second_public_user)
513 confirm_string = user.tokens.create.token
515 post :confirm_email, :confirm_string => confirm_string
516 assert_response :redirect
517 assert_redirected_to :action => :account, :display_name => user.display_name
518 assert_match /Confirmed your change of email address/, flash[:notice]
521 def test_confirm_email_already_confirmed
522 user = users(:normal_user)
523 confirm_string = user.tokens.create.token
525 post :confirm_email, :confirm_string => confirm_string
526 assert_response :redirect
527 assert_redirected_to :action => :account, :display_name => user.display_name
528 assert_match /already been confirmed/, flash[:error]
531 def test_confirm_email_bad_token
532 post :confirm_email, :confirm_string => "XXXXX"
533 assert_response :success
534 assert_template :confirm_email
535 assert_match /confirmation code has expired or does not exist/, flash[:error]
538 def test_terms_new_user
539 get :terms, {}, { :new_user => User.new }
540 assert_response :success
541 assert_template :terms
545 user = users(:normal_user)
547 session[:user] = user.id
550 assert_response :redirect
551 assert_redirected_to :action => :account, :display_name => user.display_name
554 def test_terms_not_seen_without_referer
555 user = users(:terms_not_seen_user)
557 session[:user] = user.id
560 assert_response :success
561 assert_template :terms
563 post :save, :user => { :consider_pd => true }
564 assert_response :redirect
565 assert_redirected_to :action => :account, :display_name => user.display_name
566 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
570 assert_equal true, user.consider_pd
571 assert_not_nil user.terms_agreed
572 assert_equal true, user.terms_seen
575 def test_terms_not_seen_with_referer
576 user = users(:terms_not_seen_user)
578 session[:user] = user.id
580 get :terms, :referer => "/test"
581 assert_response :success
582 assert_template :terms
584 post :save, :user => { :consider_pd => true }, :referer => "/test"
585 assert_response :redirect
586 assert_redirected_to "/test"
587 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
591 assert_equal true, user.consider_pd
592 assert_not_nil user.terms_agreed
593 assert_equal true, user.terms_seen
597 post :go_public, {}, { :user => users(:normal_user) }
598 assert_response :redirect
599 assert_redirected_to :action => :account, :display_name => users(:normal_user).display_name
600 assert_equal true, User.find(users(:normal_user).id).data_public
603 def test_lost_password
604 # Test fetching the lost password page
606 assert_response :success
607 assert_template :lost_password
608 assert_select "div#notice", false
610 # Test resetting using the address as recorded for a user that has an
611 # address which is duplicated in a different case by another user
612 assert_difference "ActionMailer::Base.deliveries.size", 1 do
613 post :lost_password, :user => { :email => users(:normal_user).email }
615 assert_response :redirect
616 assert_redirected_to :action => :login
617 assert_match /^Sorry you lost it/, flash[:notice]
618 email = ActionMailer::Base.deliveries.first
619 assert_equal 1, email.to.count
620 assert_equal users(:normal_user).email, email.to.first
621 ActionMailer::Base.deliveries.clear
623 # Test resetting using an address that matches a different user
624 # that has the same address in a different case
625 assert_difference "ActionMailer::Base.deliveries.size", 1 do
626 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
628 assert_response :redirect
629 assert_redirected_to :action => :login
630 assert_match /^Sorry you lost it/, flash[:notice]
631 email = ActionMailer::Base.deliveries.first
632 assert_equal 1, email.to.count
633 assert_equal users(:uppercase_user).email, email.to.first
634 ActionMailer::Base.deliveries.clear
636 # Test resetting using an address that is a case insensitive match
637 # for more than one user but not an exact match for either
638 assert_no_difference "ActionMailer::Base.deliveries.size" do
639 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
641 assert_response :success
642 assert_template :lost_password
643 assert_select ".error", /^Could not find that email address/
645 # Test resetting using the address as recorded for a user that has an
646 # address which is case insensitively unique
647 assert_difference "ActionMailer::Base.deliveries.size", 1 do
648 post :lost_password, :user => { :email => users(:public_user).email }
650 assert_response :redirect
651 assert_redirected_to :action => :login
652 assert_match /^Sorry you lost it/, flash[:notice]
653 email = ActionMailer::Base.deliveries.first
654 assert_equal 1, email.to.count
655 assert_equal users(:public_user).email, email.to.first
656 ActionMailer::Base.deliveries.clear
658 # Test resetting using an address that matches a user that has the
659 # same (case insensitively unique) address in a different case
660 assert_difference "ActionMailer::Base.deliveries.size", 1 do
661 post :lost_password, :user => { :email => users(:public_user).email.upcase }
663 assert_response :redirect
664 assert_redirected_to :action => :login
665 assert_match /^Sorry you lost it/, flash[:notice]
666 email = ActionMailer::Base.deliveries.first
667 assert_equal 1, email.to.count
668 assert_equal users(:public_user).email, email.to.first
669 ActionMailer::Base.deliveries.clear
672 def test_reset_password
673 # Test a request with no token
675 assert_response :bad_request
677 # Test a request with a bogus token
678 get :reset_password, :token => "made_up_token"
679 assert_response :redirect
680 assert_redirected_to :action => :lost_password
682 # Create a valid token for a user
683 token = User.find(users(:inactive_user).id).tokens.create
685 # Test a request with a valid token
686 get :reset_password, :token => token.token
687 assert_response :success
688 assert_template :reset_password
690 # Test setting a new password
691 post :reset_password, :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" }
692 assert_response :redirect
693 assert_redirected_to :action => :login
694 user = User.find(users(:inactive_user).id)
695 assert_equal "active", user.status
696 assert_equal true, user.email_valid
697 assert_equal user, User.authenticate(:username => "inactive@openstreetmap.org", :password => "new_password")
701 # Get a user to work with - note that this user deliberately
702 # conflicts with uppercase_user in the email and display name
703 # fields to test that we can change other fields without any
704 # validation errors being reported
705 user = users(:normal_user)
707 # Make sure that you are redirected to the login page when
708 # you are not logged in
709 get :account, :display_name => user.display_name
710 assert_response :redirect
711 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
713 # Make sure that you are blocked when not logged in as the right user
714 get :account, { :display_name => user.display_name }, { :user => users(:public_user).id }
715 assert_response :forbidden
717 # Make sure we get the page when we are logged in as the right user
718 get :account, { :display_name => user.display_name }, { :user => user }
719 assert_response :success
720 assert_template :account
722 # Updating the description should work
723 user.description = "new description"
724 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
725 assert_response :success
726 assert_template :account
727 assert_select "div#errorExplanation", false
728 assert_select ".notice", /^User information updated successfully/
729 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
731 # Changing to a invalid editor should fail
732 user.preferred_editor = "unknown"
733 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
734 assert_response :success
735 assert_template :account
736 assert_select ".notice", false
737 assert_select "div#errorExplanation"
738 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
740 # Changing to a valid editor should work
741 user.preferred_editor = "potlatch2"
742 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
743 assert_response :success
744 assert_template :account
745 assert_select "div#errorExplanation", false
746 assert_select ".notice", /^User information updated successfully/
747 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
749 # Changing to the default editor should work
750 user.preferred_editor = "default"
751 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
752 assert_response :success
753 assert_template :account
754 assert_select "div#errorExplanation", false
755 assert_select ".notice", /^User information updated successfully/
756 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
758 # Changing to an uploaded image should work
759 image = Rack::Test::UploadedFile.new("test/traces/1.gif", "image/gif")
760 post :account, { :display_name => user.display_name, :image_action => "new", :user => user.attributes.merge(:image => image) }, { :user => user.id }
761 assert_response :success
762 assert_template :account
763 assert_select "div#errorExplanation", false
764 assert_select ".notice", /^User information updated successfully/
765 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "keep"
767 # Changing to a gravatar image should work
768 post :account, { :display_name => user.display_name, :image_action => "gravatar", :user => user.attributes }, { :user => user.id }
769 assert_response :success
770 assert_template :account
771 assert_select "div#errorExplanation", false
772 assert_select ".notice", /^User information updated successfully/
773 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "gravatar"
775 # Removing the image should work
776 post :account, { :display_name => user.display_name, :image_action => "delete", :user => user.attributes }, { :user => user.id }
777 assert_response :success
778 assert_template :account
779 assert_select "div#errorExplanation", false
780 assert_select ".notice", /^User information updated successfully/
781 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked]", false
783 # Adding external authentication should redirect to the auth provider
784 post :account, { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, { :user => user.id }
785 assert_response :redirect
786 assert_redirected_to auth_path(:provider => "openid", :openid_url => "https://www.google.com/accounts/o8/id", :origin => "/user/#{user.display_name}/account")
788 # Changing name to one that exists should fail
789 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name)
790 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id }
791 assert_response :success
792 assert_template :account
793 assert_select ".notice", false
794 assert_select "div#errorExplanation"
795 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
797 # Changing name to one that exists should fail, regardless of case
798 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name.upcase)
799 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id }
800 assert_response :success
801 assert_template :account
802 assert_select ".notice", false
803 assert_select "div#errorExplanation"
804 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
806 # Changing name to one that doesn't exist should work
807 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
808 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id }
809 assert_response :success
810 assert_template :account
811 assert_select "div#errorExplanation", false
812 assert_select ".notice", /^User information updated successfully/
813 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
815 # Record the change of name
816 user.display_name = "new tester"
818 # Changing email to one that exists should fail
819 user.new_email = users(:public_user).email
820 assert_no_difference "ActionMailer::Base.deliveries.size" do
821 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
823 assert_response :success
824 assert_template :account
825 assert_select ".notice", false
826 assert_select "div#errorExplanation"
827 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
829 # Changing email to one that exists should fail, regardless of case
830 user.new_email = users(:public_user).email.upcase
831 assert_no_difference "ActionMailer::Base.deliveries.size" do
832 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
834 assert_response :success
835 assert_template :account
836 assert_select ".notice", false
837 assert_select "div#errorExplanation"
838 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
840 # Changing email to one that doesn't exist should work
841 user.new_email = "new_tester@example.com"
842 assert_difference "ActionMailer::Base.deliveries.size", 1 do
843 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
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 > input#user_new_email[value=?]", user.new_email
850 email = ActionMailer::Base.deliveries.first
851 assert_equal 1, email.to.count
852 assert_equal user.new_email, email.to.first
853 ActionMailer::Base.deliveries.clear
856 # Check that the user account page will display and contains some relevant
857 # information for the user
859 # Test a non-existent user
860 get :view, :display_name => "unknown"
861 assert_response :not_found
864 get :view, :display_name => "test"
865 assert_response :success
866 assert_select "div#userinformation" do
867 assert_select "a[href^='/user/test/history']", 1
868 assert_select "a[href='/user/test/traces']", 1
869 assert_select "a[href='/user/test/diary']", 1
870 assert_select "a[href='/user/test/diary/comments']", 1
871 assert_select "a[href='/user/test/account']", 0
872 assert_select "a[href='/user/test/blocks']", 0
873 assert_select "a[href='/user/test/blocks_by']", 0
874 assert_select "a[href='/blocks/new/test']", 0
877 # Test a user who has been blocked
878 get :view, :display_name => "blocked"
879 assert_response :success
880 assert_select "div#userinformation" do
881 assert_select "a[href^='/user/blocked/history']", 1
882 assert_select "a[href='/user/blocked/traces']", 1
883 assert_select "a[href='/user/blocked/diary']", 1
884 assert_select "a[href='/user/blocked/diary/comments']", 1
885 assert_select "a[href='/user/blocked/account']", 0
886 assert_select "a[href='/user/blocked/blocks']", 1
887 assert_select "a[href='/user/blocked/blocks_by']", 0
888 assert_select "a[href='/blocks/new/blocked']", 0
891 # Test a moderator who has applied blocks
892 get :view, :display_name => "moderator"
893 assert_response :success
894 assert_select "div#userinformation" do
895 assert_select "a[href^='/user/moderator/history']", 1
896 assert_select "a[href='/user/moderator/traces']", 1
897 assert_select "a[href='/user/moderator/diary']", 1
898 assert_select "a[href='/user/moderator/diary/comments']", 1
899 assert_select "a[href='/user/moderator/account']", 0
900 assert_select "a[href='/user/moderator/blocks']", 0
901 assert_select "a[href='/user/moderator/blocks_by']", 1
902 assert_select "a[href='/blocks/new/moderator']", 0
905 # Login as a normal user
906 session[:user] = users(:normal_user).id
908 # Test the normal user
909 get :view, :display_name => "test"
910 assert_response :success
911 assert_select "div#userinformation" do
912 assert_select "a[href^='/user/test/history']", 1
913 assert_select "a[href='/traces/mine']", 1
914 assert_select "a[href='/user/test/diary']", 1
915 assert_select "a[href='/user/test/diary/comments']", 1
916 assert_select "a[href='/user/test/account']", 1
917 assert_select "a[href='/user/test/blocks']", 0
918 assert_select "a[href='/user/test/blocks_by']", 0
919 assert_select "a[href='/blocks/new/test']", 0
922 # Login as a moderator
923 session[:user] = users(:moderator_user).id
925 # Test the normal user
926 get :view, :display_name => "test"
927 assert_response :success
928 assert_select "div#userinformation" do
929 assert_select "a[href^='/user/test/history']", 1
930 assert_select "a[href='/user/test/traces']", 1
931 assert_select "a[href='/user/test/diary']", 1
932 assert_select "a[href='/user/test/diary/comments']", 1
933 assert_select "a[href='/user/test/account']", 0
934 assert_select "a[href='/user/test/blocks']", 0
935 assert_select "a[href='/user/test/blocks_by']", 0
936 assert_select "a[href='/blocks/new/test']", 1
941 # check that a visible user is returned properly
942 get :api_read, :id => users(:normal_user).id
943 assert_response :success
944 assert_equal "text/xml", response.content_type
946 # check the data that is returned
947 assert_select "description", :count => 1, :text => "test"
948 assert_select "contributor-terms", :count => 1 do
949 assert_select "[agreed='true']"
951 assert_select "img", :count => 1
952 assert_select "roles", :count => 1 do
953 assert_select "role", :count => 0
955 assert_select "changesets", :count => 1 do
956 assert_select "[count='0']"
958 assert_select "traces", :count => 1 do
959 assert_select "[count='0']"
961 assert_select "blocks", :count => 1 do
962 assert_select "received", :count => 1 do
963 assert_select "[count='0'][active='0']"
965 assert_select "issued", :count => 0
968 # check that we aren't revealing private information
969 assert_select "contributor-terms[pd]", false
970 assert_select "home", false
971 assert_select "languages", false
972 assert_select "messages", false
974 # check that a suspended user is not returned
975 get :api_read, :id => users(:suspended_user).id
976 assert_response :gone
978 # check that a deleted user is not returned
979 get :api_read, :id => users(:deleted_user).id
980 assert_response :gone
982 # check that a non-existent user is not returned
983 get :api_read, :id => 0
984 assert_response :not_found
988 # check that nothing is returned when not logged in
990 assert_response :unauthorized
992 # check that we get a response when logged in
993 basic_authorization(users(:normal_user).email, "test")
995 assert_response :success
996 assert_equal "text/xml", response.content_type
998 # check the data that is returned
999 assert_select "description", :count => 1, :text => "test"
1000 assert_select "contributor-terms", :count => 1 do
1001 assert_select "[agreed='true'][pd='false']"
1003 assert_select "img", :count => 1
1004 assert_select "roles", :count => 1 do
1005 assert_select "role", :count => 0
1007 assert_select "changesets", :count => 1 do
1008 assert_select "[count='0']", :count => 1
1010 assert_select "traces", :count => 1 do
1011 assert_select "[count='0']", :count => 1
1013 assert_select "blocks", :count => 1 do
1014 assert_select "received", :count => 1 do
1015 assert_select "[count='0'][active='0']"
1017 assert_select "issued", :count => 0
1019 assert_select "home", :count => 1 do
1020 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
1022 assert_select "languages", :count => 1 do
1023 assert_select "lang", :count => 1, :text => "en"
1025 assert_select "messages", :count => 1 do
1026 assert_select "received", :count => 1 do
1027 assert_select "[count='1'][unread='0']"
1029 assert_select "sent", :count => 1 do
1030 assert_select "[count='1']"
1035 def test_api_gpx_files
1036 # check that nothing is returned when not logged in
1038 assert_response :unauthorized
1040 # check that we get a response when logged in
1041 basic_authorization(users(:normal_user).email, "test")
1043 assert_response :success
1044 assert_equal "text/xml", response.content_type
1046 # check the data that is returned
1047 assert_select "gpx_file[id='1']", 1 do
1048 assert_select "tag", "London"
1050 assert_select "gpx_file[id='4']", 1 do
1051 assert_select "tag", "Birmingham"
1055 def test_make_friend
1056 # Get users to work with
1057 user = users(:normal_user)
1058 friend = users(:second_public_user)
1060 # Check that the users aren't already friends
1061 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1063 # When not logged in a GET should ask us to login
1064 get :make_friend, :display_name => friend.display_name
1065 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1067 # When not logged in a POST should error
1068 post :make_friend, :display_name => friend.display_name
1069 assert_response :forbidden
1070 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1072 # When logged in a GET should get a confirmation page
1073 get :make_friend, { :display_name => friend.display_name }, { :user => user.id }
1074 assert_response :success
1075 assert_template :make_friend
1076 assert_select "form" do
1077 assert_select "input[type='hidden'][name='referer']", 0
1078 assert_select "input[type='submit']", 1
1080 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1082 # When logged in a POST should add the friendship
1083 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1084 post :make_friend, { :display_name => friend.display_name }, { :user => user.id }
1086 assert_redirected_to user_path(:display_name => friend.display_name)
1087 assert_match /is now your friend/, flash[:notice]
1088 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1089 email = ActionMailer::Base.deliveries.first
1090 assert_equal 1, email.to.count
1091 assert_equal friend.email, email.to.first
1092 ActionMailer::Base.deliveries.clear
1094 # A second POST should report that the friendship already exists
1095 assert_no_difference "ActionMailer::Base.deliveries.size" do
1096 post :make_friend, { :display_name => friend.display_name }, { :user => user.id }
1098 assert_redirected_to user_path(:display_name => friend.display_name)
1099 assert_match /You are already friends with/, flash[:warning]
1100 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1103 def test_make_friend_with_referer
1104 # Get users to work with
1105 user = users(:normal_user)
1106 friend = users(:second_public_user)
1108 # Check that the users aren't already friends
1109 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1111 # The GET should preserve any referer
1112 get :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1113 assert_response :success
1114 assert_template :make_friend
1115 assert_select "form" do
1116 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1117 assert_select "input[type='submit']", 1
1119 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1121 # When logged in a POST should add the friendship and refer us
1122 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1123 post :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1125 assert_redirected_to "/test"
1126 assert_match /is now your friend/, flash[:notice]
1127 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1128 email = ActionMailer::Base.deliveries.first
1129 assert_equal 1, email.to.count
1130 assert_equal friend.email, email.to.first
1131 ActionMailer::Base.deliveries.clear
1134 def test_make_friend_unkown_user
1135 # Should error when a bogus user is specified
1136 get :make_friend, { :display_name => "No Such User" }, { :user => users(:normal_user).id }
1137 assert_response :not_found
1138 assert_template :no_such_user
1141 def test_remove_friend
1142 # Get users to work with
1143 user = users(:normal_user)
1144 friend = users(:public_user)
1146 # Check that the users are friends
1147 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1149 # When not logged in a GET should ask us to login
1150 get :remove_friend, :display_name => friend.display_name
1151 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1153 # When not logged in a POST should error
1154 post :remove_friend, :display_name => friend.display_name
1155 assert_response :forbidden
1156 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1158 # When logged in a GET should get a confirmation page
1159 get :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1160 assert_response :success
1161 assert_template :remove_friend
1162 assert_select "form" do
1163 assert_select "input[type='hidden'][name='referer']", 0
1164 assert_select "input[type='submit']", 1
1166 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1168 # When logged in a POST should remove the friendship
1169 post :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1170 assert_redirected_to user_path(:display_name => friend.display_name)
1171 assert_match /was removed from your friends/, flash[:notice]
1172 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1174 # A second POST should report that the friendship does not exist
1175 post :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1176 assert_redirected_to user_path(:display_name => friend.display_name)
1177 assert_match /is not one of your friends/, flash[:error]
1178 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1181 def test_remove_friend_with_referer
1182 # Get users to work with
1183 user = users(:normal_user)
1184 friend = users(:public_user)
1186 # Check that the users are friends
1187 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1189 # The GET should preserve any referer
1190 get :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1191 assert_response :success
1192 assert_template :remove_friend
1193 assert_select "form" do
1194 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1195 assert_select "input[type='submit']", 1
1197 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1199 # When logged in a POST should remove the friendship and refer
1200 post :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1201 assert_redirected_to "/test"
1202 assert_match /was removed from your friends/, flash[:notice]
1203 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1206 def test_remove_friend_unkown_user
1207 # Should error when a bogus user is specified
1208 get :remove_friend, { :display_name => "No Such User" }, { :user => users(:normal_user).id }
1209 assert_response :not_found
1210 assert_template :no_such_user
1214 # Try without logging in
1215 get :set_status, :display_name => users(:normal_user).display_name, :status => "suspended"
1216 assert_response :redirect
1217 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1219 # Now try as a normal user
1220 get :set_status, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:normal_user).id }
1221 assert_response :redirect
1222 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1224 # Finally try as an administrator
1225 get :set_status, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:administrator_user).id }
1226 assert_response :redirect
1227 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1228 assert_equal "suspended", User.find(users(:normal_user).id).status
1232 # Try without logging in
1233 get :delete, :display_name => users(:normal_user).display_name, :status => "suspended"
1234 assert_response :redirect
1235 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1237 # Now try as a normal user
1238 get :delete, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:normal_user).id }
1239 assert_response :redirect
1240 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1242 # Finally try as an administrator
1243 get :delete, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:administrator_user).id }
1244 assert_response :redirect
1245 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1247 # Check that the user was deleted properly
1248 user = User.find(users(:normal_user).id)
1249 assert_equal "user_1", user.display_name
1250 assert_equal "", user.description
1251 assert_nil user.home_lat
1252 assert_nil user.home_lon
1253 assert_equal false, user.image.file?
1254 assert_equal false, user.email_valid
1255 assert_nil user.new_email
1256 assert_nil user.auth_provider
1257 assert_nil user.auth_uid
1258 assert_equal "deleted", user.status
1262 # Shouldn't work when not logged in
1264 assert_response :redirect
1265 assert_redirected_to :action => :login, :referer => users_path
1267 session[:user] = users(:normal_user).id
1269 # Shouldn't work when logged in as a normal user
1271 assert_response :redirect
1272 assert_redirected_to :action => :login, :referer => users_path
1274 session[:user] = users(:moderator_user).id
1276 # Shouldn't work when logged in as a moderator
1278 assert_response :redirect
1279 assert_redirected_to :action => :login, :referer => users_path
1281 session[:user] = users(:administrator_user).id
1283 # Should work when logged in as an administrator
1285 assert_response :success
1286 assert_template :list
1287 assert_select "table#user_list tr", :count => User.count + 1
1289 # Should be able to limit by status
1290 get :list, :status => "suspended"
1291 assert_response :success
1292 assert_template :list
1293 assert_select "table#user_list tr", :count => User.where(:status => "suspended").count + 1
1295 # Should be able to limit by IP address
1296 get :list, :ip => "1.2.3.4"
1297 assert_response :success
1298 assert_template :list
1299 assert_select "table#user_list tr", :count => User.where(:creation_ip => "1.2.3.4").count + 1
1302 def test_list_get_paginated
1303 1.upto(100).each do |n|
1304 User.create(:display_name => "extra_#{n}",
1305 :email => "extra#{n}@example.com",
1306 :pass_crypt => "extraextra")
1309 session[:user] = users(:administrator_user).id
1312 assert_response :success
1313 assert_template :list
1314 assert_select "table#user_list tr", :count => 51
1316 get :list, :page => 2
1317 assert_response :success
1318 assert_template :list
1319 assert_select "table#user_list tr", :count => 51
1321 get :list, :page => 3
1322 assert_response :success
1323 assert_template :list
1324 assert_select "table#user_list tr", :count => 20
1327 def test_list_post_confirm
1328 inactive_user = users(:inactive_user)
1329 suspended_user = users(:suspended_user)
1331 # Shouldn't work when not logged in
1332 assert_no_difference "User.active.count" do
1333 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1335 assert_response :redirect
1336 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1337 assert_equal "pending", inactive_user.reload.status
1338 assert_equal "suspended", suspended_user.reload.status
1340 session[:user] = users(:normal_user).id
1342 # Shouldn't work when logged in as a normal user
1343 assert_no_difference "User.active.count" do
1344 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1346 assert_response :redirect
1347 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1348 assert_equal "pending", inactive_user.reload.status
1349 assert_equal "suspended", suspended_user.reload.status
1351 session[:user] = users(:moderator_user).id
1353 # Shouldn't work when logged in as a moderator
1354 assert_no_difference "User.active.count" do
1355 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1357 assert_response :redirect
1358 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1359 assert_equal "pending", inactive_user.reload.status
1360 assert_equal "suspended", suspended_user.reload.status
1362 session[:user] = users(:administrator_user).id
1364 # Should work when logged in as an administrator
1365 assert_difference "User.active.count", 2 do
1366 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1368 assert_response :redirect
1369 assert_redirected_to :action => :list
1370 assert_equal "confirmed", inactive_user.reload.status
1371 assert_equal "confirmed", suspended_user.reload.status
1374 def test_list_post_hide
1375 normal_user = users(:normal_user)
1376 confirmed_user = users(:confirmed_user)
1378 # Shouldn't work when not logged in
1379 assert_no_difference "User.active.count" do
1380 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1382 assert_response :redirect
1383 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1384 assert_equal "active", normal_user.reload.status
1385 assert_equal "confirmed", confirmed_user.reload.status
1387 session[:user] = users(:normal_user).id
1389 # Shouldn't work when logged in as a normal user
1390 assert_no_difference "User.active.count" do
1391 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1393 assert_response :redirect
1394 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1395 assert_equal "active", normal_user.reload.status
1396 assert_equal "confirmed", confirmed_user.reload.status
1398 session[:user] = users(:moderator_user).id
1400 # Shouldn't work when logged in as a moderator
1401 assert_no_difference "User.active.count" do
1402 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1404 assert_response :redirect
1405 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1406 assert_equal "active", normal_user.reload.status
1407 assert_equal "confirmed", confirmed_user.reload.status
1409 session[:user] = users(:administrator_user).id
1411 # Should work when logged in as an administrator
1412 assert_difference "User.active.count", -2 do
1413 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1415 assert_response :redirect
1416 assert_redirected_to :action => :list
1417 assert_equal "deleted", normal_user.reload.status
1418 assert_equal "deleted", confirmed_user.reload.status
1425 user.status = "pending"
1426 user.display_name = "new_tester"
1427 user.email = "newtester@osm.org"
1428 user.email_confirmation = "newtester@osm.org"
1429 user.pass_crypt = "testtest"
1430 user.pass_crypt_confirmation = "testtest"