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