Skip to content

Commit

Permalink
add user controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukmanern committed Jan 27, 2024
1 parent c1949ab commit 385a0a2
Show file tree
Hide file tree
Showing 10 changed files with 936 additions and 85 deletions.
10 changes: 1 addition & 9 deletions controller/role/role_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,11 @@ import (
)

type RoleController interface {
// Create func creates a new role
// auth + admin
Create(c *fiber.Ctx) error

// Get func gets a role
Get(c *fiber.Ctx) error

// GetAll func gets some roles
GetAll(c *fiber.Ctx) error

// Update func updates a role
Update(c *fiber.Ctx) error

// Delete func deletes a role
Delete(c *fiber.Ctx) error
}

Expand Down
1 change: 1 addition & 0 deletions controller/role/role_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package controller
29 changes: 24 additions & 5 deletions controller/user/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/Lukmanern/gost/domain/model"
"github.com/Lukmanern/gost/internal/consts"
"github.com/Lukmanern/gost/internal/helper"
"github.com/Lukmanern/gost/internal/middleware"
"github.com/Lukmanern/gost/internal/response"
service "github.com/Lukmanern/gost/service/user"
Expand All @@ -22,14 +23,14 @@ type UserController interface {
Login(c *fiber.Ctx) error
ForgetPassword(c *fiber.Ctx) error
ResetPassword(c *fiber.Ctx) error
// auth+admin
// auth + admin
GetAll(c *fiber.Ctx) error
// auth
MyProfile(c *fiber.Ctx) error
Logout(c *fiber.Ctx) error
UpdateProfile(c *fiber.Ctx) error
UpdatePassword(c *fiber.Ctx) error
Delete(c *fiber.Ctx) error
DeleteAccount(c *fiber.Ctx) error
}

type UserControllerImpl struct {
Expand Down Expand Up @@ -58,6 +59,9 @@ func (ctr *UserControllerImpl) Register(c *fiber.Ctx) error {
}
user.Email = strings.ToLower(user.Email)
validate := validator.New()
if len(user.RoleIDs) < 1 {
return response.BadRequest(c, "please choose one or more role")
}
if err := validate.Struct(&user); err != nil {
return response.BadRequest(c, consts.InvalidJSONBody+err.Error())
}
Expand Down Expand Up @@ -116,11 +120,10 @@ func (ctr *UserControllerImpl) AccountActivation(c *fiber.Ctx) error {

func (ctr *UserControllerImpl) Login(c *fiber.Ctx) error {
var user model.UserLogin
// user.IP = c.IP() // Note : uncomment this line in production
if err := c.BodyParser(&user); err != nil {
return response.BadRequest(c, consts.InvalidJSONBody+err.Error())
}

user.IP = helper.RandomIPAddress() // Todo : update to c.IP()
validate := validator.New()
if err := validate.Struct(&user); err != nil {
return response.BadRequest(c, consts.InvalidJSONBody+err.Error())
Expand Down Expand Up @@ -345,6 +348,22 @@ func (ctr *UserControllerImpl) UpdatePassword(c *fiber.Ctx) error {
return response.SuccessNoContent(c)
}

func (ctr *UserControllerImpl) Delete(c *fiber.Ctx) error {
func (ctr *UserControllerImpl) DeleteAccount(c *fiber.Ctx) error {
userClaims, ok := c.Locals("claims").(*middleware.Claims)
if !ok || userClaims == nil {
return response.Unauthorized(c)
}

ctx := c.Context()
err := ctr.service.DeleteAccount(ctx, userClaims.ID)
if err != nil {
fiberErr, ok := err.(*fiber.Error)
if ok {
return response.CreateResponse(c, fiberErr.Code, response.Response{
Message: fiberErr.Message, Success: false, Data: nil,
})
}
return response.Error(c, consts.ErrServer)
}
return response.SuccessNoContent(c)
}
Loading

0 comments on commit 385a0a2

Please sign in to comment.