forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_tests.js
240 lines (216 loc) · 8.26 KB
/
email_tests.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
var streamBuffers = Npm.require('stream-buffers');
var devWarningBanner = "(Mail not sent; to enable " +
"sending, set the MAIL_URL environment variable.)\n";
function smokeEmailTest(testFunction) {
// This only tests dev mode, so don't run the test if this is deployed.
if (process.env.MAIL_URL) return;
try {
var stream = new streamBuffers.WritableStreamBuffer;
EmailTest.overrideOutputStream(stream);
testFunction(stream);
} finally {
EmailTest.restoreOutputStream();
}
}
function canonicalize(string) {
// Remove generated content for test.equal to succeed.
return string.replace(/Message-ID: <[^<>]*>\r\n/, "Message-ID: <...>\r\n")
.replace(/Date: (?!dummy).*\r\n/, "Date: ...\r\n")
.replace(/(boundary="|^--)--[^\s"]+?(-Part|")/mg, "$1--...$2");
}
Tinytest.add("email - fully customizable", function (test) {
smokeEmailTest(function(stream) {
Email.send({
from: "[email protected]",
to: "[email protected]",
cc: ["[email protected]", "[email protected]"],
subject: "This is the subject",
text: "This is the body\nof the message\nFrom us.",
headers: {
'X-Meteor-Test': 'a custom header',
'Date': 'dummy',
},
});
// XXX brittle if mailcomposer changes header order, etc
test.equal(canonicalize(stream.getContentsAsString("utf8")),
"====== BEGIN MAIL #0 ======\n" +
devWarningBanner +
"Content-Type: text/plain\r\n" +
"X-Meteor-Test: a custom header\r\n" +
"Date: dummy\r\n" +
"From: [email protected]\r\n" +
"To: [email protected]\r\n" +
"Cc: [email protected], [email protected]\r\n" +
"Subject: This is the subject\r\n" +
"Message-ID: <...>\r\n" +
"Content-Transfer-Encoding: 7bit\r\n" +
"MIME-Version: 1.0\r\n" +
"\r\n" +
"This is the body\n" +
"of the message\n" +
"From us.\r\n" +
"====== END MAIL #0 ======\n");
});
});
Tinytest.add("email - undefined headers sends properly", function (test) {
smokeEmailTest(function (stream) {
Email.send({
from: "[email protected]",
to: "[email protected]",
subject: "This is the subject",
text: "This is the body\nof the message\nFrom us.",
});
test.matches(canonicalize(stream.getContentsAsString("utf8")),
/^====== BEGIN MAIL #0 ======$[\s\S]+^To: [email protected]$/m);
});
});
Tinytest.add("email - multiple e-mails same stream", function (test) {
smokeEmailTest(function (stream) {
Email.send({
from: "[email protected]",
to: "[email protected]",
subject: "This is the subject",
text: "This is the body\nof the message\nFrom us.",
});
var contents;
contents = canonicalize(stream.getContentsAsString("utf8"));
test.matches(contents, /^====== BEGIN MAIL #0 ======$/m);
test.matches(contents, /^From: [email protected]$/m);
test.matches(contents, /^To: [email protected]$/m);
Email.send({
from: "[email protected]",
to: "[email protected]",
subject: "This is important",
text: "This is another message\nFrom Qux.",
});
contents = canonicalize(stream.getContentsAsString("utf8"));
test.matches(contents, /^====== BEGIN MAIL #1 ======$/m);
test.matches(contents, /^From: [email protected]$/m);
test.matches(contents, /^To: [email protected]$/m);
});
});
Tinytest.add("email - using mail composer", function (test) {
smokeEmailTest(function (stream) {
// Test direct MailComposer usage.
var mc = new EmailInternals.NpmModules.mailcomposer.module({
from: "[email protected]",
text: "body"
});
Email.send({mailComposer: mc});
test.equal(canonicalize(stream.getContentsAsString("utf8")),
"====== BEGIN MAIL #0 ======\n" +
devWarningBanner +
"Content-Type: text/plain\r\n" +
"From: [email protected]\r\n" +
"Message-ID: <...>\r\n" +
"Content-Transfer-Encoding: 7bit\r\n" +
"Date: ...\r\n" +
"MIME-Version: 1.0\r\n" +
"\r\n" +
"body\r\n" +
"====== END MAIL #0 ======\n");
});
});
Tinytest.add("email - date auto generated", function (test) {
smokeEmailTest(function (stream) {
// Test if date header is automatically generated, if not specified
Email.send({
from: "[email protected]",
to: "[email protected]",
subject: "This is the subject",
text: "This is the body\nof the message\nFrom us.",
headers: {
'X-Meteor-Test': 'a custom header',
},
});
test.matches(canonicalize(stream.getContentsAsString("utf8")),
/^Date: .+$/m);
});
});
Tinytest.add("email - long lines", function (test) {
smokeEmailTest(function (stream) {
// Test that long header lines get wrapped with single leading whitespace,
// and that long body lines get wrapped with quoted-printable conventions.
Email.send({
from: "[email protected]",
to: "[email protected]",
subject: "This is a very very very very very very very very very very very very long subject",
text: "This is a very very very very very very very very very very very very long text",
});
test.equal(canonicalize(stream.getContentsAsString("utf8")),
"====== BEGIN MAIL #0 ======\n" +
devWarningBanner +
"Content-Type: text/plain\r\n" +
"From: [email protected]\r\n" +
"To: [email protected]\r\n" +
"Subject: This is a very very very very very very very very " +
"very very very\r\n very long subject\r\n" +
"Message-ID: <...>\r\n" +
"Content-Transfer-Encoding: quoted-printable\r\n" +
"Date: ...\r\n" +
"MIME-Version: 1.0\r\n" +
"\r\n" +
"This is a very very very very very very very very very very " +
"very very long =\r\ntext\r\n" +
"====== END MAIL #0 ======\n");
});
});
Tinytest.add("email - unicode", function (test) {
smokeEmailTest(function (stream) {
// Test that unicode characters in header and body get encoded.
Email.send({
from: "[email protected]",
to: "[email protected]",
subject: "\u263a",
text: "I \u2665 Meteor",
});
test.equal(canonicalize(stream.getContentsAsString("utf8")),
"====== BEGIN MAIL #0 ======\n" +
devWarningBanner +
"Content-Type: text/plain; charset=utf-8\r\n" +
"From: [email protected]\r\n" +
"To: [email protected]\r\n" +
"Subject: =?UTF-8?B?4pi6?=\r\n" +
"Message-ID: <...>\r\n" +
"Content-Transfer-Encoding: quoted-printable\r\n" +
"Date: ...\r\n" +
"MIME-Version: 1.0\r\n" +
"\r\n" +
"I =E2=99=A5 Meteor\r\n" +
"====== END MAIL #0 ======\n");
});
});
Tinytest.add("email - text and html", function (test) {
smokeEmailTest(function (stream) {
// Test including both text and HTML versions of message.
Email.send({
from: "[email protected]",
to: "[email protected]",
text: "*Cool*, man",
html: "<i>Cool</i>, man",
});
test.equal(canonicalize(stream.getContentsAsString("utf8")),
"====== BEGIN MAIL #0 ======\n" +
devWarningBanner +
"Content-Type: multipart/alternative;\r\n" +
' boundary="--...-Part_1"\r\n' +
"From: [email protected]\r\n" +
"To: [email protected]\r\n" +
"Message-ID: <...>\r\n" +
"Date: ...\r\n" +
"MIME-Version: 1.0\r\n" +
"\r\n" +
"----...-Part_1\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Transfer-Encoding: 7bit\r\n" +
"\r\n" +
"*Cool*, man\r\n" +
"----...-Part_1\r\n" +
"Content-Type: text/html\r\n" +
"Content-Transfer-Encoding: 7bit\r\n" +
"\r\n" +
"<i>Cool</i>, man\r\n" +
"----...-Part_1--\r\n" +
"====== END MAIL #0 ======\n");
});
});