-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
addf2d8
commit f8e03ca
Showing
4 changed files
with
102 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
|
||
|
@@ -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":"[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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |