1 require File.dirname(__FILE__) + '/../test_helper'
3 class UserControllerTest < ActionController::TestCase
7 # test all routes which lead to this controller
10 { :path => "/api/0.6/user/1", :method => :get },
11 { :controller => "user", :action => "api_read", :id => "1" }
14 { :path => "/api/0.6/user/details", :method => :get },
15 { :controller => "user", :action => "api_details" }
18 { :path => "/api/0.6/user/gpx_files", :method => :get },
19 { :controller => "user", :action => "api_gpx_files" }
23 { :path => "/login", :method => :get },
24 { :controller => "user", :action => "login" }
27 { :path => "/login", :method => :post },
28 { :controller => "user", :action => "login" }
31 { :controller => "user", :action => "login", :format => "html" },
32 { :path => "/login.html", :method => :get }
36 { :path => "/logout", :method => :get },
37 { :controller => "user", :action => "logout" }
40 { :path => "/logout", :method => :post },
41 { :controller => "user", :action => "logout" }
44 { :controller => "user", :action => "logout", :format => "html" },
45 { :path => "/logout.html", :method => :get }
49 { :path => "/user/new", :method => :get },
50 { :controller => "user", :action => "new" }
53 { :controller => "user", :action => "new" },
54 { :path => "/create-account.html", :method => :get }
58 { :path => "/user/new", :method => :post },
59 { :controller => "user", :action => "create" }
63 { :path => "/user/terms", :method => :get },
64 { :controller => "user", :action => "terms" }
68 { :path => "/user/save", :method => :post },
69 { :controller => "user", :action => "save" }
73 { :path => "/user/username/confirm", :method => :get },
74 { :controller => "user", :action => "confirm", :display_name => "username" }
77 { :path => "/user/username/confirm", :method => :post },
78 { :controller => "user", :action => "confirm", :display_name => "username" }
81 { :path => "/user/username/confirm/resend", :method => :get },
82 { :controller => "user", :action => "confirm_resend", :display_name => "username" }
86 { :path => "/user/confirm", :method => :get },
87 { :controller => "user", :action => "confirm" }
90 { :path => "/user/confirm", :method => :post },
91 { :controller => "user", :action => "confirm" }
94 { :path => "/user/confirm-email", :method => :get },
95 { :controller => "user", :action => "confirm_email" }
98 { :path => "/user/confirm-email", :method => :post },
99 { :controller => "user", :action => "confirm_email" }
103 { :path => "/user/go_public", :method => :post },
104 { :controller => "user", :action => "go_public" }
108 { :path => "/user/forgot-password", :method => :get },
109 { :controller => "user", :action => "lost_password" }
112 { :path => "/user/forgot-password", :method => :post },
113 { :controller => "user", :action => "lost_password" }
116 { :controller => "user", :action => "lost_password" },
117 { :path => "/forgot-password.html", :method => :get }
120 { :path => "/user/reset-password", :method => :get },
121 { :controller => "user", :action => "reset_password" }
124 { :path => "/user/reset-password", :method => :post },
125 { :controller => "user", :action => "reset_password" }
129 { :path => "/user/suspended", :method => :get },
130 { :controller => "user", :action => "suspended" }
134 { :path => "/user/username", :method => :get },
135 { :controller => "user", :action => "view", :display_name => "username" }
139 { :path => "/user/username/account", :method => :get },
140 { :controller => "user", :action => "account", :display_name => "username" }
143 { :path => "/user/username/account", :method => :post },
144 { :controller => "user", :action => "account", :display_name => "username" }
148 { :path => "/user/username/make_friend", :method => :get },
149 { :controller => "user", :action => "make_friend", :display_name => "username" }
152 { :path => "/user/username/make_friend", :method => :post },
153 { :controller => "user", :action => "make_friend", :display_name => "username" }
156 { :path => "/user/username/remove_friend", :method => :get },
157 { :controller => "user", :action => "remove_friend", :display_name => "username" }
160 { :path => "/user/username/remove_friend", :method => :post },
161 { :controller => "user", :action => "remove_friend", :display_name => "username" }
165 { :path => "/user/username/set_status", :method => :get },
166 { :controller => "user", :action => "set_status", :display_name => "username" }
169 { :path => "/user/username/delete", :method => :get },
170 { :controller => "user", :action => "delete", :display_name => "username" }
174 { :path => "/users", :method => :get },
175 { :controller => "user", :action => "list" }
178 { :path => "/users", :method => :post },
179 { :controller => "user", :action => "list" }
182 { :path => "/users/status", :method => :get },
183 { :controller => "user", :action => "list", :status => "status" }
186 { :path => "/users/status", :method => :post },
187 { :controller => "user", :action => "list", :status => "status" }
191 # The user creation page loads
192 def test_user_create_view
194 assert_response :redirect
195 assert_redirected_to user_new_path(:cookie_test => "true")
197 get :new, { :cookie_test => "true" }, { :cookie_test => true }
198 assert_response :success
200 assert_select "html", :count => 1 do
201 assert_select "head", :count => 1 do
202 assert_select "title", :text => /Sign Up/, :count => 1
204 assert_select "body", :count => 1 do
205 assert_select "div#content", :count => 1 do
206 assert_select "form[action='/user/new'][method=post]", :count => 1 do
207 assert_select "input[id=user_email]", :count => 1
208 assert_select "input[id=user_email_confirmation]", :count => 1
209 assert_select "input[id=user_display_name]", :count => 1
210 assert_select "input[id=user_pass_crypt][type=password]", :count => 1
211 assert_select "input[id=user_pass_crypt_confirmation][type=password]", :count => 1
212 assert_select "input[type=submit][value=Sign Up]", :count => 1
221 user.status = "pending"
222 user.display_name = "new_tester"
223 user.email = "newtester@osm.org"
224 user.email_confirmation = "newtester@osm.org"
225 user.pass_crypt = "testtest"
226 user.pass_crypt_confirmation = "testtest"
230 def test_user_create_success
233 assert_difference('User.count') do
234 assert_difference('ActionMailer::Base.deliveries.size') do
235 post :save, {}, {:new_user => user}
240 register_email = ActionMailer::Base.deliveries.first
242 assert_equal register_email.to[0], user.email
243 assert_match /#{@url}/, register_email.body.to_s
246 assert_redirected_to :action => 'confirm', :display_name => user.display_name
248 ActionMailer::Base.deliveries.clear
251 def test_user_create_submit_duplicate_email
253 user.email = users(:public_user).email
255 assert_no_difference('User.count') do
256 assert_no_difference('ActionMailer::Base.deliveries.size') do
257 post :save, {}, {:new_user => user}
261 assert_response :success
262 assert_template 'new'
263 assert_select "form > fieldset > div.form-row > div.field_with_errors > input#user_email"
266 def test_user_create_submit_duplicate_email_uppercase
268 user.email = users(:public_user).email.upcase
270 assert_no_difference('User.count') do
271 assert_no_difference('ActionMailer::Base.deliveries.size') do
272 post :save, {}, {:new_user => user}
276 assert_response :success
277 assert_template 'new'
278 assert_select "form > fieldset > div.form-row > div.field_with_errors > input#user_email"
281 def test_user_create_submit_duplicate_name
283 user.display_name = users(:public_user).display_name
285 assert_no_difference('User.count') do
286 assert_no_difference('ActionMailer::Base.deliveries.size') do
287 post :save, {}, {:new_user => user}
291 assert_response :success
292 assert_template 'new'
293 assert_select "form > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
296 def test_user_create_submit_duplicate_name_uppercase
298 user.display_name = users(:public_user).display_name.upcase
300 assert_no_difference('User.count') do
301 assert_no_difference('ActionMailer::Base.deliveries.size') do
302 post :save, {}, {:new_user => user}
306 assert_response :success
307 assert_template 'new'
308 assert_select "form > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
311 def test_user_save_referer_params
314 post :save, {}, {:new_user => user,
315 :referer => '/edit?editor=id#map=1/2/3'}
317 assert_equal welcome_path(:editor => 'id', :zoom => 1, :lat => 2, :lon => 3),
318 user.tokens.order("id DESC").first.referer
321 def test_user_confirm_expired_token
322 user = users(:inactive_user)
323 token = user.tokens.new
324 token.expiry = 1.day.ago
327 @request.cookies["_osm_session"] = user.display_name
328 post :confirm, :confirm_string => token.token
330 assert_redirected_to :action => 'confirm'
331 assert_match /expired/, flash[:error]
334 def test_user_already_confirmed
335 user = users(:normal_user)
336 token = user.tokens.create
338 @request.cookies["_osm_session"] = user.display_name
339 post :confirm, :confirm_string => token.token
341 assert_redirected_to :action => 'login'
342 assert_match /confirmed/, flash[:error]
345 def test_user_terms_new_user
346 get :terms, {}, { "new_user" => User.new }
347 assert_response :success
348 assert_template :terms
351 def test_user_terms_seen
352 user = users(:normal_user)
354 # Set the username cookie
355 @request.cookies["_osm_username"] = user.display_name
357 get :terms, {}, { "user" => user }
358 assert_response :redirect
359 assert_redirected_to :action => :account, :display_name => user.display_name
362 def test_user_go_public
363 @request.cookies["_osm_username"] = users(:normal_user).display_name
365 post :go_public, {}, { :user => users(:normal_user) }
366 assert_response :redirect
367 assert_redirected_to :action => :account, :display_name => users(:normal_user).display_name
368 assert_equal true, User.find(users(:normal_user).id).data_public
371 def test_user_lost_password
372 # Test fetching the lost password page
374 assert_response :success
375 assert_template :lost_password
376 assert_select "div#notice", false
378 # Test resetting using the address as recorded for a user that has an
379 # address which is duplicated in a different case by another user
380 assert_difference('ActionMailer::Base.deliveries.size', 1) do
381 post :lost_password, :user => { :email => users(:normal_user).email }
383 assert_response :redirect
384 assert_redirected_to :action => :login
385 assert_match /^Sorry you lost it/, flash[:notice]
386 assert_equal users(:normal_user).email, ActionMailer::Base.deliveries.last.to[0]
388 # Test resetting using an address that matches a different user
389 # that has the same address in a different case
390 assert_difference('ActionMailer::Base.deliveries.size', 1) do
391 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
393 assert_response :redirect
394 assert_redirected_to :action => :login
395 assert_match /^Sorry you lost it/, flash[:notice]
396 assert_equal users(:uppercase_user).email, ActionMailer::Base.deliveries.last.to[0]
398 # Test resetting using an address that is a case insensitive match
399 # for more than one user but not an exact match for either
400 assert_difference('ActionMailer::Base.deliveries.size', 0) do
401 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
403 assert_response :success
404 assert_template :lost_password
405 assert_select "div#error", /^Could not find that email address/
407 # Test resetting using the address as recorded for a user that has an
408 # address which is case insensitively unique
409 assert_difference('ActionMailer::Base.deliveries.size', 1) do
410 post :lost_password, :user => { :email => users(:public_user).email }
412 assert_response :redirect
413 assert_redirected_to :action => :login
414 assert_match /^Sorry you lost it/, flash[:notice]
415 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
417 # Test resetting using an address that matches a user that has the
418 # same (case insensitively unique) address in a different case
419 assert_difference('ActionMailer::Base.deliveries.size', 1) do
420 post :lost_password, :user => { :email => users(:public_user).email.upcase }
422 assert_response :redirect
423 assert_redirected_to :action => :login
424 assert_match /^Sorry you lost it/, flash[:notice]
425 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
428 def test_reset_password
429 # Test a request with no token
431 assert_response :bad_request
433 # Test a request with a bogus token
434 get :reset_password, :token => "made_up_token"
435 assert_response :redirect
436 assert_redirected_to :action => :lost_password
438 # Create a valid token for a user
439 token = User.find(users(:inactive_user).id).tokens.create
441 # Test a request with a valid token
442 get :reset_password, :token => token.token
443 assert_response :success
444 assert_template :reset_password
446 # Test setting a new password
447 post :reset_password, :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" }
448 assert_response :redirect
449 assert_redirected_to :action => :login
450 user = User.find(users(:inactive_user).id)
451 assert_equal "active", user.status
452 assert_equal true, user.email_valid
453 assert_equal user, User.authenticate(:username => "inactive@openstreetmap.org", :password => "new_password")
457 # Get a user to work with - note that this user deliberately
458 # conflicts with uppercase_user in the email and display name
459 # fields to test that we can change other fields without any
460 # validation errors being reported
461 user = users(:normal_user)
463 # Set the username cookie
464 @request.cookies["_osm_username"] = user.display_name
466 # Make sure that you are redirected to the login page when
467 # you are not logged in
468 get :account, { :display_name => user.display_name }
469 assert_response :redirect
470 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
472 # Make sure that you are redirected to the login page when
473 # you are not logged in as the right user
474 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
475 assert_response :redirect
476 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
478 # Make sure we get the page when we are logged in as the right user
479 get :account, { :display_name => user.display_name }, { "user" => user }
480 assert_response :success
481 assert_template :account
483 # Updating the description should work
484 user.description = "new description"
485 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
486 assert_response :success
487 assert_template :account
488 assert_select "div#errorExplanation", false
489 assert_select "div#notice", /^User information updated successfully/
490 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
492 # Changing name to one that exists should fail
493 user.display_name = users(:public_user).display_name
494 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
495 assert_response :success
496 assert_template :account
497 assert_select "div#notice", false
498 assert_select "div#errorExplanation"
499 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
501 # Changing name to one that exists should fail, regardless of case
502 user.display_name = users(:public_user).display_name.upcase
503 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
504 assert_response :success
505 assert_template :account
506 assert_select "div#notice", false
507 assert_select "div#errorExplanation"
508 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
510 # Changing name to one that doesn't exist should work
511 user.display_name = "new tester"
512 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
513 assert_response :success
514 assert_template :account
515 assert_select "div#errorExplanation", false
516 assert_select "div#notice", /^User information updated successfully/
517 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", user.display_name
519 # Need to update cookies now to stay valid
520 @request.cookies["_osm_username"] = user.display_name
522 # Changing email to one that exists should fail
523 user.new_email = users(:public_user).email
524 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
525 assert_response :success
526 assert_template :account
527 assert_select "div#notice", false
528 assert_select "div#errorExplanation"
529 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_new_email"
531 # Changing email to one that exists should fail, regardless of case
532 user.new_email = users(:public_user).email.upcase
533 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
534 assert_response :success
535 assert_template :account
536 assert_select "div#notice", false
537 assert_select "div#errorExplanation"
538 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_new_email"
540 # Changing email to one that doesn't exist should work
541 user.new_email = "new_tester@example.com"
542 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
543 assert_response :success
544 assert_template :account
545 assert_select "div#errorExplanation", false
546 assert_select "div#notice", /^User information updated successfully/
547 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
550 # Check that the user account page will display and contains some relevant
551 # information for the user
552 def test_user_view_account
553 # Test a non-existent user
554 get :view, {:display_name => "unknown"}
555 assert_response :not_found
558 get :view, {:display_name => "test"}
559 assert_response :success
560 assert_select "div#userinformation" do
561 assert_select "a[href=/user/test/edits]", 1
562 assert_select "a[href=/user/test/traces]", 1
563 assert_select "a[href=/user/test/diary]", 1
564 assert_select "a[href=/user/test/diary/comments]", 1
565 assert_select "a[href=/user/test/account]", 0
566 assert_select "a[href=/user/test/blocks]", 0
567 assert_select "a[href=/user/test/blocks_by]", 0
568 assert_select "a[href=/blocks/new/test]", 0
571 # Test a user who has been blocked
572 get :view, {:display_name => "blocked"}
573 assert_response :success
574 assert_select "div#userinformation" do
575 assert_select "a[href=/user/blocked/edits]", 1
576 assert_select "a[href=/user/blocked/traces]", 1
577 assert_select "a[href=/user/blocked/diary]", 1
578 assert_select "a[href=/user/blocked/diary/comments]", 1
579 assert_select "a[href=/user/blocked/account]", 0
580 assert_select "a[href=/user/blocked/blocks]", 1
581 assert_select "a[href=/user/blocked/blocks_by]", 0
582 assert_select "a[href=/blocks/new/blocked]", 0
585 # Test a moderator who has applied blocks
586 get :view, {:display_name => "moderator"}
587 assert_response :success
588 assert_select "div#userinformation" do
589 assert_select "a[href=/user/moderator/edits]", 1
590 assert_select "a[href=/user/moderator/traces]", 1
591 assert_select "a[href=/user/moderator/diary]", 1
592 assert_select "a[href=/user/moderator/diary/comments]", 1
593 assert_select "a[href=/user/moderator/account]", 0
594 assert_select "a[href=/user/moderator/blocks]", 0
595 assert_select "a[href=/user/moderator/blocks_by]", 1
596 assert_select "a[href=/blocks/new/moderator]", 0
599 # Login as a normal user
600 session[:user] = users(:normal_user).id
601 cookies["_osm_username"] = users(:normal_user).display_name
603 # Test the normal user
604 get :view, {:display_name => "test"}
605 assert_response :success
606 assert_select "div#userinformation" do
607 assert_select "a[href=/user/test/edits]", 1
608 assert_select "a[href=/traces/mine]", 1
609 assert_select "a[href=/user/test/diary]", 1
610 assert_select "a[href=/user/test/diary/comments]", 1
611 assert_select "a[href=/user/test/account]", 1
612 assert_select "a[href=/user/test/blocks]", 0
613 assert_select "a[href=/user/test/blocks_by]", 0
614 assert_select "a[href=/blocks/new/test]", 0
617 # Login as a moderator
618 session[:user] = users(:moderator_user).id
619 cookies["_osm_username"] = users(:moderator_user).display_name
621 # Test the normal user
622 get :view, {:display_name => "test"}
623 assert_response :success
624 assert_select "div#userinformation" do
625 assert_select "a[href=/user/test/edits]", 1
626 assert_select "a[href=/user/test/traces]", 1
627 assert_select "a[href=/user/test/diary]", 1
628 assert_select "a[href=/user/test/diary/comments]", 1
629 assert_select "a[href=/user/test/account]", 0
630 assert_select "a[href=/user/test/blocks]", 0
631 assert_select "a[href=/user/test/blocks_by]", 0
632 assert_select "a[href=/blocks/new/test]", 1
636 def test_user_api_read
637 # check that a visible user is returned properly
638 get :api_read, :id => users(:normal_user).id
639 assert_response :success
641 # check the data that is returned
642 assert_select "description", :count => 1, :text => "test"
643 assert_select "contributor-terms", :count => 1 do
644 assert_select "[agreed=true]"
646 assert_select "img", :count => 1
647 assert_select "roles", :count => 1 do
648 assert_select "role", :count => 0
650 assert_select "changesets", :count => 1 do
651 assert_select "[count=0]"
653 assert_select "traces", :count => 1 do
654 assert_select "[count=0]"
656 assert_select "blocks", :count => 1 do
657 assert_select "received", :count => 1 do
658 assert_select "[count=0][active=0]"
660 assert_select "issued", :count => 0
663 # check that we aren't revealing private information
664 assert_select "contributor-terms[pd]", false
665 assert_select "home", false
666 assert_select "languages", false
667 assert_select "messages", false
669 # check that a suspended user is not returned
670 get :api_read, :id => users(:suspended_user).id
671 assert_response :gone
673 # check that a deleted user is not returned
674 get :api_read, :id => users(:deleted_user).id
675 assert_response :gone
677 # check that a non-existent user is not returned
678 get :api_read, :id => 0
679 assert_response :not_found
682 def test_user_api_details
683 # check that nothing is returned when not logged in
685 assert_response :unauthorized
687 # check that we get a response when logged in
688 basic_authorization(users(:normal_user).email, "test")
690 assert_response :success
692 # check the data that is returned
693 assert_select "description", :count => 1, :text => "test"
694 assert_select "contributor-terms", :count => 1 do
695 assert_select "[agreed=true][pd=false]"
697 assert_select "img", :count => 1
698 assert_select "roles", :count => 1 do
699 assert_select "role", :count => 0
701 assert_select "changesets", :count => 1 do
702 assert_select "[count=0]", :count => 1
704 assert_select "traces", :count => 1 do
705 assert_select "[count=0]", :count => 1
707 assert_select "blocks", :count => 1 do
708 assert_select "received", :count => 1 do
709 assert_select "[count=0][active=0]"
711 assert_select "issued", :count => 0
713 assert_select "home", :count => 1 do
714 assert_select "[lat=12.1][lon=12.1][zoom=3]"
716 assert_select "languages", :count => 1 do
717 assert_select "lang", :count => 1, :text => "en"
719 assert_select "messages", :count => 1 do
720 assert_select "received", :count => 1 do
721 assert_select "[count=1][unread=0]"
723 assert_select "sent", :count => 1 do
724 assert_select "[count=1]"
729 def test_user_make_friend
730 # Get users to work with
731 user = users(:normal_user)
732 friend = users(:second_public_user)
734 # Check that the users aren't already friends
735 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
737 # Set the username cookie
738 @request.cookies["_osm_username"] = user.display_name
740 # When not logged in a GET should ask us to login
741 get :make_friend, {:display_name => friend.display_name}
742 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
744 # When not logged in a POST should error
745 post :make_friend, {:display_name => friend.display_name}
746 assert_response :forbidden
747 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
749 # When logged in a GET should get a confirmation page
750 get :make_friend, {:display_name => friend.display_name}, {"user" => user}
751 assert_response :success
752 assert_template :make_friend
753 assert_select "form" do
754 assert_select "input[type=hidden][name=referer]", 0
755 assert_select "input[type=submit]", 1
757 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
759 # The GET should preserve any referer
760 get :make_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
761 assert_response :success
762 assert_template :make_friend
763 assert_select "form" do
764 assert_select "input[type=hidden][name=referer][value=/test]", 1
765 assert_select "input[type=submit]", 1
767 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
769 # When logged in a POST should add the friendship
770 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
771 assert_redirected_to user_path(:display_name => friend.display_name)
772 assert_match /is now your friend/, flash[:notice]
773 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
775 # A second POST should report that the friendship already exists
776 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
777 assert_redirected_to user_path(:display_name => friend.display_name)
778 assert_match /You are already friends with/, flash[:warning]
779 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
782 def test_user_remove_friend
783 # Get users to work with
784 user = users(:normal_user)
785 friend = users(:public_user)
787 # Check that the users are friends
788 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
790 # Set the username cookie
791 @request.cookies["_osm_username"] = user.display_name
793 # When not logged in a GET should ask us to login
794 get :remove_friend, {:display_name => friend.display_name}
795 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
797 # When not logged in a POST should error
798 post :remove_friend, {:display_name => friend.display_name}
799 assert_response :forbidden
800 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
802 # When logged in a GET should get a confirmation page
803 get :remove_friend, {:display_name => friend.display_name}, {"user" => user}
804 assert_response :success
805 assert_template :remove_friend
806 assert_select "form" do
807 assert_select "input[type=hidden][name=referer]", 0
808 assert_select "input[type=submit]", 1
810 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
812 # The GET should preserve any referer
813 get :remove_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
814 assert_response :success
815 assert_template :remove_friend
816 assert_select "form" do
817 assert_select "input[type=hidden][name=referer][value=/test]", 1
818 assert_select "input[type=submit]", 1
820 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
822 # When logged in a POST should remove the friendship
823 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
824 assert_redirected_to user_path(:display_name => friend.display_name)
825 assert_match /was removed from your friends/, flash[:notice]
826 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
828 # A second POST should report that the friendship does not exist
829 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
830 assert_redirected_to user_path(:display_name => friend.display_name)
831 assert_match /is not one of your friends/, flash[:error]
832 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
836 # Try without logging in
837 get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"}
838 assert_response :redirect
839 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
841 @request.cookies["_osm_username"] = users(:normal_user).display_name
843 # Now try as a normal user
844 get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:normal_user).id}
845 assert_response :redirect
846 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
848 @request.cookies["_osm_username"] = users(:administrator_user).display_name
850 # Finally try as an administrator
851 get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:administrator_user).id}
852 assert_response :redirect
853 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
854 assert_equal "suspended", User.find(users(:normal_user).id).status
858 # Try without logging in
859 get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"}
860 assert_response :redirect
861 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
863 @request.cookies["_osm_username"] = users(:normal_user).display_name
865 # Now try as a normal user
866 get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:normal_user).id}
867 assert_response :redirect
868 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
870 @request.cookies["_osm_username"] = users(:administrator_user).display_name
872 # Finally try as an administrator
873 get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:administrator_user).id}
874 assert_response :redirect
875 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
877 # Check that the user was deleted properly
878 user = User.find(users(:normal_user).id)
879 assert_equal "user_1", user.display_name
880 assert_equal "", user.description
881 assert_nil user.home_lat
882 assert_nil user.home_lon
883 assert_equal false, user.image.file?
884 assert_equal false, user.email_valid
885 assert_nil user.new_email
886 assert_nil user.openid_url
887 assert_equal "deleted", user.status