Skip to content

Commit

Permalink
Revert "Revert "Merge branch 'dev' into JOH-37/docker""
Browse files Browse the repository at this point in the history
This reverts commit 6b487dc.
  • Loading branch information
bookpanda committed Jan 5, 2024
1 parent 6b487dc commit 4078153
Show file tree
Hide file tree
Showing 26 changed files with 1,404 additions and 439 deletions.
2 changes: 2 additions & 0 deletions src/app/constant/error.constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package constant

const BindingRequestErrorMessage = "Binding Request Error due to:"
const InvalidRequestBodyMessage = "Invalid Request Body due to:"
const InvalidTokenMessage = "Invalid token"
const IncorrectEmailPasswordMessage = "Incorrect Email or Password"
const UnauthorizedMessage = "Unauthorized"
const DuplicateEmailMessage = "Duplicate Email"
const InternalErrorMessage = "Internal Error"
const UnavailableServiceMessage = "Unavailable Service"
Expand Down
13 changes: 5 additions & 8 deletions src/app/dto/auth.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ package dto

type TokenPayloadAuth struct {
UserId string `json:"user_id"`
}

type Validate struct {
Token string `json:"token" validate:"jwt"`
}

type RedeemNewToken struct {
RefreshToken string `json:"refresh_token" validate:"required"`
Role string `json:"role"`
}

