3 class UserControllerTest < ActionController::TestCase
5 fixtures :user_blocks, :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
185 def test_user_create_view
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
214 user.status = "pending"
215 user.display_name = "new_tester"
216 user.email = "newtester@osm.org"
217 user.email_confirmation = "newtester@osm.org"
218 user.pass_crypt = "testtest"
219 user.pass_crypt_confirmation = "testtest"
223 def test_user_create_success
226 assert_difference("User.count", 1) do
227 assert_difference("ActionMailer::Base.deliveries.size", 1) do
228 post :save, {}, { :new_user => user }
233 register_email = ActionMailer::Base.deliveries.first
235 assert_equal register_email.to[0], user.email
236 assert_match /#{@url}/, register_email.body.to_s
239 assert_redirected_to :action => "confirm", :display_name => user.display_name
241 ActionMailer::Base.deliveries.clear
244 def test_user_create_submit_duplicate_email
246 user.email = users(:public_user).email
248 assert_no_difference("User.count") do
249 assert_no_difference("ActionMailer::Base.deliveries.size") do
250 post :save, {}, { :new_user => user }
254 assert_response :success
255 assert_template "new"
256 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
259 def test_user_create_submit_duplicate_email_uppercase
261 user.email = users(:public_user).email.upcase
263 assert_no_difference("User.count") do
264 assert_no_difference("ActionMailer::Base.deliveries.size") do
265 post :save, {}, { :new_user => user }
269 assert_response :success
270 assert_template "new"
271 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
274 def test_user_create_submit_duplicate_name
276 user.display_name = users(:public_user).display_name
278 assert_no_difference("User.count") do
279 assert_no_difference("ActionMailer::Base.deliveries.size") do
280 post :save, {}, { :new_user => user }
284 assert_response :success
285 assert_template "new"
286 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
289 def test_user_create_submit_duplicate_name_uppercase
291 user.display_name = users(:public_user).display_name.upcase
293 assert_no_difference("User.count") do
294 assert_no_difference("ActionMailer::Base.deliveries.size") do
295 post :save, {}, { :new_user => user }
299 assert_response :success
300 assert_template "new"
301 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
304 def test_user_save_referer_params
307 assert_difference("User.count", 1) do
308 assert_difference("ActionMailer::Base.deliveries.size", 1) do
309 post :save, {}, { :new_user => user,
310 :referer => "/edit?editor=id#map=1/2/3" }
314 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
315 user.tokens.order("id DESC").first.referer
317 ActionMailer::Base.deliveries.clear
320 def test_user_confirm_expired_token
321 user = users(:inactive_user)
322 token = user.tokens.new
323 token.expiry = 1.day.ago
326 @request.cookies["_osm_session"] = user.display_name
327 post :confirm, :confirm_string => token.token
329 assert_redirected_to :action => "confirm"
330 assert_match /expired/, flash[:error]
333 def test_user_already_confirmed
334 user = users(:normal_user)
335 token = user.tokens.create
337 @request.cookies["_osm_session"] = user.display_name
338 post :confirm, :confirm_string => token.token
340 assert_redirected_to :action => "login"
341 assert_match /confirmed/, flash[:error]
344 def test_user_terms_new_user
345 get :terms, {}, { "new_user" => User.new }
346 assert_response :success
347 assert_template :terms
350 def test_user_terms_seen
351 user = users(:normal_user)
353 get :terms, {}, { "user" => user }
354 assert_response :redirect
355 assert_redirected_to :action => :account, :display_name => user.display_name
358 def test_user_go_public
359 post :go_public, {}, { :user => users(:normal_user) }
360 assert_response :redirect
361 assert_redirected_to :action => :account, :display_name => users(:normal_user).display_name
362 assert_equal true, User.find(users(:normal_user).id).data_public
365 def test_user_lost_password
366 # Test fetching the lost password page
368 assert_response :success
369 assert_template :lost_password
370 assert_select "div#notice", false
372 # Test resetting using the address as recorded for a user that has an
373 # address which is duplicated in a different case by another user
374 assert_difference("ActionMailer::Base.deliveries.size", 1) do
375 post :lost_password, :user => { :email => users(:normal_user).email }
377 assert_response :redirect
378 assert_redirected_to :action => :login
379 assert_match /^Sorry you lost it/, flash[:notice]
380 email = ActionMailer::Base.deliveries.first
381 assert_equal 1, email.to.count
382 assert_equal users(:normal_user).email, email.to.first
383 ActionMailer::Base.deliveries.clear
385 # Test resetting using an address that matches a different user
386 # that has the same address in a different case
387 assert_difference("ActionMailer::Base.deliveries.size", 1) do
388 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
390 assert_response :redirect
391 assert_redirected_to :action => :login
392 assert_match /^Sorry you lost it/, flash[:notice]
393 email = ActionMailer::Base.deliveries.first
394 assert_equal 1, email.to.count
395 assert_equal users(:uppercase_user).email, email.to.first
396 ActionMailer::Base.deliveries.clear
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 ".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 email = ActionMailer::Base.deliveries.first
416 assert_equal 1, email.to.count
417 assert_equal users(:public_user).email, email.to.first
418 ActionMailer::Base.deliveries.clear
420 # Test resetting using an address that matches a user that has the
421 # same (case insensitively unique) address in a different case
422 assert_difference("ActionMailer::Base.deliveries.size", 1) do
423 post :lost_password, :user => { :email => users(:public_user).email.upcase }
425 assert_response :redirect
426 assert_redirected_to :action => :login
427 assert_match /^Sorry you lost it/, flash[:notice]
428 email = ActionMailer::Base.deliveries.first
429 assert_equal 1, email.to.count
430 assert_equal users(:public_user).email, email.to.first
431 ActionMailer::Base.deliveries.clear
434 def test_reset_password
435 # Test a request with no token
437 assert_response :bad_request
439 # Test a request with a bogus token
440 get :reset_password, :token => "made_up_token"
441 assert_response :redirect
442 assert_redirected_to :action => :lost_password
444 # Create a valid token for a user
445 token = User.find(users(:inactive_user).id).tokens.create
447 # Test a request with a valid token
448 get :reset_password, :token => token.token
449 assert_response :success
450 assert_template :reset_password
452 # Test setting a new password
453 post :reset_password, :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" }
454 assert_response :redirect
455 assert_redirected_to :action => :login
456 user = User.find(users(:inactive_user).id)
457 assert_equal "active", user.status
458 assert_equal true, user.email_valid
459 assert_equal user, User.authenticate(:username => "inactive@openstreetmap.org", :password => "new_password")
463 # Get a user to work with - note that this user deliberately
464 # conflicts with uppercase_user in the email and display name
465 # fields to test that we can change other fields without any
466 # validation errors being reported
467 user = users(:normal_user)
469 # Make sure that you are redirected to the login page when
470 # you are not logged in
471 get :account, :display_name => user.display_name
472 assert_response :redirect
473 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
475 # Make sure that you are blocked when not logged in as the right user
476 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
477 assert_response :forbidden
479 # Make sure we get the page when we are logged in as the right user
480 get :account, { :display_name => user.display_name }, { "user" => user }
481 assert_response :success
482 assert_template :account
484 # Updating the description should work
485 user.description = "new description"
486 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
487 assert_response :success
488 assert_template :account
489 assert_select "div#errorExplanation", false
490 assert_select ".notice", /^User information updated successfully/
491 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
493 # Changing name to one that exists should fail
494 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name)
495 post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id }
496 assert_response :success
497 assert_template :account
498 assert_select ".notice", false
499 assert_select "div#errorExplanation"
500 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
502 # Changing name to one that exists should fail, regardless of case
503 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name.upcase)
504 post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id }
505 assert_response :success
506 assert_template :account
507 assert_select ".notice", false
508 assert_select "div#errorExplanation"
509 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
511 # Changing name to one that doesn't exist should work
512 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
513 post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id }
514 assert_response :success
515 assert_template :account
516 assert_select "div#errorExplanation", false
517 assert_select ".notice", /^User information updated successfully/
518 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
520 # Record the change of name
521 user.display_name = "new tester"
523 # Changing email to one that exists should fail
524 user.new_email = users(:public_user).email
525 assert_no_difference("ActionMailer::Base.deliveries.size") do
526 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
528 assert_response :success
529 assert_template :account
530 assert_select ".notice", false
531 assert_select "div#errorExplanation"
532 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
534 # Changing email to one that exists should fail, regardless of case
535 user.new_email = users(:public_user).email.upcase
536 assert_no_difference("ActionMailer::Base.deliveries.size") do
537 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
539 assert_response :success
540 assert_template :account
541 assert_select ".notice", false
542 assert_select "div#errorExplanation"
543 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
545 # Changing email to one that doesn't exist should work
546 user.new_email = "new_tester@example.com"
547 assert_difference("ActionMailer::Base.deliveries.size", 1) do
548 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
550 assert_response :success
551 assert_template :account
552 assert_select "div#errorExplanation", false
553 assert_select ".notice", /^User information updated successfully/
554 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
555 email = ActionMailer::Base.deliveries.first
556 assert_equal 1, email.to.count
557 assert_equal user.new_email, email.to.first
558 ActionMailer::Base.deliveries.clear
561 # Check that the user account page will display and contains some relevant
562 # information for the user
563 def test_user_view_account
564 # Test a non-existent user
565 get :view, :display_name => "unknown"
566 assert_response :not_found
569 get :view, :display_name => "test"
570 assert_response :success
571 assert_select "div#userinformation" do
572 assert_select "a[href^='/user/test/history']", 1
573 assert_select "a[href='/user/test/traces']", 1
574 assert_select "a[href='/user/test/diary']", 1
575 assert_select "a[href='/user/test/diary/comments']", 1
576 assert_select "a[href='/user/test/account']", 0
577 assert_select "a[href='/user/test/blocks']", 0
578 assert_select "a[href='/user/test/blocks_by']", 0
579 assert_select "a[href='/blocks/new/test']", 0
582 # Test a user who has been blocked
583 get :view, :display_name => "blocked"
584 assert_response :success
585 assert_select "div#userinformation" do
586 assert_select "a[href^='/user/blocked/history']", 1
587 assert_select "a[href='/user/blocked/traces']", 1
588 assert_select "a[href='/user/blocked/diary']", 1
589 assert_select "a[href='/user/blocked/diary/comments']", 1
590 assert_select "a[href='/user/blocked/account']", 0
591 assert_select "a[href='/user/blocked/blocks']", 1
592 assert_select "a[href='/user/blocked/blocks_by']", 0
593 assert_select "a[href='/blocks/new/blocked']", 0
596 # Test a moderator who has applied blocks
597 get :view, :display_name => "moderator"
598 assert_response :success
599 assert_select "div#userinformation" do
600 assert_select "a[href^='/user/moderator/history']", 1
601 assert_select "a[href='/user/moderator/traces']", 1
602 assert_select "a[href='/user/moderator/diary']", 1
603 assert_select "a[href='/user/moderator/diary/comments']", 1
604 assert_select "a[href='/user/moderator/account']", 0
605 assert_select "a[href='/user/moderator/blocks']", 0
606 assert_select "a[href='/user/moderator/blocks_by']", 1
607 assert_select "a[href='/blocks/new/moderator']", 0
610 # Login as a normal user
611 session[:user] = users(:normal_user).id
613 # Test the normal user
614 get :view, :display_name => "test"
615 assert_response :success
616 assert_select "div#userinformation" do
617 assert_select "a[href^='/user/test/history']", 1
618 assert_select "a[href='/traces/mine']", 1
619 assert_select "a[href='/user/test/diary']", 1
620 assert_select "a[href='/user/test/diary/comments']", 1
621 assert_select "a[href='/user/test/account']", 1
622 assert_select "a[href='/user/test/blocks']", 0
623 assert_select "a[href='/user/test/blocks_by']", 0
624 assert_select "a[href='/blocks/new/test']", 0
627 # Login as a moderator
628 session[:user] = users(:moderator_user).id
630 # Test the normal user
631 get :view, :display_name => "test"
632 assert_response :success
633 assert_select "div#userinformation" do
634 assert_select "a[href^='/user/test/history']", 1
635 assert_select "a[href='/user/test/traces']", 1
636 assert_select "a[href='/user/test/diary']", 1
637 assert_select "a[href='/user/test/diary/comments']", 1
638 assert_select "a[href='/user/test/account']", 0
639 assert_select "a[href='/user/test/blocks']", 0
640 assert_select "a[href='/user/test/blocks_by']", 0
641 assert_select "a[href='/blocks/new/test']", 1
645 def test_user_api_read
646 # check that a visible user is returned properly
647 get :api_read, :id => users(:normal_user).id
648 assert_response :success
650 # check the data that is returned
651 assert_select "description", :count => 1, :text => "test"
652 assert_select "contributor-terms", :count => 1 do
653 assert_select "[agreed='true']"
655 assert_select "img", :count => 1
656 assert_select "roles", :count => 1 do
657 assert_select "role", :count => 0
659 assert_select "changesets", :count => 1 do
660 assert_select "[count='0']"
662 assert_select "traces", :count => 1 do
663 assert_select "[count='0']"
665 assert_select "blocks", :count => 1 do
666 assert_select "received", :count => 1 do
667 assert_select "[count='0'][active='0']"
669 assert_select "issued", :count => 0
672 # check that we aren't revealing private information
673 assert_select "contributor-terms[pd]", false
674 assert_select "home", false
675 assert_select "languages", false
676 assert_select "messages", false
678 # check that a suspended user is not returned
679 get :api_read, :id => users(:suspended_user).id
680 assert_response :gone
682 # check that a deleted user is not returned
683 get :api_read, :id => users(:deleted_user).id
684 assert_response :gone
686 # check that a non-existent user is not returned
687 get :api_read, :id => 0
688 assert_response :not_found
691 def test_user_api_details
692 # check that nothing is returned when not logged in
694 assert_response :unauthorized
696 # check that we get a response when logged in
697 basic_authorization(users(:normal_user).email, "test")
699 assert_response :success
701 # check the data that is returned
702 assert_select "description", :count => 1, :text => "test"
703 assert_select "contributor-terms", :count => 1 do
704 assert_select "[agreed='true'][pd='false']"
706 assert_select "img", :count => 1
707 assert_select "roles", :count => 1 do
708 assert_select "role", :count => 0
710 assert_select "changesets", :count => 1 do
711 assert_select "[count='0']", :count => 1
713 assert_select "traces", :count => 1 do
714 assert_select "[count='0']", :count => 1
716 assert_select "blocks", :count => 1 do
717 assert_select "received", :count => 1 do
718 assert_select "[count='0'][active='0']"
720 assert_select "issued", :count => 0
722 assert_select "home", :count => 1 do
723 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
725 assert_select "languages", :count => 1 do
726 assert_select "lang", :count => 1, :text => "en"
728 assert_select "messages", :count => 1 do
729 assert_select "received", :count => 1 do
730 assert_select "[count='1'][unread='0']"
732 assert_select "sent", :count => 1 do
733 assert_select "[count='1']"
738 def test_user_make_friend
739 # Get users to work with
740 user = users(:normal_user)
741 friend = users(:second_public_user)
743 # Check that the users aren't already friends
744 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
746 # When not logged in a GET should ask us to login
747 get :make_friend, :display_name => friend.display_name
748 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
750 # When not logged in a POST should error
751 post :make_friend, :display_name => friend.display_name
752 assert_response :forbidden
753 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
755 # When logged in a GET should get a confirmation page
756 get :make_friend, { :display_name => friend.display_name }, { "user" => user }
757 assert_response :success
758 assert_template :make_friend
759 assert_select "form" do
760 assert_select "input[type='hidden'][name='referer']", 0
761 assert_select "input[type='submit']", 1
763 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
765 # The GET should preserve any referer
766 get :make_friend, { :display_name => friend.display_name, :referer => "/test" }, { "user" => user }
767 assert_response :success
768 assert_template :make_friend
769 assert_select "form" do
770 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
771 assert_select "input[type='submit']", 1
773 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
775 # When logged in a POST should add the friendship
776 assert_difference("ActionMailer::Base.deliveries.size", 1) do
777 post :make_friend, { :display_name => friend.display_name }, { "user" => user }
779 assert_redirected_to user_path(:display_name => friend.display_name)
780 assert_match /is now your friend/, flash[:notice]
781 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
782 email = ActionMailer::Base.deliveries.first
783 assert_equal 1, email.to.count
784 assert_equal friend.email, email.to.first
785 ActionMailer::Base.deliveries.clear
787 # A second POST should report that the friendship already exists
788 assert_no_difference("ActionMailer::Base.deliveries.size") do
789 post :make_friend, { :display_name => friend.display_name }, { "user" => user }
791 assert_redirected_to user_path(:display_name => friend.display_name)
792 assert_match /You are already friends with/, flash[:warning]
793 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
796 def test_user_remove_friend
797 # Get users to work with
798 user = users(:normal_user)
799 friend = users(:public_user)
801 # Check that the users are friends
802 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
804 # When not logged in a GET should ask us to login
805 get :remove_friend, :display_name => friend.display_name
806 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
808 # When not logged in a POST should error
809 post :remove_friend, :display_name => friend.display_name
810 assert_response :forbidden
811 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
813 # When logged in a GET should get a confirmation page
814 get :remove_friend, { :display_name => friend.display_name }, { "user" => user }
815 assert_response :success
816 assert_template :remove_friend
817 assert_select "form" do
818 assert_select "input[type='hidden'][name='referer']", 0
819 assert_select "input[type='submit']", 1
821 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
823 # The GET should preserve any referer
824 get :remove_friend, { :display_name => friend.display_name, :referer => "/test" }, { "user" => user }
825 assert_response :success
826 assert_template :remove_friend
827 assert_select "form" do
828 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
829 assert_select "input[type='submit']", 1
831 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
833 # When logged in a POST should remove the friendship
834 post :remove_friend, { :display_name => friend.display_name }, { "user" => user }
835 assert_redirected_to user_path(:display_name => friend.display_name)
836 assert_match /was removed from your friends/, flash[:notice]
837 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
839 # A second POST should report that the friendship does not exist
840 post :remove_friend, { :display_name => friend.display_name }, { "user" => user }
841 assert_redirected_to user_path(:display_name => friend.display_name)
842 assert_match /is not one of your friends/, flash[:error]
843 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
847 # Try without logging in
848 get :set_status, :display_name => users(:normal_user).display_name, :status => "suspended"
849 assert_response :redirect
850 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
852 # Now try as a normal user
853 get :set_status, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:normal_user).id }
854 assert_response :redirect
855 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
857 # Finally try as an administrator
858 get :set_status, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:administrator_user).id }
859 assert_response :redirect
860 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
861 assert_equal "suspended", User.find(users(:normal_user).id).status
865 # Try without logging in
866 get :delete, :display_name => users(:normal_user).display_name, :status => "suspended"
867 assert_response :redirect
868 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
870 # Now try as a normal user
871 get :delete, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:normal_user).id }
872 assert_response :redirect
873 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
875 # Finally try as an administrator
876 get :delete, { :display_name => users(:normal_user).display_name, :status => "suspended" }, { :user => users(:administrator_user).id }
877 assert_response :redirect
878 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
880 # Check that the user was deleted properly
881 user = User.find(users(:normal_user).id)
882 assert_equal "user_1", user.display_name
883 assert_equal "", user.description
884 assert_nil user.home_lat
885 assert_nil user.home_lon
886 assert_equal false, user.image.file?
887 assert_equal false, user.email_valid
888 assert_nil user.new_email
889 assert_nil user.auth_provider
890 assert_nil user.auth_uid
891 assert_equal "deleted", user.status