3 class UserControllerTest < ActionController::TestCase
9 # test all routes which lead to this controller
12 { :path => "/api/0.6/user/1", :method => :get },
13 { :controller => "user", :action => "api_read", :id => "1" }
16 { :path => "/api/0.6/user/details", :method => :get },
17 { :controller => "user", :action => "api_details" }
20 { :path => "/api/0.6/user/gpx_files", :method => :get },
21 { :controller => "user", :action => "api_gpx_files" }
24 { :path => "/api/0.6/users", :method => :get },
25 { :controller => "user", :action => "api_users" }
29 { :path => "/login", :method => :get },
30 { :controller => "user", :action => "login" }
33 { :path => "/login", :method => :post },
34 { :controller => "user", :action => "login" }
37 { :controller => "user", :action => "login", :format => "html" },
38 { :path => "/login.html", :method => :get }
42 { :path => "/logout", :method => :get },
43 { :controller => "user", :action => "logout" }
46 { :path => "/logout", :method => :post },
47 { :controller => "user", :action => "logout" }
50 { :controller => "user", :action => "logout", :format => "html" },
51 { :path => "/logout.html", :method => :get }
55 { :path => "/user/new", :method => :get },
56 { :controller => "user", :action => "new" }
60 { :path => "/user/new", :method => :post },
61 { :controller => "user", :action => "create" }
65 { :path => "/user/terms", :method => :get },
66 { :controller => "user", :action => "terms" }
70 { :path => "/user/save", :method => :post },
71 { :controller => "user", :action => "save" }
75 { :path => "/user/username/confirm", :method => :get },
76 { :controller => "user", :action => "confirm", :display_name => "username" }
79 { :path => "/user/username/confirm", :method => :post },
80 { :controller => "user", :action => "confirm", :display_name => "username" }
83 { :path => "/user/username/confirm/resend", :method => :get },
84 { :controller => "user", :action => "confirm_resend", :display_name => "username" }
88 { :path => "/user/confirm", :method => :get },
89 { :controller => "user", :action => "confirm" }
92 { :path => "/user/confirm", :method => :post },
93 { :controller => "user", :action => "confirm" }
96 { :path => "/user/confirm-email", :method => :get },
97 { :controller => "user", :action => "confirm_email" }
100 { :path => "/user/confirm-email", :method => :post },
101 { :controller => "user", :action => "confirm_email" }
105 { :path => "/user/go_public", :method => :post },
106 { :controller => "user", :action => "go_public" }
110 { :path => "/user/forgot-password", :method => :get },
111 { :controller => "user", :action => "lost_password" }
114 { :path => "/user/forgot-password", :method => :post },
115 { :controller => "user", :action => "lost_password" }
118 { :path => "/user/reset-password", :method => :get },
119 { :controller => "user", :action => "reset_password" }
122 { :path => "/user/reset-password", :method => :post },
123 { :controller => "user", :action => "reset_password" }
127 { :path => "/user/suspended", :method => :get },
128 { :controller => "user", :action => "suspended" }
132 { :path => "/user/username", :method => :get },
133 { :controller => "user", :action => "show", :display_name => "username" }
137 { :path => "/user/username/account", :method => :get },
138 { :controller => "user", :action => "account", :display_name => "username" }
141 { :path => "/user/username/account", :method => :post },
142 { :controller => "user", :action => "account", :display_name => "username" }
146 { :path => "/user/username/make_friend", :method => :get },
147 { :controller => "user", :action => "make_friend", :display_name => "username" }
150 { :path => "/user/username/make_friend", :method => :post },
151 { :controller => "user", :action => "make_friend", :display_name => "username" }
154 { :path => "/user/username/remove_friend", :method => :get },
155 { :controller => "user", :action => "remove_friend", :display_name => "username" }
158 { :path => "/user/username/remove_friend", :method => :post },
159 { :controller => "user", :action => "remove_friend", :display_name => "username" }
163 { :path => "/user/username/set_status", :method => :get },
164 { :controller => "user", :action => "set_status", :display_name => "username" }
167 { :path => "/user/username/delete", :method => :get },
168 { :controller => "user", :action => "delete", :display_name => "username" }
172 { :path => "/users", :method => :get },
173 { :controller => "user", :action => "list" }
176 { :path => "/users", :method => :post },
177 { :controller => "user", :action => "list" }
180 { :path => "/users/status", :method => :get },
181 { :controller => "user", :action => "list", :status => "status" }
184 { :path => "/users/status", :method => :post },
185 { :controller => "user", :action => "list", :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 :controller => :user, :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 get :show, :params => { :display_name => user.display_name }
946 assert_response :success
947 assert_select "div#userinformation" do
948 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
949 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
950 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
951 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
952 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
953 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
954 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
955 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
958 # Test a user who has been blocked
959 blocked_user = create(:user)
960 create(:user_block, :user => blocked_user)
961 get :show, :params => { :display_name => blocked_user.display_name }
962 assert_response :success
963 assert_select "div#userinformation" do
964 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
965 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
966 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
967 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
968 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
969 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
970 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
971 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
974 # Test a moderator who has applied blocks
975 moderator_user = create(:moderator_user)
976 create(:user_block, :creator => moderator_user)
977 get :show, :params => { :display_name => moderator_user.display_name }
978 assert_response :success
979 assert_select "div#userinformation" do
980 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
981 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
982 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
983 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
984 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
985 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
986 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
987 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
990 # Login as a normal user
991 session[:user] = user.id
993 # Test the normal user
994 get :show, :params => { :display_name => user.display_name }
995 assert_response :success
996 assert_select "div#userinformation" do
997 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
998 assert_select "a[href='/traces/mine']", 1
999 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1000 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1001 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 1
1002 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1003 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1004 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
1007 # Login as a moderator
1008 session[:user] = create(:moderator_user).id
1010 # Test the normal user
1011 get :show, :params => { :display_name => user.display_name }
1012 assert_response :success
1013 assert_select "div#userinformation" do
1014 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
1015 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
1016 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
1017 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
1018 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
1019 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
1020 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
1021 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
1025 # Test whether information about contributor terms is shown for users who haven't agreed
1026 def test_terms_not_agreed
1027 agreed_user = create(:user, :terms_agreed => 3.days.ago)
1028 seen_user = create(:user, :terms_seen => true)
1029 not_seen_user = create(:user, :terms_seen => false)
1031 get :show, :params => { :display_name => agreed_user.display_name }
1032 assert_response :success
1033 assert_select "div#userinformation" do
1034 assert_select "p", :count => 0, :text => /Contributor terms/
1037 get :show, :params => { :display_name => seen_user.display_name }
1038 assert_response :success
1039 # put @response.body
1040 assert_select "div#userinformation" do
1041 assert_select "p", :count => 1, :text => /Contributor terms/
1042 assert_select "p", /Declined/
1045 get :show, :params => { :display_name => not_seen_user.display_name }
1046 assert_response :success
1047 assert_select "div#userinformation" do
1048 assert_select "p", :count => 1, :text => /Contributor terms/
1049 assert_select "p", /Undecided/
1054 user = create(:user, :description => "test", :terms_agreed => Date.yesterday)
1055 # check that a visible user is returned properly
1056 get :api_read, :params => { :id => user.id }
1057 assert_response :success
1058 assert_equal "text/xml", response.content_type
1060 # check the data that is returned
1061 assert_select "description", :count => 1, :text => "test"
1062 assert_select "contributor-terms", :count => 1 do
1063 assert_select "[agreed='true']"
1065 assert_select "img", :count => 0
1066 assert_select "roles", :count => 1 do
1067 assert_select "role", :count => 0
1069 assert_select "changesets", :count => 1 do
1070 assert_select "[count='0']"
1072 assert_select "traces", :count => 1 do
1073 assert_select "[count='0']"
1075 assert_select "blocks", :count => 1 do
1076 assert_select "received", :count => 1 do
1077 assert_select "[count='0'][active='0']"
1079 assert_select "issued", :count => 0
1082 # check that we aren't revealing private information
1083 assert_select "contributor-terms[pd]", false
1084 assert_select "home", false
1085 assert_select "languages", false
1086 assert_select "messages", false
1088 # check that a suspended user is not returned
1089 get :api_read, :params => { :id => create(:user, :suspended).id }
1090 assert_response :gone
1092 # check that a deleted user is not returned
1093 get :api_read, :params => { :id => create(:user, :deleted).id }
1094 assert_response :gone
1096 # check that a non-existent user is not returned
1097 get :api_read, :params => { :id => 0 }
1098 assert_response :not_found
1101 def test_api_details
1102 user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"])
1103 create(:message, :read, :recipient => user)
1104 create(:message, :sender => user)
1106 # check that nothing is returned when not logged in
1108 assert_response :unauthorized
1110 # check that we get a response when logged in
1111 basic_authorization user.email, "test"
1113 assert_response :success
1114 assert_equal "text/xml", response.content_type
1116 # check the data that is returned
1117 assert_select "description", :count => 1, :text => "test"
1118 assert_select "contributor-terms", :count => 1 do
1119 assert_select "[agreed='true'][pd='false']"
1121 assert_select "img", :count => 0
1122 assert_select "roles", :count => 1 do
1123 assert_select "role", :count => 0
1125 assert_select "changesets", :count => 1 do
1126 assert_select "[count='0']", :count => 1
1128 assert_select "traces", :count => 1 do
1129 assert_select "[count='0']", :count => 1
1131 assert_select "blocks", :count => 1 do
1132 assert_select "received", :count => 1 do
1133 assert_select "[count='0'][active='0']"
1135 assert_select "issued", :count => 0
1137 assert_select "home", :count => 1 do
1138 assert_select "[lat='12.1'][lon='12.1'][zoom='3']"
1140 assert_select "languages", :count => 1 do
1141 assert_select "lang", :count => 1, :text => "en"
1143 assert_select "messages", :count => 1 do
1144 assert_select "received", :count => 1 do
1145 assert_select "[count='1'][unread='0']"
1147 assert_select "sent", :count => 1 do
1148 assert_select "[count='1']"
1154 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
1155 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
1156 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
1158 get :api_users, :params => { :users => user1.id }
1159 assert_response :success
1160 assert_equal "text/xml", response.content_type
1161 assert_select "user", :count => 1 do
1162 assert_select "user[id='#{user1.id}']", :count => 1
1163 assert_select "user[id='#{user2.id}']", :count => 0
1164 assert_select "user[id='#{user3.id}']", :count => 0
1167 get :api_users, :params => { :users => user2.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 => 0
1172 assert_select "user[id='#{user2.id}']", :count => 1
1173 assert_select "user[id='#{user3.id}']", :count => 0
1176 get :api_users, :params => { :users => "#{user1.id},#{user3.id}" }
1177 assert_response :success
1178 assert_equal "text/xml", response.content_type
1179 assert_select "user", :count => 2 do
1180 assert_select "user[id='#{user1.id}']", :count => 1
1181 assert_select "user[id='#{user2.id}']", :count => 0
1182 assert_select "user[id='#{user3.id}']", :count => 1
1185 get :api_users, :params => { :users => create(:user, :suspended).id }
1186 assert_response :not_found
1188 get :api_users, :params => { :users => create(:user, :deleted).id }
1189 assert_response :not_found
1191 get :api_users, :params => { :users => 0 }
1192 assert_response :not_found
1195 def test_api_gpx_files
1196 user = create(:user)
1197 trace1 = create(:trace, :user => user) do |trace|
1198 create(:tracetag, :trace => trace, :tag => "London")
1200 trace2 = create(:trace, :user => user) do |trace|
1201 create(:tracetag, :trace => trace, :tag => "Birmingham")
1203 # check that nothing is returned when not logged in
1205 assert_response :unauthorized
1207 # check that we get a response when logged in
1208 basic_authorization user.email, "test"
1210 assert_response :success
1211 assert_equal "application/xml", response.content_type
1213 # check the data that is returned
1214 assert_select "gpx_file[id='#{trace1.id}']", 1 do
1215 assert_select "tag", "London"
1217 assert_select "gpx_file[id='#{trace2.id}']", 1 do
1218 assert_select "tag", "Birmingham"
1222 def test_make_friend
1223 # Get users to work with
1224 user = create(:user)
1225 friend = create(:user)
1227 # Check that the users aren't already friends
1228 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1230 # When not logged in a GET should ask us to login
1231 get :make_friend, :params => { :display_name => friend.display_name }
1232 assert_redirected_to :controller => :user, :action => "login", :referer => make_friend_path(:display_name => friend.display_name)
1234 # When not logged in a POST should error
1235 post :make_friend, :params => { :display_name => friend.display_name }
1236 assert_response :forbidden
1237 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1239 # When logged in a GET should get a confirmation page
1240 get :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1241 assert_response :success
1242 assert_template :make_friend
1243 assert_select "form" do
1244 assert_select "input[type='hidden'][name='referer']", 0
1245 assert_select "input[type='submit']", 1
1247 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1249 # When logged in a POST should add the friendship
1250 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1251 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1253 assert_redirected_to user_path(:display_name => friend.display_name)
1254 assert_match(/is now your friend/, flash[:notice])
1255 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1256 email = ActionMailer::Base.deliveries.first
1257 assert_equal 1, email.to.count
1258 assert_equal friend.email, email.to.first
1259 ActionMailer::Base.deliveries.clear
1261 # A second POST should report that the friendship already exists
1262 assert_no_difference "ActionMailer::Base.deliveries.size" do
1263 post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1265 assert_redirected_to user_path(:display_name => friend.display_name)
1266 assert_match(/You are already friends with/, flash[:warning])
1267 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1270 def test_make_friend_with_referer
1271 # Get users to work with
1272 user = create(:user)
1273 friend = create(:user)
1275 # Check that the users aren't already friends
1276 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1278 # The GET should preserve any referer
1279 get :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1280 assert_response :success
1281 assert_template :make_friend
1282 assert_select "form" do
1283 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1284 assert_select "input[type='submit']", 1
1286 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1288 # When logged in a POST should add the friendship and refer us
1289 assert_difference "ActionMailer::Base.deliveries.size", 1 do
1290 post :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1292 assert_redirected_to "/test"
1293 assert_match(/is now your friend/, flash[:notice])
1294 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1295 email = ActionMailer::Base.deliveries.first
1296 assert_equal 1, email.to.count
1297 assert_equal friend.email, email.to.first
1298 ActionMailer::Base.deliveries.clear
1301 def test_make_friend_unkown_user
1302 # Should error when a bogus user is specified
1303 get :make_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1304 assert_response :not_found
1305 assert_template :no_such_user
1308 def test_remove_friend
1309 # Get users to work with
1310 user = create(:user)
1311 friend = create(:user)
1312 create(:friend, :befriender => user, :befriendee => friend)
1314 # Check that the users are friends
1315 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1317 # When not logged in a GET should ask us to login
1318 get :remove_friend, :params => { :display_name => friend.display_name }
1319 assert_redirected_to :controller => :user, :action => "login", :referer => remove_friend_path(:display_name => friend.display_name)
1321 # When not logged in a POST should error
1322 post :remove_friend, :params => { :display_name => friend.display_name }
1323 assert_response :forbidden
1324 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1326 # When logged in a GET should get a confirmation page
1327 get :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1328 assert_response :success
1329 assert_template :remove_friend
1330 assert_select "form" do
1331 assert_select "input[type='hidden'][name='referer']", 0
1332 assert_select "input[type='submit']", 1
1334 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1336 # When logged in a POST should remove the friendship
1337 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1338 assert_redirected_to user_path(:display_name => friend.display_name)
1339 assert_match(/was removed from your friends/, flash[:notice])
1340 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1342 # A second POST should report that the friendship does not exist
1343 post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user }
1344 assert_redirected_to user_path(:display_name => friend.display_name)
1345 assert_match(/is not one of your friends/, flash[:error])
1346 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1349 def test_remove_friend_with_referer
1350 # Get users to work with
1351 user = create(:user)
1352 friend = create(:user)
1353 create(:friend, :user_id => user.id, :friend_user_id => friend.id)
1355 # Check that the users are friends
1356 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1358 # The GET should preserve any referer
1359 get :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1360 assert_response :success
1361 assert_template :remove_friend
1362 assert_select "form" do
1363 assert_select "input[type='hidden'][name='referer'][value='/test']", 1
1364 assert_select "input[type='submit']", 1
1366 assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1368 # When logged in a POST should remove the friendship and refer
1369 post :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user }
1370 assert_redirected_to "/test"
1371 assert_match(/was removed from your friends/, flash[:notice])
1372 assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first
1375 def test_remove_friend_unkown_user
1376 # Should error when a bogus user is specified
1377 get :remove_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) }
1378 assert_response :not_found
1379 assert_template :no_such_user
1383 user = create(:user)
1385 # Try without logging in
1386 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }
1387 assert_response :redirect
1388 assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended")
1390 # Now try as a normal user
1391 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1392 assert_response :redirect
1393 assert_redirected_to :action => :show, :display_name => user.display_name
1395 # Finally try as an administrator
1396 get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1397 assert_response :redirect
1398 assert_redirected_to :action => :show, :display_name => user.display_name
1399 assert_equal "suspended", User.find(user.id).status
1403 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
1405 # Try without logging in
1406 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }
1407 assert_response :redirect
1408 assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended")
1410 # Now try as a normal user
1411 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user }
1412 assert_response :redirect
1413 assert_redirected_to :action => :show, :display_name => user.display_name
1415 # Finally try as an administrator
1416 get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) }
1417 assert_response :redirect
1418 assert_redirected_to :action => :show, :display_name => user.display_name
1420 # Check that the user was deleted properly
1422 assert_equal "user_#{user.id}", user.display_name
1423 assert_equal "", user.description
1424 assert_nil user.home_lat
1425 assert_nil user.home_lon
1426 assert_equal false, user.image.file?
1427 assert_equal false, user.email_valid
1428 assert_nil user.new_email
1429 assert_nil user.auth_provider
1430 assert_nil user.auth_uid
1431 assert_equal "deleted", user.status
1435 user = create(:user)
1436 moderator_user = create(:moderator_user)
1437 administrator_user = create(:administrator_user)
1438 _suspended_user = create(:user, :suspended)
1439 _ip_user = create(:user, :creation_ip => "1.2.3.4")
1441 # There are now 7 users - the five above, plus two extra "granters" for the
1442 # moderator_user and administrator_user
1443 assert_equal 7, User.count
1445 # Shouldn't work when not logged in
1447 assert_response :redirect
1448 assert_redirected_to :action => :login, :referer => users_path
1450 session[:user] = user.id
1452 # Shouldn't work when logged in as a normal user
1454 assert_response :redirect
1455 assert_redirected_to :action => :login, :referer => users_path
1457 session[:user] = moderator_user.id
1459 # Shouldn't work when logged in as a moderator
1461 assert_response :redirect
1462 assert_redirected_to :action => :login, :referer => users_path
1464 session[:user] = administrator_user.id
1466 # Note there is a header row, so all row counts are users + 1
1467 # Should work when logged in as an administrator
1469 assert_response :success
1470 assert_template :list
1471 assert_select "table#user_list tr", :count => 7 + 1
1473 # Should be able to limit by status
1474 get :list, :params => { :status => "suspended" }
1475 assert_response :success
1476 assert_template :list
1477 assert_select "table#user_list tr", :count => 1 + 1
1479 # Should be able to limit by IP address
1480 get :list, :params => { :ip => "1.2.3.4" }
1481 assert_response :success
1482 assert_template :list
1483 assert_select "table#user_list tr", :count => 1 + 1
1486 def test_list_get_paginated
1487 1.upto(100).each do |n|
1488 User.create(:display_name => "extra_#{n}",
1489 :email => "extra#{n}@example.com",
1490 :pass_crypt => "extraextra")
1493 session[:user] = create(:administrator_user).id
1495 # 100 examples, an administrator, and a granter for the admin.
1496 assert_equal 102, User.count
1499 assert_response :success
1500 assert_template :list
1501 assert_select "table#user_list tr", :count => 51
1503 get :list, :params => { :page => 2 }
1504 assert_response :success
1505 assert_template :list
1506 assert_select "table#user_list tr", :count => 51
1508 get :list, :params => { :page => 3 }
1509 assert_response :success
1510 assert_template :list
1511 assert_select "table#user_list tr", :count => 3
1514 def test_list_post_confirm
1515 inactive_user = create(:user, :pending)
1516 suspended_user = create(:user, :suspended)
1518 # Shouldn't work when not logged in
1519 assert_no_difference "User.active.count" do
1520 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1522 assert_response :redirect
1523 assert_redirected_to :action => :login, :referer => users_path
1524 assert_equal "pending", inactive_user.reload.status
1525 assert_equal "suspended", suspended_user.reload.status
1527 session[:user] = create(:user).id
1529 # Shouldn't work when logged in as a normal user
1530 assert_no_difference "User.active.count" do
1531 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1533 assert_response :redirect
1534 assert_redirected_to :action => :login, :referer => users_path
1535 assert_equal "pending", inactive_user.reload.status
1536 assert_equal "suspended", suspended_user.reload.status
1538 session[:user] = create(:moderator_user).id
1540 # Shouldn't work when logged in as a moderator
1541 assert_no_difference "User.active.count" do
1542 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1544 assert_response :redirect
1545 assert_redirected_to :action => :login, :referer => users_path
1546 assert_equal "pending", inactive_user.reload.status
1547 assert_equal "suspended", suspended_user.reload.status
1549 session[:user] = create(:administrator_user).id
1551 # Should work when logged in as an administrator
1552 assert_difference "User.active.count", 2 do
1553 post :list, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
1555 assert_response :redirect
1556 assert_redirected_to :action => :list
1557 assert_equal "confirmed", inactive_user.reload.status
1558 assert_equal "confirmed", suspended_user.reload.status
1561 def test_list_post_hide
1562 normal_user = create(:user)
1563 confirmed_user = create(:user, :confirmed)
1565 # Shouldn't work when not logged in
1566 assert_no_difference "User.active.count" do
1567 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1569 assert_response :redirect
1570 assert_redirected_to :action => :login, :referer => users_path
1571 assert_equal "active", normal_user.reload.status
1572 assert_equal "confirmed", confirmed_user.reload.status
1574 session[:user] = create(:user).id
1576 # Shouldn't work when logged in as a normal user
1577 assert_no_difference "User.active.count" do
1578 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1580 assert_response :redirect
1581 assert_redirected_to :action => :login, :referer => users_path
1582 assert_equal "active", normal_user.reload.status
1583 assert_equal "confirmed", confirmed_user.reload.status
1585 session[:user] = create(:moderator_user).id
1587 # Shouldn't work when logged in as a moderator
1588 assert_no_difference "User.active.count" do
1589 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1591 assert_response :redirect
1592 assert_redirected_to :action => :login, :referer => users_path
1593 assert_equal "active", normal_user.reload.status
1594 assert_equal "confirmed", confirmed_user.reload.status
1596 session[:user] = create(:administrator_user).id
1598 # Should work when logged in as an administrator
1599 assert_difference "User.active.count", -2 do
1600 post :list, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
1602 assert_response :redirect
1603 assert_redirected_to :action => :list
1604 assert_equal "deleted", normal_user.reload.status
1605 assert_equal "deleted", confirmed_user.reload.status