-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
172 lines (148 loc) · 5.19 KB
/
main_test.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"p3/models"
u "p3/utils"
"reflect"
"strings"
"testing"
"github.com/go-playground/assert/v2"
)
func TestMain(m *testing.M) {
exitCode := m.Run()
//teardown()
os.Exit(exitCode)
}
func teardown() {
ctx, _ := u.Connect()
models.GetDB().Drop(ctx)
}
var JwtAuthSkip = func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
}
func makeRequest(method, url string, requestBody []byte) *httptest.ResponseRecorder {
router := Router(JwtAuthSkip)
recorder := httptest.NewRecorder()
request, _ := http.NewRequest(method, url, bytes.NewBuffer(requestBody))
router.ServeHTTP(recorder, request)
return recorder
}
func TestCreateLoginAccount(t *testing.T) {
// Test create new account
requestBody := []byte(`{
"email": "[email protected]",
"password": "pass123secret"
}`)
recorder := makeRequest("POST", "/api", requestBody)
assert.Equal(t, http.StatusCreated, recorder.Code)
var response map[string]interface{}
json.Unmarshal(recorder.Body.Bytes(), &response)
_, exists := response["account"].(map[string]interface{})["token"].(string)
assert.Equal(t, true, exists)
// Test recreate existing account
recorder = makeRequest("POST", "/api", requestBody)
assert.Equal(t, http.StatusConflict, recorder.Code)
// Test login
recorder = makeRequest("POST", "/api/login", requestBody)
assert.Equal(t, http.StatusOK, recorder.Code)
json.Unmarshal(recorder.Body.Bytes(), &response)
_, exists = response["account"].(map[string]interface{})["token"].(string)
assert.Equal(t, true, exists)
}
func TestObjects(t *testing.T) {
var response map[string]interface{}
var parentId string
var exists bool
// Create a tenant (root)
requestBody := []byte(`{
"name": "DEMOTENANT",
"category": "tenant",
"description": [],
"domain": "DEMO",
"attributes": {
"color": "FFFFFF",
"mainContact": "Moi",
"mainPhone": "0612345678",
"mainEmail": "[email protected]"
}
}`)
recorder := makeRequest("POST", "/api/tenants", requestBody)
assert.Equal(t, http.StatusCreated, recorder.Code)
json.Unmarshal(recorder.Body.Bytes(), &response)
parentId, exists = response["data"].(map[string]interface{})["id"].(string)
assert.Equal(t, true, exists)
// Create objects from schema examples
for _, entInt := range []int{u.SITE, u.BLDG, u.ROOM, u.RACK, u.DEVICE} {
// Get object from schema
entStr := u.EntityToString(entInt)
data, _ := ioutil.ReadFile("models/schemas/" + entStr + "_schema.json")
var obj map[string]interface{}
json.Unmarshal(data, &obj)
obj = obj["examples"].([]interface{})[0].(map[string]interface{})
// Add parentId
obj["parentId"] = parentId
data, _ = json.Marshal(obj)
// Create (POST)
recorder := makeRequest("POST", "/api/"+entStr+"s", data)
assert.Equal(t, http.StatusCreated, recorder.Code)
json.Unmarshal(recorder.Body.Bytes(), &response)
parentId, exists = response["data"].(map[string]interface{})["id"].(string)
assert.Equal(t, true, exists)
// Verify create with GET
hierarchyName := response["data"].(map[string]interface{})["hierarchyName"].(string)
recorder = makeRequest("GET", "/api/"+entStr+"s/"+hierarchyName, nil)
assert.Equal(t, http.StatusOK, recorder.Code)
var responseGET map[string]interface{}
json.Unmarshal(recorder.Body.Bytes(), &responseGET)
delete(responseGET, "message")
delete(response, "message")
assert.Equal(t, true, reflect.DeepEqual(response, responseGET))
// Update with PUT
oldName := obj["name"].(string)
obj["name"] = entStr + "Test"
data, _ = json.Marshal(obj)
recorder = makeRequest("PUT", "/api/"+entStr+"s/"+hierarchyName, data)
assert.Equal(t, http.StatusOK, recorder.Code)
// Verify it
hierarchyName = strings.Replace(hierarchyName, oldName, obj["name"].(string), 1)
recorder = makeRequest("GET", "/api/"+entStr+"s/"+hierarchyName, nil)
assert.Equal(t, http.StatusOK, recorder.Code)
}
// Try to patch tenant name
hierarchyName := "TESTPATCHTENANT"
requestBody = []byte(`{
"name": "` + hierarchyName + `"
}`)
recorder = makeRequest("PATCH", "/api/tenants/DEMOTENANT", requestBody)
assert.Equal(t, http.StatusOK, recorder.Code)
// Verify the whole tree
recorder = makeRequest("GET", "/api/tenants/"+hierarchyName+"/all", nil)
assert.Equal(t, http.StatusOK, recorder.Code)
json.Unmarshal(recorder.Body.Bytes(), &response)
response = response["data"].(map[string]interface{})
for _, entInt := range []int{u.SITE, u.BLDG, u.ROOM, u.RACK, u.DEVICE} {
entStr := u.EntityToString(entInt)
println(entStr)
child := response["children"].([]interface{})[0].(map[string]interface{})
hierarchyName = hierarchyName + "." + entStr + "Test"
assert.Equal(t, hierarchyName, child["hierarchyName"].(string))
if entInt != u.DEVICE {
response = child
}
}
// Delete everything
recorder = makeRequest("DELETE", "/api/tenants/TESTPATCHTENANT", nil)
assert.Equal(t, http.StatusNoContent, recorder.Code)
recorder = makeRequest("GET", "/api/hierarchy", nil)
assert.Equal(t, http.StatusOK, recorder.Code)
json.Unmarshal(recorder.Body.Bytes(), &response)
assert.Equal(t, 0,
len(response["data"].(map[string]interface{})["tree"].(map[string]interface{})))
}