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/remove_friend", :method => :get },
148 { :controller => "user", :action => "remove_friend", :display_name => "username" }
152 { :path => "/user/username/set_status", :method => :get },
153 { :controller => "user", :action => "set_status", :display_name => "username" }
156 { :path => "/user/username/delete", :method => :get },
157 { :controller => "user", :action => "delete", :display_name => "username" }
161 { :path => "/users", :method => :get },
162 { :controller => "user", :action => "list" }
165 { :path => "/users", :method => :post },
166 { :controller => "user", :action => "list" }
169 { :path => "/users/status", :method => :get },
170 { :controller => "user", :action => "list", :status => "status" }
173 { :path => "/users/status", :method => :post },
174 { :controller => "user", :action => "list", :status => "status" }
178 # The user creation page loads
179 def test_user_create_view
181 assert_response :success
183 assert_select "html", :count => 1 do
184 assert_select "head", :count => 1 do
185 assert_select "title", :text => /Create account/, :count => 1
187 assert_select "body", :count => 1 do
188 assert_select "div#content", :count => 1 do
189 assert_select "form[action='/user/terms'][method=post]", :count => 1 do
190 assert_select "input[id=user_email]", :count => 1
191 assert_select "input[id=user_email_confirmation]", :count => 1
192 assert_select "input[id=user_display_name]", :count => 1
193 assert_select "input[id=user_pass_crypt][type=password]", :count => 1
194 assert_select "input[id=user_pass_crypt_confirmation][type=password]", :count => 1
195 assert_select "input[type=submit][value=Continue]", :count => 1
202 def test_user_create_success
203 new_email = "newtester@osm.org"
204 display_name = "new_tester"
205 assert_difference('User.count') do
206 assert_difference('ActionMailer::Base.deliveries.size') do
207 post :save, {:user => { :email => new_email, :email_confirmation => new_email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}}
212 register_email = ActionMailer::Base.deliveries.first
214 assert_equal register_email.to[0], new_email
215 assert_match /#{@url}/, register_email.body.to_s
218 assert_redirected_to :action => 'login', :referer => nil
220 ActionMailer::Base.deliveries.clear
223 def test_user_create_submit_duplicate_email
224 email = users(:public_user).email
225 display_name = "new_tester"
226 assert_difference('User.count', 0) do
227 assert_difference('ActionMailer::Base.deliveries.size', 0) do
228 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
231 assert_response :success
232 assert_template 'new'
233 assert_select "div#errorExplanation"
234 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_email"
237 def test_user_create_submit_duplicate_email_uppercase
238 email = users(:public_user).email.upcase
239 display_name = "new_tester"
240 assert_difference('User.count', 0) do
241 assert_difference('ActionMailer::Base.deliveries.size', 0) do
242 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
245 assert_response :success
246 assert_template 'new'
247 assert_select "div#errorExplanation"
248 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_email"
251 def test_user_create_submit_duplicate_name
252 email = "new_tester@example.com"
253 display_name = users(:public_user).display_name
254 assert_difference('User.count', 0) do
255 assert_difference('ActionMailer::Base.deliveries.size', 0) do
256 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
259 assert_response :success
260 assert_template 'new'
261 assert_select "div#errorExplanation"
262 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_display_name"
265 def test_user_create_submit_duplicate_name_uppercase
266 email = "new_tester@example.com"
267 display_name = users(:public_user).display_name.upcase
268 assert_difference('User.count', 0) do
269 assert_difference('ActionMailer::Base.deliveries.size', 0) do
270 post :save, :user => { :email => email, :email_confirmation => email, :display_name => display_name, :pass_crypt => "testtest", :pass_crypt_confirmation => "testtest"}
273 assert_response :success
274 assert_template 'new'
275 assert_select "div#errorExplanation"
276 assert_select "table#signupForm > tr > td > div[class=field_with_errors] > input#user_display_name"
279 def test_user_lost_password
280 # Test fetching the lost password page
282 assert_response :success
283 assert_template :lost_password
284 assert_select "div#notice", false
286 # Test resetting using the address as recorded for a user that has an
287 # address which is duplicated in a different case by another user
288 assert_difference('ActionMailer::Base.deliveries.size', 1) do
289 post :lost_password, :user => { :email => users(:normal_user).email }
291 assert_response :redirect
292 assert_redirected_to :action => :login
293 assert_match /^Sorry you lost it/, flash[:notice]
294 assert_equal users(:normal_user).email, ActionMailer::Base.deliveries.last.to[0]
296 # Test resetting using an address that matches a different user
297 # that has the same address in a different case
298 assert_difference('ActionMailer::Base.deliveries.size', 1) do
299 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
301 assert_response :redirect
302 assert_redirected_to :action => :login
303 assert_match /^Sorry you lost it/, flash[:notice]
304 assert_equal users(:uppercase_user).email, ActionMailer::Base.deliveries.last.to[0]
306 # Test resetting using an address that is a case insensitive match
307 # for more than one user but not an exact match for either
308 assert_difference('ActionMailer::Base.deliveries.size', 0) do
309 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
311 assert_response :success
312 assert_template :lost_password
313 assert_select "div#error", /^Could not find that email address/
315 # Test resetting using the address as recorded for a user that has an
316 # address which is case insensitively unique
317 assert_difference('ActionMailer::Base.deliveries.size', 1) do
318 post :lost_password, :user => { :email => users(:public_user).email }
320 assert_response :redirect
321 assert_redirected_to :action => :login
322 assert_match /^Sorry you lost it/, flash[:notice]
323 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
325 # Test resetting using an address that matches a user that has the
326 # same (case insensitively unique) address in a different case
327 assert_difference('ActionMailer::Base.deliveries.size', 1) do
328 post :lost_password, :user => { :email => users(:public_user).email.upcase }
330 assert_response :redirect
331 assert_redirected_to :action => :login
332 assert_match /^Sorry you lost it/, flash[:notice]
333 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
337 # Get a user to work with - note that this user deliberately
338 # conflicts with uppercase_user in the email and display name
339 # fields to test that we can change other fields without any
340 # validation errors being reported
341 user = users(:normal_user)
343 # Set the username cookie
344 @request.cookies["_osm_username"] = user.display_name
346 # Make sure that you are redirected to the login page when
347 # you are not logged in
348 get :account, { :display_name => user.display_name }
349 assert_response :redirect
350 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
352 # Make sure that you are redirected to the login page when
353 # you are not logged in as the right user
354 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
355 assert_response :redirect
356 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
358 # Make sure we get the page when we are logged in as the right user
359 get :account, { :display_name => user.display_name }, { "user" => user }
360 assert_response :success
361 assert_template :account
363 # Updating the description should work
364 user.description = "new description"
365 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
366 assert_response :success
367 assert_template :account
368 assert_select "div#errorExplanation", false
369 assert_select "div#notice", /^User information updated successfully/
370 assert_select "table#accountForm > tr > td > div#user_description_container > div#user_description_content > textarea#user_description", user.description
372 # Changing name to one that exists should fail
373 user.display_name = users(:public_user).display_name
374 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
375 assert_response :success
376 assert_template :account
377 assert_select "div#notice", false
378 assert_select "div#errorExplanation"
379 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_display_name"
381 # Changing name to one that exists should fail, regardless of case
382 user.display_name = users(:public_user).display_name.upcase
383 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
384 assert_response :success
385 assert_template :account
386 assert_select "div#notice", false
387 assert_select "div#errorExplanation"
388 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_display_name"
390 # Changing name to one that doesn't exist should work
391 user.display_name = "new tester"
392 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
393 assert_response :success
394 assert_template :account
395 assert_select "div#errorExplanation", false
396 assert_select "div#notice", /^User information updated successfully/
397 assert_select "table#accountForm > tr > td > input#user_display_name[value=?]", user.display_name
399 # Need to update cookies now to stay valid
400 @request.cookies["_osm_username"] = user.display_name
402 # Changing email to one that exists should fail
403 user.new_email = users(:public_user).email
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#notice", false
408 assert_select "div#errorExplanation"
409 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_new_email"
411 # Changing email to one that exists should fail, regardless of case
412 user.new_email = users(:public_user).email.upcase
413 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
414 assert_response :success
415 assert_template :account
416 assert_select "div#notice", false
417 assert_select "div#errorExplanation"
418 assert_select "table#accountForm > tr > td > div[class=field_with_errors] > input#user_new_email"
420 # Changing email to one that doesn't exist should work
421 user.new_email = "new_tester@example.com"
422 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
423 assert_response :success
424 assert_template :account
425 assert_select "div#errorExplanation", false
426 assert_select "div#notice", /^User information updated successfully/
427 assert_select "table#accountForm > tr > td > input#user_new_email[value=?]", user.new_email
430 # Check that the user account page will display and contains some relevant
431 # information for the user
432 def test_view_user_account
433 get :view, {:display_name => "unknown"}
434 assert_response :not_found
436 get :view, {:display_name => "test"}
437 assert_response :success
440 def test_user_api_details
442 assert_response :unauthorized
444 basic_authorization(users(:normal_user).email, "test")
446 assert_response :success