Skip to content

Commit

Permalink
http handler for user request
Browse files Browse the repository at this point in the history
  • Loading branch information
fransfilastap committed Dec 29, 2023
1 parent addf2d8 commit f8e03ca
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 18 deletions.
40 changes: 40 additions & 0 deletions internal/handler/http/fakes/fake_userservice.go
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
Avatar: "https://ui-avatars.com/api/?uppercase=false&name=frans",
Role: "admin",
},
{
Id: 2,
FullName: "Rahma Fitri",
Email: "[email protected]",
Avatar: "https://ui-avatars.com/api/?uppercase=false&name=rahma+fitri",
Role: "author",
},
{
Id: 3,
FullName: "Ibrahim Finra Achernar",
Email: "[email protected]",
Avatar: "https://ui-avatars.com/api/?uppercase=false&name=finn",
Role: "author",
},
}

return users, nil
}
23 changes: 20 additions & 3 deletions internal/handler/http/user_handler.go
Original file line number Diff line number Diff line change
@@ -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

Check failure on line 11 in internal/handler/http/user_handler.go

View workflow job for this annotation

GitHub Actions / build

undefined: service.UserService
}

func NewUserRequestHandler(handler *Handler) *UserRequestHandler {
return &UserRequestHandler{handler}
func NewUserRequestHandler(handler *Handler, userService service.UserService) *UserRequestHandler {

Check failure on line 14 in internal/handler/http/user_handler.go

View workflow job for this annotation

GitHub Actions / build

undefined: service.UserService
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
}
50 changes: 35 additions & 15 deletions internal/handler/http/user_handler_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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")
Expand All @@ -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":"[email protected]","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)
}
}
7 changes: 7 additions & 0 deletions internal/service/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package service

import "bphn/artikel-hukum/pkg/log"

type Service struct {
logger *log.Logger
}

0 comments on commit f8e03ca

Please sign in to comment.