-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendmail_test.go
More file actions
163 lines (145 loc) · 3.59 KB
/
Copy pathsendmail_test.go
File metadata and controls
163 lines (145 loc) · 3.59 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"testing"
)
func TestBuildAndSend(t *testing.T) {
cfg := &Config{
Smtp: struct {
User string `env:"SMTP_USER"`
Password string `env:"SMTP_PASS"`
Host string `env:"SMTP_HOST" envDefault:"localhost"`
Port int `env:"SMTP_PORT" envDefault:"1025"`
}{
User: "",
Password: "",
Host: "localhost",
Port: 1025,
},
}
sub := FormSubmission{
Id: "example",
FormCfg: FormConfig{
Mail: struct {
Recipient string
Sender string
Subject string
}{
Recipient: "recipient@example.com",
Sender: "sender@example.com",
Subject: "Test Subject",
},
Fields: struct {
Name string
Email string
Message string
Honeypot string
}{
Name: "name",
Email: "email",
Message: "message",
Honeypot: "honeypot",
},
},
Body: map[string]string{
"email": "replyto@example.com",
},
}
err := buildAndSend(cfg, sub)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
}
func TestBuildEmailMessage(t *testing.T) {
formCfg := FormConfig{
Mail: struct {
Recipient string
Sender string
Subject string
}{
Recipient: "recipient@example.com",
Sender: "sender@example.com",
Subject: "Test Subject",
},
Fields: struct {
Name string
Email string
Message string
Honeypot string
}{
Name: "name",
Email: "email",
Message: "message",
Honeypot: "honeypot",
},
}
sub := FormSubmission{
Id: "example",
FormCfg: formCfg,
Body: map[string]string{
"name": "TestName",
"email": "test@example.com",
"message": "Testing the message field.",
},
}
msg, err := buildEmailMessage(sub)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
headers := "From: <" + sub.FormCfg.Mail.Sender + ">\r\n" +
"To: <" + sub.FormCfg.Mail.Recipient + ">\r\n" +
"Subject: " + sub.FormCfg.Mail.Subject + " - " + sub.Id + "\r\n" +
"Reply-To: <" + sub.Body[sub.FormCfg.Fields.Email] + ">\r\n" +
"MIME-version: 1.0;\r\nContent-Type: text/html; charset=\"UTF-8\";\r\n\r\n"
expectedMsg := message{
Subject: "Test Subject - example",
Body: []byte(headers + "TestName test@example.com Testing the message field."),
Recipient: "<recipient@example.com>",
Sender: "<sender@example.com>",
ReplyTo: "test@example.com",
}
if string(msg.Body) != string(expectedMsg.Body) {
t.Errorf("Expected body %q, got %q", expectedMsg.Body, msg.Body)
}
if msg.Recipient != expectedMsg.Recipient {
t.Errorf("Expected recipient %q, got %q", expectedMsg.Recipient, msg.Recipient)
}
if msg.Sender != expectedMsg.Sender {
t.Errorf("Expected sender %q, got %q", expectedMsg.Sender, msg.Sender)
}
if msg.ReplyTo != expectedMsg.ReplyTo {
t.Errorf("Expected reply-to %q, got %q", expectedMsg.ReplyTo, msg.ReplyTo)
}
}
func TestSendEmail(t *testing.T) {
cfg := &Config{
Smtp: struct {
User string `env:"SMTP_USER"`
Password string `env:"SMTP_PASS"`
Host string `env:"SMTP_HOST" envDefault:"localhost"`
Port int `env:"SMTP_PORT" envDefault:"1025"`
}{
User: "",
Password: "",
Host: "localhost",
Port: 1025,
},
}
msg := message{
Subject: "Test Subject",
Body: []byte("Test message"),
Recipient: "recipient@example.com",
Sender: "sender@example.com",
ReplyTo: "replyto@example.com",
}
err := sendEmail(cfg, msg)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
}
func TestLoadTemplate(t *testing.T) {
id := "example"
tmpl := loadTemplate(id)
if tmpl == nil {
t.Errorf("Expected template, got nil")
}
}