Skip to content

Commit

Permalink
fix validation of email and password in signup API
Browse files Browse the repository at this point in the history
Signed-off-by: Arnav Gupta <championswimmer@gmail.com>
  • Loading branch information
championswimmer committed Nov 10, 2023
1 parent 0350676 commit 95d2d80
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/dtos/http_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ type UserResponse struct {
}

type ErrorResponse struct {
Status uint `json:"status"`
Status int `json:"status"`
Message string `json:"message"`
}

func UserResponseFromUser(user *models.User) UserResponse {
func CreateUserResponseFromUser(user *models.User) UserResponse {
return UserResponse{
ID: user.ID,
Email: user.Email,
}
}

func GetErrorResponse(status uint, message string) ErrorResponse {
func CreateErrorResponse(status int, message string) ErrorResponse {
return ErrorResponse{
Status: status,
Message: message,
Expand Down
16 changes: 13 additions & 3 deletions src/routes/api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,27 @@ func UsersRoute(db *gorm.DB) func(router fiber.Router) {
func registerUser(ctx *fiber.Ctx) error {
var u = new(dtos.CreateUserRequest)
if err := ctx.BodyParser(u); err != nil {
return ctx.Status(fiber.StatusBadRequest).JSON(dtos.GetErrorResponse(fiber.StatusBadRequest, "The request body is not valid"))
return ctx.Status(fiber.StatusBadRequest).JSON(dtos.CreateErrorResponse(
fiber.StatusBadRequest,
"The request body is not valid",
))
}

if u.Email == "" || u.Password == "" {
return ctx.Status(fiber.StatusUnprocessableEntity).JSON(dtos.CreateErrorResponse(
fiber.StatusUnprocessableEntity,
"email and password are required to create user",
))
}

savedUser, err := usersController.Create(u.Email, u.Password)
if err != nil {
if errors.Is(err, gorm.ErrDuplicatedKey) {
return ctx.Status(fiber.StatusConflict).JSON(dtos.GetErrorResponse(fiber.StatusConflict, "User with this email already exists"))
return ctx.Status(fiber.StatusConflict).JSON(dtos.CreateErrorResponse(fiber.StatusConflict, "User with this email already exists"))
}
}

return ctx.Status(fiber.StatusCreated).JSON(dtos.UserResponseFromUser(savedUser))
return ctx.Status(fiber.StatusCreated).JSON(dtos.CreateUserResponseFromUser(savedUser))
}

func loginUser(ctx *fiber.Ctx) error {
Expand Down
9 changes: 4 additions & 5 deletions tests/routes/api/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestUsersRoute_RegisterUserDuplicateFail(t *testing.T) {
}

assert.Equal(t, 409, resp.StatusCode)
assert.Equal(t, uint(409), responseBody.Status)
assert.Equal(t, 409, responseBody.Status)
assert.Equal(t, "User with this email already exists", responseBody.Message)
}

Expand All @@ -70,7 +70,7 @@ func TestUsersRoute_RegisterUserBodyParsingFail(t *testing.T) {
}

assert.Equal(t, 400, resp.StatusCode)
assert.Equal(t, uint(400), responseBody.Status)
assert.Equal(t, 400, responseBody.Status)
assert.Equal(t, "The request body is not valid", responseBody.Message)
}

Expand Down Expand Up @@ -99,13 +99,12 @@ func TestUsersRoute_ShouldNotRegisterUserWhenNoPassword(t *testing.T) {
req := httptest.NewRequest("POST", "/api/v1/users", bytes.NewBuffer(reqBody))
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
resp := lo.Must(app.Test(req))
assert.Equal(t, 400, resp.StatusCode)
assert.Equal(t, 422, resp.StatusCode)
}
func TestUsersRoute_ShouldNotRegisterUserWhenNoEmail(t *testing.T) {
reqBody := []byte(`{"password": "12345"}`)
req := httptest.NewRequest("POST", "/api/v1/users", bytes.NewBuffer(reqBody))
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
resp := lo.Must(app.Test(req))
assert.Equal(t, 400, resp.StatusCode)
assert.Equal(t, 422, resp.StatusCode)
}

0 comments on commit 95d2d80

Please sign in to comment.