-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail_test.go
More file actions
113 lines (103 loc) · 2.92 KB
/
mail_test.go
File metadata and controls
113 lines (103 loc) · 2.92 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
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
package gin_test
import (
"errors"
"os"
"path/filepath"
"testing"
engine "github.com/darkit/gin"
"github.com/darkit/gin/pkg/mail"
)
func TestSendMail_Basic(t *testing.T) {
cfg := mail.MailConfig{
Host: "smtp.example.com",
Port: 587,
From: "noreply@example.com",
}
if err := mail.InitDefaultMailer(cfg); err != nil {
t.Fatalf("init mailer: %v", err)
}
if err := engine.SendMail("to@example.com", "标题", "内容"); err == nil {
t.Fatalf("expected send error")
}
}
func TestSendMailHTML(t *testing.T) {
cfg := mail.MailConfig{
Host: "smtp.example.com",
Port: 587,
From: "noreply@example.com",
}
if err := mail.InitDefaultMailer(cfg); err != nil {
t.Fatalf("init mailer: %v", err)
}
if err := engine.SendMailHTML("to@example.com", "标题", "<h1>内容</h1>"); err == nil {
t.Fatalf("expected send error")
}
}
func TestSendTemplate(t *testing.T) {
dir := t.TempDir()
templateDir := filepath.Join(dir, "mail", "templates")
if err := os.MkdirAll(templateDir, 0o755); err != nil {
t.Fatalf("mkdir template dir: %v", err)
}
templatePath := filepath.Join(templateDir, "welcome.html")
if err := os.WriteFile(templatePath, []byte("<h1>{{.Name}}</h1>"), 0o644); err != nil {
t.Fatalf("write template: %v", err)
}
old, _ := os.Getwd()
if err := os.Chdir(dir); err != nil {
t.Fatalf("chdir: %v", err)
}
t.Cleanup(func() {
_ = os.Chdir(old)
})
if _, err := mail.RenderTemplate("welcome.html", map[string]any{"Name": "张三"}); err != nil {
t.Fatalf("render template: %v", err)
}
}
func TestSendMail_WithAttachment(t *testing.T) {
cfg := mail.MailConfig{
Host: "smtp.example.com",
Port: 587,
From: "noreply@example.com",
}
if err := mail.InitDefaultMailer(cfg); err != nil {
t.Fatalf("init mailer: %v", err)
}
attachment := filepath.Join(t.TempDir(), "report.txt")
if err := os.WriteFile(attachment, []byte("report"), 0o644); err != nil {
t.Fatalf("write attachment: %v", err)
}
if err := engine.SendMail("to@example.com", "标题", "内容", mail.WithMailAttachment(attachment)); err == nil {
t.Fatalf("expected send error")
}
}
func TestSendBatch(t *testing.T) {
cfg := mail.MailConfig{
Host: "smtp.example.com",
Port: 587,
From: "noreply@example.com",
}
if err := mail.InitDefaultMailer(cfg); err != nil {
t.Fatalf("init mailer: %v", err)
}
if _, err := engine.SendBatch([]string{"a@example.com", "b@example.com"}, "标题", "内容"); err == nil {
t.Fatalf("expected send error")
}
if _, err := engine.SendBatch(nil, "标题", "内容"); !errors.Is(err, mail.ErrMailToMissing) {
t.Fatalf("expected empty recipients error")
}
}
func TestSendMail_TLS(t *testing.T) {
cfg := mail.MailConfig{
Host: "smtp.example.com",
Port: 465,
From: "noreply@example.com",
TLS: true,
}
if err := mail.InitDefaultMailer(cfg); err != nil {
t.Fatalf("init mailer: %v", err)
}
if err := engine.SendMail("to@example.com", "标题", "内容"); err == nil {
t.Fatalf("expected send error")
}
}