Repository navigation
Expand file tree
/
Copy pathapi_test.go
More file actions
283 lines (266 loc) · 9.1 KB
/
Copy pathapi_test.go
File metadata and controls
283 lines (266 loc) · 9.1 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"regexp"
"sort"
"strings"
"testing"
"time"
)
// These tests enforce the rules in docs/API.md. Each failure names the rule.
type apiRoute struct{ method, path, auth string }
// documentedRoutes reads the endpoint table in docs/API.md.
func documentedRoutes(t *testing.T) []apiRoute {
t.Helper()
b, err := os.ReadFile("docs/API.md")
if err != nil {
t.Fatal(err)
}
row := regexp.MustCompile(`(?m)^\| (GET|POST|PUT|DELETE|PATCH) \| (/api/[^ |]*) \| ([^|]*) \|`)
var out []apiRoute
for _, m := range row.FindAllStringSubmatch(string(b), -1) {
out = append(out, apiRoute{m[1], m[2], strings.TrimSpace(m[3])})
}
if len(out) == 0 {
t.Fatal("no endpoint rows found in docs/API.md")
}
return out
}
// Rules 2 and 7: every /api route the code registers names its method, and the
// documentation lists exactly the routes the code registers.
func TestAPIRoutesDocumentedAndMethodBound(t *testing.T) {
re := regexp.MustCompile(`HandleFunc\("(?:([A-Z]+) )?(/api/[^"]*)"`)
files, _ := os.ReadDir(".")
code := map[string]bool{}
for _, f := range files {
if !strings.HasSuffix(f.Name(), ".go") || strings.HasSuffix(f.Name(), "_test.go") {
continue
}
b, _ := os.ReadFile(f.Name())
for _, m := range re.FindAllStringSubmatch(string(b), -1) {
if m[1] == "" {
t.Errorf("rule 2: %s registers %s without a method", f.Name(), m[2])
continue
}
code[m[1]+" "+m[2]] = true
}
}
doc := map[string]bool{}
for _, r := range documentedRoutes(t) {
doc[r.method+" "+r.path] = true
}
for k := range code {
if !doc[k] {
t.Errorf("rule 7: %s is registered but not in docs/API.md", k)
}
}
for k := range doc {
if !code[k] {
t.Errorf("rule 7: %s is in docs/API.md but not registered", k)
}
}
}
// apiHarness is an instance with a login, --edit, --zabbix and one target with data.
func apiHarness(t *testing.T) (http.Handler, string, int64) {
t.Helper()
old := loginFailDelay
loginFailDelay = 0
t.Cleanup(func() { loginFailDelay = old })
tg := TargetCfg{Name: "api-target", Type: "tcp", Host: "127.0.0.1", Port: 9}
s, err := NewStore(t.TempDir(), []TargetCfg{tg})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { s.Close() })
now := time.Now().Unix()
for i := 3; i >= 0; i-- {
s.Append("api-target", Round{T: now - int64(i)*60, S: 10, R: 9, MS: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9}})
}
run := NewRunner(ProbeCfg{Packets: 1, TimeoutMs: 50}, s, NewDetector(s))
cfg := &Config{Editable: true, StatsToken: testStatsToken, RetentionDays: 93, HotDays: 2}
h := newMux(cfg, s, run, map[string]string{"admin": "pw"})
w := apiCall(h, "POST", "/api/login", `{"user":"admin","pass":"pw"}`, "", "")
if w.Code != 200 {
t.Fatalf("rule 5: login with snake_case keys: %d %s", w.Code, w.Body)
}
var tok string
for _, c := range w.Result().Cookies() {
if c.Name == "fogping_session" {
tok = c.Value
}
}
id, _ := s.targetID("api-target")
return h, tok, id
}
func apiCall(h http.Handler, method, path, body, session, bearer string) *httptest.ResponseRecorder {
req := httptest.NewRequest(method, "http://fp.local"+path, strings.NewReader(body))
if body != "" {
req.Header.Set("Content-Type", "application/json")
}
if session != "" {
req.Header.Set("Cookie", "fogping_session="+session)
}
if bearer != "" {
req.Header.Set("Authorization", "Bearer "+bearer)
}
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
return w
}
// checkAPIResponse applies rules 1 and 3 to any /api response, and rule 4 to an
// error.
func checkAPIResponse(t *testing.T, label string, w *httptest.ResponseRecorder) {
t.Helper()
if got := w.Header().Get("API-Version"); got != apiVersion {
t.Errorf("rule 1: %s: API-Version = %q", label, got)
}
if ct := w.Header().Get("Content-Type"); !strings.HasPrefix(ct, "application/json") {
t.Errorf("rule 3: %s: Content-Type = %q", label, ct)
}
if w.Code < 400 {
return
}
var e map[string]any
if err := json.Unmarshal(w.Body.Bytes(), &e); err != nil {
t.Errorf("rule 4: %s: error body is not JSON: %q", label, w.Body)
return
}
msg, _ := e["error"].(string)
code, _ := e["code"].(string)
if len(e) != 2 || msg == "" || code != errorCode(w.Code) {
t.Errorf("rule 4: %s: error body %s, want {error, code:%q}", label, w.Body, errorCode(w.Code))
}
}
// concrete turns a documented path into one a request can use.
func concrete(path string, id int64) string {
return strings.ReplaceAll(path, "{id}", itoa64(id))
}
func itoa64(n int64) string { b, _ := json.Marshal(n); return string(b) }
// allowedFor lists the documented methods whose pattern matches a concrete path.
func allowedFor(routes []apiRoute, path string) map[string]bool {
out := map[string]bool{}
for _, r := range routes {
pat := "^" + regexp.MustCompile(`\\\{[a-z_]+\\\}`).ReplaceAllString(regexp.QuoteMeta(r.path), `[^/]+`) + "$"
if regexp.MustCompile(pat).MatchString(path) {
out[r.method] = true
}
}
return out
}
// Rules 1–4: wrong methods, missing credentials and unknown paths all answer in
// the one error shape, with the version header.
func TestAPIErrorsFollowTheRules(t *testing.T) {
h, session, id := apiHarness(t)
routes := documentedRoutes(t)
for _, r := range routes {
path := concrete(r.path, id)
allowed := allowedFor(routes, path)
for _, m := range []string{"GET", "POST", "PUT", "DELETE", "PATCH"} {
if allowed[m] || (m == "HEAD" && allowed["GET"]) {
continue
}
w := apiCall(h, m, path, "", session, testStatsToken)
label := m + " " + path
if w.Code != http.StatusMethodNotAllowed {
t.Errorf("rule 2: %s = %d, want 405", label, w.Code)
continue
}
if w.Header().Get("Allow") == "" {
t.Errorf("rule 2: %s: 405 without Allow", label)
}
checkAPIResponse(t, label, w)
}
if strings.HasPrefix(r.auth, "session") {
w := apiCall(h, r.method, path, "", "", "")
if w.Code != http.StatusUnauthorized {
t.Errorf("rule 6: %s %s without a session = %d, want 401", r.method, path, w.Code)
}
checkAPIResponse(t, r.method+" "+path+" (no session)", w)
}
if r.auth == "bearer token" {
w := apiCall(h, r.method, path, "", "", "")
if w.Code != http.StatusUnauthorized {
t.Errorf("rule 6: %s %s without a token = %d, want 401", r.method, path, w.Code)
}
checkAPIResponse(t, r.method+" "+path+" (no token)", w)
}
}
for _, p := range []string{"/api/nope", "/api/targets/1/nope", "/api/"} {
w := apiCall(h, "GET", p, "", session, "")
if w.Code != http.StatusNotFound {
t.Errorf("rule 4: GET %s = %d, want 404", p, w.Code)
}
checkAPIResponse(t, "GET "+p, w)
}
w := apiCall(h, "POST", "/api/login", `{"user":"admin","pass":"wrong"}`, "", "")
checkAPIResponse(t, "bad login", w)
w = apiCall(h, "POST", "/api/targets", "name=x", session, "")
checkAPIResponse(t, "form body", w)
}
var snakeKey = regexp.MustCompile(`^[a-z][a-z0-9]*(_[a-z0-9]+)*$`)
func checkKeys(t *testing.T, label string, v any) {
t.Helper()
switch x := v.(type) {
case map[string]any:
keys := make([]string, 0, len(x))
for k := range x {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
if !snakeKey.MatchString(k) {
t.Errorf("rule 5: %s: key %q is not snake_case", label, k)
}
checkKeys(t, label+"."+k, x[k])
}
case []any:
for _, e := range x {
checkKeys(t, label+"[]", e)
}
}
}
// Rules 1, 3 and 5 on every successful read.
func TestAPIReadsFollowTheRules(t *testing.T) {
h, session, _ := apiHarness(t)
for _, p := range []string{"/api/version", "/api/targets", "/api/series?target=api-target&minutes=60", "/api/targets/export", "/api/stats"} {
w := apiCall(h, "GET", p, "", session, testStatsToken)
if w.Code != 200 {
t.Errorf("GET %s = %d %s", p, w.Code, w.Body)
continue
}
checkAPIResponse(t, "GET "+p, w)
var v any
if err := json.Unmarshal(w.Body.Bytes(), &v); err != nil {
t.Errorf("GET %s: not JSON: %v", p, err)
continue
}
checkKeys(t, p, v)
}
}
// Rule 3 does not touch the pages: they stay HTML, without the API header.
func TestAPIRulesLeavePagesAlone(t *testing.T) {
h, session, _ := apiHarness(t)
w := apiCall(h, "GET", "/", "", session, "")
if !strings.HasPrefix(w.Header().Get("Content-Type"), "text/html") || w.Header().Get("API-Version") != "" {
t.Errorf("page: %d %q %q", w.Code, w.Header().Get("Content-Type"), w.Header().Get("API-Version"))
}
w = apiCall(h, "GET", "/nope", "", session, "")
if w.Code != 404 || w.Header().Get("API-Version") != "" {
t.Errorf("non-API 404 changed: %d %v", w.Code, w.Header())
}
}
// Writes refuse fields they do not know, naming the bad one and the good ones.
func TestAPIRefusesUnknownFields(t *testing.T) {
h, session, _ := apiHarness(t)
w := apiCall(h, "POST", "/api/targets", `{"type":"tcp","host":"127.0.0.1","port":1,"interval":30}`, session, "")
if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), `unknown field \"interval\"`) || !strings.Contains(w.Body.String(), "interval_sec") {
t.Fatalf("unknown field: %d %s", w.Code, w.Body)
}
checkAPIResponse(t, "unknown field", w)
if w := apiCall(h, "POST", "/api/targets", `{"type":"tcp","host":"127.0.0.1","port":1,"interval_sec":30}`, session, ""); w.Code != 200 {
t.Fatalf("known fields refused: %d %s", w.Code, w.Body)
}
}