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 => /Create account/, :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=Continue]", :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 => 'login', :referer => nil
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 "div#errorExplanation"
263 assert_select "div#signupForm > fieldset > div.form-row > div.field_with_errors > input#user_email"
266 def test_user_create_submit_duplicate_email_uppercase
267 email = users(:public_user).email.upcase
268 display_name = "new_tester"
269 assert_difference('User.count', 0) do
270 assert_difference('ActionMailer::Base.deliveries.size', 0) do
271 session[:new_user] = User.new({
272 :status => "pending", :display_name => display_name,
273 :email => email, :email_confirmation => email,
274 :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"
275 }, :without_protection => true)
280 assert_response :success
281 assert_template 'new'
282 assert_select "div#errorExplanation"
283 assert_select "div#signupForm > fieldset > div.form-row > div.field_with_errors > input#user_email"
286 def test_user_create_submit_duplicate_name
287 email = "new_tester@example.com"
288 display_name = users(:public_user).display_name
289 assert_difference('User.count', 0) do
290 assert_difference('ActionMailer::Base.deliveries.size', 0) do
291 session[:new_user] = User.new({
292 :status => "pending", :display_name => display_name,
293 :email => email, :email_confirmation => email,
294 :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"
295 }, :without_protection => true)
300 assert_response :success
301 assert_template 'new'
302 assert_select "div#errorExplanation"
303 assert_select "div#signupForm > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
306 def test_user_create_submit_duplicate_name_uppercase
307 email = "new_tester@example.com"
308 display_name = users(:public_user).display_name.upcase
309 assert_difference('User.count', 0) do
310 assert_difference('ActionMailer::Base.deliveries.size', 0) do
311 session[:new_user] = User.new({
312 :status => "pending", :display_name => display_name,
313 :email => email, :email_confirmation => email,
314 :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"
315 }, :without_protection => true)
320 assert_response :success
321 assert_template 'new'
322 assert_select "div#errorExplanation"
323 assert_select "div#signupForm > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
326 def test_user_terms_new_user
327 get :terms, {}, { "new_user" => User.new }
328 assert_response :success
329 assert_template :terms
332 def test_user_terms_seen
333 user = users(:normal_user)
335 # Set the username cookie
336 @request.cookies["_osm_username"] = user.display_name
338 get :terms, {}, { "user" => user }
339 assert_response :redirect
340 assert_redirected_to :action => :account, :display_name => user.display_name
343 def test_user_lost_password
344 # Test fetching the lost password page
346 assert_response :success
347 assert_template :lost_password
348 assert_select "div#notice", false
350 # Test resetting using the address as recorded for a user that has an
351 # address which is duplicated in a different case by another user
352 assert_difference('ActionMailer::Base.deliveries.size', 1) do
353 post :lost_password, :user => { :email => users(:normal_user).email }
355 assert_response :redirect
356 assert_redirected_to :action => :login
357 assert_match /^Sorry you lost it/, flash[:notice]
358 assert_equal users(:normal_user).email, ActionMailer::Base.deliveries.last.to[0]
360 # Test resetting using an address that matches a different user
361 # that has the same address in a different case
362 assert_difference('ActionMailer::Base.deliveries.size', 1) do
363 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
365 assert_response :redirect
366 assert_redirected_to :action => :login
367 assert_match /^Sorry you lost it/, flash[:notice]
368 assert_equal users(:uppercase_user).email, ActionMailer::Base.deliveries.last.to[0]
370 # Test resetting using an address that is a case insensitive match
371 # for more than one user but not an exact match for either
372 assert_difference('ActionMailer::Base.deliveries.size', 0) do
373 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
375 assert_response :success
376 assert_template :lost_password
377 assert_select "div#error", /^Could not find that email address/
379 # Test resetting using the address as recorded for a user that has an
380 # address which is case insensitively unique
381 assert_difference('ActionMailer::Base.deliveries.size', 1) do
382 post :lost_password, :user => { :email => users(:public_user).email }
384 assert_response :redirect
385 assert_redirected_to :action => :login
386 assert_match /^Sorry you lost it/, flash[:notice]
387 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
389 # Test resetting using an address that matches a user that has the
390 # same (case insensitively unique) address in a different case
391 assert_difference('ActionMailer::Base.deliveries.size', 1) do
392 post :lost_password, :user => { :email => users(:public_user).email.upcase }
394 assert_response :redirect
395 assert_redirected_to :action => :login
396 assert_match /^Sorry you lost it/, flash[:notice]
397 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
401 # Get a user to work with - note that this user deliberately
402 # conflicts with uppercase_user in the email and display name
403 # fields to test that we can change other fields without any
404 # validation errors being reported
405 user = users(:normal_user)
407 # Set the username cookie
408 @request.cookies["_osm_username"] = user.display_name
410 # Make sure that you are redirected to the login page when
411 # you are not logged in
412 get :account, { :display_name => user.display_name }
413 assert_response :redirect
414 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
416 # Make sure that you are redirected to the login page when
417 # you are not logged in as the right user
418 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
419 assert_response :redirect
420 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
422 # Make sure we get the page when we are logged in as the right user
423 get :account, { :display_name => user.display_name }, { "user" => user }
424 assert_response :success
425 assert_template :account
427 # Updating the description should work
428 user.description = "new description"
429 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
430 assert_response :success
431 assert_template :account
432 assert_select "div#errorExplanation", false
433 assert_select "div#notice", /^User information updated successfully/
434 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
436 # Changing name to one that exists should fail
437 user.display_name = users(:public_user).display_name
438 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
439 assert_response :success
440 assert_template :account
441 assert_select "div#notice", false
442 assert_select "div#errorExplanation"
443 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
445 # Changing name to one that exists should fail, regardless of case
446 user.display_name = users(:public_user).display_name.upcase
447 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
448 assert_response :success
449 assert_template :account
450 assert_select "div#notice", false
451 assert_select "div#errorExplanation"
452 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
454 # Changing name to one that doesn't exist should work
455 user.display_name = "new tester"
456 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
457 assert_response :success
458 assert_template :account
459 assert_select "div#errorExplanation", false
460 assert_select "div#notice", /^User information updated successfully/
461 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", user.display_name
463 # Need to update cookies now to stay valid
464 @request.cookies["_osm_username"] = user.display_name
466 # Changing email to one that exists should fail
467 user.new_email = users(:public_user).email
468 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
469 assert_response :success
470 assert_template :account
471 assert_select "div#notice", false
472 assert_select "div#errorExplanation"
473 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_new_email"
475 # Changing email to one that exists should fail, regardless of case
476 user.new_email = users(:public_user).email.upcase
477 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
478 assert_response :success
479 assert_template :account
480 assert_select "div#notice", false
481 assert_select "div#errorExplanation"
482 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_new_email"
484 # Changing email to one that doesn't exist should work
485 user.new_email = "new_tester@example.com"
486 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
487 assert_response :success
488 assert_template :account
489 assert_select "div#errorExplanation", false
490 assert_select "div#notice", /^User information updated successfully/
491 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
494 # Check that the user account page will display and contains some relevant
495 # information for the user
496 def test_user_view_account
497 # Test a non-existent user
498 get :view, {:display_name => "unknown"}
499 assert_response :not_found
502 get :view, {:display_name => "test"}
503 assert_response :success
504 assert_select "div#userinformation" do
505 assert_select "a[href=/user/test/edits]", 1
506 assert_select "a[href=/user/test/traces]", 1
507 assert_select "a[href=/user/test/diary]", 1
508 assert_select "a[href=/user/test/diary/comments]", 1
509 assert_select "a[href=/user/test/account]", 0
510 assert_select "a[href=/user/test/blocks]", 0
511 assert_select "a[href=/user/test/blocks_by]", 0
512 assert_select "a[href=/blocks/new/test]", 0
515 # Test a user who has been blocked
516 get :view, {:display_name => "blocked"}
517 assert_response :success
518 assert_select "div#userinformation" do
519 assert_select "a[href=/user/blocked/edits]", 1
520 assert_select "a[href=/user/blocked/traces]", 1
521 assert_select "a[href=/user/blocked/diary]", 1
522 assert_select "a[href=/user/blocked/diary/comments]", 1
523 assert_select "a[href=/user/blocked/account]", 0
524 assert_select "a[href=/user/blocked/blocks]", 1
525 assert_select "a[href=/user/blocked/blocks_by]", 0
526 assert_select "a[href=/blocks/new/blocked]", 0
529 # Test a moderator who has applied blocks
530 get :view, {:display_name => "moderator"}
531 assert_response :success
532 assert_select "div#userinformation" do
533 assert_select "a[href=/user/moderator/edits]", 1
534 assert_select "a[href=/user/moderator/traces]", 1
535 assert_select "a[href=/user/moderator/diary]", 1
536 assert_select "a[href=/user/moderator/diary/comments]", 1
537 assert_select "a[href=/user/moderator/account]", 0
538 assert_select "a[href=/user/moderator/blocks]", 0
539 assert_select "a[href=/user/moderator/blocks_by]", 1
540 assert_select "a[href=/blocks/new/moderator]", 0
543 # Login as a normal user
544 session[:user] = users(:normal_user).id
545 cookies["_osm_username"] = users(:normal_user).display_name
547 # Test the normal user
548 get :view, {:display_name => "test"}
549 assert_response :success
550 assert_select "div#userinformation" do
551 assert_select "a[href=/user/test/edits]", 1
552 assert_select "a[href=/traces/mine]", 1
553 assert_select "a[href=/user/test/diary]", 1
554 assert_select "a[href=/user/test/diary/comments]", 1
555 assert_select "a[href=/user/test/account]", 1
556 assert_select "a[href=/user/test/blocks]", 0
557 assert_select "a[href=/user/test/blocks_by]", 0
558 assert_select "a[href=/blocks/new/test]", 0
561 # Login as a moderator
562 session[:user] = users(:moderator_user).id
563 cookies["_osm_username"] = users(:moderator_user).display_name
565 # Test the normal user
566 get :view, {:display_name => "test"}
567 assert_response :success
568 assert_select "div#userinformation" do
569 assert_select "a[href=/user/test/edits]", 1
570 assert_select "a[href=/user/test/traces]", 1
571 assert_select "a[href=/user/test/diary]", 1
572 assert_select "a[href=/user/test/diary/comments]", 1
573 assert_select "a[href=/user/test/account]", 0
574 assert_select "a[href=/user/test/blocks]", 0
575 assert_select "a[href=/user/test/blocks_by]", 0
576 assert_select "a[href=/blocks/new/test]", 1
580 def test_user_api_read
581 # check that a visible user is returned properly
582 get :api_read, :id => users(:normal_user).id
583 assert_response :success
585 # check that we aren't revealing private information
586 assert_select "contributor-terms[pd]", false
587 assert_select "home", false
588 assert_select "languages", false
590 # check that a suspended user is not returned
591 get :api_read, :id => users(:suspended_user).id
592 assert_response :gone
594 # check that a deleted user is not returned
595 get :api_read, :id => users(:deleted_user).id
596 assert_response :gone
598 # check that a non-existent user is not returned
599 get :api_read, :id => 0
600 assert_response :not_found
603 def test_user_api_details
605 assert_response :unauthorized
607 basic_authorization(users(:normal_user).email, "test")
609 assert_response :success
612 def test_user_make_friend
613 # Get users to work with
614 user = users(:normal_user)
615 friend = users(:second_public_user)
617 # Check that the users aren't already friends
618 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
620 # Set the username cookie
621 @request.cookies["_osm_username"] = user.display_name
623 # When not logged in a GET should ask us to login
624 get :make_friend, {:display_name => friend.display_name}
625 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
627 # When not logged in a POST should error
628 post :make_friend, {:display_name => friend.display_name}
629 assert_response :forbidden
630 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
632 # When logged in a GET should get a confirmation page
633 get :make_friend, {:display_name => friend.display_name}, {"user" => user}
634 assert_response :success
635 assert_template :make_friend
636 assert_select "form" do
637 assert_select "input[type=hidden][name=referer]", 0
638 assert_select "input[type=submit]", 1
640 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
642 # The GET should preserve any referer
643 get :make_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
644 assert_response :success
645 assert_template :make_friend
646 assert_select "form" do
647 assert_select "input[type=hidden][name=referer][value=/test]", 1
648 assert_select "input[type=submit]", 1
650 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
652 # When logged in a POST should add the friendship
653 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
654 assert_redirected_to user_path(:display_name => friend.display_name)
655 assert_match /is now your friend/, flash[:notice]
656 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
658 # A second POST should report that the friendship already exists
659 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
660 assert_redirected_to user_path(:display_name => friend.display_name)
661 assert_match /You are already friends with/, flash[:warning]
662 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
665 def test_user_remove_friend
666 # Get users to work with
667 user = users(:normal_user)
668 friend = users(:public_user)
670 # Check that the users are friends
671 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
673 # Set the username cookie
674 @request.cookies["_osm_username"] = user.display_name
676 # When not logged in a GET should ask us to login
677 get :remove_friend, {:display_name => friend.display_name}
678 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
680 # When not logged in a POST should error
681 post :remove_friend, {:display_name => friend.display_name}
682 assert_response :forbidden
683 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
685 # When logged in a GET should get a confirmation page
686 get :remove_friend, {:display_name => friend.display_name}, {"user" => user}
687 assert_response :success
688 assert_template :remove_friend
689 assert_select "form" do
690 assert_select "input[type=hidden][name=referer]", 0
691 assert_select "input[type=submit]", 1
693 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
695 # The GET should preserve any referer
696 get :remove_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
697 assert_response :success
698 assert_template :remove_friend
699 assert_select "form" do
700 assert_select "input[type=hidden][name=referer][value=/test]", 1
701 assert_select "input[type=submit]", 1
703 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
705 # When logged in a POST should remove the friendship
706 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
707 assert_redirected_to user_path(:display_name => friend.display_name)
708 assert_match /was removed from your friends/, flash[:notice]
709 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
711 # A second POST should report that the friendship does not exist
712 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
713 assert_redirected_to user_path(:display_name => friend.display_name)
714 assert_match /is not one of your friends/, flash[:error]
715 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first