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
215 assert_difference "User.count", 1 do
216 assert_difference "ActionMailer::Base.deliveries.size", 1 do
217 post :save, {}, { :new_user => user }
222 register_email = ActionMailer::Base.deliveries.first
224 assert_equal register_email.to[0], user.email
225 assert_match /#{@url}/, register_email.body.to_s
228 assert_redirected_to :action => "confirm", :display_name => user.display_name
230 ActionMailer::Base.deliveries.clear
233 def test_new_duplicate_email
235 user.email = users(:public_user).email
237 assert_no_difference "User.count" do
238 assert_no_difference "ActionMailer::Base.deliveries.size" do
239 post :save, {}, { :new_user => user }
243 assert_response :success
244 assert_template "new"
245 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
248 def test_new_duplicate_email_uppercase
250 user.email = users(:public_user).email.upcase
252 assert_no_difference "User.count" do
253 assert_no_difference "ActionMailer::Base.deliveries.size" do
254 post :save, {}, { :new_user => user }
258 assert_response :success
259 assert_template "new"
260 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
263 def test_new_duplicate_name
265 user.display_name = users(:public_user).display_name
267 assert_no_difference "User.count" do
268 assert_no_difference "ActionMailer::Base.deliveries.size" do
269 post :save, {}, { :new_user => user }
273 assert_response :success
274 assert_template "new"
275 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
278 def test_new_duplicate_name_uppercase
280 user.display_name = users(:public_user).display_name.upcase
282 assert_no_difference "User.count" do
283 assert_no_difference "ActionMailer::Base.deliveries.size" do
284 post :save, {}, { :new_user => user }
288 assert_response :success
289 assert_template "new"
290 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
293 def test_save_referer_params
296 assert_difference "User.count", 1 do
297 assert_difference "ActionMailer::Base.deliveries.size", 1 do
298 post :save, {}, { :new_user => user,
299 :referer => "/edit?editor=id#map=1/2/3" }
303 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
304 user.tokens.order("id DESC").first.referer
306 ActionMailer::Base.deliveries.clear
309 def test_logout_without_referer
311 assert_response :success
312 assert_template :logout
313 assert_select "input[name=referer][value=?]", ""
315 session_id = assert_select("input[name=session]").first["value"]
317 get :logout, :session => session_id
318 assert_response :redirect
319 assert_redirected_to root_path
322 def test_logout_with_referer
323 get :logout, :referer => "/test"
324 assert_response :success
325 assert_template :logout
326 assert_select "input[name=referer][value=?]", "/test"
328 session_id = assert_select("input[name=session]").first["value"]
330 get :logout, :session => session_id, :referer => "/test"
331 assert_response :redirect
332 assert_redirected_to "/test"
336 user = users(:inactive_user)
337 confirm_string = user.tokens.create.token
339 @request.cookies["_osm_session"] = user.display_name
340 get :confirm, :display_name => user.display_name, :confirm_string => confirm_string
341 assert_response :success
342 assert_template :confirm
345 def test_confirm_get_already_confirmed
346 user = users(:normal_user)
347 confirm_string = user.tokens.create.token
349 @request.cookies["_osm_session"] = user.display_name
350 get :confirm, :display_name => user.display_name, :confirm_string => confirm_string
351 assert_response :redirect
352 assert_redirected_to root_path
355 def test_confirm_success_no_token_no_referer
356 user = users(:inactive_user)
357 confirm_string = user.tokens.create.token
359 @request.cookies["_osm_session"] = user.display_name
360 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
361 assert_redirected_to login_path
362 assert_match /Confirmed your account/, flash[:notice]
365 def test_confirm_success_good_token_no_referer
366 user = users(:inactive_user)
367 confirm_string = user.tokens.create.token
368 token = user.tokens.create.token
370 @request.cookies["_osm_session"] = user.display_name
371 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
372 assert_redirected_to welcome_path
375 def test_confirm_success_bad_token_no_referer
376 user = users(:inactive_user)
377 confirm_string = user.tokens.create.token
378 token = users(:normal_user).tokens.create.token
380 @request.cookies["_osm_session"] = user.display_name
381 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
382 assert_redirected_to login_path
383 assert_match /Confirmed your account/, flash[:notice]
386 def test_confirm_success_no_token_with_referer
387 user = users(:inactive_user)
388 confirm_string = user.tokens.create(:referer => diary_new_path).token
390 @request.cookies["_osm_session"] = user.display_name
391 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
392 assert_redirected_to login_path(:referer => diary_new_path)
393 assert_match /Confirmed your account/, flash[:notice]
396 def test_confirm_success_good_token_with_referer
397 user = users(:inactive_user)
398 confirm_string = user.tokens.create(:referer => diary_new_path).token
399 token = user.tokens.create.token
401 @request.cookies["_osm_session"] = user.display_name
402 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
403 assert_redirected_to diary_new_path
406 def test_confirm_success_bad_token_with_referer
407 user = users(:inactive_user)
408 confirm_string = user.tokens.create(:referer => diary_new_path).token
409 token = users(:normal_user).tokens.create.token
411 @request.cookies["_osm_session"] = user.display_name
412 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
413 assert_redirected_to login_path(:referer => diary_new_path)
414 assert_match /Confirmed your account/, flash[:notice]
417 def test_confirm_expired_token
418 user = users(:inactive_user)
419 confirm_string = user.tokens.create(:expiry => 1.day.ago).token
421 @request.cookies["_osm_session"] = user.display_name
422 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
423 assert_redirected_to :action => "confirm"
424 assert_match /confirmation code has expired/, flash[:error]
427 def test_confirm_already_confirmed
428 user = users(:normal_user)
429 confirm_string = user.tokens.create(:referer => diary_new_path).token
431 @request.cookies["_osm_session"] = user.display_name
432 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
433 assert_redirected_to :action => "login"
434 assert_match /already been confirmed/, flash[:error]
437 def test_confirm_resend_success
438 assert_difference "ActionMailer::Base.deliveries.size", 1 do
439 get :confirm_resend, :display_name => users(:inactive_user).display_name
442 assert_response :redirect
443 assert_redirected_to login_path
444 assert_match /sent a new confirmation/, flash[:notice]
446 email = ActionMailer::Base.deliveries.last
448 assert_equal users(:inactive_user).email, email.to.first
450 ActionMailer::Base.deliveries.clear
453 def test_confirm_resend_failure
454 assert_no_difference "ActionMailer::Base.deliveries.size" do
455 get :confirm_resend, :display_name => "No Such User"
458 assert_response :redirect
459 assert_redirected_to login_path
460 assert_match "User No Such User not found.", flash[:error]
463 def test_confirm_email_get
464 user = users(:normal_user)
465 confirm_string = user.tokens.create.token
467 get :confirm_email, :confirm_string => confirm_string
468 assert_response :success
469 assert_template :confirm_email
472 def test_confirm_email_success
473 user = users(:second_public_user)
474 confirm_string = user.tokens.create.token
476 post :confirm_email, :confirm_string => confirm_string
477 assert_response :redirect
478 assert_redirected_to :action => :account, :display_name => user.display_name
479 assert_match /Confirmed your change of email address/, flash[:notice]
482 def test_confirm_email_already_confirmed
483 user = users(:normal_user)
484 confirm_string = user.tokens.create.token
486 post :confirm_email, :confirm_string => confirm_string
487 assert_response :redirect
488 assert_redirected_to :action => :account, :display_name => user.display_name
489 assert_match /already been confirmed/, flash[:error]
492 def test_confirm_email_bad_token
493 post :confirm_email, :confirm_string => "XXXXX"
494 assert_response :success
495 assert_template :confirm_email
496 assert_match /confirmation code has expired or does not exist/, flash[:error]
499 def test_terms_new_user
500 get :terms, {}, { :new_user => User.new }
501 assert_response :success
502 assert_template :terms
506 user = users(:normal_user)
508 get :terms, {}, { :user => user }
509 assert_response :redirect
510 assert_redirected_to :action => :account, :display_name => user.display_name
513 def test_terms_not_seen
514 user = users(:terms_not_seen_user)
516 get :terms, {}, { :user => user }
517 assert_response :success
518 assert_template :terms
522 post :go_public, {}, { :user => users(:normal_user) }
523 assert_response :redirect
524 assert_redirected_to :action => :account, :display_name => users(:normal_user).display_name
525 assert_equal true, User.find(users(:normal_user).id).data_public
528 def test_lost_password
529 # Test fetching the lost password page
531 assert_response :success
532 assert_template :lost_password
533 assert_select "div#notice", false
535 # Test resetting using the address as recorded for a user that has an
536 # address which is duplicated in a different case by another user
537 assert_difference "ActionMailer::Base.deliveries.size", 1 do
538 post :lost_password, :user => { :email => users(:normal_user).email }
540 assert_response :redirect
541 assert_redirected_to :action => :login
542 assert_match /^Sorry you lost it/, flash[:notice]
543 email = ActionMailer::Base.deliveries.first
544 assert_equal 1, email.to.count
545 assert_equal users(:normal_user).email, email.to.first
546 ActionMailer::Base.deliveries.clear
548 # Test resetting using an address that matches a different user
549 # that has the same address in a different case
550 assert_difference "ActionMailer::Base.deliveries.size", 1 do
551 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
553 assert_response :redirect
554 assert_redirected_to :action => :login
555 assert_match /^Sorry you lost it/, flash[:notice]
556 email = ActionMailer::Base.deliveries.first
557 assert_equal 1, email.to.count
558 assert_equal users(:uppercase_user).email, email.to.first
559 ActionMailer::Base.deliveries.clear
561 # Test resetting using an address that is a case insensitive match
562 # for more than one user but not an exact match for either
563 assert_no_difference "ActionMailer::Base.deliveries.size" do
564 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
566 assert_response :success
567 assert_template :lost_password
568 assert_select ".error", /^Could not find that email address/
570 # Test resetting using the address as recorded for a user that has an
571 # address which is case insensitively unique
572 assert_difference "ActionMailer::Base.deliveries.size", 1 do
573 post :lost_password, :user => { :email => users(:public_user).email }
575 assert_response :redirect
576 assert_redirected_to :action => :login
577 assert_match /^Sorry you lost it/, flash[:notice]
578 email = ActionMailer::Base.deliveries.first
579 assert_equal 1, email.to.count
580 assert_equal users(:public_user).email, email.to.first
581 ActionMailer::Base.deliveries.clear
583 # Test resetting using an address that matches a user that has the
584 # same (case insensitively unique) address in a different case
585 assert_difference "ActionMailer::Base.deliveries.size", 1 do
586 post :lost_password, :user => { :email => users(:public_user).email.upcase }
588 assert_response :redirect
589 assert_redirected_to :action => :login
590 assert_match /^Sorry you lost it/, flash[:notice]
591 email = ActionMailer::Base.deliveries.first
592 assert_equal 1, email.to.count
593 assert_equal users(:public_user).email, email.to.first
594 ActionMailer::Base.deliveries.clear
597 def test_reset_password
598 # Test a request with no token
600 assert_response :bad_request
602 # Test a request with a bogus token
603 get :reset_password, :token => "made_up_token"
604 assert_response :redirect
605 assert_redirected_to :action => :lost_password
607 # Create a valid token for a user
608 token = User.find(users(:inactive_user).id).tokens.create
610 # Test a request with a valid token
611 get :reset_password, :token => token.token
612 assert_response :success
613 assert_template :reset_password
615 # Test setting a new password
616 post :reset_password, :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" }
617 assert_response :redirect
618 assert_redirected_to :action => :login
619 user = User.find(users(:inactive_user).id)
620 assert_equal "active", user.status
621 assert_equal true, user.email_valid
622 assert_equal user, User.authenticate(:username => "inactive@openstreetmap.org", :password => "new_password")
626 # Get a user to work with - note that this user deliberately
627 # conflicts with uppercase_user in the email and display name
628 # fields to test that we can change other fields without any
629 # validation errors being reported
630 user = users(:normal_user)
632 # Make sure that you are redirected to the login page when
633 # you are not logged in
634 get :account, :display_name => user.display_name
635 assert_response :redirect
636 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
638 # Make sure that you are blocked when not logged in as the right user
639 get :account, { :display_name => user.display_name }, { :user => users(:public_user).id }
640 assert_response :forbidden
642 # Make sure we get the page when we are logged in as the right user
643 get :account, { :display_name => user.display_name }, { :user => user }
644 assert_response :success
645 assert_template :account
647 # Updating the description should work
648 user.description = "new description"
649 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
650 assert_response :success
651 assert_template :account
652 assert_select "div#errorExplanation", false
653 assert_select ".notice", /^User information updated successfully/
654 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
656 # Changing to a invalid editor should fail
657 user.preferred_editor = "unknown"
658 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
659 assert_response :success
660 assert_template :account
661 assert_select ".notice", false
662 assert_select "div#errorExplanation"
663 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
665 # Changing to a valid editor should work
666 user.preferred_editor = "potlatch2"
667 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
668 assert_response :success
669 assert_template :account
670 assert_select "div#errorExplanation", false
671 assert_select ".notice", /^User information updated successfully/
672 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
674 # Changing to the default editor should work
675 user.preferred_editor = "default"
676 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
677 assert_response :success
678 assert_template :account
679 assert_select "div#errorExplanation", false
680 assert_select ".notice", /^User information updated successfully/
681 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
683 # Changing to an uploaded image should work
684 image = Rack::Test::UploadedFile.new("test/traces/1.gif", "image/gif")
685 post :account, { :display_name => user.display_name, :image_action => "new", :user => user.attributes.merge(:image => image) }, { :user => user.id }
686 assert_response :success
687 assert_template :account
688 assert_select "div#errorExplanation", false
689 assert_select ".notice", /^User information updated successfully/
690 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "keep"
692 # Changing to a gravatar image should work
693 post :account, { :display_name => user.display_name, :image_action => "gravatar", :user => user.attributes }, { :user => user.id }
694 assert_response :success
695 assert_template :account
696 assert_select "div#errorExplanation", false
697 assert_select ".notice", /^User information updated successfully/
698 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "gravatar"
700 # Removing the image should work
701 post :account, { :display_name => user.display_name, :image_action => "delete", :user => user.attributes }, { :user => user.id }
702 assert_response :success
703 assert_template :account
704 assert_select "div#errorExplanation", false
705 assert_select ".notice", /^User information updated successfully/
706 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked]", false
708 # Changing name to one that exists should fail
709 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name)
710 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id }
711 assert_response :success
712 assert_template :account
713 assert_select ".notice", false
714 assert_select "div#errorExplanation"
715 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
717 # Changing name to one that exists should fail, regardless of case
718 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name.upcase)
719 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id }
720 assert_response :success
721 assert_template :account
722 assert_select ".notice", false
723 assert_select "div#errorExplanation"
724 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
726 # Changing name to one that doesn't exist should work
727 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
728 post :account, { :display_name => user.display_name, :user => new_attributes }, { :user => user.id }
729 assert_response :success
730 assert_template :account
731 assert_select "div#errorExplanation", false
732 assert_select ".notice", /^User information updated successfully/
733 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
735 # Record the change of name
736 user.display_name = "new tester"
738 # Changing email to one that exists should fail
739 user.new_email = users(:public_user).email
740 assert_no_difference "ActionMailer::Base.deliveries.size" do
741 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
743 assert_response :success
744 assert_template :account
745 assert_select ".notice", false
746 assert_select "div#errorExplanation"
747 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
749 # Changing email to one that exists should fail, regardless of case
750 user.new_email = users(:public_user).email.upcase
751 assert_no_difference "ActionMailer::Base.deliveries.size" do
752 post :account, { :display_name => user.display_name, :user => user.attributes }, { :user => user.id }
754 assert_response :success
755 assert_template :account
756 assert_select ".notice", false
757 assert_select "div#errorExplanation"
758 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
760 # Changing email to one that doesn't exist should work
761 user.new_email = "new_tester@example.com"
762 assert_difference "ActionMailer::Base.deliveries.size", 1 do
763 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 > input#user_new_email[value=?]", user.new_email
770 email = ActionMailer::Base.deliveries.first
771 assert_equal 1, email.to.count
772 assert_equal user.new_email, email.to.first
773 ActionMailer::Base.deliveries.clear
776 # Check that the user account page will display and contains some relevant
777 # information for the user
779 # Test a non-existent user
780 get :view, :display_name => "unknown"
781 assert_response :not_found
784 get :view, :display_name => "test"
785 assert_response :success
786 assert_select "div#userinformation" do
787 assert_select "a[href^='/user/test/history']", 1
788 assert_select "a[href='/user/test/traces']", 1
789 assert_select "a[href='/user/test/diary']", 1
790 assert_select "a[href='/user/test/diary/comments']", 1
791 assert_select "a[href='/user/test/account']", 0
792 assert_select "a[href='/user/test/blocks']", 0
793 assert_select "a[href='/user/test/blocks_by']", 0
794 assert_select "a[href='/blocks/new/test']", 0
797 # Test a user who has been blocked
798 get :view, :display_name => "blocked"
799 assert_response :success
800 assert_select "div#userinformation" do
801 assert_select "a[href^='/user/blocked/history']", 1
802 assert_select "a[href='/user/blocked/traces']", 1
803 assert_select "a[href='/user/blocked/diary']", 1
804 assert_select "a[href='/user/blocked/diary/comments']", 1
805 assert_select "a[href='/user/blocked/account']", 0
806 assert_select "a[href='/user/blocked/blocks']", 1
807 assert_select "a[href='/user/blocked/blocks_by']", 0
808 assert_select "a[href='/blocks/new/blocked']", 0
811 # Test a moderator who has applied blocks
812 get :view, :display_name => "moderator"
813 assert_response :success
814 assert_select "div#userinformation" do
815 assert_select "a[href^='/user/moderator/history']", 1
816 assert_select "a[href='/user/moderator/traces']", 1
817 assert_select "a[href='/user/moderator/diary']", 1
818 assert_select "a[href='/user/moderator/diary/comments']", 1
819 assert_select "a[href='/user/moderator/account']", 0
820 assert_select "a[href='/user/moderator/blocks']", 0
821 assert_select "a[href='/user/moderator/blocks_by']", 1
822 assert_select "a[href='/blocks/new/moderator']", 0
825 # Login as a normal user
826 session[:user] = users(:normal_user).id
828 # Test the normal user
829 get :view, :display_name => "test"
830 assert_response :success
831 assert_select "div#userinformation" do
832 assert_select "a[href^='/user/test/history']", 1
833 assert_select "a[href='/traces/mine']", 1
834 assert_select "a[href='/user/test/diary']", 1
835 assert_select "a[href='/user/test/diary/comments']", 1
836 assert_select "a[href='/user/test/account']", 1
837 assert_select "a[href='/user/test/blocks']", 0
838 assert_select "a[href='/user/test/blocks_by']", 0
839 assert_select "a[href='/blocks/new/test']", 0
842 # Login as a moderator
843 session[:user] = users(:moderator_user).id
845 # Test the normal user
846 get :view, :display_name => "test"
847 assert_response :success
848 assert_select "div#userinformation" do
849 assert_select "a[href^='/user/test/history']", 1
850 assert_select "a[href='/user/test/traces']", 1
851 assert_select "a[href='/user/test/diary']", 1
852 assert_select "a[href='/user/test/diary/comments']", 1
853 assert_select "a[href='/user/test/account']", 0
854 assert_select "a[href='/user/test/blocks']", 0
855 assert_select "a[href='/user/test/blocks_by']", 0
856 assert_select "a[href='/blocks/new/test']", 1
861 # check that a visible user is returned properly
862 get :api_read, :id => users(:normal_user).id
863 assert_response :success
864 assert_equal "text/xml", response.content_type
866 # check the data that is returned
867 assert_select "description", :count => 1, :text => "test"
868 assert_select "contributor-terms", :count => 1 do
869 assert_select "[agreed='true']"
871 assert_select "img", :count => 1
872 assert_select "roles", :count => 1 do
873 assert_select "role", :count => 0
875 assert_select "changesets", :count => 1 do
876 assert_select "[count='0']"
878 assert_select "traces", :count => 1 do
879 assert_select "[count='0']"
881 assert_select "blocks", :count => 1 do
882 assert_select "received", :count => 1 do
883 assert_select "[count='0'][active='0']"
885 assert_select "issued", :count => 0
888 # check that we aren't revealing private information
889 assert_select "contributor-terms[pd]", false
890 assert_select "home", false
891 assert_select "languages", false
892 assert_select "messages", false
894 # check that a suspended user is not returned
895 get :api_read, :id => users(:suspended_user).id
896 assert_response :gone
898 # check that a deleted user is not returned
899 get :api_read, :id => users(:deleted_user).id
900 assert_response :gone
902 # check that a non-existent user is not returned
903 get :api_read, :id => 0
904 assert_response :not_found
908 # check that nothing is returned when not logged in
910 assert_response :unauthorized
912 # check that we get a response when logged in
913 basic_authorization(users(:normal_user).email, "test")
915 assert_response :success
916 assert_equal "text/xml", response.content_type
918 # check the data that is returned
919 assert_select "description", :count => 1, :text => "test"
920 assert_select "contributor-terms", :count => 1 do
921 assert_select "[agreed='true'][pd='false']"
923 assert_select "img", :count => 1
924 assert_select "roles", :count => 1 do
925 assert_select "role", :count => 0
927 assert_select "changesets", :count => 1 do
928 assert_select "[count='0']", :count => 1
930 assert_select "traces", :count => 1 do
931 assert_select "[count='0']", :count => 1
933 assert_select "blocks", :count => 1 do
934 assert_select "received", :count => 1 do
935 assert_select "[count='0'][active='0']"
937 assert_select "issued", :count => 0
939 assert_select "home", :count => 1 do
940 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
942 assert_select "languages", :count => 1 do
943 assert_select "lang", :count => 1, :text => "en"
945 assert_select "messages", :count => 1 do
946 assert_select "received", :count => 1 do
947 assert_select "[count='1'][unread='0']"
949 assert_select "sent", :count => 1 do
950 assert_select "[count='1']"
955 def test_api_gpx_files
956 # check that nothing is returned when not logged in
958 assert_response :unauthorized
960 # check that we get a response when logged in
961 basic_authorization(users(:normal_user).email, "test")
963 assert_response :success
964 assert_equal "text/xml", response.content_type
966 # check the data that is returned
967 assert_select "gpx_file[id='1']", 1 do
968 assert_select "tag", "London"
970 assert_select "gpx_file[id='4']", 1 do
971 assert_select "tag", "Birmingham"
976 # Get users to work with
977 user = users(:normal_user)
978 friend = users(:second_public_user)
980 # Check that the users aren't already friends
981 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
983 # When not logged in a GET should ask us to login
984 get :make_friend, :display_name => friend.display_name
985 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
987 # When not logged in a POST should error
988 post :make_friend, :display_name => friend.display_name
989 assert_response :forbidden
990 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
992 # When logged in a GET should get a confirmation page
993 get :make_friend, { :display_name => friend.display_name }, { :user => user.id }
994 assert_response :success
995 assert_template :make_friend
996 assert_select "form" do
997 assert_select "input[type='hidden'][name='referer']", 0
998 assert_select "input[type='submit']", 1
1000 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1002 # When logged in a POST should add the friendship
1003 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1004 post :make_friend, { :display_name => friend.display_name }, { :user => user.id }
1006 assert_redirected_to user_path(:display_name => friend.display_name)
1007 assert_match /is now your friend/, flash[:notice]
1008 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1009 email = ActionMailer::Base.deliveries.first
1010 assert_equal 1, email.to.count
1011 assert_equal friend.email, email.to.first
1012 ActionMailer::Base.deliveries.clear
1014 # A second POST should report that the friendship already exists
1015 assert_no_difference "ActionMailer::Base.deliveries.size" do
1016 post :make_friend, { :display_name => friend.display_name }, { :user => user.id }
1018 assert_redirected_to user_path(:display_name => friend.display_name)
1019 assert_match /You are already friends with/, flash[:warning]
1020 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1023 def test_make_friend_with_referer
1024 # Get users to work with
1025 user = users(:normal_user)
1026 friend = users(:second_public_user)
1028 # Check that the users aren't already friends
1029 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1031 # The GET should preserve any referer
1032 get :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1033 assert_response :success
1034 assert_template :make_friend
1035 assert_select "form" do
1036 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1037 assert_select "input[type='submit']", 1
1039 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1041 # When logged in a POST should add the friendship and refer us
1042 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1043 post :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1045 assert_redirected_to "/test"
1046 assert_match /is now your friend/, flash[:notice]
1047 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1048 email = ActionMailer::Base.deliveries.first
1049 assert_equal 1, email.to.count
1050 assert_equal friend.email, email.to.first
1051 ActionMailer::Base.deliveries.clear
1054 def test_make_friend_unkown_user
1055 # Should error when a bogus user is specified
1056 get :make_friend, { :display_name => "No Such User" }, { :user => users(:normal_user).id }
1057 assert_response :not_found
1058 assert_template :no_such_user
1061 def test_remove_friend
1062 # Get users to work with
1063 user = users(:normal_user)
1064 friend = users(:public_user)
1066 # Check that the users are friends
1067 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1069 # When not logged in a GET should ask us to login
1070 get :remove_friend, :display_name => friend.display_name
1071 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1073 # When not logged in a POST should error
1074 post :remove_friend, :display_name => friend.display_name
1075 assert_response :forbidden
1076 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1078 # When logged in a GET should get a confirmation page
1079 get :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1080 assert_response :success
1081 assert_template :remove_friend
1082 assert_select "form" do
1083 assert_select "input[type='hidden'][name='referer']", 0
1084 assert_select "input[type='submit']", 1
1086 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1088 # When logged in a POST should remove the friendship
1089 post :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1090 assert_redirected_to user_path(:display_name => friend.display_name)
1091 assert_match /was removed from your friends/, flash[:notice]
1092 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1094 # A second POST should report that the friendship does not exist
1095 post :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1096 assert_redirected_to user_path(:display_name => friend.display_name)
1097 assert_match /is not one of your friends/, flash[:error]
1098 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1101 def test_remove_friend_with_referer
1102 # Get users to work with
1103 user = users(:normal_user)
1104 friend = users(:public_user)
1106 # Check that the users are friends
1107 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1109 # The GET should preserve any referer
1110 get :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1111 assert_response :success
1112 assert_template :remove_friend
1113 assert_select "form" do
1114 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1115 assert_select "input[type='submit']", 1
1117 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1119 # When logged in a POST should remove the friendship and refer
1120 post :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1121 assert_redirected_to "/test"
1122 assert_match /was removed from your friends/, flash[:notice]
1123 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1126 def test_remove_friend_unkown_user
1127 # Should error when a bogus user is specified
1128 get :remove_friend, { :display_name => "No Such User" }, { :user => users(:normal_user).id }
1129 assert_response :not_found
1130 assert_template :no_such_user
1134 # Try without logging in
1135 get :set_status, :display_name => users(:normal_user).display_name, :status => "suspended"
1136 assert_response :redirect
1137 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1139 # Now try as a normal user
1140 get :set_status, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:normal_user).id }
1141 assert_response :redirect
1142 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1144 # Finally try as an administrator
1145 get :set_status, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:administrator_user).id }
1146 assert_response :redirect
1147 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1148 assert_equal "suspended", User.find(users(:normal_user).id).status
1152 # Try without logging in
1153 get :delete, :display_name => users(:normal_user).display_name, :status => "suspended"
1154 assert_response :redirect
1155 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1157 # Now try as a normal user
1158 get :delete, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:normal_user).id }
1159 assert_response :redirect
1160 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1162 # Finally try as an administrator
1163 get :delete, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:administrator_user).id }
1164 assert_response :redirect
1165 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1167 # Check that the user was deleted properly
1168 user = User.find(users(:normal_user).id)
1169 assert_equal "user_1", user.display_name
1170 assert_equal "", user.description
1171 assert_nil user.home_lat
1172 assert_nil user.home_lon
1173 assert_equal false, user.image.file?
1174 assert_equal false, user.email_valid
1175 assert_nil user.new_email
1176 assert_nil user.auth_provider
1177 assert_nil user.auth_uid
1178 assert_equal "deleted", user.status
1182 # Shouldn't work when not logged in
1184 assert_response :redirect
1185 assert_redirected_to :action => :login, :referer => users_path
1187 session[:user] = users(:normal_user).id
1189 # Shouldn't work when logged in as a normal user
1191 assert_response :redirect
1192 assert_redirected_to :action => :login, :referer => users_path
1194 session[:user] = users(:moderator_user).id
1196 # Shouldn't work when logged in as a moderator
1198 assert_response :redirect
1199 assert_redirected_to :action => :login, :referer => users_path
1201 session[:user] = users(:administrator_user).id
1203 # Should work when logged in as an administrator
1205 assert_response :success
1206 assert_template :list
1207 assert_select "table#user_list tr", :count => User.count + 1
1209 # Should be able to limit by status
1210 get :list, :status => "suspended"
1211 assert_response :success
1212 assert_template :list
1213 assert_select "table#user_list tr", :count => User.where(:status => "suspended").count + 1
1215 # Should be able to limit by IP address
1216 get :list, :ip => "1.2.3.4"
1217 assert_response :success
1218 assert_template :list
1219 assert_select "table#user_list tr", :count => User.where(:creation_ip => "1.2.3.4").count + 1
1222 def test_list_get_paginated
1223 1.upto(100).each do |n|
1224 User.create(:display_name => "extra_#{n}",
1225 :email => "extra#{n}@example.com",
1226 :pass_crypt => "extraextra")
1229 session[:user] = users(:administrator_user).id
1232 assert_response :success
1233 assert_template :list
1234 assert_select "table#user_list tr", :count => 51
1236 get :list, :page => 2
1237 assert_response :success
1238 assert_template :list
1239 assert_select "table#user_list tr", :count => 51
1241 get :list, :page => 3
1242 assert_response :success
1243 assert_template :list
1244 assert_select "table#user_list tr", :count => 19
1247 def test_list_post_confirm
1248 inactive_user = users(:inactive_user)
1249 suspended_user = users(:suspended_user)
1251 # Shouldn't work when not logged in
1252 assert_no_difference "User.active.count" do
1253 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1255 assert_response :redirect
1256 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1257 assert_equal "pending", inactive_user.reload.status
1258 assert_equal "suspended", suspended_user.reload.status
1260 session[:user] = users(:normal_user).id
1262 # Shouldn't work when logged in as a normal user
1263 assert_no_difference "User.active.count" do
1264 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1266 assert_response :redirect
1267 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1268 assert_equal "pending", inactive_user.reload.status
1269 assert_equal "suspended", suspended_user.reload.status
1271 session[:user] = users(:moderator_user).id
1273 # Shouldn't work when logged in as a moderator
1274 assert_no_difference "User.active.count" do
1275 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1277 assert_response :redirect
1278 assert_redirected_to :action => :login, :referer => users_path(:confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 })
1279 assert_equal "pending", inactive_user.reload.status
1280 assert_equal "suspended", suspended_user.reload.status
1282 session[:user] = users(:administrator_user).id
1284 # Should work when logged in as an administrator
1285 assert_difference "User.active.count", 2 do
1286 post :list, :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 }
1288 assert_response :redirect
1289 assert_redirected_to :action => :list
1290 assert_equal "confirmed", inactive_user.reload.status
1291 assert_equal "confirmed", suspended_user.reload.status
1294 def test_list_post_hide
1295 normal_user = users(:normal_user)
1296 confirmed_user = users(:confirmed_user)
1298 # Shouldn't work when not logged in
1299 assert_no_difference "User.active.count" do
1300 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1302 assert_response :redirect
1303 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1304 assert_equal "active", normal_user.reload.status
1305 assert_equal "confirmed", confirmed_user.reload.status
1307 session[:user] = users(:normal_user).id
1309 # Shouldn't work when logged in as a normal user
1310 assert_no_difference "User.active.count" do
1311 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1313 assert_response :redirect
1314 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1315 assert_equal "active", normal_user.reload.status
1316 assert_equal "confirmed", confirmed_user.reload.status
1318 session[:user] = users(:moderator_user).id
1320 # Shouldn't work when logged in as a moderator
1321 assert_no_difference "User.active.count" do
1322 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1324 assert_response :redirect
1325 assert_redirected_to :action => :login, :referer => users_path(:hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 })
1326 assert_equal "active", normal_user.reload.status
1327 assert_equal "confirmed", confirmed_user.reload.status
1329 session[:user] = users(:administrator_user).id
1331 # Should work when logged in as an administrator
1332 assert_difference "User.active.count", -2 do
1333 post :list, :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 }
1335 assert_response :redirect
1336 assert_redirected_to :action => :list
1337 assert_equal "deleted", normal_user.reload.status
1338 assert_equal "deleted", confirmed_user.reload.status
1345 user.status = "pending"
1346 user.display_name = "new_tester"
1347 user.email = "newtester@osm.org"
1348 user.email_confirmation = "newtester@osm.org"
1349 user.pass_crypt = "testtest"
1350 user.pass_crypt_confirmation = "testtest"