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]
429 # Get a user to work with - note that this user deliberately
430 # conflicts with uppercase_user in the email and display name
431 # fields to test that we can change other fields without any
432 # validation errors being reported
433 user = users(:normal_user)
435 # Set the username cookie
436 @request.cookies["_osm_username"] = user.display_name
438 # Make sure that you are redirected to the login page when
439 # you are not logged in
440 get :account, { :display_name => user.display_name }
441 assert_response :redirect
442 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
444 # Make sure that you are redirected to the login page when
445 # you are not logged in as the right user
446 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
447 assert_response :redirect
448 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
450 # Make sure we get the page when we are logged in as the right user
451 get :account, { :display_name => user.display_name }, { "user" => user }
452 assert_response :success
453 assert_template :account
455 # Updating the description should work
456 user.description = "new description"
457 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
458 assert_response :success
459 assert_template :account
460 assert_select "div#errorExplanation", false
461 assert_select "div#notice", /^User information updated successfully/
462 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
464 # Changing name to one that exists should fail
465 user.display_name = users(:public_user).display_name
466 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
467 assert_response :success
468 assert_template :account
469 assert_select "div#notice", false
470 assert_select "div#errorExplanation"
471 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
473 # Changing name to one that exists should fail, regardless of case
474 user.display_name = users(:public_user).display_name.upcase
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#notice", false
479 assert_select "div#errorExplanation"
480 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
482 # Changing name to one that doesn't exist should work
483 user.display_name = "new tester"
484 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
485 assert_response :success
486 assert_template :account
487 assert_select "div#errorExplanation", false
488 assert_select "div#notice", /^User information updated successfully/
489 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", user.display_name
491 # Need to update cookies now to stay valid
492 @request.cookies["_osm_username"] = user.display_name
494 # Changing email to one that exists should fail
495 user.new_email = users(:public_user).email
496 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
497 assert_response :success
498 assert_template :account
499 assert_select "div#notice", false
500 assert_select "div#errorExplanation"
501 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_new_email"
503 # Changing email to one that exists should fail, regardless of case
504 user.new_email = users(:public_user).email.upcase
505 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
506 assert_response :success
507 assert_template :account
508 assert_select "div#notice", false
509 assert_select "div#errorExplanation"
510 assert_select "form#accountForm > fieldset > div.form-row > div.field_with_errors > input#user_new_email"
512 # Changing email to one that doesn't exist should work
513 user.new_email = "new_tester@example.com"
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 "div#errorExplanation", false
518 assert_select "div#notice", /^User information updated successfully/
519 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
522 # Check that the user account page will display and contains some relevant
523 # information for the user
524 def test_user_view_account
525 # Test a non-existent user
526 get :view, {:display_name => "unknown"}
527 assert_response :not_found
530 get :view, {:display_name => "test"}
531 assert_response :success
532 assert_select "div#userinformation" do
533 assert_select "a[href=/user/test/edits]", 1
534 assert_select "a[href=/user/test/traces]", 1
535 assert_select "a[href=/user/test/diary]", 1
536 assert_select "a[href=/user/test/diary/comments]", 1
537 assert_select "a[href=/user/test/account]", 0
538 assert_select "a[href=/user/test/blocks]", 0
539 assert_select "a[href=/user/test/blocks_by]", 0
540 assert_select "a[href=/blocks/new/test]", 0
543 # Test a user who has been blocked
544 get :view, {:display_name => "blocked"}
545 assert_response :success
546 assert_select "div#userinformation" do
547 assert_select "a[href=/user/blocked/edits]", 1
548 assert_select "a[href=/user/blocked/traces]", 1
549 assert_select "a[href=/user/blocked/diary]", 1
550 assert_select "a[href=/user/blocked/diary/comments]", 1
551 assert_select "a[href=/user/blocked/account]", 0
552 assert_select "a[href=/user/blocked/blocks]", 1
553 assert_select "a[href=/user/blocked/blocks_by]", 0
554 assert_select "a[href=/blocks/new/blocked]", 0
557 # Test a moderator who has applied blocks
558 get :view, {:display_name => "moderator"}
559 assert_response :success
560 assert_select "div#userinformation" do
561 assert_select "a[href=/user/moderator/edits]", 1
562 assert_select "a[href=/user/moderator/traces]", 1
563 assert_select "a[href=/user/moderator/diary]", 1
564 assert_select "a[href=/user/moderator/diary/comments]", 1
565 assert_select "a[href=/user/moderator/account]", 0
566 assert_select "a[href=/user/moderator/blocks]", 0
567 assert_select "a[href=/user/moderator/blocks_by]", 1
568 assert_select "a[href=/blocks/new/moderator]", 0
571 # Login as a normal user
572 session[:user] = users(:normal_user).id
573 cookies["_osm_username"] = users(:normal_user).display_name
575 # Test the normal user
576 get :view, {:display_name => "test"}
577 assert_response :success
578 assert_select "div#userinformation" do
579 assert_select "a[href=/user/test/edits]", 1
580 assert_select "a[href=/traces/mine]", 1
581 assert_select "a[href=/user/test/diary]", 1
582 assert_select "a[href=/user/test/diary/comments]", 1
583 assert_select "a[href=/user/test/account]", 1
584 assert_select "a[href=/user/test/blocks]", 0
585 assert_select "a[href=/user/test/blocks_by]", 0
586 assert_select "a[href=/blocks/new/test]", 0
589 # Login as a moderator
590 session[:user] = users(:moderator_user).id
591 cookies["_osm_username"] = users(:moderator_user).display_name
593 # Test the normal user
594 get :view, {:display_name => "test"}
595 assert_response :success
596 assert_select "div#userinformation" do
597 assert_select "a[href=/user/test/edits]", 1
598 assert_select "a[href=/user/test/traces]", 1
599 assert_select "a[href=/user/test/diary]", 1
600 assert_select "a[href=/user/test/diary/comments]", 1
601 assert_select "a[href=/user/test/account]", 0
602 assert_select "a[href=/user/test/blocks]", 0
603 assert_select "a[href=/user/test/blocks_by]", 0
604 assert_select "a[href=/blocks/new/test]", 1
608 def test_user_api_read
609 # check that a visible user is returned properly
610 get :api_read, :id => users(:normal_user).id
611 assert_response :success
613 # check the data that is returned
614 assert_select "description", :count => 1, :text => "test"
615 assert_select "contributor-terms", :count => 1 do
616 assert_select "[agreed=true]"
618 assert_select "img", :count => 1
619 assert_select "roles", :count => 1 do
620 assert_select "role", :count => 0
622 assert_select "changesets", :count => 1 do
623 assert_select "[count=0]"
625 assert_select "traces", :count => 1 do
626 assert_select "[count=0]"
628 assert_select "blocks", :count => 1 do
629 assert_select "received", :count => 1 do
630 assert_select "[count=0][active=0]"
632 assert_select "issued", :count => 0
635 # check that we aren't revealing private information
636 assert_select "contributor-terms[pd]", false
637 assert_select "home", false
638 assert_select "languages", false
639 assert_select "messages", false
641 # check that a suspended user is not returned
642 get :api_read, :id => users(:suspended_user).id
643 assert_response :gone
645 # check that a deleted user is not returned
646 get :api_read, :id => users(:deleted_user).id
647 assert_response :gone
649 # check that a non-existent user is not returned
650 get :api_read, :id => 0
651 assert_response :not_found
654 def test_user_api_details
655 # check that nothing is returned when not logged in
657 assert_response :unauthorized
659 # check that we get a response when logged in
660 basic_authorization(users(:normal_user).email, "test")
662 assert_response :success
664 # check the data that is returned
665 assert_select "description", :count => 1, :text => "test"
666 assert_select "contributor-terms", :count => 1 do
667 assert_select "[agreed=true][pd=false]"
669 assert_select "img", :count => 1
670 assert_select "roles", :count => 1 do
671 assert_select "role", :count => 0
673 assert_select "changesets", :count => 1 do
674 assert_select "[count=0]", :count => 1
676 assert_select "traces", :count => 1 do
677 assert_select "[count=0]", :count => 1
679 assert_select "blocks", :count => 1 do
680 assert_select "received", :count => 1 do
681 assert_select "[count=0][active=0]"
683 assert_select "issued", :count => 0
685 assert_select "home", :count => 1 do
686 assert_select "[lat=12.1][lon=12.1][zoom=3]"
688 assert_select "languages", :count => 1 do
689 assert_select "lang", :count => 1, :text => "en"
691 assert_select "messages", :count => 1 do
692 assert_select "received", :count => 1 do
693 assert_select "[count=1][unread=0]"
695 assert_select "sent", :count => 1 do
696 assert_select "[count=1]"
701 def test_user_make_friend
702 # Get users to work with
703 user = users(:normal_user)
704 friend = users(:second_public_user)
706 # Check that the users aren't already friends
707 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
709 # Set the username cookie
710 @request.cookies["_osm_username"] = user.display_name
712 # When not logged in a GET should ask us to login
713 get :make_friend, {:display_name => friend.display_name}
714 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
716 # When not logged in a POST should error
717 post :make_friend, {:display_name => friend.display_name}
718 assert_response :forbidden
719 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
721 # When logged in a GET should get a confirmation page
722 get :make_friend, {:display_name => friend.display_name}, {"user" => user}
723 assert_response :success
724 assert_template :make_friend
725 assert_select "form" do
726 assert_select "input[type=hidden][name=referer]", 0
727 assert_select "input[type=submit]", 1
729 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
731 # The GET should preserve any referer
732 get :make_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
733 assert_response :success
734 assert_template :make_friend
735 assert_select "form" do
736 assert_select "input[type=hidden][name=referer][value=/test]", 1
737 assert_select "input[type=submit]", 1
739 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
741 # When logged in a POST should add the friendship
742 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
743 assert_redirected_to user_path(:display_name => friend.display_name)
744 assert_match /is now your friend/, flash[:notice]
745 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
747 # A second POST should report that the friendship already exists
748 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
749 assert_redirected_to user_path(:display_name => friend.display_name)
750 assert_match /You are already friends with/, flash[:warning]
751 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
754 def test_user_remove_friend
755 # Get users to work with
756 user = users(:normal_user)
757 friend = users(:public_user)
759 # Check that the users are friends
760 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
762 # Set the username cookie
763 @request.cookies["_osm_username"] = user.display_name
765 # When not logged in a GET should ask us to login
766 get :remove_friend, {:display_name => friend.display_name}
767 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
769 # When not logged in a POST should error
770 post :remove_friend, {:display_name => friend.display_name}
771 assert_response :forbidden
772 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
774 # When logged in a GET should get a confirmation page
775 get :remove_friend, {:display_name => friend.display_name}, {"user" => user}
776 assert_response :success
777 assert_template :remove_friend
778 assert_select "form" do
779 assert_select "input[type=hidden][name=referer]", 0
780 assert_select "input[type=submit]", 1
782 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
784 # The GET should preserve any referer
785 get :remove_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
786 assert_response :success
787 assert_template :remove_friend
788 assert_select "form" do
789 assert_select "input[type=hidden][name=referer][value=/test]", 1
790 assert_select "input[type=submit]", 1
792 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
794 # When logged in a POST should remove the friendship
795 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
796 assert_redirected_to user_path(:display_name => friend.display_name)
797 assert_match /was removed from your friends/, flash[:notice]
798 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
800 # A second POST should report that the friendship does not exist
801 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
802 assert_redirected_to user_path(:display_name => friend.display_name)
803 assert_match /is not one of your friends/, flash[:error]
804 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first