forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_api_test.go
213 lines (204 loc) · 6.02 KB
/
d_api_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package uadmin
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
// TestdAPI to test dAPI
func TestDAPI(t *testing.T) {
u1 := &User{
Username: "u1",
Password: "u1",
Active: true,
RemoteAccess: true,
Admin: true,
}
u1.Save()
s1 := &Session{
Active: true,
UserID: u1.ID,
}
s1.GenerateKey()
s1.Save()
e := []struct {
url string
session string
validate func(string) string
}{
{
"/api/d",
s1.Key,
func(v string) string {
if v != dAPIHelp {
return fmt.Sprintf("Invalid return for dAPI url=%%s")
}
return ""
},
},
{
"/api/d/$allmodels",
s1.Key,
func(v string) string {
obj := map[string]interface{}{}
json.Unmarshal([]byte(v), &obj)
if result, ok := obj["result"].([]interface{}); !ok {
return fmt.Sprintf("Invalid return for dAPI url=%%s. No 'result' in response")
} else if len(result) != 17 {
return fmt.Sprintf("Invalid length of 'result' dAPI url=%%s. Expected %d got %d", 17, len(result))
}
return ""
},
},
{
"/api/d/user/read",
s1.Key,
func(v string) string {
obj := map[string]interface{}{}
json.Unmarshal([]byte(v), &obj)
if result, ok := obj["result"].([]interface{}); !ok {
return fmt.Sprintf("Invalid return for dAPI url=%%s. No 'result' in response")
} else if len(result) != 2 {
return fmt.Sprintf("Invalid length of 'result' dAPI url=%%s. Expected %d got %d", 2, len(result))
}
return ""
},
},
{
"/api/d/user/read/" + fmt.Sprint(u1.ID),
s1.Key,
func(v string) string {
obj := map[string]interface{}{}
json.Unmarshal([]byte(v), &obj)
if result, ok := obj["result"]; !ok {
return fmt.Sprintf("Invalid return for dAPI url=%%s. No 'result' in response")
} else if _, ok := result.(map[string]interface{}); !ok {
return fmt.Sprintf("Invalid value 'result' dAPI url=%%s. Expected map got %v", result)
}
return ""
},
},
{
"/api/d/user/none_existing_command",
s1.Key,
func(v string) string {
obj := map[string]interface{}{}
json.Unmarshal([]byte(v), &obj)
if result, ok := obj["status"].(string); !ok {
return fmt.Sprintf("Invalid return for dAPI url=%%s. No 'status' in response")
} else if result != "error" {
return fmt.Sprintf("Invalid value of 'status' dAPI url=%%s. Expected %s got %s", "error", result)
}
return ""
},
},
{
"/api/d/none_existing_model/read",
s1.Key,
func(v string) string {
obj := map[string]interface{}{}
json.Unmarshal([]byte(v), &obj)
if result, ok := obj["status"].(string); !ok {
return fmt.Sprintf("Invalid return for dAPI url=%%s. No 'status' in response")
} else if result != "error" {
return fmt.Sprintf("Invalid value of 'status' dAPI url=%%s. Expected %s got %s", "error", result)
}
return ""
},
},
{
"/api/d/testmodela/add?_name=test_dAPI&x-csrf-token=" + s1.Key,
s1.Key,
func(v string) string {
obj := map[string]interface{}{}
json.Unmarshal([]byte(v), &obj)
if result, ok := obj["status"].(string); !ok {
return fmt.Sprintf("Invalid return for dAPI url=%%s. No 'status' in response")
} else if result != "ok" {
return fmt.Sprintf("Invalid value of 'status' dAPI url=%%s. Expected %s got %s. %s", "ok", result, v)
}
if result, ok := obj["rows_count"]; !ok {
return fmt.Sprintf("Invalid return for dAPI url=%%s. No 'rows_count' in response")
} else if result.(float64) != 1 {
return fmt.Sprintf("Invalid value of 'rows_count' dAPI url=%%s. Expected %f got %f", 1.0, result.(float64))
}
if result, ok := obj["id"]; !ok {
return fmt.Sprintf("Invalid return for dAPI url=%%s. No 'id' in response")
} else if len(result.([]interface{})) != 1 {
return fmt.Sprintf("Invalid length of 'id' dAPI url=%%s. Expected %d got %d", 1, len(result.([]interface{})))
}
return ""
},
},
{
"/api/d/testmodela/edit?name=test_dAPI&_name=test_dAPI2&x-csrf-token=" + s1.Key,
s1.Key,
func(v string) string {
obj := map[string]interface{}{}
json.Unmarshal([]byte(v), &obj)
if result, ok := obj["status"].(string); !ok {
return fmt.Sprintf("Invalid return for dAPI url=%%s. No 'status' in response")
} else if result != "ok" {
return fmt.Sprintf("Invalid value of 'status' dAPI url=%%s. Expected %s got %s", "ok", result)
}
return ""
},
},
{
"/api/d/testmodela/delete?name=test_dAPI2&x-csrf-token=" + s1.Key,
s1.Key,
func(v string) string {
obj := map[string]interface{}{}
json.Unmarshal([]byte(v), &obj)
if result, ok := obj["status"].(string); !ok {
return fmt.Sprintf("Invalid return for dAPI url=%%s. No 'status' in response")
} else if result != "ok" {
return fmt.Sprintf("Invalid value of 'status' dAPI url=%%s. Expected %s got %s", "ok", result)
}
return ""
},
},
{
"/api/d/testmodela/schema",
s1.Key,
func(v string) string {
obj := map[string]interface{}{}
json.Unmarshal([]byte(v), &obj)
if result, ok := obj["status"].(string); !ok {
return fmt.Sprintf("Invalid return for dAPI url=%%s. No 'status' in response")
} else if result != "ok" {
return fmt.Sprintf("Invalid value of 'status' dAPI url=%%s. Expected %s got %s", "ok", result)
}
if result, ok := obj["result"]; !ok {
return fmt.Sprintf("Invalid return for dAPI url=%%s. No 'result' in response")
} else if _, ok := result.(map[string]interface{}); !ok {
return fmt.Sprintf("Invalid value 'result' dAPI url=%%s. Expected map got %v", result)
}
return ""
},
},
}
for i := range e {
r := httptest.NewRequest("GET", e[i].url, nil)
if e[i].session != "" {
c := http.Cookie{}
c.Name = "session"
c.Value = e[i].session
r.AddCookie(&c)
}
w := httptest.NewRecorder()
apiHandler(w, r)
buf, err := ioutil.ReadAll(w.Result().Body)
if err != nil {
t.Errorf("Unable to read dAPI response")
continue
}
if msg := e[i].validate(string(buf)); msg != "" {
t.Errorf(msg, e[i].url)
}
}
Delete(s1)
Delete(u1)
}