forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_email_send_test.go
96 lines (82 loc) · 2.75 KB
/
test_email_send_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 forms_test
import (
"fmt"
"strings"
"testing"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/pocketbase/pocketbase/forms"
"github.com/pocketbase/pocketbase/tests"
)
func TestEmailSendValidateAndSubmit(t *testing.T) {
t.Parallel()
scenarios := []struct {
template string
email string
collection string
expectedErrors []string
}{
{"", "", "", []string{"template", "email"}},
{"invalid", "[email protected]", "", []string{"template"}},
{forms.TestTemplateVerification, "invalid", "", []string{"email"}},
{forms.TestTemplateVerification, "[email protected]", "invalid", []string{"collection"}},
{forms.TestTemplateVerification, "[email protected]", "demo1", []string{"collection"}},
{forms.TestTemplateVerification, "[email protected]", "users", nil},
{forms.TestTemplatePasswordReset, "[email protected]", "", nil},
{forms.TestTemplateEmailChange, "[email protected]", "", nil},
{forms.TestTemplateOTP, "[email protected]", "", nil},
{forms.TestTemplateAuthAlert, "[email protected]", "", nil},
}
for i, s := range scenarios {
t.Run(fmt.Sprintf("%d_%s", i, s.template), func(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
form := forms.NewTestEmailSend(app)
form.Email = s.email
form.Template = s.template
form.Collection = s.collection
result := form.Submit()
// parse errors
errs, ok := result.(validation.Errors)
if !ok && result != nil {
t.Fatalf("Failed to parse errors %v", result)
}
// check errors
if len(errs) > len(s.expectedErrors) {
t.Fatalf("Expected error keys %v, got %v", s.expectedErrors, errs)
}
for _, k := range s.expectedErrors {
if _, ok := errs[k]; !ok {
t.Fatalf("Missing expected error key %q in %v", k, errs)
}
}
expectedEmails := 1
if len(s.expectedErrors) > 0 {
expectedEmails = 0
}
if app.TestMailer.TotalSend() != expectedEmails {
t.Fatalf("Expected %d email(s) to be sent, got %d", expectedEmails, app.TestMailer.TotalSend())
}
if len(s.expectedErrors) > 0 {
return
}
var expectedContent string
switch s.template {
case forms.TestTemplatePasswordReset:
expectedContent = "Reset password"
case forms.TestTemplateEmailChange:
expectedContent = "Confirm new email"
case forms.TestTemplateVerification:
expectedContent = "Verify"
case forms.TestTemplateOTP:
expectedContent = "one-time password"
case forms.TestTemplateAuthAlert:
expectedContent = "from a new location"
default:
expectedContent = "__UNKNOWN_TEMPLATE__"
}
if !strings.Contains(app.TestMailer.LastMessage().HTML, expectedContent) {
t.Errorf("Expected the email to contains %q, got\n%v", expectedContent, app.TestMailer.LastMessage().HTML)
}
})
}
}