diff --git a/server/notification-providers/apprise.js b/server/notification-providers/apprise.js index fdcd8d6..90cefac 100644 --- a/server/notification-providers/apprise.js +++ b/server/notification-providers/apprise.js @@ -1,22 +1,22 @@ const NotificationProvider = require("./notification-provider"); -const child_process = require("child_process"); +const childProcess = require("child_process"); class Apprise extends NotificationProvider { name = "apprise"; async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { - let s = child_process.spawnSync("apprise", [ "-vv", "-b", msg, notification.appriseURL]) + let s = childProcess.spawnSync("apprise", [ "-vv", "-b", msg, notification.appriseURL]); let output = (s.stdout) ? s.stdout.toString() : "ERROR: maybe apprise not found"; if (output) { if (! output.includes("ERROR")) { - return "Sent Successfully"; + return this.sendSuccess; } - throw new Error(output) + throw new Error(output); } else { return "No output from apprise"; } diff --git a/server/notification-providers/apprise.spec.js b/server/notification-providers/apprise.spec.js index 6cb5a1f..148521a 100644 --- a/server/notification-providers/apprise.spec.js +++ b/server/notification-providers/apprise.spec.js @@ -1,13 +1,15 @@ -// jest.mock("nodemailer", () => ({ -// createTransport: jest.fn(), -// })); +jest.mock("child_process", () => ({ + spawnSync: jest.fn(), +})); -// const mockNodeMailer = require("nodemailer"); +const childProcess = require("child_process"); +const { UP } = require("../../src/util"); +const NotificationSend = require("../notification"); const Apprise = require("./apprise"); beforeEach(() => { - // mockNodeMailer.createTransport.mockReset(); + childProcess.spawnSync.mockReset(); }); describe("notification default information", () => { @@ -16,3 +18,95 @@ describe("notification default information", () => { expect(notification.name).toBe("apprise"); }); }); + +describe("notification to act properly on send", () => { + it("should call apprise with the proper default data", async () => { + + childProcess.spawnSync.mockImplementationOnce(() => { + return { stdout: "response" }; + }); + + let notif = new Apprise(); + let notificationConf = { + appriseURL: "appriseURL", + }; + let msg = "PassedInMessage"; + let res = await notif.send(notificationConf, msg, null, null); + + expect(childProcess.spawnSync).toHaveBeenCalledWith("apprise", ["-vv", "-b", "PassedInMessage", "appriseURL"]); + expect(res).toBe("Sent Successfully."); + }); + + //TODO code under test unreachable. Remove or resolve. + // it("should call output no data when no data", async () => { + + // childProcess.spawnSync.mockImplementationOnce(() => { + // return { stdout: "" }; + // }); + + // let notif = new Apprise(); + // let notificationConf = { + // appriseURL: "appriseURL", + // }; + // let msg = "PassedInMessage"; + // let res = await notif.send(notificationConf, msg, null, null); + + // expect(childProcess.spawnSync).toHaveBeenCalledWith("apprise", ["-vv", "-b", "PassedInMessage", "appriseURL"]); + // expect(res).toBe("No output from apprise"); + // }); + +}); + +describe("notification to act properly on errors from apprise", () => { + it("should call apprise with the proper default data", async () => { + + childProcess.spawnSync.mockImplementationOnce(() => { + return { stdout: "ERROR FROM APPRISE" }; + }); + + let notif = new Apprise(); + let notificationConf = { + appriseURL: "appriseURL", + }; + let msg = "PassedInMessage"; + try { + await notif.send(notificationConf, msg, null, null); + expect("not reached").toBe(false); + } catch (e) { + expect(e.message).toBe("ERROR FROM APPRISE"); + } + + expect(childProcess.spawnSync).toHaveBeenCalledWith("apprise", ["-vv", "-b", "PassedInMessage", "appriseURL"]); + }); + +}); + +describe("notification to get proper data from Notification.send", () => { + it("should call sendMail with proper data", async () => { + childProcess.spawnSync.mockImplementationOnce(() => { + return { stdout: "response" }; + }); + + let notificationConf = { + type: "apprise", + appriseURL: "appriseURL", + }; + let monitorConf = { + type: "http", + url: "https://www.google.com", + name: "testing", + }; + let heartbeatConf = { + status: UP, + }; + + NotificationSend.Notification.init(); + let res = await NotificationSend.Notification.send(notificationConf, "PassedInMessage", monitorConf, heartbeatConf); + + expect(res).toBe("Sent Successfully."); + + expect(childProcess.spawnSync).toHaveBeenCalledWith("apprise", ["-vv", "-b", "PassedInMessage", "appriseURL"]); + expect(res).toBe("Sent Successfully."); + }); + +}); diff --git a/server/notification-providers/notification-provider.js b/server/notification-providers/notification-provider.js index 61c6242..3ccff04 100644 --- a/server/notification-providers/notification-provider.js +++ b/server/notification-providers/notification-provider.js @@ -6,6 +6,8 @@ class NotificationProvider { */ name = undefined; + sendSuccess = "Sent Successfully."; + /** * @param notification : BeanModel * @param msg : string General Message @@ -25,11 +27,11 @@ class NotificationProvider { if (typeof error.response.data === "string") { msg += error.response.data; } else { - msg += JSON.stringify(error.response.data) + msg += JSON.stringify(error.response.data); } } - throw new Error(msg) + throw new Error(msg); } } diff --git a/server/notification-providers/smtp.js b/server/notification-providers/smtp.js index 60068eb..7f35d1c 100644 --- a/server/notification-providers/smtp.js +++ b/server/notification-providers/smtp.js @@ -92,7 +92,7 @@ class SMTP extends NotificationProvider { }, }); - return "Sent Successfully."; + return this.sendSuccess; } }