Skip to content

Commit

Permalink
chore: remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitiwat-owen committed Dec 26, 2023
1 parent e3863ee commit 2174678
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions src/app/handler/auth/auth.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,37 @@ import (

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

func TestAuthHandler(t *testing.T) {
suite.Run(t, new(AuthHandlerTest))
}

func (t *AuthHandlerTest) SetupTest() {
signupRequest := &dto.SignupRequest{}
bindErr := errors.New("Binding request failed")
validateErr := []*dto.BadReqErrResponse{
{
Message: "BadRequestError1",
FailedField: "Field1",
Value: nil,
},
{
Message: "BadRequestError2",
FailedField: "Field2",
Value: nil,
},
}

t.signupRequest = signupRequest
t.bindErr = bindErr
t.validateErr = validateErr
}

func (t *AuthHandlerTest) TestSignupSuccess() {
signupRequest := &dto.SignupRequest{}
signupResponse := &dto.SignupResponse{
Id: faker.UUIDDigit(),
Email: faker.Email(),
Expand All @@ -42,9 +61,9 @@ func (t *AuthHandlerTest) TestSignupSuccess() {
validator := mock_validator.NewMockIDtoValidator(controller)
context := mock_router.NewMockIContext(controller)

context.EXPECT().Bind(signupRequest).Return(nil)
validator.EXPECT().Validate(signupRequest).Return(nil)
authSvc.EXPECT().Signup(signupRequest).Return(signupResponse, nil)
context.EXPECT().Bind(t.signupRequest).Return(nil)
validator.EXPECT().Validate(t.signupRequest).Return(nil)
authSvc.EXPECT().Signup(t.signupRequest).Return(signupResponse, nil)
context.EXPECT().JSON(http.StatusOK, signupResponse)

handler := NewHandler(authSvc, userSvc, validator)
Expand All @@ -53,11 +72,9 @@ func (t *AuthHandlerTest) TestSignupSuccess() {
}

func (t *AuthHandlerTest) TestSignupBindFailed() {
signupRequest := &dto.SignupRequest{}
bindReqErr := errors.New("Binding request failed")
errResponse := dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: constant.BindingRequestErrorMessage + bindReqErr.Error(),
Message: constant.BindingRequestErrorMessage + t.bindErr.Error(),
Data: nil,
}

Expand All @@ -68,7 +85,7 @@ func (t *AuthHandlerTest) TestSignupBindFailed() {
validator := mock_validator.NewMockIDtoValidator(controller)
context := mock_router.NewMockIContext(controller)

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

handler := NewHandler(authSvc, userSvc, validator)
Expand All @@ -77,19 +94,6 @@ func (t *AuthHandlerTest) TestSignupBindFailed() {
}

func (t *AuthHandlerTest) TestSignupValidateFailed() {
signupRequest := &dto.SignupRequest{}
validateErr := []*dto.BadReqErrResponse{
{
Message: "BadRequestError1",
FailedField: "Field1",
Value: nil,
},
{
Message: "BadRequestError2",
FailedField: "Field2",
Value: nil,
},
}
errResponse := dto.ResponseErr{
StatusCode: http.StatusBadRequest,
Message: constant.InvalidRequestBodyMessage + "BadRequestError1, BadRequestError2",
Expand All @@ -103,8 +107,8 @@ func (t *AuthHandlerTest) TestSignupValidateFailed() {
validator := mock_validator.NewMockIDtoValidator(controller)
context := mock_router.NewMockIContext(controller)

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

handler := NewHandler(authSvc, userSvc, validator)
Expand All @@ -113,7 +117,6 @@ func (t *AuthHandlerTest) TestSignupValidateFailed() {
}

func (t *AuthHandlerTest) TestSignupServiceError() {
signupRequest := &dto.SignupRequest{}
signupError := &dto.ResponseErr{
StatusCode: http.StatusInternalServerError,
Message: "Some Exception",
Expand All @@ -127,9 +130,9 @@ func (t *AuthHandlerTest) TestSignupServiceError() {
validator := mock_validator.NewMockIDtoValidator(controller)
context := mock_router.NewMockIContext(controller)

context.EXPECT().Bind(signupRequest).Return(nil)
validator.EXPECT().Validate(signupRequest).Return(nil)
authSvc.EXPECT().Signup(signupRequest).Return(nil, signupError)
context.EXPECT().Bind(t.signupRequest).Return(nil)
validator.EXPECT().Validate(t.signupRequest).Return(nil)
authSvc.EXPECT().Signup(t.signupRequest).Return(nil, signupError)
context.EXPECT().JSON(http.StatusInternalServerError, signupError)

handler := NewHandler(authSvc, userSvc, validator)
Expand Down

0 comments on commit 2174678

Please sign in to comment.