forked from doublesharp/nodemailer-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodemailer-mock.js
224 lines (204 loc) · 6.88 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
'use strict';
const debug = require('debug')('nodemailer-mock');
const realmailer = require('nodemailer');
const messages = require('./lib/messages');
// alias old function names to new function names
const setLegacyAliases = (mock) => {
// legacy aliases
Object.assign(mock, {
shouldFailOnce: mock.setShouldFailOnce,
shouldFail: mock.setShouldFail,
mockedVerify: mock.setMockedVerify,
successResponse: mock.setSuccessResponse,
failResponse: mock.setFailResponse,
sentMail: mock.getSentMail,
shouldFailCheck: mock.shouldFailCheck,
});
};
function NodemailerMock(nodemailer) {
// the real nodemailer transport
let transport = null;
let _mockedVerify = true;
// our response messages
let _successResponse = messages.success_response;
let _failResponse = messages.fail_response;
// transport plugins
const _userPluginsDefault = {
compile: [],
stream: [],
};
let _userPlugins = { ..._userPluginsDefault };
// Sent mail cache
let _sentMail = [];
// Should the callback be a success or failure?
let _shouldFail = false;
let _shouldFailOnce = false;
let _shouldFailCheck = null;
// Determine if the test should return success or failure
const determineResponseSuccess = function determineResponseSuccess(email) {
return new Promise((resolve, reject) => {
// if shouldFailCheck defined use it
if (email && _shouldFailCheck && _shouldFailCheck(email)) {
return reject(new Error('nodemailer-mock fail response'));
}
// 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);
// in some mocks the real nodemailer won't be available
/* istanbul ignore else */
if (typeof nodemailer.createTransport === 'function') {
transport = nodemailer.createTransport(options);
}
return {
// 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(email)
.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);
// 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 (!transport || _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) => {
const stepId = (step || '').toString();
if (!Object.prototype.hasOwnProperty.call(_userPlugins, stepId)) {
_userPlugins[stepId] = [plugin];
} else {
_userPlugins[stepId].push(plugin);
}
return;
},
// the options this transport was created with
mock: {
options,
/**
* get a dictionary of plugins used by this transport
* @return {Object} plugins keyed by the step id
*/
getPlugins: () => _userPlugins,
},
};
};
// these functions provide test functionality
const mock = {
/**
* determine if sendMail() should return errors once then succeed
*/
setShouldFailOnce: () => (_shouldFail = _shouldFailOnce = true),
/**
* 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 {Error} error
*/
setFailResponse: (error) => (_failResponse = error),
/**
* set the check function that returns true if a message send should fail
* @param {function} check
* * */
setShouldFailCheck: (check) => (_shouldFailCheck = check),
/**
* get an array of sent emails
* @return {Object[]} an array of emails
*/
getSentMail: () => _sentMail,
/**
* reset mock values to defaults
*/
reset: () => {
_userPlugins = { ..._userPluginsDefault };
_sentMail = [];
_shouldFail = _shouldFailOnce = false;
_successResponse = messages.success_response;
_failResponse = messages.fail_response;
_mockedVerify = true;
_shouldFailCheck = null;
},
};
setLegacyAliases(mock);
return {
// Our mocked transport
createTransport,
// Test helper methods
mock,
// Will the real nodemailer please stand up
nodemailer,
};
}
// this mocks the functionality of nodemailer
module.exports = NodemailerMock(realmailer);
// use this to pass in a real nodemailer instance
module.exports.getMockFor = (nodemailer = realmailer) =>
NodemailerMock(nodemailer);