The personal finance app for everyone (by everyone)
Ruby
fix(auth): return 400 on mobile signup when user payload is missing (#4869)
sentry[bot]d0135879d93a
diff --git a/app/controllers/api/v1/auth_controller.rb b/app/controllers/api/v1/auth_controller.rbindex bf96a55153..997ed1c03a 100644--- a/app/controllers/api/v1/auth_controller.rb+++ b/app/controllers/api/v1/auth_controller.rb@@ -35,7 +35,9 @@ module Apiend# Validate password- password_errors = validate_password(params[:user][:password])+ # user_signup_params requires the `user` key; a missing payload raises+ # ParameterMissing, which BaseController renders as a 400.+ password_errors = validate_password(user_signup_params[:password])if password_errors.any?render json: { errors: password_errors }, status: :unprocessable_entityreturndiff --git a/docs/api/openapi.yaml b/docs/api/openapi.yamlindex 48c6a95596..fea2c23c3b 100644--- a/docs/api/openapi.yaml+++ b/docs/api/openapi.yaml@@ -5276,6 +5276,13 @@ paths:application/json:schema:"$ref": "#/components/schemas/ErrorResponse"+ '400':+ description: 'bad request: the user payload is missing, or device information+ is missing or invalid'+ content:+ application/json:+ schema:+ "$ref": "#/components/schemas/ErrorResponse"requestBody:content:application/json:diff --git a/spec/requests/api/v1/auth_spec.rb b/spec/requests/api/v1/auth_spec.rbindex f5dd5fe491..ddb696bbb4 100644--- a/spec/requests/api/v1/auth_spec.rb+++ b/spec/requests/api/v1/auth_spec.rb@@ -71,6 +71,11 @@ RSpec.describe 'API V1 Auth', type: :request doschema '$ref' => '#/components/schemas/ErrorResponse'run_test!end++ response '400', 'bad request: the user payload is missing, or device information is missing or invalid' do+ schema '$ref' => '#/components/schemas/ErrorResponse'+ run_test!+ endendenddiff --git a/test/controllers/api/v1/auth_controller_test.rb b/test/controllers/api/v1/auth_controller_test.rbindex c5200ee008..814fa8ff28 100644--- a/test/controllers/api/v1/auth_controller_test.rb+++ b/test/controllers/api/v1/auth_controller_test.rb@@ -128,6 +128,19 @@ class Api::V1::AuthControllerTest < ActionDispatch::IntegrationTestassert_response :bad_requestend+ test "should return bad request when signup payload has no user key" do+ assert_no_difference([ "User.count", "MobileDevice.count", "Doorkeeper::AccessToken.count" ]) do+ post "/api/v1/auth/signup", params: {+ email: "[email protected]",+ password: "SecurePass123!",+ device: @device_info+ }, as: :json+ end++ assert_response :bad_request+ assert_equal "bad_request", JSON.parse(response.body)["error"]+ end+test "should not signup with invalid password" doassert_no_difference("User.count") dopost "/api/v1/auth/signup", params: {