mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-06-14 16:42:35 +02:00
apprise tests
This commit is contained in:
parent
bc4f0f94fa
commit
833feaf073
4 changed files with 108 additions and 12 deletions
|
@ -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";
|
||||
}
|
||||
|
|
|
@ -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.");
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -92,7 +92,7 @@ class SMTP extends NotificationProvider {
|
|||
},
|
||||
});
|
||||
|
||||
return "Sent Successfully.";
|
||||
return this.sendSuccess;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue