Skip to content

Commit eb478fc

Browse files
committed
test: adiciona testes para rest
1 parent 1935192 commit eb478fc

File tree

5 files changed

+137
-3
lines changed

5 files changed

+137
-3
lines changed

internal/adapters/rest/controller/cart_controller_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controller_test
22

33
import (
4+
"errors"
45
"github.com/gin-gonic/gin"
56
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/adapters/rest/controller"
67
"github.com/pangolin-do-golang/tech-challenge-cart-api/mocks"
@@ -48,6 +49,27 @@ func TestAddProduct_InvalidPayload(t *testing.T) {
4849
}
4950
}
5051

52+
func TestAddProduct_Err(t *testing.T) {
53+
gin.SetMode(gin.TestMode)
54+
router := gin.Default()
55+
mockService := new(mocks.IService)
56+
mockService.
57+
On("AddProduct", mock.Anything, mock.Anything, mock.Anything).
58+
Return(errors.New("error"))
59+
ctrl := controller.NewCartController(mockService)
60+
router.POST("/cart/add-product", ctrl.AddProduct)
61+
62+
payload := `{"client_id":"123e4567-e89b-12d3-a456-426614174000","product_id":"123e4567-e89b-12d3-a456-426614174001","quantity":1}`
63+
req, _ := http.NewRequest(http.MethodPost, "/cart/add-product", strings.NewReader(payload))
64+
req.Header.Set("Content-Type", "application/json")
65+
w := httptest.NewRecorder()
66+
router.ServeHTTP(w, req)
67+
68+
if w.Code != http.StatusInternalServerError {
69+
t.Errorf("Expected status 200, got %d", w.Code)
70+
}
71+
}
72+
5173
func TestEditProduct_Success(t *testing.T) {
5274
gin.SetMode(gin.TestMode)
5375
router := gin.Default()

internal/adapters/rest/handler/product.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/core/product"
77
)
88

9-
func RegisterProductHandlers(router *gin.Engine, service *product.Service) {
9+
func RegisterProductHandlers(router *gin.Engine, service product.IProductService) {
1010
productController := controller.NewProductController(service)
1111

1212
router.GET("/product", productController.Search)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package middleware_test
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/gin-gonic/gin"
9+
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/adapters/rest/middleware"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestCorsMiddleware_AllowsCORS(t *testing.T) {
14+
gin.SetMode(gin.TestMode)
15+
router := gin.Default()
16+
router.Use(middleware.CorsMiddleware())
17+
router.GET("/test", func(c *gin.Context) {
18+
c.String(http.StatusOK, "test")
19+
})
20+
21+
req, _ := http.NewRequest(http.MethodGet, "/test", nil)
22+
w := httptest.NewRecorder()
23+
router.ServeHTTP(w, req)
24+
25+
assert.Equal(t, "*", w.Header().Get("Access-Control-Allow-Origin"))
26+
assert.Equal(t, "GET, POST, PUT, DELETE", w.Header().Get("Access-Control-Allow-Methods"))
27+
assert.Equal(t, "Content-Type, Authorization", w.Header().Get("Access-Control-Allow-Headers"))
28+
assert.Equal(t, http.StatusOK, w.Code)
29+
}
30+
31+
func TestCorsMiddleware_HandlesOptionsRequest(t *testing.T) {
32+
gin.SetMode(gin.TestMode)
33+
router := gin.Default()
34+
router.Use(middleware.CorsMiddleware())
35+
router.GET("/test", func(c *gin.Context) {
36+
c.String(http.StatusOK, "test")
37+
})
38+
39+
req, _ := http.NewRequest(http.MethodOptions, "/test", nil)
40+
w := httptest.NewRecorder()
41+
router.ServeHTTP(w, req)
42+
43+
assert.Equal(t, http.StatusNoContent, w.Code)
44+
}

internal/adapters/rest/server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
)
1212

1313
type RestServer struct {
14-
productService *product.Service
14+
productService product.IProductService
1515
cartService cart.IService
1616
}
1717

1818
type RestServerOptions struct {
19-
ProductService *product.Service
19+
ProductService product.IProductService
2020
CartService cart.IService
2121
}
2222

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package server_test
2+
3+
import (
4+
"github.com/pangolin-do-golang/tech-challenge-cart-api/mocks"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/gin-gonic/gin"
10+
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/adapters/rest/handler"
11+
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/adapters/rest/middleware"
12+
"github.com/pangolin-do-golang/tech-challenge-cart-api/internal/adapters/rest/server"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestHealthEndpoint_ReturnsOK(t *testing.T) {
17+
gin.SetMode(gin.TestMode)
18+
router := gin.Default()
19+
router.Use(middleware.CorsMiddleware())
20+
router.GET("/health", func(c *gin.Context) {
21+
c.Status(http.StatusOK)
22+
})
23+
24+
req, _ := http.NewRequest(http.MethodGet, "/health", nil)
25+
w := httptest.NewRecorder()
26+
router.ServeHTTP(w, req)
27+
28+
assert.Equal(t, http.StatusOK, w.Code)
29+
}
30+
31+
func TestServe_StartsServer(t *testing.T) {
32+
productService := new(mocks.IProductService)
33+
cartService := new(mocks.IService)
34+
rs := server.NewRestServer(&server.RestServerOptions{
35+
ProductService: productService,
36+
CartService: cartService,
37+
})
38+
39+
go func() {
40+
rs.Serve()
41+
}()
42+
}
43+
44+
func TestRegisterProductHandlers_CallsHandler(t *testing.T) {
45+
gin.SetMode(gin.TestMode)
46+
router := gin.Default()
47+
productService := new(mocks.IProductService)
48+
handler.RegisterProductHandlers(router, productService)
49+
50+
req, _ := http.NewRequest(http.MethodGet, "/products", nil)
51+
w := httptest.NewRecorder()
52+
router.ServeHTTP(w, req)
53+
54+
assert.Equal(t, http.StatusOK, w.Code)
55+
}
56+
57+
func TestRegisterCartHandlers_CallsHandler(t *testing.T) {
58+
gin.SetMode(gin.TestMode)
59+
router := gin.Default()
60+
cartService := new(mocks.IService)
61+
handler.RegisterCartHandlers(router, cartService)
62+
63+
req, _ := http.NewRequest(http.MethodGet, "/cart", nil)
64+
w := httptest.NewRecorder()
65+
router.ServeHTTP(w, req)
66+
67+
assert.Equal(t, http.StatusOK, w.Code)
68+
}

0 commit comments

Comments
 (0)