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 root_path
706 assert_equal users(:inactive_user).id, session[:user]
707 user = User.find(users(:inactive_user).id)
708 assert_equal "active", user.status
709 assert_equal true, user.email_valid
710 assert_equal user, User.authenticate(:username => "inactive@openstreetmap.org", :password => "new_password")
714 # Get a user to work with - note that this user deliberately
715 # conflicts with uppercase_user in the email and display name
716 # fields to test that we can change other fields without any
717 # validation errors being reported
718 user = users(:normal_user)
720 # Make sure that you are redirected to the login page when
721 # you are not logged in
722 get :account, :display_name => user.display_name
723 assert_response :redirect
724 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
726 # Make sure that you are blocked when not logged in as the right user
727 get :account, { :display_name => user.display_name }, { :user => users(:public_user).id }
728 assert_response :forbidden
730 # Make sure we get the page when we are logged in as the right user
731 get :account, { :display_name => user.display_name }, { :user => user }
732 assert_response :success
733 assert_template :account
735 # Updating the description should work
736 user.description = "new description"
737 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
738 assert_response :success
739 assert_template :account
740 assert_select "div#errorExplanation", false
741 assert_select ".notice", /^User information updated successfully/
742 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
744 # Changing to a invalid editor should fail
745 user.preferred_editor = "unknown"
746 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
747 assert_response :success
748 assert_template :account
749 assert_select ".notice", false
750 assert_select "div#errorExplanation"
751 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
753 # Changing to a valid editor should work
754 user.preferred_editor = "potlatch2"
755 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
756 assert_response :success
757 assert_template :account
758 assert_select "div#errorExplanation", false
759 assert_select ".notice", /^User information updated successfully/
760 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
762 # Changing to the default editor should work
763 user.preferred_editor = "default"
764 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
765 assert_response :success
766 assert_template :account
767 assert_select "div#errorExplanation", false
768 assert_select ".notice", /^User information updated successfully/
769 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
771 # Changing to an uploaded image should work
772 image = Rack::Test::UploadedFile.new("test/traces/1.gif", "image/gif")
773 post :account, { :display_name => user.display_name, :image_action => "new", :user => user.attributes.merge(:image => image) }, { :user => user.id }
774 assert_response :success
775 assert_template :account
776 assert_select "div#errorExplanation", false
777 assert_select ".notice", /^User information updated successfully/
778 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "keep"
780 # Changing to a gravatar image should work
781 post :account, { :display_name => user.display_name, :image_action => "gravatar", :user => user.attributes }, { :user => user.id }
782 assert_response :success
783 assert_template :account
784 assert_select "div#errorExplanation", false
785 assert_select ".notice", /^User information updated successfully/
786 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "gravatar"
788 # Removing the image should work
789 post :account, { :display_name => user.display_name, :image_action => "delete", :user => user.attributes }, { :user => user.id }
790 assert_response :success
791 assert_template :account
792 assert_select "div#errorExplanation", false
793 assert_select ".notice", /^User information updated successfully/
794 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked]", false
796 # Adding external authentication should redirect to the auth provider
797 post :account, { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, { :user => user.id }
798 assert_response :redirect
799 assert_redirected_to auth_path(:provider => "openid", :openid_url => "https://www.google.com/accounts/o8/id", :origin => "/user/#{user.display_name}/account")
801 # Changing name to one that exists should fail
802 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name)
803 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id }
804 assert_response :success
805 assert_template :account
806 assert_select ".notice", false
807 assert_select "div#errorExplanation"
808 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
810 # Changing name to one that exists should fail, regardless of case
811 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name.upcase)
812 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id }
813 assert_response :success
814 assert_template :account
815 assert_select ".notice", false
816 assert_select "div#errorExplanation"
817 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
819 # Changing name to one that doesn't exist should work
820 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
821 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id }
822 assert_response :success
823 assert_template :account
824 assert_select "div#errorExplanation", false
825 assert_select ".notice", /^User information updated successfully/
826 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
828 # Record the change of name
829 user.display_name = "new tester"
831 # Changing email to one that exists should fail
832 user.new_email = users(:public_user).email
833 assert_no_difference "ActionMailer::Base.deliveries.size" do
834 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
836 assert_response :success
837 assert_template :account
838 assert_select ".notice", false
839 assert_select "div#errorExplanation"
840 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
842 # Changing email to one that exists should fail, regardless of case
843 user.new_email = users(:public_user).email.upcase
844 assert_no_difference "ActionMailer::Base.deliveries.size" do
845 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
847 assert_response :success
848 assert_template :account
849 assert_select ".notice", false
850 assert_select "div#errorExplanation"
851 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
853 # Changing email to one that doesn't exist should work
854 user.new_email = "new_tester@example.com"
855 assert_difference "ActionMailer::Base.deliveries.size", 1 do
856 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
858 assert_response :success
859 assert_template :account
860 assert_select "div#errorExplanation", false
861 assert_select ".notice", /^User information updated successfully/
862 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
863 email = ActionMailer::Base.deliveries.first
864 assert_equal 1, email.to.count
865 assert_equal user.new_email, email.to.first
866 ActionMailer::Base.deliveries.clear
869 # Check that the user account page will display and contains some relevant
870 # information for the user
872 # Test a non-existent user
873 get :view, :display_name => "unknown"
874 assert_response :not_found
877 get :view, :display_name => "test"
878 assert_response :success
879 assert_select "div#userinformation" do
880 assert_select "a[href^='/user/test/history']", 1
881 assert_select "a[href='/user/test/traces']", 1
882 assert_select "a[href='/user/test/diary']", 1
883 assert_select "a[href='/user/test/diary/comments']", 1
884 assert_select "a[href='/user/test/account']", 0
885 assert_select "a[href='/user/test/blocks']", 0
886 assert_select "a[href='/user/test/blocks_by']", 0
887 assert_select "a[href='/blocks/new/test']", 0
890 # Test a user who has been blocked
891 get :view, :display_name => "blocked"
892 assert_response :success
893 assert_select "div#userinformation" do
894 assert_select "a[href^='/user/blocked/history']", 1
895 assert_select "a[href='/user/blocked/traces']", 1
896 assert_select "a[href='/user/blocked/diary']", 1
897 assert_select "a[href='/user/blocked/diary/comments']", 1
898 assert_select "a[href='/user/blocked/account']", 0
899 assert_select "a[href='/user/blocked/blocks']", 1
900 assert_select "a[href='/user/blocked/blocks_by']", 0
901 assert_select "a[href='/blocks/new/blocked']", 0
904 # Test a moderator who has applied blocks
905 get :view, :display_name => "moderator"
906 assert_response :success
907 assert_select "div#userinformation" do
908 assert_select "a[href^='/user/moderator/history']", 1
909 assert_select "a[href='/user/moderator/traces']", 1
910 assert_select "a[href='/user/moderator/diary']", 1
911 assert_select "a[href='/user/moderator/diary/comments']", 1
912 assert_select "a[href='/user/moderator/account']", 0
913 assert_select "a[href='/user/moderator/blocks']", 0
914 assert_select "a[href='/user/moderator/blocks_by']", 1
915 assert_select "a[href='/blocks/new/moderator']", 0
918 # Login as a normal user
919 session[:user] = users(:normal_user).id
921 # Test the normal user
922 get :view, :display_name => "test"
923 assert_response :success
924 assert_select "div#userinformation" do
925 assert_select "a[href^='/user/test/history']", 1
926 assert_select "a[href='/traces/mine']", 1
927 assert_select "a[href='/user/test/diary']", 1
928 assert_select "a[href='/user/test/diary/comments']", 1
929 assert_select "a[href='/user/test/account']", 1
930 assert_select "a[href='/user/test/blocks']", 0
931 assert_select "a[href='/user/test/blocks_by']", 0
932 assert_select "a[href='/blocks/new/test']", 0
935 # Login as a moderator
936 session[:user] = users(:moderator_user).id
938 # Test the normal user
939 get :view, :display_name => "test"
940 assert_response :success
941 assert_select "div#userinformation" do
942 assert_select "a[href^='/user/test/history']", 1
943 assert_select "a[href='/user/test/traces']", 1
944 assert_select "a[href='/user/test/diary']", 1
945 assert_select "a[href='/user/test/diary/comments']", 1
946 assert_select "a[href='/user/test/account']", 0
947 assert_select "a[href='/user/test/blocks']", 0
948 assert_select "a[href='/user/test/blocks_by']", 0
949 assert_select "a[href='/blocks/new/test']", 1
954 # check that a visible user is returned properly
955 get :api_read, :id => users(:normal_user).id
956 assert_response :success
957 assert_equal "text/xml", response.content_type
959 # check the data that is returned
960 assert_select "description", :count => 1, :text => "test"
961 assert_select "contributor-terms", :count => 1 do
962 assert_select "[agreed='true']"
964 assert_select "img", :count => 1
965 assert_select "roles", :count => 1 do
966 assert_select "role", :count => 0
968 assert_select "changesets", :count => 1 do
969 assert_select "[count='0']"
971 assert_select "traces", :count => 1 do
972 assert_select "[count='0']"
974 assert_select "blocks", :count => 1 do
975 assert_select "received", :count => 1 do
976 assert_select "[count='0'][active='0']"
978 assert_select "issued", :count => 0
981 # check that we aren't revealing private information
982 assert_select "contributor-terms[pd]", false
983 assert_select "home", false
984 assert_select "languages", false
985 assert_select "messages", false
987 # check that a suspended user is not returned
988 get :api_read, :id => users(:suspended_user).id
989 assert_response :gone
991 # check that a deleted user is not returned
992 get :api_read, :id => users(:deleted_user).id
993 assert_response :gone
995 # check that a non-existent user is not returned
996 get :api_read, :id => 0
997 assert_response :not_found
1000 def test_api_details
1001 # check that nothing is returned when not logged in
1003 assert_response :unauthorized
1005 # check that we get a response when logged in
1006 basic_authorization(users(:normal_user).email, "test")
1008 assert_response :success
1009 assert_equal "text/xml", response.content_type
1011 # check the data that is returned
1012 assert_select "description", :count => 1, :text => "test"
1013 assert_select "contributor-terms", :count => 1 do
1014 assert_select "[agreed='true'][pd='false']"
1016 assert_select "img", :count => 1
1017 assert_select "roles", :count => 1 do
1018 assert_select "role", :count => 0
1020 assert_select "changesets", :count => 1 do
1021 assert_select "[count='0']", :count => 1
1023 assert_select "traces", :count => 1 do
1024 assert_select "[count='0']", :count => 1
1026 assert_select "blocks", :count => 1 do
1027 assert_select "received", :count => 1 do
1028 assert_select "[count='0'][active='0']"
1030 assert_select "issued", :count => 0
1032 assert_select "home", :count => 1 do
1033 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
1035 assert_select "languages", :count => 1 do
1036 assert_select "lang", :count => 1, :text => "en"
1038 assert_select "messages", :count => 1 do
1039 assert_select "received", :count => 1 do
1040 assert_select "[count='1'][unread='0']"
1042 assert_select "sent", :count => 1 do
1043 assert_select "[count='1']"
1048 def test_api_gpx_files
1049 # check that nothing is returned when not logged in
1051 assert_response :unauthorized
1053 # check that we get a response when logged in
1054 basic_authorization(users(:normal_user).email, "test")
1056 assert_response :success
1057 assert_equal "text/xml", response.content_type
1059 # check the data that is returned
1060 assert_select "gpx_file[id='1']", 1 do
1061 assert_select "tag", "London"
1063 assert_select "gpx_file[id='4']", 1 do
1064 assert_select "tag", "Birmingham"
1068 def test_make_friend
1069 # Get users to work with
1070 user = users(:normal_user)
1071 friend = users(:second_public_user)
1073 # Check that the users aren't already friends
1074 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1076 # When not logged in a GET should ask us to login
1077 get :make_friend, :display_name => friend.display_name
1078 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1080 # When not logged in a POST should error
1081 post :make_friend, :display_name => friend.display_name
1082 assert_response :forbidden
1083 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1085 # When logged in a GET should get a confirmation page
1086 get :make_friend, { :display_name => friend.display_name }, { :user => user.id }
1087 assert_response :success
1088 assert_template :make_friend
1089 assert_select "form" do
1090 assert_select "input[type='hidden'][name='referer']", 0
1091 assert_select "input[type='submit']", 1
1093 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1095 # When logged in a POST should add the friendship
1096 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1097 post :make_friend, { :display_name => friend.display_name }, { :user => user.id }
1099 assert_redirected_to user_path(:display_name => friend.display_name)
1100 assert_match /is now your friend/, flash[:notice]
1101 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1102 email = ActionMailer::Base.deliveries.first
1103 assert_equal 1, email.to.count
1104 assert_equal friend.email, email.to.first
1105 ActionMailer::Base.deliveries.clear
1107 # A second POST should report that the friendship already exists
1108 assert_no_difference "ActionMailer::Base.deliveries.size" do
1109 post :make_friend, { :display_name => friend.display_name }, { :user => user.id }
1111 assert_redirected_to user_path(:display_name => friend.display_name)
1112 assert_match /You are already friends with/, flash[:warning]
1113 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1116 def test_make_friend_with_referer
1117 # Get users to work with
1118 user = users(:normal_user)
1119 friend = users(:second_public_user)
1121 # Check that the users aren't already friends
1122 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1124 # The GET should preserve any referer
1125 get :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1126 assert_response :success
1127 assert_template :make_friend
1128 assert_select "form" do
1129 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1130 assert_select "input[type='submit']", 1
1132 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1134 # When logged in a POST should add the friendship and refer us
1135 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1136 post :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1138 assert_redirected_to "/test"
1139 assert_match /is now your friend/, flash[:notice]
1140 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1141 email = ActionMailer::Base.deliveries.first
1142 assert_equal 1, email.to.count
1143 assert_equal friend.email, email.to.first
1144 ActionMailer::Base.deliveries.clear
1147 def test_make_friend_unkown_user
1148 # Should error when a bogus user is specified
1149 get :make_friend, { :display_name => "No Such User" }, { :user => users(:normal_user).id }
1150 assert_response :not_found
1151 assert_template :no_such_user
1154 def test_remove_friend
1155 # Get users to work with
1156 user = users(:normal_user)
1157 friend = users(:public_user)
1159 # Check that the users are friends
1160 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1162 # When not logged in a GET should ask us to login
1163 get :remove_friend, :display_name => friend.display_name
1164 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1166 # When not logged in a POST should error
1167 post :remove_friend, :display_name => friend.display_name
1168 assert_response :forbidden
1169 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1171 # When logged in a GET should get a confirmation page
1172 get :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1173 assert_response :success
1174 assert_template :remove_friend
1175 assert_select "form" do
1176 assert_select "input[type='hidden'][name='referer']", 0
1177 assert_select "input[type='submit']", 1
1179 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1181 # When logged in a POST should remove the friendship
1182 post :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1183 assert_redirected_to user_path(:display_name => friend.display_name)
1184 assert_match /was removed from your friends/, flash[:notice]
1185 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1187 # A second POST should report that the friendship does not exist
1188 post :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1189 assert_redirected_to user_path(:display_name => friend.display_name)
1190 assert_match /is not one of your friends/, flash[:error]
1191 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1194 def test_remove_friend_with_referer
1195 # Get users to work with
1196 user = users(:normal_user)
1197 friend = users(:public_user)
1199 # Check that the users are friends
1200 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1202 # The GET should preserve any referer
1203 get :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1204 assert_response :success
1205 assert_template :remove_friend
1206 assert_select "form" do
1207 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1208 assert_select "input[type='submit']", 1
1210 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1212 # When logged in a POST should remove the friendship and refer
1213 post :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1214 assert_redirected_to "/test"
1215 assert_match /was removed from your friends/, flash[:notice]
1216 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1219 def test_remove_friend_unkown_user
1220 # Should error when a bogus user is specified
1221 get :remove_friend, { :display_name => "No Such User" }, { :user => users(:normal_user).id }
1222 assert_response :not_found
1223 assert_template :no_such_user
1227 # Try without logging in
1228 get :set_status, :display_name => users(:normal_user).display_name, :status => "suspended"
1229 assert_response :redirect
1230 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1232 # Now try as a normal user
1233 get :set_status, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:normal_user).id }
1234 assert_response :redirect
1235 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1237 # Finally try as an administrator
1238 get :set_status, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:administrator_user).id }
1239 assert_response :redirect
1240 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1241 assert_equal "suspended", User.find(users(:normal_user).id).status
1245 # Try without logging in
1246 get :delete, :display_name => users(:normal_user).display_name, :status => "suspended"
1247 assert_response :redirect
1248 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1250 # Now try as a normal user
1251 get :delete, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:normal_user).id }
1252 assert_response :redirect
1253 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1255 # Finally try as an administrator
1256 get :delete, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:administrator_user).id }
1257 assert_response :redirect
1258 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1260 # Check that the user was deleted properly
1261 user = User.find(users(:normal_user).id)
1262 assert_equal "user_1", user.display_name
1263 assert_equal "", user.description
1264 assert_nil user.home_lat
1265 assert_nil user.home_lon
1266 assert_equal false, user.image.file?
1267 assert_equal false, user.email_valid
1268 assert_nil user.new_email
1269 assert_nil user.auth_provider
1270 assert_nil user.auth_uid
1271 assert_equal "deleted", user.status
1275 # Shouldn't work when not logged in
1277 assert_response :redirect
1278 assert_redirected_to :action => :login, :referer => users_path
1280 session[:user] = users(:normal_user).id
1282 # Shouldn't work when logged in as a normal user
1284 assert_response :redirect
1285 assert_redirected_to :action => :login, :referer => users_path
1287 session[:user] = users(:moderator_user).id
1289 # Shouldn't work when logged in as a moderator
1291 assert_response :redirect
1292 assert_redirected_to :action => :login, :referer => users_path
1294 session[:user] = users(:administrator_user).id
1296 # Should work when logged in as an administrator
1298 assert_response :success
1299 assert_template :list
1300 assert_select "table#user_list tr", :count => User.count + 1
1302 # Should be able to limit by status
1303 get :list, :status => "suspended"
1304 assert_response :success
1305 assert_template :list
1306 assert_select "table#user_list tr", :count => User.where(:status => "suspended").count + 1
1308 # Should be able to limit by IP address
1309 get :list, :ip => "1.2.3.4"
1310 assert_response :success
1311 assert_template :list
1312 assert_select "table#user_list tr", :count => User.where(:creation_ip => "1.2.3.4").count + 1
1315 def test_list_get_paginated
1316 1.upto(100).each do |n|
1317 User.create(:display_name => "extra_#{n}",
1318 :email => "extra#{n}@example.com",
1319 :pass_crypt => "extraextra")
1322 session[:user] = users(:administrator_user).id
1325 assert_response :success
1326 assert_template :list
1327 assert_select "table#user_list tr", :count => 51
1329 get :list, :page => 2
1330 assert_response :success
1331 assert_template :list
1332 assert_select "table#user_list tr", :count => 51
1334 get :list, :page => 3
1335 assert_response :success
1336 assert_template :list
1337 assert_select "table#user_list tr", :count => 22
1340 def test_list_post_confirm
1341 inactive_user = users(:inactive_user)
1342 suspended_user = users(:suspended_user)
1344 # Shouldn't work when not logged in
1345 assert_no_difference "User.active.count" do
1346 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1348 assert_response :redirect
1349 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1350 assert_equal "pending", inactive_user.reload.status
1351 assert_equal "suspended", suspended_user.reload.status
1353 session[:user] = users(:normal_user).id
1355 # Shouldn't work when logged in as a normal user
1356 assert_no_difference "User.active.count" do
1357 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1359 assert_response :redirect
1360 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1361 assert_equal "pending", inactive_user.reload.status
1362 assert_equal "suspended", suspended_user.reload.status
1364 session[:user] = users(:moderator_user).id
1366 # Shouldn't work when logged in as a moderator
1367 assert_no_difference "User.active.count" do
1368 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1370 assert_response :redirect
1371 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1372 assert_equal "pending", inactive_user.reload.status
1373 assert_equal "suspended", suspended_user.reload.status
1375 session[:user] = users(:administrator_user).id
1377 # Should work when logged in as an administrator
1378 assert_difference "User.active.count", 2 do
1379 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1381 assert_response :redirect
1382 assert_redirected_to :action => :list
1383 assert_equal "confirmed", inactive_user.reload.status
1384 assert_equal "confirmed", suspended_user.reload.status
1387 def test_list_post_hide
1388 normal_user = users(:normal_user)
1389 confirmed_user = users(:confirmed_user)
1391 # Shouldn't work when not logged in
1392 assert_no_difference "User.active.count" do
1393 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1395 assert_response :redirect
1396 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1397 assert_equal "active", normal_user.reload.status
1398 assert_equal "confirmed", confirmed_user.reload.status
1400 session[:user] = users(:normal_user).id
1402 # Shouldn't work when logged in as a normal user
1403 assert_no_difference "User.active.count" do
1404 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1406 assert_response :redirect
1407 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1408 assert_equal "active", normal_user.reload.status
1409 assert_equal "confirmed", confirmed_user.reload.status
1411 session[:user] = users(:moderator_user).id
1413 # Shouldn't work when logged in as a moderator
1414 assert_no_difference "User.active.count" do
1415 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1417 assert_response :redirect
1418 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1419 assert_equal "active", normal_user.reload.status
1420 assert_equal "confirmed", confirmed_user.reload.status
1422 session[:user] = users(:administrator_user).id
1424 # Should work when logged in as an administrator
1425 assert_difference "User.active.count", -2 do
1426 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1428 assert_response :redirect
1429 assert_redirected_to :action => :list
1430 assert_equal "deleted", normal_user.reload.status
1431 assert_equal "deleted", confirmed_user.reload.status
1438 user.status = "pending"
1439 user.display_name = "new_tester"
1440 user.email = "newtester@osm.org"
1441 user.email_confirmation = "newtester@osm.org"
1442 user.pass_crypt = "testtest"
1443 user.pass_crypt_confirmation = "testtest"