X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/35a2d66e1960b00e5ed0cecbe4263cdeedb1ad62..93b8c47c8fa1aac86d2d9c730c327c076b578a84:/test/controllers/users_controller_test.rb?ds=sidebyside diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index b85dcf65b..ff87c9466 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -1,30 +1,9 @@ require "test_helper" -class UsersControllerTest < ActionController::TestCase - def setup - stub_hostip_requests - end - +class UsersControllerTest < ActionDispatch::IntegrationTest ## # test all routes which lead to this controller def test_routes - assert_routing( - { :path => "/api/0.6/user/1", :method => :get }, - { :controller => "users", :action => "api_read", :id => "1" } - ) - assert_routing( - { :path => "/api/0.6/user/details", :method => :get }, - { :controller => "users", :action => "api_details" } - ) - assert_routing( - { :path => "/api/0.6/user/gpx_files", :method => :get }, - { :controller => "users", :action => "api_gpx_files" } - ) - assert_routing( - { :path => "/api/0.6/users", :method => :get }, - { :controller => "users", :action => "api_users" } - ) - assert_routing( { :path => "/login", :method => :get }, { :controller => "users", :action => "login" } @@ -143,29 +122,12 @@ class UsersControllerTest < ActionController::TestCase ) assert_routing( - { :path => "/user/username/make_friend", :method => :get }, - { :controller => "users", :action => "make_friend", :display_name => "username" } - ) - assert_routing( - { :path => "/user/username/make_friend", :method => :post }, - { :controller => "users", :action => "make_friend", :display_name => "username" } - ) - assert_routing( - { :path => "/user/username/remove_friend", :method => :get }, - { :controller => "users", :action => "remove_friend", :display_name => "username" } - ) - assert_routing( - { :path => "/user/username/remove_friend", :method => :post }, - { :controller => "users", :action => "remove_friend", :display_name => "username" } - ) - - assert_routing( - { :path => "/user/username/set_status", :method => :get }, + { :path => "/user/username/set_status", :method => :post }, { :controller => "users", :action => "set_status", :display_name => "username" } ) assert_routing( - { :path => "/user/username/delete", :method => :get }, - { :controller => "users", :action => "delete", :display_name => "username" } + { :path => "/user/username", :method => :delete }, + { :controller => "users", :action => "destroy", :display_name => "username" } ) assert_routing( @@ -188,11 +150,11 @@ class UsersControllerTest < ActionController::TestCase # The user creation page loads def test_new_view - get :new + get user_new_path assert_response :redirect assert_redirected_to user_new_path(:cookie_test => "true") - get :new, :params => { :cookie_test => "true" }, :session => { :cookie_test => true } + get user_new_path, :params => { :cookie_test => "true" } assert_response :success assert_select "html", :count => 1 do @@ -215,19 +177,13 @@ class UsersControllerTest < ActionController::TestCase end def test_new_view_logged_in - session[:user] = create(:user).id + session_for(create(:user)) - get :new - assert_response :redirect - assert_redirected_to user_new_path(:cookie_test => "true") - get :new, :params => { :cookie_test => "true" } + get user_new_path assert_response :redirect assert_redirected_to root_path - get :new, :params => { :referer => "/test" } - assert_response :redirect - assert_redirected_to user_new_path(:referer => "/test", :cookie_test => "true") - get :new, :params => { :referer => "/test", :cookie_test => "true" } + get user_new_path, :params => { :referer => "/test" } assert_response :redirect assert_redirected_to "/test" end @@ -235,10 +191,18 @@ class UsersControllerTest < ActionController::TestCase def test_new_success user = build(:user, :pending) + assert_no_difference "User.count" do + assert_no_difference "ActionMailer::Base.deliveries.size" do + perform_enqueued_jobs do + post user_new_path, :params => { :user => user.attributes } + end + end + end + assert_difference "User.count", 1 do assert_difference "ActionMailer::Base.deliveries.size", 1 do perform_enqueued_jobs do - post :save, :session => { :new_user => user } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } end end end @@ -257,80 +221,157 @@ class UsersControllerTest < ActionController::TestCase def test_new_duplicate_email user = build(:user, :pending) - user.email = create(:user).email + create(:user, :email => user.email) assert_no_difference "User.count" do assert_no_difference "ActionMailer::Base.deliveries.size" do perform_enqueued_jobs do - post :save, :session => { :new_user => user } + post user_new_path, :params => { :user => user.attributes } end end end assert_response :success assert_template "new" - assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email" + assert_select "form > div.form-group > input.is-invalid#user_email" end - def test_new_duplicate_email_uppercase + def test_save_duplicate_email user = build(:user, :pending) - user.email = create(:user).email.upcase + # Set up our user as being half-way through registration + assert_no_difference "User.count" do + assert_no_difference "ActionMailer::Base.deliveries.size" do + perform_enqueued_jobs do + post user_new_path, :params => { :user => user.attributes } + end + end + end + + # Now create another user with that email + create(:user, :email => user.email) + + # Check that the second half of registration fails assert_no_difference "User.count" do assert_no_difference "ActionMailer::Base.deliveries.size" do perform_enqueued_jobs do - post :save, :session => { :new_user => user } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } end end end assert_response :success assert_template "new" - assert_select "form > fieldset > div.form-row > input.field_with_errors#user_email" + assert_select "form > div.form-group > input.is-invalid#user_email" end - def test_new_duplicate_name + def test_save_duplicate_email_uppercase user = build(:user, :pending) - user.display_name = create(:user).display_name + # Set up our user as being half-way through registration assert_no_difference "User.count" do assert_no_difference "ActionMailer::Base.deliveries.size" do perform_enqueued_jobs do - post :save, :session => { :new_user => user } + post user_new_path, :params => { :user => user.attributes } + end + end + end + + # Now create another user with that email, but uppercased + create(:user, :email => user.email.upcase) + + # Check that the second half of registration fails + assert_no_difference "User.count" do + assert_no_difference "ActionMailer::Base.deliveries.size" do + perform_enqueued_jobs do + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } + end + end + end + + assert_response :success + assert_template "new" + assert_select "form > div.form-group > input.is-invalid#user_email" + end + + def test_save_duplicate_name + user = build(:user, :pending) + + # Set up our user as being half-way through registration + assert_no_difference "User.count" do + assert_no_difference "ActionMailer::Base.deliveries.size" do + perform_enqueued_jobs do + post user_new_path, :params => { :user => user.attributes } + end + end + end + + # Now create another user with that display name + create(:user, :display_name => user.display_name) + + # Check that the second half of registration fails + assert_no_difference "User.count" do + assert_no_difference "ActionMailer::Base.deliveries.size" do + perform_enqueued_jobs do + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } end end end assert_response :success assert_template "new" - assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name" + assert_select "form > div.form-group > input.is-invalid#user_display_name" end - def test_new_duplicate_name_uppercase + def test_save_duplicate_name_uppercase user = build(:user, :pending) - user.display_name = create(:user).display_name.upcase + # Set up our user as being half-way through registration + assert_no_difference "User.count" do + assert_no_difference "ActionMailer::Base.deliveries.size" do + perform_enqueued_jobs do + post user_new_path, :params => { :user => user.attributes } + end + end + end + + # Now create another user with that display_name, but uppercased + create(:user, :display_name => user.display_name.upcase) + + # Check that the second half of registration fails assert_no_difference "User.count" do assert_no_difference "ActionMailer::Base.deliveries.size" do perform_enqueued_jobs do - post :save, :session => { :new_user => user } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } end end end assert_response :success assert_template "new" - assert_select "form > fieldset > div.form-row > input.field_with_errors#user_display_name" + assert_select "form > div.form-group > input.is-invalid#user_display_name" end - def test_new_blocked_domain + def test_save_blocked_domain user = build(:user, :pending, :email => "user@example.net") + + # Set up our user as being half-way through registration + assert_no_difference "User.count" do + assert_no_difference "ActionMailer::Base.deliveries.size" do + perform_enqueued_jobs do + post user_new_path, :params => { :user => user.attributes } + end + end + end + + # Now block that domain create(:acl, :domain => "example.net", :k => "no_account_creation") + # Check that the second half of registration fails assert_no_difference "User.count" do assert_no_difference "ActionMailer::Base.deliveries.size" do perform_enqueued_jobs do - post :save, :session => { :new_user => user } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } end end end @@ -342,183 +383,237 @@ class UsersControllerTest < ActionController::TestCase def test_save_referer_params user = build(:user, :pending) + # Set up our user as being half-way through registration + assert_no_difference "User.count" do + assert_no_difference "ActionMailer::Base.deliveries.size" do + perform_enqueued_jobs do + post user_new_path, :params => { :user => user.attributes, :referer => "/edit?editor=id#map=1/2/3" } + end + end + end + assert_difference "User.count", 1 do assert_difference "ActionMailer::Base.deliveries.size", 1 do perform_enqueued_jobs do - post :save, :session => { :new_user => user, - :referer => "/edit?editor=id#map=1/2/3" } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } end end end assert_equal welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3), - user.tokens.order("id DESC").first.referer + User.find_by(:email => user.email).tokens.order("id DESC").first.referer ActionMailer::Base.deliveries.clear end - def test_logout_without_referer - get :logout + def test_login + user = create(:user) + + get login_path + assert_response :redirect + assert_redirected_to login_path(:cookie_test => true) + follow_redirect! assert_response :success - assert_template :logout - assert_select "input[name=referer][value=?]", "" + assert_template "login" - session_id = assert_select("input[name=session]").first["value"] + get login_path, :params => { :username => user.display_name, :password => "test" } + assert_response :success + assert_template "login" - get :logout, :params => { :session => session_id } + post login_path, :params => { :username => user.display_name, :password => "test" } assert_response :redirect assert_redirected_to root_path end - def test_logout_with_referer - get :logout, :params => { :referer => "/test" } - assert_response :success - assert_template :logout - assert_select "input[name=referer][value=?]", "/test" - - session_id = assert_select("input[name=session]").first["value"] + def test_logout_without_referer + post logout_path + assert_response :redirect + assert_redirected_to root_path + end - get :logout, :params => { :session => session_id, :referer => "/test" } + def test_logout_with_referer + post logout_path, :params => { :referer => "/test" } assert_response :redirect assert_redirected_to "/test" end - def test_logout_with_token - token = create(:user).tokens.create - - session[:token] = token.token + def test_logout_fallback_without_referer + get logout_path + assert_response :success + assert_template :logout + assert_select "input[name=referer]:not([value])" + end - get :logout + def test_logout_fallback_with_referer + get logout_path, :params => { :referer => "/test" } assert_response :success assert_template :logout - assert_select "input[name=referer][value=?]", "" - assert_equal token.token, session[:token] - assert_not_nil UserToken.where(:id => token.id).first + assert_select "input[name=referer][value=?]", "/test" + end - session_id = assert_select("input[name=session]").first["value"] + def test_logout_removes_session_token + user = build(:user, :pending) + post user_new_path, :params => { :user => user.attributes } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } - get :logout, :params => { :session => session_id } + assert_difference "User.find_by(:email => user.email).tokens.count", -1 do + post logout_path + end assert_response :redirect assert_redirected_to root_path - assert_nil session[:token] - assert_nil UserToken.where(:id => token.id).first end def test_confirm_get - user = create(:user, :pending) - confirm_string = user.tokens.create.token + user = build(:user, :pending) + post user_new_path, :params => { :user => user.attributes } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } + confirm_string = User.find_by(:email => user.email).tokens.create.token - @request.cookies["_osm_session"] = user.display_name - get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string } + get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string } assert_response :success assert_template :confirm end def test_confirm_get_already_confirmed - user = create(:user) - confirm_string = user.tokens.create.token + user = build(:user, :pending) + stub_gravatar_request(user.email) + post user_new_path, :params => { :user => user.attributes } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } + confirm_string = User.find_by(:email => user.email).tokens.create.token - @request.cookies["_osm_session"] = user.display_name - get :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string } + # Get the confirmation page + get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string } + assert_response :success + assert_template :confirm + + # Confirm the user + post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string } + assert_redirected_to welcome_path + + # Now try to get the confirmation page again + get user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string } assert_response :redirect assert_redirected_to root_path end def test_confirm_success_no_token_no_referer - user = create(:user, :pending) + user = build(:user, :pending) stub_gravatar_request(user.email) - confirm_string = user.tokens.create.token + post user_new_path, :params => { :user => user.attributes } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } + confirm_string = User.find_by(:email => user.email).tokens.create.token - @request.cookies["_osm_session"] = user.display_name - post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string } + post logout_path + + post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string } assert_redirected_to login_path assert_match(/Confirmed your account/, flash[:notice]) end def test_confirm_success_good_token_no_referer - user = create(:user, :pending) + user = build(:user, :pending) stub_gravatar_request(user.email) - confirm_string = user.tokens.create.token - token = user.tokens.create.token + post user_new_path, :params => { :user => user.attributes } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } + confirm_string = User.find_by(:email => user.email).tokens.create.token - @request.cookies["_osm_session"] = user.display_name - post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token } + post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string } assert_redirected_to welcome_path end def test_confirm_success_bad_token_no_referer - user = create(:user, :pending) + user = build(:user, :pending) stub_gravatar_request(user.email) - confirm_string = user.tokens.create.token - token = create(:user).tokens.create.token + post user_new_path, :params => { :user => user.attributes } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } + confirm_string = User.find_by(:email => user.email).tokens.create.token - @request.cookies["_osm_session"] = user.display_name - post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token } + post logout_path + session_for(create(:user)) + + post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string } assert_redirected_to login_path assert_match(/Confirmed your account/, flash[:notice]) end def test_confirm_success_no_token_with_referer - user = create(:user, :pending) + user = build(:user, :pending) stub_gravatar_request(user.email) - confirm_string = user.tokens.create(:referer => diary_new_path).token + post user_new_path, :params => { :user => user.attributes } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } + confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token - @request.cookies["_osm_session"] = user.display_name - post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string } - assert_redirected_to login_path(:referer => diary_new_path) + post logout_path + + post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string } + assert_redirected_to login_path(:referer => new_diary_entry_path) assert_match(/Confirmed your account/, flash[:notice]) end def test_confirm_success_good_token_with_referer - user = create(:user, :pending) + user = build(:user, :pending) stub_gravatar_request(user.email) - confirm_string = user.tokens.create(:referer => diary_new_path).token - token = user.tokens.create.token + post user_new_path, :params => { :user => user.attributes } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } + confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token - @request.cookies["_osm_session"] = user.display_name - post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token } - assert_redirected_to diary_new_path + post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string } + assert_redirected_to new_diary_entry_path end def test_confirm_success_bad_token_with_referer - user = create(:user, :pending) + user = build(:user, :pending) stub_gravatar_request(user.email) - confirm_string = user.tokens.create(:referer => diary_new_path).token - token = create(:user).tokens.create.token + post user_new_path, :params => { :user => user.attributes } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } + confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token - @request.cookies["_osm_session"] = user.display_name - post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string }, :session => { :token => token } - assert_redirected_to login_path(:referer => diary_new_path) + post logout_path + session_for(create(:user)) + + post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string } + assert_redirected_to login_path(:referer => new_diary_entry_path) assert_match(/Confirmed your account/, flash[:notice]) end def test_confirm_expired_token - user = create(:user, :pending) - confirm_string = user.tokens.create(:expiry => 1.day.ago).token + user = build(:user, :pending) + stub_gravatar_request(user.email) + post user_new_path, :params => { :user => user.attributes } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } + confirm_string = User.find_by(:email => user.email).tokens.create(:expiry => 1.day.ago).token - @request.cookies["_osm_session"] = user.display_name - post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string } + post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string } assert_redirected_to :action => "confirm" assert_match(/confirmation code has expired/, flash[:error]) end def test_confirm_already_confirmed - user = create(:user) - confirm_string = user.tokens.create(:referer => diary_new_path).token + user = build(:user, :pending) + stub_gravatar_request(user.email) + post user_new_path, :params => { :user => user.attributes } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } + confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token - @request.cookies["_osm_session"] = user.display_name - post :confirm, :params => { :display_name => user.display_name, :confirm_string => confirm_string } - assert_redirected_to :action => "login" + post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string } + assert_redirected_to new_diary_entry_path + + post logout_path + + confirm_string = User.find_by(:email => user.email).tokens.create(:referer => new_diary_entry_path).token + post user_confirm_path, :params => { :display_name => user.display_name, :confirm_string => confirm_string } + assert_redirected_to login_path assert_match(/already been confirmed/, flash[:error]) end def test_confirm_resend_success - user = create(:user, :pending) - session[:token] = user.tokens.create.token + user = build(:user, :pending) + post user_new_path, :params => { :user => user.attributes } + post user_save_path, :params => { :read_ct => 1, :read_tou => 1 } assert_difference "ActionMailer::Base.deliveries.size", 1 do perform_enqueued_jobs do - get :confirm_resend, :params => { :display_name => user.display_name } + get user_confirm_resend_path(user) end end @@ -534,10 +629,13 @@ class UsersControllerTest < ActionController::TestCase end def test_confirm_resend_no_token - user = create(:user, :pending) + user = build(:user, :pending) + # only complete first half of registration + post user_new_path, :params => { :user => user.attributes } + assert_no_difference "ActionMailer::Base.deliveries.size" do perform_enqueued_jobs do - get :confirm_resend, :params => { :display_name => user.display_name } + get user_confirm_resend_path(user) end end @@ -549,7 +647,7 @@ class UsersControllerTest < ActionController::TestCase def test_confirm_resend_unknown_user assert_no_difference "ActionMailer::Base.deliveries.size" do perform_enqueued_jobs do - get :confirm_resend, :params => { :display_name => "No Such User" } + get user_confirm_resend_path(:display_name => "No Such User") end end @@ -562,7 +660,7 @@ class UsersControllerTest < ActionController::TestCase user = create(:user) confirm_string = user.tokens.create.token - get :confirm_email, :params => { :confirm_string => confirm_string } + get user_confirm_email_path, :params => { :confirm_string => confirm_string } assert_response :success assert_template :confirm_email end @@ -572,7 +670,7 @@ class UsersControllerTest < ActionController::TestCase stub_gravatar_request(user.new_email) confirm_string = user.tokens.create.token - post :confirm_email, :params => { :confirm_string => confirm_string } + post user_confirm_email_path, :params => { :confirm_string => confirm_string } assert_response :redirect assert_redirected_to :action => :account, :display_name => user.display_name assert_match(/Confirmed your change of email address/, flash[:notice]) @@ -582,14 +680,14 @@ class UsersControllerTest < ActionController::TestCase user = create(:user) confirm_string = user.tokens.create.token - post :confirm_email, :params => { :confirm_string => confirm_string } + post user_confirm_email_path, :params => { :confirm_string => confirm_string } assert_response :redirect assert_redirected_to :action => :account, :display_name => user.display_name assert_match(/already been confirmed/, flash[:error]) end def test_confirm_email_bad_token - post :confirm_email, :params => { :confirm_string => "XXXXX" } + post user_confirm_email_path, :params => { :confirm_string => "XXXXX" } assert_response :success assert_template :confirm_email assert_match(/confirmation code has expired or does not exist/, flash[:error]) @@ -606,7 +704,7 @@ class UsersControllerTest < ActionController::TestCase confirm_string = user.tokens.create.token # precondition gravatar should be turned off assert_not user.image_use_gravatar - post :confirm_email, :params => { :confirm_string => confirm_string } + post user_confirm_email_path, :params => { :confirm_string => confirm_string } assert_response :redirect assert_redirected_to :action => :account, :display_name => user.display_name assert_match(/Confirmed your change of email address/, flash[:notice]) @@ -621,7 +719,7 @@ class UsersControllerTest < ActionController::TestCase confirm_string = user.tokens.create.token # precondition gravatar should be turned on assert user.image_use_gravatar - post :confirm_email, :params => { :confirm_string => confirm_string } + post user_confirm_email_path, :params => { :confirm_string => confirm_string } assert_response :redirect assert_redirected_to :action => :account, :display_name => user.display_name assert_match(/Confirmed your change of email address/, flash[:notice]) @@ -630,7 +728,19 @@ class UsersControllerTest < ActionController::TestCase end def test_terms_new_user - get :terms, :session => { :new_user => User.new } + user = build(:user, :pending) + + # Set up our user as being half-way through registration + assert_no_difference "User.count" do + assert_no_difference "ActionMailer::Base.deliveries.size" do + perform_enqueued_jobs do + post user_new_path, :params => { :user => user.attributes } + end + end + end + + get user_terms_path + assert_response :success assert_template :terms end @@ -638,9 +748,9 @@ class UsersControllerTest < ActionController::TestCase def test_terms_agreed user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday) - session[:user] = user.id + session_for(user) - get :terms + get user_terms_path assert_response :redirect assert_redirected_to :action => :account, :display_name => user.display_name end @@ -648,67 +758,70 @@ class UsersControllerTest < ActionController::TestCase def test_terms_not_seen_without_referer user = create(:user, :terms_seen => false, :terms_agreed => nil) - session[:user] = user.id + session_for(user) - get :terms + get user_terms_path assert_response :success assert_template :terms - post :save, :params => { :user => { :consider_pd => true } } + post user_save_path, :params => { :user => { :consider_pd => true }, :read_ct => 1, :read_tou => 1 } assert_response :redirect assert_redirected_to :action => :account, :display_name => user.display_name assert_equal "Thanks for accepting the new contributor terms!", flash[:notice] user.reload - assert_equal true, user.consider_pd + assert user.consider_pd assert_not_nil user.terms_agreed - assert_equal true, user.terms_seen + assert user.terms_seen end def test_terms_not_seen_with_referer user = create(:user, :terms_seen => false, :terms_agreed => nil) - session[:user] = user.id + session_for(user) - get :terms, :params => { :referer => "/test" } + get user_terms_path, :params => { :referer => "/test" } assert_response :success assert_template :terms - post :save, :params => { :user => { :consider_pd => true }, :referer => "/test" } + post user_save_path, :params => { :user => { :consider_pd => true }, :referer => "/test", :read_ct => 1, :read_tou => 1 } assert_response :redirect assert_redirected_to "/test" assert_equal "Thanks for accepting the new contributor terms!", flash[:notice] user.reload - assert_equal true, user.consider_pd + assert user.consider_pd assert_not_nil user.terms_agreed - assert_equal true, user.terms_seen + assert user.terms_seen end # Check that if you haven't seen the terms, and make a request that requires authentication, # that your request is redirected to view the terms def test_terms_not_seen_redirection user = create(:user, :terms_seen => false, :terms_agreed => nil) - session[:user] = user.id + session_for(user) - get :account, :params => { :display_name => user.display_name } + get user_account_path(user) assert_response :redirect assert_redirected_to :action => :terms, :referer => "/user/#{ERB::Util.u(user.display_name)}/account" end def test_go_public user = create(:user, :data_public => false) - post :go_public, :session => { :user => user } + session_for(user) + + post user_go_public_path + assert_response :redirect assert_redirected_to :action => :account, :display_name => user.display_name - assert_equal true, User.find(user.id).data_public + assert User.find(user.id).data_public end def test_lost_password # Test fetching the lost password page - get :lost_password + get user_forgot_password_path assert_response :success assert_template :lost_password assert_select "div#notice", false @@ -718,13 +831,23 @@ class UsersControllerTest < ActionController::TestCase user = create(:user) uppercase_user = build(:user, :email => user.email.upcase).tap { |u| u.save(:validate => false) } + # Resetting with GET should fail + assert_no_difference "ActionMailer::Base.deliveries.size" do + perform_enqueued_jobs do + get user_forgot_password_path, :params => { :email => user.email } + end + end + assert_response :success + assert_template :lost_password + + # Resetting with POST should work assert_difference "ActionMailer::Base.deliveries.size", 1 do perform_enqueued_jobs do - post :lost_password, :params => { :user => { :email => user.email } } + post user_forgot_password_path, :params => { :email => user.email } end end assert_response :redirect - assert_redirected_to :action => :login + assert_redirected_to login_path assert_match(/^Sorry you lost it/, flash[:notice]) email = ActionMailer::Base.deliveries.first assert_equal 1, email.to.count @@ -735,11 +858,11 @@ class UsersControllerTest < ActionController::TestCase # that has the same address in a different case assert_difference "ActionMailer::Base.deliveries.size", 1 do perform_enqueued_jobs do - post :lost_password, :params => { :user => { :email => user.email.upcase } } + post user_forgot_password_path, :params => { :email => user.email.upcase } end end assert_response :redirect - assert_redirected_to :action => :login + assert_redirected_to login_path assert_match(/^Sorry you lost it/, flash[:notice]) email = ActionMailer::Base.deliveries.first assert_equal 1, email.to.count @@ -750,7 +873,7 @@ class UsersControllerTest < ActionController::TestCase # for more than one user but not an exact match for either assert_no_difference "ActionMailer::Base.deliveries.size" do perform_enqueued_jobs do - post :lost_password, :params => { :user => { :email => user.email.titlecase } } + post user_forgot_password_path, :params => { :email => user.email.titlecase } end end assert_response :success @@ -762,11 +885,11 @@ class UsersControllerTest < ActionController::TestCase third_user = create(:user) assert_difference "ActionMailer::Base.deliveries.size", 1 do perform_enqueued_jobs do - post :lost_password, :params => { :user => { :email => third_user.email } } + post user_forgot_password_path, :params => { :email => third_user.email } end end assert_response :redirect - assert_redirected_to :action => :login + assert_redirected_to login_path assert_match(/^Sorry you lost it/, flash[:notice]) email = ActionMailer::Base.deliveries.first assert_equal 1, email.to.count @@ -777,11 +900,11 @@ class UsersControllerTest < ActionController::TestCase # same (case insensitively unique) address in a different case assert_difference "ActionMailer::Base.deliveries.size", 1 do perform_enqueued_jobs do - post :lost_password, :params => { :user => { :email => third_user.email.upcase } } + post user_forgot_password_path, :params => { :email => third_user.email.upcase } end end assert_response :redirect - assert_redirected_to :action => :login + assert_redirected_to login_path assert_match(/^Sorry you lost it/, flash[:notice]) email = ActionMailer::Base.deliveries.first assert_equal 1, email.to.count @@ -792,11 +915,11 @@ class UsersControllerTest < ActionController::TestCase def test_reset_password user = create(:user, :pending) # Test a request with no token - get :reset_password + get user_reset_password_path assert_response :bad_request # Test a request with a bogus token - get :reset_password, :params => { :token => "made_up_token" } + get user_reset_password_path, :params => { :token => "made_up_token" } assert_response :redirect assert_redirected_to :action => :lost_password @@ -804,24 +927,24 @@ class UsersControllerTest < ActionController::TestCase token = user.tokens.create # Test a request with a valid token - get :reset_password, :params => { :token => token.token } + get user_reset_password_path, :params => { :token => token.token } assert_response :success assert_template :reset_password # Test that errors are reported for erroneous submissions - post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "different_password" } } + post user_reset_password_path, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "different_password" } } assert_response :success assert_template :reset_password - assert_select "div#errorExplanation" + assert_select "div.invalid-feedback" # Test setting a new password - post :reset_password, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" } } + post user_reset_password_path, :params => { :token => token.token, :user => { :pass_crypt => "new_password", :pass_crypt_confirmation => "new_password" } } assert_response :redirect assert_redirected_to root_path assert_equal user.id, session[:user] user.reload assert_equal "active", user.status - assert_equal true, user.email_valid + assert user.email_valid assert_equal user, User.authenticate(:username => user.email, :password => "new_password") end @@ -835,16 +958,18 @@ class UsersControllerTest < ActionController::TestCase # Make sure that you are redirected to the login page when # you are not logged in - get :account, :params => { :display_name => user.display_name } + get user_account_path(user) assert_response :redirect - assert_redirected_to :action => "login", :referer => "/user/#{ERB::Util.u(user.display_name)}/account" + assert_redirected_to login_path(:referer => "/user/#{ERB::Util.u(user.display_name)}/account") # Make sure that you are blocked when not logged in as the right user - get :account, :params => { :display_name => user.display_name }, :session => { :user => create(:user) } + session_for(create(:user)) + get user_account_path(user) assert_response :forbidden # Make sure we get the page when we are logged in as the right user - get :account, :params => { :display_name => user.display_name }, :session => { :user => user } + session_for(user) + get user_account_path(user) assert_response :success assert_template :account assert_select "form#accountForm" do |form| @@ -853,99 +978,119 @@ class UsersControllerTest < ActionController::TestCase assert_equal "/user/#{ERB::Util.u(user.display_name)}/account", form.attr("action").to_s end + # Updating the description using GET should fail + user.description = "new description" + user.preferred_editor = "default" + get user_account_path(user), :params => { :user => user.attributes } + assert_response :success + assert_template :account + assert_not_equal user.description, User.find(user.id).description + # Updating the description should work user.description = "new description" user.preferred_editor = "default" - post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user } + post user_account_path(user), :params => { :user => user.attributes } + assert_response :redirect + assert_redirected_to user_account_url(user) + get user_account_path(user) assert_response :success assert_template :account - assert_select "div#errorExplanation", false assert_select ".notice", /^User information updated successfully/ - assert_select "form#accountForm > fieldset > div.form-row > div#user_description_container > div#user_description_content > textarea#user_description", user.description + assert_select "form#accountForm > div.form-group > div#user_description_container > div#user_description_content > textarea#user_description", user.description # Changing to a invalid editor should fail user.preferred_editor = "unknown" - post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user } + post user_account_path(user), :params => { :user => user.attributes } assert_response :success assert_template :account assert_select ".notice", false - assert_select "div#errorExplanation" - assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false + assert_select "form#accountForm > div.form-group > select#user_preferred_editor > option[selected]", false # Changing to a valid editor should work - user.preferred_editor = "potlatch2" - post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user } + user.preferred_editor = "id" + post user_account_path(user), :params => { :user => user.attributes } + assert_response :redirect + assert_redirected_to user_account_url(user) + get user_account_path(user) assert_response :success assert_template :account - assert_select "div#errorExplanation", false assert_select ".notice", /^User information updated successfully/ - assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected][value=?]", "potlatch2" + assert_select "form#accountForm > div.form-group > select#user_preferred_editor > option[selected][value=?]", "id" # Changing to the default editor should work user.preferred_editor = "default" - post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user } + post user_account_path(user), :params => { :user => user.attributes } + assert_response :redirect + assert_redirected_to user_account_url(user) + get user_account_path(user) assert_response :success assert_template :account - assert_select "div#errorExplanation", false assert_select ".notice", /^User information updated successfully/ - assert_select "form#accountForm > fieldset > div.form-row > select#user_preferred_editor > option[selected]", false + assert_select "form#accountForm > div.form-group > select#user_preferred_editor > option[selected]", false # Changing to an uploaded image should work image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif") - post :account, :params => { :display_name => user.display_name, :image_action => "new", :user => user.attributes.merge(:image => image) }, :session => { :user => user } + post user_account_path(user), :params => { :avatar_action => "new", :user => user.attributes.merge(:avatar => image) } + assert_response :redirect + assert_redirected_to user_account_url(user) + get user_account_path(user) assert_response :success assert_template :account - assert_select "div#errorExplanation", false assert_select ".notice", /^User information updated successfully/ - assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "keep" + assert_select "form#accountForm > fieldset.form-group > div.form-row > div.col-sm-10 > div.form-check > input[name=avatar_action][checked][value=?]", "keep" # Changing to a gravatar image should work - post :account, :params => { :display_name => user.display_name, :image_action => "gravatar", :user => user.attributes }, :session => { :user => user } + post user_account_path(user), :params => { :avatar_action => "gravatar", :user => user.attributes } + assert_response :redirect + assert_redirected_to user_account_url(user) + get user_account_path(user) assert_response :success assert_template :account - assert_select "div#errorExplanation", false assert_select ".notice", /^User information updated successfully/ - assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked][value=?]", "gravatar" + assert_select "form#accountForm > fieldset.form-group > div.form-row > div.col-sm-10 > div.form-group > div.form-check > input[name=avatar_action][checked][value=?]", "gravatar" # Removing the image should work - post :account, :params => { :display_name => user.display_name, :image_action => "delete", :user => user.attributes }, :session => { :user => user } + post user_account_path(user), :params => { :avatar_action => "delete", :user => user.attributes } + assert_response :redirect + assert_redirected_to user_account_url(user) + get user_account_path(user) assert_response :success assert_template :account - assert_select "div#errorExplanation", false assert_select ".notice", /^User information updated successfully/ - assert_select "form#accountForm > fieldset > div.form-row.accountImage input[name=image_action][checked]", false + assert_select "form#accountForm > fieldset.form-group > div.form-row > div.col-sm-10 > div.form-check > input[name=avatar_action][checked]", false + assert_select "form#accountForm > fieldset.form-group > div.form-row > div.col-sm-10 > div.form-group > div.form-check > input[name=avatar_action][checked]", false # Adding external authentication should redirect to the auth provider - post :account, :params => { :display_name => user.display_name, :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }, :session => { :user => user } + post user_account_path(user), :params => { :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") } assert_response :redirect 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") # Changing name to one that exists should fail new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name) - post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user } + post user_account_path(user), :params => { :user => new_attributes } assert_response :success assert_template :account assert_select ".notice", false - assert_select "div#errorExplanation" - assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name" + assert_select "form#accountForm > div.form-group > input.is-invalid#user_display_name" # Changing name to one that exists should fail, regardless of case new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase) - post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user } + post user_account_path(user), :params => { :user => new_attributes } assert_response :success assert_template :account assert_select ".notice", false - assert_select "div#errorExplanation" - assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_display_name" + assert_select "form#accountForm > div.form-group > input.is-invalid#user_display_name" # Changing name to one that doesn't exist should work new_attributes = user.attributes.dup.merge(:display_name => "new tester") - post :account, :params => { :display_name => user.display_name, :user => new_attributes }, :session => { :user => user } + post user_account_path(user), :params => { :user => new_attributes } + assert_response :redirect + assert_redirected_to user_account_url(:display_name => "new tester") + get user_account_path(:display_name => "new tester") assert_response :success assert_template :account - assert_select "div#errorExplanation", false assert_select ".notice", /^User information updated successfully/ - assert_select "form#accountForm > fieldset > div.form-row > input#user_display_name[value=?]", "new tester" + assert_select "form#accountForm > div.form-group > input#user_display_name[value=?]", "new tester" # Record the change of name user.display_name = "new tester" @@ -954,40 +1099,40 @@ class UsersControllerTest < ActionController::TestCase user.new_email = create(:user).email assert_no_difference "ActionMailer::Base.deliveries.size" do perform_enqueued_jobs do - post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user } + post user_account_path(user), :params => { :user => user.attributes } end end assert_response :success assert_template :account assert_select ".notice", false - assert_select "div#errorExplanation" - assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email" + assert_select "form#accountForm > div.form-group > input.is-invalid#user_new_email" # Changing email to one that exists should fail, regardless of case user.new_email = create(:user).email.upcase assert_no_difference "ActionMailer::Base.deliveries.size" do perform_enqueued_jobs do - post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user } + post user_account_path(user), :params => { :user => user.attributes } end end assert_response :success assert_template :account assert_select ".notice", false - assert_select "div#errorExplanation" - assert_select "form#accountForm > fieldset > div.form-row > input.field_with_errors#user_new_email" + assert_select "form#accountForm > div.form-group > input.is-invalid#user_new_email" # Changing email to one that doesn't exist should work user.new_email = "new_tester@example.com" assert_difference "ActionMailer::Base.deliveries.size", 1 do perform_enqueued_jobs do - post :account, :params => { :display_name => user.display_name, :user => user.attributes }, :session => { :user => user } + post user_account_path(user), :params => { :user => user.attributes } end end + assert_response :redirect + assert_redirected_to user_account_url(user) + get user_account_path(user) assert_response :success assert_template :account - assert_select "div#errorExplanation", false assert_select ".notice", /^User information updated successfully/ - assert_select "form#accountForm > fieldset > div.form-row > input#user_new_email[value=?]", user.new_email + assert_select "form#accountForm > div.form-group > input#user_new_email[value=?]", user.new_email email = ActionMailer::Base.deliveries.first assert_equal 1, email.to.count assert_equal user.new_email, email.to.first @@ -998,16 +1143,16 @@ class UsersControllerTest < ActionController::TestCase # information for the user def test_show # Test a non-existent user - get :show, :params => { :display_name => "unknown" } + get user_path(:display_name => "unknown") assert_response :not_found # Test a normal user user = create(:user, :home_lon => 1.1, :home_lat => 1.1) friend_user = create(:user, :home_lon => 1.2, :home_lat => 1.2) - create(:friend, :befriender => user, :befriendee => friend_user) + create(:friendship, :befriender => user, :befriendee => friend_user) create(:changeset, :user => friend_user) - get :show, :params => { :display_name => user.display_name } + get user_path(user) assert_response :success assert_select "div#userinformation" do assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1 @@ -1026,7 +1171,7 @@ class UsersControllerTest < ActionController::TestCase # Test a user who has been blocked blocked_user = create(:user) create(:user_block, :user => blocked_user) - get :show, :params => { :display_name => blocked_user.display_name } + get user_path(blocked_user) assert_response :success assert_select "div#userinformation" do assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1 @@ -1042,7 +1187,7 @@ class UsersControllerTest < ActionController::TestCase # Test a moderator who has applied blocks moderator_user = create(:moderator_user) create(:user_block, :creator => moderator_user) - get :show, :params => { :display_name => moderator_user.display_name } + get user_path(moderator_user) assert_response :success assert_select "div#userinformation" do assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1 @@ -1056,10 +1201,10 @@ class UsersControllerTest < ActionController::TestCase end # Login as a normal user - session[:user] = user.id + session_for(user) # Test the normal user - get :show, :params => { :display_name => user.display_name } + get user_path(user) assert_response :success assert_select "div#userinformation" do assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1 @@ -1078,10 +1223,10 @@ class UsersControllerTest < ActionController::TestCase end # Login as a moderator - session[:user] = create(:moderator_user).id + session_for(create(:moderator_user)) # Test the normal user - get :show, :params => { :display_name => user.display_name } + get user_path(user) assert_response :success assert_select "div#userinformation" do assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1 @@ -1101,13 +1246,13 @@ class UsersControllerTest < ActionController::TestCase seen_user = create(:user, :terms_seen => true, :terms_agreed => nil) not_seen_user = create(:user, :terms_seen => false, :terms_agreed => nil) - get :show, :params => { :display_name => agreed_user.display_name } + get user_path(agreed_user) assert_response :success assert_select "div#userinformation" do assert_select "p", :count => 0, :text => /Contributor terms/ end - get :show, :params => { :display_name => seen_user.display_name } + get user_path(seen_user) assert_response :success # put @response.body assert_select "div#userinformation" do @@ -1115,7 +1260,7 @@ class UsersControllerTest < ActionController::TestCase assert_select "p", /Declined/ end - get :show, :params => { :display_name => not_seen_user.display_name } + get user_path(not_seen_user) assert_response :success assert_select "div#userinformation" do assert_select "p", :count => 1, :text => /Contributor terms/ @@ -1123,376 +1268,43 @@ class UsersControllerTest < ActionController::TestCase end end - def test_api_read - user = create(:user, :description => "test", :terms_agreed => Date.yesterday) - # check that a visible user is returned properly - get :api_read, :params => { :id => user.id } - assert_response :success - assert_equal "text/xml", response.content_type - - # check the data that is returned - assert_select "description", :count => 1, :text => "test" - assert_select "contributor-terms", :count => 1 do - assert_select "[agreed='true']" - end - assert_select "img", :count => 0 - assert_select "roles", :count => 1 do - assert_select "role", :count => 0 - end - assert_select "changesets", :count => 1 do - assert_select "[count='0']" - end - assert_select "traces", :count => 1 do - assert_select "[count='0']" - end - assert_select "blocks", :count => 1 do - assert_select "received", :count => 1 do - assert_select "[count='0'][active='0']" - end - assert_select "issued", :count => 0 - end - - # check that we aren't revealing private information - assert_select "contributor-terms[pd]", false - assert_select "home", false - assert_select "languages", false - assert_select "messages", false - - # check that a suspended user is not returned - get :api_read, :params => { :id => create(:user, :suspended).id } - assert_response :gone - - # check that a deleted user is not returned - get :api_read, :params => { :id => create(:user, :deleted).id } - assert_response :gone - - # check that a non-existent user is not returned - get :api_read, :params => { :id => 0 } - assert_response :not_found - end - - def test_api_details - user = create(:user, :description => "test", :terms_agreed => Date.yesterday, :home_lat => 12.1, :home_lon => 12.1, :languages => ["en"]) - create(:message, :read, :recipient => user) - create(:message, :sender => user) - - # check that nothing is returned when not logged in - get :api_details - assert_response :unauthorized - - # check that we get a response when logged in - basic_authorization user.email, "test" - get :api_details - assert_response :success - assert_equal "text/xml", response.content_type - - # check the data that is returned - assert_select "description", :count => 1, :text => "test" - assert_select "contributor-terms", :count => 1 do - assert_select "[agreed='true'][pd='false']" - end - assert_select "img", :count => 0 - assert_select "roles", :count => 1 do - assert_select "role", :count => 0 - end - assert_select "changesets", :count => 1 do - assert_select "[count='0']", :count => 1 - end - assert_select "traces", :count => 1 do - assert_select "[count='0']", :count => 1 - end - assert_select "blocks", :count => 1 do - assert_select "received", :count => 1 do - assert_select "[count='0'][active='0']" - end - assert_select "issued", :count => 0 - end - assert_select "home", :count => 1 do - assert_select "[lat='12.1'][lon='12.1'][zoom='3']" - end - assert_select "languages", :count => 1 do - assert_select "lang", :count => 1, :text => "en" - end - assert_select "messages", :count => 1 do - assert_select "received", :count => 1 do - assert_select "[count='1'][unread='0']" - end - assert_select "sent", :count => 1 do - assert_select "[count='1']" - end - end - end - - def test_api_users - user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday) - user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday) - user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday) - - get :api_users, :params => { :users => user1.id } - assert_response :success - assert_equal "text/xml", response.content_type - assert_select "user", :count => 1 do - assert_select "user[id='#{user1.id}']", :count => 1 - assert_select "user[id='#{user2.id}']", :count => 0 - assert_select "user[id='#{user3.id}']", :count => 0 - end - - get :api_users, :params => { :users => user2.id } - assert_response :success - assert_equal "text/xml", response.content_type - assert_select "user", :count => 1 do - assert_select "user[id='#{user1.id}']", :count => 0 - assert_select "user[id='#{user2.id}']", :count => 1 - assert_select "user[id='#{user3.id}']", :count => 0 - end - - get :api_users, :params => { :users => "#{user1.id},#{user3.id}" } - assert_response :success - assert_equal "text/xml", response.content_type - assert_select "user", :count => 2 do - assert_select "user[id='#{user1.id}']", :count => 1 - assert_select "user[id='#{user2.id}']", :count => 0 - assert_select "user[id='#{user3.id}']", :count => 1 - end - - get :api_users, :params => { :users => create(:user, :suspended).id } - assert_response :not_found - - get :api_users, :params => { :users => create(:user, :deleted).id } - assert_response :not_found - - get :api_users, :params => { :users => 0 } - assert_response :not_found - end - - def test_api_gpx_files - user = create(:user) - trace1 = create(:trace, :user => user) do |trace| - create(:tracetag, :trace => trace, :tag => "London") - end - trace2 = create(:trace, :user => user) do |trace| - create(:tracetag, :trace => trace, :tag => "Birmingham") - end - # check that nothing is returned when not logged in - get :api_gpx_files - assert_response :unauthorized - - # check that we get a response when logged in - basic_authorization user.email, "test" - get :api_gpx_files - assert_response :success - assert_equal "application/xml", response.content_type - - # check the data that is returned - assert_select "gpx_file[id='#{trace1.id}']", 1 do - assert_select "tag", "London" - end - assert_select "gpx_file[id='#{trace2.id}']", 1 do - assert_select "tag", "Birmingham" - end - end - - def test_make_friend - # Get users to work with - user = create(:user) - friend = create(:user) - - # Check that the users aren't already friends - assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - - # When not logged in a GET should ask us to login - get :make_friend, :params => { :display_name => friend.display_name } - assert_redirected_to :action => "login", :referer => make_friend_path(:display_name => friend.display_name) - - # When not logged in a POST should error - post :make_friend, :params => { :display_name => friend.display_name } - assert_response :forbidden - assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - - # When logged in a GET should get a confirmation page - get :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user } - assert_response :success - assert_template :make_friend - assert_select "form" do - assert_select "input[type='hidden'][name='referer']", 0 - assert_select "input[type='submit']", 1 - end - assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - - # When logged in a POST should add the friendship - assert_difference "ActionMailer::Base.deliveries.size", 1 do - perform_enqueued_jobs do - post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user } - end - end - assert_redirected_to user_path(friend) - assert_match(/is now your friend/, flash[:notice]) - assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - email = ActionMailer::Base.deliveries.first - assert_equal 1, email.to.count - assert_equal friend.email, email.to.first - ActionMailer::Base.deliveries.clear - - # A second POST should report that the friendship already exists - assert_no_difference "ActionMailer::Base.deliveries.size" do - perform_enqueued_jobs do - post :make_friend, :params => { :display_name => friend.display_name }, :session => { :user => user } - end - end - assert_redirected_to user_path(friend) - assert_match(/You are already friends with/, flash[:warning]) - assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - end - - def test_make_friend_with_referer - # Get users to work with - user = create(:user) - friend = create(:user) - - # Check that the users aren't already friends - assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - - # The GET should preserve any referer - get :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user } - assert_response :success - assert_template :make_friend - assert_select "form" do - assert_select "input[type='hidden'][name='referer'][value='/test']", 1 - assert_select "input[type='submit']", 1 - end - assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - - # When logged in a POST should add the friendship and refer us - assert_difference "ActionMailer::Base.deliveries.size", 1 do - perform_enqueued_jobs do - post :make_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user } - end - end - assert_redirected_to "/test" - assert_match(/is now your friend/, flash[:notice]) - assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - email = ActionMailer::Base.deliveries.first - assert_equal 1, email.to.count - assert_equal friend.email, email.to.first - ActionMailer::Base.deliveries.clear - end - - def test_make_friend_unkown_user - # Should error when a bogus user is specified - get :make_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) } - assert_response :not_found - assert_template :no_such_user - end - - def test_remove_friend - # Get users to work with - user = create(:user) - friend = create(:user) - create(:friend, :befriender => user, :befriendee => friend) - - # Check that the users are friends - assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - - # When not logged in a GET should ask us to login - get :remove_friend, :params => { :display_name => friend.display_name } - assert_redirected_to :action => "login", :referer => remove_friend_path(:display_name => friend.display_name) - - # When not logged in a POST should error - post :remove_friend, :params => { :display_name => friend.display_name } - assert_response :forbidden - assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - - # When logged in a GET should get a confirmation page - get :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user } - assert_response :success - assert_template :remove_friend - assert_select "form" do - assert_select "input[type='hidden'][name='referer']", 0 - assert_select "input[type='submit']", 1 - end - assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - - # When logged in a POST should remove the friendship - post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user } - assert_redirected_to user_path(friend) - assert_match(/was removed from your friends/, flash[:notice]) - assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - - # A second POST should report that the friendship does not exist - post :remove_friend, :params => { :display_name => friend.display_name }, :session => { :user => user } - assert_redirected_to user_path(friend) - assert_match(/is not one of your friends/, flash[:error]) - assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - end - - def test_remove_friend_with_referer - # Get users to work with - user = create(:user) - friend = create(:user) - create(:friend, :user_id => user.id, :friend_user_id => friend.id) - - # Check that the users are friends - assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - - # The GET should preserve any referer - get :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user } - assert_response :success - assert_template :remove_friend - assert_select "form" do - assert_select "input[type='hidden'][name='referer'][value='/test']", 1 - assert_select "input[type='submit']", 1 - end - assert Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - - # When logged in a POST should remove the friendship and refer - post :remove_friend, :params => { :display_name => friend.display_name, :referer => "/test" }, :session => { :user => user } - assert_redirected_to "/test" - assert_match(/was removed from your friends/, flash[:notice]) - assert_nil Friend.where(:user_id => user.id, :friend_user_id => friend.id).first - end - - def test_remove_friend_unkown_user - # Should error when a bogus user is specified - get :remove_friend, :params => { :display_name => "No Such User" }, :session => { :user => create(:user) } - assert_response :not_found - assert_template :no_such_user - end - def test_set_status user = create(:user) # Try without logging in - get :set_status, :params => { :display_name => user.display_name, :status => "suspended" } - assert_response :redirect - assert_redirected_to :action => :login, :referer => set_status_user_path(:status => "suspended") + post set_status_user_path(user), :params => { :status => "suspended" } + assert_response :forbidden # Now try as a normal user - get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user } + session_for(user) + post set_status_user_path(user), :params => { :status => "suspended" } assert_response :redirect assert_redirected_to :controller => :errors, :action => :forbidden # Finally try as an administrator - get :set_status, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) } + session_for(create(:administrator_user)) + post set_status_user_path(user), :params => { :status => "suspended" } assert_response :redirect assert_redirected_to :action => :show, :display_name => user.display_name assert_equal "suspended", User.find(user.id).status end - def test_delete + def test_destroy user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test") # Try without logging in - get :delete, :params => { :display_name => user.display_name, :status => "suspended" } - assert_response :redirect - assert_redirected_to :action => :login, :referer => delete_user_path(:status => "suspended") + delete user_path(user), :params => { :status => "suspended" } + assert_response :forbidden # Now try as a normal user - get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => user } + session_for(user) + delete user_path(user), :params => { :status => "suspended" } assert_response :redirect assert_redirected_to :controller => :errors, :action => :forbidden # Finally try as an administrator - get :delete, :params => { :display_name => user.display_name, :status => "suspended" }, :session => { :user => create(:administrator_user) } + session_for(create(:administrator_user)) + delete user_path(user), :params => { :status => "suspended" } assert_response :redirect assert_redirected_to :action => :show, :display_name => user.display_name @@ -1502,8 +1314,8 @@ class UsersControllerTest < ActionController::TestCase assert_equal "", user.description assert_nil user.home_lat assert_nil user.home_lon - assert_equal false, user.image.file? - assert_equal false, user.email_valid + assert_not user.avatar.attached? + assert_not user.email_valid assert_nil user.new_email assert_nil user.auth_provider assert_nil user.auth_uid @@ -1522,41 +1334,41 @@ class UsersControllerTest < ActionController::TestCase assert_equal 7, User.count # Shouldn't work when not logged in - get :index + get users_path assert_response :redirect - assert_redirected_to :action => :login, :referer => users_path + assert_redirected_to login_path(:referer => users_path) - session[:user] = user.id + session_for(user) # Shouldn't work when logged in as a normal user - get :index + get users_path assert_response :redirect assert_redirected_to :controller => :errors, :action => :forbidden - session[:user] = moderator_user.id + session_for(moderator_user) # Shouldn't work when logged in as a moderator - get :index + get users_path assert_response :redirect assert_redirected_to :controller => :errors, :action => :forbidden - session[:user] = administrator_user.id + session_for(administrator_user) # Note there is a header row, so all row counts are users + 1 # Should work when logged in as an administrator - get :index + get users_path assert_response :success assert_template :index assert_select "table#user_list tr", :count => 7 + 1 # Should be able to limit by status - get :index, :params => { :status => "suspended" } + get users_path, :params => { :status => "suspended" } assert_response :success assert_template :index assert_select "table#user_list tr", :count => 1 + 1 # Should be able to limit by IP address - get :index, :params => { :ip => "1.2.3.4" } + get users_path, :params => { :ip => "1.2.3.4" } assert_response :success assert_template :index assert_select "table#user_list tr", :count => 1 + 1 @@ -1569,22 +1381,22 @@ class UsersControllerTest < ActionController::TestCase :pass_crypt => "extraextra") end - session[:user] = create(:administrator_user).id + session_for(create(:administrator_user)) # 100 examples, an administrator, and a granter for the admin. assert_equal 102, User.count - get :index + get users_path assert_response :success assert_template :index assert_select "table#user_list tr", :count => 51 - get :index, :params => { :page => 2 } + get users_path, :params => { :page => 2 } assert_response :success assert_template :index assert_select "table#user_list tr", :count => 51 - get :index, :params => { :page => 3 } + get users_path, :params => { :page => 3 } assert_response :success assert_template :index assert_select "table#user_list tr", :count => 3 @@ -1596,40 +1408,40 @@ class UsersControllerTest < ActionController::TestCase # Shouldn't work when not logged in assert_no_difference "User.active.count" do - post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } } + post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } } end assert_response :forbidden assert_equal "pending", inactive_user.reload.status assert_equal "suspended", suspended_user.reload.status - session[:user] = create(:user).id + session_for(create(:user)) # Shouldn't work when logged in as a normal user assert_no_difference "User.active.count" do - post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } } + post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } } end assert_response :redirect assert_redirected_to :controller => :errors, :action => :forbidden assert_equal "pending", inactive_user.reload.status assert_equal "suspended", suspended_user.reload.status - session[:user] = create(:moderator_user).id + session_for(create(:moderator_user)) # Shouldn't work when logged in as a moderator assert_no_difference "User.active.count" do - post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } } + post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } } end assert_response :redirect assert_redirected_to :controller => :errors, :action => :forbidden assert_equal "pending", inactive_user.reload.status assert_equal "suspended", suspended_user.reload.status - session[:user] = create(:administrator_user).id + session_for(create(:administrator_user)) # Should work when logged in as an administrator assert_difference "User.active.count", 2 do - post :index, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } } + post users_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } } end assert_response :redirect assert_redirected_to :action => :index @@ -1643,40 +1455,40 @@ class UsersControllerTest < ActionController::TestCase # Shouldn't work when not logged in assert_no_difference "User.active.count" do - post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } } + post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } } end assert_response :forbidden assert_equal "active", normal_user.reload.status assert_equal "confirmed", confirmed_user.reload.status - session[:user] = create(:user).id + session_for(create(:user)) # Shouldn't work when logged in as a normal user assert_no_difference "User.active.count" do - post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } } + post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } } end assert_response :redirect assert_redirected_to :controller => :errors, :action => :forbidden assert_equal "active", normal_user.reload.status assert_equal "confirmed", confirmed_user.reload.status - session[:user] = create(:moderator_user).id + session_for(create(:moderator_user)) # Shouldn't work when logged in as a moderator assert_no_difference "User.active.count" do - post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } } + post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } } end assert_response :redirect assert_redirected_to :controller => :errors, :action => :forbidden assert_equal "active", normal_user.reload.status assert_equal "confirmed", confirmed_user.reload.status - session[:user] = create(:administrator_user).id + session_for(create(:administrator_user)) # Should work when logged in as an administrator assert_difference "User.active.count", -2 do - post :index, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } } + post users_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } } end assert_response :redirect assert_redirected_to :action => :index