diff --git a/.coderabbit.yaml b/.coderabbit.yaml new file mode 100644 index 0000000..b2d4e44 --- /dev/null +++ b/.coderabbit.yaml @@ -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 \ No newline at end of file diff --git a/.github/workflows/code-checker.yml b/.github/workflows/code-checker.yml index e36cae6..bd5f692 100644 --- a/.github/workflows/code-checker.yml +++ b/.github/workflows/code-checker.yml @@ -14,7 +14,7 @@ on: env: - TESTCOVERAGE_THRESHOLD: 10 + TESTCOVERAGE_THRESHOLD: 20 jobs: lint: diff --git a/.github/workflows/pr-size-labeler.yml b/.github/workflows/pr-size-labeler.yml new file mode 100644 index 0000000..77030a5 --- /dev/null +++ b/.github/workflows/pr-size-labeler.yml @@ -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: '' \ No newline at end of file diff --git a/go.mod b/go.mod index 9d692fd..b110c94 100644 --- a/go.mod +++ b/go.mod @@ -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 ) @@ -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 diff --git a/internal/app/api/handler/auth_handler.go b/internal/app/api/handler/auth_handler.go index 1483578..975735c 100644 --- a/internal/app/api/handler/auth_handler.go +++ b/internal/app/api/handler/auth_handler.go @@ -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 { @@ -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, } diff --git a/internal/app/api/handler/auth_handler_test.go b/internal/app/api/handler/auth_handler_test.go index 47f2b7f..89c5220 100644 --- a/internal/app/api/handler/auth_handler_test.go +++ b/internal/app/api/handler/auth_handler_test.go @@ -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) { @@ -26,6 +30,7 @@ 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", @@ -33,8 +38,74 @@ func TestAuthHandler_Login(t *testing.T) { }) 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 }, }, @@ -43,12 +114,11 @@ 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) @@ -56,3 +126,27 @@ func TestAuthHandler_Login(t *testing.T) { }) } } + +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) + } + }) + } +} diff --git a/internal/app/auth/auth.go b/internal/app/auth/auth.go index 8a84113..f878d1f 100644 --- a/internal/app/auth/auth.go +++ b/internal/app/auth/auth.go @@ -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 diff --git a/internal/app/repository/user_repository_test.go b/internal/app/repository/user_repository_test.go index fa1bda3..003a521 100644 --- a/internal/app/repository/user_repository_test.go +++ b/internal/app/repository/user_repository_test.go @@ -28,7 +28,7 @@ func TestUserRepository_GetUserByEmail(t *testing.T) { { name: "SUCCESS - GetUserByEmail", args: args{ - + ctx: context.Background(), email: "mock@email.com", }, diff --git a/internal/app/service/auth_service_test.go b/internal/app/service/auth_service_test.go index 3ff03eb..a1f680a 100644 --- a/internal/app/service/auth_service_test.go +++ b/internal/app/service/auth_service_test.go @@ -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) {