-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathproducts.controller_test.go
More file actions
58 lines (46 loc) · 1.38 KB
/
products.controller_test.go
File metadata and controls
58 lines (46 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"bytes"
"encoding/json"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestInsertNewProduct(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
var product Products
product.Name = "test category"
product.CategoryId = 1
product.Description = "product description"
product.Price = 10000
jsonValue, _ := json.Marshal(product)
req, _ := http.NewRequest("POST", "/v1/companies/1/products", bytes.NewBuffer(jsonValue))
req.Header.Add("Authorization", "Bearer "+os.Getenv("JWT_TEST_TOKEN"))
router.ServeHTTP(w, req)
assert.Equal(t, 201, w.Code)
}
func TestInsertNewProductFailed(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
var product Products
product.Name = ""
product.CategoryId = 0
product.Description = ""
product.Price = 0
jsonValue, _ := json.Marshal(product)
req, _ := http.NewRequest("POST", "/v1/companies/1/products", bytes.NewBuffer(jsonValue))
req.Header.Add("Authorization", "Bearer "+os.Getenv("JWT_TEST_TOKEN"))
router.ServeHTTP(w, req)
assert.Equal(t, 400, w.Code)
}
func TestGetAllProducts(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/v1/companies/1/products", nil)
req.Header.Add("Authorization", "Bearer "+os.Getenv("JWT_TEST_TOKEN"))
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
}