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" }
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') do
226 assert_difference('ActionMailer::Base.deliveries.size') 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 post :save, {}, {:new_user => user,
307 :referer => '/edit?editor=id#map=1/2/3'}
309 assert_equal welcome_path(:editor => 'id', :zoom => 1, :lat => 2, :lon => 3),
310 user.tokens.order("id DESC").first.referer
313 def test_user_confirm_expired_token
314 user = users(:inactive_user)
315 token = user.tokens.new
316 token.expiry = 1.day.ago
319 @request.cookies["_osm_session"] = user.display_name
320 post :confirm, :confirm_string => token.token
322 assert_redirected_to :action => 'confirm'
323 assert_match /expired/, flash[:error]
326 def test_user_already_confirmed
327 user = users(:normal_user)
328 token = user.tokens.create
330 @request.cookies["_osm_session"] = user.display_name
331 post :confirm, :confirm_string => token.token
333 assert_redirected_to :action => 'login'
334 assert_match /confirmed/, flash[:error]
337 def test_user_terms_new_user
338 get :terms, {}, { "new_user" => User.new }
339 assert_response :success
340 assert_template :terms
343 def test_user_terms_seen
344 user = users(:normal_user)
346 get :terms, {}, { "user" => user }
347 assert_response :redirect
348 assert_redirected_to :action => :account, :display_name => user.display_name
351 def test_user_go_public
352 post :go_public, {}, { :user => users(:normal_user) }
353 assert_response :redirect
354 assert_redirected_to :action => :account, :display_name => users(:normal_user).display_name
355 assert_equal true, User.find(users(:normal_user).id).data_public
358 def test_user_lost_password
359 # Test fetching the lost password page
361 assert_response :success
362 assert_template :lost_password
363 assert_select "div#notice", false
365 # Test resetting using the address as recorded for a user that has an
366 # address which is duplicated in a different case by another user
367 assert_difference('ActionMailer::Base.deliveries.size', 1) do
368 post :lost_password, :user => { :email => users(:normal_user).email }
370 assert_response :redirect
371 assert_redirected_to :action => :login
372 assert_match /^Sorry you lost it/, flash[:notice]
373 assert_equal users(:normal_user).email, ActionMailer::Base.deliveries.last.to[0]
375 # Test resetting using an address that matches a different user
376 # that has the same address in a different case
377 assert_difference('ActionMailer::Base.deliveries.size', 1) do
378 post :lost_password, :user => { :email => users(:normal_user).email.upcase }
380 assert_response :redirect
381 assert_redirected_to :action => :login
382 assert_match /^Sorry you lost it/, flash[:notice]
383 assert_equal users(:uppercase_user).email, ActionMailer::Base.deliveries.last.to[0]
385 # Test resetting using an address that is a case insensitive match
386 # for more than one user but not an exact match for either
387 assert_difference('ActionMailer::Base.deliveries.size', 0) do
388 post :lost_password, :user => { :email => users(:normal_user).email.titlecase }
390 assert_response :success
391 assert_template :lost_password
392 assert_select ".error", /^Could not find that email address/
394 # Test resetting using the address as recorded for a user that has an
395 # address which is case insensitively unique
396 assert_difference('ActionMailer::Base.deliveries.size', 1) do
397 post :lost_password, :user => { :email => users(:public_user).email }
399 assert_response :redirect
400 assert_redirected_to :action => :login
401 assert_match /^Sorry you lost it/, flash[:notice]
402 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
404 # Test resetting using an address that matches a user that has the
405 # same (case insensitively unique) address in a different case
406 assert_difference('ActionMailer::Base.deliveries.size', 1) do
407 post :lost_password, :user => { :email => users(:public_user).email.upcase }
409 assert_response :redirect
410 assert_redirected_to :action => :login
411 assert_match /^Sorry you lost it/, flash[:notice]
412 assert_equal users(:public_user).email, ActionMailer::Base.deliveries.last.to[0]
415 def test_reset_password
416 # Test a request with no token
418 assert_response :bad_request
420 # Test a request with a bogus token
421 get :reset_password, :token => "made_up_token"
422 assert_response :redirect
423 assert_redirected_to :action => :lost_password
425 # Create a valid token for a user
426 token = User.find(users(:inactive_user).id).tokens.create
428 # Test a request with a valid token
429 get :reset_password, :token => token.token
430 assert_response :success
431 assert_template :reset_password
433 # Test setting a new password
434 post :reset_password, :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" }
435 assert_response :redirect
436 assert_redirected_to :action => :login
437 user = User.find(users(:inactive_user).id)
438 assert_equal "active", user.status
439 assert_equal true, user.email_valid
440 assert_equal user, User.authenticate(:username => "inactive@openstreetmap.org", :password => "new_password")
444 # Get a user to work with - note that this user deliberately
445 # conflicts with uppercase_user in the email and display name
446 # fields to test that we can change other fields without any
447 # validation errors being reported
448 user = users(:normal_user)
450 # Make sure that you are redirected to the login page when
451 # you are not logged in
452 get :account, { :display_name => user.display_name }
453 assert_response :redirect
454 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/test/account"
456 # Make sure that you are blocked when not logged in as the right user
457 get :account, { :display_name => user.display_name }, { "user" => users(:public_user).id }
458 assert_response :forbidden
460 # Make sure we get the page when we are logged in as the right user
461 get :account, { :display_name => user.display_name }, { "user" => user }
462 assert_response :success
463 assert_template :account
465 # Updating the description should work
466 user.description = "new description"
467 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
468 assert_response :success
469 assert_template :account
470 assert_select "div#errorExplanation", false
471 assert_select ".notice", /^User information updated successfully/
472 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
474 # Changing name to one that exists should fail
475 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name)
476 post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id }
477 assert_response :success
478 assert_template :account
479 assert_select ".notice", false
480 assert_select "div#errorExplanation"
481 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
483 # Changing name to one that exists should fail, regardless of case
484 new_attributes = user.attributes.dup.merge(:display_name => users(:public_user).display_name.upcase)
485 post :account, { :display_name => user.display_name, :user => new_attributes }, { "user" => user.id }
486 assert_response :success
487 assert_template :account
488 assert_select ".notice", false
489 assert_select "div#errorExplanation"
490 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
492 # Changing name to one that doesn't exist should work
493 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
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 "div#errorExplanation", false
498 assert_select ".notice", /^User information updated successfully/
499 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
501 # Record the change of name
502 user.display_name = "new tester"
504 # Changing email to one that exists should fail
505 user.new_email = users(:public_user).email
506 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
507 assert_response :success
508 assert_template :account
509 assert_select ".notice", false
510 assert_select "div#errorExplanation"
511 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
513 # Changing email to one that exists should fail, regardless of case
514 user.new_email = users(:public_user).email.upcase
515 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
516 assert_response :success
517 assert_template :account
518 assert_select ".notice", false
519 assert_select "div#errorExplanation"
520 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
522 # Changing email to one that doesn't exist should work
523 user.new_email = "new_tester@example.com"
524 post :account, { :display_name => user.display_name, :user => user.attributes }, { "user" => user.id }
525 assert_response :success
526 assert_template :account
527 assert_select "div#errorExplanation", false
528 assert_select ".notice", /^User information updated successfully/
529 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
532 # Check that the user account page will display and contains some relevant
533 # information for the user
534 def test_user_view_account
535 # Test a non-existent user
536 get :view, {:display_name => "unknown"}
537 assert_response :not_found
540 get :view, {:display_name => "test"}
541 assert_response :success
542 assert_select "div#userinformation" do
543 assert_select "a[href^=/user/test/history]", 1
544 assert_select "a[href=/user/test/traces]", 1
545 assert_select "a[href=/user/test/diary]", 1
546 assert_select "a[href=/user/test/diary/comments]", 1
547 assert_select "a[href=/user/test/account]", 0
548 assert_select "a[href=/user/test/blocks]", 0
549 assert_select "a[href=/user/test/blocks_by]", 0
550 assert_select "a[href=/blocks/new/test]", 0
553 # Test a user who has been blocked
554 get :view, {:display_name => "blocked"}
555 assert_response :success
556 assert_select "div#userinformation" do
557 assert_select "a[href^=/user/blocked/history]", 1
558 assert_select "a[href=/user/blocked/traces]", 1
559 assert_select "a[href=/user/blocked/diary]", 1
560 assert_select "a[href=/user/blocked/diary/comments]", 1
561 assert_select "a[href=/user/blocked/account]", 0
562 assert_select "a[href=/user/blocked/blocks]", 1
563 assert_select "a[href=/user/blocked/blocks_by]", 0
564 assert_select "a[href=/blocks/new/blocked]", 0
567 # Test a moderator who has applied blocks
568 get :view, {:display_name => "moderator"}
569 assert_response :success
570 assert_select "div#userinformation" do
571 assert_select "a[href^=/user/moderator/history]", 1
572 assert_select "a[href=/user/moderator/traces]", 1
573 assert_select "a[href=/user/moderator/diary]", 1
574 assert_select "a[href=/user/moderator/diary/comments]", 1
575 assert_select "a[href=/user/moderator/account]", 0
576 assert_select "a[href=/user/moderator/blocks]", 0
577 assert_select "a[href=/user/moderator/blocks_by]", 1
578 assert_select "a[href=/blocks/new/moderator]", 0
581 # Login as a normal user
582 session[:user] = users(:normal_user).id
584 # Test the normal user
585 get :view, {:display_name => "test"}
586 assert_response :success
587 assert_select "div#userinformation" do
588 assert_select "a[href^=/user/test/history]", 1
589 assert_select "a[href=/traces/mine]", 1
590 assert_select "a[href=/user/test/diary]", 1
591 assert_select "a[href=/user/test/diary/comments]", 1
592 assert_select "a[href=/user/test/account]", 1
593 assert_select "a[href=/user/test/blocks]", 0
594 assert_select "a[href=/user/test/blocks_by]", 0
595 assert_select "a[href=/blocks/new/test]", 0
598 # Login as a moderator
599 session[:user] = users(:moderator_user).id
601 # Test the normal user
602 get :view, {:display_name => "test"}
603 assert_response :success
604 assert_select "div#userinformation" do
605 assert_select "a[href^=/user/test/history]", 1
606 assert_select "a[href=/user/test/traces]", 1
607 assert_select "a[href=/user/test/diary]", 1
608 assert_select "a[href=/user/test/diary/comments]", 1
609 assert_select "a[href=/user/test/account]", 0
610 assert_select "a[href=/user/test/blocks]", 0
611 assert_select "a[href=/user/test/blocks_by]", 0
612 assert_select "a[href=/blocks/new/test]", 1
616 def test_user_api_read
617 # check that a visible user is returned properly
618 get :api_read, :id => users(:normal_user).id
619 assert_response :success
621 # check the data that is returned
622 assert_select "description", :count => 1, :text => "test"
623 assert_select "contributor-terms", :count => 1 do
624 assert_select "[agreed=true]"
626 assert_select "img", :count => 1
627 assert_select "roles", :count => 1 do
628 assert_select "role", :count => 0
630 assert_select "changesets", :count => 1 do
631 assert_select "[count=0]"
633 assert_select "traces", :count => 1 do
634 assert_select "[count=0]"
636 assert_select "blocks", :count => 1 do
637 assert_select "received", :count => 1 do
638 assert_select "[count=0][active=0]"
640 assert_select "issued", :count => 0
643 # check that we aren't revealing private information
644 assert_select "contributor-terms[pd]", false
645 assert_select "home", false
646 assert_select "languages", false
647 assert_select "messages", false
649 # check that a suspended user is not returned
650 get :api_read, :id => users(:suspended_user).id
651 assert_response :gone
653 # check that a deleted user is not returned
654 get :api_read, :id => users(:deleted_user).id
655 assert_response :gone
657 # check that a non-existent user is not returned
658 get :api_read, :id => 0
659 assert_response :not_found
662 def test_user_api_details
663 # check that nothing is returned when not logged in
665 assert_response :unauthorized
667 # check that we get a response when logged in
668 basic_authorization(users(:normal_user).email, "test")
670 assert_response :success
672 # check the data that is returned
673 assert_select "description", :count => 1, :text => "test"
674 assert_select "contributor-terms", :count => 1 do
675 assert_select "[agreed=true][pd=false]"
677 assert_select "img", :count => 1
678 assert_select "roles", :count => 1 do
679 assert_select "role", :count => 0
681 assert_select "changesets", :count => 1 do
682 assert_select "[count=0]", :count => 1
684 assert_select "traces", :count => 1 do
685 assert_select "[count=0]", :count => 1
687 assert_select "blocks", :count => 1 do
688 assert_select "received", :count => 1 do
689 assert_select "[count=0][active=0]"
691 assert_select "issued", :count => 0
693 assert_select "home", :count => 1 do
694 assert_select "[lat=12.1][lon=12.1][zoom=3]"
696 assert_select "languages", :count => 1 do
697 assert_select "lang", :count => 1, :text => "en"
699 assert_select "messages", :count => 1 do
700 assert_select "received", :count => 1 do
701 assert_select "[count=1][unread=0]"
703 assert_select "sent", :count => 1 do
704 assert_select "[count=1]"
709 def test_user_make_friend
710 # Get users to work with
711 user = users(:normal_user)
712 friend = users(:second_public_user)
714 # Check that the users aren't already friends
715 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
717 # When not logged in a GET should ask us to login
718 get :make_friend, {:display_name => friend.display_name}
719 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
721 # When not logged in a POST should error
722 post :make_friend, {:display_name => friend.display_name}
723 assert_response :forbidden
724 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
726 # When logged in a GET should get a confirmation page
727 get :make_friend, {:display_name => friend.display_name}, {"user" => user}
728 assert_response :success
729 assert_template :make_friend
730 assert_select "form" do
731 assert_select "input[type=hidden][name=referer]", 0
732 assert_select "input[type=submit]", 1
734 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
736 # The GET should preserve any referer
737 get :make_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
738 assert_response :success
739 assert_template :make_friend
740 assert_select "form" do
741 assert_select "input[type=hidden][name=referer][value=/test]", 1
742 assert_select "input[type=submit]", 1
744 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
746 # When logged in a POST should add the friendship
747 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
748 assert_redirected_to user_path(:display_name => friend.display_name)
749 assert_match /is now your friend/, flash[:notice]
750 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
752 # A second POST should report that the friendship already exists
753 post :make_friend, {:display_name => friend.display_name}, {"user" => user}
754 assert_redirected_to user_path(:display_name => friend.display_name)
755 assert_match /You are already friends with/, flash[:warning]
756 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
759 def test_user_remove_friend
760 # Get users to work with
761 user = users(:normal_user)
762 friend = users(:public_user)
764 # Check that the users are friends
765 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
767 # When not logged in a GET should ask us to login
768 get :remove_friend, {:display_name => friend.display_name}
769 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
771 # When not logged in a POST should error
772 post :remove_friend, {:display_name => friend.display_name}
773 assert_response :forbidden
774 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
776 # When logged in a GET should get a confirmation page
777 get :remove_friend, {:display_name => friend.display_name}, {"user" => user}
778 assert_response :success
779 assert_template :remove_friend
780 assert_select "form" do
781 assert_select "input[type=hidden][name=referer]", 0
782 assert_select "input[type=submit]", 1
784 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
786 # The GET should preserve any referer
787 get :remove_friend, {:display_name => friend.display_name, :referer => "/test"}, {"user" => user}
788 assert_response :success
789 assert_template :remove_friend
790 assert_select "form" do
791 assert_select "input[type=hidden][name=referer][value=/test]", 1
792 assert_select "input[type=submit]", 1
794 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
796 # When logged in a POST should remove the friendship
797 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
798 assert_redirected_to user_path(:display_name => friend.display_name)
799 assert_match /was removed from your friends/, flash[:notice]
800 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
802 # A second POST should report that the friendship does not exist
803 post :remove_friend, {:display_name => friend.display_name}, {"user" => user}
804 assert_redirected_to user_path(:display_name => friend.display_name)
805 assert_match /is not one of your friends/, flash[:error]
806 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
810 # Try without logging in
811 get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"}
812 assert_response :redirect
813 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
815 # Now try as a normal user
816 get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:normal_user).id}
817 assert_response :redirect
818 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
820 # Finally try as an administrator
821 get :set_status, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:administrator_user).id}
822 assert_response :redirect
823 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
824 assert_equal "suspended", User.find(users(:normal_user).id).status
828 # Try without logging in
829 get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"}
830 assert_response :redirect
831 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
833 # Now try as a normal user
834 get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:normal_user).id}
835 assert_response :redirect
836 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
838 # Finally try as an administrator
839 get :delete, {:display_name => users(:normal_user).display_name, :status => "suspended"}, {:user => users(:administrator_user).id}
840 assert_response :redirect
841 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
843 # Check that the user was deleted properly
844 user = User.find(users(:normal_user).id)
845 assert_equal "user_1", user.display_name
846 assert_equal "", user.description
847 assert_nil user.home_lat
848 assert_nil user.home_lon
849 assert_equal false, user.image.file?
850 assert_equal false, user.email_valid
851 assert_nil user.new_email
852 assert_nil user.openid_url
853 assert_equal "deleted", user.status