diff --git a/src/app/handler/estamp/estamp.handler.go b/src/app/handler/estamp/estamp.handler.go index 947059b..132ef9d 100644 --- a/src/app/handler/estamp/estamp.handler.go +++ b/src/app/handler/estamp/estamp.handler.go @@ -35,7 +35,7 @@ type IEstampService interface { FindAllEventWithType(string) (*proto.FindAllEventWithTypeResponse, *dto.ResponseErr) } -// Get detail of event using event id +// FindEventByID Get detail of event using event id // @Summary Get event detail // @Description Get detail of event using event id // @Param id path string true "id" @@ -67,7 +67,7 @@ func (h *Handler) FindEventByID(ctx IContext) { ctx.JSON(http.StatusOK, res) } -// verify estamp for event day +// VerifyEstamp to verified estamp for event day // @Summary check if estamp exist // @Description check if estamp exist // @Param event_id body dto.VerifyEstampRequest true "event id" @@ -102,7 +102,7 @@ func (h *Handler) VerifyEstamp(ctx qr.IContext) { return } -// Get all event by type +// FindAllEventWithType Get all event by type // @Summary Get all event by type // @Description Get get all event with the given type // @Param eventType query string true "id" diff --git a/src/app/middleware/auth/auth.middleware.go b/src/app/middleware/auth/auth.middleware.go index 4deeea8..6c82ad2 100644 --- a/src/app/middleware/auth/auth.middleware.go +++ b/src/app/middleware/auth/auth.middleware.go @@ -5,16 +5,16 @@ import ( "github.com/isd-sgcu/rnkm65-gateway/src/app/handler/auth" "github.com/isd-sgcu/rnkm65-gateway/src/app/utils" "github.com/isd-sgcu/rnkm65-gateway/src/config" - phase "github.com/isd-sgcu/rnkm65-gateway/src/constant/auth" "net/http" "strings" ) type Guard struct { - service auth.IService - excludes map[string]struct{} - conf config.App - isValidate bool + service auth.IService + excludes map[string]struct{} + allowPhases map[string][]string + conf config.App + isValidate bool } type IContext interface { @@ -23,39 +23,19 @@ type IContext interface { Path() string StoreValue(string, string) JSON(int, interface{}) - Next() + Next() error } -func NewAuthGuard(s auth.IService, e map[string]struct{}, conf config.App) Guard { +func NewAuthGuard(s auth.IService, e map[string]struct{}, allowPhase map[string][]string, conf config.App) Guard { return Guard{ - service: s, - excludes: e, - conf: conf, - isValidate: true, + service: s, + excludes: e, + allowPhases: allowPhase, + conf: conf, + isValidate: true, } } -func (m *Guard) Use(ctx IContext) { - m.isValidate = true - - m.Validate(ctx) - - if !m.isValidate { - return - } - - if !m.conf.Debug { - m.CheckConfig(ctx) - - if !m.isValidate { - return - } - } - - ctx.Next() - -} - func (m *Guard) Validate(ctx IContext) { method := ctx.Method() path := ctx.Path() @@ -113,7 +93,7 @@ func (m *Guard) CheckConfig(ctx IContext) { return } - phses, ok := phase.MapPath2Phase[path] + phses, ok := m.allowPhases[path] if !ok { ctx.Next() return diff --git a/src/app/middleware/auth/auth.middleware_test.go b/src/app/middleware/auth/auth.middleware_test.go index 92e5e2e..cf0f455 100644 --- a/src/app/middleware/auth/auth.middleware_test.go +++ b/src/app/middleware/auth/auth.middleware_test.go @@ -16,6 +16,7 @@ type AuthGuardTest struct { suite.Suite conf config.App ExcludePath map[string]struct{} + AllowPhases map[string][]string UserId string Token string UnauthorizedErr *dto.ResponseErr @@ -54,6 +55,12 @@ func (u *AuthGuardTest) SetupTest() { "POST /exclude/:id": {}, } + u.AllowPhases = map[string][]string{ + "GET /allow1": {"phase1"}, + "GET /allow2": {"phase1", "phase2"}, + "GET /allow3": {"phase2"}, + } + u.conf = config.App{ Port: 3000, Debug: true, @@ -78,9 +85,9 @@ func (u *AuthGuardTest) TestValidateSuccess() { }, nil) c.On("StoreValue", "UserId", u.UserId) c.On("StoreValue", "Role", role.USER) - c.On("Next") + c.On("Next").Return(nil) - h := NewAuthGuard(srv, u.ExcludePath, u.conf) + h := NewAuthGuard(srv, u.ExcludePath, u.AllowPhases, u.conf) h.Validate(c) actual := c.Header["UserId"] @@ -96,9 +103,9 @@ func (u *AuthGuardTest) TestValidateSkippedFromExcludePath() { c.On("Method").Return("POST") c.On("Path").Return("/exclude") c.On("Token").Return("") - c.On("Next") + c.On("Next").Return(nil) - h := NewAuthGuard(srv, u.ExcludePath, u.conf) + h := NewAuthGuard(srv, u.ExcludePath, u.AllowPhases, u.conf) h.Validate(c) c.AssertNumberOfCalls(u.T(), "Next", 1) @@ -112,9 +119,9 @@ func (u *AuthGuardTest) TestValidateSkippedFromExcludePathWithID() { c.On("Method").Return("POST") c.On("Path").Return("/exclude/1") c.On("Token").Return("") - c.On("Next") + c.On("Next").Return(nil) - h := NewAuthGuard(srv, u.ExcludePath, u.conf) + h := NewAuthGuard(srv, u.ExcludePath, u.AllowPhases, u.conf) h.Validate(c) c.AssertNumberOfCalls(u.T(), "Next", 1) @@ -132,7 +139,7 @@ func (u *AuthGuardTest) TestValidateFailed() { c.On("Token").Return(u.Token) srv.On("Validate", u.Token).Return(nil, u.UnauthorizedErr) - h := NewAuthGuard(srv, u.ExcludePath, u.conf) + h := NewAuthGuard(srv, u.ExcludePath, u.AllowPhases, u.conf) h.Validate(c) assert.Equal(u.T(), want, c.V) @@ -149,7 +156,7 @@ func (u *AuthGuardTest) TestValidateTokenNotIncluded() { c.On("Token").Return("") srv.On("Validate") - h := NewAuthGuard(srv, u.ExcludePath, u.conf) + h := NewAuthGuard(srv, u.ExcludePath, u.AllowPhases, u.conf) h.Validate(c) assert.Equal(u.T(), want, c.V) @@ -167,7 +174,7 @@ func (u *AuthGuardTest) TestValidateTokenGrpcErr() { c.On("Token").Return(u.Token) srv.On("Validate", u.Token).Return(nil, u.ServiceDownErr) - h := NewAuthGuard(srv, u.ExcludePath, u.conf) + h := NewAuthGuard(srv, u.ExcludePath, u.AllowPhases, u.conf) h.Validate(c) assert.Equal(u.T(), want, c.V) @@ -179,26 +186,23 @@ func testConfigSuccess(t *testing.T, u *AuthGuardTest, conf config.App, mth stri c.On("Method").Return(mth) c.On("Path").Return(pth) - c.On("Next") + c.On("Next").Return(nil) - h := NewAuthGuard(srv, u.ExcludePath, conf) + h := NewAuthGuard(srv, u.ExcludePath, u.AllowPhases, conf) h.CheckConfig(c) c.AssertNumberOfCalls(t, "Next", 1) } func (u *AuthGuardTest) TestConfigSuccess() { - u.conf.Phase = "register" - testConfigSuccess(u.T(), u, u.conf, "GET", "/user") - testConfigSuccess(u.T(), u, u.conf, "PUT", "/user") - u.conf.Phase = "select" - testConfigSuccess(u.T(), u, u.conf, "GET", "/group/1") - testConfigSuccess(u.T(), u, u.conf, "DELETE", "/group/members/2") - testConfigSuccess(u.T(), u, u.conf, "DELETE", "/group/leave") - u.conf.Phase = "eventDay" - testConfigSuccess(u.T(), u, u.conf, "POST", "/qr/checkin/verify") - u.conf.Phase = "eStamp" - testConfigSuccess(u.T(), u, u.conf, "POST", "/qr/estamp/confirm") + u.conf.Phase = "phase1" + testConfigSuccess(u.T(), u, u.conf, "GET", "/allow1") + testConfigSuccess(u.T(), u, u.conf, "GET", "/allow2") + u.conf.Phase = "phase2" + testConfigSuccess(u.T(), u, u.conf, "GET", "/allow2") + testConfigSuccess(u.T(), u, u.conf, "GET", "/allow3") + testConfigSuccess(u.T(), u, u.conf, "GET", "/") + testConfigSuccess(u.T(), u, u.conf, "GET", "/allowall") } func testConfigFail(t *testing.T, u *AuthGuardTest, conf config.App, mth string, pth string) { @@ -209,22 +213,18 @@ func testConfigFail(t *testing.T, u *AuthGuardTest, conf config.App, mth string, c.On("Method").Return(mth) c.On("Path").Return(pth) - c.On("Next") + c.On("Next").Return(nil) - h := NewAuthGuard(srv, u.ExcludePath, conf) + h := NewAuthGuard(srv, u.ExcludePath, u.AllowPhases, conf) h.CheckConfig(c) assert.Equal(t, want, c.V) + assert.Equal(t, http.StatusForbidden, c.Status) } func (u *AuthGuardTest) TestConfigFail() { - u.conf.Phase = "register" - testConfigFail(u.T(), u, u.conf, "PUT", "/group") - u.conf.Phase = "select" - testConfigFail(u.T(), u, u.conf, "PUT", "/file/upload") - testConfigFail(u.T(), u, u.conf, "GET", "/estamp/1") - u.conf.Phase = "eventDay" - testConfigFail(u.T(), u, u.conf, "PUT", "/group") - u.conf.Phase = "emStamp" - testConfigFail(u.T(), u, u.conf, "PUT", "/group") + u.conf.Phase = "phase1" + testConfigFail(u.T(), u, u.conf, "GET", "/allow3") + u.conf.Phase = "phase2" + testConfigFail(u.T(), u, u.conf, "GET", "/allow1") } diff --git a/src/app/router/auth.router_test.go b/src/app/router/auth.router_test.go deleted file mode 100644 index 0fb3cfc..0000000 --- a/src/app/router/auth.router_test.go +++ /dev/null @@ -1,102 +0,0 @@ -package router - -import ( - "github.com/isd-sgcu/rnkm65-gateway/src/app/handler/auth" - "github.com/isd-sgcu/rnkm65-gateway/src/config" - mock "github.com/isd-sgcu/rnkm65-gateway/src/mocks/common" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/suite" - "net/http" - "net/http/httptest" - "testing" -) - -type AuthRouterTest struct { - suite.Suite -} - -func TestAuthRouter(t *testing.T) { - suite.Run(t, new(AuthRouterTest)) -} - -func (t *AuthRouterTest) TestGetAuthRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "GET /auth status 200", - route: "/auth", - expectedCode: http.StatusOK, - }, - { - description: "GET HTTP status 404, when route is not exists", - route: "/not-found", - expectedCode: http.StatusNotFound, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.GetAuth("/", func(ctx auth.IContext) { - ctx.JSON(http.StatusOK, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("GET", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} - -func (t *AuthRouterTest) TestPostAuthRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "POST /auth status 201", - route: "/auth", - expectedCode: http.StatusCreated, - }, - { - description: "POST HTTP status 404, when route is not exists", - route: "/not-found", - expectedCode: http.StatusNotFound, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.PostAuth("/", func(ctx auth.IContext) { - ctx.JSON(http.StatusCreated, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("POST", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} diff --git a/src/app/router/baan.router_test.go b/src/app/router/baan.router_test.go deleted file mode 100644 index ca39c6c..0000000 --- a/src/app/router/baan.router_test.go +++ /dev/null @@ -1,61 +0,0 @@ -package router - -import ( - "github.com/isd-sgcu/rnkm65-gateway/src/app/handler/baan" - "github.com/isd-sgcu/rnkm65-gateway/src/config" - mock "github.com/isd-sgcu/rnkm65-gateway/src/mocks/common" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/suite" - "net/http" - "net/http/httptest" - "testing" -) - -type BaanRouterTest struct { - suite.Suite -} - -func TestBaanRouter(t *testing.T) { - suite.Run(t, new(BaanRouterTest)) -} - -func (t *BaanRouterTest) TestPostBaanRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "POST /file status 200", - route: "/baan", - expectedCode: http.StatusOK, - }, - { - description: "POST HTTP status 404, when route is not exists", - route: "/not-found", - expectedCode: http.StatusNotFound, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.GetBaan("/", func(ctx baan.IContext) { - ctx.JSON(http.StatusOK, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("GET", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} diff --git a/src/app/router/fiber.go b/src/app/router/fiber.go index 018fb18..f451c6b 100644 --- a/src/app/router/fiber.go +++ b/src/app/router/fiber.go @@ -29,7 +29,8 @@ type FiberRouter struct { } type IGuard interface { - Use(guard.IContext) + Validate(ctx guard.IContext) + CheckConfig(ctx guard.IContext) } func NewFiberRouter(authGuard IGuard, conf config.App) *FiberRouter { @@ -39,9 +40,11 @@ func NewFiberRouter(authGuard IGuard, conf config.App) *FiberRouter { BodyLimit: conf.MaxFileSize * 1024 * 1024, }) - r.Use(cors.New(cors.Config{ - AllowOrigins: "*", - })) + r.Use( + cors.New(cors.Config{ + AllowOrigins: "*", + }), + ) if conf.Debug { r.Use(logger.New(logger.Config{Next: func(c *fiber.Ctx) bool { @@ -50,23 +53,35 @@ func NewFiberRouter(authGuard IGuard, conf config.App) *FiberRouter { r.Get("/docs/*", swagger.HandlerDefault) } - user := NewGroupRouteWithAuthMiddleware(r, "/user", authGuard.Use) - auth := NewGroupRouteWithAuthMiddleware(r, "/auth", authGuard.Use) - file := NewGroupRouteWithAuthMiddleware(r, "/file", authGuard.Use) - vaccine := NewGroupRouteWithAuthMiddleware(r, "/vaccine", authGuard.Use) - baan := NewGroupRouteWithAuthMiddleware(r, "/baan", authGuard.Use) - group := NewGroupRouteWithAuthMiddleware(r, "/group", authGuard.Use) - qr := NewGroupRouteWithAuthMiddleware(r, "/qr", authGuard.Use) - estamp := NewGroupRouteWithAuthMiddleware(r, "/estamp", authGuard.Use) + user := NewGroupRouteWithAuthMiddleware(r, "/user", authGuard.Validate, authGuard.CheckConfig) + auth := NewGroupRouteWithAuthMiddleware(r, "/auth", authGuard.Validate, authGuard.CheckConfig) + file := NewGroupRouteWithAuthMiddleware(r, "/file", authGuard.Validate, authGuard.CheckConfig) + vaccine := NewGroupRouteWithAuthMiddleware(r, "/vaccine", authGuard.Validate, authGuard.CheckConfig) + baan := NewGroupRouteWithAuthMiddleware(r, "/baan", authGuard.CheckConfig) + group := NewGroupRouteWithAuthMiddleware(r, "/group", authGuard.Validate, authGuard.CheckConfig) + qr := NewGroupRouteWithAuthMiddleware(r, "/qr", authGuard.Validate, authGuard.CheckConfig) + estamp := NewGroupRouteWithAuthMiddleware(r, "/estamp", authGuard.Validate, authGuard.CheckConfig) return &FiberRouter{r, user, auth, file, group, vaccine, baan, qr, estamp} } -func NewGroupRouteWithAuthMiddleware(r *fiber.App, path string, middleware func(ctx guard.IContext)) fiber.Router { - return r.Group(path, func(c *fiber.Ctx) error { - middleware(NewFiberCtx(c)) - return nil - }) +func NewGroupRouteWithAuthMiddleware(r *fiber.App, path string, middlewares ...func(ctx guard.IContext)) fiber.Router { + mList := createMiddlewaresList(middlewares) + return r.Group(path, mList...) +} + +func createMiddlewaresList(middlewares []func(ctx guard.IContext)) []func(ctx *fiber.Ctx) error { + var result []func(ctx *fiber.Ctx) error + for _, middleware := range middlewares { + m := func(c *fiber.Ctx) error { + middleware(NewFiberCtx(c)) + return nil + } + + result = append(result, m) + } + + return result } type FiberCtx struct { @@ -128,8 +143,8 @@ func (c *FiberCtx) StoreValue(k string, v string) { c.Locals(k, v) } -func (c *FiberCtx) Next() { - c.Ctx.Next() +func (c *FiberCtx) Next() error { + return c.Ctx.Next() } func (c *FiberCtx) Query(key string) string { @@ -174,3 +189,7 @@ func (c *FiberCtx) GetFormData(key string) string { func (c *FiberCtx) Host() string { return c.Ctx.Hostname() } + +func (c *FiberCtx) GetCTX() *fiber.Ctx { + return c.Ctx +} diff --git a/src/app/router/file.router_test.go b/src/app/router/file.router_test.go deleted file mode 100644 index fa71268..0000000 --- a/src/app/router/file.router_test.go +++ /dev/null @@ -1,61 +0,0 @@ -package router - -import ( - "github.com/isd-sgcu/rnkm65-gateway/src/app/handler/file" - "github.com/isd-sgcu/rnkm65-gateway/src/config" - mock "github.com/isd-sgcu/rnkm65-gateway/src/mocks/common" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/suite" - "net/http" - "net/http/httptest" - "testing" -) - -type FileRouterTest struct { - suite.Suite -} - -func TestFileRouter(t *testing.T) { - suite.Run(t, new(FileRouterTest)) -} - -func (t *FileRouterTest) TestPostFileRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "POST /file status 201", - route: "/file", - expectedCode: http.StatusCreated, - }, - { - description: "POST HTTP status 404, when route is not exists", - route: "/not-found", - expectedCode: http.StatusNotFound, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.PostFile("/", func(ctx file.IContext) { - ctx.JSON(http.StatusCreated, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("POST", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} diff --git a/src/app/router/group.router_test.go b/src/app/router/group.router_test.go deleted file mode 100644 index 74ed8ea..0000000 --- a/src/app/router/group.router_test.go +++ /dev/null @@ -1,185 +0,0 @@ -package router - -import ( - "github.com/isd-sgcu/rnkm65-gateway/src/app/handler/group" - "github.com/isd-sgcu/rnkm65-gateway/src/app/handler/user" - "github.com/isd-sgcu/rnkm65-gateway/src/config" - mock "github.com/isd-sgcu/rnkm65-gateway/src/mocks/common" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/suite" - "net/http" - "net/http/httptest" - "testing" -) - -type GroupRouterTest struct { - suite.Suite -} - -func TestGroupRouter(t *testing.T) { - suite.Run(t, new(GroupRouterTest)) -} - -func (t *GroupRouterTest) TestGetGroupRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "GET /Group status 200", - route: "/group", - expectedCode: http.StatusOK, - }, - { - description: "GET HTTP status 404, when route is not exists", - route: "/not-found", - expectedCode: http.StatusNotFound, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.GetUser("/", func(ctx user.IContext) { - ctx.JSON(http.StatusOK, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("GET", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} - -func (t *GroupRouterTest) TestPostGroupRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "POST /group status 201", - route: "/group", - expectedCode: http.StatusCreated, - }, - { - description: "POST HTTP status 404, when route is not exists", - route: "/not-found", - expectedCode: http.StatusNotFound, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.PostGroup("/", func(ctx group.IContext) { - ctx.JSON(http.StatusCreated, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("POST", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} - -func (t *GroupRouterTest) TestPutGroupRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "PUT /group status 200", - route: "/group", - expectedCode: http.StatusOK, - }, - { - description: "PUT HTTP status 404, when route is not exists", - route: "/not-found", - expectedCode: http.StatusNotFound, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.PutGroup("/", func(ctx group.IContext) { - ctx.JSON(http.StatusOK, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("PUT", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} - -func (t *GroupRouterTest) TestDeleteGroupRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "DELETE /group status 200", - route: "/group", - expectedCode: http.StatusOK, - }, - { - description: "DELETE HTTP status 404, when route is not exists", - route: "/not-found", - expectedCode: http.StatusNotFound, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.DeleteGroup("/", func(ctx group.IContext) { - ctx.JSON(http.StatusOK, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("DELETE", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} diff --git a/src/app/router/health-check.router_test.go b/src/app/router/health-check.router_test.go deleted file mode 100644 index 48c5f90..0000000 --- a/src/app/router/health-check.router_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package router - -import ( - health_check "github.com/isd-sgcu/rnkm65-gateway/src/app/handler/health-check" - "github.com/isd-sgcu/rnkm65-gateway/src/config" - mock "github.com/isd-sgcu/rnkm65-gateway/src/mocks/common" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/suite" - "net/http" - "net/http/httptest" - "testing" -) - -type HealthCheckRouterTest struct { - suite.Suite -} - -func TestHealthCheckRouter(t *testing.T) { - suite.Run(t, new(HealthCheckRouterTest)) -} - -func (t *HealthCheckRouterTest) TestHealthCheckRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "Health Check with status 200", - route: "/", - expectedCode: http.StatusOK, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.GetHealthCheck("/", func(ctx health_check.IContext) { - ctx.JSON(http.StatusOK, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("GET", test.route, nil) - - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} diff --git a/src/app/router/qr.router_test.go b/src/app/router/qr.router_test.go deleted file mode 100644 index d11cecf..0000000 --- a/src/app/router/qr.router_test.go +++ /dev/null @@ -1,94 +0,0 @@ -package router - -import ( - "net/http" - "net/http/httptest" - "testing" - - "github.com/isd-sgcu/rnkm65-gateway/src/config" - "github.com/isd-sgcu/rnkm65-gateway/src/interfaces/qr" - mock "github.com/isd-sgcu/rnkm65-gateway/src/mocks/common" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/suite" -) - -type QrRouterTest struct { - suite.Suite -} - -func TestQrRouter(t *testing.T) { - suite.Run(t, new(QrRouterTest)) -} - -func (t *HealthCheckRouterTest) TestCheckinVerifyRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "POST /qr/checkin/verify status 200", - route: "/qr/checkin/verify", - expectedCode: http.StatusOK, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.PostQr("/checkin/verify", func(ctx qr.IContext) { - ctx.JSON(http.StatusOK, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("POST", test.route, nil) - - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} - -func (t *HealthCheckRouterTest) TestCheckinConfirmRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "POST /qr/checkin/confirm status 200", - route: "/qr/checkin/confirm", - expectedCode: http.StatusOK, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.PostQr("/checkin/confirm", func(ctx qr.IContext) { - ctx.JSON(http.StatusOK, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("POST", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} diff --git a/src/app/router/user.router_test.go b/src/app/router/user.router_test.go deleted file mode 100644 index 5b32646..0000000 --- a/src/app/router/user.router_test.go +++ /dev/null @@ -1,225 +0,0 @@ -package router - -import ( - "github.com/isd-sgcu/rnkm65-gateway/src/app/handler/user" - "github.com/isd-sgcu/rnkm65-gateway/src/config" - mock "github.com/isd-sgcu/rnkm65-gateway/src/mocks/common" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/suite" - "net/http" - "net/http/httptest" - "testing" -) - -type UserRouterTest struct { - suite.Suite -} - -func TestUserRouter(t *testing.T) { - suite.Run(t, new(UserRouterTest)) -} - -func (t *UserRouterTest) TestGetUserRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "GET /user status 200", - route: "/user", - expectedCode: http.StatusOK, - }, - { - description: "GET HTTP status 404, when route is not exists", - route: "/not-found", - expectedCode: http.StatusNotFound, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.GetUser("/", func(ctx user.IContext) { - ctx.JSON(http.StatusOK, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("GET", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} - -func (t *UserRouterTest) TestPostUserRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "POST /user status 201", - route: "/user", - expectedCode: http.StatusCreated, - }, - { - description: "POST HTTP status 404, when route is not exists", - route: "/not-found", - expectedCode: http.StatusNotFound, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.PostUser("/", func(ctx user.IContext) { - ctx.JSON(http.StatusCreated, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("POST", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} - -func (t *UserRouterTest) TestPutUserRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "PUT /user status 200", - route: "/user", - expectedCode: http.StatusOK, - }, - { - description: "PUT HTTP status 404, when route is not exists", - route: "/not-found", - expectedCode: http.StatusNotFound, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.PutUser("/", func(ctx user.IContext) { - ctx.JSON(http.StatusOK, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("PUT", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} - -func (t *UserRouterTest) TestPatchUserRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "PATCH /user status 200", - route: "/user", - expectedCode: http.StatusOK, - }, - { - description: "PATCH HTTP status 404, when route is not exists", - route: "/not-found", - expectedCode: http.StatusNotFound, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.PatchUser("/", func(ctx user.IContext) { - ctx.JSON(http.StatusOK, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("PATCH", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} - -func (t *UserRouterTest) TestDeleteUserRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "DELETE /user status 200", - route: "/user", - expectedCode: http.StatusOK, - }, - { - description: "DELETE HTTP status 404, when route is not exists", - route: "/not-found", - expectedCode: http.StatusNotFound, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.DeleteUser("/", func(ctx user.IContext) { - ctx.JSON(http.StatusOK, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("DELETE", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} diff --git a/src/app/router/vaccine.router_test.go b/src/app/router/vaccine.router_test.go deleted file mode 100644 index 3b8c6b2..0000000 --- a/src/app/router/vaccine.router_test.go +++ /dev/null @@ -1,61 +0,0 @@ -package router - -import ( - "github.com/isd-sgcu/rnkm65-gateway/src/app/handler/vaccine" - "github.com/isd-sgcu/rnkm65-gateway/src/config" - mock "github.com/isd-sgcu/rnkm65-gateway/src/mocks/common" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/suite" - "net/http" - "net/http/httptest" - "testing" -) - -type VaccineRouterTest struct { - suite.Suite -} - -func TestVaccineRouter(t *testing.T) { - suite.Run(t, new(VaccineRouterTest)) -} - -func (t *VaccineRouterTest) TestPostVaccineRouter() { - tests := []struct { - description string - route string - expectedCode int - }{ - { - description: "POST /vaccine status 200", - route: "/vaccine/verify", - expectedCode: http.StatusOK, - }, - { - description: "POST HTTP status 404, when route is not exists", - route: "/not-found", - expectedCode: http.StatusNotFound, - }, - } - - g := mock.GuardMock{} - conf := config.App{ - Port: 3000, - Debug: true, - MaxFileSize: 1000000, - } - - r := NewFiberRouter(&g, conf) - - r.PostVaccine("/verify", func(ctx vaccine.IContext) { - ctx.JSON(http.StatusOK, map[string]string{ - "message": "Hello World", - }) - }) - - for _, test := range tests { - req := httptest.NewRequest("POST", test.route, nil) - res, _ := r.Test(req, 1) - - assert.Equal(t.T(), test.expectedCode, res.StatusCode, test.description) - } -} diff --git a/src/constant/auth/auth.constant.go b/src/constant/auth/auth.constant.go index 0e9f1b8..ddd0e7b 100644 --- a/src/constant/auth/auth.constant.go +++ b/src/constant/auth/auth.constant.go @@ -16,14 +16,10 @@ var MapPath2Phase = map[string][]string{ "GET /group/:token": {"select"}, "POST /group/:token": {"select"}, "DELETE /group/leave": {"select"}, - "PUT /group": {"select"}, - "GET /baan": {"select"}, - "GET /baan/:id": {"select"}, + "PUT /group/select": {"select"}, "DELETE /group/members/:id": {"select"}, "POST /qr/checkin/verify": {"eventDay", "eStamp"}, "POST /qr/checkin/confirm": {"eventDay", "eStamp"}, "POST /qr/estamp/verify": {"eStamp"}, "POST /qr/estamp/confirm": {"eStamp"}, - "GET /estamp": {"eStamp"}, - "GET /estamp/:id": {"eStamp"}, } diff --git a/src/main.go b/src/main.go index 2fb32a7..32c95bd 100644 --- a/src/main.go +++ b/src/main.go @@ -156,7 +156,7 @@ func main() { estampHdr := eHdr.NewHandler(estampSrv, v) ciHandler := ciHdr.NewHandler(checkinSrv, v) - authGuard := guard.NewAuthGuard(athSrv, auth.ExcludePath, conf.App) + authGuard := guard.NewAuthGuard(athSrv, auth.ExcludePath, auth.MapPath2Phase, conf.App) r := router.NewFiberRouter(&authGuard, conf.App) diff --git a/src/mocks/auth/auth.mock.go b/src/mocks/auth/auth.mock.go index 9cc9363..578ec3a 100644 --- a/src/mocks/auth/auth.mock.go +++ b/src/mocks/auth/auth.mock.go @@ -11,6 +11,7 @@ import ( type ContextMock struct { mock.Mock V interface{} + Status int Header map[string]string VerifyTicketDto *dto.VerifyTicket RefreshTokenDto *dto.RedeemNewToken @@ -29,8 +30,9 @@ func (c *ContextMock) Bind(v interface{}) error { return args.Error(1) } -func (c *ContextMock) JSON(_ int, v interface{}) { +func (c *ContextMock) JSON(status int, v interface{}) { c.V = v + c.Status = status } func (c *ContextMock) UserID() string { @@ -63,10 +65,10 @@ func (c *ContextMock) Path() string { return args.String(0) } -func (c *ContextMock) Next() { - _ = c.Called() +func (c *ContextMock) Next() error { + args := c.Called() - return + return args.Error(0) } type ClientMock struct {