3 class UsersControllerTest < ActionController::TestCase
9 # test all routes which lead to this controller
12 { :path => "/api/0.6/user/1", :method => :get },
13 { :controller => "users", :action => "api_read", :id => "1" }
16 { :path => "/api/0.6/user/details", :method => :get },
17 { :controller => "users", :action => "api_details" }
20 { :path => "/api/0.6/user/gpx_files", :method => :get },
21 { :controller => "users", :action => "api_gpx_files" }
24 { :path => "/api/0.6/users", :method => :get },
25 { :controller => "users", :action => "api_users" }
29 { :path => "/login", :method => :get },
30 { :controller => "users", :action => "login" }
33 { :path => "/login", :method => :post },
34 { :controller => "users", :action => "login" }
37 { :controller => "users", :action => "login", :format => "html" },
38 { :path => "/login.html", :method => :get }
42 { :path => "/logout", :method => :get },
43 { :controller => "users", :action => "logout" }
46 { :path => "/logout", :method => :post },
47 { :controller => "users", :action => "logout" }
50 { :controller => "users", :action => "logout", :format => "html" },
51 { :path => "/logout.html", :method => :get }
55 { :path => "/user/new", :method => :get },
56 { :controller => "users", :action => "new" }
60 { :path => "/user/new", :method => :post },
61 { :controller => "users", :action => "create" }
65 { :path => "/user/terms", :method => :get },
66 { :controller => "users", :action => "terms" }
70 { :path => "/user/save", :method => :post },
71 { :controller => "users", :action => "save" }
75 { :path => "/user/username/confirm", :method => :get },
76 { :controller => "users", :action => "confirm", :display_name => "username" }
79 { :path => "/user/username/confirm", :method => :post },
80 { :controller => "users", :action => "confirm", :display_name => "username" }
83 { :path => "/user/username/confirm/resend", :method => :get },
84 { :controller => "users", :action => "confirm_resend", :display_name => "username" }
88 { :path => "/user/confirm", :method => :get },
89 { :controller => "users", :action => "confirm" }
92 { :path => "/user/confirm", :method => :post },
93 { :controller => "users", :action => "confirm" }
96 { :path => "/user/confirm-email", :method => :get },
97 { :controller => "users", :action => "confirm_email" }
100 { :path => "/user/confirm-email", :method => :post },
101 { :controller => "users", :action => "confirm_email" }
105 { :path => "/user/go_public", :method => :post },
106 { :controller => "users", :action => "go_public" }
110 { :path => "/user/forgot-password", :method => :get },
111 { :controller => "users", :action => "lost_password" }
114 { :path => "/user/forgot-password", :method => :post },
115 { :controller => "users", :action => "lost_password" }
118 { :path => "/user/reset-password", :method => :get },
119 { :controller => "users", :action => "reset_password" }
122 { :path => "/user/reset-password", :method => :post },
123 { :controller => "users", :action => "reset_password" }
127 { :path => "/user/suspended", :method => :get },
128 { :controller => "users", :action => "suspended" }
132 { :path => "/user/username", :method => :get },
133 { :controller => "users", :action => "show", :display_name => "username" }
137 { :path => "/user/username/account", :method => :get },
138 { :controller => "users", :action => "account", :display_name => "username" }
141 { :path => "/user/username/account", :method => :post },
142 { :controller => "users", :action => "account", :display_name => "username" }
146 { :path => "/user/username/make_friend", :method => :get },
147 { :controller => "users", :action => "make_friend", :display_name => "username" }
150 { :path => "/user/username/make_friend", :method => :post },
151 { :controller => "users", :action => "make_friend", :display_name => "username" }
154 { :path => "/user/username/remove_friend", :method => :get },
155 { :controller => "users", :action => "remove_friend", :display_name => "username" }
158 { :path => "/user/username/remove_friend", :method => :post },
159 { :controller => "users", :action => "remove_friend", :display_name => "username" }
163 { :path => "/user/username/set_status", :method => :get },
164 { :controller => "users", :action => "set_status", :display_name => "username" }
167 { :path => "/user/username/delete", :method => :get },
168 { :controller => "users", :action => "delete", :display_name => "username" }
172 { :path => "/users", :method => :get },
173 { :controller => "users", :action => "index" }
176 { :path => "/users", :method => :post },
177 { :controller => "users", :action => "index" }
180 { :path => "/users/status", :method => :get },
181 { :controller => "users", :action => "index", :status => "status" }
184 { :path => "/users/status", :method => :post },
185 { :controller => "users", :action => "index", :status => "status" }
189 # The user creation page loads
192 assert_response :redirect
193 assert_redirected_to user_new_path(:cookie_test => "true")
195 get :new, :params => { :cookie_test => "true" }, :session => { :cookie_test => true }
196 assert_response :success
198 assert_select "html", :count => 1 do
199 assert_select "head", :count => 1 do
200 assert_select "title", :text => /Sign Up/, :count => 1
202 assert_select "body", :count => 1 do
203 assert_select "div#content", :count => 1 do
204 assert_select "form[action='/user/new'][method='post']", :count => 1 do
205 assert_select "input[id='user_email']", :count => 1
206 assert_select "input[id='user_email_confirmation']", :count => 1
207 assert_select "input[id='user_display_name']", :count => 1
208 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
209 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
210 assert_select "input[type='submit'][value='Sign Up']", :count => 1
217 def test_new_view_logged_in
218 session[:user] = create(:user).id
221 assert_response :redirect
222 assert_redirected_to user_new_path(:cookie_test => "true")
223 get :new, :params => { :cookie_test => "true" }
224 assert_response :redirect
225 assert_redirected_to root_path
227 get :new, :params => { :referer => "/test" }
228 assert_response :redirect
229 assert_redirected_to user_new_path(:referer => "/test", :cookie_test => "true")
230 get :new, :params => { :referer => "/test", :cookie_test => "true" }
231 assert_response :redirect
232 assert_redirected_to "/test"
236 user = build(:user, :pending)
238 assert_difference "User.count", 1 do
239 assert_difference "ActionMailer::Base.deliveries.size", 1 do
240 post :save, :session => { :new_user => user }
245 register_email = ActionMailer::Base.deliveries.first
247 assert_equal register_email.to[0], user.email
248 assert_match(/#{@url}/, register_email.body.to_s)
251 assert_redirected_to :action => "confirm", :display_name => user.display_name
253 ActionMailer::Base.deliveries.clear
256 def test_new_duplicate_email
257 user = build(:user, :pending)
258 user.email = create(:user).email
260 assert_no_difference "User.count" do
261 assert_no_difference "ActionMailer::Base.deliveries.size" do
262 post :save, :session => { :new_user => user }
266 assert_response :success
267 assert_template "new"
268 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
271 def test_new_duplicate_email_uppercase
272 user = build(:user, :pending)
273 user.email = create(:user).email.upcase
275 assert_no_difference "User.count" do
276 assert_no_difference "ActionMailer::Base.deliveries.size" do
277 post :save, :session => { :new_user => user }
281 assert_response :success
282 assert_template "new"
283 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
286 def test_new_duplicate_name
287 user = build(:user, :pending)
288 user.display_name = create(:user).display_name
290 assert_no_difference "User.count" do
291 assert_no_difference "ActionMailer::Base.deliveries.size" do
292 post :save, :session => { :new_user => user }
296 assert_response :success
297 assert_template "new"
298 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
301 def test_new_duplicate_name_uppercase
302 user = build(:user, :pending)
303 user.display_name = create(:user).display_name.upcase
305 assert_no_difference "User.count" do
306 assert_no_difference "ActionMailer::Base.deliveries.size" do
307 post :save, :session => { :new_user => user }
311 assert_response :success
312 assert_template "new"
313 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
316 def test_save_referer_params
317 user = build(:user, :pending)
319 assert_difference "User.count", 1 do
320 assert_difference "ActionMailer::Base.deliveries.size", 1 do
321 post :save, :session => { :new_user => user,
322 :referer => "/edit?editor=id#map=1/2/3" }
326 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
327 user.tokens.order("id DESC").first.referer
329 ActionMailer::Base.deliveries.clear
332 def test_logout_without_referer
334 assert_response :success
335 assert_template :logout
336 assert_select "input[name=referer][value=?]", ""
338 session_id = assert_select("input[name=session]").first["value"]
340 get :logout, :params => { :session => session_id }
341 assert_response :redirect
342 assert_redirected_to root_path
345 def test_logout_with_referer
346 get :logout, :params => { :referer => "/test" }
347 assert_response :success
348 assert_template :logout
349 assert_select "input[name=referer][value=?]", "/test"
351 session_id = assert_select("input[name=session]").first["value"]
353 get :logout, :params => { :session => session_id, :referer => "/test" }
354 assert_response :redirect
355 assert_redirected_to "/test"
358 def test_logout_with_token
359 token = create(:user).tokens.create
361 session[:token] = token.token
364 assert_response :success
365 assert_template :logout
366 assert_select "input[name=referer][value=?]", ""
367 assert_equal token.token, session[:token]
368 assert_not_nil UserToken.where(:id => token.id).first
370 session_id = assert_select("input[name=session]").first["value"]
372 get :logout, :params => { :session => session_id }
373 assert_response :redirect
374 assert_redirected_to root_path
375 assert_nil session[:token]
376 assert_nil UserToken.where(:id => token.id).first
380 user = create(:user, :pending)
381 confirm_string = user.tokens.create.token
383 @request.cookies["_osm_session"] = user.display_name
384 get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
385 assert_response :success
386 assert_template :confirm
389 def test_confirm_get_already_confirmed
391 confirm_string = user.tokens.create.token
393 @request.cookies["_osm_session"] = user.display_name
394 get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
395 assert_response :redirect
396 assert_redirected_to root_path
399 def test_confirm_success_no_token_no_referer
400 user = create(:user, :pending)
401 stub_gravatar_request(user.email)
402 confirm_string = user.tokens.create.token
404 @request.cookies["_osm_session"] = user.display_name
405 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
406 assert_redirected_to login_path
407 assert_match(/Confirmed your account/, flash[:notice])
410 def test_confirm_success_good_token_no_referer
411 user = create(:user, :pending)
412 stub_gravatar_request(user.email)
413 confirm_string = user.tokens.create.token
414 token = user.tokens.create.token
416 @request.cookies["_osm_session"] = user.display_name
417 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
418 assert_redirected_to welcome_path
421 def test_confirm_success_bad_token_no_referer
422 user = create(:user, :pending)
423 stub_gravatar_request(user.email)
424 confirm_string = user.tokens.create.token
425 token = create(:user).tokens.create.token
427 @request.cookies["_osm_session"] = user.display_name
428 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
429 assert_redirected_to login_path
430 assert_match(/Confirmed your account/, flash[:notice])
433 def test_confirm_success_no_token_with_referer
434 user = create(:user, :pending)
435 stub_gravatar_request(user.email)
436 confirm_string = user.tokens.create(:referer => diary_new_path).token
438 @request.cookies["_osm_session"] = user.display_name
439 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
440 assert_redirected_to login_path(:referer => diary_new_path)
441 assert_match(/Confirmed your account/, flash[:notice])
444 def test_confirm_success_good_token_with_referer
445 user = create(:user, :pending)
446 stub_gravatar_request(user.email)
447 confirm_string = user.tokens.create(:referer => diary_new_path).token
448 token = user.tokens.create.token
450 @request.cookies["_osm_session"] = user.display_name
451 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
452 assert_redirected_to diary_new_path
455 def test_confirm_success_bad_token_with_referer
456 user = create(:user, :pending)
457 stub_gravatar_request(user.email)
458 confirm_string = user.tokens.create(:referer => diary_new_path).token
459 token = create(:user).tokens.create.token
461 @request.cookies["_osm_session"] = user.display_name
462 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
463 assert_redirected_to login_path(:referer => diary_new_path)
464 assert_match(/Confirmed your account/, flash[:notice])
467 def test_confirm_expired_token
468 user = create(:user, :pending)
469 confirm_string = user.tokens.create(:expiry => 1.day.ago).token
471 @request.cookies["_osm_session"] = user.display_name
472 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
473 assert_redirected_to :action => "confirm"
474 assert_match(/confirmation code has expired/, flash[:error])
477 def test_confirm_already_confirmed
479 confirm_string = user.tokens.create(:referer => diary_new_path).token
481 @request.cookies["_osm_session"] = user.display_name
482 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
483 assert_redirected_to :action => "login"
484 assert_match(/already been confirmed/, flash[:error])
487 def test_confirm_resend_success
488 user = create(:user, :pending)
489 session[:token] = user.tokens.create.token
491 assert_difference "ActionMailer::Base.deliveries.size", 1 do
492 get :confirm_resend, :params => { :display_name => user.display_name }
495 assert_response :redirect
496 assert_redirected_to login_path
497 assert_match(/sent a new confirmation/, flash[:notice])
499 email = ActionMailer::Base.deliveries.last
501 assert_equal user.email, email.to.first
503 ActionMailer::Base.deliveries.clear
506 def test_confirm_resend_no_token
507 user = create(:user, :pending)
508 assert_no_difference "ActionMailer::Base.deliveries.size" do
509 get :confirm_resend, :params => { :display_name => user.display_name }
512 assert_response :redirect
513 assert_redirected_to login_path
514 assert_match "User #{user.display_name} not found.", flash[:error]
517 def test_confirm_resend_unknown_user
518 assert_no_difference "ActionMailer::Base.deliveries.size" do
519 get :confirm_resend, :params => { :display_name => "No Such User" }
522 assert_response :redirect
523 assert_redirected_to login_path
524 assert_match "User No Such User not found.", flash[:error]
527 def test_confirm_email_get
529 confirm_string = user.tokens.create.token
531 get :confirm_email, :params => { :confirm_string => confirm_string }
532 assert_response :success
533 assert_template :confirm_email
536 def test_confirm_email_success
537 user = create(:user, :new_email => "test-new@example.com")
538 stub_gravatar_request(user.new_email)
539 confirm_string = user.tokens.create.token
541 post :confirm_email, :params => { :confirm_string => confirm_string }
542 assert_response :redirect
543 assert_redirected_to :action => :account, :display_name => user.display_name
544 assert_match(/Confirmed your change of email address/, flash[:notice])
547 def test_confirm_email_already_confirmed
549 confirm_string = user.tokens.create.token
551 post :confirm_email, :params => { :confirm_string => confirm_string }
552 assert_response :redirect
553 assert_redirected_to :action => :account, :display_name => user.display_name
554 assert_match(/already been confirmed/, flash[:error])
557 def test_confirm_email_bad_token
558 post :confirm_email, :params => { :confirm_string => "XXXXX" }
559 assert_response :success
560 assert_template :confirm_email
561 assert_match(/confirmation code has expired or does not exist/, flash[:error])
565 # test if testing for a gravatar works
566 # this happens when the email is actually changed
567 # which is triggered by the confirmation mail
568 def test_gravatar_auto_enable
569 # switch to email that has a gravatar
570 user = create(:user, :new_email => "test-new@example.com")
571 stub_gravatar_request(user.new_email, 200)
572 confirm_string = user.tokens.create.token
573 # precondition gravatar should be turned off
574 assert_not user.image_use_gravatar
575 post :confirm_email, :params => { :confirm_string => confirm_string }
576 assert_response :redirect
577 assert_redirected_to :action => :account, :display_name => user.display_name
578 assert_match(/Confirmed your change of email address/, flash[:notice])
579 # gravatar use should now be enabled
580 assert User.find(user.id).image_use_gravatar
583 def test_gravatar_auto_disable
584 # switch to email without a gravatar
585 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
586 stub_gravatar_request(user.new_email, 404)
587 confirm_string = user.tokens.create.token
588 # precondition gravatar should be turned on
589 assert user.image_use_gravatar
590 post :confirm_email, :params => { :confirm_string => confirm_string }
591 assert_response :redirect
592 assert_redirected_to :action => :account, :display_name => user.display_name
593 assert_match(/Confirmed your change of email address/, flash[:notice])
594 # gravatar use should now be disabled
595 assert_not User.find(user.id).image_use_gravatar
598 def test_terms_new_user
599 get :terms, :session => { :new_user => User.new }
600 assert_response :success
601 assert_template :terms
604 def test_terms_agreed
605 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
607 session[:user] = user.id
610 assert_response :redirect
611 assert_redirected_to :action => :account, :display_name => user.display_name
614 def test_terms_not_seen_without_referer
615 user = create(:user, :terms_seen => false)
617 session[:user] = user.id
620 assert_response :success
621 assert_template :terms
623 post :save, :params => { :user => { :consider_pd => true } }
624 assert_response :redirect
625 assert_redirected_to :action => :account, :display_name => user.display_name
626 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
630 assert_equal true, user.consider_pd
631 assert_not_nil user.terms_agreed
632 assert_equal true, user.terms_seen
635 def test_terms_not_seen_with_referer
636 user = create(:user, :terms_seen => false)
638 session[:user] = user.id
640 get :terms, :params => { :referer => "/test" }
641 assert_response :success
642 assert_template :terms
644 post :save, :params => { :user => { :consider_pd => true }, :referer => "/test" }
645 assert_response :redirect
646 assert_redirected_to "/test"
647 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
651 assert_equal true, user.consider_pd
652 assert_not_nil user.terms_agreed
653 assert_equal true, user.terms_seen
657 user = create(:user, :data_public => false)
658 post :go_public, :session => { :user => user }
659 assert_response :redirect
660 assert_redirected_to :action => :account, :display_name => user.display_name
661 assert_equal true, User.find(user.id).data_public
664 def test_lost_password
665 # Test fetching the lost password page
667 assert_response :success
668 assert_template :lost_password
669 assert_select "div#notice", false
671 # Test resetting using the address as recorded for a user that has an
672 # address which is duplicated in a different case by another user
674 uppercase_user = build(:user, :email => user.email.upcase).tap { |u| u.save(:validate => false) }
676 assert_difference "ActionMailer::Base.deliveries.size", 1 do
677 post :lost_password, :params => { :user => { :email => user.email } }
679 assert_response :redirect
680 assert_redirected_to :action => :login
681 assert_match(/^Sorry you lost it/, flash[:notice])
682 email = ActionMailer::Base.deliveries.first
683 assert_equal 1, email.to.count
684 assert_equal user.email, email.to.first
685 ActionMailer::Base.deliveries.clear
687 # Test resetting using an address that matches a different user
688 # that has the same address in a different case
689 assert_difference "ActionMailer::Base.deliveries.size", 1 do
690 post :lost_password, :params => { :user => { :email => user.email.upcase } }
692 assert_response :redirect
693 assert_redirected_to :action => :login
694 assert_match(/^Sorry you lost it/, flash[:notice])
695 email = ActionMailer::Base.deliveries.first
696 assert_equal 1, email.to.count
697 assert_equal uppercase_user.email, email.to.first
698 ActionMailer::Base.deliveries.clear
700 # Test resetting using an address that is a case insensitive match
701 # for more than one user but not an exact match for either
702 assert_no_difference "ActionMailer::Base.deliveries.size" do
703 post :lost_password, :params => { :user => { :email => user.email.titlecase } }
705 assert_response :success
706 assert_template :lost_password
707 assert_select ".error", /^Could not find that email address/
709 # Test resetting using the address as recorded for a user that has an
710 # address which is case insensitively unique
711 third_user = create(:user)
712 assert_difference "ActionMailer::Base.deliveries.size", 1 do
713 post :lost_password, :params => { :user => { :email => third_user.email } }
715 assert_response :redirect
716 assert_redirected_to :action => :login
717 assert_match(/^Sorry you lost it/, flash[:notice])
718 email = ActionMailer::Base.deliveries.first
719 assert_equal 1, email.to.count
720 assert_equal third_user.email, email.to.first
721 ActionMailer::Base.deliveries.clear
723 # Test resetting using an address that matches a user that has the
724 # same (case insensitively unique) address in a different case
725 assert_difference "ActionMailer::Base.deliveries.size", 1 do
726 post :lost_password, :params => { :user => { :email => third_user.email.upcase } }
728 assert_response :redirect
729 assert_redirected_to :action => :login
730 assert_match(/^Sorry you lost it/, flash[:notice])
731 email = ActionMailer::Base.deliveries.first
732 assert_equal 1, email.to.count
733 assert_equal third_user.email, email.to.first
734 ActionMailer::Base.deliveries.clear
737 def test_reset_password
738 user = create(:user, :pending)
739 # Test a request with no token
741 assert_response :bad_request
743 # Test a request with a bogus token
744 get :reset_password, :params => { :token => "made_up_token" }
745 assert_response :redirect
746 assert_redirected_to :action => :lost_password
748 # Create a valid token for a user
749 token = user.tokens.create
751 # Test a request with a valid token
752 get :reset_password, :params => { :token => token.token }
753 assert_response :success
754 assert_template :reset_password
756 # Test that errors are reported for erroneous submissions
757 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "different_password" } }
758 assert_response :success
759 assert_template :reset_password
760 assert_select "div#errorExplanation"
762 # Test setting a new password
763 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" } }
764 assert_response :redirect
765 assert_redirected_to root_path
766 assert_equal user.id, session[:user]
768 assert_equal "active", user.status
769 assert_equal true, user.email_valid
770 assert_equal user, User.authenticate(:username => user.email, :password => "new_password")
774 # Get a user to work with - note that this user deliberately
775 # conflicts with uppercase_user in the email and display name
776 # fields to test that we can change other fields without any
777 # validation errors being reported
778 user = create(:user, :languages => [])
779 _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
781 # Make sure that you are redirected to the login page when
782 # you are not logged in
783 get :account, :params => { :display_name => user.display_name }
784 assert_response :redirect
785 assert_redirected_to :action => "login", :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
787 # Make sure that you are blocked when not logged in as the right user
788 get :account, :params => { :display_name => user.display_name }, :session => { :user => create(:user) }
789 assert_response :forbidden
791 # Make sure we get the page when we are logged in as the right user
792 get :account, :params => { :display_name => user.display_name }, :session => { :user => user }
793 assert_response :success
794 assert_template :account
795 assert_select "form#accountForm" do |form|
796 assert_equal "post", form.attr("method").to_s
797 assert_select "input[name='_method']", false
798 assert_equal "/user/#{ERB::Util.u(user.display_name)}/account", form.attr("action").to_s
801 # Updating the description should work
802 user.description = "new description"
803 user.preferred_editor = "default"
804 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
805 assert_response :success
806 assert_template :account
807 assert_select "div#errorExplanation", false
808 assert_select ".notice", /^User information updated successfully/
809 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
811 # Changing to a invalid editor should fail
812 user.preferred_editor = "unknown"
813 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
814 assert_response :success
815 assert_template :account
816 assert_select ".notice", false
817 assert_select "div#errorExplanation"
818 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
820 # Changing to a valid editor should work
821 user.preferred_editor = "potlatch2"
822 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
823 assert_response :success
824 assert_template :account
825 assert_select "div#errorExplanation", false
826 assert_select ".notice", /^User information updated successfully/
827 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
829 # Changing to the default editor should work
830 user.preferred_editor = "default"
831 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
832 assert_response :success
833 assert_template :account
834 assert_select "div#errorExplanation", false
835 assert_select ".notice", /^User information updated successfully/
836 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
838 # Changing to an uploaded image should work
839 image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif")
840 post :account, :params => { :display_name => user.display_name, :image_action => "new", :user => user.attributes.merge(:image => image) }, :session => { :user => user }
841 assert_response :success
842 assert_template :account
843 assert_select "div#errorExplanation", false
844 assert_select ".notice", /^User information updated successfully/
845 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "keep"
847 # Changing to a gravatar image should work
848 post :account, :params => { :display_name => user.display_name, :image_action => "gravatar", :user => user.attributes }, :session => { :user => user }
849 assert_response :success
850 assert_template :account
851 assert_select "div#errorExplanation", false
852 assert_select ".notice", /^User information updated successfully/
853 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "gravatar"
855 # Removing the image should work
856 post :account, :params => { :display_name => user.display_name, :image_action => "delete", :user => user.attributes }, :session => { :user => user }
857 assert_response :success
858 assert_template :account
859 assert_select "div#errorExplanation", false
860 assert_select ".notice", /^User information updated successfully/
861 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked]", false
863 # Adding external authentication should redirect to the auth provider
864 post :account, :params => { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, :session => { :user => user }
865 assert_response :redirect
866 assert_redirected_to auth_path(:provider => "openid", :openid_url => "https://www.google.com/accounts/o8/id", :origin => "/user/#{ERB::Util.u(user.display_name)}/account")
868 # Changing name to one that exists should fail
869 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
870 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
871 assert_response :success
872 assert_template :account
873 assert_select ".notice", false
874 assert_select "div#errorExplanation"
875 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
877 # Changing name to one that exists should fail, regardless of case
878 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
879 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
880 assert_response :success
881 assert_template :account
882 assert_select ".notice", false
883 assert_select "div#errorExplanation"
884 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
886 # Changing name to one that doesn't exist should work
887 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
888 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
889 assert_response :success
890 assert_template :account
891 assert_select "div#errorExplanation", false
892 assert_select ".notice", /^User information updated successfully/
893 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
895 # Record the change of name
896 user.display_name = "new tester"
898 # Changing email to one that exists should fail
899 user.new_email = create(:user).email
900 assert_no_difference "ActionMailer::Base.deliveries.size" do
901 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
903 assert_response :success
904 assert_template :account
905 assert_select ".notice", false
906 assert_select "div#errorExplanation"
907 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
909 # Changing email to one that exists should fail, regardless of case
910 user.new_email = create(:user).email.upcase
911 assert_no_difference "ActionMailer::Base.deliveries.size" do
912 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
914 assert_response :success
915 assert_template :account
916 assert_select ".notice", false
917 assert_select "div#errorExplanation"
918 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
920 # Changing email to one that doesn't exist should work
921 user.new_email = "new_tester@example.com"
922 assert_difference "ActionMailer::Base.deliveries.size", 1 do
923 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
925 assert_response :success
926 assert_template :account
927 assert_select "div#errorExplanation", false
928 assert_select ".notice", /^User information updated successfully/
929 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
930 email = ActionMailer::Base.deliveries.first
931 assert_equal 1, email.to.count
932 assert_equal user.new_email, email.to.first
933 ActionMailer::Base.deliveries.clear
936 # Check that the user account page will display and contains some relevant
937 # information for the user
939 # Test a non-existent user
940 get :show, :params => { :display_name => "unknown" }
941 assert_response :not_found
944 user = create(:user, :home_lon => 1.1, :home_lat => 1.1)
945 friend_user = create(:user, :home_lon => 1.2, :home_lat => 1.2)
946 create(:friend, :befriender => user, :befriendee => friend_user)
947 get :show, :params => { :display_name => user.display_name }
948 assert_response :success
949 assert_select "div#userinformation" do
950 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
951 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
952 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
953 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
954 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
955 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
956 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
957 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
960 # Friends shouldn't be visible as we're not logged in
961 assert_select "div#friends-container", :count => 0
963 # Test a user who has been blocked
964 blocked_user = create(:user)
965 create(:user_block, :user => blocked_user)
966 get :show, :params => { :display_name => blocked_user.display_name }
967 assert_response :success
968 assert_select "div#userinformation" do
969 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
970 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
971 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
972 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
973 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
974 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
975 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
976 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
979 # Test a moderator who has applied blocks
980 moderator_user = create(:moderator_user)
981 create(:user_block, :creator => moderator_user)
982 get :show, :params => { :display_name => moderator_user.display_name }
983 assert_response :success
984 assert_select "div#userinformation" do
985 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
986 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
987 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
988 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
989 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
990 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
991 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
992 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
995 # Login as a normal user
996 session[:user] = user.id
998 # Test the normal user
999 get :show, :params => { :display_name => user.display_name }
1000 assert_response :success
1001 assert_select "div#userinformation" do
1002 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1003 assert_select "a[href='/traces/mine']", 1
1004 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1005 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1006 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 1
1007 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1008 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1009 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1012 # Friends should be visible as we're now logged in
1013 assert_select "div#friends-container" do
1014 assert_select "div.contact-activity", :count => 1
1017 # Login as a moderator
1018 session[:user] = create(:moderator_user).id
1020 # Test the normal user
1021 get :show, :params => { :display_name => user.display_name }
1022 assert_response :success
1023 assert_select "div#userinformation" do
1024 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1025 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1026 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1027 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1028 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1029 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1030 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1031 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
1035 # Test whether information about contributor terms is shown for users who haven't agreed
1036 def test_terms_not_agreed
1037 agreed_user = create(:user, :terms_agreed => 3.days.ago)
1038 seen_user = create(:user, :terms_seen => true)
1039 not_seen_user = create(:user, :terms_seen => false)
1041 get :show, :params => { :display_name => agreed_user.display_name }
1042 assert_response :success
1043 assert_select "div#userinformation" do
1044 assert_select "p", :count => 0, :text => /Contributor terms/
1047 get :show, :params => { :display_name => seen_user.display_name }
1048 assert_response :success
1049 # put @response.body
1050 assert_select "div#userinformation" do
1051 assert_select "p", :count => 1, :text => /Contributor terms/
1052 assert_select "p", /Declined/
1055 get :show, :params => { :display_name => not_seen_user.display_name }
1056 assert_response :success
1057 assert_select "div#userinformation" do
1058 assert_select "p", :count => 1, :text => /Contributor terms/
1059 assert_select "p", /Undecided/
1064 user = create(:user, :description => "test", :terms_agreed => Date.yesterday)
1065 # check that a visible user is returned properly
1066 get :api_read, :params => { :id => user.id }
1067 assert_response :success
1068 assert_equal "text/xml", response.content_type
1070 # check the data that is returned
1071 assert_select "description", :count => 1, :text => "test"
1072 assert_select "contributor-terms", :count => 1 do
1073 assert_select "[agreed='true']"
1075 assert_select "img", :count => 0
1076 assert_select "roles", :count => 1 do
1077 assert_select "role", :count => 0
1079 assert_select "changesets", :count => 1 do
1080 assert_select "[count='0']"
1082 assert_select "traces", :count => 1 do
1083 assert_select "[count='0']"
1085 assert_select "blocks", :count => 1 do
1086 assert_select "received", :count => 1 do
1087 assert_select "[count='0'][active='0']"
1089 assert_select "issued", :count => 0
1092 # check that we aren't revealing private information
1093 assert_select "contributor-terms[pd]", false
1094 assert_select "home", false
1095 assert_select "languages", false
1096 assert_select "messages", false
1098 # check that a suspended user is not returned
1099 get :api_read, :params => { :id => create(:user, :suspended).id }
1100 assert_response :gone
1102 # check that a deleted user is not returned
1103 get :api_read, :params => { :id => create(:user, :deleted).id }
1104 assert_response :gone
1106 # check that a non-existent user is not returned
1107 get :api_read, :params => { :id => 0 }
1108 assert_response :not_found
1111 def test_api_details
1112 user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"])
1113 create(:message, :read, :recipient => user)
1114 create(:message, :sender => user)
1116 # check that nothing is returned when not logged in
1118 assert_response :unauthorized
1120 # check that we get a response when logged in
1121 basic_authorization user.email, "test"
1123 assert_response :success
1124 assert_equal "text/xml", response.content_type
1126 # check the data that is returned
1127 assert_select "description", :count => 1, :text => "test"
1128 assert_select "contributor-terms", :count => 1 do
1129 assert_select "[agreed='true'][pd='false']"
1131 assert_select "img", :count => 0
1132 assert_select "roles", :count => 1 do
1133 assert_select "role", :count => 0
1135 assert_select "changesets", :count => 1 do
1136 assert_select "[count='0']", :count => 1
1138 assert_select "traces", :count => 1 do
1139 assert_select "[count='0']", :count => 1
1141 assert_select "blocks", :count => 1 do
1142 assert_select "received", :count => 1 do
1143 assert_select "[count='0'][active='0']"
1145 assert_select "issued", :count => 0
1147 assert_select "home", :count => 1 do
1148 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
1150 assert_select "languages", :count => 1 do
1151 assert_select "lang", :count => 1, :text => "en"
1153 assert_select "messages", :count => 1 do
1154 assert_select "received", :count => 1 do
1155 assert_select "[count='1'][unread='0']"
1157 assert_select "sent", :count => 1 do
1158 assert_select "[count='1']"
1164 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
1165 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
1166 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
1168 get :api_users, :params => { :users => user1.id }
1169 assert_response :success
1170 assert_equal "text/xml", response.content_type
1171 assert_select "user", :count => 1 do
1172 assert_select "user[id='#{user1.id}']", :count => 1
1173 assert_select "user[id='#{user2.id}']", :count => 0
1174 assert_select "user[id='#{user3.id}']", :count => 0
1177 get :api_users, :params => { :users => user2.id }
1178 assert_response :success
1179 assert_equal "text/xml", response.content_type
1180 assert_select "user", :count => 1 do
1181 assert_select "user[id='#{user1.id}']", :count => 0
1182 assert_select "user[id='#{user2.id}']", :count => 1
1183 assert_select "user[id='#{user3.id}']", :count => 0
1186 get :api_users, :params => { :users => "#{user1.id},#{user3.id}" }
1187 assert_response :success
1188 assert_equal "text/xml", response.content_type
1189 assert_select "user", :count => 2 do
1190 assert_select "user[id='#{user1.id}']", :count => 1
1191 assert_select "user[id='#{user2.id}']", :count => 0
1192 assert_select "user[id='#{user3.id}']", :count => 1
1195 get :api_users, :params => { :users => create(:user, :suspended).id }
1196 assert_response :not_found
1198 get :api_users, :params => { :users => create(:user, :deleted).id }
1199 assert_response :not_found
1201 get :api_users, :params => { :users => 0 }
1202 assert_response :not_found
1205 def test_api_gpx_files
1206 user = create(:user)
1207 trace1 = create(:trace, :user => user) do |trace|
1208 create(:tracetag, :trace => trace, :tag => "London")
1210 trace2 = create(:trace, :user => user) do |trace|
1211 create(:tracetag, :trace => trace, :tag => "Birmingham")
1213 # check that nothing is returned when not logged in
1215 assert_response :unauthorized
1217 # check that we get a response when logged in
1218 basic_authorization user.email, "test"
1220 assert_response :success
1221 assert_equal "application/xml", response.content_type
1223 # check the data that is returned
1224 assert_select "gpx_file[id='#{trace1.id}']", 1 do
1225 assert_select "tag", "London"
1227 assert_select "gpx_file[id='#{trace2.id}']", 1 do
1228 assert_select "tag", "Birmingham"
1232 def test_make_friend
1233 # Get users to work with
1234 user = create(:user)
1235 friend = create(:user)
1237 # Check that the users aren't already friends
1238 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1240 # When not logged in a GET should ask us to login
1241 get :make_friend, :params => { :display_name => friend.display_name }
1242 assert_redirected_to :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1244 # When not logged in a POST should error
1245 post :make_friend, :params => { :display_name => friend.display_name }
1246 assert_response :forbidden
1247 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1249 # When logged in a GET should get a confirmation page
1250 get :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1251 assert_response :success
1252 assert_template :make_friend
1253 assert_select "form" do
1254 assert_select "input[type='hidden'][name='referer']", 0
1255 assert_select "input[type='submit']", 1
1257 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1259 # When logged in a POST should add the friendship
1260 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1261 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1263 assert_redirected_to user_path(friend)
1264 assert_match(/is now your friend/, flash[:notice])
1265 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1266 email = ActionMailer::Base.deliveries.first
1267 assert_equal 1, email.to.count
1268 assert_equal friend.email, email.to.first
1269 ActionMailer::Base.deliveries.clear
1271 # A second POST should report that the friendship already exists
1272 assert_no_difference "ActionMailer::Base.deliveries.size" do
1273 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1275 assert_redirected_to user_path(friend)
1276 assert_match(/You are already friends with/, flash[:warning])
1277 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1280 def test_make_friend_with_referer
1281 # Get users to work with
1282 user = create(:user)
1283 friend = create(:user)
1285 # Check that the users aren't already friends
1286 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1288 # The GET should preserve any referer
1289 get :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1290 assert_response :success
1291 assert_template :make_friend
1292 assert_select "form" do
1293 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1294 assert_select "input[type='submit']", 1
1296 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1298 # When logged in a POST should add the friendship and refer us
1299 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1300 post :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1302 assert_redirected_to "/test"
1303 assert_match(/is now your friend/, flash[:notice])
1304 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1305 email = ActionMailer::Base.deliveries.first
1306 assert_equal 1, email.to.count
1307 assert_equal friend.email, email.to.first
1308 ActionMailer::Base.deliveries.clear
1311 def test_make_friend_unkown_user
1312 # Should error when a bogus user is specified
1313 get :make_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1314 assert_response :not_found
1315 assert_template :no_such_user
1318 def test_remove_friend
1319 # Get users to work with
1320 user = create(:user)
1321 friend = create(:user)
1322 create(:friend, :befriender => user, :befriendee => friend)
1324 # Check that the users are friends
1325 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1327 # When not logged in a GET should ask us to login
1328 get :remove_friend, :params => { :display_name => friend.display_name }
1329 assert_redirected_to :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1331 # When not logged in a POST should error
1332 post :remove_friend, :params => { :display_name => friend.display_name }
1333 assert_response :forbidden
1334 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1336 # When logged in a GET should get a confirmation page
1337 get :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1338 assert_response :success
1339 assert_template :remove_friend
1340 assert_select "form" do
1341 assert_select "input[type='hidden'][name='referer']", 0
1342 assert_select "input[type='submit']", 1
1344 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1346 # When logged in a POST should remove the friendship
1347 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1348 assert_redirected_to user_path(friend)
1349 assert_match(/was removed from your friends/, flash[:notice])
1350 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1352 # A second POST should report that the friendship does not exist
1353 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1354 assert_redirected_to user_path(friend)
1355 assert_match(/is not one of your friends/, flash[:error])
1356 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1359 def test_remove_friend_with_referer
1360 # Get users to work with
1361 user = create(:user)
1362 friend = create(:user)
1363 create(:friend, :user_id => user.id, :friend_user_id => friend.id)
1365 # Check that the users are friends
1366 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1368 # The GET should preserve any referer
1369 get :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1370 assert_response :success
1371 assert_template :remove_friend
1372 assert_select "form" do
1373 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1374 assert_select "input[type='submit']", 1
1376 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1378 # When logged in a POST should remove the friendship and refer
1379 post :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1380 assert_redirected_to "/test"
1381 assert_match(/was removed from your friends/, flash[:notice])
1382 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1385 def test_remove_friend_unkown_user
1386 # Should error when a bogus user is specified
1387 get :remove_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1388 assert_response :not_found
1389 assert_template :no_such_user
1393 user = create(:user)
1395 # Try without logging in
1396 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }
1397 assert_response :redirect
1398 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1400 # Now try as a normal user
1401 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1402 assert_response :redirect
1403 assert_redirected_to :action => :show, :display_name => user.display_name
1405 # Finally try as an administrator
1406 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1407 assert_response :redirect
1408 assert_redirected_to :action => :show, :display_name => user.display_name
1409 assert_equal "suspended", User.find(user.id).status
1413 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1415 # Try without logging in
1416 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }
1417 assert_response :redirect
1418 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1420 # Now try as a normal user
1421 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1422 assert_response :redirect
1423 assert_redirected_to :action => :show, :display_name => user.display_name
1425 # Finally try as an administrator
1426 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1427 assert_response :redirect
1428 assert_redirected_to :action => :show, :display_name => user.display_name
1430 # Check that the user was deleted properly
1432 assert_equal "user_#{user.id}", user.display_name
1433 assert_equal "", user.description
1434 assert_nil user.home_lat
1435 assert_nil user.home_lon
1436 assert_equal false, user.image.file?
1437 assert_equal false, user.email_valid
1438 assert_nil user.new_email
1439 assert_nil user.auth_provider
1440 assert_nil user.auth_uid
1441 assert_equal "deleted", user.status
1445 user = create(:user)
1446 moderator_user = create(:moderator_user)
1447 administrator_user = create(:administrator_user)
1448 _suspended_user = create(:user, :suspended)
1449 _ip_user = create(:user, :creation_ip => "1.2.3.4")
1451 # There are now 7 users - the five above, plus two extra "granters" for the
1452 # moderator_user and administrator_user
1453 assert_equal 7, User.count
1455 # Shouldn't work when not logged in
1457 assert_response :redirect
1458 assert_redirected_to :action => :login, :referer => users_path
1460 session[:user] = user.id
1462 # Shouldn't work when logged in as a normal user
1464 assert_response :redirect
1465 assert_redirected_to :action => :login, :referer => users_path
1467 session[:user] = moderator_user.id
1469 # Shouldn't work when logged in as a moderator
1471 assert_response :redirect
1472 assert_redirected_to :action => :login, :referer => users_path
1474 session[:user] = administrator_user.id
1476 # Note there is a header row, so all row counts are users + 1
1477 # Should work when logged in as an administrator
1479 assert_response :success
1480 assert_template :index
1481 assert_select "table#user_list tr", :count => 7 + 1
1483 # Should be able to limit by status
1484 get :index, :params => { :status => "suspended" }
1485 assert_response :success
1486 assert_template :index
1487 assert_select "table#user_list tr", :count => 1 + 1
1489 # Should be able to limit by IP address
1490 get :index, :params => { :ip => "1.2.3.4" }
1491 assert_response :success
1492 assert_template :index
1493 assert_select "table#user_list tr", :count => 1 + 1
1496 def test_index_get_paginated
1497 1.upto(100).each do |n|
1498 User.create(:display_name => "extra_#{n}",
1499 :email => "extra#{n}@example.com",
1500 :pass_crypt => "extraextra")
1503 session[:user] = create(:administrator_user).id
1505 # 100 examples, an administrator, and a granter for the admin.
1506 assert_equal 102, User.count
1509 assert_response :success
1510 assert_template :index
1511 assert_select "table#user_list tr", :count => 51
1513 get :index, :params => { :page => 2 }
1514 assert_response :success
1515 assert_template :index
1516 assert_select "table#user_list tr", :count => 51
1518 get :index, :params => { :page => 3 }
1519 assert_response :success
1520 assert_template :index
1521 assert_select "table#user_list tr", :count => 3
1524 def test_index_post_confirm
1525 inactive_user = create(:user, :pending)
1526 suspended_user = create(:user, :suspended)
1528 # Shouldn't work when not logged in
1529 assert_no_difference "User.active.count" do
1530 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1532 assert_response :redirect
1533 assert_redirected_to :action => :login, :referer => users_path
1534 assert_equal "pending", inactive_user.reload.status
1535 assert_equal "suspended", suspended_user.reload.status
1537 session[:user] = create(:user).id
1539 # Shouldn't work when logged in as a normal user
1540 assert_no_difference "User.active.count" do
1541 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1543 assert_response :redirect
1544 assert_redirected_to :action => :login, :referer => users_path
1545 assert_equal "pending", inactive_user.reload.status
1546 assert_equal "suspended", suspended_user.reload.status
1548 session[:user] = create(:moderator_user).id
1550 # Shouldn't work when logged in as a moderator
1551 assert_no_difference "User.active.count" do
1552 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1554 assert_response :redirect
1555 assert_redirected_to :action => :login, :referer => users_path
1556 assert_equal "pending", inactive_user.reload.status
1557 assert_equal "suspended", suspended_user.reload.status
1559 session[:user] = create(:administrator_user).id
1561 # Should work when logged in as an administrator
1562 assert_difference "User.active.count", 2 do
1563 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1565 assert_response :redirect
1566 assert_redirected_to :action => :index
1567 assert_equal "confirmed", inactive_user.reload.status
1568 assert_equal "confirmed", suspended_user.reload.status
1571 def test_index_post_hide
1572 normal_user = create(:user)
1573 confirmed_user = create(:user, :confirmed)
1575 # Shouldn't work when not logged in
1576 assert_no_difference "User.active.count" do
1577 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1579 assert_response :redirect
1580 assert_redirected_to :action => :login, :referer => users_path
1581 assert_equal "active", normal_user.reload.status
1582 assert_equal "confirmed", confirmed_user.reload.status
1584 session[:user] = create(:user).id
1586 # Shouldn't work when logged in as a normal user
1587 assert_no_difference "User.active.count" do
1588 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1590 assert_response :redirect
1591 assert_redirected_to :action => :login, :referer => users_path
1592 assert_equal "active", normal_user.reload.status
1593 assert_equal "confirmed", confirmed_user.reload.status
1595 session[:user] = create(:moderator_user).id
1597 # Shouldn't work when logged in as a moderator
1598 assert_no_difference "User.active.count" do
1599 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1601 assert_response :redirect
1602 assert_redirected_to :action => :login, :referer => users_path
1603 assert_equal "active", normal_user.reload.status
1604 assert_equal "confirmed", confirmed_user.reload.status
1606 session[:user] = create(:administrator_user).id
1608 # Should work when logged in as an administrator
1609 assert_difference "User.active.count", -2 do
1610 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1612 assert_response :redirect
1613 assert_redirected_to :action => :index
1614 assert_equal "deleted", normal_user.reload.status
1615 assert_equal "deleted", confirmed_user.reload.status