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 post :save, {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}}
224 register_email = ActionMailer::Base.deliveries.first
226 assert_equal register_email.to[0], new_email
227 assert_match /#{@url}/, register_email.body.to_s
230 assert_redirected_to :action => 'login', :referer => nil
232 ActionMailer::Base.deliveries.clear
235 def test_user_create_submit_duplicate_email
236 email = users(:public_user).email
237 display_name = "new_tester"
238 assert_difference('User.count', 0) do
239 assert_difference('ActionMailer::Base.deliveries.size', 0) do
240 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
243 assert_response :success
244 assert_template 'new'
245 assert_select "div#errorExplanation"
246 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_email"
249 def test_user_create_submit_duplicate_email_uppercase
250 email = users(:public_user).email.upcase
251 display_name = "new_tester"
252 assert_difference('User.count', 0) do
253 assert_difference('ActionMailer::Base.deliveries.size', 0) do
254 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
257 assert_response :success
258 assert_template 'new'
259 assert_select "div#errorExplanation"
260 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_email"
263 def test_user_create_submit_duplicate_name
264 email = "new_tester@example.com"
265 display_name = users(:public_user).display_name
266 assert_difference('User.count', 0) do
267 assert_difference('ActionMailer::Base.deliveries.size', 0) do
268 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
271 assert_response :success
272 assert_template 'new'
273 assert_select "div#errorExplanation"
274 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_display_name"
277 def test_user_create_submit_duplicate_name_uppercase
278 email = "new_tester@example.com"
279 display_name = users(:public_user).display_name.upcase
280 assert_difference('User.count', 0) do
281 assert_difference('ActionMailer::Base.deliveries.size', 0) do
282 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
285 assert_response :success
286 assert_template 'new'
287 assert_select "div#errorExplanation"
288 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_display_name"
291 def test_user_lost_password
292 # Test fetching the lost password page
294 assert_response :success
295 assert_template :lost_password
296 assert_select "div#notice", false
298 # Test resetting using the address as recorded for a user that has an
299 # address which is duplicated in a different case by another user
300 assert_difference('ActionMailer::Base.deliveries.size', 1) do
301 post :lost_password, :user => { :email => users(:normal_user).email }
303 assert_response :redirect
304 assert_redirected_to :action => :login
305 assert_match /^Sorry you lost it/, flash[:notice]
306 assert_equal users(:normal_user).email, ActionMailer::Base.deliveries.last.to[0]
308 # Test resetting using an address that matches a different user
309 # that has the same address in a different case
310 assert_difference('ActionMailer::Base.deliveries.size', 1) do
311 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
313 assert_response :redirect
314 assert_redirected_to :action => :login
315 assert_match /^Sorry you lost it/, flash[:notice]
316 assert_equal users(:uppercase_user).email, ActionMailer::Base.deliveries.last.to[0]
318 # Test resetting using an address that is a case insensitive match
319 # for more than one user but not an exact match for either
320 assert_difference('ActionMailer::Base.deliveries.size', 0) do
321 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
323 assert_response :success
324 assert_template :lost_password
325 assert_select "div#error", /^Could not find that email address/
327 # Test resetting using the address as recorded for a user that has an
328 # address which is case insensitively unique
329 assert_difference('ActionMailer::Base.deliveries.size', 1) do
330 post :lost_password, :user => { :email => users(:public_user).email }
332 assert_response :redirect
333 assert_redirected_to :action => :login
334 assert_match /^Sorry you lost it/, flash[:notice]
335 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
337 # Test resetting using an address that matches a user that has the
338 # same (case insensitively unique) address in a different case
339 assert_difference('ActionMailer::Base.deliveries.size', 1) do
340 post :lost_password, :user => { :email => users(:public_user).email.upcase }
342 assert_response :redirect
343 assert_redirected_to :action => :login
344 assert_match /^Sorry you lost it/, flash[:notice]
345 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
349 # Get a user to work with - note that this user deliberately
350 # conflicts with uppercase_user in the email and display name
351 # fields to test that we can change other fields without any
352 # validation errors being reported
353 user = users(:normal_user)
355 # Set the username cookie
356 @request.cookies["_osm_username"] = user.display_name
358 # Make sure that you are redirected to the login page when
359 # you are not logged in
360 get :account, { :display_name => user.display_name }
361 assert_response :redirect
362 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
364 # Make sure that you are redirected to the login page when
365 # you are not logged in as the right user
366 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
367 assert_response :redirect
368 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
370 # Make sure we get the page when we are logged in as the right user
371 get :account, { :display_name => user.display_name }, { "user" => user }
372 assert_response :success
373 assert_template :account
375 # Updating the description should work
376 user.description = "new description"
377 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
378 assert_response :success
379 assert_template :account
380 assert_select "div#errorExplanation", false
381 assert_select "div#notice", /^User information updated successfully/
382 assert_select "table#accountForm > tr > td > div#user_description_container > div#user_description_content > textarea#user_description", user.description
384 # Changing name to one that exists should fail
385 user.display_name = users(:public_user).display_name
386 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
387 assert_response :success
388 assert_template :account
389 assert_select "div#notice", false
390 assert_select "div#errorExplanation"
391 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_display_name"
393 # Changing name to one that exists should fail, regardless of case
394 user.display_name = users(:public_user).display_name.upcase
395 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
396 assert_response :success
397 assert_template :account
398 assert_select "div#notice", false
399 assert_select "div#errorExplanation"
400 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_display_name"
402 # Changing name to one that doesn't exist should work
403 user.display_name = "new tester"
404 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
405 assert_response :success
406 assert_template :account
407 assert_select "div#errorExplanation", false
408 assert_select "div#notice", /^User information updated successfully/
409 assert_select "table#accountForm > tr > td > input#user_display_name[value=?]", user.display_name
411 # Need to update cookies now to stay valid
412 @request.cookies["_osm_username"] = user.display_name
414 # Changing email to one that exists should fail
415 user.new_email = users(:public_user).email
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_new_email"
423 # Changing email to one that exists should fail, regardless of case
424 user.new_email = users(:public_user).email.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_new_email"
432 # Changing email to one that doesn't exist should work
433 user.new_email = "new_tester@example.com"
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_new_email[value=?]", user.new_email
442 # Check that the user account page will display and contains some relevant
443 # information for the user
444 def test_user_view_account
445 # Test a non-existent user
446 get :view, {:display_name => "unknown"}
447 assert_response :not_found
450 get :view, {:display_name => "test"}
451 assert_response :success
452 assert_select "div#userinformation" do
453 assert_select "a[href=/user/test/edits]", 1
454 assert_select "a[href=/user/test/traces]", 1
455 assert_select "a[href=/user/test/diary]", 1
456 assert_select "a[href=/user/test/diary/comments]", 1
457 assert_select "a[href=/user/test/account]", 0
458 assert_select "a[href=/user/test/blocks]", 0
459 assert_select "a[href=/user/test/blocks_by]", 0
460 assert_select "a[href=/blocks/new/test]", 0
463 # Test a user who has been blocked
464 get :view, {:display_name => "blocked"}
465 assert_response :success
466 assert_select "div#userinformation" do
467 assert_select "a[href=/user/blocked/edits]", 1
468 assert_select "a[href=/user/blocked/traces]", 1
469 assert_select "a[href=/user/blocked/diary]", 1
470 assert_select "a[href=/user/blocked/diary/comments]", 1
471 assert_select "a[href=/user/blocked/account]", 0
472 assert_select "a[href=/user/blocked/blocks]", 1
473 assert_select "a[href=/user/blocked/blocks_by]", 0
474 assert_select "a[href=/blocks/new/blocked]", 0
477 # Test a moderator who has applied blocks
478 get :view, {:display_name => "moderator"}
479 assert_response :success
480 assert_select "div#userinformation" do
481 assert_select "a[href=/user/moderator/edits]", 1
482 assert_select "a[href=/user/moderator/traces]", 1
483 assert_select "a[href=/user/moderator/diary]", 1
484 assert_select "a[href=/user/moderator/diary/comments]", 1
485 assert_select "a[href=/user/moderator/account]", 0
486 assert_select "a[href=/user/moderator/blocks]", 0
487 assert_select "a[href=/user/moderator/blocks_by]", 1
488 assert_select "a[href=/blocks/new/moderator]", 0
491 # Login as a normal user
492 session[:user] = users(:normal_user).id
493 cookies["_osm_username"] = users(:normal_user).display_name
495 # Test the normal user
496 get :view, {:display_name => "test"}
497 assert_response :success
498 assert_select "div#userinformation" do
499 assert_select "a[href=/user/test/edits]", 1
500 assert_select "a[href=/traces/mine]", 1
501 assert_select "a[href=/user/test/diary]", 1
502 assert_select "a[href=/user/test/diary/comments]", 1
503 assert_select "a[href=/user/test/account]", 1
504 assert_select "a[href=/user/test/blocks]", 0
505 assert_select "a[href=/user/test/blocks_by]", 0
506 assert_select "a[href=/blocks/new/test]", 0
509 # Login as a moderator
510 session[:user] = users(:moderator_user).id
511 cookies["_osm_username"] = users(:moderator_user).display_name
513 # Test the normal user
514 get :view, {:display_name => "test"}
515 assert_response :success
516 assert_select "div#userinformation" do
517 assert_select "a[href=/user/test/edits]", 1
518 assert_select "a[href=/user/test/traces]", 1
519 assert_select "a[href=/user/test/diary]", 1
520 assert_select "a[href=/user/test/diary/comments]", 1
521 assert_select "a[href=/user/test/account]", 0
522 assert_select "a[href=/user/test/blocks]", 0
523 assert_select "a[href=/user/test/blocks_by]", 0
524 assert_select "a[href=/blocks/new/test]", 1
528 def test_user_api_read
529 # check that a visible user is returned properly
530 get :api_read, :id => users(:normal_user).id
531 assert_response :success
533 # check that we aren't revealing private information
534 assert_select "contributor-terms[pd]", false
535 assert_select "home", false
536 assert_select "languages", false
538 # check that a suspended user is not returned
539 get :api_read, :id => users(:suspended_user).id
540 assert_response :gone
542 # check that a deleted user is not returned
543 get :api_read, :id => users(:deleted_user).id
544 assert_response :gone
546 # check that a non-existent user is not returned
547 get :api_read, :id => 0
548 assert_response :not_found
551 def test_user_api_details
553 assert_response :unauthorized
555 basic_authorization(users(:normal_user).email, "test")
557 assert_response :success
560 def test_user_make_friend
561 # Get users to work with
562 user = users(:normal_user)
563 friend = users(:second_public_user)
565 # Check that the users aren't already friends
566 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
568 # Set the username cookie
569 @request.cookies["_osm_username"] = user.display_name
571 # When not logged in a GET should ask us to login
572 get :make_friend, {:display_name => friend.display_name}
573 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
575 # When not logged in a POST should error
576 post :make_friend, {:display_name => friend.display_name}
577 assert_response :forbidden
578 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
580 # When logged in a GET should get a confirmation page
581 get :make_friend, {:display_name => friend.display_name}, {"user" => user}
582 assert_response :success
583 assert_template :make_friend
584 assert_select "form" do
585 assert_select "input[type=hidden][name=referer]", 0
586 assert_select "input[type=submit]", 1
588 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
590 # The GET should preserve any referer
591 get :make_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
592 assert_response :success
593 assert_template :make_friend
594 assert_select "form" do
595 assert_select "input[type=hidden][name=referer][value=/test]", 1
596 assert_select "input[type=submit]", 1
598 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
600 # When logged in a POST should add the friendship
601 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
602 assert_redirected_to user_path(:display_name => friend.display_name)
603 assert_match /is now your friend/, flash[:notice]
604 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
606 # A second POST should report that the friendship already exists
607 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
608 assert_redirected_to user_path(:display_name => friend.display_name)
609 assert_match /You are already friends with/, flash[:warning]
610 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
613 def test_user_remove_friend
614 # Get users to work with
615 user = users(:normal_user)
616 friend = users(:public_user)
618 # Check that the users are friends
619 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
621 # Set the username cookie
622 @request.cookies["_osm_username"] = user.display_name
624 # When not logged in a GET should ask us to login
625 get :remove_friend, {:display_name => friend.display_name}
626 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
628 # When not logged in a POST should error
629 post :remove_friend, {:display_name => friend.display_name}
630 assert_response :forbidden
631 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
633 # When logged in a GET should get a confirmation page
634 get :remove_friend, {:display_name => friend.display_name}, {"user" => user}
635 assert_response :success
636 assert_template :remove_friend
637 assert_select "form" do
638 assert_select "input[type=hidden][name=referer]", 0
639 assert_select "input[type=submit]", 1
641 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
643 # The GET should preserve any referer
644 get :remove_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
645 assert_response :success
646 assert_template :remove_friend
647 assert_select "form" do
648 assert_select "input[type=hidden][name=referer][value=/test]", 1
649 assert_select "input[type=submit]", 1
651 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
653 # When logged in a POST should remove the friendship
654 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
655 assert_redirected_to user_path(:display_name => friend.display_name)
656 assert_match /was removed from your friends/, flash[:notice]
657 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
659 # A second POST should report that the friendship does not exist
660 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
661 assert_redirected_to user_path(:display_name => friend.display_name)
662 assert_match /is not one of your friends/, flash[:error]
663 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first