Skip to content

Commit 56ea39e

Browse files
committed
Only add tlsRejectUnauthorized if it is a boolean. Add tests to check various config options for tls.
1 parent 364f514 commit 56ea39e

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/cli/emailNotifications.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,15 @@ async function sendEmailNotification(notificationInfo, errors, debug = false) {
5757
emailBody += 'The stack trace information can be seen in the terminal as well as in the notification email.';
5858
}
5959

60+
// Ensure that the tlsRejectUnauthorized property is a boolean
61+
if (notificationInfo.tlsRejectUnauthorized && (notificationInfo.tlsRejectUnauthorized !== true || notificationInfo.tlsRejectUnauthorized !== false)) {
62+
logger.warn('The notificationInfo.tlsRejectUnauthorized should be a boolean value. The value provided will not be used.');
63+
}
64+
6065
const transporter = nodemailer.createTransport({
6166
host: notificationInfo.host,
6267
...(notificationInfo.port && { port: notificationInfo.port }),
63-
...(notificationInfo.tlsRejectUnauthorized !== undefined && { tls: { rejectUnauthorized: notificationInfo.tlsRejectUnauthorized } }),
68+
...((notificationInfo.tlsRejectUnauthorized === true || notificationInfo.tlsRejectUnauthorized === false) && { tls: { rejectUnauthorized: notificationInfo.tlsRejectUnauthorized } }),
6469
});
6570

6671
logger.debug('Sending email with error information');

test/cli/emailNotifications.test.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('sendEmailNotification', () => {
8080
};
8181

8282
await expect(sendEmailNotification(notificationInfo, errors, false)).resolves.not.toThrow();
83-
expect(createTransportSpy).toBeCalledWith({ host: notificationInfo.host, port: notificationInfo.port, tls: { rejectUnauthorized: false } });
83+
expect(createTransportSpy).toBeCalledWith({ host: notificationInfo.host, port: notificationInfo.port, tls: { rejectUnauthorized: notificationInfo.tlsRejectUnauthorized } });
8484
expect(sendMailMock).toBeCalled();
8585
const sendMailMockArgs = sendMailMock.mock.calls[0][0];
8686
expect(sendMailMockArgs.to).toEqual(notificationInfo.to);
@@ -94,6 +94,48 @@ describe('sendEmailNotification', () => {
9494
expect(sendMailMockArgs.text).not.toMatch(/Error at line 4`/i);
9595
});
9696

97+
it('should send an email with tlsRejectUnauthorized set to true, false, and not set', async () => {
98+
const notificationInfoTLSFalse = {
99+
host: 'my.host.com',
100+
to: ['something@example.com', 'someone@example.com'],
101+
tlsRejectUnauthorized: false,
102+
};
103+
const notificationInfoTLSTrue = {
104+
host: 'my.host.com',
105+
to: ['something@example.com', 'someone@example.com'],
106+
tlsRejectUnauthorized: true,
107+
};
108+
const notificationInfoNoTLS = {
109+
host: 'my.host.com',
110+
to: ['something@example.com', 'someone@example.com'],
111+
};
112+
const notificationInfoUnexpectedTLS = {
113+
host: 'my.host.com',
114+
to: ['something@example.com', 'someone@example.com'],
115+
tlsRejectUnauthorized: 'true', // Any value that is not true or false will log a warning and not be used
116+
};
117+
const errors = {
118+
0: [],
119+
1: [{ message: 'something bad', stack: 'Error at line 1' }],
120+
};
121+
createTransportSpy.mockReturnValueOnce({ sendMail: sendMailMock });
122+
await expect(sendEmailNotification(notificationInfoTLSFalse, errors, false)).resolves.not.toThrow();
123+
expect(createTransportSpy).toBeCalledWith({ host: notificationInfoTLSFalse.host, port: notificationInfoTLSFalse.port, tls: { rejectUnauthorized: false } });
124+
125+
createTransportSpy.mockReturnValueOnce({ sendMail: sendMailMock });
126+
await expect(sendEmailNotification(notificationInfoTLSTrue, errors, false)).resolves.not.toThrow();
127+
expect(createTransportSpy).toBeCalledWith({ host: notificationInfoTLSTrue.host, port: notificationInfoTLSTrue.port, tls: { rejectUnauthorized: true } });
128+
129+
createTransportSpy.mockReturnValueOnce({ sendMail: sendMailMock });
130+
await expect(sendEmailNotification(notificationInfoNoTLS, errors, false)).resolves.not.toThrow();
131+
expect(createTransportSpy).toBeCalledWith({ host: notificationInfoNoTLS.host, port: notificationInfoNoTLS.port }); // No tls object set
132+
133+
createTransportSpy.mockReturnValueOnce({ sendMail: sendMailMock });
134+
await expect(sendEmailNotification(notificationInfoUnexpectedTLS, errors, false)).resolves.not.toThrow();
135+
// A warning will be logged and the unexpected value will not be used
136+
expect(createTransportSpy).toBeCalledWith({ host: notificationInfoUnexpectedTLS.host, port: notificationInfoUnexpectedTLS.port });
137+
});
138+
97139
it('should send an email with stack traces if debug flag was used', async () => {
98140
createTransportSpy.mockReturnValueOnce({ sendMail: sendMailMock });
99141
const notificationInfo = {

0 commit comments

Comments
 (0)