-
Notifications
You must be signed in to change notification settings - Fork 2
/
moxapi_test.go
58 lines (52 loc) · 1.88 KB
/
moxapi_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
package main
import (
"bytes"
"context"
"encoding/json"
"net/http"
"testing"
"github.com/mjl-/bstore"
"github.com/mjl-/mox/webhook"
)
func TestSignup(t *testing.T) {
test := func(from, subject string, validated bool, expUser bool) {
t.Helper()
in := webhook.Incoming{
From: []webhook.NameAddress{{Address: from}},
To: []webhook.NameAddress{{Address: config.SignupAddress}},
Subject: subject,
MessageID: random() + "@localhost",
Meta: webhook.IncomingMeta{
MsgFromValidated: validated,
},
}
inbuf, err := json.Marshal(in)
tcheckf(t, err, "marshal incoming webhook")
u := moxhookin.URL + config.Mox.Webhook.IncomingPath
inreq, err := http.NewRequest("POST", u, bytes.NewReader(inbuf))
tcheckf(t, err, "request for incoming webhook")
inreq.Header.Set("Content-Type", "application/json")
inreq.SetBasicAuth(config.Mox.Webhook.Username, config.Mox.Webhook.Password)
inresp, err := http.DefaultClient.Do(inreq)
tcheckf(t, err, "http transaction for incoming webhook")
tcompare(t, inresp.StatusCode, http.StatusOK)
if expUser {
tneedmail0(t, "re: signup for "+config.ServiceName)
}
user, err := bstore.QueryDB[User](ctxbg, database).Get()
if !expUser {
tcompare(t, err, bstore.ErrAbsent)
return
}
tcheckf(t, err, "get user for verify token")
req := http.Request{Header: http.Header{}, RemoteAddr: "127.0.0.1:1234"} // For rate limiter.
resp := httpResponse{http.Header{}} // For capturing cookies to use in next call.
reqInfo := requestInfo{user.Email, user.ID, &resp, &req}
ctx := context.WithValue(ctxbg, requestInfoCtxKey, reqInfo)
api := API{}
api.UserRemove(ctx)
}
test("[email protected]", "signup for "+config.ServiceName, true, true)
test("[email protected]", "spam subject", true, false)
test("[email protected]", "signup for "+config.ServiceName, false, false)
}