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 perform_enqueued_jobs do
241 post :save, :session => { :new_user => user }
247 register_email = ActionMailer::Base.deliveries.first
249 assert_equal register_email.to[0], user.email
250 assert_match(/#{@url}/, register_email.body.to_s)
253 assert_redirected_to :action => "confirm", :display_name => user.display_name
255 ActionMailer::Base.deliveries.clear
258 def test_new_duplicate_email
259 user = build(:user, :pending)
260 user.email = create(:user).email
262 assert_no_difference "User.count" do
263 assert_no_difference "ActionMailer::Base.deliveries.size" do
264 perform_enqueued_jobs do
265 post :save, :session => { :new_user => user }
270 assert_response :success
271 assert_template "new"
272 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
275 def test_new_duplicate_email_uppercase
276 user = build(:user, :pending)
277 user.email = create(:user).email.upcase
279 assert_no_difference "User.count" do
280 assert_no_difference "ActionMailer::Base.deliveries.size" do
281 perform_enqueued_jobs do
282 post :save, :session => { :new_user => user }
287 assert_response :success
288 assert_template "new"
289 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email"
292 def test_new_duplicate_name
293 user = build(:user, :pending)
294 user.display_name = create(:user).display_name
296 assert_no_difference "User.count" do
297 assert_no_difference "ActionMailer::Base.deliveries.size" do
298 perform_enqueued_jobs do
299 post :save, :session => { :new_user => user }
304 assert_response :success
305 assert_template "new"
306 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
309 def test_new_duplicate_name_uppercase
310 user = build(:user, :pending)
311 user.display_name = create(:user).display_name.upcase
313 assert_no_difference "User.count" do
314 assert_no_difference "ActionMailer::Base.deliveries.size" do
315 perform_enqueued_jobs do
316 post :save, :session => { :new_user => user }
321 assert_response :success
322 assert_template "new"
323 assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name"
326 def test_new_blocked_domain
327 user = build(:user, :pending, :email => "user@example.net")
328 create(:acl, :domain => "example.net", :k => "no_account_creation")
330 assert_no_difference "User.count" do
331 assert_no_difference "ActionMailer::Base.deliveries.size" do
332 perform_enqueued_jobs do
333 post :save, :session => { :new_user => user }
338 assert_response :success
339 assert_template "blocked"
342 def test_save_referer_params
343 user = build(:user, :pending)
345 assert_difference "User.count", 1 do
346 assert_difference "ActionMailer::Base.deliveries.size", 1 do
347 perform_enqueued_jobs do
348 post :save, :session => { :new_user => user,
349 :referer => "/edit?editor=id#map=1/2/3" }
354 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
355 user.tokens.order("id DESC").first.referer
357 ActionMailer::Base.deliveries.clear
360 def test_logout_without_referer
362 assert_response :success
363 assert_template :logout
364 assert_select "input[name=referer][value=?]", ""
366 session_id = assert_select("input[name=session]").first["value"]
368 get :logout, :params => { :session => session_id }
369 assert_response :redirect
370 assert_redirected_to root_path
373 def test_logout_with_referer
374 get :logout, :params => { :referer => "/test" }
375 assert_response :success
376 assert_template :logout
377 assert_select "input[name=referer][value=?]", "/test"
379 session_id = assert_select("input[name=session]").first["value"]
381 get :logout, :params => { :session => session_id, :referer => "/test" }
382 assert_response :redirect
383 assert_redirected_to "/test"
386 def test_logout_with_token
387 token = create(:user).tokens.create
389 session[:token] = token.token
392 assert_response :success
393 assert_template :logout
394 assert_select "input[name=referer][value=?]", ""
395 assert_equal token.token, session[:token]
396 assert_not_nil UserToken.where(:id => token.id).first
398 session_id = assert_select("input[name=session]").first["value"]
400 get :logout, :params => { :session => session_id }
401 assert_response :redirect
402 assert_redirected_to root_path
403 assert_nil session[:token]
404 assert_nil UserToken.where(:id => token.id).first
408 user = create(:user, :pending)
409 confirm_string = user.tokens.create.token
411 @request.cookies["_osm_session"] = user.display_name
412 get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
413 assert_response :success
414 assert_template :confirm
417 def test_confirm_get_already_confirmed
419 confirm_string = user.tokens.create.token
421 @request.cookies["_osm_session"] = user.display_name
422 get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
423 assert_response :redirect
424 assert_redirected_to root_path
427 def test_confirm_success_no_token_no_referer
428 user = create(:user, :pending)
429 stub_gravatar_request(user.email)
430 confirm_string = user.tokens.create.token
432 @request.cookies["_osm_session"] = user.display_name
433 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
434 assert_redirected_to login_path
435 assert_match(/Confirmed your account/, flash[:notice])
438 def test_confirm_success_good_token_no_referer
439 user = create(:user, :pending)
440 stub_gravatar_request(user.email)
441 confirm_string = user.tokens.create.token
442 token = user.tokens.create.token
444 @request.cookies["_osm_session"] = user.display_name
445 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
446 assert_redirected_to welcome_path
449 def test_confirm_success_bad_token_no_referer
450 user = create(:user, :pending)
451 stub_gravatar_request(user.email)
452 confirm_string = user.tokens.create.token
453 token = create(:user).tokens.create.token
455 @request.cookies["_osm_session"] = user.display_name
456 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
457 assert_redirected_to login_path
458 assert_match(/Confirmed your account/, flash[:notice])
461 def test_confirm_success_no_token_with_referer
462 user = create(:user, :pending)
463 stub_gravatar_request(user.email)
464 confirm_string = user.tokens.create(:referer => diary_new_path).token
466 @request.cookies["_osm_session"] = user.display_name
467 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
468 assert_redirected_to login_path(:referer => diary_new_path)
469 assert_match(/Confirmed your account/, flash[:notice])
472 def test_confirm_success_good_token_with_referer
473 user = create(:user, :pending)
474 stub_gravatar_request(user.email)
475 confirm_string = user.tokens.create(:referer => diary_new_path).token
476 token = user.tokens.create.token
478 @request.cookies["_osm_session"] = user.display_name
479 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
480 assert_redirected_to diary_new_path
483 def test_confirm_success_bad_token_with_referer
484 user = create(:user, :pending)
485 stub_gravatar_request(user.email)
486 confirm_string = user.tokens.create(:referer => diary_new_path).token
487 token = create(:user).tokens.create.token
489 @request.cookies["_osm_session"] = user.display_name
490 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
491 assert_redirected_to login_path(:referer => diary_new_path)
492 assert_match(/Confirmed your account/, flash[:notice])
495 def test_confirm_expired_token
496 user = create(:user, :pending)
497 confirm_string = user.tokens.create(:expiry => 1.day.ago).token
499 @request.cookies["_osm_session"] = user.display_name
500 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
501 assert_redirected_to :action => "confirm"
502 assert_match(/confirmation code has expired/, flash[:error])
505 def test_confirm_already_confirmed
507 confirm_string = user.tokens.create(:referer => diary_new_path).token
509 @request.cookies["_osm_session"] = user.display_name
510 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
511 assert_redirected_to :action => "login"
512 assert_match(/already been confirmed/, flash[:error])
515 def test_confirm_resend_success
516 user = create(:user, :pending)
517 session[:token] = user.tokens.create.token
519 assert_difference "ActionMailer::Base.deliveries.size", 1 do
520 perform_enqueued_jobs do
521 get :confirm_resend, :params => { :display_name => user.display_name }
525 assert_response :redirect
526 assert_redirected_to login_path
527 assert_match(/sent a new confirmation/, flash[:notice])
529 email = ActionMailer::Base.deliveries.last
531 assert_equal user.email, email.to.first
533 ActionMailer::Base.deliveries.clear
536 def test_confirm_resend_no_token
537 user = create(:user, :pending)
538 assert_no_difference "ActionMailer::Base.deliveries.size" do
539 perform_enqueued_jobs do
540 get :confirm_resend, :params => { :display_name => user.display_name }
544 assert_response :redirect
545 assert_redirected_to login_path
546 assert_match "User #{user.display_name} not found.", flash[:error]
549 def test_confirm_resend_unknown_user
550 assert_no_difference "ActionMailer::Base.deliveries.size" do
551 perform_enqueued_jobs do
552 get :confirm_resend, :params => { :display_name => "No Such User" }
556 assert_response :redirect
557 assert_redirected_to login_path
558 assert_match "User No Such User not found.", flash[:error]
561 def test_confirm_email_get
563 confirm_string = user.tokens.create.token
565 get :confirm_email, :params => { :confirm_string => confirm_string }
566 assert_response :success
567 assert_template :confirm_email
570 def test_confirm_email_success
571 user = create(:user, :new_email => "test-new@example.com")
572 stub_gravatar_request(user.new_email)
573 confirm_string = user.tokens.create.token
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])
581 def test_confirm_email_already_confirmed
583 confirm_string = user.tokens.create.token
585 post :confirm_email, :params => { :confirm_string => confirm_string }
586 assert_response :redirect
587 assert_redirected_to :action => :account, :display_name => user.display_name
588 assert_match(/already been confirmed/, flash[:error])
591 def test_confirm_email_bad_token
592 post :confirm_email, :params => { :confirm_string => "XXXXX" }
593 assert_response :success
594 assert_template :confirm_email
595 assert_match(/confirmation code has expired or does not exist/, flash[:error])
599 # test if testing for a gravatar works
600 # this happens when the email is actually changed
601 # which is triggered by the confirmation mail
602 def test_gravatar_auto_enable
603 # switch to email that has a gravatar
604 user = create(:user, :new_email => "test-new@example.com")
605 stub_gravatar_request(user.new_email, 200)
606 confirm_string = user.tokens.create.token
607 # precondition gravatar should be turned off
608 assert_not user.image_use_gravatar
609 post :confirm_email, :params => { :confirm_string => confirm_string }
610 assert_response :redirect
611 assert_redirected_to :action => :account, :display_name => user.display_name
612 assert_match(/Confirmed your change of email address/, flash[:notice])
613 # gravatar use should now be enabled
614 assert User.find(user.id).image_use_gravatar
617 def test_gravatar_auto_disable
618 # switch to email without a gravatar
619 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
620 stub_gravatar_request(user.new_email, 404)
621 confirm_string = user.tokens.create.token
622 # precondition gravatar should be turned on
623 assert user.image_use_gravatar
624 post :confirm_email, :params => { :confirm_string => confirm_string }
625 assert_response :redirect
626 assert_redirected_to :action => :account, :display_name => user.display_name
627 assert_match(/Confirmed your change of email address/, flash[:notice])
628 # gravatar use should now be disabled
629 assert_not User.find(user.id).image_use_gravatar
632 def test_terms_new_user
633 get :terms, :session => { :new_user => User.new }
634 assert_response :success
635 assert_template :terms
638 def test_terms_agreed
639 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
641 session[:user] = user.id
644 assert_response :redirect
645 assert_redirected_to :action => :account, :display_name => user.display_name
648 def test_terms_not_seen_without_referer
649 user = create(:user, :terms_seen => false)
651 session[:user] = user.id
654 assert_response :success
655 assert_template :terms
657 post :save, :params => { :user => { :consider_pd => true } }
658 assert_response :redirect
659 assert_redirected_to :action => :account, :display_name => user.display_name
660 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
664 assert_equal true, user.consider_pd
665 assert_not_nil user.terms_agreed
666 assert_equal true, user.terms_seen
669 def test_terms_not_seen_with_referer
670 user = create(:user, :terms_seen => false)
672 session[:user] = user.id
674 get :terms, :params => { :referer => "/test" }
675 assert_response :success
676 assert_template :terms
678 post :save, :params => { :user => { :consider_pd => true }, :referer => "/test" }
679 assert_response :redirect
680 assert_redirected_to "/test"
681 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
685 assert_equal true, user.consider_pd
686 assert_not_nil user.terms_agreed
687 assert_equal true, user.terms_seen
690 # Check that if you haven't seen the terms, and make a request that requires authentication,
691 # that your request is redirected to view the terms
692 def test_terms_not_seen_redirection
693 user = create(:user, :terms_seen => false)
694 session[:user] = user.id
696 get :account, :params => { :display_name => user.display_name }
697 assert_response :redirect
698 assert_redirected_to :action => :terms, :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
702 user = create(:user, :data_public => false)
703 post :go_public, :session => { :user => user }
704 assert_response :redirect
705 assert_redirected_to :action => :account, :display_name => user.display_name
706 assert_equal true, User.find(user.id).data_public
709 def test_lost_password
710 # Test fetching the lost password page
712 assert_response :success
713 assert_template :lost_password
714 assert_select "div#notice", false
716 # Test resetting using the address as recorded for a user that has an
717 # address which is duplicated in a different case by another user
719 uppercase_user = build(:user, :email => user.email.upcase).tap { |u| u.save(:validate => false) }
721 assert_difference "ActionMailer::Base.deliveries.size", 1 do
722 perform_enqueued_jobs do
723 post :lost_password, :params => { :user => { :email => user.email } }
726 assert_response :redirect
727 assert_redirected_to :action => :login
728 assert_match(/^Sorry you lost it/, flash[:notice])
729 email = ActionMailer::Base.deliveries.first
730 assert_equal 1, email.to.count
731 assert_equal user.email, email.to.first
732 ActionMailer::Base.deliveries.clear
734 # Test resetting using an address that matches a different user
735 # that has the same address in a different case
736 assert_difference "ActionMailer::Base.deliveries.size", 1 do
737 perform_enqueued_jobs do
738 post :lost_password, :params => { :user => { :email => user.email.upcase } }
741 assert_response :redirect
742 assert_redirected_to :action => :login
743 assert_match(/^Sorry you lost it/, flash[:notice])
744 email = ActionMailer::Base.deliveries.first
745 assert_equal 1, email.to.count
746 assert_equal uppercase_user.email, email.to.first
747 ActionMailer::Base.deliveries.clear
749 # Test resetting using an address that is a case insensitive match
750 # for more than one user but not an exact match for either
751 assert_no_difference "ActionMailer::Base.deliveries.size" do
752 perform_enqueued_jobs do
753 post :lost_password, :params => { :user => { :email => user.email.titlecase } }
756 assert_response :success
757 assert_template :lost_password
758 assert_select ".error", /^Could not find that email address/
760 # Test resetting using the address as recorded for a user that has an
761 # address which is case insensitively unique
762 third_user = create(:user)
763 assert_difference "ActionMailer::Base.deliveries.size", 1 do
764 perform_enqueued_jobs do
765 post :lost_password, :params => { :user => { :email => third_user.email } }
768 assert_response :redirect
769 assert_redirected_to :action => :login
770 assert_match(/^Sorry you lost it/, flash[:notice])
771 email = ActionMailer::Base.deliveries.first
772 assert_equal 1, email.to.count
773 assert_equal third_user.email, email.to.first
774 ActionMailer::Base.deliveries.clear
776 # Test resetting using an address that matches a user that has the
777 # same (case insensitively unique) address in a different case
778 assert_difference "ActionMailer::Base.deliveries.size", 1 do
779 perform_enqueued_jobs do
780 post :lost_password, :params => { :user => { :email => third_user.email.upcase } }
783 assert_response :redirect
784 assert_redirected_to :action => :login
785 assert_match(/^Sorry you lost it/, flash[:notice])
786 email = ActionMailer::Base.deliveries.first
787 assert_equal 1, email.to.count
788 assert_equal third_user.email, email.to.first
789 ActionMailer::Base.deliveries.clear
792 def test_reset_password
793 user = create(:user, :pending)
794 # Test a request with no token
796 assert_response :bad_request
798 # Test a request with a bogus token
799 get :reset_password, :params => { :token => "made_up_token" }
800 assert_response :redirect
801 assert_redirected_to :action => :lost_password
803 # Create a valid token for a user
804 token = user.tokens.create
806 # Test a request with a valid token
807 get :reset_password, :params => { :token => token.token }
808 assert_response :success
809 assert_template :reset_password
811 # Test that errors are reported for erroneous submissions
812 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "different_password" } }
813 assert_response :success
814 assert_template :reset_password
815 assert_select "div#errorExplanation"
817 # Test setting a new password
818 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" } }
819 assert_response :redirect
820 assert_redirected_to root_path
821 assert_equal user.id, session[:user]
823 assert_equal "active", user.status
824 assert_equal true, user.email_valid
825 assert_equal user, User.authenticate(:username => user.email, :password => "new_password")
829 # Get a user to work with - note that this user deliberately
830 # conflicts with uppercase_user in the email and display name
831 # fields to test that we can change other fields without any
832 # validation errors being reported
833 user = create(:user, :languages => [])
834 _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
836 # Make sure that you are redirected to the login page when
837 # you are not logged in
838 get :account, :params => { :display_name => user.display_name }
839 assert_response :redirect
840 assert_redirected_to :action => "login", :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
842 # Make sure that you are blocked when not logged in as the right user
843 get :account, :params => { :display_name => user.display_name }, :session => { :user => create(:user) }
844 assert_response :forbidden
846 # Make sure we get the page when we are logged in as the right user
847 get :account, :params => { :display_name => user.display_name }, :session => { :user => user }
848 assert_response :success
849 assert_template :account
850 assert_select "form#accountForm" do |form|
851 assert_equal "post", form.attr("method").to_s
852 assert_select "input[name='_method']", false
853 assert_equal "/user/#{ERB::Util.u(user.display_name)}/account", form.attr("action").to_s
856 # Updating the description should work
857 user.description = "new description"
858 user.preferred_editor = "default"
859 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
860 assert_response :success
861 assert_template :account
862 assert_select "div#errorExplanation", false
863 assert_select ".notice", /^User information updated successfully/
864 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
866 # Changing to a invalid editor should fail
867 user.preferred_editor = "unknown"
868 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
869 assert_response :success
870 assert_template :account
871 assert_select ".notice", false
872 assert_select "div#errorExplanation"
873 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
875 # Changing to a valid editor should work
876 user.preferred_editor = "potlatch2"
877 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
878 assert_response :success
879 assert_template :account
880 assert_select "div#errorExplanation", false
881 assert_select ".notice", /^User information updated successfully/
882 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
884 # Changing to the default editor should work
885 user.preferred_editor = "default"
886 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
887 assert_response :success
888 assert_template :account
889 assert_select "div#errorExplanation", false
890 assert_select ".notice", /^User information updated successfully/
891 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
893 # Changing to an uploaded image should work
894 image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif")
895 post :account, :params => { :display_name => user.display_name, :image_action => "new", :user => user.attributes.merge(:image => image) }, :session => { :user => user }
896 assert_response :success
897 assert_template :account
898 assert_select "div#errorExplanation", false
899 assert_select ".notice", /^User information updated successfully/
900 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "keep"
902 # Changing to a gravatar image should work
903 post :account, :params => { :display_name => user.display_name, :image_action => "gravatar", :user => user.attributes }, :session => { :user => user }
904 assert_response :success
905 assert_template :account
906 assert_select "div#errorExplanation", false
907 assert_select ".notice", /^User information updated successfully/
908 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "gravatar"
910 # Removing the image should work
911 post :account, :params => { :display_name => user.display_name, :image_action => "delete", :user => user.attributes }, :session => { :user => user }
912 assert_response :success
913 assert_template :account
914 assert_select "div#errorExplanation", false
915 assert_select ".notice", /^User information updated successfully/
916 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked]", false
918 # Adding external authentication should redirect to the auth provider
919 post :account, :params => { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, :session => { :user => user }
920 assert_response :redirect
921 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")
923 # Changing name to one that exists should fail
924 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
925 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
926 assert_response :success
927 assert_template :account
928 assert_select ".notice", false
929 assert_select "div#errorExplanation"
930 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
932 # Changing name to one that exists should fail, regardless of case
933 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
934 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
935 assert_response :success
936 assert_template :account
937 assert_select ".notice", false
938 assert_select "div#errorExplanation"
939 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
941 # Changing name to one that doesn't exist should work
942 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
943 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
944 assert_response :success
945 assert_template :account
946 assert_select "div#errorExplanation", false
947 assert_select ".notice", /^User information updated successfully/
948 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
950 # Record the change of name
951 user.display_name = "new tester"
953 # Changing email to one that exists should fail
954 user.new_email = create(:user).email
955 assert_no_difference "ActionMailer::Base.deliveries.size" do
956 perform_enqueued_jobs do
957 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
960 assert_response :success
961 assert_template :account
962 assert_select ".notice", false
963 assert_select "div#errorExplanation"
964 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
966 # Changing email to one that exists should fail, regardless of case
967 user.new_email = create(:user).email.upcase
968 assert_no_difference "ActionMailer::Base.deliveries.size" do
969 perform_enqueued_jobs do
970 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
973 assert_response :success
974 assert_template :account
975 assert_select ".notice", false
976 assert_select "div#errorExplanation"
977 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
979 # Changing email to one that doesn't exist should work
980 user.new_email = "new_tester@example.com"
981 assert_difference "ActionMailer::Base.deliveries.size", 1 do
982 perform_enqueued_jobs do
983 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
986 assert_response :success
987 assert_template :account
988 assert_select "div#errorExplanation", false
989 assert_select ".notice", /^User information updated successfully/
990 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
991 email = ActionMailer::Base.deliveries.first
992 assert_equal 1, email.to.count
993 assert_equal user.new_email, email.to.first
994 ActionMailer::Base.deliveries.clear
997 # Check that the user account page will display and contains some relevant
998 # information for the user
1000 # Test a non-existent user
1001 get :show, :params => { :display_name => "unknown" }
1002 assert_response :not_found
1004 # Test a normal user
1005 user = create(:user, :home_lon => 1.1, :home_lat => 1.1)
1006 friend_user = create(:user, :home_lon => 1.2, :home_lat => 1.2)
1007 create(:friend, :befriender => user, :befriendee => friend_user)
1008 create(:changeset, :user => friend_user)
1010 get :show, :params => { :display_name => user.display_name }
1011 assert_response :success
1012 assert_select "div#userinformation" do
1013 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1014 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1015 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1016 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1017 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1018 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1019 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1020 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1023 # Friends shouldn't be visible as we're not logged in
1024 assert_select "div#friends-container", :count => 0
1026 # Test a user who has been blocked
1027 blocked_user = create(:user)
1028 create(:user_block, :user => blocked_user)
1029 get :show, :params => { :display_name => blocked_user.display_name }
1030 assert_response :success
1031 assert_select "div#userinformation" do
1032 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
1033 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
1034 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
1035 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
1036 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
1037 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
1038 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
1039 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
1042 # Test a moderator who has applied blocks
1043 moderator_user = create(:moderator_user)
1044 create(:user_block, :creator => moderator_user)
1045 get :show, :params => { :display_name => moderator_user.display_name }
1046 assert_response :success
1047 assert_select "div#userinformation" do
1048 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
1049 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
1050 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
1051 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
1052 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
1053 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
1054 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
1055 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
1058 # Login as a normal user
1059 session[:user] = user.id
1061 # Test the normal user
1062 get :show, :params => { :display_name => user.display_name }
1063 assert_response :success
1064 assert_select "div#userinformation" do
1065 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1066 assert_select "a[href='/traces/mine']", 1
1067 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1068 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1069 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 1
1070 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1071 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1072 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1075 # Friends should be visible as we're now logged in
1076 assert_select "div#friends-container" do
1077 assert_select "div.contact-activity", :count => 1
1080 # Login as a moderator
1081 session[:user] = create(:moderator_user).id
1083 # Test the normal user
1084 get :show, :params => { :display_name => user.display_name }
1085 assert_response :success
1086 assert_select "div#userinformation" do
1087 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1088 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1089 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1090 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1091 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1092 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1093 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1094 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
1098 # Test whether information about contributor terms is shown for users who haven't agreed
1099 def test_terms_not_agreed
1100 agreed_user = create(:user, :terms_agreed => 3.days.ago)
1101 seen_user = create(:user, :terms_seen => true)
1102 not_seen_user = create(:user, :terms_seen => false)
1104 get :show, :params => { :display_name => agreed_user.display_name }
1105 assert_response :success
1106 assert_select "div#userinformation" do
1107 assert_select "p", :count => 0, :text => /Contributor terms/
1110 get :show, :params => { :display_name => seen_user.display_name }
1111 assert_response :success
1112 # put @response.body
1113 assert_select "div#userinformation" do
1114 assert_select "p", :count => 1, :text => /Contributor terms/
1115 assert_select "p", /Declined/
1118 get :show, :params => { :display_name => not_seen_user.display_name }
1119 assert_response :success
1120 assert_select "div#userinformation" do
1121 assert_select "p", :count => 1, :text => /Contributor terms/
1122 assert_select "p", /Undecided/
1127 user = create(:user, :description => "test", :terms_agreed => Date.yesterday)
1128 # check that a visible user is returned properly
1129 get :api_read, :params => { :id => user.id }
1130 assert_response :success
1131 assert_equal "text/xml", response.content_type
1133 # check the data that is returned
1134 assert_select "description", :count => 1, :text => "test"
1135 assert_select "contributor-terms", :count => 1 do
1136 assert_select "[agreed='true']"
1138 assert_select "img", :count => 0
1139 assert_select "roles", :count => 1 do
1140 assert_select "role", :count => 0
1142 assert_select "changesets", :count => 1 do
1143 assert_select "[count='0']"
1145 assert_select "traces", :count => 1 do
1146 assert_select "[count='0']"
1148 assert_select "blocks", :count => 1 do
1149 assert_select "received", :count => 1 do
1150 assert_select "[count='0'][active='0']"
1152 assert_select "issued", :count => 0
1155 # check that we aren't revealing private information
1156 assert_select "contributor-terms[pd]", false
1157 assert_select "home", false
1158 assert_select "languages", false
1159 assert_select "messages", false
1161 # check that a suspended user is not returned
1162 get :api_read, :params => { :id => create(:user, :suspended).id }
1163 assert_response :gone
1165 # check that a deleted user is not returned
1166 get :api_read, :params => { :id => create(:user, :deleted).id }
1167 assert_response :gone
1169 # check that a non-existent user is not returned
1170 get :api_read, :params => { :id => 0 }
1171 assert_response :not_found
1174 def test_api_details
1175 user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"])
1176 create(:message, :read, :recipient => user)
1177 create(:message, :sender => user)
1179 # check that nothing is returned when not logged in
1181 assert_response :unauthorized
1183 # check that we get a response when logged in
1184 basic_authorization user.email, "test"
1186 assert_response :success
1187 assert_equal "text/xml", response.content_type
1189 # check the data that is returned
1190 assert_select "description", :count => 1, :text => "test"
1191 assert_select "contributor-terms", :count => 1 do
1192 assert_select "[agreed='true'][pd='false']"
1194 assert_select "img", :count => 0
1195 assert_select "roles", :count => 1 do
1196 assert_select "role", :count => 0
1198 assert_select "changesets", :count => 1 do
1199 assert_select "[count='0']", :count => 1
1201 assert_select "traces", :count => 1 do
1202 assert_select "[count='0']", :count => 1
1204 assert_select "blocks", :count => 1 do
1205 assert_select "received", :count => 1 do
1206 assert_select "[count='0'][active='0']"
1208 assert_select "issued", :count => 0
1210 assert_select "home", :count => 1 do
1211 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
1213 assert_select "languages", :count => 1 do
1214 assert_select "lang", :count => 1, :text => "en"
1216 assert_select "messages", :count => 1 do
1217 assert_select "received", :count => 1 do
1218 assert_select "[count='1'][unread='0']"
1220 assert_select "sent", :count => 1 do
1221 assert_select "[count='1']"
1227 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
1228 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
1229 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
1231 get :api_users, :params => { :users => user1.id }
1232 assert_response :success
1233 assert_equal "text/xml", response.content_type
1234 assert_select "user", :count => 1 do
1235 assert_select "user[id='#{user1.id}']", :count => 1
1236 assert_select "user[id='#{user2.id}']", :count => 0
1237 assert_select "user[id='#{user3.id}']", :count => 0
1240 get :api_users, :params => { :users => user2.id }
1241 assert_response :success
1242 assert_equal "text/xml", response.content_type
1243 assert_select "user", :count => 1 do
1244 assert_select "user[id='#{user1.id}']", :count => 0
1245 assert_select "user[id='#{user2.id}']", :count => 1
1246 assert_select "user[id='#{user3.id}']", :count => 0
1249 get :api_users, :params => { :users => "#{user1.id},#{user3.id}" }
1250 assert_response :success
1251 assert_equal "text/xml", response.content_type
1252 assert_select "user", :count => 2 do
1253 assert_select "user[id='#{user1.id}']", :count => 1
1254 assert_select "user[id='#{user2.id}']", :count => 0
1255 assert_select "user[id='#{user3.id}']", :count => 1
1258 get :api_users, :params => { :users => create(:user, :suspended).id }
1259 assert_response :not_found
1261 get :api_users, :params => { :users => create(:user, :deleted).id }
1262 assert_response :not_found
1264 get :api_users, :params => { :users => 0 }
1265 assert_response :not_found
1268 def test_api_gpx_files
1269 user = create(:user)
1270 trace1 = create(:trace, :user => user) do |trace|
1271 create(:tracetag, :trace => trace, :tag => "London")
1273 trace2 = create(:trace, :user => user) do |trace|
1274 create(:tracetag, :trace => trace, :tag => "Birmingham")
1276 # check that nothing is returned when not logged in
1278 assert_response :unauthorized
1280 # check that we get a response when logged in
1281 basic_authorization user.email, "test"
1283 assert_response :success
1284 assert_equal "application/xml", response.content_type
1286 # check the data that is returned
1287 assert_select "gpx_file[id='#{trace1.id}']", 1 do
1288 assert_select "tag", "London"
1290 assert_select "gpx_file[id='#{trace2.id}']", 1 do
1291 assert_select "tag", "Birmingham"
1295 def test_make_friend
1296 # Get users to work with
1297 user = create(:user)
1298 friend = create(:user)
1300 # Check that the users aren't already friends
1301 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1303 # When not logged in a GET should ask us to login
1304 get :make_friend, :params => { :display_name => friend.display_name }
1305 assert_redirected_to :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1307 # When not logged in a POST should error
1308 post :make_friend, :params => { :display_name => friend.display_name }
1309 assert_response :forbidden
1310 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1312 # When logged in a GET should get a confirmation page
1313 get :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1314 assert_response :success
1315 assert_template :make_friend
1316 assert_select "form" do
1317 assert_select "input[type='hidden'][name='referer']", 0
1318 assert_select "input[type='submit']", 1
1320 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1322 # When logged in a POST should add the friendship
1323 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1324 perform_enqueued_jobs do
1325 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1328 assert_redirected_to user_path(friend)
1329 assert_match(/is now your friend/, flash[:notice])
1330 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1331 email = ActionMailer::Base.deliveries.first
1332 assert_equal 1, email.to.count
1333 assert_equal friend.email, email.to.first
1334 ActionMailer::Base.deliveries.clear
1336 # A second POST should report that the friendship already exists
1337 assert_no_difference "ActionMailer::Base.deliveries.size" do
1338 perform_enqueued_jobs do
1339 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1342 assert_redirected_to user_path(friend)
1343 assert_match(/You are already friends with/, flash[:warning])
1344 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1347 def test_make_friend_with_referer
1348 # Get users to work with
1349 user = create(:user)
1350 friend = create(:user)
1352 # Check that the users aren't already friends
1353 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1355 # The GET should preserve any referer
1356 get :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1357 assert_response :success
1358 assert_template :make_friend
1359 assert_select "form" do
1360 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1361 assert_select "input[type='submit']", 1
1363 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1365 # When logged in a POST should add the friendship and refer us
1366 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1367 perform_enqueued_jobs do
1368 post :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1371 assert_redirected_to "/test"
1372 assert_match(/is now your friend/, flash[:notice])
1373 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1374 email = ActionMailer::Base.deliveries.first
1375 assert_equal 1, email.to.count
1376 assert_equal friend.email, email.to.first
1377 ActionMailer::Base.deliveries.clear
1380 def test_make_friend_unkown_user
1381 # Should error when a bogus user is specified
1382 get :make_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1383 assert_response :not_found
1384 assert_template :no_such_user
1387 def test_remove_friend
1388 # Get users to work with
1389 user = create(:user)
1390 friend = create(:user)
1391 create(:friend, :befriender => user, :befriendee => friend)
1393 # Check that the users are friends
1394 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1396 # When not logged in a GET should ask us to login
1397 get :remove_friend, :params => { :display_name => friend.display_name }
1398 assert_redirected_to :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1400 # When not logged in a POST should error
1401 post :remove_friend, :params => { :display_name => friend.display_name }
1402 assert_response :forbidden
1403 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1405 # When logged in a GET should get a confirmation page
1406 get :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1407 assert_response :success
1408 assert_template :remove_friend
1409 assert_select "form" do
1410 assert_select "input[type='hidden'][name='referer']", 0
1411 assert_select "input[type='submit']", 1
1413 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1415 # When logged in a POST should remove the friendship
1416 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1417 assert_redirected_to user_path(friend)
1418 assert_match(/was removed from your friends/, flash[:notice])
1419 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1421 # A second POST should report that the friendship does not exist
1422 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1423 assert_redirected_to user_path(friend)
1424 assert_match(/is not one of your friends/, flash[:error])
1425 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1428 def test_remove_friend_with_referer
1429 # Get users to work with
1430 user = create(:user)
1431 friend = create(:user)
1432 create(:friend, :user_id => user.id, :friend_user_id => friend.id)
1434 # Check that the users are friends
1435 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1437 # The GET should preserve any referer
1438 get :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1439 assert_response :success
1440 assert_template :remove_friend
1441 assert_select "form" do
1442 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1443 assert_select "input[type='submit']", 1
1445 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1447 # When logged in a POST should remove the friendship and refer
1448 post :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1449 assert_redirected_to "/test"
1450 assert_match(/was removed from your friends/, flash[:notice])
1451 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1454 def test_remove_friend_unkown_user
1455 # Should error when a bogus user is specified
1456 get :remove_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1457 assert_response :not_found
1458 assert_template :no_such_user
1462 user = create(:user)
1464 # Try without logging in
1465 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }
1466 assert_response :redirect
1467 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1469 # Now try as a normal user
1470 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1471 assert_response :redirect
1472 assert_redirected_to :controller => :errors, :action => :forbidden
1474 # Finally try as an administrator
1475 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1476 assert_response :redirect
1477 assert_redirected_to :action => :show, :display_name => user.display_name
1478 assert_equal "suspended", User.find(user.id).status
1482 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1484 # Try without logging in
1485 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }
1486 assert_response :redirect
1487 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1489 # Now try as a normal user
1490 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1491 assert_response :redirect
1492 assert_redirected_to :controller => :errors, :action => :forbidden
1494 # Finally try as an administrator
1495 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1496 assert_response :redirect
1497 assert_redirected_to :action => :show, :display_name => user.display_name
1499 # Check that the user was deleted properly
1501 assert_equal "user_#{user.id}", user.display_name
1502 assert_equal "", user.description
1503 assert_nil user.home_lat
1504 assert_nil user.home_lon
1505 assert_equal false, user.image.file?
1506 assert_equal false, user.email_valid
1507 assert_nil user.new_email
1508 assert_nil user.auth_provider
1509 assert_nil user.auth_uid
1510 assert_equal "deleted", user.status
1514 user = create(:user)
1515 moderator_user = create(:moderator_user)
1516 administrator_user = create(:administrator_user)
1517 _suspended_user = create(:user, :suspended)
1518 _ip_user = create(:user, :creation_ip => "1.2.3.4")
1520 # There are now 7 users - the five above, plus two extra "granters" for the
1521 # moderator_user and administrator_user
1522 assert_equal 7, User.count
1524 # Shouldn't work when not logged in
1526 assert_response :redirect
1527 assert_redirected_to :action => :login, :referer => users_path
1529 session[:user] = user.id
1531 # Shouldn't work when logged in as a normal user
1533 assert_response :redirect
1534 assert_redirected_to :controller => :errors, :action => :forbidden
1536 session[:user] = moderator_user.id
1538 # Shouldn't work when logged in as a moderator
1540 assert_response :redirect
1541 assert_redirected_to :controller => :errors, :action => :forbidden
1543 session[:user] = administrator_user.id
1545 # Note there is a header row, so all row counts are users + 1
1546 # Should work when logged in as an administrator
1548 assert_response :success
1549 assert_template :index
1550 assert_select "table#user_list tr", :count => 7 + 1
1552 # Should be able to limit by status
1553 get :index, :params => { :status => "suspended" }
1554 assert_response :success
1555 assert_template :index
1556 assert_select "table#user_list tr", :count => 1 + 1
1558 # Should be able to limit by IP address
1559 get :index, :params => { :ip => "1.2.3.4" }
1560 assert_response :success
1561 assert_template :index
1562 assert_select "table#user_list tr", :count => 1 + 1
1565 def test_index_get_paginated
1566 1.upto(100).each do |n|
1567 User.create(:display_name => "extra_#{n}",
1568 :email => "extra#{n}@example.com",
1569 :pass_crypt => "extraextra")
1572 session[:user] = create(:administrator_user).id
1574 # 100 examples, an administrator, and a granter for the admin.
1575 assert_equal 102, User.count
1578 assert_response :success
1579 assert_template :index
1580 assert_select "table#user_list tr", :count => 51
1582 get :index, :params => { :page => 2 }
1583 assert_response :success
1584 assert_template :index
1585 assert_select "table#user_list tr", :count => 51
1587 get :index, :params => { :page => 3 }
1588 assert_response :success
1589 assert_template :index
1590 assert_select "table#user_list tr", :count => 3
1593 def test_index_post_confirm
1594 inactive_user = create(:user, :pending)
1595 suspended_user = create(:user, :suspended)
1597 # Shouldn't work when not logged in
1598 assert_no_difference "User.active.count" do
1599 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1601 assert_response :forbidden
1603 assert_equal "pending", inactive_user.reload.status
1604 assert_equal "suspended", suspended_user.reload.status
1606 session[:user] = create(:user).id
1608 # Shouldn't work when logged in as a normal user
1609 assert_no_difference "User.active.count" do
1610 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1612 assert_response :redirect
1613 assert_redirected_to :controller => :errors, :action => :forbidden
1614 assert_equal "pending", inactive_user.reload.status
1615 assert_equal "suspended", suspended_user.reload.status
1617 session[:user] = create(:moderator_user).id
1619 # Shouldn't work when logged in as a moderator
1620 assert_no_difference "User.active.count" do
1621 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1623 assert_response :redirect
1624 assert_redirected_to :controller => :errors, :action => :forbidden
1625 assert_equal "pending", inactive_user.reload.status
1626 assert_equal "suspended", suspended_user.reload.status
1628 session[:user] = create(:administrator_user).id
1630 # Should work when logged in as an administrator
1631 assert_difference "User.active.count", 2 do
1632 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1634 assert_response :redirect
1635 assert_redirected_to :action => :index
1636 assert_equal "confirmed", inactive_user.reload.status
1637 assert_equal "confirmed", suspended_user.reload.status
1640 def test_index_post_hide
1641 normal_user = create(:user)
1642 confirmed_user = create(:user, :confirmed)
1644 # Shouldn't work when not logged in
1645 assert_no_difference "User.active.count" do
1646 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1648 assert_response :forbidden
1650 assert_equal "active", normal_user.reload.status
1651 assert_equal "confirmed", confirmed_user.reload.status
1653 session[:user] = create(:user).id
1655 # Shouldn't work when logged in as a normal user
1656 assert_no_difference "User.active.count" do
1657 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1659 assert_response :redirect
1660 assert_redirected_to :controller => :errors, :action => :forbidden
1661 assert_equal "active", normal_user.reload.status
1662 assert_equal "confirmed", confirmed_user.reload.status
1664 session[:user] = create(:moderator_user).id
1666 # Shouldn't work when logged in as a moderator
1667 assert_no_difference "User.active.count" do
1668 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1670 assert_response :redirect
1671 assert_redirected_to :controller => :errors, :action => :forbidden
1672 assert_equal "active", normal_user.reload.status
1673 assert_equal "confirmed", confirmed_user.reload.status
1675 session[:user] = create(:administrator_user).id
1677 # Should work when logged in as an administrator
1678 assert_difference "User.active.count", -2 do
1679 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1681 assert_response :redirect
1682 assert_redirected_to :action => :index
1683 assert_equal "deleted", normal_user.reload.status
1684 assert_equal "deleted", confirmed_user.reload.status