From 1ff1fc6edfb34ddcc720d690955582bf8894bd70 Mon Sep 17 00:00:00 2001 From: Jeffrey Koehler Date: Thu, 21 Oct 2021 02:31:17 -0500 Subject: [PATCH] add tests for SMTP errors --- server/notification-providers/smtp.spec.js | 134 ++++++++++++++++++++- 1 file changed, 128 insertions(+), 6 deletions(-) diff --git a/server/notification-providers/smtp.spec.js b/server/notification-providers/smtp.spec.js index dcae967..1444235 100644 --- a/server/notification-providers/smtp.spec.js +++ b/server/notification-providers/smtp.spec.js @@ -2,9 +2,14 @@ jest.mock("nodemailer", () => ({ createTransport: jest.fn(), })); const mockNodeMailer = require("nodemailer"); +const { UP } = require("../../src/util"); const SMTP = require("./smtp"); +beforeEach(() => { + mockNodeMailer.createTransport.mockReset(); +}); + describe("notification default information", () => { it("should have the correct name", () => { let notification = new SMTP(); @@ -29,7 +34,11 @@ describe("notification to act properly on send", () => { smtpSecure: "secure", smtpUsername: "username", smtpPassword: "password", - customSubject: "custom subject", + customSubject: "", + smtpFrom: "From", + smtpCC: "CC", + smtpBCC: "BCC", + smtpTo: "To", }; let msg = "PassedInMessage"; let monitorConf = { }; @@ -47,15 +56,128 @@ describe("notification to act properly on send", () => { }); expect(res).toBe("Sent Successfully."); expect(sender).toHaveBeenCalledWith({ - bcc: undefined, - cc: undefined, - from: undefined, - subject: "custom subject", + bcc: "BCC", + cc: "CC", + from: "From", + subject: "PassedInMessage", text: "PassedInMessage\nTime (UTC): undefined", tls: { rejectUnauthorized: false, }, - to: undefined, + to: "To", }); }); + + it("should use the proper email subject", async () => { + let sender = jest.fn() + .mockResolvedValue(() => { + return; + }); + mockNodeMailer.createTransport.mockImplementationOnce(() => { + return { sendMail: sender }; + }); + + let notif = new SMTP(); + let notificationConf = { + smtpHost: "host", + smtpPort: "port", + smtpSecure: "secure", + smtpUsername: "username", + smtpPassword: "password", + customSubject: "Name: {{NAME}} | Status: {{STATUS}} | Hostname: {{HOSTNAME_OR_URL}}", + smtpFrom: "From", + smtpCC: "CC", + smtpBCC: "BCC", + smtpTo: "To", + }; + let msg = "PassedInMessage"; + let monitorConf = { + type: "http", + url: "https://www.google.com", + name: "testing", + }; + let heartbeatConf = { + status: UP, + + }; + let res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf); + + expect(mockNodeMailer.createTransport).toHaveBeenCalledWith({ + auth: { + pass: "password", + user: "username", + }, + host: "host", + port: "port", + secure: "secure", + }); + expect(res).toBe("Sent Successfully."); + expect(sender).toHaveBeenCalledWith({ + bcc: "BCC", + cc: "CC", + from: "From", + subject: "Name: testing | Status: ✅ Up | Hostname: https://www.google.com", + text: "PassedInMessage\nTime (UTC): undefined", + tls: { + rejectUnauthorized: false, + }, + to: "To", + }); + }); +}); + +describe("notification to act properly on error from transport", () => { + it("should pass a createTransport error on", async () => { + let sender = jest.fn() + .mockResolvedValue(() => { + return; + }); + mockNodeMailer.createTransport.mockImplementationOnce(() => { + throw new Error("Test Error"); + }); + + let notif = new SMTP(); + let notificationConf = { }; + let msg = "PassedInMessage"; + let monitorConf = { }; + let heartbeatConf = { }; + let res = ""; + try { + res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf); + expect(true).toBe(false); + } catch (e) { + expect(e.message).toBe("Test Error"); + } + + expect(mockNodeMailer.createTransport).toHaveBeenCalledTimes(1); + expect(res).toBe(""); + expect(sender).toHaveBeenCalledTimes(0); + }); + + it("should pass a send mail error on", async () => { + let sender = jest.fn() + .mockRejectedValue(new Error("Test Error")); + mockNodeMailer.createTransport.mockImplementationOnce(() => { + return { sendMail: sender }; + + }); + + let notif = new SMTP(); + let notificationConf = { }; + let msg = "PassedInMessage"; + let monitorConf = { }; + let heartbeatConf = { }; + let res = ""; + try { + res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf); + expect("threw error").toBe(false); + } catch (e) { + expect(e.message).toBe("Test Error"); + } + + expect(mockNodeMailer.createTransport).toHaveBeenCalledTimes(1); + expect(res).toBe(""); + expect(sender).toHaveBeenCalledTimes(1); + }); + });