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_save_referer_params
327 user = build(:user, :pending)
329 assert_difference "User.count", 1 do
330 assert_difference "ActionMailer::Base.deliveries.size", 1 do
331 perform_enqueued_jobs do
332 post :save, :session => { :new_user => user,
333 :referer => "/edit?editor=id#map=1/2/3" }
338 assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3),
339 user.tokens.order("id DESC").first.referer
341 ActionMailer::Base.deliveries.clear
344 def test_logout_without_referer
346 assert_response :success
347 assert_template :logout
348 assert_select "input[name=referer][value=?]", ""
350 session_id = assert_select("input[name=session]").first["value"]
352 get :logout, :params => { :session => session_id }
353 assert_response :redirect
354 assert_redirected_to root_path
357 def test_logout_with_referer
358 get :logout, :params => { :referer => "/test" }
359 assert_response :success
360 assert_template :logout
361 assert_select "input[name=referer][value=?]", "/test"
363 session_id = assert_select("input[name=session]").first["value"]
365 get :logout, :params => { :session => session_id, :referer => "/test" }
366 assert_response :redirect
367 assert_redirected_to "/test"
370 def test_logout_with_token
371 token = create(:user).tokens.create
373 session[:token] = token.token
376 assert_response :success
377 assert_template :logout
378 assert_select "input[name=referer][value=?]", ""
379 assert_equal token.token, session[:token]
380 assert_not_nil UserToken.where(:id => token.id).first
382 session_id = assert_select("input[name=session]").first["value"]
384 get :logout, :params => { :session => session_id }
385 assert_response :redirect
386 assert_redirected_to root_path
387 assert_nil session[:token]
388 assert_nil UserToken.where(:id => token.id).first
392 user = create(:user, :pending)
393 confirm_string = user.tokens.create.token
395 @request.cookies["_osm_session"] = user.display_name
396 get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
397 assert_response :success
398 assert_template :confirm
401 def test_confirm_get_already_confirmed
403 confirm_string = user.tokens.create.token
405 @request.cookies["_osm_session"] = user.display_name
406 get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
407 assert_response :redirect
408 assert_redirected_to root_path
411 def test_confirm_success_no_token_no_referer
412 user = create(:user, :pending)
413 stub_gravatar_request(user.email)
414 confirm_string = user.tokens.create.token
416 @request.cookies["_osm_session"] = user.display_name
417 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
418 assert_redirected_to login_path
419 assert_match(/Confirmed your account/, flash[:notice])
422 def test_confirm_success_good_token_no_referer
423 user = create(:user, :pending)
424 stub_gravatar_request(user.email)
425 confirm_string = user.tokens.create.token
426 token = user.tokens.create.token
428 @request.cookies["_osm_session"] = user.display_name
429 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
430 assert_redirected_to welcome_path
433 def test_confirm_success_bad_token_no_referer
434 user = create(:user, :pending)
435 stub_gravatar_request(user.email)
436 confirm_string = user.tokens.create.token
437 token = create(:user).tokens.create.token
439 @request.cookies["_osm_session"] = user.display_name
440 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
441 assert_redirected_to login_path
442 assert_match(/Confirmed your account/, flash[:notice])
445 def test_confirm_success_no_token_with_referer
446 user = create(:user, :pending)
447 stub_gravatar_request(user.email)
448 confirm_string = user.tokens.create(:referer => diary_new_path).token
450 @request.cookies["_osm_session"] = user.display_name
451 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
452 assert_redirected_to login_path(:referer => diary_new_path)
453 assert_match(/Confirmed your account/, flash[:notice])
456 def test_confirm_success_good_token_with_referer
457 user = create(:user, :pending)
458 stub_gravatar_request(user.email)
459 confirm_string = user.tokens.create(:referer => diary_new_path).token
460 token = user.tokens.create.token
462 @request.cookies["_osm_session"] = user.display_name
463 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
464 assert_redirected_to diary_new_path
467 def test_confirm_success_bad_token_with_referer
468 user = create(:user, :pending)
469 stub_gravatar_request(user.email)
470 confirm_string = user.tokens.create(:referer => diary_new_path).token
471 token = create(:user).tokens.create.token
473 @request.cookies["_osm_session"] = user.display_name
474 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token }
475 assert_redirected_to login_path(:referer => diary_new_path)
476 assert_match(/Confirmed your account/, flash[:notice])
479 def test_confirm_expired_token
480 user = create(:user, :pending)
481 confirm_string = user.tokens.create(:expiry => 1.day.ago).token
483 @request.cookies["_osm_session"] = user.display_name
484 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
485 assert_redirected_to :action => "confirm"
486 assert_match(/confirmation code has expired/, flash[:error])
489 def test_confirm_already_confirmed
491 confirm_string = user.tokens.create(:referer => diary_new_path).token
493 @request.cookies["_osm_session"] = user.display_name
494 post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }
495 assert_redirected_to :action => "login"
496 assert_match(/already been confirmed/, flash[:error])
499 def test_confirm_resend_success
500 user = create(:user, :pending)
501 session[:token] = user.tokens.create.token
503 assert_difference "ActionMailer::Base.deliveries.size", 1 do
504 perform_enqueued_jobs do
505 get :confirm_resend, :params => { :display_name => user.display_name }
509 assert_response :redirect
510 assert_redirected_to login_path
511 assert_match(/sent a new confirmation/, flash[:notice])
513 email = ActionMailer::Base.deliveries.last
515 assert_equal user.email, email.to.first
517 ActionMailer::Base.deliveries.clear
520 def test_confirm_resend_no_token
521 user = create(:user, :pending)
522 assert_no_difference "ActionMailer::Base.deliveries.size" do
523 perform_enqueued_jobs do
524 get :confirm_resend, :params => { :display_name => user.display_name }
528 assert_response :redirect
529 assert_redirected_to login_path
530 assert_match "User #{user.display_name} not found.", flash[:error]
533 def test_confirm_resend_unknown_user
534 assert_no_difference "ActionMailer::Base.deliveries.size" do
535 perform_enqueued_jobs do
536 get :confirm_resend, :params => { :display_name => "No Such User" }
540 assert_response :redirect
541 assert_redirected_to login_path
542 assert_match "User No Such User not found.", flash[:error]
545 def test_confirm_email_get
547 confirm_string = user.tokens.create.token
549 get :confirm_email, :params => { :confirm_string => confirm_string }
550 assert_response :success
551 assert_template :confirm_email
554 def test_confirm_email_success
555 user = create(:user, :new_email => "test-new@example.com")
556 stub_gravatar_request(user.new_email)
557 confirm_string = user.tokens.create.token
559 post :confirm_email, :params => { :confirm_string => confirm_string }
560 assert_response :redirect
561 assert_redirected_to :action => :account, :display_name => user.display_name
562 assert_match(/Confirmed your change of email address/, flash[:notice])
565 def test_confirm_email_already_confirmed
567 confirm_string = user.tokens.create.token
569 post :confirm_email, :params => { :confirm_string => confirm_string }
570 assert_response :redirect
571 assert_redirected_to :action => :account, :display_name => user.display_name
572 assert_match(/already been confirmed/, flash[:error])
575 def test_confirm_email_bad_token
576 post :confirm_email, :params => { :confirm_string => "XXXXX" }
577 assert_response :success
578 assert_template :confirm_email
579 assert_match(/confirmation code has expired or does not exist/, flash[:error])
583 # test if testing for a gravatar works
584 # this happens when the email is actually changed
585 # which is triggered by the confirmation mail
586 def test_gravatar_auto_enable
587 # switch to email that has a gravatar
588 user = create(:user, :new_email => "test-new@example.com")
589 stub_gravatar_request(user.new_email, 200)
590 confirm_string = user.tokens.create.token
591 # precondition gravatar should be turned off
592 assert_not user.image_use_gravatar
593 post :confirm_email, :params => { :confirm_string => confirm_string }
594 assert_response :redirect
595 assert_redirected_to :action => :account, :display_name => user.display_name
596 assert_match(/Confirmed your change of email address/, flash[:notice])
597 # gravatar use should now be enabled
598 assert User.find(user.id).image_use_gravatar
601 def test_gravatar_auto_disable
602 # switch to email without a gravatar
603 user = create(:user, :new_email => "test-new@example.com", :image_use_gravatar => true)
604 stub_gravatar_request(user.new_email, 404)
605 confirm_string = user.tokens.create.token
606 # precondition gravatar should be turned on
607 assert user.image_use_gravatar
608 post :confirm_email, :params => { :confirm_string => confirm_string }
609 assert_response :redirect
610 assert_redirected_to :action => :account, :display_name => user.display_name
611 assert_match(/Confirmed your change of email address/, flash[:notice])
612 # gravatar use should now be disabled
613 assert_not User.find(user.id).image_use_gravatar
616 def test_terms_new_user
617 get :terms, :session => { :new_user => User.new }
618 assert_response :success
619 assert_template :terms
622 def test_terms_agreed
623 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
625 session[:user] = user.id
628 assert_response :redirect
629 assert_redirected_to :action => :account, :display_name => user.display_name
632 def test_terms_not_seen_without_referer
633 user = create(:user, :terms_seen => false)
635 session[:user] = user.id
638 assert_response :success
639 assert_template :terms
641 post :save, :params => { :user => { :consider_pd => true } }
642 assert_response :redirect
643 assert_redirected_to :action => :account, :display_name => user.display_name
644 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
648 assert_equal true, user.consider_pd
649 assert_not_nil user.terms_agreed
650 assert_equal true, user.terms_seen
653 def test_terms_not_seen_with_referer
654 user = create(:user, :terms_seen => false)
656 session[:user] = user.id
658 get :terms, :params => { :referer => "/test" }
659 assert_response :success
660 assert_template :terms
662 post :save, :params => { :user => { :consider_pd => true }, :referer => "/test" }
663 assert_response :redirect
664 assert_redirected_to "/test"
665 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
669 assert_equal true, user.consider_pd
670 assert_not_nil user.terms_agreed
671 assert_equal true, user.terms_seen
674 # Check that if you haven't seen the terms, and make a request that requires authentication,
675 # that your request is redirected to view the terms
676 def test_terms_not_seen_redirection
677 user = create(:user, :terms_seen => false)
678 session[:user] = user.id
680 get :account, :params => { :display_name => user.display_name }
681 assert_response :redirect
682 assert_redirected_to :action => :terms, :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
686 user = create(:user, :data_public => false)
687 post :go_public, :session => { :user => user }
688 assert_response :redirect
689 assert_redirected_to :action => :account, :display_name => user.display_name
690 assert_equal true, User.find(user.id).data_public
693 def test_lost_password
694 # Test fetching the lost password page
696 assert_response :success
697 assert_template :lost_password
698 assert_select "div#notice", false
700 # Test resetting using the address as recorded for a user that has an
701 # address which is duplicated in a different case by another user
703 uppercase_user = build(:user, :email => user.email.upcase).tap { |u| u.save(:validate => false) }
705 assert_difference "ActionMailer::Base.deliveries.size", 1 do
706 perform_enqueued_jobs do
707 post :lost_password, :params => { :user => { :email => user.email } }
710 assert_response :redirect
711 assert_redirected_to :action => :login
712 assert_match(/^Sorry you lost it/, flash[:notice])
713 email = ActionMailer::Base.deliveries.first
714 assert_equal 1, email.to.count
715 assert_equal user.email, email.to.first
716 ActionMailer::Base.deliveries.clear
718 # Test resetting using an address that matches a different user
719 # that has the same address in a different case
720 assert_difference "ActionMailer::Base.deliveries.size", 1 do
721 perform_enqueued_jobs do
722 post :lost_password, :params => { :user => { :email => user.email.upcase } }
725 assert_response :redirect
726 assert_redirected_to :action => :login
727 assert_match(/^Sorry you lost it/, flash[:notice])
728 email = ActionMailer::Base.deliveries.first
729 assert_equal 1, email.to.count
730 assert_equal uppercase_user.email, email.to.first
731 ActionMailer::Base.deliveries.clear
733 # Test resetting using an address that is a case insensitive match
734 # for more than one user but not an exact match for either
735 assert_no_difference "ActionMailer::Base.deliveries.size" do
736 perform_enqueued_jobs do
737 post :lost_password, :params => { :user => { :email => user.email.titlecase } }
740 assert_response :success
741 assert_template :lost_password
742 assert_select ".error", /^Could not find that email address/
744 # Test resetting using the address as recorded for a user that has an
745 # address which is case insensitively unique
746 third_user = create(:user)
747 assert_difference "ActionMailer::Base.deliveries.size", 1 do
748 perform_enqueued_jobs do
749 post :lost_password, :params => { :user => { :email => third_user.email } }
752 assert_response :redirect
753 assert_redirected_to :action => :login
754 assert_match(/^Sorry you lost it/, flash[:notice])
755 email = ActionMailer::Base.deliveries.first
756 assert_equal 1, email.to.count
757 assert_equal third_user.email, email.to.first
758 ActionMailer::Base.deliveries.clear
760 # Test resetting using an address that matches a user that has the
761 # same (case insensitively unique) address in a different case
762 assert_difference "ActionMailer::Base.deliveries.size", 1 do
763 perform_enqueued_jobs do
764 post :lost_password, :params => { :user => { :email => third_user.email.upcase } }
767 assert_response :redirect
768 assert_redirected_to :action => :login
769 assert_match(/^Sorry you lost it/, flash[:notice])
770 email = ActionMailer::Base.deliveries.first
771 assert_equal 1, email.to.count
772 assert_equal third_user.email, email.to.first
773 ActionMailer::Base.deliveries.clear
776 def test_reset_password
777 user = create(:user, :pending)
778 # Test a request with no token
780 assert_response :bad_request
782 # Test a request with a bogus token
783 get :reset_password, :params => { :token => "made_up_token" }
784 assert_response :redirect
785 assert_redirected_to :action => :lost_password
787 # Create a valid token for a user
788 token = user.tokens.create
790 # Test a request with a valid token
791 get :reset_password, :params => { :token => token.token }
792 assert_response :success
793 assert_template :reset_password
795 # Test that errors are reported for erroneous submissions
796 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "different_password" } }
797 assert_response :success
798 assert_template :reset_password
799 assert_select "div#errorExplanation"
801 # Test setting a new password
802 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" } }
803 assert_response :redirect
804 assert_redirected_to root_path
805 assert_equal user.id, session[:user]
807 assert_equal "active", user.status
808 assert_equal true, user.email_valid
809 assert_equal user, User.authenticate(:username => user.email, :password => "new_password")
813 # Get a user to work with - note that this user deliberately
814 # conflicts with uppercase_user in the email and display name
815 # fields to test that we can change other fields without any
816 # validation errors being reported
817 user = create(:user, :languages => [])
818 _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
820 # Make sure that you are redirected to the login page when
821 # you are not logged in
822 get :account, :params => { :display_name => user.display_name }
823 assert_response :redirect
824 assert_redirected_to :action => "login", :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
826 # Make sure that you are blocked when not logged in as the right user
827 get :account, :params => { :display_name => user.display_name }, :session => { :user => create(:user) }
828 assert_response :forbidden
830 # Make sure we get the page when we are logged in as the right user
831 get :account, :params => { :display_name => user.display_name }, :session => { :user => user }
832 assert_response :success
833 assert_template :account
834 assert_select "form#accountForm" do |form|
835 assert_equal "post", form.attr("method").to_s
836 assert_select "input[name='_method']", false
837 assert_equal "/user/#{ERB::Util.u(user.display_name)}/account", form.attr("action").to_s
840 # Updating the description should work
841 user.description = "new description"
842 user.preferred_editor = "default"
843 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
844 assert_response :success
845 assert_template :account
846 assert_select "div#errorExplanation", false
847 assert_select ".notice", /^User information updated successfully/
848 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
850 # Changing to a invalid editor should fail
851 user.preferred_editor = "unknown"
852 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
853 assert_response :success
854 assert_template :account
855 assert_select ".notice", false
856 assert_select "div#errorExplanation"
857 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
859 # Changing to a valid editor should work
860 user.preferred_editor = "potlatch2"
861 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
862 assert_response :success
863 assert_template :account
864 assert_select "div#errorExplanation", false
865 assert_select ".notice", /^User information updated successfully/
866 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
868 # Changing to the default editor should work
869 user.preferred_editor = "default"
870 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
871 assert_response :success
872 assert_template :account
873 assert_select "div#errorExplanation", false
874 assert_select ".notice", /^User information updated successfully/
875 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
877 # Changing to an uploaded image should work
878 image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif")
879 post :account, :params => { :display_name => user.display_name, :image_action => "new", :user => user.attributes.merge(:image => image) }, :session => { :user => user }
880 assert_response :success
881 assert_template :account
882 assert_select "div#errorExplanation", false
883 assert_select ".notice", /^User information updated successfully/
884 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "keep"
886 # Changing to a gravatar image should work
887 post :account, :params => { :display_name => user.display_name, :image_action => "gravatar", :user => user.attributes }, :session => { :user => user }
888 assert_response :success
889 assert_template :account
890 assert_select "div#errorExplanation", false
891 assert_select ".notice", /^User information updated successfully/
892 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "gravatar"
894 # Removing the image should work
895 post :account, :params => { :display_name => user.display_name, :image_action => "delete", :user => user.attributes }, :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]", false
902 # Adding external authentication should redirect to the auth provider
903 post :account, :params => { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, :session => { :user => user }
904 assert_response :redirect
905 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")
907 # Changing name to one that exists should fail
908 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
909 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
910 assert_response :success
911 assert_template :account
912 assert_select ".notice", false
913 assert_select "div#errorExplanation"
914 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
916 # Changing name to one that exists should fail, regardless of case
917 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
918 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
919 assert_response :success
920 assert_template :account
921 assert_select ".notice", false
922 assert_select "div#errorExplanation"
923 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
925 # Changing name to one that doesn't exist should work
926 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
927 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
928 assert_response :success
929 assert_template :account
930 assert_select "div#errorExplanation", false
931 assert_select ".notice", /^User information updated successfully/
932 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
934 # Record the change of name
935 user.display_name = "new tester"
937 # Changing email to one that exists should fail
938 user.new_email = create(:user).email
939 assert_no_difference "ActionMailer::Base.deliveries.size" do
940 perform_enqueued_jobs do
941 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
944 assert_response :success
945 assert_template :account
946 assert_select ".notice", false
947 assert_select "div#errorExplanation"
948 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
950 # Changing email to one that exists should fail, regardless of case
951 user.new_email = create(:user).email.upcase
952 assert_no_difference "ActionMailer::Base.deliveries.size" do
953 perform_enqueued_jobs do
954 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
957 assert_response :success
958 assert_template :account
959 assert_select ".notice", false
960 assert_select "div#errorExplanation"
961 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
963 # Changing email to one that doesn't exist should work
964 user.new_email = "new_tester@example.com"
965 assert_difference "ActionMailer::Base.deliveries.size", 1 do
966 perform_enqueued_jobs do
967 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
970 assert_response :success
971 assert_template :account
972 assert_select "div#errorExplanation", false
973 assert_select ".notice", /^User information updated successfully/
974 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
975 email = ActionMailer::Base.deliveries.first
976 assert_equal 1, email.to.count
977 assert_equal user.new_email, email.to.first
978 ActionMailer::Base.deliveries.clear
981 # Check that the user account page will display and contains some relevant
982 # information for the user
984 # Test a non-existent user
985 get :show, :params => { :display_name => "unknown" }
986 assert_response :not_found
989 user = create(:user, :home_lon => 1.1, :home_lat => 1.1)
990 friend_user = create(:user, :home_lon => 1.2, :home_lat => 1.2)
991 create(:friend, :befriender => user, :befriendee => friend_user)
992 get :show, :params => { :display_name => user.display_name }
993 assert_response :success
994 assert_select "div#userinformation" do
995 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
996 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
997 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
998 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
999 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1000 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1001 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1002 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1005 # Friends shouldn't be visible as we're not logged in
1006 assert_select "div#friends-container", :count => 0
1008 # Test a user who has been blocked
1009 blocked_user = create(:user)
1010 create(:user_block, :user => blocked_user)
1011 get :show, :params => { :display_name => blocked_user.display_name }
1012 assert_response :success
1013 assert_select "div#userinformation" do
1014 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
1015 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
1016 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
1017 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
1018 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
1019 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
1020 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
1021 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
1024 # Test a moderator who has applied blocks
1025 moderator_user = create(:moderator_user)
1026 create(:user_block, :creator => moderator_user)
1027 get :show, :params => { :display_name => moderator_user.display_name }
1028 assert_response :success
1029 assert_select "div#userinformation" do
1030 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
1031 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
1032 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
1033 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
1034 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
1035 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
1036 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
1037 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
1040 # Login as a normal user
1041 session[:user] = user.id
1043 # Test the normal user
1044 get :show, :params => { :display_name => user.display_name }
1045 assert_response :success
1046 assert_select "div#userinformation" do
1047 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1048 assert_select "a[href='/traces/mine']", 1
1049 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1050 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1051 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 1
1052 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1053 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1054 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1057 # Friends should be visible as we're now logged in
1058 assert_select "div#friends-container" do
1059 assert_select "div.contact-activity", :count => 1
1062 # Login as a moderator
1063 session[:user] = create(:moderator_user).id
1065 # Test the normal user
1066 get :show, :params => { :display_name => user.display_name }
1067 assert_response :success
1068 assert_select "div#userinformation" do
1069 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1070 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1071 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1072 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1073 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1074 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1075 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1076 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
1080 # Test whether information about contributor terms is shown for users who haven't agreed
1081 def test_terms_not_agreed
1082 agreed_user = create(:user, :terms_agreed => 3.days.ago)
1083 seen_user = create(:user, :terms_seen => true)
1084 not_seen_user = create(:user, :terms_seen => false)
1086 get :show, :params => { :display_name => agreed_user.display_name }
1087 assert_response :success
1088 assert_select "div#userinformation" do
1089 assert_select "p", :count => 0, :text => /Contributor terms/
1092 get :show, :params => { :display_name => seen_user.display_name }
1093 assert_response :success
1094 # put @response.body
1095 assert_select "div#userinformation" do
1096 assert_select "p", :count => 1, :text => /Contributor terms/
1097 assert_select "p", /Declined/
1100 get :show, :params => { :display_name => not_seen_user.display_name }
1101 assert_response :success
1102 assert_select "div#userinformation" do
1103 assert_select "p", :count => 1, :text => /Contributor terms/
1104 assert_select "p", /Undecided/
1109 user = create(:user, :description => "test", :terms_agreed => Date.yesterday)
1110 # check that a visible user is returned properly
1111 get :api_read, :params => { :id => user.id }
1112 assert_response :success
1113 assert_equal "text/xml", response.content_type
1115 # check the data that is returned
1116 assert_select "description", :count => 1, :text => "test"
1117 assert_select "contributor-terms", :count => 1 do
1118 assert_select "[agreed='true']"
1120 assert_select "img", :count => 0
1121 assert_select "roles", :count => 1 do
1122 assert_select "role", :count => 0
1124 assert_select "changesets", :count => 1 do
1125 assert_select "[count='0']"
1127 assert_select "traces", :count => 1 do
1128 assert_select "[count='0']"
1130 assert_select "blocks", :count => 1 do
1131 assert_select "received", :count => 1 do
1132 assert_select "[count='0'][active='0']"
1134 assert_select "issued", :count => 0
1137 # check that we aren't revealing private information
1138 assert_select "contributor-terms[pd]", false
1139 assert_select "home", false
1140 assert_select "languages", false
1141 assert_select "messages", false
1143 # check that a suspended user is not returned
1144 get :api_read, :params => { :id => create(:user, :suspended).id }
1145 assert_response :gone
1147 # check that a deleted user is not returned
1148 get :api_read, :params => { :id => create(:user, :deleted).id }
1149 assert_response :gone
1151 # check that a non-existent user is not returned
1152 get :api_read, :params => { :id => 0 }
1153 assert_response :not_found
1156 def test_api_details
1157 user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"])
1158 create(:message, :read, :recipient => user)
1159 create(:message, :sender => user)
1161 # check that nothing is returned when not logged in
1163 assert_response :unauthorized
1165 # check that we get a response when logged in
1166 basic_authorization user.email, "test"
1168 assert_response :success
1169 assert_equal "text/xml", response.content_type
1171 # check the data that is returned
1172 assert_select "description", :count => 1, :text => "test"
1173 assert_select "contributor-terms", :count => 1 do
1174 assert_select "[agreed='true'][pd='false']"
1176 assert_select "img", :count => 0
1177 assert_select "roles", :count => 1 do
1178 assert_select "role", :count => 0
1180 assert_select "changesets", :count => 1 do
1181 assert_select "[count='0']", :count => 1
1183 assert_select "traces", :count => 1 do
1184 assert_select "[count='0']", :count => 1
1186 assert_select "blocks", :count => 1 do
1187 assert_select "received", :count => 1 do
1188 assert_select "[count='0'][active='0']"
1190 assert_select "issued", :count => 0
1192 assert_select "home", :count => 1 do
1193 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
1195 assert_select "languages", :count => 1 do
1196 assert_select "lang", :count => 1, :text => "en"
1198 assert_select "messages", :count => 1 do
1199 assert_select "received", :count => 1 do
1200 assert_select "[count='1'][unread='0']"
1202 assert_select "sent", :count => 1 do
1203 assert_select "[count='1']"
1209 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
1210 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
1211 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
1213 get :api_users, :params => { :users => user1.id }
1214 assert_response :success
1215 assert_equal "text/xml", response.content_type
1216 assert_select "user", :count => 1 do
1217 assert_select "user[id='#{user1.id}']", :count => 1
1218 assert_select "user[id='#{user2.id}']", :count => 0
1219 assert_select "user[id='#{user3.id}']", :count => 0
1222 get :api_users, :params => { :users => user2.id }
1223 assert_response :success
1224 assert_equal "text/xml", response.content_type
1225 assert_select "user", :count => 1 do
1226 assert_select "user[id='#{user1.id}']", :count => 0
1227 assert_select "user[id='#{user2.id}']", :count => 1
1228 assert_select "user[id='#{user3.id}']", :count => 0
1231 get :api_users, :params => { :users => "#{user1.id},#{user3.id}" }
1232 assert_response :success
1233 assert_equal "text/xml", response.content_type
1234 assert_select "user", :count => 2 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 => 1
1240 get :api_users, :params => { :users => create(:user, :suspended).id }
1241 assert_response :not_found
1243 get :api_users, :params => { :users => create(:user, :deleted).id }
1244 assert_response :not_found
1246 get :api_users, :params => { :users => 0 }
1247 assert_response :not_found
1250 def test_api_gpx_files
1251 user = create(:user)
1252 trace1 = create(:trace, :user => user) do |trace|
1253 create(:tracetag, :trace => trace, :tag => "London")
1255 trace2 = create(:trace, :user => user) do |trace|
1256 create(:tracetag, :trace => trace, :tag => "Birmingham")
1258 # check that nothing is returned when not logged in
1260 assert_response :unauthorized
1262 # check that we get a response when logged in
1263 basic_authorization user.email, "test"
1265 assert_response :success
1266 assert_equal "application/xml", response.content_type
1268 # check the data that is returned
1269 assert_select "gpx_file[id='#{trace1.id}']", 1 do
1270 assert_select "tag", "London"
1272 assert_select "gpx_file[id='#{trace2.id}']", 1 do
1273 assert_select "tag", "Birmingham"
1277 def test_make_friend
1278 # Get users to work with
1279 user = create(:user)
1280 friend = create(:user)
1282 # Check that the users aren't already friends
1283 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1285 # When not logged in a GET should ask us to login
1286 get :make_friend, :params => { :display_name => friend.display_name }
1287 assert_redirected_to :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1289 # When not logged in a POST should error
1290 post :make_friend, :params => { :display_name => friend.display_name }
1291 assert_response :forbidden
1292 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1294 # When logged in a GET should get a confirmation page
1295 get :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1296 assert_response :success
1297 assert_template :make_friend
1298 assert_select "form" do
1299 assert_select "input[type='hidden'][name='referer']", 0
1300 assert_select "input[type='submit']", 1
1302 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1304 # When logged in a POST should add the friendship
1305 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1306 perform_enqueued_jobs do
1307 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1310 assert_redirected_to user_path(friend)
1311 assert_match(/is now your friend/, flash[:notice])
1312 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1313 email = ActionMailer::Base.deliveries.first
1314 assert_equal 1, email.to.count
1315 assert_equal friend.email, email.to.first
1316 ActionMailer::Base.deliveries.clear
1318 # A second POST should report that the friendship already exists
1319 assert_no_difference "ActionMailer::Base.deliveries.size" do
1320 perform_enqueued_jobs do
1321 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1324 assert_redirected_to user_path(friend)
1325 assert_match(/You are already friends with/, flash[:warning])
1326 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1329 def test_make_friend_with_referer
1330 # Get users to work with
1331 user = create(:user)
1332 friend = create(:user)
1334 # Check that the users aren't already friends
1335 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1337 # The GET should preserve any referer
1338 get :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1339 assert_response :success
1340 assert_template :make_friend
1341 assert_select "form" do
1342 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1343 assert_select "input[type='submit']", 1
1345 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1347 # When logged in a POST should add the friendship and refer us
1348 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1349 perform_enqueued_jobs do
1350 post :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1353 assert_redirected_to "/test"
1354 assert_match(/is now your friend/, flash[:notice])
1355 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1356 email = ActionMailer::Base.deliveries.first
1357 assert_equal 1, email.to.count
1358 assert_equal friend.email, email.to.first
1359 ActionMailer::Base.deliveries.clear
1362 def test_make_friend_unkown_user
1363 # Should error when a bogus user is specified
1364 get :make_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1365 assert_response :not_found
1366 assert_template :no_such_user
1369 def test_remove_friend
1370 # Get users to work with
1371 user = create(:user)
1372 friend = create(:user)
1373 create(:friend, :befriender => user, :befriendee => friend)
1375 # Check that the users are friends
1376 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1378 # When not logged in a GET should ask us to login
1379 get :remove_friend, :params => { :display_name => friend.display_name }
1380 assert_redirected_to :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1382 # When not logged in a POST should error
1383 post :remove_friend, :params => { :display_name => friend.display_name }
1384 assert_response :forbidden
1385 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1387 # When logged in a GET should get a confirmation page
1388 get :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1389 assert_response :success
1390 assert_template :remove_friend
1391 assert_select "form" do
1392 assert_select "input[type='hidden'][name='referer']", 0
1393 assert_select "input[type='submit']", 1
1395 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1397 # When logged in a POST should remove the friendship
1398 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1399 assert_redirected_to user_path(friend)
1400 assert_match(/was removed from your friends/, flash[:notice])
1401 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1403 # A second POST should report that the friendship does not exist
1404 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1405 assert_redirected_to user_path(friend)
1406 assert_match(/is not one of your friends/, flash[:error])
1407 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1410 def test_remove_friend_with_referer
1411 # Get users to work with
1412 user = create(:user)
1413 friend = create(:user)
1414 create(:friend, :user_id => user.id, :friend_user_id => friend.id)
1416 # Check that the users are friends
1417 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1419 # The GET should preserve any referer
1420 get :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1421 assert_response :success
1422 assert_template :remove_friend
1423 assert_select "form" do
1424 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1425 assert_select "input[type='submit']", 1
1427 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1429 # When logged in a POST should remove the friendship and refer
1430 post :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1431 assert_redirected_to "/test"
1432 assert_match(/was removed from your friends/, flash[:notice])
1433 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1436 def test_remove_friend_unkown_user
1437 # Should error when a bogus user is specified
1438 get :remove_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1439 assert_response :not_found
1440 assert_template :no_such_user
1444 user = create(:user)
1446 # Try without logging in
1447 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }
1448 assert_response :redirect
1449 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1451 # Now try as a normal user
1452 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1453 assert_response :redirect
1454 assert_redirected_to :action => :show, :display_name => user.display_name
1456 # Finally try as an administrator
1457 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1458 assert_response :redirect
1459 assert_redirected_to :action => :show, :display_name => user.display_name
1460 assert_equal "suspended", User.find(user.id).status
1464 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1466 # Try without logging in
1467 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }
1468 assert_response :redirect
1469 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1471 # Now try as a normal user
1472 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1473 assert_response :redirect
1474 assert_redirected_to :action => :show, :display_name => user.display_name
1476 # Finally try as an administrator
1477 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1478 assert_response :redirect
1479 assert_redirected_to :action => :show, :display_name => user.display_name
1481 # Check that the user was deleted properly
1483 assert_equal "user_#{user.id}", user.display_name
1484 assert_equal "", user.description
1485 assert_nil user.home_lat
1486 assert_nil user.home_lon
1487 assert_equal false, user.image.file?
1488 assert_equal false, user.email_valid
1489 assert_nil user.new_email
1490 assert_nil user.auth_provider
1491 assert_nil user.auth_uid
1492 assert_equal "deleted", user.status
1496 user = create(:user)
1497 moderator_user = create(:moderator_user)
1498 administrator_user = create(:administrator_user)
1499 _suspended_user = create(:user, :suspended)
1500 _ip_user = create(:user, :creation_ip => "1.2.3.4")
1502 # There are now 7 users - the five above, plus two extra "granters" for the
1503 # moderator_user and administrator_user
1504 assert_equal 7, User.count
1506 # Shouldn't work when not logged in
1508 assert_response :redirect
1509 assert_redirected_to :action => :login, :referer => users_path
1511 session[:user] = user.id
1513 # Shouldn't work when logged in as a normal user
1515 assert_response :redirect
1516 assert_redirected_to :action => :login, :referer => users_path
1518 session[:user] = moderator_user.id
1520 # Shouldn't work when logged in as a moderator
1522 assert_response :redirect
1523 assert_redirected_to :action => :login, :referer => users_path
1525 session[:user] = administrator_user.id
1527 # Note there is a header row, so all row counts are users + 1
1528 # Should work when logged in as an administrator
1530 assert_response :success
1531 assert_template :index
1532 assert_select "table#user_list tr", :count => 7 + 1
1534 # Should be able to limit by status
1535 get :index, :params => { :status => "suspended" }
1536 assert_response :success
1537 assert_template :index
1538 assert_select "table#user_list tr", :count => 1 + 1
1540 # Should be able to limit by IP address
1541 get :index, :params => { :ip => "1.2.3.4" }
1542 assert_response :success
1543 assert_template :index
1544 assert_select "table#user_list tr", :count => 1 + 1
1547 def test_index_get_paginated
1548 1.upto(100).each do |n|
1549 User.create(:display_name => "extra_#{n}",
1550 :email => "extra#{n}@example.com",
1551 :pass_crypt => "extraextra")
1554 session[:user] = create(:administrator_user).id
1556 # 100 examples, an administrator, and a granter for the admin.
1557 assert_equal 102, User.count
1560 assert_response :success
1561 assert_template :index
1562 assert_select "table#user_list tr", :count => 51
1564 get :index, :params => { :page => 2 }
1565 assert_response :success
1566 assert_template :index
1567 assert_select "table#user_list tr", :count => 51
1569 get :index, :params => { :page => 3 }
1570 assert_response :success
1571 assert_template :index
1572 assert_select "table#user_list tr", :count => 3
1575 def test_index_post_confirm
1576 inactive_user = create(:user, :pending)
1577 suspended_user = create(:user, :suspended)
1579 # Shouldn't work when not logged in
1580 assert_no_difference "User.active.count" do
1581 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1583 assert_response :redirect
1584 assert_redirected_to :action => :login, :referer => users_path
1585 assert_equal "pending", inactive_user.reload.status
1586 assert_equal "suspended", suspended_user.reload.status
1588 session[:user] = create(:user).id
1590 # Shouldn't work when logged in as a normal user
1591 assert_no_difference "User.active.count" do
1592 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1594 assert_response :redirect
1595 assert_redirected_to :action => :login, :referer => users_path
1596 assert_equal "pending", inactive_user.reload.status
1597 assert_equal "suspended", suspended_user.reload.status
1599 session[:user] = create(:moderator_user).id
1601 # Shouldn't work when logged in as a moderator
1602 assert_no_difference "User.active.count" do
1603 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1605 assert_response :redirect
1606 assert_redirected_to :action => :login, :referer => users_path
1607 assert_equal "pending", inactive_user.reload.status
1608 assert_equal "suspended", suspended_user.reload.status
1610 session[:user] = create(:administrator_user).id
1612 # Should work when logged in as an administrator
1613 assert_difference "User.active.count", 2 do
1614 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1616 assert_response :redirect
1617 assert_redirected_to :action => :index
1618 assert_equal "confirmed", inactive_user.reload.status
1619 assert_equal "confirmed", suspended_user.reload.status
1622 def test_index_post_hide
1623 normal_user = create(:user)
1624 confirmed_user = create(:user, :confirmed)
1626 # Shouldn't work when not logged in
1627 assert_no_difference "User.active.count" do
1628 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1630 assert_response :redirect
1631 assert_redirected_to :action => :login, :referer => users_path
1632 assert_equal "active", normal_user.reload.status
1633 assert_equal "confirmed", confirmed_user.reload.status
1635 session[:user] = create(:user).id
1637 # Shouldn't work when logged in as a normal user
1638 assert_no_difference "User.active.count" do
1639 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1641 assert_response :redirect
1642 assert_redirected_to :action => :login, :referer => users_path
1643 assert_equal "active", normal_user.reload.status
1644 assert_equal "confirmed", confirmed_user.reload.status
1646 session[:user] = create(:moderator_user).id
1648 # Shouldn't work when logged in as a moderator
1649 assert_no_difference "User.active.count" do
1650 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1652 assert_response :redirect
1653 assert_redirected_to :action => :login, :referer => users_path
1654 assert_equal "active", normal_user.reload.status
1655 assert_equal "confirmed", confirmed_user.reload.status
1657 session[:user] = create(:administrator_user).id
1659 # Should work when logged in as an administrator
1660 assert_difference "User.active.count", -2 do
1661 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1663 assert_response :redirect
1664 assert_redirected_to :action => :index
1665 assert_equal "deleted", normal_user.reload.status
1666 assert_equal "deleted", confirmed_user.reload.status