Repository navigation
Expand file tree
/
Copy pathapi_test.go
More file actions
52 lines (47 loc) · 1.54 KB
/
Copy pathapi_test.go
File metadata and controls
52 lines (47 loc) · 1.54 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
package chat
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestSubmitContacts_AcceptsOnlyBoundedResolvedUserIDs(t *testing.T) {
db := newTestDB(t)
owner := testIdentity("tenant", 1)
request := httptest.NewRequest(http.MethodPost, "/contacts", bytes.NewBufferString(`[{"user_id":2},{"user_id":2}]`))
response := httptest.NewRecorder()
SubmitContacts(owner, db, response, request)
if response.Code != http.StatusOK {
t.Fatalf("valid contacts status=%d body=%s", response.Code, response.Body.String())
}
var count int
if err := db.Get(&count, `SELECT count(*) FROM contacts_v2`); err != nil || count != 1 {
t.Fatalf("valid contact count=%d error=%v", count, err)
}
tests := map[string]string{
"empty": `[]`,
"self only": `[{"user_id":1}]`,
"invalid id": `[{"user_id":0}]`,
"unknown field": `[{"user_id":2,"mobile":"0990000000"}]`,
}
tooMany := make([]ContactsRequest, maxContactsPerRequest+1)
for index := range tooMany {
tooMany[index].UserID = int64(index + 2)
}
encoded, err := json.Marshal(tooMany)
if err != nil {
t.Fatalf("marshal oversized contacts: %v", err)
}
tests["too many"] = string(encoded)
for name, body := range tests {
t.Run(name, func(t *testing.T) {
request := httptest.NewRequest(http.MethodPost, "/contacts", bytes.NewBufferString(body))
response := httptest.NewRecorder()
SubmitContacts(owner, db, response, request)
if response.Code != http.StatusBadRequest {
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
}
})
}
}