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
219 def test_user_create_success
220 new_email = "newtester@osm.org"
221 display_name = "new_tester"
222 assert_difference('User.count') do
223 assert_difference('ActionMailer::Base.deliveries.size') do
224 session[:new_user] = User.new({
225 :status => "pending", :display_name => display_name,
226 :email => new_email, :email_confirmation => new_email,
227 :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"
228 }, :without_protection => true)
235 register_email = ActionMailer::Base.deliveries.first
237 assert_equal register_email.to[0], new_email
238 assert_match /#{@url}/, register_email.body.to_s
241 assert_redirected_to :action => 'confirm', :display_name => display_name
243 ActionMailer::Base.deliveries.clear
246 def test_user_create_submit_duplicate_email
247 email = users(:public_user).email
248 display_name = "new_tester"
249 assert_difference('User.count', 0) do
250 assert_difference('ActionMailer::Base.deliveries.size', 0) do
251 session[:new_user] = User.new({
252 :status => "pending", :display_name => display_name,
253 :email => email, :email_confirmation => email,
254 :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"
255 }, :without_protection => true)
260 assert_response :success
261 assert_template 'new'
262 assert_select "form > fieldset > div.form-row > div.field_with_errors > input#user_email"
265 def test_user_create_submit_duplicate_email_uppercase
266 email = users(:public_user).email.upcase
267 display_name = "new_tester"
268 assert_difference('User.count', 0) do
269 assert_difference('ActionMailer::Base.deliveries.size', 0) do
270 session[:new_user] = User.new({
271 :status => "pending", :display_name => display_name,
272 :email => email, :email_confirmation => email,
273 :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"
274 }, :without_protection => true)
279 assert_response :success
280 assert_template 'new'
281 assert_select "form > fieldset > div.form-row > div.field_with_errors > input#user_email"
284 def test_user_create_submit_duplicate_name
285 email = "new_tester@example.com"
286 display_name = users(:public_user).display_name
287 assert_difference('User.count', 0) do
288 assert_difference('ActionMailer::Base.deliveries.size', 0) do
289 session[:new_user] = User.new({
290 :status => "pending", :display_name => display_name,
291 :email => email, :email_confirmation => email,
292 :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"
293 }, :without_protection => true)
298 assert_response :success
299 assert_template 'new'
300 assert_select "form > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
303 def test_user_create_submit_duplicate_name_uppercase
304 email = "new_tester@example.com"
305 display_name = users(:public_user).display_name.upcase
306 assert_difference('User.count', 0) do
307 assert_difference('ActionMailer::Base.deliveries.size', 0) do
308 session[:new_user] = User.new({
309 :status => "pending", :display_name => display_name,
310 :email => email, :email_confirmation => email,
311 :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"
312 }, :without_protection => true)
317 assert_response :success
318 assert_template 'new'
319 assert_select "form > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
322 def test_user_terms_new_user
323 get :terms, {}, { "new_user" => User.new }
324 assert_response :success
325 assert_template :terms
328 def test_user_terms_seen
329 user = users(:normal_user)
331 # Set the username cookie
332 @request.cookies["_osm_username"] = user.display_name
334 get :terms, {}, { "user" => user }
335 assert_response :redirect
336 assert_redirected_to :action => :account, :display_name => user.display_name
339 def test_user_lost_password
340 # Test fetching the lost password page
342 assert_response :success
343 assert_template :lost_password
344 assert_select "div#notice", false
346 # Test resetting using the address as recorded for a user that has an
347 # address which is duplicated in a different case by another user
348 assert_difference('ActionMailer::Base.deliveries.size', 1) do
349 post :lost_password, :user => { :email => users(:normal_user).email }
351 assert_response :redirect
352 assert_redirected_to :action => :login
353 assert_match /^Sorry you lost it/, flash[:notice]
354 assert_equal users(:normal_user).email, ActionMailer::Base.deliveries.last.to[0]
356 # Test resetting using an address that matches a different user
357 # that has the same address in a different case
358 assert_difference('ActionMailer::Base.deliveries.size', 1) do
359 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
361 assert_response :redirect
362 assert_redirected_to :action => :login
363 assert_match /^Sorry you lost it/, flash[:notice]
364 assert_equal users(:uppercase_user).email, ActionMailer::Base.deliveries.last.to[0]
366 # Test resetting using an address that is a case insensitive match
367 # for more than one user but not an exact match for either
368 assert_difference('ActionMailer::Base.deliveries.size', 0) do
369 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
371 assert_response :success
372 assert_template :lost_password
373 assert_select "div#error", /^Could not find that email address/
375 # Test resetting using the address as recorded for a user that has an
376 # address which is case insensitively unique
377 assert_difference('ActionMailer::Base.deliveries.size', 1) do
378 post :lost_password, :user => { :email => users(:public_user).email }
380 assert_response :redirect
381 assert_redirected_to :action => :login
382 assert_match /^Sorry you lost it/, flash[:notice]
383 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
385 # Test resetting using an address that matches a user that has the
386 # same (case insensitively unique) address in a different case
387 assert_difference('ActionMailer::Base.deliveries.size', 1) do
388 post :lost_password, :user => { :email => users(:public_user).email.upcase }
390 assert_response :redirect
391 assert_redirected_to :action => :login
392 assert_match /^Sorry you lost it/, flash[:notice]
393 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
397 # Get a user to work with - note that this user deliberately
398 # conflicts with uppercase_user in the email and display name
399 # fields to test that we can change other fields without any
400 # validation errors being reported
401 user = users(:normal_user)
403 # Set the username cookie
404 @request.cookies["_osm_username"] = user.display_name
406 # Make sure that you are redirected to the login page when
407 # you are not logged in
408 get :account, { :display_name => user.display_name }
409 assert_response :redirect
410 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
412 # Make sure that you are redirected to the login page when
413 # you are not logged in as the right user
414 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
415 assert_response :redirect
416 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
418 # Make sure we get the page when we are logged in as the right user
419 get :account, { :display_name => user.display_name }, { "user" => user }
420 assert_response :success
421 assert_template :account
423 # Updating the description should work
424 user.description = "new description"
425 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
426 assert_response :success
427 assert_template :account
428 assert_select "div#errorExplanation", false
429 assert_select "div#notice", /^User information updated successfully/
430 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
432 # Changing name to one that exists should fail
433 user.display_name = users(:public_user).display_name
434 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
435 assert_response :success
436 assert_template :account
437 assert_select "div#notice", false
438 assert_select "div#errorExplanation"
439 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
441 # Changing name to one that exists should fail, regardless of case
442 user.display_name = users(:public_user).display_name.upcase
443 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
444 assert_response :success
445 assert_template :account
446 assert_select "div#notice", false
447 assert_select "div#errorExplanation"
448 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
450 # Changing name to one that doesn't exist should work
451 user.display_name = "new tester"
452 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
453 assert_response :success
454 assert_template :account
455 assert_select "div#errorExplanation", false
456 assert_select "div#notice", /^User information updated successfully/
457 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", user.display_name
459 # Need to update cookies now to stay valid
460 @request.cookies["_osm_username"] = user.display_name
462 # Changing email to one that exists should fail
463 user.new_email = users(:public_user).email
464 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
465 assert_response :success
466 assert_template :account
467 assert_select "div#notice", false
468 assert_select "div#errorExplanation"
469 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_new_email"
471 # Changing email to one that exists should fail, regardless of case
472 user.new_email = users(:public_user).email.upcase
473 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
474 assert_response :success
475 assert_template :account
476 assert_select "div#notice", false
477 assert_select "div#errorExplanation"
478 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_new_email"
480 # Changing email to one that doesn't exist should work
481 user.new_email = "new_tester@example.com"
482 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
483 assert_response :success
484 assert_template :account
485 assert_select "div#errorExplanation", false
486 assert_select "div#notice", /^User information updated successfully/
487 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
490 # Check that the user account page will display and contains some relevant
491 # information for the user
492 def test_user_view_account
493 # Test a non-existent user
494 get :view, {:display_name => "unknown"}
495 assert_response :not_found
498 get :view, {:display_name => "test"}
499 assert_response :success
500 assert_select "div#userinformation" do
501 assert_select "a[href=/user/test/edits]", 1
502 assert_select "a[href=/user/test/traces]", 1
503 assert_select "a[href=/user/test/diary]", 1
504 assert_select "a[href=/user/test/diary/comments]", 1
505 assert_select "a[href=/user/test/account]", 0
506 assert_select "a[href=/user/test/blocks]", 0
507 assert_select "a[href=/user/test/blocks_by]", 0
508 assert_select "a[href=/blocks/new/test]", 0
511 # Test a user who has been blocked
512 get :view, {:display_name => "blocked"}
513 assert_response :success
514 assert_select "div#userinformation" do
515 assert_select "a[href=/user/blocked/edits]", 1
516 assert_select "a[href=/user/blocked/traces]", 1
517 assert_select "a[href=/user/blocked/diary]", 1
518 assert_select "a[href=/user/blocked/diary/comments]", 1
519 assert_select "a[href=/user/blocked/account]", 0
520 assert_select "a[href=/user/blocked/blocks]", 1
521 assert_select "a[href=/user/blocked/blocks_by]", 0
522 assert_select "a[href=/blocks/new/blocked]", 0
525 # Test a moderator who has applied blocks
526 get :view, {:display_name => "moderator"}
527 assert_response :success
528 assert_select "div#userinformation" do
529 assert_select "a[href=/user/moderator/edits]", 1
530 assert_select "a[href=/user/moderator/traces]", 1
531 assert_select "a[href=/user/moderator/diary]", 1
532 assert_select "a[href=/user/moderator/diary/comments]", 1
533 assert_select "a[href=/user/moderator/account]", 0
534 assert_select "a[href=/user/moderator/blocks]", 0
535 assert_select "a[href=/user/moderator/blocks_by]", 1
536 assert_select "a[href=/blocks/new/moderator]", 0
539 # Login as a normal user
540 session[:user] = users(:normal_user).id
541 cookies["_osm_username"] = users(:normal_user).display_name
543 # Test the normal user
544 get :view, {:display_name => "test"}
545 assert_response :success
546 assert_select "div#userinformation" do
547 assert_select "a[href=/user/test/edits]", 1
548 assert_select "a[href=/traces/mine]", 1
549 assert_select "a[href=/user/test/diary]", 1
550 assert_select "a[href=/user/test/diary/comments]", 1
551 assert_select "a[href=/user/test/account]", 1
552 assert_select "a[href=/user/test/blocks]", 0
553 assert_select "a[href=/user/test/blocks_by]", 0
554 assert_select "a[href=/blocks/new/test]", 0
557 # Login as a moderator
558 session[:user] = users(:moderator_user).id
559 cookies["_osm_username"] = users(:moderator_user).display_name
561 # Test the normal user
562 get :view, {:display_name => "test"}
563 assert_response :success
564 assert_select "div#userinformation" do
565 assert_select "a[href=/user/test/edits]", 1
566 assert_select "a[href=/user/test/traces]", 1
567 assert_select "a[href=/user/test/diary]", 1
568 assert_select "a[href=/user/test/diary/comments]", 1
569 assert_select "a[href=/user/test/account]", 0
570 assert_select "a[href=/user/test/blocks]", 0
571 assert_select "a[href=/user/test/blocks_by]", 0
572 assert_select "a[href=/blocks/new/test]", 1
576 def test_user_api_read
577 # check that a visible user is returned properly
578 get :api_read, :id => users(:normal_user).id
579 assert_response :success
581 # check that we aren't revealing private information
582 assert_select "contributor-terms[pd]", false
583 assert_select "home", false
584 assert_select "languages", false
586 # check that a suspended user is not returned
587 get :api_read, :id => users(:suspended_user).id
588 assert_response :gone
590 # check that a deleted user is not returned
591 get :api_read, :id => users(:deleted_user).id
592 assert_response :gone
594 # check that a non-existent user is not returned
595 get :api_read, :id => 0
596 assert_response :not_found
599 def test_user_api_details
601 assert_response :unauthorized
603 basic_authorization(users(:normal_user).email, "test")
605 assert_response :success
608 def test_user_make_friend
609 # Get users to work with
610 user = users(:normal_user)
611 friend = users(:second_public_user)
613 # Check that the users aren't already friends
614 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
616 # Set the username cookie
617 @request.cookies["_osm_username"] = user.display_name
619 # When not logged in a GET should ask us to login
620 get :make_friend, {:display_name => friend.display_name}
621 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
623 # When not logged in a POST should error
624 post :make_friend, {:display_name => friend.display_name}
625 assert_response :forbidden
626 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
628 # When logged in a GET should get a confirmation page
629 get :make_friend, {:display_name => friend.display_name}, {"user" => user}
630 assert_response :success
631 assert_template :make_friend
632 assert_select "form" do
633 assert_select "input[type=hidden][name=referer]", 0
634 assert_select "input[type=submit]", 1
636 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
638 # The GET should preserve any referer
639 get :make_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
640 assert_response :success
641 assert_template :make_friend
642 assert_select "form" do
643 assert_select "input[type=hidden][name=referer][value=/test]", 1
644 assert_select "input[type=submit]", 1
646 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
648 # When logged in a POST should add the friendship
649 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
650 assert_redirected_to user_path(:display_name => friend.display_name)
651 assert_match /is now your friend/, flash[:notice]
652 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
654 # A second POST should report that the friendship already exists
655 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
656 assert_redirected_to user_path(:display_name => friend.display_name)
657 assert_match /You are already friends with/, flash[:warning]
658 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
661 def test_user_remove_friend
662 # Get users to work with
663 user = users(:normal_user)
664 friend = users(:public_user)
666 # Check that the users are friends
667 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
669 # Set the username cookie
670 @request.cookies["_osm_username"] = user.display_name
672 # When not logged in a GET should ask us to login
673 get :remove_friend, {:display_name => friend.display_name}
674 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
676 # When not logged in a POST should error
677 post :remove_friend, {:display_name => friend.display_name}
678 assert_response :forbidden
679 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
681 # When logged in a GET should get a confirmation page
682 get :remove_friend, {:display_name => friend.display_name}, {"user" => user}
683 assert_response :success
684 assert_template :remove_friend
685 assert_select "form" do
686 assert_select "input[type=hidden][name=referer]", 0
687 assert_select "input[type=submit]", 1
689 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
691 # The GET should preserve any referer
692 get :remove_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
693 assert_response :success
694 assert_template :remove_friend
695 assert_select "form" do
696 assert_select "input[type=hidden][name=referer][value=/test]", 1
697 assert_select "input[type=submit]", 1
699 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
701 # When logged in a POST should remove the friendship
702 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
703 assert_redirected_to user_path(:display_name => friend.display_name)
704 assert_match /was removed from your friends/, flash[:notice]
705 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
707 # A second POST should report that the friendship does not exist
708 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
709 assert_redirected_to user_path(:display_name => friend.display_name)
710 assert_match /is not one of your friends/, flash[:error]
711 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first