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" }
54 { :path => "/user/new", :method => :post },
55 { :controller => "user", :action => "create" }
59 { :path => "/user/terms", :method => :get },
60 { :controller => "user", :action => "terms" }
64 { :path => "/user/save", :method => :post },
65 { :controller => "user", :action => "save" }
69 { :path => "/user/username/confirm", :method => :get },
70 { :controller => "user", :action => "confirm", :display_name => "username" }
73 { :path => "/user/username/confirm", :method => :post },
74 { :controller => "user", :action => "confirm", :display_name => "username" }
77 { :path => "/user/username/confirm/resend", :method => :get },
78 { :controller => "user", :action => "confirm_resend", :display_name => "username" }
82 { :path => "/user/confirm", :method => :get },
83 { :controller => "user", :action => "confirm" }
86 { :path => "/user/confirm", :method => :post },
87 { :controller => "user", :action => "confirm" }
90 { :path => "/user/confirm-email", :method => :get },
91 { :controller => "user", :action => "confirm_email" }
94 { :path => "/user/confirm-email", :method => :post },
95 { :controller => "user", :action => "confirm_email" }
99 { :path => "/user/go_public", :method => :post },
100 { :controller => "user", :action => "go_public" }
104 { :path => "/user/forgot-password", :method => :get },
105 { :controller => "user", :action => "lost_password" }
108 { :path => "/user/forgot-password", :method => :post },
109 { :controller => "user", :action => "lost_password" }
112 { :path => "/user/reset-password", :method => :get },
113 { :controller => "user", :action => "reset_password" }
116 { :path => "/user/reset-password", :method => :post },
117 { :controller => "user", :action => "reset_password" }
121 { :path => "/user/suspended", :method => :get },
122 { :controller => "user", :action => "suspended" }
126 { :path => "/user/username", :method => :get },
127 { :controller => "user", :action => "view", :display_name => "username" }
131 { :path => "/user/username/account", :method => :get },
132 { :controller => "user", :action => "account", :display_name => "username" }
135 { :path => "/user/username/account", :method => :post },
136 { :controller => "user", :action => "account", :display_name => "username" }
140 { :path => "/user/username/make_friend", :method => :get },
141 { :controller => "user", :action => "make_friend", :display_name => "username" }
144 { :path => "/user/username/make_friend", :method => :post },
145 { :controller => "user", :action => "make_friend", :display_name => "username" }
148 { :path => "/user/username/remove_friend", :method => :get },
149 { :controller => "user", :action => "remove_friend", :display_name => "username" }
152 { :path => "/user/username/remove_friend", :method => :post },
153 { :controller => "user", :action => "remove_friend", :display_name => "username" }
157 { :path => "/user/username/set_status", :method => :get },
158 { :controller => "user", :action => "set_status", :display_name => "username" }
161 { :path => "/user/username/delete", :method => :get },
162 { :controller => "user", :action => "delete", :display_name => "username" }
166 { :path => "/users", :method => :get },
167 { :controller => "user", :action => "list" }
170 { :path => "/users", :method => :post },
171 { :controller => "user", :action => "list" }
174 { :path => "/users/status", :method => :get },
175 { :controller => "user", :action => "list", :status => "status" }
178 { :path => "/users/status", :method => :post },
179 { :controller => "user", :action => "list", :status => "status" }
183 # The user creation page loads
184 def test_user_create_view
186 assert_response :redirect
187 assert_redirected_to user_new_path(:cookie_test => "true")
189 get :new, { :cookie_test => "true" }, { :cookie_test => true }
190 assert_response :success
192 assert_select "html", :count => 1 do
193 assert_select "head", :count => 1 do
194 assert_select "title", :text => /Sign Up/, :count => 1
196 assert_select "body", :count => 1 do
197 assert_select "div#content", :count => 1 do
198 assert_select "form[action='/user/new'][method='post']", :count => 1 do
199 assert_select "input[id='user_email']", :count => 1
200 assert_select "input[id='user_email_confirmation']", :count => 1
201 assert_select "input[id='user_display_name']", :count => 1
202 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
203 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
204 assert_select "input[type='submit'][value='Sign Up']", :count => 1
213 user.status = "pending"
214 user.display_name = "new_tester"
215 user.email = "newtester@osm.org"
216 user.email_confirmation = "newtester@osm.org"
217 user.pass_crypt = "testtest"
218 user.pass_crypt_confirmation = "testtest"
222 def test_user_create_success
225 assert_difference('User.count', 1) do
226 assert_difference('ActionMailer::Base.deliveries.size', 1) do
227 post :save, {}, {:new_user => user}
232 register_email = ActionMailer::Base.deliveries.first
234 assert_equal register_email.to[0], user.email
235 assert_match /#{@url}/, register_email.body.to_s
238 assert_redirected_to :action => 'confirm', :display_name => user.display_name
240 ActionMailer::Base.deliveries.clear
243 def test_user_create_submit_duplicate_email
245 user.email = users(:public_user).email
247 assert_no_difference('User.count') do
248 assert_no_difference('ActionMailer::Base.deliveries.size') do
249 post :save, {}, {:new_user => user}
253 assert_response :success
254 assert_template 'new'
255 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
258 def test_user_create_submit_duplicate_email_uppercase
260 user.email = users(:public_user).email.upcase
262 assert_no_difference('User.count') do
263 assert_no_difference('ActionMailer::Base.deliveries.size') do
264 post :save, {}, {:new_user => user}
268 assert_response :success
269 assert_template 'new'
270 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
273 def test_user_create_submit_duplicate_name
275 user.display_name = users(:public_user).display_name
277 assert_no_difference('User.count') do
278 assert_no_difference('ActionMailer::Base.deliveries.size') do
279 post :save, {}, {:new_user => user}
283 assert_response :success
284 assert_template 'new'
285 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
288 def test_user_create_submit_duplicate_name_uppercase
290 user.display_name = users(:public_user).display_name.upcase
292 assert_no_difference('User.count') do
293 assert_no_difference('ActionMailer::Base.deliveries.size') do
294 post :save, {}, {:new_user => user}
298 assert_response :success
299 assert_template 'new'
300 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
303 def test_user_save_referer_params
306 assert_difference('User.count', 1) do
307 assert_difference('ActionMailer::Base.deliveries.size', 1) do
308 post :save, {}, {:new_user => user,
309 :referer => '/edit?editor=id#map=1/2/3'}
313 assert_equal welcome_path(:editor => 'id', :zoom => 1, :lat => 2, :lon => 3),
314 user.tokens.order("id DESC").first.referer
316 ActionMailer::Base.deliveries.clear
319 def test_user_confirm_expired_token
320 user = users(:inactive_user)
321 token = user.tokens.new
322 token.expiry = 1.day.ago
325 @request.cookies["_osm_session"] = user.display_name
326 post :confirm, :confirm_string => token.token
328 assert_redirected_to :action => 'confirm'
329 assert_match /expired/, flash[:error]
332 def test_user_already_confirmed
333 user = users(:normal_user)
334 token = user.tokens.create
336 @request.cookies["_osm_session"] = user.display_name
337 post :confirm, :confirm_string => token.token
339 assert_redirected_to :action => 'login'
340 assert_match /confirmed/, flash[:error]
343 def test_user_terms_new_user
344 get :terms, {}, { "new_user" => User.new }
345 assert_response :success
346 assert_template :terms
349 def test_user_terms_seen
350 user = users(:normal_user)
352 get :terms, {}, { "user" => user }
353 assert_response :redirect
354 assert_redirected_to :action => :account, :display_name => user.display_name
357 def test_user_go_public
358 post :go_public, {}, { :user => users(:normal_user) }
359 assert_response :redirect
360 assert_redirected_to :action => :account, :display_name => users(:normal_user).display_name
361 assert_equal true, User.find(users(:normal_user).id).data_public
364 def test_user_lost_password
365 # Test fetching the lost password page
367 assert_response :success
368 assert_template :lost_password
369 assert_select "div#notice", false
371 # Test resetting using the address as recorded for a user that has an
372 # address which is duplicated in a different case by another user
373 assert_difference('ActionMailer::Base.deliveries.size', 1) do
374 post :lost_password, :user => { :email => users(:normal_user).email }
376 assert_response :redirect
377 assert_redirected_to :action => :login
378 assert_match /^Sorry you lost it/, flash[:notice]
379 email = ActionMailer::Base.deliveries.first
380 assert_equal 1, email.to.count
381 assert_equal users(:normal_user).email, email.to.first
382 ActionMailer::Base.deliveries.clear
384 # Test resetting using an address that matches a different user
385 # that has the same address in a different case
386 assert_difference('ActionMailer::Base.deliveries.size', 1) do
387 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
389 assert_response :redirect
390 assert_redirected_to :action => :login
391 assert_match /^Sorry you lost it/, flash[:notice]
392 email = ActionMailer::Base.deliveries.first
393 assert_equal 1, email.to.count
394 assert_equal users(:uppercase_user).email, email.to.first
395 ActionMailer::Base.deliveries.clear
397 # Test resetting using an address that is a case insensitive match
398 # for more than one user but not an exact match for either
399 assert_difference('ActionMailer::Base.deliveries.size', 0) do
400 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
402 assert_response :success
403 assert_template :lost_password
404 assert_select ".error", /^Could not find that email address/
406 # Test resetting using the address as recorded for a user that has an
407 # address which is case insensitively unique
408 assert_difference('ActionMailer::Base.deliveries.size', 1) do
409 post :lost_password, :user => { :email => users(:public_user).email }
411 assert_response :redirect
412 assert_redirected_to :action => :login
413 assert_match /^Sorry you lost it/, flash[:notice]
414 email = ActionMailer::Base.deliveries.first
415 assert_equal 1, email.to.count
416 assert_equal users(:public_user).email, email.to.first
417 ActionMailer::Base.deliveries.clear
419 # Test resetting using an address that matches a user that has the
420 # same (case insensitively unique) address in a different case
421 assert_difference('ActionMailer::Base.deliveries.size', 1) do
422 post :lost_password, :user => { :email => users(:public_user).email.upcase }
424 assert_response :redirect
425 assert_redirected_to :action => :login
426 assert_match /^Sorry you lost it/, flash[:notice]
427 email = ActionMailer::Base.deliveries.first
428 assert_equal 1, email.to.count
429 assert_equal users(:public_user).email, email.to.first
430 ActionMailer::Base.deliveries.clear
433 def test_reset_password
434 # Test a request with no token
436 assert_response :bad_request
438 # Test a request with a bogus token
439 get :reset_password, :token => "made_up_token"
440 assert_response :redirect
441 assert_redirected_to :action => :lost_password
443 # Create a valid token for a user
444 token = User.find(users(:inactive_user).id).tokens.create
446 # Test a request with a valid token
447 get :reset_password, :token => token.token
448 assert_response :success
449 assert_template :reset_password
451 # Test setting a new password
452 post :reset_password, :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" }
453 assert_response :redirect
454 assert_redirected_to :action => :login
455 user = User.find(users(:inactive_user).id)
456 assert_equal "active", user.status
457 assert_equal true, user.email_valid
458 assert_equal user, User.authenticate(:username => "inactive@openstreetmap.org", :password => "new_password")
462 # Get a user to work with - note that this user deliberately
463 # conflicts with uppercase_user in the email and display name
464 # fields to test that we can change other fields without any
465 # validation errors being reported
466 user = users(:normal_user)
468 # Make sure that you are redirected to the login page when
469 # you are not logged in
470 get :account, { :display_name => user.display_name }
471 assert_response :redirect
472 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
474 # Make sure that you are blocked when not logged in as the right user
475 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
476 assert_response :forbidden
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 ".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 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name)
494 post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id }
495 assert_response :success
496 assert_template :account
497 assert_select ".notice", false
498 assert_select "div#errorExplanation"
499 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
501 # Changing name to one that exists should fail, regardless of case
502 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name.upcase)
503 post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id }
504 assert_response :success
505 assert_template :account
506 assert_select ".notice", false
507 assert_select "div#errorExplanation"
508 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
510 # Changing name to one that doesn't exist should work
511 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
512 post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id }
513 assert_response :success
514 assert_template :account
515 assert_select "div#errorExplanation", false
516 assert_select ".notice", /^User information updated successfully/
517 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
519 # Record the change of name
520 user.display_name = "new tester"
522 # Changing email to one that exists should fail
523 user.new_email = users(:public_user).email
524 assert_no_difference('ActionMailer::Base.deliveries.size') do
525 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
527 assert_response :success
528 assert_template :account
529 assert_select ".notice", false
530 assert_select "div#errorExplanation"
531 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
533 # Changing email to one that exists should fail, regardless of case
534 user.new_email = users(:public_user).email.upcase
535 assert_no_difference('ActionMailer::Base.deliveries.size') do
536 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
538 assert_response :success
539 assert_template :account
540 assert_select ".notice", false
541 assert_select "div#errorExplanation"
542 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
544 # Changing email to one that doesn't exist should work
545 user.new_email = "new_tester@example.com"
546 assert_difference('ActionMailer::Base.deliveries.size', 1) do
547 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
549 assert_response :success
550 assert_template :account
551 assert_select "div#errorExplanation", false
552 assert_select ".notice", /^User information updated successfully/
553 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
554 email = ActionMailer::Base.deliveries.first
555 assert_equal 1, email.to.count
556 assert_equal user.new_email, email.to.first
557 ActionMailer::Base.deliveries.clear
560 # Check that the user account page will display and contains some relevant
561 # information for the user
562 def test_user_view_account
563 # Test a non-existent user
564 get :view, {:display_name => "unknown"}
565 assert_response :not_found
568 get :view, {:display_name => "test"}
569 assert_response :success
570 assert_select "div#userinformation" do
571 assert_select "a[href^='/user/test/history']", 1
572 assert_select "a[href='/user/test/traces']", 1
573 assert_select "a[href='/user/test/diary']", 1
574 assert_select "a[href='/user/test/diary/comments']", 1
575 assert_select "a[href='/user/test/account']", 0
576 assert_select "a[href='/user/test/blocks']", 0
577 assert_select "a[href='/user/test/blocks_by']", 0
578 assert_select "a[href='/blocks/new/test']", 0
581 # Test a user who has been blocked
582 get :view, {:display_name => "blocked"}
583 assert_response :success
584 assert_select "div#userinformation" do
585 assert_select "a[href^='/user/blocked/history']", 1
586 assert_select "a[href='/user/blocked/traces']", 1
587 assert_select "a[href='/user/blocked/diary']", 1
588 assert_select "a[href='/user/blocked/diary/comments']", 1
589 assert_select "a[href='/user/blocked/account']", 0
590 assert_select "a[href='/user/blocked/blocks']", 1
591 assert_select "a[href='/user/blocked/blocks_by']", 0
592 assert_select "a[href='/blocks/new/blocked']", 0
595 # Test a moderator who has applied blocks
596 get :view, {:display_name => "moderator"}
597 assert_response :success
598 assert_select "div#userinformation" do
599 assert_select "a[href^='/user/moderator/history']", 1
600 assert_select "a[href='/user/moderator/traces']", 1
601 assert_select "a[href='/user/moderator/diary']", 1
602 assert_select "a[href='/user/moderator/diary/comments']", 1
603 assert_select "a[href='/user/moderator/account']", 0
604 assert_select "a[href='/user/moderator/blocks']", 0
605 assert_select "a[href='/user/moderator/blocks_by']", 1
606 assert_select "a[href='/blocks/new/moderator']", 0
609 # Login as a normal user
610 session[:user] = users(:normal_user).id
612 # Test the normal user
613 get :view, {:display_name => "test"}
614 assert_response :success
615 assert_select "div#userinformation" do
616 assert_select "a[href^='/user/test/history']", 1
617 assert_select "a[href='/traces/mine']", 1
618 assert_select "a[href='/user/test/diary']", 1
619 assert_select "a[href='/user/test/diary/comments']", 1
620 assert_select "a[href='/user/test/account']", 1
621 assert_select "a[href='/user/test/blocks']", 0
622 assert_select "a[href='/user/test/blocks_by']", 0
623 assert_select "a[href='/blocks/new/test']", 0
626 # Login as a moderator
627 session[:user] = users(:moderator_user).id
629 # Test the normal user
630 get :view, {:display_name => "test"}
631 assert_response :success
632 assert_select "div#userinformation" do
633 assert_select "a[href^='/user/test/history']", 1
634 assert_select "a[href='/user/test/traces']", 1
635 assert_select "a[href='/user/test/diary']", 1
636 assert_select "a[href='/user/test/diary/comments']", 1
637 assert_select "a[href='/user/test/account']", 0
638 assert_select "a[href='/user/test/blocks']", 0
639 assert_select "a[href='/user/test/blocks_by']", 0
640 assert_select "a[href='/blocks/new/test']", 1
644 def test_user_api_read
645 # check that a visible user is returned properly
646 get :api_read, :id => users(:normal_user).id
647 assert_response :success
649 # check the data that is returned
650 assert_select "description", :count => 1, :text => "test"
651 assert_select "contributor-terms", :count => 1 do
652 assert_select "[agreed='true']"
654 assert_select "img", :count => 1
655 assert_select "roles", :count => 1 do
656 assert_select "role", :count => 0
658 assert_select "changesets", :count => 1 do
659 assert_select "[count='0']"
661 assert_select "traces", :count => 1 do
662 assert_select "[count='0']"
664 assert_select "blocks", :count => 1 do
665 assert_select "received", :count => 1 do
666 assert_select "[count='0'][active='0']"
668 assert_select "issued", :count => 0
671 # check that we aren't revealing private information
672 assert_select "contributor-terms[pd]", false
673 assert_select "home", false
674 assert_select "languages", false
675 assert_select "messages", false
677 # check that a suspended user is not returned
678 get :api_read, :id => users(:suspended_user).id
679 assert_response :gone
681 # check that a deleted user is not returned
682 get :api_read, :id => users(:deleted_user).id
683 assert_response :gone
685 # check that a non-existent user is not returned
686 get :api_read, :id => 0
687 assert_response :not_found
690 def test_user_api_details
691 # check that nothing is returned when not logged in
693 assert_response :unauthorized
695 # check that we get a response when logged in
696 basic_authorization(users(:normal_user).email, "test")
698 assert_response :success
700 # check the data that is returned
701 assert_select "description", :count => 1, :text => "test"
702 assert_select "contributor-terms", :count => 1 do
703 assert_select "[agreed='true'][pd='false']"
705 assert_select "img", :count => 1
706 assert_select "roles", :count => 1 do
707 assert_select "role", :count => 0
709 assert_select "changesets", :count => 1 do
710 assert_select "[count='0']", :count => 1
712 assert_select "traces", :count => 1 do
713 assert_select "[count='0']", :count => 1
715 assert_select "blocks", :count => 1 do
716 assert_select "received", :count => 1 do
717 assert_select "[count='0'][active='0']"
719 assert_select "issued", :count => 0
721 assert_select "home", :count => 1 do
722 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
724 assert_select "languages", :count => 1 do
725 assert_select "lang", :count => 1, :text => "en"
727 assert_select "messages", :count => 1 do
728 assert_select "received", :count => 1 do
729 assert_select "[count='1'][unread='0']"
731 assert_select "sent", :count => 1 do
732 assert_select "[count='1']"
737 def test_user_make_friend
738 # Get users to work with
739 user = users(:normal_user)
740 friend = users(:second_public_user)
742 # Check that the users aren't already friends
743 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
745 # When not logged in a GET should ask us to login
746 get :make_friend, {:display_name => friend.display_name}
747 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
749 # When not logged in a POST should error
750 post :make_friend, {:display_name => friend.display_name}
751 assert_response :forbidden
752 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
754 # When logged in a GET should get a confirmation page
755 get :make_friend, {:display_name => friend.display_name}, {"user" => user}
756 assert_response :success
757 assert_template :make_friend
758 assert_select "form" do
759 assert_select "input[type='hidden'][name='referer']", 0
760 assert_select "input[type='submit']", 1
762 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
764 # The GET should preserve any referer
765 get :make_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
766 assert_response :success
767 assert_template :make_friend
768 assert_select "form" do
769 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
770 assert_select "input[type='submit']", 1
772 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
774 # When logged in a POST should add the friendship
775 assert_difference('ActionMailer::Base.deliveries.size', 1) do
776 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
778 assert_redirected_to user_path(:display_name => friend.display_name)
779 assert_match /is now your friend/, flash[:notice]
780 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
781 email = ActionMailer::Base.deliveries.first
782 assert_equal 1, email.to.count
783 assert_equal friend.email, email.to.first
784 ActionMailer::Base.deliveries.clear
786 # A second POST should report that the friendship already exists
787 assert_no_difference('ActionMailer::Base.deliveries.size') do
788 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
790 assert_redirected_to user_path(:display_name => friend.display_name)
791 assert_match /You are already friends with/, flash[:warning]
792 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
795 def test_user_remove_friend
796 # Get users to work with
797 user = users(:normal_user)
798 friend = users(:public_user)
800 # Check that the users are friends
801 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
803 # When not logged in a GET should ask us to login
804 get :remove_friend, {:display_name => friend.display_name}
805 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
807 # When not logged in a POST should error
808 post :remove_friend, {:display_name => friend.display_name}
809 assert_response :forbidden
810 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
812 # When logged in a GET should get a confirmation page
813 get :remove_friend, {:display_name => friend.display_name}, {"user" => user}
814 assert_response :success
815 assert_template :remove_friend
816 assert_select "form" do
817 assert_select "input[type='hidden'][name='referer']", 0
818 assert_select "input[type='submit']", 1
820 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
822 # The GET should preserve any referer
823 get :remove_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
824 assert_response :success
825 assert_template :remove_friend
826 assert_select "form" do
827 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
828 assert_select "input[type='submit']", 1
830 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
832 # When logged in a POST should remove the friendship
833 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
834 assert_redirected_to user_path(:display_name => friend.display_name)
835 assert_match /was removed from your friends/, flash[:notice]
836 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
838 # A second POST should report that the friendship does not exist
839 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
840 assert_redirected_to user_path(:display_name => friend.display_name)
841 assert_match /is not one of your friends/, flash[:error]
842 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
846 # Try without logging in
847 get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"}
848 assert_response :redirect
849 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
851 # Now try as a normal user
852 get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:normal_user).id}
853 assert_response :redirect
854 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
856 # Finally try as an administrator
857 get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:administrator_user).id}
858 assert_response :redirect
859 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
860 assert_equal "suspended", User.find(users(:normal_user).id).status
864 # Try without logging in
865 get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"}
866 assert_response :redirect
867 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
869 # Now try as a normal user
870 get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:normal_user).id}
871 assert_response :redirect
872 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
874 # Finally try as an administrator
875 get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:administrator_user).id}
876 assert_response :redirect
877 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
879 # Check that the user was deleted properly
880 user = User.find(users(:normal_user).id)
881 assert_equal "user_1", user.display_name
882 assert_equal "", user.description
883 assert_nil user.home_lat
884 assert_nil user.home_lon
885 assert_equal false, user.image.file?
886 assert_equal false, user.email_valid
887 assert_nil user.new_email
888 assert_nil user.openid_url
889 assert_equal "deleted", user.status