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_view_user_account
441 get :view, {:display_name => "unknown"}
442 assert_response :not_found
444 get :view, {:display_name => "test"}
445 assert_response :success
448 def test_user_api_details
450 assert_response :unauthorized
452 basic_authorization(users(:normal_user).email, "test")
454 assert_response :success
457 def test_user_make_friend
458 # Get users to work with
459 user = users(:normal_user)
460 friend = users(:second_public_user)
462 # Check that the users aren't already friends
463 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
465 # Set the username cookie
466 @request.cookies["_osm_username"] = user.display_name
468 # When not logged in a GET should ask us to login
469 get :make_friend, {:display_name => friend.display_name}
470 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
472 # When not logged in a POST should error
473 post :make_friend, {:display_name => friend.display_name}
474 assert_response :forbidden
475 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
477 # When logged in a GET should get a confirmation page
478 get :make_friend, {:display_name => friend.display_name}, {"user" => user}
479 assert_response :success
480 assert_template :make_friend
481 assert_select "form" do
482 assert_select "input[type=hidden][name=referer]", 0
483 assert_select "input[type=submit]", 1
485 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
487 # The GET should preserve any referer
488 get :make_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
489 assert_response :success
490 assert_template :make_friend
491 assert_select "form" do
492 assert_select "input[type=hidden][name=referer][value=/test]", 1
493 assert_select "input[type=submit]", 1
495 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
497 # When logged in a POST should add the friendship
498 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
499 assert_redirected_to user_path(:display_name => friend.display_name)
500 assert_match /is now your friend/, flash[:notice]
501 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
503 # A second POST should report that the friendship already exists
504 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
505 assert_redirected_to user_path(:display_name => friend.display_name)
506 assert_match /You are already friends with/, flash[:warning]
507 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
510 def test_user_remove_friend
511 # Get users to work with
512 user = users(:normal_user)
513 friend = users(:public_user)
515 # Check that the users are friends
516 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
518 # Set the username cookie
519 @request.cookies["_osm_username"] = user.display_name
521 # When not logged in a GET should ask us to login
522 get :remove_friend, {:display_name => friend.display_name}
523 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
525 # When not logged in a POST should error
526 post :remove_friend, {:display_name => friend.display_name}
527 assert_response :forbidden
528 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
530 # When logged in a GET should get a confirmation page
531 get :remove_friend, {:display_name => friend.display_name}, {"user" => user}
532 assert_response :success
533 assert_template :remove_friend
534 assert_select "form" do
535 assert_select "input[type=hidden][name=referer]", 0
536 assert_select "input[type=submit]", 1
538 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
540 # The GET should preserve any referer
541 get :remove_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
542 assert_response :success
543 assert_template :remove_friend
544 assert_select "form" do
545 assert_select "input[type=hidden][name=referer][value=/test]", 1
546 assert_select "input[type=submit]", 1
548 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
550 # When logged in a POST should remove the friendship
551 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
552 assert_redirected_to user_path(:display_name => friend.display_name)
553 assert_match /was removed from your friends/, flash[:notice]
554 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
556 # A second POST should report that the friendship does not exist
557 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
558 assert_redirected_to user_path(:display_name => friend.display_name)
559 assert_match /is not one of your friends/, flash[:error]
560 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first