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 get :terms, {}, { "user" => user }
355 assert_response :redirect
356 assert_redirected_to :action => :account, :display_name => user.display_name
359 def test_user_go_public
360 post :go_public, {}, { :user => users(:normal_user) }
361 assert_response :redirect
362 assert_redirected_to :action => :account, :display_name => users(:normal_user).display_name
363 assert_equal true, User.find(users(:normal_user).id).data_public
366 def test_user_lost_password
367 # Test fetching the lost password page
369 assert_response :success
370 assert_template :lost_password
371 assert_select "div#notice", false
373 # Test resetting using the address as recorded for a user that has an
374 # address which is duplicated in a different case by another user
375 assert_difference('ActionMailer::Base.deliveries.size', 1) do
376 post :lost_password, :user => { :email => users(:normal_user).email }
378 assert_response :redirect
379 assert_redirected_to :action => :login
380 assert_match /^Sorry you lost it/, flash[:notice]
381 assert_equal users(:normal_user).email, ActionMailer::Base.deliveries.last.to[0]
383 # Test resetting using an address that matches a different user
384 # that has the same address in a different case
385 assert_difference('ActionMailer::Base.deliveries.size', 1) do
386 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
388 assert_response :redirect
389 assert_redirected_to :action => :login
390 assert_match /^Sorry you lost it/, flash[:notice]
391 assert_equal users(:uppercase_user).email, ActionMailer::Base.deliveries.last.to[0]
393 # Test resetting using an address that is a case insensitive match
394 # for more than one user but not an exact match for either
395 assert_difference('ActionMailer::Base.deliveries.size', 0) do
396 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
398 assert_response :success
399 assert_template :lost_password
400 assert_select ".error", /^Could not find that email address/
402 # Test resetting using the address as recorded for a user that has an
403 # address which is case insensitively unique
404 assert_difference('ActionMailer::Base.deliveries.size', 1) do
405 post :lost_password, :user => { :email => users(:public_user).email }
407 assert_response :redirect
408 assert_redirected_to :action => :login
409 assert_match /^Sorry you lost it/, flash[:notice]
410 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
412 # Test resetting using an address that matches a user that has the
413 # same (case insensitively unique) address in a different case
414 assert_difference('ActionMailer::Base.deliveries.size', 1) do
415 post :lost_password, :user => { :email => users(:public_user).email.upcase }
417 assert_response :redirect
418 assert_redirected_to :action => :login
419 assert_match /^Sorry you lost it/, flash[:notice]
420 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
423 def test_reset_password
424 # Test a request with no token
426 assert_response :bad_request
428 # Test a request with a bogus token
429 get :reset_password, :token => "made_up_token"
430 assert_response :redirect
431 assert_redirected_to :action => :lost_password
433 # Create a valid token for a user
434 token = User.find(users(:inactive_user).id).tokens.create
436 # Test a request with a valid token
437 get :reset_password, :token => token.token
438 assert_response :success
439 assert_template :reset_password
441 # Test setting a new password
442 post :reset_password, :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" }
443 assert_response :redirect
444 assert_redirected_to :action => :login
445 user = User.find(users(:inactive_user).id)
446 assert_equal "active", user.status
447 assert_equal true, user.email_valid
448 assert_equal user, User.authenticate(:username => "inactive@openstreetmap.org", :password => "new_password")
452 # Get a user to work with - note that this user deliberately
453 # conflicts with uppercase_user in the email and display name
454 # fields to test that we can change other fields without any
455 # validation errors being reported
456 user = users(:normal_user)
458 # Make sure that you are redirected to the login page when
459 # you are not logged in
460 get :account, { :display_name => user.display_name }
461 assert_response :redirect
462 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
464 # Make sure that you are blocked when not logged in as the right user
465 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
466 assert_response :forbidden
468 # Make sure we get the page when we are logged in as the right user
469 get :account, { :display_name => user.display_name }, { "user" => user }
470 assert_response :success
471 assert_template :account
473 # Updating the description should work
474 user.description = "new description"
475 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
476 assert_response :success
477 assert_template :account
478 assert_select "div#errorExplanation", false
479 assert_select ".notice", /^User information updated successfully/
480 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
482 # Changing name to one that exists should fail
483 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name)
484 post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id }
485 assert_response :success
486 assert_template :account
487 assert_select ".notice", false
488 assert_select "div#errorExplanation"
489 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
491 # Changing name to one that exists should fail, regardless of case
492 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name.upcase)
493 post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id }
494 assert_response :success
495 assert_template :account
496 assert_select ".notice", false
497 assert_select "div#errorExplanation"
498 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
500 # Changing name to one that doesn't exist should work
501 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
502 post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id }
503 assert_response :success
504 assert_template :account
505 assert_select "div#errorExplanation", false
506 assert_select ".notice", /^User information updated successfully/
507 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
509 # Record the change of name
510 user.display_name = "new tester"
512 # Changing email to one that exists should fail
513 user.new_email = users(:public_user).email
514 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
515 assert_response :success
516 assert_template :account
517 assert_select ".notice", false
518 assert_select "div#errorExplanation"
519 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_new_email"
521 # Changing email to one that exists should fail, regardless of case
522 user.new_email = users(:public_user).email.upcase
523 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
524 assert_response :success
525 assert_template :account
526 assert_select ".notice", false
527 assert_select "div#errorExplanation"
528 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_new_email"
530 # Changing email to one that doesn't exist should work
531 user.new_email = "new_tester@example.com"
532 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
533 assert_response :success
534 assert_template :account
535 assert_select "div#errorExplanation", false
536 assert_select ".notice", /^User information updated successfully/
537 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
540 # Check that the user account page will display and contains some relevant
541 # information for the user
542 def test_user_view_account
543 # Test a non-existent user
544 get :view, {:display_name => "unknown"}
545 assert_response :not_found
548 get :view, {:display_name => "test"}
549 assert_response :success
550 assert_select "div#userinformation" do
551 assert_select "a[href^=/user/test/history]", 1
552 assert_select "a[href=/user/test/traces]", 1
553 assert_select "a[href=/user/test/diary]", 1
554 assert_select "a[href=/user/test/diary/comments]", 1
555 assert_select "a[href=/user/test/account]", 0
556 assert_select "a[href=/user/test/blocks]", 0
557 assert_select "a[href=/user/test/blocks_by]", 0
558 assert_select "a[href=/blocks/new/test]", 0
561 # Test a user who has been blocked
562 get :view, {:display_name => "blocked"}
563 assert_response :success
564 assert_select "div#userinformation" do
565 assert_select "a[href^=/user/blocked/history]", 1
566 assert_select "a[href=/user/blocked/traces]", 1
567 assert_select "a[href=/user/blocked/diary]", 1
568 assert_select "a[href=/user/blocked/diary/comments]", 1
569 assert_select "a[href=/user/blocked/account]", 0
570 assert_select "a[href=/user/blocked/blocks]", 1
571 assert_select "a[href=/user/blocked/blocks_by]", 0
572 assert_select "a[href=/blocks/new/blocked]", 0
575 # Test a moderator who has applied blocks
576 get :view, {:display_name => "moderator"}
577 assert_response :success
578 assert_select "div#userinformation" do
579 assert_select "a[href^=/user/moderator/history]", 1
580 assert_select "a[href=/user/moderator/traces]", 1
581 assert_select "a[href=/user/moderator/diary]", 1
582 assert_select "a[href=/user/moderator/diary/comments]", 1
583 assert_select "a[href=/user/moderator/account]", 0
584 assert_select "a[href=/user/moderator/blocks]", 0
585 assert_select "a[href=/user/moderator/blocks_by]", 1
586 assert_select "a[href=/blocks/new/moderator]", 0
589 # Login as a normal user
590 session[:user] = users(:normal_user).id
592 # Test the normal user
593 get :view, {:display_name => "test"}
594 assert_response :success
595 assert_select "div#userinformation" do
596 assert_select "a[href^=/user/test/history]", 1
597 assert_select "a[href=/traces/mine]", 1
598 assert_select "a[href=/user/test/diary]", 1
599 assert_select "a[href=/user/test/diary/comments]", 1
600 assert_select "a[href=/user/test/account]", 1
601 assert_select "a[href=/user/test/blocks]", 0
602 assert_select "a[href=/user/test/blocks_by]", 0
603 assert_select "a[href=/blocks/new/test]", 0
606 # Login as a moderator
607 session[:user] = users(:moderator_user).id
609 # Test the normal user
610 get :view, {:display_name => "test"}
611 assert_response :success
612 assert_select "div#userinformation" do
613 assert_select "a[href^=/user/test/history]", 1
614 assert_select "a[href=/user/test/traces]", 1
615 assert_select "a[href=/user/test/diary]", 1
616 assert_select "a[href=/user/test/diary/comments]", 1
617 assert_select "a[href=/user/test/account]", 0
618 assert_select "a[href=/user/test/blocks]", 0
619 assert_select "a[href=/user/test/blocks_by]", 0
620 assert_select "a[href=/blocks/new/test]", 1
624 def test_user_api_read
625 # check that a visible user is returned properly
626 get :api_read, :id => users(:normal_user).id
627 assert_response :success
629 # check the data that is returned
630 assert_select "description", :count => 1, :text => "test"
631 assert_select "contributor-terms", :count => 1 do
632 assert_select "[agreed=true]"
634 assert_select "img", :count => 1
635 assert_select "roles", :count => 1 do
636 assert_select "role", :count => 0
638 assert_select "changesets", :count => 1 do
639 assert_select "[count=0]"
641 assert_select "traces", :count => 1 do
642 assert_select "[count=0]"
644 assert_select "blocks", :count => 1 do
645 assert_select "received", :count => 1 do
646 assert_select "[count=0][active=0]"
648 assert_select "issued", :count => 0
651 # check that we aren't revealing private information
652 assert_select "contributor-terms[pd]", false
653 assert_select "home", false
654 assert_select "languages", false
655 assert_select "messages", false
657 # check that a suspended user is not returned
658 get :api_read, :id => users(:suspended_user).id
659 assert_response :gone
661 # check that a deleted user is not returned
662 get :api_read, :id => users(:deleted_user).id
663 assert_response :gone
665 # check that a non-existent user is not returned
666 get :api_read, :id => 0
667 assert_response :not_found
670 def test_user_api_details
671 # check that nothing is returned when not logged in
673 assert_response :unauthorized
675 # check that we get a response when logged in
676 basic_authorization(users(:normal_user).email, "test")
678 assert_response :success
680 # check the data that is returned
681 assert_select "description", :count => 1, :text => "test"
682 assert_select "contributor-terms", :count => 1 do
683 assert_select "[agreed=true][pd=false]"
685 assert_select "img", :count => 1
686 assert_select "roles", :count => 1 do
687 assert_select "role", :count => 0
689 assert_select "changesets", :count => 1 do
690 assert_select "[count=0]", :count => 1
692 assert_select "traces", :count => 1 do
693 assert_select "[count=0]", :count => 1
695 assert_select "blocks", :count => 1 do
696 assert_select "received", :count => 1 do
697 assert_select "[count=0][active=0]"
699 assert_select "issued", :count => 0
701 assert_select "home", :count => 1 do
702 assert_select "[lat=12.1][lon=12.1][zoom=3]"
704 assert_select "languages", :count => 1 do
705 assert_select "lang", :count => 1, :text => "en"
707 assert_select "messages", :count => 1 do
708 assert_select "received", :count => 1 do
709 assert_select "[count=1][unread=0]"
711 assert_select "sent", :count => 1 do
712 assert_select "[count=1]"
717 def test_user_make_friend
718 # Get users to work with
719 user = users(:normal_user)
720 friend = users(:second_public_user)
722 # Check that the users aren't already friends
723 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
725 # When not logged in a GET should ask us to login
726 get :make_friend, {:display_name => friend.display_name}
727 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
729 # When not logged in a POST should error
730 post :make_friend, {:display_name => friend.display_name}
731 assert_response :forbidden
732 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
734 # When logged in a GET should get a confirmation page
735 get :make_friend, {:display_name => friend.display_name}, {"user" => user}
736 assert_response :success
737 assert_template :make_friend
738 assert_select "form" do
739 assert_select "input[type=hidden][name=referer]", 0
740 assert_select "input[type=submit]", 1
742 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
744 # The GET should preserve any referer
745 get :make_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
746 assert_response :success
747 assert_template :make_friend
748 assert_select "form" do
749 assert_select "input[type=hidden][name=referer][value=/test]", 1
750 assert_select "input[type=submit]", 1
752 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
754 # When logged in a POST should add the friendship
755 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
756 assert_redirected_to user_path(:display_name => friend.display_name)
757 assert_match /is now your friend/, flash[:notice]
758 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
760 # A second POST should report that the friendship already exists
761 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
762 assert_redirected_to user_path(:display_name => friend.display_name)
763 assert_match /You are already friends with/, flash[:warning]
764 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
767 def test_user_remove_friend
768 # Get users to work with
769 user = users(:normal_user)
770 friend = users(:public_user)
772 # Check that the users are friends
773 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
775 # When not logged in a GET should ask us to login
776 get :remove_friend, {:display_name => friend.display_name}
777 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
779 # When not logged in a POST should error
780 post :remove_friend, {:display_name => friend.display_name}
781 assert_response :forbidden
782 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
784 # When logged in a GET should get a confirmation page
785 get :remove_friend, {:display_name => friend.display_name}, {"user" => user}
786 assert_response :success
787 assert_template :remove_friend
788 assert_select "form" do
789 assert_select "input[type=hidden][name=referer]", 0
790 assert_select "input[type=submit]", 1
792 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
794 # The GET should preserve any referer
795 get :remove_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
796 assert_response :success
797 assert_template :remove_friend
798 assert_select "form" do
799 assert_select "input[type=hidden][name=referer][value=/test]", 1
800 assert_select "input[type=submit]", 1
802 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
804 # When logged in a POST should remove the friendship
805 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
806 assert_redirected_to user_path(:display_name => friend.display_name)
807 assert_match /was removed from your friends/, flash[:notice]
808 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
810 # A second POST should report that the friendship does not exist
811 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
812 assert_redirected_to user_path(:display_name => friend.display_name)
813 assert_match /is not one of your friends/, flash[:error]
814 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
818 # Try without logging in
819 get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"}
820 assert_response :redirect
821 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
823 # Now try as a normal user
824 get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:normal_user).id}
825 assert_response :redirect
826 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
828 # Finally try as an administrator
829 get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:administrator_user).id}
830 assert_response :redirect
831 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
832 assert_equal "suspended", User.find(users(:normal_user).id).status
836 # Try without logging in
837 get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"}
838 assert_response :redirect
839 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
841 # Now try as a normal user
842 get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:normal_user).id}
843 assert_response :redirect
844 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
846 # Finally try as an administrator
847 get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:administrator_user).id}
848 assert_response :redirect
849 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
851 # Check that the user was deleted properly
852 user = User.find(users(:normal_user).id)
853 assert_equal "user_1", user.display_name
854 assert_equal "", user.description
855 assert_nil user.home_lat
856 assert_nil user.home_lon
857 assert_equal false, user.image.file?
858 assert_equal false, user.email_valid
859 assert_nil user.new_email
860 assert_nil user.openid_url
861 assert_equal "deleted", user.status