-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandle_email_test.go
96 lines (78 loc) · 2.21 KB
/
handle_email_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
package maildoor_test
import (
"errors"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/wawandco/maildoor"
"github.com/wawandco/maildoor/internal/testhelpers"
)
func TestHandleEmail(t *testing.T) {
// Test the handleEmail endpoint
t.Run("basic test", func(t *testing.T) {
auth := maildoor.New(
maildoor.EmailValidator(func(email string) error {
return nil
}),
maildoor.EmailSender(func(email, html, txt string) error {
return nil
}),
)
w := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/email", nil)
req.Form = url.Values{
"email": []string{"[email protected]"},
}
auth.ServeHTTP(w, req)
testhelpers.Equals(t, http.StatusOK, w.Code)
testhelpers.Contains(t, w.Body.String(), "Check your inbox")
})
t.Run("invalid email", func(t *testing.T) {
auth := maildoor.New(
maildoor.EmailValidator(func(email string) error {
return errors.New("invalid email")
}),
)
w := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/email", nil)
req.Form = url.Values{
"email": []string{"[email protected]"},
}
auth.ServeHTTP(w, req)
testhelpers.Equals(t, http.StatusUnprocessableEntity, w.Code)
testhelpers.Contains(t, w.Body.String(), "invalid email")
})
t.Run("error sending email", func(t *testing.T) {
auth := maildoor.New(
maildoor.EmailValidator(func(email string) error {
return nil
}),
maildoor.EmailSender(func(email, html, txt string) error {
return errors.New("error sending email")
}),
)
w := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/email", nil)
auth.ServeHTTP(w, req)
testhelpers.Equals(t, http.StatusInternalServerError, w.Code)
testhelpers.Contains(t, w.Body.String(), "error sending email")
})
t.Run("calls sending email", func(t *testing.T) {
var textMessage string
auth := maildoor.New(
maildoor.EmailValidator(func(email string) error {
return nil
}),
maildoor.EmailSender(func(email, html, txt string) error {
textMessage = txt
return nil
}),
)
w := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/email", nil)
auth.ServeHTTP(w, req)
testhelpers.Equals(t, http.StatusOK, w.Code)
testhelpers.Contains(t, textMessage, "Code:")
})
}