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 get :show, :params => { :display_name => user.display_name }
1009 assert_response :success
1010 assert_select "div#userinformation" do
1011 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1012 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1013 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1014 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1015 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1016 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1017 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1018 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1021 # Friends shouldn't be visible as we're not logged in
1022 assert_select "div#friends-container", :count => 0
1024 # Test a user who has been blocked
1025 blocked_user = create(:user)
1026 create(:user_block, :user => blocked_user)
1027 get :show, :params => { :display_name => blocked_user.display_name }
1028 assert_response :success
1029 assert_select "div#userinformation" do
1030 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
1031 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
1032 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
1033 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
1034 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
1035 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
1036 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
1037 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
1040 # Test a moderator who has applied blocks
1041 moderator_user = create(:moderator_user)
1042 create(:user_block, :creator => moderator_user)
1043 get :show, :params => { :display_name => moderator_user.display_name }
1044 assert_response :success
1045 assert_select "div#userinformation" do
1046 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
1047 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
1048 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
1049 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
1050 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
1051 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
1052 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
1053 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
1056 # Login as a normal user
1057 session[:user] = user.id
1059 # Test the normal user
1060 get :show, :params => { :display_name => user.display_name }
1061 assert_response :success
1062 assert_select "div#userinformation" do
1063 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1064 assert_select "a[href='/traces/mine']", 1
1065 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1066 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1067 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 1
1068 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1069 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1070 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1073 # Friends should be visible as we're now logged in
1074 assert_select "div#friends-container" do
1075 assert_select "div.contact-activity", :count => 1
1078 # Login as a moderator
1079 session[:user] = create(:moderator_user).id
1081 # Test the normal user
1082 get :show, :params => { :display_name => user.display_name }
1083 assert_response :success
1084 assert_select "div#userinformation" do
1085 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1086 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1087 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1088 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1089 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1090 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1091 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1092 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
1096 # Test whether information about contributor terms is shown for users who haven't agreed
1097 def test_terms_not_agreed
1098 agreed_user = create(:user, :terms_agreed => 3.days.ago)
1099 seen_user = create(:user, :terms_seen => true)
1100 not_seen_user = create(:user, :terms_seen => false)
1102 get :show, :params => { :display_name => agreed_user.display_name }
1103 assert_response :success
1104 assert_select "div#userinformation" do
1105 assert_select "p", :count => 0, :text => /Contributor terms/
1108 get :show, :params => { :display_name => seen_user.display_name }
1109 assert_response :success
1110 # put @response.body
1111 assert_select "div#userinformation" do
1112 assert_select "p", :count => 1, :text => /Contributor terms/
1113 assert_select "p", /Declined/
1116 get :show, :params => { :display_name => not_seen_user.display_name }
1117 assert_response :success
1118 assert_select "div#userinformation" do
1119 assert_select "p", :count => 1, :text => /Contributor terms/
1120 assert_select "p", /Undecided/
1125 user = create(:user, :description => "test", :terms_agreed => Date.yesterday)
1126 # check that a visible user is returned properly
1127 get :api_read, :params => { :id => user.id }
1128 assert_response :success
1129 assert_equal "text/xml", response.content_type
1131 # check the data that is returned
1132 assert_select "description", :count => 1, :text => "test"
1133 assert_select "contributor-terms", :count => 1 do
1134 assert_select "[agreed='true']"
1136 assert_select "img", :count => 0
1137 assert_select "roles", :count => 1 do
1138 assert_select "role", :count => 0
1140 assert_select "changesets", :count => 1 do
1141 assert_select "[count='0']"
1143 assert_select "traces", :count => 1 do
1144 assert_select "[count='0']"
1146 assert_select "blocks", :count => 1 do
1147 assert_select "received", :count => 1 do
1148 assert_select "[count='0'][active='0']"
1150 assert_select "issued", :count => 0
1153 # check that we aren't revealing private information
1154 assert_select "contributor-terms[pd]", false
1155 assert_select "home", false
1156 assert_select "languages", false
1157 assert_select "messages", false
1159 # check that a suspended user is not returned
1160 get :api_read, :params => { :id => create(:user, :suspended).id }
1161 assert_response :gone
1163 # check that a deleted user is not returned
1164 get :api_read, :params => { :id => create(:user, :deleted).id }
1165 assert_response :gone
1167 # check that a non-existent user is not returned
1168 get :api_read, :params => { :id => 0 }
1169 assert_response :not_found
1172 def test_api_details
1173 user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"])
1174 create(:message, :read, :recipient => user)
1175 create(:message, :sender => user)
1177 # check that nothing is returned when not logged in
1179 assert_response :unauthorized
1181 # check that we get a response when logged in
1182 basic_authorization user.email, "test"
1184 assert_response :success
1185 assert_equal "text/xml", response.content_type
1187 # check the data that is returned
1188 assert_select "description", :count => 1, :text => "test"
1189 assert_select "contributor-terms", :count => 1 do
1190 assert_select "[agreed='true'][pd='false']"
1192 assert_select "img", :count => 0
1193 assert_select "roles", :count => 1 do
1194 assert_select "role", :count => 0
1196 assert_select "changesets", :count => 1 do
1197 assert_select "[count='0']", :count => 1
1199 assert_select "traces", :count => 1 do
1200 assert_select "[count='0']", :count => 1
1202 assert_select "blocks", :count => 1 do
1203 assert_select "received", :count => 1 do
1204 assert_select "[count='0'][active='0']"
1206 assert_select "issued", :count => 0
1208 assert_select "home", :count => 1 do
1209 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
1211 assert_select "languages", :count => 1 do
1212 assert_select "lang", :count => 1, :text => "en"
1214 assert_select "messages", :count => 1 do
1215 assert_select "received", :count => 1 do
1216 assert_select "[count='1'][unread='0']"
1218 assert_select "sent", :count => 1 do
1219 assert_select "[count='1']"
1225 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
1226 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
1227 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
1229 get :api_users, :params => { :users => user1.id }
1230 assert_response :success
1231 assert_equal "text/xml", response.content_type
1232 assert_select "user", :count => 1 do
1233 assert_select "user[id='#{user1.id}']", :count => 1
1234 assert_select "user[id='#{user2.id}']", :count => 0
1235 assert_select "user[id='#{user3.id}']", :count => 0
1238 get :api_users, :params => { :users => user2.id }
1239 assert_response :success
1240 assert_equal "text/xml", response.content_type
1241 assert_select "user", :count => 1 do
1242 assert_select "user[id='#{user1.id}']", :count => 0
1243 assert_select "user[id='#{user2.id}']", :count => 1
1244 assert_select "user[id='#{user3.id}']", :count => 0
1247 get :api_users, :params => { :users => "#{user1.id},#{user3.id}" }
1248 assert_response :success
1249 assert_equal "text/xml", response.content_type
1250 assert_select "user", :count => 2 do
1251 assert_select "user[id='#{user1.id}']", :count => 1
1252 assert_select "user[id='#{user2.id}']", :count => 0
1253 assert_select "user[id='#{user3.id}']", :count => 1
1256 get :api_users, :params => { :users => create(:user, :suspended).id }
1257 assert_response :not_found
1259 get :api_users, :params => { :users => create(:user, :deleted).id }
1260 assert_response :not_found
1262 get :api_users, :params => { :users => 0 }
1263 assert_response :not_found
1266 def test_api_gpx_files
1267 user = create(:user)
1268 trace1 = create(:trace, :user => user) do |trace|
1269 create(:tracetag, :trace => trace, :tag => "London")
1271 trace2 = create(:trace, :user => user) do |trace|
1272 create(:tracetag, :trace => trace, :tag => "Birmingham")
1274 # check that nothing is returned when not logged in
1276 assert_response :unauthorized
1278 # check that we get a response when logged in
1279 basic_authorization user.email, "test"
1281 assert_response :success
1282 assert_equal "application/xml", response.content_type
1284 # check the data that is returned
1285 assert_select "gpx_file[id='#{trace1.id}']", 1 do
1286 assert_select "tag", "London"
1288 assert_select "gpx_file[id='#{trace2.id}']", 1 do
1289 assert_select "tag", "Birmingham"
1293 def test_make_friend
1294 # Get users to work with
1295 user = create(:user)
1296 friend = create(:user)
1298 # Check that the users aren't already friends
1299 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1301 # When not logged in a GET should ask us to login
1302 get :make_friend, :params => { :display_name => friend.display_name }
1303 assert_redirected_to :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1305 # When not logged in a POST should error
1306 post :make_friend, :params => { :display_name => friend.display_name }
1307 assert_response :forbidden
1308 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1310 # When logged in a GET should get a confirmation page
1311 get :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1312 assert_response :success
1313 assert_template :make_friend
1314 assert_select "form" do
1315 assert_select "input[type='hidden'][name='referer']", 0
1316 assert_select "input[type='submit']", 1
1318 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1320 # When logged in a POST should add the friendship
1321 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1322 perform_enqueued_jobs do
1323 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1326 assert_redirected_to user_path(friend)
1327 assert_match(/is now your friend/, flash[:notice])
1328 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1329 email = ActionMailer::Base.deliveries.first
1330 assert_equal 1, email.to.count
1331 assert_equal friend.email, email.to.first
1332 ActionMailer::Base.deliveries.clear
1334 # A second POST should report that the friendship already exists
1335 assert_no_difference "ActionMailer::Base.deliveries.size" do
1336 perform_enqueued_jobs do
1337 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1340 assert_redirected_to user_path(friend)
1341 assert_match(/You are already friends with/, flash[:warning])
1342 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1345 def test_make_friend_with_referer
1346 # Get users to work with
1347 user = create(:user)
1348 friend = create(:user)
1350 # Check that the users aren't already friends
1351 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1353 # The GET should preserve any referer
1354 get :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1355 assert_response :success
1356 assert_template :make_friend
1357 assert_select "form" do
1358 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1359 assert_select "input[type='submit']", 1
1361 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1363 # When logged in a POST should add the friendship and refer us
1364 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1365 perform_enqueued_jobs do
1366 post :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1369 assert_redirected_to "/test"
1370 assert_match(/is now your friend/, flash[:notice])
1371 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1372 email = ActionMailer::Base.deliveries.first
1373 assert_equal 1, email.to.count
1374 assert_equal friend.email, email.to.first
1375 ActionMailer::Base.deliveries.clear
1378 def test_make_friend_unkown_user
1379 # Should error when a bogus user is specified
1380 get :make_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1381 assert_response :not_found
1382 assert_template :no_such_user
1385 def test_remove_friend
1386 # Get users to work with
1387 user = create(:user)
1388 friend = create(:user)
1389 create(:friend, :befriender => user, :befriendee => friend)
1391 # Check that the users are friends
1392 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1394 # When not logged in a GET should ask us to login
1395 get :remove_friend, :params => { :display_name => friend.display_name }
1396 assert_redirected_to :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1398 # When not logged in a POST should error
1399 post :remove_friend, :params => { :display_name => friend.display_name }
1400 assert_response :forbidden
1401 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1403 # When logged in a GET should get a confirmation page
1404 get :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1405 assert_response :success
1406 assert_template :remove_friend
1407 assert_select "form" do
1408 assert_select "input[type='hidden'][name='referer']", 0
1409 assert_select "input[type='submit']", 1
1411 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1413 # When logged in a POST should remove the friendship
1414 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1415 assert_redirected_to user_path(friend)
1416 assert_match(/was removed from your friends/, flash[:notice])
1417 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1419 # A second POST should report that the friendship does not exist
1420 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1421 assert_redirected_to user_path(friend)
1422 assert_match(/is not one of your friends/, flash[:error])
1423 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1426 def test_remove_friend_with_referer
1427 # Get users to work with
1428 user = create(:user)
1429 friend = create(:user)
1430 create(:friend, :user_id => user.id, :friend_user_id => friend.id)
1432 # Check that the users are friends
1433 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1435 # The GET should preserve any referer
1436 get :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1437 assert_response :success
1438 assert_template :remove_friend
1439 assert_select "form" do
1440 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1441 assert_select "input[type='submit']", 1
1443 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1445 # When logged in a POST should remove the friendship and refer
1446 post :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1447 assert_redirected_to "/test"
1448 assert_match(/was removed from your friends/, flash[:notice])
1449 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1452 def test_remove_friend_unkown_user
1453 # Should error when a bogus user is specified
1454 get :remove_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1455 assert_response :not_found
1456 assert_template :no_such_user
1460 user = create(:user)
1462 # Try without logging in
1463 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }
1464 assert_response :redirect
1465 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1467 # Now try as a normal user
1468 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1469 assert_response :redirect
1470 assert_redirected_to :action => :show, :display_name => user.display_name
1472 # Finally try as an administrator
1473 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1474 assert_response :redirect
1475 assert_redirected_to :action => :show, :display_name => user.display_name
1476 assert_equal "suspended", User.find(user.id).status
1480 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1482 # Try without logging in
1483 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }
1484 assert_response :redirect
1485 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1487 # Now try as a normal user
1488 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1489 assert_response :redirect
1490 assert_redirected_to :action => :show, :display_name => user.display_name
1492 # Finally try as an administrator
1493 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1494 assert_response :redirect
1495 assert_redirected_to :action => :show, :display_name => user.display_name
1497 # Check that the user was deleted properly
1499 assert_equal "user_#{user.id}", user.display_name
1500 assert_equal "", user.description
1501 assert_nil user.home_lat
1502 assert_nil user.home_lon
1503 assert_equal false, user.image.file?
1504 assert_equal false, user.email_valid
1505 assert_nil user.new_email
1506 assert_nil user.auth_provider
1507 assert_nil user.auth_uid
1508 assert_equal "deleted", user.status
1512 user = create(:user)
1513 moderator_user = create(:moderator_user)
1514 administrator_user = create(:administrator_user)
1515 _suspended_user = create(:user, :suspended)
1516 _ip_user = create(:user, :creation_ip => "1.2.3.4")
1518 # There are now 7 users - the five above, plus two extra "granters" for the
1519 # moderator_user and administrator_user
1520 assert_equal 7, User.count
1522 # Shouldn't work when not logged in
1524 assert_response :redirect
1525 assert_redirected_to :action => :login, :referer => users_path
1527 session[:user] = user.id
1529 # Shouldn't work when logged in as a normal user
1531 assert_response :redirect
1532 assert_redirected_to :action => :login, :referer => users_path
1534 session[:user] = moderator_user.id
1536 # Shouldn't work when logged in as a moderator
1538 assert_response :redirect
1539 assert_redirected_to :action => :login, :referer => users_path
1541 session[:user] = administrator_user.id
1543 # Note there is a header row, so all row counts are users + 1
1544 # Should work when logged in as an administrator
1546 assert_response :success
1547 assert_template :index
1548 assert_select "table#user_list tr", :count => 7 + 1
1550 # Should be able to limit by status
1551 get :index, :params => { :status => "suspended" }
1552 assert_response :success
1553 assert_template :index
1554 assert_select "table#user_list tr", :count => 1 + 1
1556 # Should be able to limit by IP address
1557 get :index, :params => { :ip => "1.2.3.4" }
1558 assert_response :success
1559 assert_template :index
1560 assert_select "table#user_list tr", :count => 1 + 1
1563 def test_index_get_paginated
1564 1.upto(100).each do |n|
1565 User.create(:display_name => "extra_#{n}",
1566 :email => "extra#{n}@example.com",
1567 :pass_crypt => "extraextra")
1570 session[:user] = create(:administrator_user).id
1572 # 100 examples, an administrator, and a granter for the admin.
1573 assert_equal 102, User.count
1576 assert_response :success
1577 assert_template :index
1578 assert_select "table#user_list tr", :count => 51
1580 get :index, :params => { :page => 2 }
1581 assert_response :success
1582 assert_template :index
1583 assert_select "table#user_list tr", :count => 51
1585 get :index, :params => { :page => 3 }
1586 assert_response :success
1587 assert_template :index
1588 assert_select "table#user_list tr", :count => 3
1591 def test_index_post_confirm
1592 inactive_user = create(:user, :pending)
1593 suspended_user = create(:user, :suspended)
1595 # Shouldn't work when not logged in
1596 assert_no_difference "User.active.count" do
1597 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1599 assert_response :redirect
1600 assert_redirected_to :action => :login, :referer => users_path
1601 assert_equal "pending", inactive_user.reload.status
1602 assert_equal "suspended", suspended_user.reload.status
1604 session[:user] = create(:user).id
1606 # Shouldn't work when logged in as a normal user
1607 assert_no_difference "User.active.count" do
1608 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1610 assert_response :redirect
1611 assert_redirected_to :action => :login, :referer => users_path
1612 assert_equal "pending", inactive_user.reload.status
1613 assert_equal "suspended", suspended_user.reload.status
1615 session[:user] = create(:moderator_user).id
1617 # Shouldn't work when logged in as a moderator
1618 assert_no_difference "User.active.count" do
1619 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1621 assert_response :redirect
1622 assert_redirected_to :action => :login, :referer => users_path
1623 assert_equal "pending", inactive_user.reload.status
1624 assert_equal "suspended", suspended_user.reload.status
1626 session[:user] = create(:administrator_user).id
1628 # Should work when logged in as an administrator
1629 assert_difference "User.active.count", 2 do
1630 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1632 assert_response :redirect
1633 assert_redirected_to :action => :index
1634 assert_equal "confirmed", inactive_user.reload.status
1635 assert_equal "confirmed", suspended_user.reload.status
1638 def test_index_post_hide
1639 normal_user = create(:user)
1640 confirmed_user = create(:user, :confirmed)
1642 # Shouldn't work when not logged in
1643 assert_no_difference "User.active.count" do
1644 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1646 assert_response :redirect
1647 assert_redirected_to :action => :login, :referer => users_path
1648 assert_equal "active", normal_user.reload.status
1649 assert_equal "confirmed", confirmed_user.reload.status
1651 session[:user] = create(:user).id
1653 # Shouldn't work when logged in as a normal user
1654 assert_no_difference "User.active.count" do
1655 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1657 assert_response :redirect
1658 assert_redirected_to :action => :login, :referer => users_path
1659 assert_equal "active", normal_user.reload.status
1660 assert_equal "confirmed", confirmed_user.reload.status
1662 session[:user] = create(:moderator_user).id
1664 # Shouldn't work when logged in as a moderator
1665 assert_no_difference "User.active.count" do
1666 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1668 assert_response :redirect
1669 assert_redirected_to :action => :login, :referer => users_path
1670 assert_equal "active", normal_user.reload.status
1671 assert_equal "confirmed", confirmed_user.reload.status
1673 session[:user] = create(:administrator_user).id
1675 # Should work when logged in as an administrator
1676 assert_difference "User.active.count", -2 do
1677 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1679 assert_response :redirect
1680 assert_redirected_to :action => :index
1681 assert_equal "deleted", normal_user.reload.status
1682 assert_equal "deleted", confirmed_user.reload.status