Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Felix 11 Add handler unit test #31

Merged
merged 4 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .coderabbit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
language: "en-US"
early_access: false
reviews:
profile: "chill"
request_changes_workflow: false
high_level_summary: true
poem: true
review_status: true
collapse_walkthrough: false
auto_review:
enabled: true
drafts: false
chat:
auto_reply: true
2 changes: 1 addition & 1 deletion .github/workflows/code-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:


env:
TESTCOVERAGE_THRESHOLD: 10
TESTCOVERAGE_THRESHOLD: 20

jobs:
lint:
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/pr-size-labeler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: 👀 Pull Request Size Labeler

on: [pull_request]

jobs:
labeler:
runs-on: ubuntu-latest
name: Label the PR size
steps:
- uses: codelytv/pr-size-labeler@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
xs_label: 'size/xs'
xs_max_size: '10'
s_label: 'size/s'
s_max_size: '100'
m_label: 'size/m'
m_max_size: '500'
l_label: 'size/l'
l_max_size: '1000'
xl_label: 'size/xl'
fail_if_xl: 'false'
message_if_xl: >
This PR exceeds the recommended size of 1000 lines.
Please make sure you are NOT addressing multiple issues with one PR.
Note this PR might be rejected due to its size.
github_api_url: 'https://api.github.com'
files_to_ignore: ''
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/jmoiron/sqlx v1.4.0
github.com/opentracing/opentracing-go v1.2.0
github.com/valyala/fasthttp v1.54.0
go.uber.org/mock v0.4.0
)

