forked from autotelic/fastify-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
271 lines (219 loc) · 8.53 KB
/
index.test.js
File metadata and controls
271 lines (219 loc) · 8.53 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const { test } = require('tap')
const Fastify = require('fastify')
const fastifyMail = require('.')
const nunjucks = require('nunjucks')
const { relative, resolve } = require('path')
const sinon = require('sinon')
const testContext = { name: 'Test Name' }
const testHtmlTemplate =
'<!DOCTYPE html>\n' +
'<html lang="en">\n' +
' <head></head>\n' +
' <body>\n' +
' <p>Name: {{ name }}</p>\n' +
' </body>\n' +
'</html>\n'
const testTextTemplate = 'This is a test text message to {{ name }}'
const testHtml =
'<!DOCTYPE html>\n' +
'<html lang="en">\n' +
' <head></head>\n' +
' <body>\n' +
' <p>Name: Test Name</p>\n' +
' </body>\n' +
'</html>\n'
const testMessage = {
to: 'to@ignoreme.com',
from: 'from@ignoreme.com',
cc: 'cc@ignoreme.com',
bcc: 'bcc@ignoreme.com',
replyTo: 'reply@ignoreme.com',
subject: 'This is a plain text subject',
html: testHtml,
text: 'This is a plain text email message.'
}
const responseWhenTemplatesPresent = {
to: 'to@ignoreme.com',
from: 'from@ignoreme.com',
cc: 'cc@ignoreme.com',
bcc: 'bcc@ignoreme.com',
replyTo: 'reply@ignoreme.com',
subject: 'This is a plain text subject',
html: testHtml,
text: 'This is a test text message to Test Name'
}
test('mail, nodemailer & view decorators exist', async t => {
t.teardown(async () => fastify.close())
const fastify = Fastify()
fastify.register(fastifyMail, { pov: { engine: { nunjucks } }, transporter: { jsonTransport: true } })
await fastify.ready()
t.ok(fastify.hasDecorator('mail'))
t.ok(fastify.hasDecorator('nodemailer'))
t.ok(fastify.hasDecorator('view'))
})
test('view decorator does not exist if the engine is not provided', async (t) => {
t.teardown(() => fastify.close())
const fastify = Fastify()
fastify.register(fastifyMail, { transporter: { jsonTransport: true } })
t.notOk(fastify.hasDecorator('view'))
})
test('throws an error if point-of-view is not registered', async (t) => {
t.teardown(() => fastify.close())
const fastify = Fastify()
fastify.register(fastifyMail, { transporter: { jsonTransport: true } })
await t.rejects(fastify.ready(), Error('fastify-mail requires a view decorator.'))
t.notOk(fastify.hasDecorator('view'))
})
test('throws an error if an invalid transporter is given', async (t) => {
t.teardown(() => fastify.close())
const fastify = Fastify()
fastify.register(fastifyMail, { pov: { engine: { nunjucks } }, transporter: 'error' })
await t.rejects(fastify.ready(), Error('Cannot create property \'mailer\' on string \'error\''))
})
test('fastify-mail uses templates to send mail when point-of-view is registered separately', async (t) => {
t.teardown(() => {
fastify.close()
sendMailStub.restore()
})
const testTemplates = t.testdir({
'html.njk': t.fixture('file', testHtmlTemplate),
'text.njk': t.fixture('file', testTextTemplate)
})
const povConfig = {
propertyName: 'foo',
engine: { nunjucks },
includeViewExtension: true,
options: { filename: resolve('templates') }
}
const fastify = Fastify()
fastify.register(require('@fastify/view'), povConfig)
fastify.after(() => {
fastify.register(fastifyMail, { pov: { propertyName: 'foo' }, transporter: { jsonTransport: true } })
})
await fastify.ready()
const sendMailStub = sinon.stub(fastify.nodemailer, 'sendMail')
await fastify.mail.sendMail(testMessage, { templatePath: relative(__dirname, testTemplates), context: testContext })
t.ok(fastify.hasDecorator('foo'))
t.same(sendMailStub.args[0], [responseWhenTemplatesPresent])
t.is(sendMailStub.args.length, 1)
})
test('fastify-mail uses string variables (for text and html) when a template is not present', async (t) => {
t.teardown(() => {
fastify.close()
sendMailStub.restore()
})
const povConfig = {
propertyName: 'foo',
engine: { nunjucks },
includeViewExtension: true,
options: { filename: resolve('templates') }
}
const fastify = Fastify()
fastify.register(require('@fastify/view'), povConfig)
fastify.after(() => {
fastify.register(fastifyMail, { pov: { propertyName: 'foo' }, transporter: { jsonTransport: true } })
})
await fastify.ready()
const sendMailStub = sinon.stub(fastify.nodemailer, 'sendMail')
await fastify.mail.sendMail(testMessage, { context: testContext })
t.ok(fastify.hasDecorator('foo'))
t.same(sendMailStub.args[0], [testMessage])
t.is(sendMailStub.args.length, 1)
})
test('fastify-mail uses text template when available but defaults to provided html if no template is available', async (t) => {
t.teardown(() => {
fastify.close()
sendMailStub.restore()
})
const testTemplates = t.testdir({
'text.njk': t.fixture('file', testTextTemplate)
})
const povConfig = {
propertyName: 'foo',
engine: { nunjucks },
includeViewExtension: true,
options: { filename: resolve('templates') }
}
const fastify = Fastify()
fastify.register(require('@fastify/view'), povConfig)
fastify.after(() => {
fastify.register(fastifyMail, { pov: { propertyName: 'foo' }, transporter: { jsonTransport: true } })
})
await fastify.ready()
const sendMailStub = sinon.stub(fastify.nodemailer, 'sendMail')
await fastify.mail.sendMail(testMessage, { templatePath: relative(__dirname, testTemplates), context: testContext })
t.ok(fastify.hasDecorator('foo'))
t.same(sendMailStub.args[0][0].html, testHtml)
t.is(sendMailStub.args.length, 1)
})
test('fastify-mail uses html template when available but defaults to provided text if no template is available', async (t) => {
t.teardown(() => {
fastify.close()
sendMailStub.restore()
})
const testTemplates = t.testdir({
'html.njk': t.fixture('file', testHtmlTemplate)
})
const povConfig = {
propertyName: 'foo',
engine: { nunjucks },
includeViewExtension: true,
options: { filename: resolve('templates') }
}
const fastify = Fastify()
fastify.register(require('@fastify/view'), povConfig)
fastify.after(() => {
fastify.register(fastifyMail, { pov: { propertyName: 'foo' }, transporter: { jsonTransport: true } })
})
await fastify.ready()
const sendMailStub = sinon.stub(fastify.nodemailer, 'sendMail')
await fastify.mail.sendMail(testMessage, { templatePath: relative(__dirname, testTemplates), context: testContext })
t.ok(fastify.hasDecorator('foo'))
t.same(sendMailStub.args[0][0].html, testHtml)
t.same(sendMailStub.args[0][0].text, 'This is a plain text email message.')
t.is(sendMailStub.args.length, 1)
})
test('fastify.mail.sendMail calls nodemailer.sendMail with correct arguments', async t => {
t.teardown(() => {
fastify.close()
sendMailStub.restore()
})
const testTemplates = t.testdir({
'html.njk': t.fixture('file', testHtmlTemplate),
'text.njk': t.fixture('file', testTextTemplate)
})
const fastify = Fastify()
fastify.register(fastifyMail, { pov: { engine: { nunjucks } }, transporter: { jsonTransport: true } })
await fastify.ready()
const sendMailStub = sinon.stub(fastify.nodemailer, 'sendMail')
await fastify.mail.sendMail(testMessage, { templatePath: relative(__dirname, testTemplates), context: testContext })
t.same(sendMailStub.args[0], [responseWhenTemplatesPresent])
t.is(sendMailStub.args.length, 1)
})
test('fastify.mail.sendMail returns an error when required values are not present (from)', async (t) => {
t.teardown(() => fastify.close())
const fastify = Fastify()
fastify.register(fastifyMail, { pov: { engine: { nunjucks } }, transporter: { jsonTransport: true } })
await fastify.ready()
const { error } = await fastify.mail.sendMail({ to: 'to@ignoreme.com', subject: 'subject@ignoreme.com' })
t.ok(error instanceof TypeError)
t.is(error.message, '"from" must be defined')
})
test('fastify.mail.sendMail returns an error multiple required values are missing', async (t) => {
t.teardown(() => fastify.close())
const fastify = Fastify()
fastify.register(fastifyMail, { pov: { engine: { nunjucks } }, transporter: { jsonTransport: true } })
await fastify.ready()
const { error } = await fastify.mail.sendMail({ from: 'from@ignoreme.com' })
t.ok(error instanceof TypeError)
t.is(error.message, '"to" must be defined\n"subject" must be defined')
})
test('fastify.mail.sendMail returns an error when no message is defined', async (t) => {
t.teardown(() => fastify.close())
const fastify = Fastify()
fastify.register(fastifyMail, { pov: { engine: { nunjucks } }, transporter: { jsonTransport: true } })
await fastify.ready()
const { error } = await fastify.mail.sendMail()
t.ok(error instanceof TypeError)
t.is(error.message, 'message is not defined')
})