You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.5 KiB

3 years ago
jest.mock("child_process", () => ({
spawnSync: jest.fn(),
}));
3 years ago
const childProcess = require("child_process");
const { UP } = require("../../src/util");
const NotificationSend = require("../notification");
const Apprise = require("./apprise");
beforeEach(() => {
3 years ago
childProcess.spawnSync.mockReset();
});
describe("notification default information", () => {
it("should have the correct name", () => {
let notification = new Apprise();
expect(notification.name).toBe("apprise");
});
});
3 years ago
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.");
});
});