1 require File.dirname(__FILE__) + '/../test_helper'
3 class UserControllerTest < ActionController::TestCase
7 # test all routes which lead to this controller
10 { :path => "/api/0.6/user/1", :method => :get },
11 { :controller => "user", :action => "api_read", :id => "1" }
14 { :path => "/api/0.6/user/details", :method => :get },
15 { :controller => "user", :action => "api_details" }
18 { :path => "/api/0.6/user/gpx_files", :method => :get },
19 { :controller => "user", :action => "api_gpx_files" }
23 { :path => "/login", :method => :get },
24 { :controller => "user", :action => "login" }
27 { :path => "/login", :method => :post },
28 { :controller => "user", :action => "login" }
31 { :controller => "user", :action => "login", :format => "html" },
32 { :path => "/login.html", :method => :get }
36 { :path => "/logout", :method => :get },
37 { :controller => "user", :action => "logout" }
40 { :path => "/logout", :method => :post },
41 { :controller => "user", :action => "logout" }
44 { :controller => "user", :action => "logout", :format => "html" },
45 { :path => "/logout.html", :method => :get }
49 { :path => "/user/new", :method => :get },
50 { :controller => "user", :action => "new" }
53 { :controller => "user", :action => "new" },
54 { :path => "/create-account.html", :method => :get }
58 { :path => "/user/new", :method => :post },
59 { :controller => "user", :action => "create" }
63 { :path => "/user/terms", :method => :get },
64 { :controller => "user", :action => "terms" }
68 { :path => "/user/save", :method => :post },
69 { :controller => "user", :action => "save" }
73 { :path => "/user/username/confirm", :method => :get },
74 { :controller => "user", :action => "confirm", :display_name => "username" }
77 { :path => "/user/username/confirm", :method => :post },
78 { :controller => "user", :action => "confirm", :display_name => "username" }
81 { :path => "/user/username/confirm/resend", :method => :get },
82 { :controller => "user", :action => "confirm_resend", :display_name => "username" }
86 { :path => "/user/confirm", :method => :get },
87 { :controller => "user", :action => "confirm" }
90 { :path => "/user/confirm", :method => :post },
91 { :controller => "user", :action => "confirm" }
94 { :path => "/user/confirm-email", :method => :get },
95 { :controller => "user", :action => "confirm_email" }
98 { :path => "/user/confirm-email", :method => :post },
99 { :controller => "user", :action => "confirm_email" }
103 { :path => "/user/go_public", :method => :post },
104 { :controller => "user", :action => "go_public" }
108 { :path => "/user/forgot-password", :method => :get },
109 { :controller => "user", :action => "lost_password" }
112 { :path => "/user/forgot-password", :method => :post },
113 { :controller => "user", :action => "lost_password" }
116 { :controller => "user", :action => "lost_password" },
117 { :path => "/forgot-password.html", :method => :get }
120 { :path => "/user/reset-password", :method => :get },
121 { :controller => "user", :action => "reset_password" }
124 { :path => "/user/reset-password", :method => :post },
125 { :controller => "user", :action => "reset_password" }
129 { :path => "/user/suspended", :method => :get },
130 { :controller => "user", :action => "suspended" }
134 { :path => "/user/username", :method => :get },
135 { :controller => "user", :action => "view", :display_name => "username" }
139 { :path => "/user/username/account", :method => :get },
140 { :controller => "user", :action => "account", :display_name => "username" }
143 { :path => "/user/username/account", :method => :post },
144 { :controller => "user", :action => "account", :display_name => "username" }
148 { :path => "/user/username/make_friend", :method => :get },
149 { :controller => "user", :action => "make_friend", :display_name => "username" }
152 { :path => "/user/username/make_friend", :method => :post },
153 { :controller => "user", :action => "make_friend", :display_name => "username" }
156 { :path => "/user/username/remove_friend", :method => :get },
157 { :controller => "user", :action => "remove_friend", :display_name => "username" }
160 { :path => "/user/username/remove_friend", :method => :post },
161 { :controller => "user", :action => "remove_friend", :display_name => "username" }
165 { :path => "/user/username/set_status", :method => :get },
166 { :controller => "user", :action => "set_status", :display_name => "username" }
169 { :path => "/user/username/delete", :method => :get },
170 { :controller => "user", :action => "delete", :display_name => "username" }
174 { :path => "/users", :method => :get },
175 { :controller => "user", :action => "list" }
178 { :path => "/users", :method => :post },
179 { :controller => "user", :action => "list" }
182 { :path => "/users/status", :method => :get },
183 { :controller => "user", :action => "list", :status => "status" }
186 { :path => "/users/status", :method => :post },
187 { :controller => "user", :action => "list", :status => "status" }
191 # The user creation page loads
192 def test_user_create_view
194 assert_response :redirect
195 assert_redirected_to user_new_path(:cookie_test => "true")
197 get :new, { :cookie_test => "true" }, { :cookie_test => true }
198 assert_response :success
200 assert_select "html", :count => 1 do
201 assert_select "head", :count => 1 do
202 assert_select "title", :text => /Sign Up/, :count => 1
204 assert_select "body", :count => 1 do
205 assert_select "div#content", :count => 1 do
206 assert_select "form[action='/user/new'][method=post]", :count => 1 do
207 assert_select "input[id=user_email]", :count => 1
208 assert_select "input[id=user_email_confirmation]", :count => 1
209 assert_select "input[id=user_display_name]", :count => 1
210 assert_select "input[id=user_pass_crypt][type=password]", :count => 1
211 assert_select "input[id=user_pass_crypt_confirmation][type=password]", :count => 1
212 assert_select "input[type=submit][value=Sign Up]", :count => 1
221 user.status = "pending"
222 user.display_name = "new_tester"
223 user.email = "newtester@osm.org"
224 user.email_confirmation = "newtester@osm.org"
225 user.pass_crypt = "testtest"
226 user.pass_crypt_confirmation = "testtest"
230 def test_user_create_success
233 assert_difference('User.count') do
234 assert_difference('ActionMailer::Base.deliveries.size') 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_user_create_submit_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 > div.field_with_errors > input#user_email"
266 def test_user_create_submit_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 > div.field_with_errors > input#user_email"
281 def test_user_create_submit_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 > div.field_with_errors > input#user_display_name"
296 def test_user_create_submit_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 > div.field_with_errors > input#user_display_name"
311 def test_user_save_referer_params
314 post :save, {}, {:new_user => user,
315 :referer => '/edit?editor=id#map=1/2/3'}
317 assert_equal welcome_path(:editor => 'id', :zoom => 1, :lat => 2, :lon => 3),
318 user.tokens.first.referer
321 def test_user_confirm_expired_token
322 user = users(:inactive_user)
323 token = user.tokens.new
324 token.expiry = 1.day.ago
327 @request.cookies["_osm_session"] = user.display_name
328 post :confirm, :confirm_string => token.token
330 assert_redirected_to :action => 'confirm'
331 assert_match /expired/, flash[:error]
334 def test_user_already_confirmed
335 user = users(:normal_user)
336 token = user.tokens.create
338 @request.cookies["_osm_session"] = user.display_name
339 post :confirm, :confirm_string => token.token
341 assert_redirected_to :action => 'login'
342 assert_match /confirmed/, flash[:error]
345 def test_user_terms_new_user
346 get :terms, {}, { "new_user" => User.new }
347 assert_response :success
348 assert_template :terms
351 def test_user_terms_seen
352 user = users(:normal_user)
354 # Set the username cookie
355 @request.cookies["_osm_username"] = user.display_name
357 get :terms, {}, { "user" => user }
358 assert_response :redirect
359 assert_redirected_to :action => :account, :display_name => user.display_name
362 def test_user_lost_password
363 # Test fetching the lost password page
365 assert_response :success
366 assert_template :lost_password
367 assert_select "div#notice", false
369 # Test resetting using the address as recorded for a user that has an
370 # address which is duplicated in a different case by another user
371 assert_difference('ActionMailer::Base.deliveries.size', 1) do
372 post :lost_password, :user => { :email => users(:normal_user).email }
374 assert_response :redirect
375 assert_redirected_to :action => :login
376 assert_match /^Sorry you lost it/, flash[:notice]
377 assert_equal users(:normal_user).email, ActionMailer::Base.deliveries.last.to[0]
379 # Test resetting using an address that matches a different user
380 # that has the same address in a different case
381 assert_difference('ActionMailer::Base.deliveries.size', 1) do
382 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
384 assert_response :redirect
385 assert_redirected_to :action => :login
386 assert_match /^Sorry you lost it/, flash[:notice]
387 assert_equal users(:uppercase_user).email, ActionMailer::Base.deliveries.last.to[0]
389 # Test resetting using an address that is a case insensitive match
390 # for more than one user but not an exact match for either
391 assert_difference('ActionMailer::Base.deliveries.size', 0) do
392 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
394 assert_response :success
395 assert_template :lost_password
396 assert_select "div#error", /^Could not find that email address/
398 # Test resetting using the address as recorded for a user that has an
399 # address which is case insensitively unique
400 assert_difference('ActionMailer::Base.deliveries.size', 1) do
401 post :lost_password, :user => { :email => users(:public_user).email }
403 assert_response :redirect
404 assert_redirected_to :action => :login
405 assert_match /^Sorry you lost it/, flash[:notice]
406 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
408 # Test resetting using an address that matches a user that has the
409 # same (case insensitively unique) address in a different case
410 assert_difference('ActionMailer::Base.deliveries.size', 1) do
411 post :lost_password, :user => { :email => users(:public_user).email.upcase }
413 assert_response :redirect
414 assert_redirected_to :action => :login
415 assert_match /^Sorry you lost it/, flash[:notice]
416 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
420 # Get a user to work with - note that this user deliberately
421 # conflicts with uppercase_user in the email and display name
422 # fields to test that we can change other fields without any
423 # validation errors being reported
424 user = users(:normal_user)
426 # Set the username cookie
427 @request.cookies["_osm_username"] = user.display_name
429 # Make sure that you are redirected to the login page when
430 # you are not logged in
431 get :account, { :display_name => user.display_name }
432 assert_response :redirect
433 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
435 # Make sure that you are redirected to the login page when
436 # you are not logged in as the right user
437 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
438 assert_response :redirect
439 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
441 # Make sure we get the page when we are logged in as the right user
442 get :account, { :display_name => user.display_name }, { "user" => user }
443 assert_response :success
444 assert_template :account
446 # Updating the description should work
447 user.description = "new description"
448 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
449 assert_response :success
450 assert_template :account
451 assert_select "div#errorExplanation", false
452 assert_select "div#notice", /^User information updated successfully/
453 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
455 # Changing name to one that exists should fail
456 user.display_name = users(:public_user).display_name
457 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
458 assert_response :success
459 assert_template :account
460 assert_select "div#notice", false
461 assert_select "div#errorExplanation"
462 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
464 # Changing name to one that exists should fail, regardless of case
465 user.display_name = users(:public_user).display_name.upcase
466 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
467 assert_response :success
468 assert_template :account
469 assert_select "div#notice", false
470 assert_select "div#errorExplanation"
471 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
473 # Changing name to one that doesn't exist should work
474 user.display_name = "new tester"
475 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
476 assert_response :success
477 assert_template :account
478 assert_select "div#errorExplanation", false
479 assert_select "div#notice", /^User information updated successfully/
480 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", user.display_name
482 # Need to update cookies now to stay valid
483 @request.cookies["_osm_username"] = user.display_name
485 # Changing email to one that exists should fail
486 user.new_email = users(:public_user).email
487 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
488 assert_response :success
489 assert_template :account
490 assert_select "div#notice", false
491 assert_select "div#errorExplanation"
492 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_new_email"
494 # Changing email to one that exists should fail, regardless of case
495 user.new_email = users(:public_user).email.upcase
496 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
497 assert_response :success
498 assert_template :account
499 assert_select "div#notice", false
500 assert_select "div#errorExplanation"
501 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_new_email"
503 # Changing email to one that doesn't exist should work
504 user.new_email = "new_tester@example.com"
505 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
506 assert_response :success
507 assert_template :account
508 assert_select "div#errorExplanation", false
509 assert_select "div#notice", /^User information updated successfully/
510 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
513 # Check that the user account page will display and contains some relevant
514 # information for the user
515 def test_user_view_account
516 # Test a non-existent user
517 get :view, {:display_name => "unknown"}
518 assert_response :not_found
521 get :view, {:display_name => "test"}
522 assert_response :success
523 assert_select "div#userinformation" do
524 assert_select "a[href=/user/test/edits]", 1
525 assert_select "a[href=/user/test/traces]", 1
526 assert_select "a[href=/user/test/diary]", 1
527 assert_select "a[href=/user/test/diary/comments]", 1
528 assert_select "a[href=/user/test/account]", 0
529 assert_select "a[href=/user/test/blocks]", 0
530 assert_select "a[href=/user/test/blocks_by]", 0
531 assert_select "a[href=/blocks/new/test]", 0
534 # Test a user who has been blocked
535 get :view, {:display_name => "blocked"}
536 assert_response :success
537 assert_select "div#userinformation" do
538 assert_select "a[href=/user/blocked/edits]", 1
539 assert_select "a[href=/user/blocked/traces]", 1
540 assert_select "a[href=/user/blocked/diary]", 1
541 assert_select "a[href=/user/blocked/diary/comments]", 1
542 assert_select "a[href=/user/blocked/account]", 0
543 assert_select "a[href=/user/blocked/blocks]", 1
544 assert_select "a[href=/user/blocked/blocks_by]", 0
545 assert_select "a[href=/blocks/new/blocked]", 0
548 # Test a moderator who has applied blocks
549 get :view, {:display_name => "moderator"}
550 assert_response :success
551 assert_select "div#userinformation" do
552 assert_select "a[href=/user/moderator/edits]", 1
553 assert_select "a[href=/user/moderator/traces]", 1
554 assert_select "a[href=/user/moderator/diary]", 1
555 assert_select "a[href=/user/moderator/diary/comments]", 1
556 assert_select "a[href=/user/moderator/account]", 0
557 assert_select "a[href=/user/moderator/blocks]", 0
558 assert_select "a[href=/user/moderator/blocks_by]", 1
559 assert_select "a[href=/blocks/new/moderator]", 0
562 # Login as a normal user
563 session[:user] = users(:normal_user).id
564 cookies["_osm_username"] = users(:normal_user).display_name
566 # Test the normal user
567 get :view, {:display_name => "test"}
568 assert_response :success
569 assert_select "div#userinformation" do
570 assert_select "a[href=/user/test/edits]", 1
571 assert_select "a[href=/traces/mine]", 1
572 assert_select "a[href=/user/test/diary]", 1
573 assert_select "a[href=/user/test/diary/comments]", 1
574 assert_select "a[href=/user/test/account]", 1
575 assert_select "a[href=/user/test/blocks]", 0
576 assert_select "a[href=/user/test/blocks_by]", 0
577 assert_select "a[href=/blocks/new/test]", 0
580 # Login as a moderator
581 session[:user] = users(:moderator_user).id
582 cookies["_osm_username"] = users(:moderator_user).display_name
584 # Test the normal user
585 get :view, {:display_name => "test"}
586 assert_response :success
587 assert_select "div#userinformation" do
588 assert_select "a[href=/user/test/edits]", 1
589 assert_select "a[href=/user/test/traces]", 1
590 assert_select "a[href=/user/test/diary]", 1
591 assert_select "a[href=/user/test/diary/comments]", 1
592 assert_select "a[href=/user/test/account]", 0
593 assert_select "a[href=/user/test/blocks]", 0
594 assert_select "a[href=/user/test/blocks_by]", 0
595 assert_select "a[href=/blocks/new/test]", 1
599 def test_user_api_read
600 # check that a visible user is returned properly
601 get :api_read, :id => users(:normal_user).id
602 assert_response :success
604 # check that we aren't revealing private information
605 assert_select "contributor-terms[pd]", false
606 assert_select "home", false
607 assert_select "languages", false
609 # check that a suspended user is not returned
610 get :api_read, :id => users(:suspended_user).id
611 assert_response :gone
613 # check that a deleted user is not returned
614 get :api_read, :id => users(:deleted_user).id
615 assert_response :gone
617 # check that a non-existent user is not returned
618 get :api_read, :id => 0
619 assert_response :not_found
622 def test_user_api_details
624 assert_response :unauthorized
626 basic_authorization(users(:normal_user).email, "test")
628 assert_response :success
631 def test_user_make_friend
632 # Get users to work with
633 user = users(:normal_user)
634 friend = users(:second_public_user)
636 # Check that the users aren't already friends
637 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
639 # Set the username cookie
640 @request.cookies["_osm_username"] = user.display_name
642 # When not logged in a GET should ask us to login
643 get :make_friend, {:display_name => friend.display_name}
644 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
646 # When not logged in a POST should error
647 post :make_friend, {:display_name => friend.display_name}
648 assert_response :forbidden
649 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
651 # When logged in a GET should get a confirmation page
652 get :make_friend, {:display_name => friend.display_name}, {"user" => user}
653 assert_response :success
654 assert_template :make_friend
655 assert_select "form" do
656 assert_select "input[type=hidden][name=referer]", 0
657 assert_select "input[type=submit]", 1
659 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
661 # The GET should preserve any referer
662 get :make_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
663 assert_response :success
664 assert_template :make_friend
665 assert_select "form" do
666 assert_select "input[type=hidden][name=referer][value=/test]", 1
667 assert_select "input[type=submit]", 1
669 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
671 # When logged in a POST should add the friendship
672 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
673 assert_redirected_to user_path(:display_name => friend.display_name)
674 assert_match /is now your friend/, flash[:notice]
675 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
677 # A second POST should report that the friendship already exists
678 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
679 assert_redirected_to user_path(:display_name => friend.display_name)
680 assert_match /You are already friends with/, flash[:warning]
681 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
684 def test_user_remove_friend
685 # Get users to work with
686 user = users(:normal_user)
687 friend = users(:public_user)
689 # Check that the users are friends
690 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
692 # Set the username cookie
693 @request.cookies["_osm_username"] = user.display_name
695 # When not logged in a GET should ask us to login
696 get :remove_friend, {:display_name => friend.display_name}
697 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
699 # When not logged in a POST should error
700 post :remove_friend, {:display_name => friend.display_name}
701 assert_response :forbidden
702 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
704 # When logged in a GET should get a confirmation page
705 get :remove_friend, {:display_name => friend.display_name}, {"user" => user}
706 assert_response :success
707 assert_template :remove_friend
708 assert_select "form" do
709 assert_select "input[type=hidden][name=referer]", 0
710 assert_select "input[type=submit]", 1
712 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
714 # The GET should preserve any referer
715 get :remove_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
716 assert_response :success
717 assert_template :remove_friend
718 assert_select "form" do
719 assert_select "input[type=hidden][name=referer][value=/test]", 1
720 assert_select "input[type=submit]", 1
722 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
724 # When logged in a POST should remove the friendship
725 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
726 assert_redirected_to user_path(:display_name => friend.display_name)
727 assert_match /was removed from your friends/, flash[:notice]
728 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
730 # A second POST should report that the friendship does not exist
731 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
732 assert_redirected_to user_path(:display_name => friend.display_name)
733 assert_match /is not one of your friends/, flash[:error]
734 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first