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 session[:token] = users(:inactive_user).tokens.create.token
479 assert_difference "ActionMailer::Base.deliveries.size", 1 do
480 get :confirm_resend, :display_name => users(:inactive_user).display_name
483 assert_response :redirect
484 assert_redirected_to login_path
485 assert_match /sent a new confirmation/, flash[:notice]
487 email = ActionMailer::Base.deliveries.last
489 assert_equal users(:inactive_user).email, email.to.first
491 ActionMailer::Base.deliveries.clear
494 def test_confirm_resend_no_token
495 assert_no_difference "ActionMailer::Base.deliveries.size" do
496 get :confirm_resend, :display_name => users(:inactive_user).display_name
499 assert_response :redirect
500 assert_redirected_to login_path
501 assert_match "User Inactive User not found.", flash[:error]
504 def test_confirm_resend_unknown_user
505 assert_no_difference "ActionMailer::Base.deliveries.size" do
506 get :confirm_resend, :display_name => "No Such User"
509 assert_response :redirect
510 assert_redirected_to login_path
511 assert_match "User No Such User not found.", flash[:error]
514 def test_confirm_email_get
515 user = users(:normal_user)
516 confirm_string = user.tokens.create.token
518 get :confirm_email, :confirm_string => confirm_string
519 assert_response :success
520 assert_template :confirm_email
523 def test_confirm_email_success
524 user = users(:second_public_user)
525 confirm_string = user.tokens.create.token
527 post :confirm_email, :confirm_string => confirm_string
528 assert_response :redirect
529 assert_redirected_to :action => :account, :display_name => user.display_name
530 assert_match /Confirmed your change of email address/, flash[:notice]
533 def test_confirm_email_already_confirmed
534 user = users(:normal_user)
535 confirm_string = user.tokens.create.token
537 post :confirm_email, :confirm_string => confirm_string
538 assert_response :redirect
539 assert_redirected_to :action => :account, :display_name => user.display_name
540 assert_match /already been confirmed/, flash[:error]
543 def test_confirm_email_bad_token
544 post :confirm_email, :confirm_string => "XXXXX"
545 assert_response :success
546 assert_template :confirm_email
547 assert_match /confirmation code has expired or does not exist/, flash[:error]
550 def test_terms_new_user
551 get :terms, {}, { :new_user => User.new }
552 assert_response :success
553 assert_template :terms
557 user = users(:normal_user)
559 session[:user] = user.id
562 assert_response :redirect
563 assert_redirected_to :action => :account, :display_name => user.display_name
566 def test_terms_not_seen_without_referer
567 user = users(:terms_not_seen_user)
569 session[:user] = user.id
572 assert_response :success
573 assert_template :terms
575 post :save, :user => { :consider_pd => true }
576 assert_response :redirect
577 assert_redirected_to :action => :account, :display_name => user.display_name
578 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
582 assert_equal true, user.consider_pd
583 assert_not_nil user.terms_agreed
584 assert_equal true, user.terms_seen
587 def test_terms_not_seen_with_referer
588 user = users(:terms_not_seen_user)
590 session[:user] = user.id
592 get :terms, :referer => "/test"
593 assert_response :success
594 assert_template :terms
596 post :save, :user => { :consider_pd => true }, :referer => "/test"
597 assert_response :redirect
598 assert_redirected_to "/test"
599 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
603 assert_equal true, user.consider_pd
604 assert_not_nil user.terms_agreed
605 assert_equal true, user.terms_seen
609 post :go_public, {}, { :user => users(:normal_user) }
610 assert_response :redirect
611 assert_redirected_to :action => :account, :display_name => users(:normal_user).display_name
612 assert_equal true, User.find(users(:normal_user).id).data_public
615 def test_lost_password
616 # Test fetching the lost password page
618 assert_response :success
619 assert_template :lost_password
620 assert_select "div#notice", false
622 # Test resetting using the address as recorded for a user that has an
623 # address which is duplicated in a different case by another user
624 assert_difference "ActionMailer::Base.deliveries.size", 1 do
625 post :lost_password, :user => { :email => users(:normal_user).email }
627 assert_response :redirect
628 assert_redirected_to :action => :login
629 assert_match /^Sorry you lost it/, flash[:notice]
630 email = ActionMailer::Base.deliveries.first
631 assert_equal 1, email.to.count
632 assert_equal users(:normal_user).email, email.to.first
633 ActionMailer::Base.deliveries.clear
635 # Test resetting using an address that matches a different user
636 # that has the same address in a different case
637 assert_difference "ActionMailer::Base.deliveries.size", 1 do
638 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
640 assert_response :redirect
641 assert_redirected_to :action => :login
642 assert_match /^Sorry you lost it/, flash[:notice]
643 email = ActionMailer::Base.deliveries.first
644 assert_equal 1, email.to.count
645 assert_equal users(:uppercase_user).email, email.to.first
646 ActionMailer::Base.deliveries.clear
648 # Test resetting using an address that is a case insensitive match
649 # for more than one user but not an exact match for either
650 assert_no_difference "ActionMailer::Base.deliveries.size" do
651 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
653 assert_response :success
654 assert_template :lost_password
655 assert_select ".error", /^Could not find that email address/
657 # Test resetting using the address as recorded for a user that has an
658 # address which is case insensitively unique
659 assert_difference "ActionMailer::Base.deliveries.size", 1 do
660 post :lost_password, :user => { :email => users(:public_user).email }
662 assert_response :redirect
663 assert_redirected_to :action => :login
664 assert_match /^Sorry you lost it/, flash[:notice]
665 email = ActionMailer::Base.deliveries.first
666 assert_equal 1, email.to.count
667 assert_equal users(:public_user).email, email.to.first
668 ActionMailer::Base.deliveries.clear
670 # Test resetting using an address that matches a user that has the
671 # same (case insensitively unique) address in a different case
672 assert_difference "ActionMailer::Base.deliveries.size", 1 do
673 post :lost_password, :user => { :email => users(:public_user).email.upcase }
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 users(:public_user).email, email.to.first
681 ActionMailer::Base.deliveries.clear
684 def test_reset_password
685 # Test a request with no token
687 assert_response :bad_request
689 # Test a request with a bogus token
690 get :reset_password, :token => "made_up_token"
691 assert_response :redirect
692 assert_redirected_to :action => :lost_password
694 # Create a valid token for a user
695 token = User.find(users(:inactive_user).id).tokens.create
697 # Test a request with a valid token
698 get :reset_password, :token => token.token
699 assert_response :success
700 assert_template :reset_password
702 # Test setting a new password
703 post :reset_password, :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" }
704 assert_response :redirect
705 assert_redirected_to :action => :login
706 user = User.find(users(:inactive_user).id)
707 assert_equal "active", user.status
708 assert_equal true, user.email_valid
709 assert_equal user, User.authenticate(:username => "inactive@openstreetmap.org", :password => "new_password")
713 # Get a user to work with - note that this user deliberately
714 # conflicts with uppercase_user in the email and display name
715 # fields to test that we can change other fields without any
716 # validation errors being reported
717 user = users(:normal_user)
719 # Make sure that you are redirected to the login page when
720 # you are not logged in
721 get :account, :display_name => user.display_name
722 assert_response :redirect
723 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
725 # Make sure that you are blocked when not logged in as the right user
726 get :account, { :display_name => user.display_name }, { :user => users(:public_user).id }
727 assert_response :forbidden
729 # Make sure we get the page when we are logged in as the right user
730 get :account, { :display_name => user.display_name }, { :user => user }
731 assert_response :success
732 assert_template :account
734 # Updating the description should work
735 user.description = "new description"
736 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
737 assert_response :success
738 assert_template :account
739 assert_select "div#errorExplanation", false
740 assert_select ".notice", /^User information updated successfully/
741 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
743 # Changing to a invalid editor should fail
744 user.preferred_editor = "unknown"
745 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
746 assert_response :success
747 assert_template :account
748 assert_select ".notice", false
749 assert_select "div#errorExplanation"
750 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
752 # Changing to a valid editor should work
753 user.preferred_editor = "potlatch2"
754 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
755 assert_response :success
756 assert_template :account
757 assert_select "div#errorExplanation", false
758 assert_select ".notice", /^User information updated successfully/
759 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
761 # Changing to the default editor should work
762 user.preferred_editor = "default"
763 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
764 assert_response :success
765 assert_template :account
766 assert_select "div#errorExplanation", false
767 assert_select ".notice", /^User information updated successfully/
768 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
770 # Changing to an uploaded image should work
771 image = Rack::Test::UploadedFile.new("test/traces/1.gif", "image/gif")
772 post :account, { :display_name => user.display_name, :image_action => "new", :user => user.attributes.merge(:image => image) }, { :user => user.id }
773 assert_response :success
774 assert_template :account
775 assert_select "div#errorExplanation", false
776 assert_select ".notice", /^User information updated successfully/
777 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "keep"
779 # Changing to a gravatar image should work
780 post :account, { :display_name => user.display_name, :image_action => "gravatar", :user => user.attributes }, { :user => user.id }
781 assert_response :success
782 assert_template :account
783 assert_select "div#errorExplanation", false
784 assert_select ".notice", /^User information updated successfully/
785 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "gravatar"
787 # Removing the image should work
788 post :account, { :display_name => user.display_name, :image_action => "delete", :user => user.attributes }, { :user => user.id }
789 assert_response :success
790 assert_template :account
791 assert_select "div#errorExplanation", false
792 assert_select ".notice", /^User information updated successfully/
793 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked]", false
795 # Adding external authentication should redirect to the auth provider
796 post :account, { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, { :user => user.id }
797 assert_response :redirect
798 assert_redirected_to auth_path(:provider => "openid", :openid_url => "https://www.google.com/accounts/o8/id", :origin => "/user/#{user.display_name}/account")
800 # Changing name to one that exists should fail
801 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name)
802 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id }
803 assert_response :success
804 assert_template :account
805 assert_select ".notice", false
806 assert_select "div#errorExplanation"
807 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
809 # Changing name to one that exists should fail, regardless of case
810 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name.upcase)
811 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id }
812 assert_response :success
813 assert_template :account
814 assert_select ".notice", false
815 assert_select "div#errorExplanation"
816 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
818 # Changing name to one that doesn't exist should work
819 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
820 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id }
821 assert_response :success
822 assert_template :account
823 assert_select "div#errorExplanation", false
824 assert_select ".notice", /^User information updated successfully/
825 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
827 # Record the change of name
828 user.display_name = "new tester"
830 # Changing email to one that exists should fail
831 user.new_email = users(:public_user).email
832 assert_no_difference "ActionMailer::Base.deliveries.size" do
833 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
835 assert_response :success
836 assert_template :account
837 assert_select ".notice", false
838 assert_select "div#errorExplanation"
839 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
841 # Changing email to one that exists should fail, regardless of case
842 user.new_email = users(:public_user).email.upcase
843 assert_no_difference "ActionMailer::Base.deliveries.size" do
844 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
846 assert_response :success
847 assert_template :account
848 assert_select ".notice", false
849 assert_select "div#errorExplanation"
850 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
852 # Changing email to one that doesn't exist should work
853 user.new_email = "new_tester@example.com"
854 assert_difference "ActionMailer::Base.deliveries.size", 1 do
855 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
857 assert_response :success
858 assert_template :account
859 assert_select "div#errorExplanation", false
860 assert_select ".notice", /^User information updated successfully/
861 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
862 email = ActionMailer::Base.deliveries.first
863 assert_equal 1, email.to.count
864 assert_equal user.new_email, email.to.first
865 ActionMailer::Base.deliveries.clear
868 # Check that the user account page will display and contains some relevant
869 # information for the user
871 # Test a non-existent user
872 get :view, :display_name => "unknown"
873 assert_response :not_found
876 get :view, :display_name => "test"
877 assert_response :success
878 assert_select "div#userinformation" do
879 assert_select "a[href^='/user/test/history']", 1
880 assert_select "a[href='/user/test/traces']", 1
881 assert_select "a[href='/user/test/diary']", 1
882 assert_select "a[href='/user/test/diary/comments']", 1
883 assert_select "a[href='/user/test/account']", 0
884 assert_select "a[href='/user/test/blocks']", 0
885 assert_select "a[href='/user/test/blocks_by']", 0
886 assert_select "a[href='/blocks/new/test']", 0
889 # Test a user who has been blocked
890 get :view, :display_name => "blocked"
891 assert_response :success
892 assert_select "div#userinformation" do
893 assert_select "a[href^='/user/blocked/history']", 1
894 assert_select "a[href='/user/blocked/traces']", 1
895 assert_select "a[href='/user/blocked/diary']", 1
896 assert_select "a[href='/user/blocked/diary/comments']", 1
897 assert_select "a[href='/user/blocked/account']", 0
898 assert_select "a[href='/user/blocked/blocks']", 1
899 assert_select "a[href='/user/blocked/blocks_by']", 0
900 assert_select "a[href='/blocks/new/blocked']", 0
903 # Test a moderator who has applied blocks
904 get :view, :display_name => "moderator"
905 assert_response :success
906 assert_select "div#userinformation" do
907 assert_select "a[href^='/user/moderator/history']", 1
908 assert_select "a[href='/user/moderator/traces']", 1
909 assert_select "a[href='/user/moderator/diary']", 1
910 assert_select "a[href='/user/moderator/diary/comments']", 1
911 assert_select "a[href='/user/moderator/account']", 0
912 assert_select "a[href='/user/moderator/blocks']", 0
913 assert_select "a[href='/user/moderator/blocks_by']", 1
914 assert_select "a[href='/blocks/new/moderator']", 0
917 # Login as a normal user
918 session[:user] = users(:normal_user).id
920 # Test the normal user
921 get :view, :display_name => "test"
922 assert_response :success
923 assert_select "div#userinformation" do
924 assert_select "a[href^='/user/test/history']", 1
925 assert_select "a[href='/traces/mine']", 1
926 assert_select "a[href='/user/test/diary']", 1
927 assert_select "a[href='/user/test/diary/comments']", 1
928 assert_select "a[href='/user/test/account']", 1
929 assert_select "a[href='/user/test/blocks']", 0
930 assert_select "a[href='/user/test/blocks_by']", 0
931 assert_select "a[href='/blocks/new/test']", 0
934 # Login as a moderator
935 session[:user] = users(:moderator_user).id
937 # Test the normal user
938 get :view, :display_name => "test"
939 assert_response :success
940 assert_select "div#userinformation" do
941 assert_select "a[href^='/user/test/history']", 1
942 assert_select "a[href='/user/test/traces']", 1
943 assert_select "a[href='/user/test/diary']", 1
944 assert_select "a[href='/user/test/diary/comments']", 1
945 assert_select "a[href='/user/test/account']", 0
946 assert_select "a[href='/user/test/blocks']", 0
947 assert_select "a[href='/user/test/blocks_by']", 0
948 assert_select "a[href='/blocks/new/test']", 1
953 # check that a visible user is returned properly
954 get :api_read, :id => users(:normal_user).id
955 assert_response :success
956 assert_equal "text/xml", response.content_type
958 # check the data that is returned
959 assert_select "description", :count => 1, :text => "test"
960 assert_select "contributor-terms", :count => 1 do
961 assert_select "[agreed='true']"
963 assert_select "img", :count => 1
964 assert_select "roles", :count => 1 do
965 assert_select "role", :count => 0
967 assert_select "changesets", :count => 1 do
968 assert_select "[count='0']"
970 assert_select "traces", :count => 1 do
971 assert_select "[count='0']"
973 assert_select "blocks", :count => 1 do
974 assert_select "received", :count => 1 do
975 assert_select "[count='0'][active='0']"
977 assert_select "issued", :count => 0
980 # check that we aren't revealing private information
981 assert_select "contributor-terms[pd]", false
982 assert_select "home", false
983 assert_select "languages", false
984 assert_select "messages", false
986 # check that a suspended user is not returned
987 get :api_read, :id => users(:suspended_user).id
988 assert_response :gone
990 # check that a deleted user is not returned
991 get :api_read, :id => users(:deleted_user).id
992 assert_response :gone
994 # check that a non-existent user is not returned
995 get :api_read, :id => 0
996 assert_response :not_found
1000 # check that nothing is returned when not logged in
1002 assert_response :unauthorized
1004 # check that we get a response when logged in
1005 basic_authorization(users(:normal_user).email, "test")
1007 assert_response :success
1008 assert_equal "text/xml", response.content_type
1010 # check the data that is returned
1011 assert_select "description", :count => 1, :text => "test"
1012 assert_select "contributor-terms", :count => 1 do
1013 assert_select "[agreed='true'][pd='false']"
1015 assert_select "img", :count => 1
1016 assert_select "roles", :count => 1 do
1017 assert_select "role", :count => 0
1019 assert_select "changesets", :count => 1 do
1020 assert_select "[count='0']", :count => 1
1022 assert_select "traces", :count => 1 do
1023 assert_select "[count='0']", :count => 1
1025 assert_select "blocks", :count => 1 do
1026 assert_select "received", :count => 1 do
1027 assert_select "[count='0'][active='0']"
1029 assert_select "issued", :count => 0
1031 assert_select "home", :count => 1 do
1032 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
1034 assert_select "languages", :count => 1 do
1035 assert_select "lang", :count => 1, :text => "en"
1037 assert_select "messages", :count => 1 do
1038 assert_select "received", :count => 1 do
1039 assert_select "[count='1'][unread='0']"
1041 assert_select "sent", :count => 1 do
1042 assert_select "[count='1']"
1047 def test_api_gpx_files
1048 # check that nothing is returned when not logged in
1050 assert_response :unauthorized
1052 # check that we get a response when logged in
1053 basic_authorization(users(:normal_user).email, "test")
1055 assert_response :success
1056 assert_equal "text/xml", response.content_type
1058 # check the data that is returned
1059 assert_select "gpx_file[id='1']", 1 do
1060 assert_select "tag", "London"
1062 assert_select "gpx_file[id='4']", 1 do
1063 assert_select "tag", "Birmingham"
1067 def test_make_friend
1068 # Get users to work with
1069 user = users(:normal_user)
1070 friend = users(:second_public_user)
1072 # Check that the users aren't already friends
1073 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1075 # When not logged in a GET should ask us to login
1076 get :make_friend, :display_name => friend.display_name
1077 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1079 # When not logged in a POST should error
1080 post :make_friend, :display_name => friend.display_name
1081 assert_response :forbidden
1082 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1084 # When logged in a GET should get a confirmation page
1085 get :make_friend, { :display_name => friend.display_name }, { :user => user.id }
1086 assert_response :success
1087 assert_template :make_friend
1088 assert_select "form" do
1089 assert_select "input[type='hidden'][name='referer']", 0
1090 assert_select "input[type='submit']", 1
1092 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1094 # When logged in a POST should add the friendship
1095 assert_difference "ActionMailer::Base.deliveries.size", 1 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 /is now your friend/, flash[:notice]
1100 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1101 email = ActionMailer::Base.deliveries.first
1102 assert_equal 1, email.to.count
1103 assert_equal friend.email, email.to.first
1104 ActionMailer::Base.deliveries.clear
1106 # A second POST should report that the friendship already exists
1107 assert_no_difference "ActionMailer::Base.deliveries.size" do
1108 post :make_friend, { :display_name => friend.display_name }, { :user => user.id }
1110 assert_redirected_to user_path(:display_name => friend.display_name)
1111 assert_match /You are already friends with/, flash[:warning]
1112 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1115 def test_make_friend_with_referer
1116 # Get users to work with
1117 user = users(:normal_user)
1118 friend = users(:second_public_user)
1120 # Check that the users aren't already friends
1121 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1123 # The GET should preserve any referer
1124 get :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1125 assert_response :success
1126 assert_template :make_friend
1127 assert_select "form" do
1128 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1129 assert_select "input[type='submit']", 1
1131 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1133 # When logged in a POST should add the friendship and refer us
1134 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1135 post :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1137 assert_redirected_to "/test"
1138 assert_match /is now your friend/, flash[:notice]
1139 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1140 email = ActionMailer::Base.deliveries.first
1141 assert_equal 1, email.to.count
1142 assert_equal friend.email, email.to.first
1143 ActionMailer::Base.deliveries.clear
1146 def test_make_friend_unkown_user
1147 # Should error when a bogus user is specified
1148 get :make_friend, { :display_name => "No Such User" }, { :user => users(:normal_user).id }
1149 assert_response :not_found
1150 assert_template :no_such_user
1153 def test_remove_friend
1154 # Get users to work with
1155 user = users(:normal_user)
1156 friend = users(:public_user)
1158 # Check that the users are friends
1159 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1161 # When not logged in a GET should ask us to login
1162 get :remove_friend, :display_name => friend.display_name
1163 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1165 # When not logged in a POST should error
1166 post :remove_friend, :display_name => friend.display_name
1167 assert_response :forbidden
1168 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1170 # When logged in a GET should get a confirmation page
1171 get :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1172 assert_response :success
1173 assert_template :remove_friend
1174 assert_select "form" do
1175 assert_select "input[type='hidden'][name='referer']", 0
1176 assert_select "input[type='submit']", 1
1178 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1180 # When logged in a POST should remove the friendship
1181 post :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1182 assert_redirected_to user_path(:display_name => friend.display_name)
1183 assert_match /was removed from your friends/, flash[:notice]
1184 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1186 # A second POST should report that the friendship does not exist
1187 post :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1188 assert_redirected_to user_path(:display_name => friend.display_name)
1189 assert_match /is not one of your friends/, flash[:error]
1190 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1193 def test_remove_friend_with_referer
1194 # Get users to work with
1195 user = users(:normal_user)
1196 friend = users(:public_user)
1198 # Check that the users are friends
1199 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1201 # The GET should preserve any referer
1202 get :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1203 assert_response :success
1204 assert_template :remove_friend
1205 assert_select "form" do
1206 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1207 assert_select "input[type='submit']", 1
1209 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1211 # When logged in a POST should remove the friendship and refer
1212 post :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1213 assert_redirected_to "/test"
1214 assert_match /was removed from your friends/, flash[:notice]
1215 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1218 def test_remove_friend_unkown_user
1219 # Should error when a bogus user is specified
1220 get :remove_friend, { :display_name => "No Such User" }, { :user => users(:normal_user).id }
1221 assert_response :not_found
1222 assert_template :no_such_user
1226 # Try without logging in
1227 get :set_status, :display_name => users(:normal_user).display_name, :status => "suspended"
1228 assert_response :redirect
1229 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1231 # Now try as a normal user
1232 get :set_status, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:normal_user).id }
1233 assert_response :redirect
1234 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1236 # Finally try as an administrator
1237 get :set_status, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:administrator_user).id }
1238 assert_response :redirect
1239 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1240 assert_equal "suspended", User.find(users(:normal_user).id).status
1244 # Try without logging in
1245 get :delete, :display_name => users(:normal_user).display_name, :status => "suspended"
1246 assert_response :redirect
1247 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1249 # Now try as a normal user
1250 get :delete, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:normal_user).id }
1251 assert_response :redirect
1252 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1254 # Finally try as an administrator
1255 get :delete, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:administrator_user).id }
1256 assert_response :redirect
1257 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1259 # Check that the user was deleted properly
1260 user = User.find(users(:normal_user).id)
1261 assert_equal "user_1", user.display_name
1262 assert_equal "", user.description
1263 assert_nil user.home_lat
1264 assert_nil user.home_lon
1265 assert_equal false, user.image.file?
1266 assert_equal false, user.email_valid
1267 assert_nil user.new_email
1268 assert_nil user.auth_provider
1269 assert_nil user.auth_uid
1270 assert_equal "deleted", user.status
1274 # Shouldn't work when not logged in
1276 assert_response :redirect
1277 assert_redirected_to :action => :login, :referer => users_path
1279 session[:user] = users(:normal_user).id
1281 # Shouldn't work when logged in as a normal user
1283 assert_response :redirect
1284 assert_redirected_to :action => :login, :referer => users_path
1286 session[:user] = users(:moderator_user).id
1288 # Shouldn't work when logged in as a moderator
1290 assert_response :redirect
1291 assert_redirected_to :action => :login, :referer => users_path
1293 session[:user] = users(:administrator_user).id
1295 # Should work when logged in as an administrator
1297 assert_response :success
1298 assert_template :list
1299 assert_select "table#user_list tr", :count => User.count + 1
1301 # Should be able to limit by status
1302 get :list, :status => "suspended"
1303 assert_response :success
1304 assert_template :list
1305 assert_select "table#user_list tr", :count => User.where(:status => "suspended").count + 1
1307 # Should be able to limit by IP address
1308 get :list, :ip => "1.2.3.4"
1309 assert_response :success
1310 assert_template :list
1311 assert_select "table#user_list tr", :count => User.where(:creation_ip => "1.2.3.4").count + 1
1314 def test_list_get_paginated
1315 1.upto(100).each do |n|
1316 User.create(:display_name => "extra_#{n}",
1317 :email => "extra#{n}@example.com",
1318 :pass_crypt => "extraextra")
1321 session[:user] = users(:administrator_user).id
1324 assert_response :success
1325 assert_template :list
1326 assert_select "table#user_list tr", :count => 51
1328 get :list, :page => 2
1329 assert_response :success
1330 assert_template :list
1331 assert_select "table#user_list tr", :count => 51
1333 get :list, :page => 3
1334 assert_response :success
1335 assert_template :list
1336 assert_select "table#user_list tr", :count => 22
1339 def test_list_post_confirm
1340 inactive_user = users(:inactive_user)
1341 suspended_user = users(:suspended_user)
1343 # Shouldn't work when not logged in
1344 assert_no_difference "User.active.count" do
1345 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1347 assert_response :redirect
1348 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1349 assert_equal "pending", inactive_user.reload.status
1350 assert_equal "suspended", suspended_user.reload.status
1352 session[:user] = users(:normal_user).id
1354 # Shouldn't work when logged in as a normal user
1355 assert_no_difference "User.active.count" do
1356 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1358 assert_response :redirect
1359 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1360 assert_equal "pending", inactive_user.reload.status
1361 assert_equal "suspended", suspended_user.reload.status
1363 session[:user] = users(:moderator_user).id
1365 # Shouldn't work when logged in as a moderator
1366 assert_no_difference "User.active.count" do
1367 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1369 assert_response :redirect
1370 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1371 assert_equal "pending", inactive_user.reload.status
1372 assert_equal "suspended", suspended_user.reload.status
1374 session[:user] = users(:administrator_user).id
1376 # Should work when logged in as an administrator
1377 assert_difference "User.active.count", 2 do
1378 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1380 assert_response :redirect
1381 assert_redirected_to :action => :list
1382 assert_equal "confirmed", inactive_user.reload.status
1383 assert_equal "confirmed", suspended_user.reload.status
1386 def test_list_post_hide
1387 normal_user = users(:normal_user)
1388 confirmed_user = users(:confirmed_user)
1390 # Shouldn't work when not logged in
1391 assert_no_difference "User.active.count" do
1392 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1394 assert_response :redirect
1395 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1396 assert_equal "active", normal_user.reload.status
1397 assert_equal "confirmed", confirmed_user.reload.status
1399 session[:user] = users(:normal_user).id
1401 # Shouldn't work when logged in as a normal user
1402 assert_no_difference "User.active.count" do
1403 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1405 assert_response :redirect
1406 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1407 assert_equal "active", normal_user.reload.status
1408 assert_equal "confirmed", confirmed_user.reload.status
1410 session[:user] = users(:moderator_user).id
1412 # Shouldn't work when logged in as a moderator
1413 assert_no_difference "User.active.count" do
1414 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1416 assert_response :redirect
1417 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1418 assert_equal "active", normal_user.reload.status
1419 assert_equal "confirmed", confirmed_user.reload.status
1421 session[:user] = users(:administrator_user).id
1423 # Should work when logged in as an administrator
1424 assert_difference "User.active.count", -2 do
1425 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1427 assert_response :redirect
1428 assert_redirected_to :action => :list
1429 assert_equal "deleted", normal_user.reload.status
1430 assert_equal "deleted", confirmed_user.reload.status
1437 user.status = "pending"
1438 user.display_name = "new_tester"
1439 user.email = "newtester@osm.org"
1440 user.email_confirmation = "newtester@osm.org"
1441 user.pass_crypt = "testtest"
1442 user.pass_crypt_confirmation = "testtest"