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
|
package forge
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"regexp"
"strings"
"sync"
"testing"
"time"
)
type fakeCloudflare struct {
sync.Mutex
sent []map[string]string
bounce bool
headers http.Header
}
func (f *fakeCloudflare) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var msg map[string]string
json.NewDecoder(r.Body).Decode(&msg)
f.Lock()
f.sent = append(f.sent, msg)
f.headers = r.Header.Clone()
bounce := f.bounce
f.Unlock()
if r.URL.Path != "/accounts/acct/email/sending/send" {
w.WriteHeader(404)
w.Write([]byte(`{"success":false,"errors":[{"code":10000,"message":"not found"}],"result":null}`))
return
}
if bounce {
w.Write([]byte(`{"success":true,"errors":[],"result":{"delivered":[],"queued":[],"permanent_bounces":["` + msg["to"] + `"]}}`))
return
}
w.Write([]byte(`{"success":true,"errors":[],"result":{"delivered":["` + msg["to"] + `"],"queued":[],"permanent_bounces":[]}}`))
}
func (f *fakeCloudflare) wait(t *testing.T, n int) map[string]string {
t.Helper()
for i := 0; i < 200; i++ {
f.Lock()
if len(f.sent) >= n {
m := f.sent[n-1]
f.Unlock()
return m
}
f.Unlock()
time.Sleep(10 * time.Millisecond)
}
t.Fatalf("message %d never arrived", n)
return nil
}
func withCloudflare(t *testing.T) (*fakeCloudflare, *Mailer) {
fake := &fakeCloudflare{}
srv := httptest.NewServer(fake)
t.Cleanup(srv.Close)
old := cloudflareAPI
cloudflareAPI = srv.URL
t.Cleanup(func() { cloudflareAPI = old })
return fake, &Mailer{Account: "acct", Token: "secret", From: "gitpocket <[email protected]>"}
}
func TestCloudflareSender(t *testing.T) {
fake, m := withCloudflare(t)
if err := m.Send("[email protected]", "hi\r\nBcc: [email protected]", "body"); err != nil {
t.Fatal(err)
}
got := fake.wait(t, 1)
if got["from"] != "[email protected]" || got["to"] != "[email protected]" || strings.ContainsAny(got["subject"], "\r\n") {
t.Errorf("sent %v", got)
}
if fake.headers.Get("Authorization") != "Bearer secret" {
t.Error("token not sent")
}
fake.bounce = true
if err := m.Send("[email protected]", "s", "b"); err == nil || !strings.Contains(err.Error(), "bounced") {
t.Errorf("bounce not reported: %v", err)
}
m.Account = "other"
if err := m.Send("[email protected]", "s", "b"); err == nil {
t.Error("api error not reported")
}
}
func TestSignupVerifyAndReset(t *testing.T) {
fake, m := withCloudflare(t)
s, h := testServer(t)
s.mailer = m
link := regexp.MustCompile(`https://example\.org(/[a-z]+/[0-9a-f]+)`)
w := post(h, "/register", url.Values{"name": {"alice"}, "password": {"correct horse"}, "email": {"[email protected]"}}, "")
if w.Code != http.StatusSeeOther || session(w) == "" {
t.Fatalf("signup with email failed: %d", w.Code)
}
verify := fake.wait(t, 1)
if verify["to"] != "[email protected]" {
t.Fatalf("verification went to %q", verify["to"])
}
path := link.FindStringSubmatch(verify["text"])
if path == nil || !strings.HasPrefix(path[1], "/verify/") {
t.Fatalf("no verification link in %q", verify["text"])
}
if w := get(h, path[1], ""); w.Code != 200 {
t.Fatalf("verification link gave %d", w.Code)
}
post(h, "/reset", url.Values{"who": {"alice"}}, "")
reset := fake.wait(t, 2)
path = link.FindStringSubmatch(reset["text"])
if path == nil || !strings.HasPrefix(path[1], "/reset/") {
t.Fatalf("no reset link in %q", reset["text"])
}
if w := post(h, path[1], url.Values{"password": {"battery staple"}}, ""); w.Code != http.StatusSeeOther {
t.Fatalf("reset gave %d", w.Code)
}
if w := post(h, path[1], url.Values{"password": {"another one!"}}, ""); w.Code != 400 {
t.Errorf("reset link worked twice: %d", w.Code)
}
if _, ok := s.store.CheckPassword("alice", "battery staple"); !ok {
t.Error("new password does not log in")
}
if w := post(h, "/register", url.Values{"name": {"bob"}, "password": {"correct horse"}, "email": {"[email protected]\r\nBcc: [email protected]"}}, ""); w.Code != 400 {
t.Errorf("injected address accepted: %d", w.Code)
}
post(h, "/register", url.Values{"name": {"carol"}, "password": {"correct horse"}, "email": {"[email protected]"}}, "")
fake.wait(t, 3)
post(h, "/reset", url.Values{"who": {"carol"}}, "")
time.Sleep(100 * time.Millisecond)
fake.Lock()
n := len(fake.sent)
fake.Unlock()
if n != 3 {
t.Errorf("reset mail sent to an unverified address, %d messages", n)
}
}
|