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/details", :method => :get },
11 { :controller => "user", :action => "api_details" }
14 { :path => "/api/0.6/user/gpx_files", :method => :get },
15 { :controller => "user", :action => "api_gpx_files" }
19 { :path => "/login", :method => :get },
20 { :controller => "user", :action => "login" }
23 { :path => "/login", :method => :post },
24 { :controller => "user", :action => "login" }
27 { :controller => "user", :action => "login", :format => "html" },
28 { :path => "/login.html", :method => :get }
32 { :path => "/logout", :method => :get },
33 { :controller => "user", :action => "logout" }
36 { :path => "/logout", :method => :post },
37 { :controller => "user", :action => "logout" }
40 { :controller => "user", :action => "logout", :format => "html" },
41 { :path => "/logout.html", :method => :get }
45 { :path => "/user/new", :method => :get },
46 { :controller => "user", :action => "new" }
49 { :controller => "user", :action => "new" },
50 { :path => "/create-account.html", :method => :get }
54 { :path => "/user/terms", :method => :get },
55 { :controller => "user", :action => "terms" }
58 { :path => "/user/terms", :method => :post },
59 { :controller => "user", :action => "terms" }
63 { :path => "/user/save", :method => :post },
64 { :controller => "user", :action => "save" }
68 { :path => "/user/username/confirm", :method => :get },
69 { :controller => "user", :action => "confirm", :display_name => "username" }
72 { :path => "/user/username/confirm", :method => :post },
73 { :controller => "user", :action => "confirm", :display_name => "username" }
76 { :path => "/user/username/confirm/resend", :method => :get },
77 { :controller => "user", :action => "confirm_resend", :display_name => "username" }
81 { :path => "/user/confirm", :method => :get },
82 { :controller => "user", :action => "confirm" }
85 { :path => "/user/confirm", :method => :post },
86 { :controller => "user", :action => "confirm" }
89 { :path => "/user/confirm-email", :method => :get },
90 { :controller => "user", :action => "confirm_email" }
93 { :path => "/user/confirm-email", :method => :post },
94 { :controller => "user", :action => "confirm_email" }
98 { :path => "/user/go_public", :method => :post },
99 { :controller => "user", :action => "go_public" }
103 { :path => "/user/forgot-password", :method => :get },
104 { :controller => "user", :action => "lost_password" }
107 { :path => "/user/forgot-password", :method => :post },
108 { :controller => "user", :action => "lost_password" }
111 { :controller => "user", :action => "lost_password" },
112 { :path => "/forgot-password.html", :method => :get }
115 { :path => "/user/reset-password", :method => :get },
116 { :controller => "user", :action => "reset_password" }
119 { :path => "/user/reset-password", :method => :post },
120 { :controller => "user", :action => "reset_password" }
124 { :path => "/user/suspended", :method => :get },
125 { :controller => "user", :action => "suspended" }
129 { :path => "/user/username", :method => :get },
130 { :controller => "user", :action => "view", :display_name => "username" }
134 { :path => "/user/username/account", :method => :get },
135 { :controller => "user", :action => "account", :display_name => "username" }
138 { :path => "/user/username/account", :method => :post },
139 { :controller => "user", :action => "account", :display_name => "username" }
143 { :path => "/user/username/make_friend", :method => :get },
144 { :controller => "user", :action => "make_friend", :display_name => "username" }
147 { :path => "/user/username/make_friend", :method => :post },
148 { :controller => "user", :action => "make_friend", :display_name => "username" }
151 { :path => "/user/username/remove_friend", :method => :get },
152 { :controller => "user", :action => "remove_friend", :display_name => "username" }
155 { :path => "/user/username/remove_friend", :method => :post },
156 { :controller => "user", :action => "remove_friend", :display_name => "username" }
160 { :path => "/user/username/set_status", :method => :get },
161 { :controller => "user", :action => "set_status", :display_name => "username" }
164 { :path => "/user/username/delete", :method => :get },
165 { :controller => "user", :action => "delete", :display_name => "username" }
169 { :path => "/users", :method => :get },
170 { :controller => "user", :action => "list" }
173 { :path => "/users", :method => :post },
174 { :controller => "user", :action => "list" }
177 { :path => "/users/status", :method => :get },
178 { :controller => "user", :action => "list", :status => "status" }
181 { :path => "/users/status", :method => :post },
182 { :controller => "user", :action => "list", :status => "status" }
186 # The user creation page loads
187 def test_user_create_view
189 assert_response :success
191 assert_select "html", :count => 1 do
192 assert_select "head", :count => 1 do
193 assert_select "title", :text => /Create account/, :count => 1
195 assert_select "body", :count => 1 do
196 assert_select "div#content", :count => 1 do
197 assert_select "form[action='/user/terms'][method=post]", :count => 1 do
198 assert_select "input[id=user_email]", :count => 1
199 assert_select "input[id=user_email_confirmation]", :count => 1
200 assert_select "input[id=user_display_name]", :count => 1
201 assert_select "input[id=user_pass_crypt][type=password]", :count => 1
202 assert_select "input[id=user_pass_crypt_confirmation][type=password]", :count => 1
203 assert_select "input[type=submit][value=Continue]", :count => 1
210 def test_user_create_success
211 new_email = "newtester@osm.org"
212 display_name = "new_tester"
213 assert_difference('User.count') do
214 assert_difference('ActionMailer::Base.deliveries.size') do
215 post :save, {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}}
220 register_email = ActionMailer::Base.deliveries.first
222 assert_equal register_email.to[0], new_email
223 assert_match /#{@url}/, register_email.body.to_s
226 assert_redirected_to :action => 'login', :referer => nil
228 ActionMailer::Base.deliveries.clear
231 def test_user_create_submit_duplicate_email
232 email = users(:public_user).email
233 display_name = "new_tester"
234 assert_difference('User.count', 0) do
235 assert_difference('ActionMailer::Base.deliveries.size', 0) do
236 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
239 assert_response :success
240 assert_template 'new'
241 assert_select "div#errorExplanation"
242 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_email"
245 def test_user_create_submit_duplicate_email_uppercase
246 email = users(:public_user).email.upcase
247 display_name = "new_tester"
248 assert_difference('User.count', 0) do
249 assert_difference('ActionMailer::Base.deliveries.size', 0) do
250 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
253 assert_response :success
254 assert_template 'new'
255 assert_select "div#errorExplanation"
256 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_email"
259 def test_user_create_submit_duplicate_name
260 email = "new_tester@example.com"
261 display_name = users(:public_user).display_name
262 assert_difference('User.count', 0) do
263 assert_difference('ActionMailer::Base.deliveries.size', 0) do
264 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
267 assert_response :success
268 assert_template 'new'
269 assert_select "div#errorExplanation"
270 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_display_name"
273 def test_user_create_submit_duplicate_name_uppercase
274 email = "new_tester@example.com"
275 display_name = users(:public_user).display_name.upcase
276 assert_difference('User.count', 0) do
277 assert_difference('ActionMailer::Base.deliveries.size', 0) do
278 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
281 assert_response :success
282 assert_template 'new'
283 assert_select "div#errorExplanation"
284 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_display_name"
287 def test_user_lost_password
288 # Test fetching the lost password page
290 assert_response :success
291 assert_template :lost_password
292 assert_select "div#notice", false
294 # Test resetting using the address as recorded for a user that has an
295 # address which is duplicated in a different case by another user
296 assert_difference('ActionMailer::Base.deliveries.size', 1) do
297 post :lost_password, :user => { :email => users(:normal_user).email }
299 assert_response :redirect
300 assert_redirected_to :action => :login
301 assert_match /^Sorry you lost it/, flash[:notice]
302 assert_equal users(:normal_user).email, ActionMailer::Base.deliveries.last.to[0]
304 # Test resetting using an address that matches a different user
305 # that has the same address in a different case
306 assert_difference('ActionMailer::Base.deliveries.size', 1) do
307 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
309 assert_response :redirect
310 assert_redirected_to :action => :login
311 assert_match /^Sorry you lost it/, flash[:notice]
312 assert_equal users(:uppercase_user).email, ActionMailer::Base.deliveries.last.to[0]
314 # Test resetting using an address that is a case insensitive match
315 # for more than one user but not an exact match for either
316 assert_difference('ActionMailer::Base.deliveries.size', 0) do
317 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
319 assert_response :success
320 assert_template :lost_password
321 assert_select "div#error", /^Could not find that email address/
323 # Test resetting using the address as recorded for a user that has an
324 # address which is case insensitively unique
325 assert_difference('ActionMailer::Base.deliveries.size', 1) do
326 post :lost_password, :user => { :email => users(:public_user).email }
328 assert_response :redirect
329 assert_redirected_to :action => :login
330 assert_match /^Sorry you lost it/, flash[:notice]
331 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
333 # Test resetting using an address that matches a user that has the
334 # same (case insensitively unique) address in a different case
335 assert_difference('ActionMailer::Base.deliveries.size', 1) do
336 post :lost_password, :user => { :email => users(:public_user).email.upcase }
338 assert_response :redirect
339 assert_redirected_to :action => :login
340 assert_match /^Sorry you lost it/, flash[:notice]
341 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
345 # Get a user to work with - note that this user deliberately
346 # conflicts with uppercase_user in the email and display name
347 # fields to test that we can change other fields without any
348 # validation errors being reported
349 user = users(:normal_user)
351 # Set the username cookie
352 @request.cookies["_osm_username"] = user.display_name
354 # Make sure that you are redirected to the login page when
355 # you are not logged in
356 get :account, { :display_name => user.display_name }
357 assert_response :redirect
358 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
360 # Make sure that you are redirected to the login page when
361 # you are not logged in as the right user
362 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
363 assert_response :redirect
364 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
366 # Make sure we get the page when we are logged in as the right user
367 get :account, { :display_name => user.display_name }, { "user" => user }
368 assert_response :success
369 assert_template :account
371 # Updating the description should work
372 user.description = "new description"
373 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
374 assert_response :success
375 assert_template :account
376 assert_select "div#errorExplanation", false
377 assert_select "div#notice", /^User information updated successfully/
378 assert_select "table#accountForm > tr > td > div#user_description_container > div#user_description_content > textarea#user_description", user.description
380 # Changing name to one that exists should fail
381 user.display_name = users(:public_user).display_name
382 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
383 assert_response :success
384 assert_template :account
385 assert_select "div#notice", false
386 assert_select "div#errorExplanation"
387 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_display_name"
389 # Changing name to one that exists should fail, regardless of case
390 user.display_name = users(:public_user).display_name.upcase
391 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
392 assert_response :success
393 assert_template :account
394 assert_select "div#notice", false
395 assert_select "div#errorExplanation"
396 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_display_name"
398 # Changing name to one that doesn't exist should work
399 user.display_name = "new tester"
400 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
401 assert_response :success
402 assert_template :account
403 assert_select "div#errorExplanation", false
404 assert_select "div#notice", /^User information updated successfully/
405 assert_select "table#accountForm > tr > td > input#user_display_name[value=?]", user.display_name
407 # Need to update cookies now to stay valid
408 @request.cookies["_osm_username"] = user.display_name
410 # Changing email to one that exists should fail
411 user.new_email = users(:public_user).email
412 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
413 assert_response :success
414 assert_template :account
415 assert_select "div#notice", false
416 assert_select "div#errorExplanation"
417 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_new_email"
419 # Changing email to one that exists should fail, regardless of case
420 user.new_email = users(:public_user).email.upcase
421 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
422 assert_response :success
423 assert_template :account
424 assert_select "div#notice", false
425 assert_select "div#errorExplanation"
426 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_new_email"
428 # Changing email to one that doesn't exist should work
429 user.new_email = "new_tester@example.com"
430 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
431 assert_response :success
432 assert_template :account
433 assert_select "div#errorExplanation", false
434 assert_select "div#notice", /^User information updated successfully/
435 assert_select "table#accountForm > tr > td > input#user_new_email[value=?]", user.new_email
438 # Check that the user account page will display and contains some relevant
439 # information for the user
440 def test_user_view_account
441 # Test a non-existent user
442 get :view, {:display_name => "unknown"}
443 assert_response :not_found
446 get :view, {:display_name => "test"}
447 assert_response :success
448 assert_select "div#userinformation" do
449 assert_select "a[href=/user/test/edits]", 1
450 assert_select "a[href=/user/test/traces]", 1
451 assert_select "a[href=/user/test/diary]", 1
452 assert_select "a[href=/user/test/diary/comments]", 1
453 assert_select "a[href=/user/test/account]", 0
454 assert_select "a[href=/user/test/blocks]", 0
455 assert_select "a[href=/user/test/blocks_by]", 0
456 assert_select "a[href=/blocks/new/test]", 0
459 # Test a user who has been blocked
460 get :view, {:display_name => "blocked"}
461 assert_response :success
462 assert_select "div#userinformation" do
463 assert_select "a[href=/user/blocked/edits]", 1
464 assert_select "a[href=/user/blocked/traces]", 1
465 assert_select "a[href=/user/blocked/diary]", 1
466 assert_select "a[href=/user/blocked/diary/comments]", 1
467 assert_select "a[href=/user/blocked/account]", 0
468 assert_select "a[href=/user/blocked/blocks]", 1
469 assert_select "a[href=/user/blocked/blocks_by]", 0
470 assert_select "a[href=/blocks/new/blocked]", 0
473 # Test a moderator who has applied blocks
474 get :view, {:display_name => "moderator"}
475 assert_response :success
476 assert_select "div#userinformation" do
477 assert_select "a[href=/user/moderator/edits]", 1
478 assert_select "a[href=/user/moderator/traces]", 1
479 assert_select "a[href=/user/moderator/diary]", 1
480 assert_select "a[href=/user/moderator/diary/comments]", 1
481 assert_select "a[href=/user/moderator/account]", 0
482 assert_select "a[href=/user/moderator/blocks]", 0
483 assert_select "a[href=/user/moderator/blocks_by]", 1
484 assert_select "a[href=/blocks/new/moderator]", 0
487 # Login as a normal user
488 session[:user] = users(:normal_user).id
489 cookies["_osm_username"] = users(:normal_user).display_name
491 # Test the normal user
492 get :view, {:display_name => "test"}
493 assert_response :success
494 assert_select "div#userinformation" do
495 assert_select "a[href=/user/test/edits]", 1
496 assert_select "a[href=/traces/mine]", 1
497 assert_select "a[href=/user/test/diary]", 1
498 assert_select "a[href=/user/test/diary/comments]", 1
499 assert_select "a[href=/user/test/account]", 1
500 assert_select "a[href=/user/test/blocks]", 0
501 assert_select "a[href=/user/test/blocks_by]", 0
502 assert_select "a[href=/blocks/new/test]", 0
505 # Login as a moderator
506 session[:user] = users(:moderator_user).id
507 cookies["_osm_username"] = users(:moderator_user).display_name
509 # Test the normal user
510 get :view, {:display_name => "test"}
511 assert_response :success
512 assert_select "div#userinformation" do
513 assert_select "a[href=/user/test/edits]", 1
514 assert_select "a[href=/user/test/traces]", 1
515 assert_select "a[href=/user/test/diary]", 1
516 assert_select "a[href=/user/test/diary/comments]", 1
517 assert_select "a[href=/user/test/account]", 0
518 assert_select "a[href=/user/test/blocks]", 0
519 assert_select "a[href=/user/test/blocks_by]", 0
520 assert_select "a[href=/blocks/new/test]", 1
524 def test_user_api_details
526 assert_response :unauthorized
528 basic_authorization(users(:normal_user).email, "test")
530 assert_response :success
533 def test_user_make_friend
534 # Get users to work with
535 user = users(:normal_user)
536 friend = users(:second_public_user)
538 # Check that the users aren't already friends
539 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
541 # Set the username cookie
542 @request.cookies["_osm_username"] = user.display_name
544 # When not logged in a GET should ask us to login
545 get :make_friend, {:display_name => friend.display_name}
546 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
548 # When not logged in a POST should error
549 post :make_friend, {:display_name => friend.display_name}
550 assert_response :forbidden
551 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
553 # When logged in a GET should get a confirmation page
554 get :make_friend, {:display_name => friend.display_name}, {"user" => user}
555 assert_response :success
556 assert_template :make_friend
557 assert_select "form" do
558 assert_select "input[type=hidden][name=referer]", 0
559 assert_select "input[type=submit]", 1
561 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
563 # The GET should preserve any referer
564 get :make_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
565 assert_response :success
566 assert_template :make_friend
567 assert_select "form" do
568 assert_select "input[type=hidden][name=referer][value=/test]", 1
569 assert_select "input[type=submit]", 1
571 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
573 # When logged in a POST should add the friendship
574 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
575 assert_redirected_to user_path(:display_name => friend.display_name)
576 assert_match /is now your friend/, flash[:notice]
577 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
579 # A second POST should report that the friendship already exists
580 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
581 assert_redirected_to user_path(:display_name => friend.display_name)
582 assert_match /You are already friends with/, flash[:warning]
583 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
586 def test_user_remove_friend
587 # Get users to work with
588 user = users(:normal_user)
589 friend = users(:public_user)
591 # Check that the users are friends
592 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
594 # Set the username cookie
595 @request.cookies["_osm_username"] = user.display_name
597 # When not logged in a GET should ask us to login
598 get :remove_friend, {:display_name => friend.display_name}
599 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
601 # When not logged in a POST should error
602 post :remove_friend, {:display_name => friend.display_name}
603 assert_response :forbidden
604 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
606 # When logged in a GET should get a confirmation page
607 get :remove_friend, {:display_name => friend.display_name}, {"user" => user}
608 assert_response :success
609 assert_template :remove_friend
610 assert_select "form" do
611 assert_select "input[type=hidden][name=referer]", 0
612 assert_select "input[type=submit]", 1
614 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
616 # The GET should preserve any referer
617 get :remove_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
618 assert_response :success
619 assert_template :remove_friend
620 assert_select "form" do
621 assert_select "input[type=hidden][name=referer][value=/test]", 1
622 assert_select "input[type=submit]", 1
624 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
626 # When logged in a POST should remove the friendship
627 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
628 assert_redirected_to user_path(:display_name => friend.display_name)
629 assert_match /was removed from your friends/, flash[:notice]
630 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
632 # A second POST should report that the friendship does not exist
633 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
634 assert_redirected_to user_path(:display_name => friend.display_name)
635 assert_match /is not one of your friends/, flash[:error]
636 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first