Expand All @@ -40,7 +41,6 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.54.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
Expand Down
2 changes: 1 addition & 1 deletion internal/app/api/handler/auth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func (h *AuthHandler) Register(c fiber.Ctx) error {
if err != nil {
return SendErrorResp(c, fiber.StatusBadRequest, "Invalid request body.")
}


err = validator.New().StructCtx(ctx, req)
if err != nil {
Expand All @@ -88,6 +87,7 @@ func (h *AuthHandler) Register(c fiber.Ctx) error {
if err != nil {
return SendErrorResp(c, fiber.StatusBadRequest, err.Error())
}

res := map[string]int64{
"user_id": result,
}
Expand Down
104 changes: 99 additions & 5 deletions internal/app/api/handler/auth_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package handler

import (
"database/sql"
"errors"
"reflect"
"testing"

"github.com/bytedance/sonic"
"github.com/gofiber/fiber/v3"
"github.com/valyala/fasthttp"

"github.com/voltgizerz/POS-restaurant/internal/app/constants"
"github.com/voltgizerz/POS-restaurant/internal/app/entity"
"github.com/voltgizerz/POS-restaurant/internal/app/interactor"
"go.uber.org/mock/gomock"
)

func TestAuthHandler_Login(t *testing.T) {
Expand All @@ -26,15 +30,82 @@ func TestAuthHandler_Login(t *testing.T) {
setup: func(mockObj *MockObject) fiber.Ctx {
mockCtx := app.AcquireCtx(&fasthttp.RequestCtx{})
mockCtx.Locals(constants.CTXKeyRequestID, "mock-req-id")
mockCtx.Request().Header.Set("Content-Type", "application/json")

data, _ := sonic.Marshal(entity.LoginRequest{
Username: "fELIX",
Password: "fELIX",
})
mockCtx.Request().SetBodyString(string(data))

// mockObj.MockAuthService.EXPECT().Login(gomock.Any(), gomock.Any(), gomock.Any()).
// Return(&entity.LoginResponse{}, nil).Times(1)
mockObj.MockAuthService.EXPECT().Login(gomock.Any(), gomock.Any(), gomock.Any()).
Return(&entity.LoginResponse{}, nil).Times(1)
return mockCtx
},
},
{
name: "ERROR - on Bind",
wantErr: false,
setup: func(mockObj *MockObject) fiber.Ctx {
mockCtx := app.AcquireCtx(&fasthttp.RequestCtx{})
mockCtx.Locals(constants.CTXKeyRequestID, "mock-req-id")
mockCtx.Request().Header.Set("Content-Type", "random")

return mockCtx
},
},
{
name: "Error - on Validator",
wantErr: false,
setup: func(mockObj *MockObject) fiber.Ctx {
mockCtx := app.AcquireCtx(&fasthttp.RequestCtx{})
mockCtx.Locals(constants.CTXKeyRequestID, "mock-req-id")
mockCtx.Request().Header.Set("Content-Type", "application/json")

data, _ := sonic.Marshal(entity.LoginRequest{
Username: "",
Password: "fELIX",
})
mockCtx.Request().SetBodyString(string(data))

return mockCtx
},
},
{
name: "Error - on Login",
wantErr: false,
setup: func(mockObj *MockObject) fiber.Ctx {
mockCtx := app.AcquireCtx(&fasthttp.RequestCtx{})
mockCtx.Locals(constants.CTXKeyRequestID, "mock-req-id")
mockCtx.Request().Header.Set("Content-Type", "application/json")

data, _ := sonic.Marshal(entity.LoginRequest{
Username: "felix",
Password: "fELIX",
})
mockCtx.Request().SetBodyString(string(data))

mockObj.MockAuthService.EXPECT().Login(gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil, errors.New("some errors")).Times(1)
return mockCtx
},
},
{
name: "Error - on Login got no rows",
wantErr: false,
setup: func(mockObj *MockObject) fiber.Ctx {
mockCtx := app.AcquireCtx(&fasthttp.RequestCtx{})
mockCtx.Locals(constants.CTXKeyRequestID, "mock-req-id")
mockCtx.Request().Header.Set("Content-Type", "application/json")

data, _ := sonic.Marshal(entity.LoginRequest{
Username: "felix",
Password: "fELIX",
})
mockCtx.Request().SetBodyString(string(data))

mockObj.MockAuthService.EXPECT().Login(gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil, sql.ErrNoRows).Times(1)
return mockCtx
},
},
Expand All @@ -43,16 +114,39 @@ func TestAuthHandler_Login(t *testing.T) {
ctrl, mockObj := NewMock(t)
defer ctrl.Finish()

c := tt.setup(mockObj)
authHandler := &AuthHandler{
authService: mockObj.MockAuthService,
}

c := tt.setup(mockObj)

t.Run(tt.name, func(t *testing.T) {
if err := authHandler.Login(c); (err != nil) != tt.wantErr {
t.Errorf("AuthHandler.Login() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestNewAuthHandler(t *testing.T) {
type args struct {
i interactor.AuthHandler
}
tests := []struct {
name string
args args
want *AuthHandler
}{
{
name: "SUCCESS",
args: args{},
want: &AuthHandler{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewAuthHandler(tt.args.i); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewAuthHandler() = %v, want %v", got, tt.want)
}
})
}
}
10 changes: 5 additions & 5 deletions internal/app/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func GetUserLoginFromCtx(ctx context.Context) (*Auth, error) {

// Create a new User struct with the retrieved values
user := &Auth{
UserID: int64(userID),
Username: username,
RoleID: int64(roleID),
IsUserActive: isUserActive,
RequestID: requestID,
UserID: int64(userID),
Username: username,
RoleID: int64(roleID),
IsUserActive: isUserActive,
RequestID: requestID,
}

return user, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/app/repository/user_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestUserRepository_GetUserByEmail(t *testing.T) {
{
name: "SUCCESS - GetUserByEmail",
args: args{

ctx: context.Background(),
email: "mock@email.com",
},
Expand Down
3 changes: 2 additions & 1 deletion internal/app/service/auth_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"reflect"
"testing"

"go.uber.org/mock/gomock"

"github.com/voltgizerz/POS-restaurant/internal/app/entity"
"github.com/voltgizerz/POS-restaurant/internal/app/interactor"
"go.uber.org/mock/gomock"
)

func TestUserService_Login(t *testing.T) {
Expand Down
Loading