type SignupRequest struct {
Expand All @@ -34,3 +27,7 @@ type SignInRequest struct {
type SignOutResponse struct {
IsSuccess bool `json:"is_success"`
}

type RefreshTokenRequest struct {
RefreshToken string `json:"refresh_token" validate:"required"`
}
6 changes: 6 additions & 0 deletions src/app/dto/common.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ type ResponseGatewayTimeoutErr struct {
Message string `json:"message" example:"Connection timeout"`
Data interface{} `json:"data"`
}

type ResponseSuccess struct {
StatusCode int `json:"status_code" example:"200"`
Message string `json:"message" example:"success"`
Data interface{} `json:"data"`
}
60 changes: 52 additions & 8 deletions src/app/dto/pet.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,33 @@ import (
"github.com/isd-sgcu/johnjud-gateway/src/constant/pet"
)

type PetDto struct {
Id string `json:"id"`
type ImageResponse struct {
Id string `json:"id"`
Url string `json:"url"`
}

type PetResponse struct {
Id string `json:"id"`
Type string `json:"type"`
Species string `json:"species"`
Name string `json:"name"`
Birthdate string `json:"birthdate"`
Gender pet.Gender `json:"gender"`
Habit string `json:"habit"`
Caption string `json:"caption"`
Status pet.Status `json:"status"`
IsSterile *bool `json:"is_sterile"`
IsVaccinated *bool `json:"is_vaccinated"`
IsVisible *bool `json:"is_visible"`
IsClubPet *bool `json:"is_club_pet"`
Background string `json:"background"`
Address string `json:"address"`
Contact string `json:"contact"`
AdoptBy string `json:"adopt_by"`
Images []ImageResponse `json:"images"`
}

type CreatePetRequest struct {
Type string `json:"type" validate:"required"`
Species string `json:"species" validate:"required"`
Name string `json:"name" validate:"required"`
Expand All @@ -22,20 +47,39 @@ type PetDto struct {
Address string `json:"address"`
Contact string `json:"contact"`
AdoptBy string `json:"adopt_by"`
}

type CreatePetRequest struct {
Pet *PetDto `json:"pet" validate:"required"`
Images []string `json:"images"`
}

type ChangeViewPetRequest struct {
Visible bool `json:"visible" validate:"required"`
}

type UpdatePetRequest struct {
Pet *PetDto `json:"pet" validate:"required"`
type ChangeViewPetResponse struct {
Success bool `json:"success" validate:"required"`
}

type UpdatePetRequest struct {
Type string `json:"type"`
Species string `json:"species"`
Name string `json:"name"`
Birthdate string `json:"birthdate"`
Gender pet.Gender `json:"gender"`
Habit string `json:"habit"`
Caption string `json:"caption"`
Status pet.Status `json:"status"`
IsSterile *bool `json:"is_sterile"`
IsVaccinated *bool `json:"is_vaccinated"`
IsVisible *bool `json:"is_visible"`
IsClubPet *bool `json:"is_club_pet"`
Background string `json:"background"`
Address string `json:"address"`
Contact string `json:"contact"`
AdoptBy string `json:"adopt_by"`
Images []string `json:"images"`
}
type DeleteRequest struct {
Id string `json:"id" validate:"required"`
}
type DeleteResponse struct {
Success bool `json:"success" validate:"required"`
}
56 changes: 48 additions & 8 deletions src/app/handler/auth/auth.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ func NewHandler(service auth.Service, userService user.Service, validate validat
return &Handler{service, userService, validate}
}

func (h *Handler) Validate(c router.IContext) {

}

func (h *Handler) RefreshToken(c router.IContext) {

}

// Signup is a function that create user in database
// @Summary Signup user
// @Description Return the data of user if successfully
Expand Down Expand Up @@ -130,6 +122,7 @@ func (h *Handler) SignIn(c router.IContext) {
// @Accept json
// @Produce json
// @Success 200 {object} dto.SignOutResponse
// @Failure 401 {object} dto.ResponseUnauthorizedErr "Invalid token"
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/auth/signout [post]
Expand All @@ -144,3 +137,50 @@ func (h *Handler) SignOut(c router.IContext) {

c.JSON(http.StatusOK, response)
}

// RefreshToken is a function to redeem new access token and refresh token
// @Summary Refresh token
// @Description Return the credential
// @Param request body dto.RefreshTokenRequest true "refreshToken request dto"
// @Tags auth
// @Accept json
// @Produce json
// @Success 200 {object} dto.Credential
// @Failure 400 {object} dto.ResponseBadRequestErr "Invalid token"
// @Failure 401 {object} dto.ResponseUnauthorizedErr "Invalid token"
// @Failure 500 {object} dto.ResponseInternalErr "Internal service error"
// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down"
// @Router /v1/auth/refreshToken [post]
func (h *Handler) RefreshToken(c router.IContext) {
request := &dto.RefreshTokenRequest{}
err := c.Bind(request)
if err != nil {
c.JSON(http.StatusBadRequest, dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: constant.BindingRequestErrorMessage + err.Error(),
Data: nil,
})
return
}

if err := h.validate.Validate(request); err != nil {
var errorMessage []string
for _, reqErr := range err {
errorMessage = append(errorMessage, reqErr.Message)
}
c.JSON(http.StatusBadRequest, dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: constant.InvalidRequestBodyMessage + strings.Join(errorMessage, ", "),
Data: nil,
})
return
}

response, respErr := h.service.RefreshToken(request)
if respErr != nil {
c.JSON(respErr.StatusCode, respErr)
return
}

c.JSON(http.StatusOK, response)
}
104 changes: 100 additions & 4 deletions src/app/handler/auth/auth.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (

type AuthHandlerTest struct {
suite.Suite
signupRequest *dto.SignupRequest
signInRequest *dto.SignInRequest
bindErr error
validateErr []*dto.BadReqErrResponse
signupRequest *dto.SignupRequest
signInRequest *dto.SignInRequest
refreshTokenRequest *dto.RefreshTokenRequest
bindErr error
validateErr []*dto.BadReqErrResponse
}

func TestAuthHandler(t *testing.T) {
Expand All @@ -31,6 +32,7 @@ func TestAuthHandler(t *testing.T) {
func (t *AuthHandlerTest) SetupTest() {
signupRequest := &dto.SignupRequest{}
signInRequest := &dto.SignInRequest{}
refreshTokenRequest := &dto.RefreshTokenRequest{}
bindErr := errors.New("Binding request failed")
validateErr := []*dto.BadReqErrResponse{
{
Expand All @@ -47,6 +49,7 @@ func (t *AuthHandlerTest) SetupTest() {

t.signupRequest = signupRequest
t.signInRequest = signInRequest
t.refreshTokenRequest = refreshTokenRequest
t.bindErr = bindErr
t.validateErr = validateErr
}
Expand Down Expand Up @@ -282,3 +285,96 @@ func (t *AuthHandlerTest) TestSignOutServiceError() {

handler.SignOut(context)
}

func (t *AuthHandlerTest) TestRefreshTokenSuccess() {
refreshTokenResponse := &dto.Credential{
AccessToken: faker.Word(),
RefreshToken: faker.UUIDDigit(),
ExpiresIn: 3600,
}

controller := gomock.NewController(t.T())

authSvc := authMock.NewMockService(controller)
userSvc := userMock.NewMockService(controller)
validator := validatorMock.NewMockIDtoValidator(controller)
context := routerMock.NewMockIContext(controller)

handler := NewHandler(authSvc, userSvc, validator)

context.EXPECT().Bind(t.refreshTokenRequest).Return(nil)
validator.EXPECT().Validate(t.refreshTokenRequest).Return(nil)
authSvc.EXPECT().RefreshToken(t.refreshTokenRequest).Return(refreshTokenResponse, nil)
context.EXPECT().JSON(http.StatusOK, refreshTokenResponse)

handler.RefreshToken(context)
}

func (t *AuthHandlerTest) TestRefreshTokenBindFailed() {
errResponse := dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: constant.BindingRequestErrorMessage + t.bindErr.Error(),
Data: nil,
}

controller := gomock.NewController(t.T())

authSvc := authMock.NewMockService(controller)
userSvc := userMock.NewMockService(controller)
validator := validatorMock.NewMockIDtoValidator(controller)
context := routerMock.NewMockIContext(controller)

handler := NewHandler(authSvc, userSvc, validator)

context.EXPECT().Bind(t.refreshTokenRequest).Return(t.bindErr)
context.EXPECT().JSON(http.StatusBadRequest, errResponse)

handler.RefreshToken(context)
}

func (t *AuthHandlerTest) TestRefreshTokenValidateFailed() {
errResponse := dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: constant.InvalidRequestBodyMessage + "BadRequestError1, BadRequestError2",
Data: nil,
}

controller := gomock.NewController(t.T())

authSvc := authMock.NewMockService(controller)
userSvc := userMock.NewMockService(controller)
validator := validatorMock.NewMockIDtoValidator(controller)
context := routerMock.NewMockIContext(controller)

handler := NewHandler(authSvc, userSvc, validator)

context.EXPECT().Bind(t.refreshTokenRequest).Return(nil)
validator.EXPECT().Validate(t.refreshTokenRequest).Return(t.validateErr)
context.EXPECT().JSON(http.StatusBadRequest, errResponse)

handler.RefreshToken(context)
}

func (t *AuthHandlerTest) TestRefreshTokenServiceError() {
refreshTokenErr := &dto.ResponseErr{
StatusCode: http.StatusInternalServerError,
Message: constant.InternalErrorMessage,
Data: nil,
}

controller := gomock.NewController(t.T())

authSvc := authMock.NewMockService(controller)
userSvc := userMock.NewMockService(controller)
validator := validatorMock.NewMockIDtoValidator(controller)
context := routerMock.NewMockIContext(controller)

handler := NewHandler(authSvc, userSvc, validator)

context.EXPECT().Bind(t.refreshTokenRequest).Return(nil)
validator.EXPECT().Validate(t.refreshTokenRequest).Return(nil)
authSvc.EXPECT().RefreshToken(t.refreshTokenRequest).Return(nil, refreshTokenErr)
context.EXPECT().JSON(http.StatusInternalServerError, refreshTokenErr)

handler.RefreshToken(context)
}
Loading

0 comments on commit 4078153

Please sign in to comment.