3 class UserControllerTest < ActionController::TestCase
5 fixtures :messages, :friends
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/user/1", :method => :get },
12 { :controller => "user", :action => "api_read", :id => "1" }
15 { :path => "/api/0.6/user/details", :method => :get },
16 { :controller => "user", :action => "api_details" }
19 { :path => "/api/0.6/user/gpx_files", :method => :get },
20 { :controller => "user", :action => "api_gpx_files" }
24 { :path => "/login", :method => :get },
25 { :controller => "user", :action => "login" }
28 { :path => "/login", :method => :post },
29 { :controller => "user", :action => "login" }
32 { :controller => "user", :action => "login", :format => "html" },
33 { :path => "/login.html", :method => :get }
37 { :path => "/logout", :method => :get },
38 { :controller => "user", :action => "logout" }
41 { :path => "/logout", :method => :post },
42 { :controller => "user", :action => "logout" }
45 { :controller => "user", :action => "logout", :format => "html" },
46 { :path => "/logout.html", :method => :get }
50 { :path => "/user/new", :method => :get },
51 { :controller => "user", :action => "new" }
55 { :path => "/user/new", :method => :post },
56 { :controller => "user", :action => "create" }
60 { :path => "/user/terms", :method => :get },
61 { :controller => "user", :action => "terms" }
65 { :path => "/user/save", :method => :post },
66 { :controller => "user", :action => "save" }
70 { :path => "/user/username/confirm", :method => :get },
71 { :controller => "user", :action => "confirm", :display_name => "username" }
74 { :path => "/user/username/confirm", :method => :post },
75 { :controller => "user", :action => "confirm", :display_name => "username" }
78 { :path => "/user/username/confirm/resend", :method => :get },
79 { :controller => "user", :action => "confirm_resend", :display_name => "username" }
83 { :path => "/user/confirm", :method => :get },
84 { :controller => "user", :action => "confirm" }
87 { :path => "/user/confirm", :method => :post },
88 { :controller => "user", :action => "confirm" }
91 { :path => "/user/confirm-email", :method => :get },
92 { :controller => "user", :action => "confirm_email" }
95 { :path => "/user/confirm-email", :method => :post },
96 { :controller => "user", :action => "confirm_email" }
100 { :path => "/user/go_public", :method => :post },
101 { :controller => "user", :action => "go_public" }
105 { :path => "/user/forgot-password", :method => :get },
106 { :controller => "user", :action => "lost_password" }
109 { :path => "/user/forgot-password", :method => :post },
110 { :controller => "user", :action => "lost_password" }
113 { :path => "/user/reset-password", :method => :get },
114 { :controller => "user", :action => "reset_password" }
117 { :path => "/user/reset-password", :method => :post },
118 { :controller => "user", :action => "reset_password" }
122 { :path => "/user/suspended", :method => :get },
123 { :controller => "user", :action => "suspended" }
127 { :path => "/user/username", :method => :get },
128 { :controller => "user", :action => "view", :display_name => "username" }
132 { :path => "/user/username/account", :method => :get },
133 { :controller => "user", :action => "account", :display_name => "username" }
136 { :path => "/user/username/account", :method => :post },
137 { :controller => "user", :action => "account", :display_name => "username" }
141 { :path => "/user/username/make_friend", :method => :get },
142 { :controller => "user", :action => "make_friend", :display_name => "username" }
145 { :path => "/user/username/make_friend", :method => :post },
146 { :controller => "user", :action => "make_friend", :display_name => "username" }
149 { :path => "/user/username/remove_friend", :method => :get },
150 { :controller => "user", :action => "remove_friend", :display_name => "username" }
153 { :path => "/user/username/remove_friend", :method => :post },
154 { :controller => "user", :action => "remove_friend", :display_name => "username" }
158 { :path => "/user/username/set_status", :method => :get },
159 { :controller => "user", :action => "set_status", :display_name => "username" }
162 { :path => "/user/username/delete", :method => :get },
163 { :controller => "user", :action => "delete", :display_name => "username" }
167 { :path => "/users", :method => :get },
168 { :controller => "user", :action => "list" }
171 { :path => "/users", :method => :post },
172 { :controller => "user", :action => "list" }
175 { :path => "/users/status", :method => :get },
176 { :controller => "user", :action => "list", :status => "status" }
179 { :path => "/users/status", :method => :post },
180 { :controller => "user", :action => "list", :status => "status" }
184 # The user creation page loads
187 assert_response :redirect
188 assert_redirected_to user_new_path(:cookie_test => "true")
190 get :new, { :cookie_test => "true" }, { :cookie_test => true }
191 assert_response :success
193 assert_select "html", :count => 1 do
194 assert_select "head", :count => 1 do
195 assert_select "title", :text => /Sign Up/, :count => 1
197 assert_select "body", :count => 1 do
198 assert_select "div#content", :count => 1 do
199 assert_select "form[action='/user/new'][method='post']", :count => 1 do
200 assert_select "input[id='user_email']", :count => 1
201 assert_select "input[id='user_email_confirmation']", :count => 1
202 assert_select "input[id='user_display_name']", :count => 1
203 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
204 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
205 assert_select "input[type='submit'][value='Sign Up']", :count => 1
215 assert_difference "User.count", 1 do
216 assert_difference "ActionMailer::Base.deliveries.size", 1 do
217 post :save, {}, { :new_user => user }
222 register_email = ActionMailer::Base.deliveries.first
224 assert_equal register_email.to[0], user.email
225 assert_match /#{@url}/, register_email.body.to_s
228 assert_redirected_to :action => "confirm", :display_name => user.display_name
230 ActionMailer::Base.deliveries.clear
233 def test_new_duplicate_email
235 user.email = users(:public_user).email
237 assert_no_difference "User.count" do
238 assert_no_difference "ActionMailer::Base.deliveries.size" do
239 post :save, {}, { :new_user => user }
243 assert_response :success
244 assert_template "new"
245 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
248 def test_new_duplicate_email_uppercase
250 user.email = users(:public_user).email.upcase
252 assert_no_difference "User.count" do
253 assert_no_difference "ActionMailer::Base.deliveries.size" do
254 post :save, {}, { :new_user => user }
258 assert_response :success
259 assert_template "new"
260 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
263 def test_new_duplicate_name
265 user.display_name = users(:public_user).display_name
267 assert_no_difference "User.count" do
268 assert_no_difference "ActionMailer::Base.deliveries.size" do
269 post :save, {}, { :new_user => user }
273 assert_response :success
274 assert_template "new"
275 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
278 def test_new_duplicate_name_uppercase
280 user.display_name = users(:public_user).display_name.upcase
282 assert_no_difference "User.count" do
283 assert_no_difference "ActionMailer::Base.deliveries.size" do
284 post :save, {}, { :new_user => user }
288 assert_response :success
289 assert_template "new"
290 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
293 def test_save_referer_params
296 assert_difference "User.count", 1 do
297 assert_difference "ActionMailer::Base.deliveries.size", 1 do
298 post :save, {}, { :new_user => user,
299 :referer => "/edit?editor=id#map=1/2/3" }
303 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
304 user.tokens.order("id DESC").first.referer
306 ActionMailer::Base.deliveries.clear
309 def test_logout_without_referer
311 assert_response :success
312 assert_template :logout
313 assert_select "input[name=referer][value=?]", ""
315 session_id = assert_select("input[name=session]").first["value"]
317 get :logout, :session => session_id
318 assert_response :redirect
319 assert_redirected_to root_path
322 def test_logout_with_referer
323 get :logout, :referer => "/test"
324 assert_response :success
325 assert_template :logout
326 assert_select "input[name=referer][value=?]", "/test"
328 session_id = assert_select("input[name=session]").first["value"]
330 get :logout, :session => session_id, :referer => "/test"
331 assert_response :redirect
332 assert_redirected_to "/test"
336 user = users(:inactive_user)
337 confirm_string = user.tokens.create.token
339 @request.cookies["_osm_session"] = user.display_name
340 get :confirm, :display_name => user.display_name, :confirm_string => confirm_string
341 assert_response :success
342 assert_template :confirm
345 def test_confirm_get_already_confirmed
346 user = users(:normal_user)
347 confirm_string = user.tokens.create.token
349 @request.cookies["_osm_session"] = user.display_name
350 get :confirm, :display_name => user.display_name, :confirm_string => confirm_string
351 assert_response :redirect
352 assert_redirected_to root_path
355 def test_confirm_success_no_token_no_referer
356 user = users(:inactive_user)
357 confirm_string = user.tokens.create.token
359 @request.cookies["_osm_session"] = user.display_name
360 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
361 assert_redirected_to login_path
362 assert_match /Confirmed your account/, flash[:notice]
365 def test_confirm_success_good_token_no_referer
366 user = users(:inactive_user)
367 confirm_string = user.tokens.create.token
368 token = user.tokens.create.token
370 @request.cookies["_osm_session"] = user.display_name
371 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
372 assert_redirected_to welcome_path
375 def test_confirm_success_bad_token_no_referer
376 user = users(:inactive_user)
377 confirm_string = user.tokens.create.token
378 token = users(:normal_user).tokens.create.token
380 @request.cookies["_osm_session"] = user.display_name
381 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
382 assert_redirected_to login_path
383 assert_match /Confirmed your account/, flash[:notice]
386 def test_confirm_success_no_token_with_referer
387 user = users(:inactive_user)
388 confirm_string = user.tokens.create(:referer => diary_new_path).token
390 @request.cookies["_osm_session"] = user.display_name
391 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
392 assert_redirected_to login_path(:referer => diary_new_path)
393 assert_match /Confirmed your account/, flash[:notice]
396 def test_confirm_success_good_token_with_referer
397 user = users(:inactive_user)
398 confirm_string = user.tokens.create(:referer => diary_new_path).token
399 token = user.tokens.create.token
401 @request.cookies["_osm_session"] = user.display_name
402 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
403 assert_redirected_to diary_new_path
406 def test_confirm_success_bad_token_with_referer
407 user = users(:inactive_user)
408 confirm_string = user.tokens.create(:referer => diary_new_path).token
409 token = users(:normal_user).tokens.create.token
411 @request.cookies["_osm_session"] = user.display_name
412 post :confirm, { :display_name => user.display_name, :confirm_string => confirm_string }, { :token => token }
413 assert_redirected_to login_path(:referer => diary_new_path)
414 assert_match /Confirmed your account/, flash[:notice]
417 def test_confirm_expired_token
418 user = users(:inactive_user)
419 confirm_string = user.tokens.create(:expiry => 1.day.ago).token
421 @request.cookies["_osm_session"] = user.display_name
422 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
423 assert_redirected_to :action => "confirm"
424 assert_match /confirmation code has expired/, flash[:error]
427 def test_confirm_already_confirmed
428 user = users(:normal_user)
429 confirm_string = user.tokens.create(:referer => diary_new_path).token
431 @request.cookies["_osm_session"] = user.display_name
432 post :confirm, :display_name => user.display_name, :confirm_string => confirm_string
433 assert_redirected_to :action => "login"
434 assert_match /already been confirmed/, flash[:error]
437 def test_confirm_resend_success
438 assert_difference "ActionMailer::Base.deliveries.size", 1 do
439 get :confirm_resend, :display_name => users(:inactive_user).display_name
442 assert_response :redirect
443 assert_redirected_to login_path
444 assert_match /sent a new confirmation/, flash[:notice]
446 email = ActionMailer::Base.deliveries.last
448 assert_equal users(:inactive_user).email, email.to.first
450 ActionMailer::Base.deliveries.clear
453 def test_confirm_resend_failure
454 assert_no_difference "ActionMailer::Base.deliveries.size" do
455 get :confirm_resend, :display_name => "No Such User"
458 assert_response :redirect
459 assert_redirected_to login_path
460 assert_match "User No Such User not found.", flash[:error]
463 def test_confirm_email_get
464 user = users(:normal_user)
465 confirm_string = user.tokens.create.token
467 get :confirm_email, :confirm_string => confirm_string
468 assert_response :success
469 assert_template :confirm_email
472 def test_confirm_email_success
473 user = users(:second_public_user)
474 confirm_string = user.tokens.create.token
476 post :confirm_email, :confirm_string => confirm_string
477 assert_response :redirect
478 assert_redirected_to :action => :account, :display_name => user.display_name
479 assert_match /Confirmed your change of email address/, flash[:notice]
482 def test_confirm_email_already_confirmed
483 user = users(:normal_user)
484 confirm_string = user.tokens.create.token
486 post :confirm_email, :confirm_string => confirm_string
487 assert_response :redirect
488 assert_redirected_to :action => :account, :display_name => user.display_name
489 assert_match /already been confirmed/, flash[:error]
492 def test_confirm_email_bad_token
493 post :confirm_email, :confirm_string => "XXXXX"
494 assert_response :success
495 assert_template :confirm_email
496 assert_match /confirmation code has expired or does not exist/, flash[:error]
499 def test_terms_new_user
500 get :terms, {}, { :new_user => User.new }
501 assert_response :success
502 assert_template :terms
506 user = users(:normal_user)
508 get :terms, {}, { :user => user }
509 assert_response :redirect
510 assert_redirected_to :action => :account, :display_name => user.display_name
513 def test_terms_not_seen
514 user = users(:terms_not_seen_user)
516 get :terms, {}, { :user => user }
517 assert_response :success
518 assert_template :terms
522 post :go_public, {}, { :user => users(:normal_user) }
523 assert_response :redirect
524 assert_redirected_to :action => :account, :display_name => users(:normal_user).display_name
525 assert_equal true, User.find(users(:normal_user).id).data_public
528 def test_lost_password
529 # Test fetching the lost password page
531 assert_response :success
532 assert_template :lost_password
533 assert_select "div#notice", false
535 # Test resetting using the address as recorded for a user that has an
536 # address which is duplicated in a different case by another user
537 assert_difference "ActionMailer::Base.deliveries.size", 1 do
538 post :lost_password, :user => { :email => users(:normal_user).email }
540 assert_response :redirect
541 assert_redirected_to :action => :login
542 assert_match /^Sorry you lost it/, flash[:notice]
543 email = ActionMailer::Base.deliveries.first
544 assert_equal 1, email.to.count
545 assert_equal users(:normal_user).email, email.to.first
546 ActionMailer::Base.deliveries.clear
548 # Test resetting using an address that matches a different user
549 # that has the same address in a different case
550 assert_difference "ActionMailer::Base.deliveries.size", 1 do
551 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
553 assert_response :redirect
554 assert_redirected_to :action => :login
555 assert_match /^Sorry you lost it/, flash[:notice]
556 email = ActionMailer::Base.deliveries.first
557 assert_equal 1, email.to.count
558 assert_equal users(:uppercase_user).email, email.to.first
559 ActionMailer::Base.deliveries.clear
561 # Test resetting using an address that is a case insensitive match
562 # for more than one user but not an exact match for either
563 assert_no_difference "ActionMailer::Base.deliveries.size" do
564 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
566 assert_response :success
567 assert_template :lost_password
568 assert_select ".error", /^Could not find that email address/
570 # Test resetting using the address as recorded for a user that has an
571 # address which is case insensitively unique
572 assert_difference "ActionMailer::Base.deliveries.size", 1 do
573 post :lost_password, :user => { :email => users(:public_user).email }
575 assert_response :redirect
576 assert_redirected_to :action => :login
577 assert_match /^Sorry you lost it/, flash[:notice]
578 email = ActionMailer::Base.deliveries.first
579 assert_equal 1, email.to.count
580 assert_equal users(:public_user).email, email.to.first
581 ActionMailer::Base.deliveries.clear
583 # Test resetting using an address that matches a user that has the
584 # same (case insensitively unique) address in a different case
585 assert_difference "ActionMailer::Base.deliveries.size", 1 do
586 post :lost_password, :user => { :email => users(:public_user).email.upcase }
588 assert_response :redirect
589 assert_redirected_to :action => :login
590 assert_match /^Sorry you lost it/, flash[:notice]
591 email = ActionMailer::Base.deliveries.first
592 assert_equal 1, email.to.count
593 assert_equal users(:public_user).email, email.to.first
594 ActionMailer::Base.deliveries.clear
597 def test_reset_password
598 # Test a request with no token
600 assert_response :bad_request
602 # Test a request with a bogus token
603 get :reset_password, :token => "made_up_token"
604 assert_response :redirect
605 assert_redirected_to :action => :lost_password
607 # Create a valid token for a user
608 token = User.find(users(:inactive_user).id).tokens.create
610 # Test a request with a valid token
611 get :reset_password, :token => token.token
612 assert_response :success
613 assert_template :reset_password
615 # Test setting a new password
616 post :reset_password, :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" }
617 assert_response :redirect
618 assert_redirected_to :action => :login
619 user = User.find(users(:inactive_user).id)
620 assert_equal "active", user.status
621 assert_equal true, user.email_valid
622 assert_equal user, User.authenticate(:username => "inactive@openstreetmap.org", :password => "new_password")
626 # Get a user to work with - note that this user deliberately
627 # conflicts with uppercase_user in the email and display name
628 # fields to test that we can change other fields without any
629 # validation errors being reported
630 user = users(:normal_user)
632 # Make sure that you are redirected to the login page when
633 # you are not logged in
634 get :account, :display_name => user.display_name
635 assert_response :redirect
636 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
638 # Make sure that you are blocked when not logged in as the right user
639 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
640 assert_response :forbidden
642 # Make sure we get the page when we are logged in as the right user
643 get :account, { :display_name => user.display_name }, { "user" => user }
644 assert_response :success
645 assert_template :account
647 # Updating the description should work
648 user.description = "new description"
649 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
650 assert_response :success
651 assert_template :account
652 assert_select "div#errorExplanation", false
653 assert_select ".notice", /^User information updated successfully/
654 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
656 # Changing name to one that exists should fail
657 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name)
658 post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id }
659 assert_response :success
660 assert_template :account
661 assert_select ".notice", false
662 assert_select "div#errorExplanation"
663 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
665 # Changing name to one that exists should fail, regardless of case
666 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name.upcase)
667 post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id }
668 assert_response :success
669 assert_template :account
670 assert_select ".notice", false
671 assert_select "div#errorExplanation"
672 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
674 # Changing name to one that doesn't exist should work
675 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
676 post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id }
677 assert_response :success
678 assert_template :account
679 assert_select "div#errorExplanation", false
680 assert_select ".notice", /^User information updated successfully/
681 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
683 # Record the change of name
684 user.display_name = "new tester"
686 # Changing email to one that exists should fail
687 user.new_email = users(:public_user).email
688 assert_no_difference "ActionMailer::Base.deliveries.size" do
689 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
691 assert_response :success
692 assert_template :account
693 assert_select ".notice", false
694 assert_select "div#errorExplanation"
695 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
697 # Changing email to one that exists should fail, regardless of case
698 user.new_email = users(:public_user).email.upcase
699 assert_no_difference "ActionMailer::Base.deliveries.size" do
700 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
702 assert_response :success
703 assert_template :account
704 assert_select ".notice", false
705 assert_select "div#errorExplanation"
706 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
708 # Changing email to one that doesn't exist should work
709 user.new_email = "new_tester@example.com"
710 assert_difference "ActionMailer::Base.deliveries.size", 1 do
711 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
713 assert_response :success
714 assert_template :account
715 assert_select "div#errorExplanation", false
716 assert_select ".notice", /^User information updated successfully/
717 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
718 email = ActionMailer::Base.deliveries.first
719 assert_equal 1, email.to.count
720 assert_equal user.new_email, email.to.first
721 ActionMailer::Base.deliveries.clear
724 # Check that the user account page will display and contains some relevant
725 # information for the user
727 # Test a non-existent user
728 get :view, :display_name => "unknown"
729 assert_response :not_found
732 get :view, :display_name => "test"
733 assert_response :success
734 assert_select "div#userinformation" do
735 assert_select "a[href^='/user/test/history']", 1
736 assert_select "a[href='/user/test/traces']", 1
737 assert_select "a[href='/user/test/diary']", 1
738 assert_select "a[href='/user/test/diary/comments']", 1
739 assert_select "a[href='/user/test/account']", 0
740 assert_select "a[href='/user/test/blocks']", 0
741 assert_select "a[href='/user/test/blocks_by']", 0
742 assert_select "a[href='/blocks/new/test']", 0
745 # Test a user who has been blocked
746 get :view, :display_name => "blocked"
747 assert_response :success
748 assert_select "div#userinformation" do
749 assert_select "a[href^='/user/blocked/history']", 1
750 assert_select "a[href='/user/blocked/traces']", 1
751 assert_select "a[href='/user/blocked/diary']", 1
752 assert_select "a[href='/user/blocked/diary/comments']", 1
753 assert_select "a[href='/user/blocked/account']", 0
754 assert_select "a[href='/user/blocked/blocks']", 1
755 assert_select "a[href='/user/blocked/blocks_by']", 0
756 assert_select "a[href='/blocks/new/blocked']", 0
759 # Test a moderator who has applied blocks
760 get :view, :display_name => "moderator"
761 assert_response :success
762 assert_select "div#userinformation" do
763 assert_select "a[href^='/user/moderator/history']", 1
764 assert_select "a[href='/user/moderator/traces']", 1
765 assert_select "a[href='/user/moderator/diary']", 1
766 assert_select "a[href='/user/moderator/diary/comments']", 1
767 assert_select "a[href='/user/moderator/account']", 0
768 assert_select "a[href='/user/moderator/blocks']", 0
769 assert_select "a[href='/user/moderator/blocks_by']", 1
770 assert_select "a[href='/blocks/new/moderator']", 0
773 # Login as a normal user
774 session[:user] = users(:normal_user).id
776 # Test the normal user
777 get :view, :display_name => "test"
778 assert_response :success
779 assert_select "div#userinformation" do
780 assert_select "a[href^='/user/test/history']", 1
781 assert_select "a[href='/traces/mine']", 1
782 assert_select "a[href='/user/test/diary']", 1
783 assert_select "a[href='/user/test/diary/comments']", 1
784 assert_select "a[href='/user/test/account']", 1
785 assert_select "a[href='/user/test/blocks']", 0
786 assert_select "a[href='/user/test/blocks_by']", 0
787 assert_select "a[href='/blocks/new/test']", 0
790 # Login as a moderator
791 session[:user] = users(:moderator_user).id
793 # Test the normal user
794 get :view, :display_name => "test"
795 assert_response :success
796 assert_select "div#userinformation" do
797 assert_select "a[href^='/user/test/history']", 1
798 assert_select "a[href='/user/test/traces']", 1
799 assert_select "a[href='/user/test/diary']", 1
800 assert_select "a[href='/user/test/diary/comments']", 1
801 assert_select "a[href='/user/test/account']", 0
802 assert_select "a[href='/user/test/blocks']", 0
803 assert_select "a[href='/user/test/blocks_by']", 0
804 assert_select "a[href='/blocks/new/test']", 1
809 # check that a visible user is returned properly
810 get :api_read, :id => users(:normal_user).id
811 assert_response :success
812 assert_equal "text/xml", response.content_type
814 # check the data that is returned
815 assert_select "description", :count => 1, :text => "test"
816 assert_select "contributor-terms", :count => 1 do
817 assert_select "[agreed='true']"
819 assert_select "img", :count => 1
820 assert_select "roles", :count => 1 do
821 assert_select "role", :count => 0
823 assert_select "changesets", :count => 1 do
824 assert_select "[count='0']"
826 assert_select "traces", :count => 1 do
827 assert_select "[count='0']"
829 assert_select "blocks", :count => 1 do
830 assert_select "received", :count => 1 do
831 assert_select "[count='0'][active='0']"
833 assert_select "issued", :count => 0
836 # check that we aren't revealing private information
837 assert_select "contributor-terms[pd]", false
838 assert_select "home", false
839 assert_select "languages", false
840 assert_select "messages", false
842 # check that a suspended user is not returned
843 get :api_read, :id => users(:suspended_user).id
844 assert_response :gone
846 # check that a deleted user is not returned
847 get :api_read, :id => users(:deleted_user).id
848 assert_response :gone
850 # check that a non-existent user is not returned
851 get :api_read, :id => 0
852 assert_response :not_found
856 # check that nothing is returned when not logged in
858 assert_response :unauthorized
860 # check that we get a response when logged in
861 basic_authorization(users(:normal_user).email, "test")
863 assert_response :success
864 assert_equal "text/xml", response.content_type
866 # check the data that is returned
867 assert_select "description", :count => 1, :text => "test"
868 assert_select "contributor-terms", :count => 1 do
869 assert_select "[agreed='true'][pd='false']"
871 assert_select "img", :count => 1
872 assert_select "roles", :count => 1 do
873 assert_select "role", :count => 0
875 assert_select "changesets", :count => 1 do
876 assert_select "[count='0']", :count => 1
878 assert_select "traces", :count => 1 do
879 assert_select "[count='0']", :count => 1
881 assert_select "blocks", :count => 1 do
882 assert_select "received", :count => 1 do
883 assert_select "[count='0'][active='0']"
885 assert_select "issued", :count => 0
887 assert_select "home", :count => 1 do
888 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
890 assert_select "languages", :count => 1 do
891 assert_select "lang", :count => 1, :text => "en"
893 assert_select "messages", :count => 1 do
894 assert_select "received", :count => 1 do
895 assert_select "[count='1'][unread='0']"
897 assert_select "sent", :count => 1 do
898 assert_select "[count='1']"
903 def test_api_gpx_files
904 # check that nothing is returned when not logged in
906 assert_response :unauthorized
908 # check that we get a response when logged in
909 basic_authorization(users(:normal_user).email, "test")
911 assert_response :success
912 assert_equal "text/xml", response.content_type
914 # check the data that is returned
915 assert_select "gpx_file[id='1']", 1 do
916 assert_select "tag", "London"
918 assert_select "gpx_file[id='4']", 1 do
919 assert_select "tag", "Birmingham"
924 # Get users to work with
925 user = users(:normal_user)
926 friend = users(:second_public_user)
928 # Check that the users aren't already friends
929 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
931 # When not logged in a GET should ask us to login
932 get :make_friend, :display_name => friend.display_name
933 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
935 # When not logged in a POST should error
936 post :make_friend, :display_name => friend.display_name
937 assert_response :forbidden
938 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
940 # When logged in a GET should get a confirmation page
941 get :make_friend, { :display_name => friend.display_name }, { :user => user.id }
942 assert_response :success
943 assert_template :make_friend
944 assert_select "form" do
945 assert_select "input[type='hidden'][name='referer']", 0
946 assert_select "input[type='submit']", 1
948 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
950 # When logged in a POST should add the friendship
951 assert_difference "ActionMailer::Base.deliveries.size", 1 do
952 post :make_friend, { :display_name => friend.display_name }, { :user => user.id }
954 assert_redirected_to user_path(:display_name => friend.display_name)
955 assert_match /is now your friend/, flash[:notice]
956 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
957 email = ActionMailer::Base.deliveries.first
958 assert_equal 1, email.to.count
959 assert_equal friend.email, email.to.first
960 ActionMailer::Base.deliveries.clear
962 # A second POST should report that the friendship already exists
963 assert_no_difference "ActionMailer::Base.deliveries.size" do
964 post :make_friend, { :display_name => friend.display_name }, { :user => user.id }
966 assert_redirected_to user_path(:display_name => friend.display_name)
967 assert_match /You are already friends with/, flash[:warning]
968 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
971 def test_make_friend_with_referer
972 # Get users to work with
973 user = users(:normal_user)
974 friend = users(:second_public_user)
976 # Check that the users aren't already friends
977 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
979 # The GET should preserve any referer
980 get :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
981 assert_response :success
982 assert_template :make_friend
983 assert_select "form" do
984 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
985 assert_select "input[type='submit']", 1
987 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
989 # When logged in a POST should add the friendship and refer us
990 assert_difference "ActionMailer::Base.deliveries.size", 1 do
991 post :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
993 assert_redirected_to "/test"
994 assert_match /is now your friend/, flash[:notice]
995 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
996 email = ActionMailer::Base.deliveries.first
997 assert_equal 1, email.to.count
998 assert_equal friend.email, email.to.first
999 ActionMailer::Base.deliveries.clear
1002 def test_make_friend_unkown_user
1003 # Should error when a bogus user is specified
1004 get :make_friend, { :display_name => "No Such User" }, { :user => users(:normal_user).id }
1005 assert_response :not_found
1006 assert_template :no_such_user
1009 def test_remove_friend
1010 # Get users to work with
1011 user = users(:normal_user)
1012 friend = users(:public_user)
1014 # Check that the users are friends
1015 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1017 # When not logged in a GET should ask us to login
1018 get :remove_friend, :display_name => friend.display_name
1019 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1021 # When not logged in a POST should error
1022 post :remove_friend, :display_name => friend.display_name
1023 assert_response :forbidden
1024 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1026 # When logged in a GET should get a confirmation page
1027 get :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1028 assert_response :success
1029 assert_template :remove_friend
1030 assert_select "form" do
1031 assert_select "input[type='hidden'][name='referer']", 0
1032 assert_select "input[type='submit']", 1
1034 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1036 # When logged in a POST should remove the friendship
1037 post :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1038 assert_redirected_to user_path(:display_name => friend.display_name)
1039 assert_match /was removed from your friends/, flash[:notice]
1040 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1042 # A second POST should report that the friendship does not exist
1043 post :remove_friend, { :display_name => friend.display_name }, { :user => user.id }
1044 assert_redirected_to user_path(:display_name => friend.display_name)
1045 assert_match /is not one of your friends/, flash[:error]
1046 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1049 def test_remove_friend_with_referer
1050 # Get users to work with
1051 user = users(:normal_user)
1052 friend = users(:public_user)
1054 # Check that the users are friends
1055 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1057 # The GET should preserve any referer
1058 get :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1059 assert_response :success
1060 assert_template :remove_friend
1061 assert_select "form" do
1062 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1063 assert_select "input[type='submit']", 1
1065 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1067 # When logged in a POST should remove the friendship and refer
1068 post :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { :user => user.id }
1069 assert_redirected_to "/test"
1070 assert_match /was removed from your friends/, flash[:notice]
1071 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1074 def test_remove_friend_unkown_user
1075 # Should error when a bogus user is specified
1076 get :remove_friend, { :display_name => "No Such User" }, { :user => users(:normal_user).id }
1077 assert_response :not_found
1078 assert_template :no_such_user
1082 # Try without logging in
1083 get :set_status, :display_name => users(:normal_user).display_name, :status => "suspended"
1084 assert_response :redirect
1085 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1087 # Now try as a normal user
1088 get :set_status, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:normal_user).id }
1089 assert_response :redirect
1090 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1092 # Finally try as an administrator
1093 get :set_status, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:administrator_user).id }
1094 assert_response :redirect
1095 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1096 assert_equal "suspended", User.find(users(:normal_user).id).status
1100 # Try without logging in
1101 get :delete, :display_name => users(:normal_user).display_name, :status => "suspended"
1102 assert_response :redirect
1103 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1105 # Now try as a normal user
1106 get :delete, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:normal_user).id }
1107 assert_response :redirect
1108 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1110 # Finally try as an administrator
1111 get :delete, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:administrator_user).id }
1112 assert_response :redirect
1113 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
1115 # Check that the user was deleted properly
1116 user = User.find(users(:normal_user).id)
1117 assert_equal "user_1", user.display_name
1118 assert_equal "", user.description
1119 assert_nil user.home_lat
1120 assert_nil user.home_lon
1121 assert_equal false, user.image.file?
1122 assert_equal false, user.email_valid
1123 assert_nil user.new_email
1124 assert_nil user.auth_provider
1125 assert_nil user.auth_uid
1126 assert_equal "deleted", user.status
1133 user.status = "pending"
1134 user.display_name = "new_tester"
1135 user.email = "newtester@osm.org"
1136 user.email_confirmation = "newtester@osm.org"
1137 user.pass_crypt = "testtest"
1138 user.pass_crypt_confirmation = "testtest"