diff --git a/internal/handler/http/fakes/fake_userservice.go b/internal/handler/http/fakes/fake_userservice.go new file mode 100644 index 0000000..683e218 --- /dev/null +++ b/internal/handler/http/fakes/fake_userservice.go @@ -0,0 +1,40 @@ +package fakes + +import ( + "bphn/artikel-hukum/api" + "context" +) + +type FakeUserService struct{} + +func (f *FakeUserService) Create(ctx context.Context) error { + return nil +} + +func (f *FakeUserService) List(ctx context.Context) ([]api.UserDataResponse, error) { + var users = []api.UserDataResponse{ + { + Id: 1, + FullName: "Frans Filasta Pratama", + Email: "mail@fransfp.dev", + Avatar: "https://ui-avatars.com/api/?uppercase=false&name=frans", + Role: "admin", + }, + { + Id: 2, + FullName: "Rahma Fitri", + Email: "rahmafitri92@gmail.com", + Avatar: "https://ui-avatars.com/api/?uppercase=false&name=rahma+fitri", + Role: "author", + }, + { + Id: 3, + FullName: "Ibrahim Finra Achernar", + Email: "finn@fransfp.dev", + Avatar: "https://ui-avatars.com/api/?uppercase=false&name=finn", + Role: "author", + }, + } + + return users, nil +} diff --git a/internal/handler/http/user_handler.go b/internal/handler/http/user_handler.go index ad937a3..f8f9f66 100644 --- a/internal/handler/http/user_handler.go +++ b/internal/handler/http/user_handler.go @@ -1,22 +1,39 @@ package http import ( + "bphn/artikel-hukum/internal/service" "github.com/labstack/echo/v4" "net/http" ) type UserRequestHandler struct { *Handler + userService service.UserService } -func NewUserRequestHandler(handler *Handler) *UserRequestHandler { - return &UserRequestHandler{handler} +func NewUserRequestHandler(handler *Handler, userService service.UserService) *UserRequestHandler { + return &UserRequestHandler{handler, userService} } func (h *UserRequestHandler) List(ctx echo.Context) error { - err := ctx.JSON(http.StatusOK, "ok") + users, err := h.userService.List(ctx.Request().Context()) + + if err != nil { + return err + } + + err = ctx.JSON(http.StatusOK, users) if err != nil { return err } return nil } + +func (h *UserRequestHandler) Create(ctx echo.Context) error { + if err := h.userService.Create(ctx.Request().Context()); err != nil { + // TODO Error Struct + return ctx.JSON(http.StatusInternalServerError, "Oppss") + } + + return nil +} diff --git a/internal/handler/http/user_handler_test.go b/internal/handler/http/user_handler_test.go index 7effc12..a9db9a6 100644 --- a/internal/handler/http/user_handler_test.go +++ b/internal/handler/http/user_handler_test.go @@ -1,17 +1,20 @@ package http import ( + "bphn/artikel-hukum/api" + "bphn/artikel-hukum/internal/handler/http/fakes" custommiddlerware "bphn/artikel-hukum/internal/middleware" "bphn/artikel-hukum/pkg/config" "bphn/artikel-hukum/pkg/log" + "encoding/json" "flag" "fmt" "github.com/labstack/echo/v4" - "github.com/labstack/echo/v4/middleware" "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "os" + "strings" "testing" ) @@ -35,8 +38,7 @@ func TestMain(m *testing.M) { e = echo.New() // register middlewares - e.Use(middleware.RequestLoggerWithConfig(custommiddlerware.RequestLoggerMiddleware(logger))) - e.Use(middleware.CORS()) + custommiddlerware.SetupMiddleware(conf, logger, e) code := m.Run() fmt.Println("test end") @@ -45,21 +47,39 @@ func TestMain(m *testing.M) { } -func TestUserHandler(t *testing.T) { - t.Run("should return 200 http code", func(t *testing.T) { +func TestUserRequestHandler_List(t *testing.T) { - req := httptest.NewRequest(http.MethodGet, "/api/users", nil) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) + req := httptest.NewRequest(http.MethodGet, "/api/users", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) - userHandler := NewUserRequestHandler(handler) + userHandler := NewUserRequestHandler(handler, &fakes.FakeUserService{}) - err := userHandler.List(c) - if err != nil { - panic(err) - } + err := userHandler.List(c) + if err != nil { + panic(err) + } - assert.Equal(t, http.StatusOK, rec.Code) + var users []api.UserDataResponse + unmarshalErr := json.Unmarshal(rec.Body.Bytes(), &users) - }) + if unmarshalErr != nil { + panic(unmarshalErr) + } + + assert.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, len(users), 3) +} + +func TestUserRequestHandler_Create(t *testing.T) { + userJSON := `{"full_name":"Jon Snow","email":"jon@labstack.com","password":"password","role":"editor"}` + req := httptest.NewRequest(http.MethodPost, "/api/users", strings.NewReader(userJSON)) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + userHandler := NewUserRequestHandler(handler, &fakes.FakeUserService{}) + + if err := userHandler.Create(c); err != nil { + panic(err) + } } diff --git a/internal/service/service.go b/internal/service/service.go new file mode 100644 index 0000000..64ce93c --- /dev/null +++ b/internal/service/service.go @@ -0,0 +1,7 @@ +package service + +import "bphn/artikel-hukum/pkg/log" + +type Service struct { + logger *log.Logger +}