forked from doublesharp/nodemailer-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodemailer-mock.js
250 lines (220 loc) · 7.92 KB
/
nodemailer-mock.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
241
242
243
244
245
246
247
248
249
250
'use strict';
const debug = require('debug')('nodemailer-mock');
const nodemailer = require('nodemailer');
const messages = require('./lib/messages');
// this mocks the functionality of nodemailer
const NodemailerMock = (function NodemailerMock() {
// the real nodemailer transport
let transport = null;
let mockedVerify = true;
// our response messages
let successResponse = messages.success_response;
let failResponse = messages.fail_response;
// Sent mail cache
let sentMail = [];
const _userPlugins = {
compile: [],
stream: [],
};
// Should the callback be a success or failure?
let shouldFail = false;
let shouldFailOnce = false;
// Is idle?
let isIdle = true;
// Should idle?
let shouldIdle = true;
// Determine if the test should return success or failure
const determineResponseSuccess = function determineResponseSuccess() {
return new Promise((resolve, reject) => {
// determine if we want to return an error
if (shouldFail) {
// if this is a one time failure, reset the status
if (shouldFailOnce) {
shouldFail = shouldFailOnce = false;
}
return reject(new Error('nodemailer-mock fail response'));
}
return resolve();
});
};
// return an object with the expected functions
const createTransport = function createTransport(options) {
// indicate that we are creating a transport
debug('createTransport', options);
let listeners = {
'idle': []
}
transport = nodemailer.createTransport(options);
return {
on: (event, listener) => {
if (event === 'idle' && isIdle) {
listener()
}
listeners[event].push(listener)
},
isIdle: () => isIdle,
removeListener: (event, listener) => {
const li = listeners[event]
for (let i = 0; i < li.length; i++) {
if (li[i] == listener) {
li.splice(i, 1)
}
}
},
// this will mock the nodemailer.transport.sendMail()
sendMail: (email, callback) => {
// indicate that sendMail() has been called
debug('transport.sendMail', email);
// support either callback or promise api
const isPromise = !callback && typeof Promise === 'function';
// start with a basic info object
const info = messages.info();
return determineResponseSuccess()
.then(() => {
// Resolve/Success
// add the email to our cache
sentMail.push(email);
// update the response
info.response = successResponse;
// indicate that we are sending success
debug('transport.sendMail', 'SUCCESS', info);
if (shouldIdle) {
for (const listener of listeners['idle']) {
listener()
}
}
// return success
if (isPromise) {
return Promise.resolve(info);
}
return callback(null, info);
})
.catch(() => {
// Reject/Failure
// update the response
info.response = failResponse;
// indicate that we are sending an error
debug('transport.sendMail', 'FAIL', failResponse, info);
// return the error
if (isPromise) {
return Promise.reject(failResponse);
}
return callback(failResponse, info);
});
},
verify: (callback) => {
// should we mock the verify request?
if (mockedVerify) {
// support either callback or promise api
const isPromise = !callback && typeof Promise === 'function';
return determineResponseSuccess()
.then(() => isPromise ? Promise.resolve(successResponse) : callback(null, successResponse))
.catch(() => isPromise ? Promise.reject(failResponse) : callback(failResponse));
}
// use the real nodemailer transport to verify
return transport.verify(callback);
},
use: (step, plugin) => {
step = (step || '').toString();
if (!_userPlugins.hasOwnProperty(step)) {
_userPlugins[step] = [plugin];
} else {
_userPlugins[step].push(plugin);
}
return;
},
// the options this transport was created with
mock: {
options,
},
};
};
// these functions provide test functionality
const mock = {
/**
* determine if sendMail() should return errors once then succeed
*/
setShouldFailOnce: () => {
shouldFail = shouldFailOnce = true;
},
/**
* determine if sendMail() should immediately trigger idle
*/
setShouldIdle: () => {
shouldIdle = true;
},
/**
* determines whether or not the SMTP is currently idling.
* @param {boolean} idle the new idle state
*/
setIdle: (idle) => {
isIdle = idle
if (idle) {
for (const listener of listeners['idle']) {
listener()
}
}
},
/**
* determine if sendMail() should return errors
* @param {boolean} isFail true will return errors, false will return successes
*/
setShouldFail: (isFail) => {
shouldFail = isFail;
},
/**
* determine if transport.verify() should be mocked or not
* @param {Boolean} isMocked if the function should be mocked
*/
setMockedVerify: (isMocked) => {
mockedVerify = isMocked;
},
/**
* set the response messages for successes
* @param {Mixed} response
*/
setSuccessResponse: (response) => {
successResponse = response;
},
/**
* set the response messages for failures
* @param {Mixed} response
*/
setFailResponse: (response) => {
failResponse = response;
},
/**
* get an array of sent emails
* @return {Object[]} an array of emails
*/
getSentMail: () => sentMail,
/**
* reset mock values to defaults
*/
reset: () => {
sentMail = [];
shouldFail = shouldFailOnce = false;
successResponse = messages.success_response;
failResponse = messages.fail_response;
mockedVerify = true;
isIdle = true;
shouldIdle = true;
},
};
// legacy aliases
Object.assign(mock, {
shouldFailOnce: mock.setShouldFailOnce,
shouldFail: mock.setShouldFail,
mockedVerify: mock.setMockedVerify,
successResponse: mock.setSuccessResponse,
failResponse: mock.setFailResponse,
sentMail: mock.getSentMail,
});
return {
// Our mocked transport
createTransport,
// Test helper methods
mock,
};
}());
module.exports = NodemailerMock;