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/terms", :method => :get },
59 { :controller => "user", :action => "terms" }
62 { :path => "/user/terms", :method => :post },
63 { :controller => "user", :action => "terms" }
67 { :path => "/user/save", :method => :post },
68 { :controller => "user", :action => "save" }
72 { :path => "/user/username/confirm", :method => :get },
73 { :controller => "user", :action => "confirm", :display_name => "username" }
76 { :path => "/user/username/confirm", :method => :post },
77 { :controller => "user", :action => "confirm", :display_name => "username" }
80 { :path => "/user/username/confirm/resend", :method => :get },
81 { :controller => "user", :action => "confirm_resend", :display_name => "username" }
85 { :path => "/user/confirm", :method => :get },
86 { :controller => "user", :action => "confirm" }
89 { :path => "/user/confirm", :method => :post },
90 { :controller => "user", :action => "confirm" }
93 { :path => "/user/confirm-email", :method => :get },
94 { :controller => "user", :action => "confirm_email" }
97 { :path => "/user/confirm-email", :method => :post },
98 { :controller => "user", :action => "confirm_email" }
102 { :path => "/user/go_public", :method => :post },
103 { :controller => "user", :action => "go_public" }
107 { :path => "/user/forgot-password", :method => :get },
108 { :controller => "user", :action => "lost_password" }
111 { :path => "/user/forgot-password", :method => :post },
112 { :controller => "user", :action => "lost_password" }
115 { :controller => "user", :action => "lost_password" },
116 { :path => "/forgot-password.html", :method => :get }
119 { :path => "/user/reset-password", :method => :get },
120 { :controller => "user", :action => "reset_password" }
123 { :path => "/user/reset-password", :method => :post },
124 { :controller => "user", :action => "reset_password" }
128 { :path => "/user/suspended", :method => :get },
129 { :controller => "user", :action => "suspended" }
133 { :path => "/user/username", :method => :get },
134 { :controller => "user", :action => "view", :display_name => "username" }
138 { :path => "/user/username/account", :method => :get },
139 { :controller => "user", :action => "account", :display_name => "username" }
142 { :path => "/user/username/account", :method => :post },
143 { :controller => "user", :action => "account", :display_name => "username" }
147 { :path => "/user/username/make_friend", :method => :get },
148 { :controller => "user", :action => "make_friend", :display_name => "username" }
151 { :path => "/user/username/make_friend", :method => :post },
152 { :controller => "user", :action => "make_friend", :display_name => "username" }
155 { :path => "/user/username/remove_friend", :method => :get },
156 { :controller => "user", :action => "remove_friend", :display_name => "username" }
159 { :path => "/user/username/remove_friend", :method => :post },
160 { :controller => "user", :action => "remove_friend", :display_name => "username" }
164 { :path => "/user/username/set_status", :method => :get },
165 { :controller => "user", :action => "set_status", :display_name => "username" }
168 { :path => "/user/username/delete", :method => :get },
169 { :controller => "user", :action => "delete", :display_name => "username" }
173 { :path => "/users", :method => :get },
174 { :controller => "user", :action => "list" }
177 { :path => "/users", :method => :post },
178 { :controller => "user", :action => "list" }
181 { :path => "/users/status", :method => :get },
182 { :controller => "user", :action => "list", :status => "status" }
185 { :path => "/users/status", :method => :post },
186 { :controller => "user", :action => "list", :status => "status" }
190 # The user creation page loads
191 def test_user_create_view
193 assert_response :success
195 assert_select "html", :count => 1 do
196 assert_select "head", :count => 1 do
197 assert_select "title", :text => /Create account/, :count => 1
199 assert_select "body", :count => 1 do
200 assert_select "div#content", :count => 1 do
201 assert_select "form[action='/user/terms'][method=post]", :count => 1 do
202 assert_select "input[id=user_email]", :count => 1
203 assert_select "input[id=user_email_confirmation]", :count => 1
204 assert_select "input[id=user_display_name]", :count => 1
205 assert_select "input[id=user_pass_crypt][type=password]", :count => 1
206 assert_select "input[id=user_pass_crypt_confirmation][type=password]", :count => 1
207 assert_select "input[type=submit][value=Continue]", :count => 1
214 def test_user_create_success
215 new_email = "newtester@osm.org"
216 display_name = "new_tester"
217 assert_difference('User.count') do
218 assert_difference('ActionMailer::Base.deliveries.size') do
219 session[:new_user] = User.new({
220 :status => "pending", :display_name => display_name,
221 :email => new_email, :email_confirmation => new_email,
222 :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"
223 }, :without_protection => true)
230 register_email = ActionMailer::Base.deliveries.first
232 assert_equal register_email.to[0], new_email
233 assert_match /#{@url}/, register_email.body.to_s
236 assert_redirected_to :action => 'login', :referer => nil
238 ActionMailer::Base.deliveries.clear
241 def test_user_create_submit_duplicate_email
242 email = users(:public_user).email
243 display_name = "new_tester"
244 assert_difference('User.count', 0) do
245 assert_difference('ActionMailer::Base.deliveries.size', 0) do
246 session[:new_user] = User.new({
247 :status => "pending", :display_name => display_name,
248 :email => email, :email_confirmation => email,
249 :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"
250 }, :without_protection => true)
255 assert_response :success
256 assert_template 'new'
257 assert_select "div#errorExplanation"
258 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_email"
261 def test_user_create_submit_duplicate_email_uppercase
262 email = users(:public_user).email.upcase
263 display_name = "new_tester"
264 assert_difference('User.count', 0) do
265 assert_difference('ActionMailer::Base.deliveries.size', 0) do
266 session[:new_user] = User.new({
267 :status => "pending", :display_name => display_name,
268 :email => email, :email_confirmation => email,
269 :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"
270 }, :without_protection => true)
275 assert_response :success
276 assert_template 'new'
277 assert_select "div#errorExplanation"
278 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_email"
281 def test_user_create_submit_duplicate_name
282 email = "new_tester@example.com"
283 display_name = users(:public_user).display_name
284 assert_difference('User.count', 0) do
285 assert_difference('ActionMailer::Base.deliveries.size', 0) do
286 session[:new_user] = User.new({
287 :status => "pending", :display_name => display_name,
288 :email => email, :email_confirmation => email,
289 :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"
290 }, :without_protection => true)
295 assert_response :success
296 assert_template 'new'
297 assert_select "div#errorExplanation"
298 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_display_name"
301 def test_user_create_submit_duplicate_name_uppercase
302 email = "new_tester@example.com"
303 display_name = users(:public_user).display_name.upcase
304 assert_difference('User.count', 0) do
305 assert_difference('ActionMailer::Base.deliveries.size', 0) do
306 session[:new_user] = User.new({
307 :status => "pending", :display_name => display_name,
308 :email => email, :email_confirmation => email,
309 :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"
310 }, :without_protection => true)
315 assert_response :success
316 assert_template 'new'
317 assert_select "div#errorExplanation"
318 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_display_name"
321 def test_user_lost_password
322 # Test fetching the lost password page
324 assert_response :success
325 assert_template :lost_password
326 assert_select "div#notice", false
328 # Test resetting using the address as recorded for a user that has an
329 # address which is duplicated in a different case by another user
330 assert_difference('ActionMailer::Base.deliveries.size', 1) do
331 post :lost_password, :user => { :email => users(:normal_user).email }
333 assert_response :redirect
334 assert_redirected_to :action => :login
335 assert_match /^Sorry you lost it/, flash[:notice]
336 assert_equal users(:normal_user).email, ActionMailer::Base.deliveries.last.to[0]
338 # Test resetting using an address that matches a different user
339 # that has the same address in a different case
340 assert_difference('ActionMailer::Base.deliveries.size', 1) do
341 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
343 assert_response :redirect
344 assert_redirected_to :action => :login
345 assert_match /^Sorry you lost it/, flash[:notice]
346 assert_equal users(:uppercase_user).email, ActionMailer::Base.deliveries.last.to[0]
348 # Test resetting using an address that is a case insensitive match
349 # for more than one user but not an exact match for either
350 assert_difference('ActionMailer::Base.deliveries.size', 0) do
351 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
353 assert_response :success
354 assert_template :lost_password
355 assert_select "div#error", /^Could not find that email address/
357 # Test resetting using the address as recorded for a user that has an
358 # address which is case insensitively unique
359 assert_difference('ActionMailer::Base.deliveries.size', 1) do
360 post :lost_password, :user => { :email => users(:public_user).email }
362 assert_response :redirect
363 assert_redirected_to :action => :login
364 assert_match /^Sorry you lost it/, flash[:notice]
365 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
367 # Test resetting using an address that matches a user that has the
368 # same (case insensitively unique) address in a different case
369 assert_difference('ActionMailer::Base.deliveries.size', 1) do
370 post :lost_password, :user => { :email => users(:public_user).email.upcase }
372 assert_response :redirect
373 assert_redirected_to :action => :login
374 assert_match /^Sorry you lost it/, flash[:notice]
375 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
379 # Get a user to work with - note that this user deliberately
380 # conflicts with uppercase_user in the email and display name
381 # fields to test that we can change other fields without any
382 # validation errors being reported
383 user = users(:normal_user)
385 # Set the username cookie
386 @request.cookies["_osm_username"] = user.display_name
388 # Make sure that you are redirected to the login page when
389 # you are not logged in
390 get :account, { :display_name => user.display_name }
391 assert_response :redirect
392 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
394 # Make sure that you are redirected to the login page when
395 # you are not logged in as the right user
396 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
397 assert_response :redirect
398 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
400 # Make sure we get the page when we are logged in as the right user
401 get :account, { :display_name => user.display_name }, { "user" => user }
402 assert_response :success
403 assert_template :account
405 # Updating the description should work
406 user.description = "new description"
407 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
408 assert_response :success
409 assert_template :account
410 assert_select "div#errorExplanation", false
411 assert_select "div#notice", /^User information updated successfully/
412 assert_select "table#accountForm > tr > td > div#user_description_container > div#user_description_content > textarea#user_description", user.description
414 # Changing name to one that exists should fail
415 user.display_name = users(:public_user).display_name
416 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
417 assert_response :success
418 assert_template :account
419 assert_select "div#notice", false
420 assert_select "div#errorExplanation"
421 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_display_name"
423 # Changing name to one that exists should fail, regardless of case
424 user.display_name = users(:public_user).display_name.upcase
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#notice", false
429 assert_select "div#errorExplanation"
430 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_display_name"
432 # Changing name to one that doesn't exist should work
433 user.display_name = "new tester"
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#errorExplanation", false
438 assert_select "div#notice", /^User information updated successfully/
439 assert_select "table#accountForm > tr > td > input#user_display_name[value=?]", user.display_name
441 # Need to update cookies now to stay valid
442 @request.cookies["_osm_username"] = user.display_name
444 # Changing email to one that exists should fail
445 user.new_email = users(:public_user).email
446 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
447 assert_response :success
448 assert_template :account
449 assert_select "div#notice", false
450 assert_select "div#errorExplanation"
451 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_new_email"
453 # Changing email to one that exists should fail, regardless of case
454 user.new_email = users(:public_user).email.upcase
455 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
456 assert_response :success
457 assert_template :account
458 assert_select "div#notice", false
459 assert_select "div#errorExplanation"
460 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_new_email"
462 # Changing email to one that doesn't exist should work
463 user.new_email = "new_tester@example.com"
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#errorExplanation", false
468 assert_select "div#notice", /^User information updated successfully/
469 assert_select "table#accountForm > tr > td > input#user_new_email[value=?]", user.new_email
472 # Check that the user account page will display and contains some relevant
473 # information for the user
474 def test_user_view_account
475 # Test a non-existent user
476 get :view, {:display_name => "unknown"}
477 assert_response :not_found
480 get :view, {:display_name => "test"}
481 assert_response :success
482 assert_select "div#userinformation" do
483 assert_select "a[href=/user/test/edits]", 1
484 assert_select "a[href=/user/test/traces]", 1
485 assert_select "a[href=/user/test/diary]", 1
486 assert_select "a[href=/user/test/diary/comments]", 1
487 assert_select "a[href=/user/test/account]", 0
488 assert_select "a[href=/user/test/blocks]", 0
489 assert_select "a[href=/user/test/blocks_by]", 0
490 assert_select "a[href=/blocks/new/test]", 0
493 # Test a user who has been blocked
494 get :view, {:display_name => "blocked"}
495 assert_response :success
496 assert_select "div#userinformation" do
497 assert_select "a[href=/user/blocked/edits]", 1
498 assert_select "a[href=/user/blocked/traces]", 1
499 assert_select "a[href=/user/blocked/diary]", 1
500 assert_select "a[href=/user/blocked/diary/comments]", 1
501 assert_select "a[href=/user/blocked/account]", 0
502 assert_select "a[href=/user/blocked/blocks]", 1
503 assert_select "a[href=/user/blocked/blocks_by]", 0
504 assert_select "a[href=/blocks/new/blocked]", 0
507 # Test a moderator who has applied blocks
508 get :view, {:display_name => "moderator"}
509 assert_response :success
510 assert_select "div#userinformation" do
511 assert_select "a[href=/user/moderator/edits]", 1
512 assert_select "a[href=/user/moderator/traces]", 1
513 assert_select "a[href=/user/moderator/diary]", 1
514 assert_select "a[href=/user/moderator/diary/comments]", 1
515 assert_select "a[href=/user/moderator/account]", 0
516 assert_select "a[href=/user/moderator/blocks]", 0
517 assert_select "a[href=/user/moderator/blocks_by]", 1
518 assert_select "a[href=/blocks/new/moderator]", 0
521 # Login as a normal user
522 session[:user] = users(:normal_user).id
523 cookies["_osm_username"] = users(:normal_user).display_name
525 # Test the normal user
526 get :view, {:display_name => "test"}
527 assert_response :success
528 assert_select "div#userinformation" do
529 assert_select "a[href=/user/test/edits]", 1
530 assert_select "a[href=/traces/mine]", 1
531 assert_select "a[href=/user/test/diary]", 1
532 assert_select "a[href=/user/test/diary/comments]", 1
533 assert_select "a[href=/user/test/account]", 1
534 assert_select "a[href=/user/test/blocks]", 0
535 assert_select "a[href=/user/test/blocks_by]", 0
536 assert_select "a[href=/blocks/new/test]", 0
539 # Login as a moderator
540 session[:user] = users(:moderator_user).id
541 cookies["_osm_username"] = users(:moderator_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=/user/test/traces]", 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]", 0
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]", 1
558 def test_user_api_read
559 # check that a visible user is returned properly
560 get :api_read, :id => users(:normal_user).id
561 assert_response :success
563 # check that we aren't revealing private information
564 assert_select "contributor-terms[pd]", false
565 assert_select "home", false
566 assert_select "languages", false
568 # check that a suspended user is not returned
569 get :api_read, :id => users(:suspended_user).id
570 assert_response :gone
572 # check that a deleted user is not returned
573 get :api_read, :id => users(:deleted_user).id
574 assert_response :gone
576 # check that a non-existent user is not returned
577 get :api_read, :id => 0
578 assert_response :not_found
581 def test_user_api_details
583 assert_response :unauthorized
585 basic_authorization(users(:normal_user).email, "test")
587 assert_response :success
590 def test_user_make_friend
591 # Get users to work with
592 user = users(:normal_user)
593 friend = users(:second_public_user)
595 # Check that the users aren't already friends
596 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
598 # Set the username cookie
599 @request.cookies["_osm_username"] = user.display_name
601 # When not logged in a GET should ask us to login
602 get :make_friend, {:display_name => friend.display_name}
603 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
605 # When not logged in a POST should error
606 post :make_friend, {:display_name => friend.display_name}
607 assert_response :forbidden
608 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
610 # When logged in a GET should get a confirmation page
611 get :make_friend, {:display_name => friend.display_name}, {"user" => user}
612 assert_response :success
613 assert_template :make_friend
614 assert_select "form" do
615 assert_select "input[type=hidden][name=referer]", 0
616 assert_select "input[type=submit]", 1
618 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
620 # The GET should preserve any referer
621 get :make_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
622 assert_response :success
623 assert_template :make_friend
624 assert_select "form" do
625 assert_select "input[type=hidden][name=referer][value=/test]", 1
626 assert_select "input[type=submit]", 1
628 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
630 # When logged in a POST should add the friendship
631 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
632 assert_redirected_to user_path(:display_name => friend.display_name)
633 assert_match /is now your friend/, flash[:notice]
634 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
636 # A second POST should report that the friendship already exists
637 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
638 assert_redirected_to user_path(:display_name => friend.display_name)
639 assert_match /You are already friends with/, flash[:warning]
640 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
643 def test_user_remove_friend
644 # Get users to work with
645 user = users(:normal_user)
646 friend = users(:public_user)
648 # Check that the users are friends
649 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
651 # Set the username cookie
652 @request.cookies["_osm_username"] = user.display_name
654 # When not logged in a GET should ask us to login
655 get :remove_friend, {:display_name => friend.display_name}
656 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
658 # When not logged in a POST should error
659 post :remove_friend, {:display_name => friend.display_name}
660 assert_response :forbidden
661 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
663 # When logged in a GET should get a confirmation page
664 get :remove_friend, {:display_name => friend.display_name}, {"user" => user}
665 assert_response :success
666 assert_template :remove_friend
667 assert_select "form" do
668 assert_select "input[type=hidden][name=referer]", 0
669 assert_select "input[type=submit]", 1
671 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
673 # The GET should preserve any referer
674 get :remove_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
675 assert_response :success
676 assert_template :remove_friend
677 assert_select "form" do
678 assert_select "input[type=hidden][name=referer][value=/test]", 1
679 assert_select "input[type=submit]", 1
681 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
683 # When logged in a POST should remove the friendship
684 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
685 assert_redirected_to user_path(:display_name => friend.display_name)
686 assert_match /was removed from your friends/, flash[:notice]
687 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
689 # A second POST should report that the friendship does not exist
690 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
691 assert_redirected_to user_path(:display_name => friend.display_name)
692 assert_match /is not one of your friends/, flash[:error]
693 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first