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
675 user = create(:user, :data_public => false)
676 post :go_public, :session => { :user => user }
677 assert_response :redirect
678 assert_redirected_to :action => :account, :display_name => user.display_name
679 assert_equal true, User.find(user.id).data_public
682 def test_lost_password
683 # Test fetching the lost password page
685 assert_response :success
686 assert_template :lost_password
687 assert_select "div#notice", false
689 # Test resetting using the address as recorded for a user that has an
690 # address which is duplicated in a different case by another user
692 uppercase_user = build(:user, :email => user.email.upcase).tap { |u| u.save(:validate => false) }
694 assert_difference "ActionMailer::Base.deliveries.size", 1 do
695 perform_enqueued_jobs do
696 post :lost_password, :params => { :user => { :email => user.email } }
699 assert_response :redirect
700 assert_redirected_to :action => :login
701 assert_match(/^Sorry you lost it/, flash[:notice])
702 email = ActionMailer::Base.deliveries.first
703 assert_equal 1, email.to.count
704 assert_equal user.email, email.to.first
705 ActionMailer::Base.deliveries.clear
707 # Test resetting using an address that matches a different user
708 # that has the same address in a different case
709 assert_difference "ActionMailer::Base.deliveries.size", 1 do
710 perform_enqueued_jobs do
711 post :lost_password, :params => { :user => { :email => user.email.upcase } }
714 assert_response :redirect
715 assert_redirected_to :action => :login
716 assert_match(/^Sorry you lost it/, flash[:notice])
717 email = ActionMailer::Base.deliveries.first
718 assert_equal 1, email.to.count
719 assert_equal uppercase_user.email, email.to.first
720 ActionMailer::Base.deliveries.clear
722 # Test resetting using an address that is a case insensitive match
723 # for more than one user but not an exact match for either
724 assert_no_difference "ActionMailer::Base.deliveries.size" do
725 perform_enqueued_jobs do
726 post :lost_password, :params => { :user => { :email => user.email.titlecase } }
729 assert_response :success
730 assert_template :lost_password
731 assert_select ".error", /^Could not find that email address/
733 # Test resetting using the address as recorded for a user that has an
734 # address which is case insensitively unique
735 third_user = create(:user)
736 assert_difference "ActionMailer::Base.deliveries.size", 1 do
737 perform_enqueued_jobs do
738 post :lost_password, :params => { :user => { :email => third_user.email } }
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 third_user.email, email.to.first
747 ActionMailer::Base.deliveries.clear
749 # Test resetting using an address that matches a user that has the
750 # same (case insensitively unique) address in a different case
751 assert_difference "ActionMailer::Base.deliveries.size", 1 do
752 perform_enqueued_jobs do
753 post :lost_password, :params => { :user => { :email => third_user.email.upcase } }
756 assert_response :redirect
757 assert_redirected_to :action => :login
758 assert_match(/^Sorry you lost it/, flash[:notice])
759 email = ActionMailer::Base.deliveries.first
760 assert_equal 1, email.to.count
761 assert_equal third_user.email, email.to.first
762 ActionMailer::Base.deliveries.clear
765 def test_reset_password
766 user = create(:user, :pending)
767 # Test a request with no token
769 assert_response :bad_request
771 # Test a request with a bogus token
772 get :reset_password, :params => { :token => "made_up_token" }
773 assert_response :redirect
774 assert_redirected_to :action => :lost_password
776 # Create a valid token for a user
777 token = user.tokens.create
779 # Test a request with a valid token
780 get :reset_password, :params => { :token => token.token }
781 assert_response :success
782 assert_template :reset_password
784 # Test that errors are reported for erroneous submissions
785 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "different_password" } }
786 assert_response :success
787 assert_template :reset_password
788 assert_select "div#errorExplanation"
790 # Test setting a new password
791 post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" } }
792 assert_response :redirect
793 assert_redirected_to root_path
794 assert_equal user.id, session[:user]
796 assert_equal "active", user.status
797 assert_equal true, user.email_valid
798 assert_equal user, User.authenticate(:username => user.email, :password => "new_password")
802 # Get a user to work with - note that this user deliberately
803 # conflicts with uppercase_user in the email and display name
804 # fields to test that we can change other fields without any
805 # validation errors being reported
806 user = create(:user, :languages => [])
807 _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
809 # Make sure that you are redirected to the login page when
810 # you are not logged in
811 get :account, :params => { :display_name => user.display_name }
812 assert_response :redirect
813 assert_redirected_to :action => "login", :referer => "/user/#{ERB::Util.u(user.display_name)}/account"
815 # Make sure that you are blocked when not logged in as the right user
816 get :account, :params => { :display_name => user.display_name }, :session => { :user => create(:user) }
817 assert_response :forbidden
819 # Make sure we get the page when we are logged in as the right user
820 get :account, :params => { :display_name => user.display_name }, :session => { :user => user }
821 assert_response :success
822 assert_template :account
823 assert_select "form#accountForm" do |form|
824 assert_equal "post", form.attr("method").to_s
825 assert_select "input[name='_method']", false
826 assert_equal "/user/#{ERB::Util.u(user.display_name)}/account", form.attr("action").to_s
829 # Updating the description should work
830 user.description = "new description"
831 user.preferred_editor = "default"
832 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
833 assert_response :success
834 assert_template :account
835 assert_select "div#errorExplanation", false
836 assert_select ".notice", /^User information updated successfully/
837 assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description
839 # Changing to a invalid editor should fail
840 user.preferred_editor = "unknown"
841 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
842 assert_response :success
843 assert_template :account
844 assert_select ".notice", false
845 assert_select "div#errorExplanation"
846 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false
848 # Changing to a valid editor should work
849 user.preferred_editor = "potlatch2"
850 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
851 assert_response :success
852 assert_template :account
853 assert_select "div#errorExplanation", false
854 assert_select ".notice", /^User information updated successfully/
855 assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2"
857 # Changing to the default editor should work
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 > select#user_preferred_editor > option[selected]", false
866 # Changing to an uploaded image should work
867 image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif")
868 post :account, :params => { :display_name => user.display_name, :image_action => "new", :user => user.attributes.merge(:image => image) }, :session => { :user => user }
869 assert_response :success
870 assert_template :account
871 assert_select "div#errorExplanation", false
872 assert_select ".notice", /^User information updated successfully/
873 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "keep"
875 # Changing to a gravatar image should work
876 post :account, :params => { :display_name => user.display_name, :image_action => "gravatar", :user => user.attributes }, :session => { :user => user }
877 assert_response :success
878 assert_template :account
879 assert_select "div#errorExplanation", false
880 assert_select ".notice", /^User information updated successfully/
881 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "gravatar"
883 # Removing the image should work
884 post :account, :params => { :display_name => user.display_name, :image_action => "delete", :user => user.attributes }, :session => { :user => user }
885 assert_response :success
886 assert_template :account
887 assert_select "div#errorExplanation", false
888 assert_select ".notice", /^User information updated successfully/
889 assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked]", false
891 # Adding external authentication should redirect to the auth provider
892 post :account, :params => { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, :session => { :user => user }
893 assert_response :redirect
894 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")
896 # Changing name to one that exists should fail
897 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
898 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
899 assert_response :success
900 assert_template :account
901 assert_select ".notice", false
902 assert_select "div#errorExplanation"
903 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
905 # Changing name to one that exists should fail, regardless of case
906 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
907 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
908 assert_response :success
909 assert_template :account
910 assert_select ".notice", false
911 assert_select "div#errorExplanation"
912 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name"
914 # Changing name to one that doesn't exist should work
915 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
916 post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user }
917 assert_response :success
918 assert_template :account
919 assert_select "div#errorExplanation", false
920 assert_select ".notice", /^User information updated successfully/
921 assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester"
923 # Record the change of name
924 user.display_name = "new tester"
926 # Changing email to one that exists should fail
927 user.new_email = create(:user).email
928 assert_no_difference "ActionMailer::Base.deliveries.size" do
929 perform_enqueued_jobs do
930 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
933 assert_response :success
934 assert_template :account
935 assert_select ".notice", false
936 assert_select "div#errorExplanation"
937 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
939 # Changing email to one that exists should fail, regardless of case
940 user.new_email = create(:user).email.upcase
941 assert_no_difference "ActionMailer::Base.deliveries.size" do
942 perform_enqueued_jobs do
943 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
946 assert_response :success
947 assert_template :account
948 assert_select ".notice", false
949 assert_select "div#errorExplanation"
950 assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email"
952 # Changing email to one that doesn't exist should work
953 user.new_email = "new_tester@example.com"
954 assert_difference "ActionMailer::Base.deliveries.size", 1 do
955 perform_enqueued_jobs do
956 post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user }
959 assert_response :success
960 assert_template :account
961 assert_select "div#errorExplanation", false
962 assert_select ".notice", /^User information updated successfully/
963 assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email
964 email = ActionMailer::Base.deliveries.first
965 assert_equal 1, email.to.count
966 assert_equal user.new_email, email.to.first
967 ActionMailer::Base.deliveries.clear
970 # Check that the user account page will display and contains some relevant
971 # information for the user
973 # Test a non-existent user
974 get :show, :params => { :display_name => "unknown" }
975 assert_response :not_found
978 user = create(:user, :home_lon => 1.1, :home_lat => 1.1)
979 friend_user = create(:user, :home_lon => 1.2, :home_lat => 1.2)
980 create(:friend, :befriender => user, :befriendee => friend_user)
981 get :show, :params => { :display_name => user.display_name }
982 assert_response :success
983 assert_select "div#userinformation" do
984 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
985 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
986 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
987 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
988 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
989 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
990 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
991 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
994 # Friends shouldn't be visible as we're not logged in
995 assert_select "div#friends-container", :count => 0
997 # Test a user who has been blocked
998 blocked_user = create(:user)
999 create(:user_block, :user => blocked_user)
1000 get :show, :params => { :display_name => blocked_user.display_name }
1001 assert_response :success
1002 assert_select "div#userinformation" do
1003 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
1004 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
1005 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
1006 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
1007 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
1008 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
1009 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
1010 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
1013 # Test a moderator who has applied blocks
1014 moderator_user = create(:moderator_user)
1015 create(:user_block, :creator => moderator_user)
1016 get :show, :params => { :display_name => moderator_user.display_name }
1017 assert_response :success
1018 assert_select "div#userinformation" do
1019 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
1020 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
1021 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
1022 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
1023 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
1024 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
1025 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
1026 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
1029 # Login as a normal user
1030 session[:user] = user.id
1032 # Test the normal user
1033 get :show, :params => { :display_name => user.display_name }
1034 assert_response :success
1035 assert_select "div#userinformation" do
1036 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1037 assert_select "a[href='/traces/mine']", 1
1038 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1039 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1040 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 1
1041 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1042 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1043 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1046 # Friends should be visible as we're now logged in
1047 assert_select "div#friends-container" do
1048 assert_select "div.contact-activity", :count => 1
1051 # Login as a moderator
1052 session[:user] = create(:moderator_user).id
1054 # Test the normal user
1055 get :show, :params => { :display_name => user.display_name }
1056 assert_response :success
1057 assert_select "div#userinformation" do
1058 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1059 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1060 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1061 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1062 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1063 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1064 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1065 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
1069 # Test whether information about contributor terms is shown for users who haven't agreed
1070 def test_terms_not_agreed
1071 agreed_user = create(:user, :terms_agreed => 3.days.ago)
1072 seen_user = create(:user, :terms_seen => true)
1073 not_seen_user = create(:user, :terms_seen => false)
1075 get :show, :params => { :display_name => agreed_user.display_name }
1076 assert_response :success
1077 assert_select "div#userinformation" do
1078 assert_select "p", :count => 0, :text => /Contributor terms/
1081 get :show, :params => { :display_name => seen_user.display_name }
1082 assert_response :success
1083 # put @response.body
1084 assert_select "div#userinformation" do
1085 assert_select "p", :count => 1, :text => /Contributor terms/
1086 assert_select "p", /Declined/
1089 get :show, :params => { :display_name => not_seen_user.display_name }
1090 assert_response :success
1091 assert_select "div#userinformation" do
1092 assert_select "p", :count => 1, :text => /Contributor terms/
1093 assert_select "p", /Undecided/
1098 user = create(:user, :description => "test", :terms_agreed => Date.yesterday)
1099 # check that a visible user is returned properly
1100 get :api_read, :params => { :id => user.id }
1101 assert_response :success
1102 assert_equal "text/xml", response.content_type
1104 # check the data that is returned
1105 assert_select "description", :count => 1, :text => "test"
1106 assert_select "contributor-terms", :count => 1 do
1107 assert_select "[agreed='true']"
1109 assert_select "img", :count => 0
1110 assert_select "roles", :count => 1 do
1111 assert_select "role", :count => 0
1113 assert_select "changesets", :count => 1 do
1114 assert_select "[count='0']"
1116 assert_select "traces", :count => 1 do
1117 assert_select "[count='0']"
1119 assert_select "blocks", :count => 1 do
1120 assert_select "received", :count => 1 do
1121 assert_select "[count='0'][active='0']"
1123 assert_select "issued", :count => 0
1126 # check that we aren't revealing private information
1127 assert_select "contributor-terms[pd]", false
1128 assert_select "home", false
1129 assert_select "languages", false
1130 assert_select "messages", false
1132 # check that a suspended user is not returned
1133 get :api_read, :params => { :id => create(:user, :suspended).id }
1134 assert_response :gone
1136 # check that a deleted user is not returned
1137 get :api_read, :params => { :id => create(:user, :deleted).id }
1138 assert_response :gone
1140 # check that a non-existent user is not returned
1141 get :api_read, :params => { :id => 0 }
1142 assert_response :not_found
1145 def test_api_details
1146 user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"])
1147 create(:message, :read, :recipient => user)
1148 create(:message, :sender => user)
1150 # check that nothing is returned when not logged in
1152 assert_response :unauthorized
1154 # check that we get a response when logged in
1155 basic_authorization user.email, "test"
1157 assert_response :success
1158 assert_equal "text/xml", response.content_type
1160 # check the data that is returned
1161 assert_select "description", :count => 1, :text => "test"
1162 assert_select "contributor-terms", :count => 1 do
1163 assert_select "[agreed='true'][pd='false']"
1165 assert_select "img", :count => 0
1166 assert_select "roles", :count => 1 do
1167 assert_select "role", :count => 0
1169 assert_select "changesets", :count => 1 do
1170 assert_select "[count='0']", :count => 1
1172 assert_select "traces", :count => 1 do
1173 assert_select "[count='0']", :count => 1
1175 assert_select "blocks", :count => 1 do
1176 assert_select "received", :count => 1 do
1177 assert_select "[count='0'][active='0']"
1179 assert_select "issued", :count => 0
1181 assert_select "home", :count => 1 do
1182 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
1184 assert_select "languages", :count => 1 do
1185 assert_select "lang", :count => 1, :text => "en"
1187 assert_select "messages", :count => 1 do
1188 assert_select "received", :count => 1 do
1189 assert_select "[count='1'][unread='0']"
1191 assert_select "sent", :count => 1 do
1192 assert_select "[count='1']"
1198 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
1199 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
1200 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
1202 get :api_users, :params => { :users => user1.id }
1203 assert_response :success
1204 assert_equal "text/xml", response.content_type
1205 assert_select "user", :count => 1 do
1206 assert_select "user[id='#{user1.id}']", :count => 1
1207 assert_select "user[id='#{user2.id}']", :count => 0
1208 assert_select "user[id='#{user3.id}']", :count => 0
1211 get :api_users, :params => { :users => user2.id }
1212 assert_response :success
1213 assert_equal "text/xml", response.content_type
1214 assert_select "user", :count => 1 do
1215 assert_select "user[id='#{user1.id}']", :count => 0
1216 assert_select "user[id='#{user2.id}']", :count => 1
1217 assert_select "user[id='#{user3.id}']", :count => 0
1220 get :api_users, :params => { :users => "#{user1.id},#{user3.id}" }
1221 assert_response :success
1222 assert_equal "text/xml", response.content_type
1223 assert_select "user", :count => 2 do
1224 assert_select "user[id='#{user1.id}']", :count => 1
1225 assert_select "user[id='#{user2.id}']", :count => 0
1226 assert_select "user[id='#{user3.id}']", :count => 1
1229 get :api_users, :params => { :users => create(:user, :suspended).id }
1230 assert_response :not_found
1232 get :api_users, :params => { :users => create(:user, :deleted).id }
1233 assert_response :not_found
1235 get :api_users, :params => { :users => 0 }
1236 assert_response :not_found
1239 def test_api_gpx_files
1240 user = create(:user)
1241 trace1 = create(:trace, :user => user) do |trace|
1242 create(:tracetag, :trace => trace, :tag => "London")
1244 trace2 = create(:trace, :user => user) do |trace|
1245 create(:tracetag, :trace => trace, :tag => "Birmingham")
1247 # check that nothing is returned when not logged in
1249 assert_response :unauthorized
1251 # check that we get a response when logged in
1252 basic_authorization user.email, "test"
1254 assert_response :success
1255 assert_equal "application/xml", response.content_type
1257 # check the data that is returned
1258 assert_select "gpx_file[id='#{trace1.id}']", 1 do
1259 assert_select "tag", "London"
1261 assert_select "gpx_file[id='#{trace2.id}']", 1 do
1262 assert_select "tag", "Birmingham"
1266 def test_make_friend
1267 # Get users to work with
1268 user = create(:user)
1269 friend = create(:user)
1271 # Check that the users aren't already friends
1272 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1274 # When not logged in a GET should ask us to login
1275 get :make_friend, :params => { :display_name => friend.display_name }
1276 assert_redirected_to :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1278 # When not logged in a POST should error
1279 post :make_friend, :params => { :display_name => friend.display_name }
1280 assert_response :forbidden
1281 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1283 # When logged in a GET should get a confirmation page
1284 get :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1285 assert_response :success
1286 assert_template :make_friend
1287 assert_select "form" do
1288 assert_select "input[type='hidden'][name='referer']", 0
1289 assert_select "input[type='submit']", 1
1291 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1293 # When logged in a POST should add the friendship
1294 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1295 perform_enqueued_jobs do
1296 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1299 assert_redirected_to user_path(friend)
1300 assert_match(/is now your friend/, flash[:notice])
1301 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1302 email = ActionMailer::Base.deliveries.first
1303 assert_equal 1, email.to.count
1304 assert_equal friend.email, email.to.first
1305 ActionMailer::Base.deliveries.clear
1307 # A second POST should report that the friendship already exists
1308 assert_no_difference "ActionMailer::Base.deliveries.size" do
1309 perform_enqueued_jobs do
1310 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1313 assert_redirected_to user_path(friend)
1314 assert_match(/You are already friends with/, flash[:warning])
1315 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1318 def test_make_friend_with_referer
1319 # Get users to work with
1320 user = create(:user)
1321 friend = create(:user)
1323 # Check that the users aren't already friends
1324 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1326 # The GET should preserve any referer
1327 get :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1328 assert_response :success
1329 assert_template :make_friend
1330 assert_select "form" do
1331 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1332 assert_select "input[type='submit']", 1
1334 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1336 # When logged in a POST should add the friendship and refer us
1337 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1338 perform_enqueued_jobs do
1339 post :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1342 assert_redirected_to "/test"
1343 assert_match(/is now your friend/, flash[:notice])
1344 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1345 email = ActionMailer::Base.deliveries.first
1346 assert_equal 1, email.to.count
1347 assert_equal friend.email, email.to.first
1348 ActionMailer::Base.deliveries.clear
1351 def test_make_friend_unkown_user
1352 # Should error when a bogus user is specified
1353 get :make_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1354 assert_response :not_found
1355 assert_template :no_such_user
1358 def test_remove_friend
1359 # Get users to work with
1360 user = create(:user)
1361 friend = create(:user)
1362 create(:friend, :befriender => user, :befriendee => friend)
1364 # Check that the users are friends
1365 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1367 # When not logged in a GET should ask us to login
1368 get :remove_friend, :params => { :display_name => friend.display_name }
1369 assert_redirected_to :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1371 # When not logged in a POST should error
1372 post :remove_friend, :params => { :display_name => friend.display_name }
1373 assert_response :forbidden
1374 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1376 # When logged in a GET should get a confirmation page
1377 get :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1378 assert_response :success
1379 assert_template :remove_friend
1380 assert_select "form" do
1381 assert_select "input[type='hidden'][name='referer']", 0
1382 assert_select "input[type='submit']", 1
1384 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1386 # When logged in a POST should remove the friendship
1387 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1388 assert_redirected_to user_path(friend)
1389 assert_match(/was removed from your friends/, flash[:notice])
1390 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1392 # A second POST should report that the friendship does not exist
1393 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1394 assert_redirected_to user_path(friend)
1395 assert_match(/is not one of your friends/, flash[:error])
1396 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1399 def test_remove_friend_with_referer
1400 # Get users to work with
1401 user = create(:user)
1402 friend = create(:user)
1403 create(:friend, :user_id => user.id, :friend_user_id => friend.id)
1405 # Check that the users are friends
1406 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1408 # The GET should preserve any referer
1409 get :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1410 assert_response :success
1411 assert_template :remove_friend
1412 assert_select "form" do
1413 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1414 assert_select "input[type='submit']", 1
1416 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1418 # When logged in a POST should remove the friendship and refer
1419 post :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1420 assert_redirected_to "/test"
1421 assert_match(/was removed from your friends/, flash[:notice])
1422 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1425 def test_remove_friend_unkown_user
1426 # Should error when a bogus user is specified
1427 get :remove_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1428 assert_response :not_found
1429 assert_template :no_such_user
1433 user = create(:user)
1435 # Try without logging in
1436 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }
1437 assert_response :redirect
1438 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1440 # Now try as a normal user
1441 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1442 assert_response :redirect
1443 assert_redirected_to :action => :show, :display_name => user.display_name
1445 # Finally try as an administrator
1446 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1447 assert_response :redirect
1448 assert_redirected_to :action => :show, :display_name => user.display_name
1449 assert_equal "suspended", User.find(user.id).status
1453 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1455 # Try without logging in
1456 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }
1457 assert_response :redirect
1458 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1460 # Now try as a normal user
1461 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1462 assert_response :redirect
1463 assert_redirected_to :action => :show, :display_name => user.display_name
1465 # Finally try as an administrator
1466 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1467 assert_response :redirect
1468 assert_redirected_to :action => :show, :display_name => user.display_name
1470 # Check that the user was deleted properly
1472 assert_equal "user_#{user.id}", user.display_name
1473 assert_equal "", user.description
1474 assert_nil user.home_lat
1475 assert_nil user.home_lon
1476 assert_equal false, user.image.file?
1477 assert_equal false, user.email_valid
1478 assert_nil user.new_email
1479 assert_nil user.auth_provider
1480 assert_nil user.auth_uid
1481 assert_equal "deleted", user.status
1485 user = create(:user)
1486 moderator_user = create(:moderator_user)
1487 administrator_user = create(:administrator_user)
1488 _suspended_user = create(:user, :suspended)
1489 _ip_user = create(:user, :creation_ip => "1.2.3.4")
1491 # There are now 7 users - the five above, plus two extra "granters" for the
1492 # moderator_user and administrator_user
1493 assert_equal 7, User.count
1495 # Shouldn't work when not logged in
1497 assert_response :redirect
1498 assert_redirected_to :action => :login, :referer => users_path
1500 session[:user] = user.id
1502 # Shouldn't work when logged in as a normal user
1504 assert_response :redirect
1505 assert_redirected_to :action => :login, :referer => users_path
1507 session[:user] = moderator_user.id
1509 # Shouldn't work when logged in as a moderator
1511 assert_response :redirect
1512 assert_redirected_to :action => :login, :referer => users_path
1514 session[:user] = administrator_user.id
1516 # Note there is a header row, so all row counts are users + 1
1517 # Should work when logged in as an administrator
1519 assert_response :success
1520 assert_template :index
1521 assert_select "table#user_list tr", :count => 7 + 1
1523 # Should be able to limit by status
1524 get :index, :params => { :status => "suspended" }
1525 assert_response :success
1526 assert_template :index
1527 assert_select "table#user_list tr", :count => 1 + 1
1529 # Should be able to limit by IP address
1530 get :index, :params => { :ip => "1.2.3.4" }
1531 assert_response :success
1532 assert_template :index
1533 assert_select "table#user_list tr", :count => 1 + 1
1536 def test_index_get_paginated
1537 1.upto(100).each do |n|
1538 User.create(:display_name => "extra_#{n}",
1539 :email => "extra#{n}@example.com",
1540 :pass_crypt => "extraextra")
1543 session[:user] = create(:administrator_user).id
1545 # 100 examples, an administrator, and a granter for the admin.
1546 assert_equal 102, User.count
1549 assert_response :success
1550 assert_template :index
1551 assert_select "table#user_list tr", :count => 51
1553 get :index, :params => { :page => 2 }
1554 assert_response :success
1555 assert_template :index
1556 assert_select "table#user_list tr", :count => 51
1558 get :index, :params => { :page => 3 }
1559 assert_response :success
1560 assert_template :index
1561 assert_select "table#user_list tr", :count => 3
1564 def test_index_post_confirm
1565 inactive_user = create(:user, :pending)
1566 suspended_user = create(:user, :suspended)
1568 # Shouldn't work when not logged in
1569 assert_no_difference "User.active.count" do
1570 post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1572 assert_response :redirect
1573 assert_redirected_to :action => :login, :referer => users_path
1574 assert_equal "pending", inactive_user.reload.status
1575 assert_equal "suspended", suspended_user.reload.status
1577 session[:user] = create(:user).id
1579 # Shouldn't work when logged in as a normal user
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(:moderator_user).id
1590 # Shouldn't work when logged in as a moderator
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(:administrator_user).id
1601 # Should work when logged in as an administrator
1602 assert_difference "User.active.count", 2 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 => :index
1607 assert_equal "confirmed", inactive_user.reload.status
1608 assert_equal "confirmed", suspended_user.reload.status
1611 def test_index_post_hide
1612 normal_user = create(:user)
1613 confirmed_user = create(:user, :confirmed)
1615 # Shouldn't work when not logged in
1616 assert_no_difference "User.active.count" do
1617 post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1619 assert_response :redirect
1620 assert_redirected_to :action => :login, :referer => users_path
1621 assert_equal "active", normal_user.reload.status
1622 assert_equal "confirmed", confirmed_user.reload.status
1624 session[:user] = create(:user).id
1626 # Shouldn't work when logged in as a normal user
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(:moderator_user).id
1637 # Shouldn't work when logged in as a moderator
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(:administrator_user).id
1648 # Should work when logged in as an administrator
1649 assert_difference "User.active.count", -2 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 => :index
1654 assert_equal "deleted", normal_user.reload.status
1655 assert_equal "deleted", confirmed_user.reload.status