Browse Source

lunasea tests

deefdragon/notif-tests
Jeffrey Koehler 3 years ago
parent
commit
4f12e942a7
  1. 23
      server/notification-providers/lunasea.js
  2. 168
      server/notification-providers/lunasea.spec.js

23
server/notification-providers/lunasea.js

@ -7,39 +7,38 @@ class LunaSea extends NotificationProvider {
name = "lunasea"; name = "lunasea";
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully."; let lunaseadevice = "https://notify.lunasea.app/v1/custom/device/" + notification.lunaseaDevice;
let lunaseadevice = "https://notify.lunasea.app/v1/custom/device/" + notification.lunaseaDevice
try { try {
if (heartbeatJSON == null) { if (heartbeatJSON == null) {
let testdata = { let testdata = {
"title": "Uptime Kuma Alert", "title": "Uptime Kuma Alert",
"body": "Testing Successful.", "body": "Testing Successful.",
} };
await axios.post(lunaseadevice, testdata) await axios.post(lunaseadevice, testdata);
return okMsg; return this.sendSuccess;
} }
if (heartbeatJSON["status"] == DOWN) { if (heartbeatJSON["status"] == DOWN) {
let downdata = { let downdata = {
"title": "UptimeKuma Alert: " + monitorJSON["name"], "title": "UptimeKuma Alert: " + monitorJSON["name"],
"body": "[🔴 Down] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"], "body": "[🔴 Down] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"],
} };
await axios.post(lunaseadevice, downdata) await axios.post(lunaseadevice, downdata);
return okMsg; return this.sendSuccess;
} }
if (heartbeatJSON["status"] == UP) { if (heartbeatJSON["status"] == UP) {
let updata = { let updata = {
"title": "UptimeKuma Alert: " + monitorJSON["name"], "title": "UptimeKuma Alert: " + monitorJSON["name"],
"body": "[✅ Up] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"], "body": "[✅ Up] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"],
} };
await axios.post(lunaseadevice, updata) await axios.post(lunaseadevice, updata);
return okMsg; return this.sendSuccess;
} }
} catch (error) { } catch (error) {
this.throwGeneralAxiosError(error) this.throwGeneralAxiosError(error);
} }
} }

168
server/notification-providers/lunasea.spec.js

@ -1,13 +1,15 @@
// jest.mock("nodemailer", () => ({ jest.mock("axios", () => ({
// createTransport: jest.fn(), post: jest.fn(),
// })); }));
// const mockNodeMailer = require("nodemailer"); const axios = require("axios");
const { UP, DOWN } = require("../../src/util");
const NotificationSend = require("../notification");
const LunaSea = require("./lunasea"); const LunaSea = require("./lunasea");
beforeEach(() => { beforeEach(() => {
// mockNodeMailer.createTransport.mockReset(); axios.post.mockReset();
}); });
describe("notification default information", () => { describe("notification default information", () => {
@ -16,3 +18,159 @@ describe("notification default information", () => {
expect(notification.name).toBe("lunasea"); expect(notification.name).toBe("lunasea");
}); });
}); });
describe("notification to act properly on send", () => {
it("should call axios with the proper default data when UP", async () => {
let response = {
data: {
Message: "OK"
}
};
axios.post.mockResolvedValueOnce(response);
let notif = new LunaSea();
let notificationConf = {
type: "lunasea",
lunaseaDevice: "1234",
};
let msg = "PassedInMessage";
let monitorConf = {
type: "http",
url: "https://www.google.com",
name: "testing",
};
let heartbeatConf = {
status: UP,
msg: "some message",
time: "example time",
};
let res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf);
expect(axios.post).toHaveBeenCalledWith("https://notify.lunasea.app/v1/custom/device/1234", {
"body": "[✅ Up] some message\nTime (UTC): example time",
"title": "UptimeKuma Alert: testing",
});
expect(res).toBe("Sent Successfully.");
});
it("should call axios with the proper default data when DOWN", async () => {
let response = {
data: {
Message: "OK"
}
};
axios.post.mockResolvedValueOnce(response);
let notif = new LunaSea();
let notificationConf = {
type: "lunasea",
lunaseaDevice: "1234",
};
let msg = "PassedInMessage";
let monitorConf = {
type: "http",
url: "https://www.google.com",
name: "testing",
};
let heartbeatConf = {
status: DOWN,
msg: "some message",
time: "example time",
};
let res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf);
expect(axios.post).toHaveBeenCalledWith("https://notify.lunasea.app/v1/custom/device/1234", {
"body": "[🔴 Down] some message\nTime (UTC): example time",
"title": "UptimeKuma Alert: testing",
});
expect(res).toBe("Sent Successfully.");
});
it("should call axios with the proper data when monitor nil", async () => {
let response = {
data: {
Message: "OK"
}
};
axios.post.mockResolvedValueOnce(response);
let notif = new LunaSea();
let notificationConf = {
type: "lunasea",
lunaseaDevice: "1234",
};
let msg = "PassedInMessage";
let res = await notif.send(notificationConf, msg, null, null);
expect(axios.post).toHaveBeenCalledWith("https://notify.lunasea.app/v1/custom/device/1234", {
"body": "Testing Successful.",
"title": "Uptime Kuma Alert",
});
expect(res).toBe("Sent Successfully.");
});
});
describe("notification to act properly on error", () => {
it("should respond with an axios error on error", async () => {
axios.post.mockImplementation(() => {
throw new Error("Test Error");
});
let notif = new LunaSea();
let notificationConf = {
type: "lunasea",
lunaseaDevice: "1234",
};
let msg = "PassedInMessage";
try {
await notif.send(notificationConf, msg, null, null);
expect("Error thrown").toBe(false);
} catch (e) {
expect(e.message).toBe("Error: Error: Test Error ");
}
expect(axios.post).toHaveBeenCalledWith("https://notify.lunasea.app/v1/custom/device/1234", {
"body": "Testing Successful.",
"title": "Uptime Kuma Alert" });
});
});
describe("notification to get proper data from Notification.send", () => {
it("should call axios with proper data", async () => {
let response = {
data: {
Message: "OK"
}
};
axios.post.mockResolvedValueOnce(response);
let notificationConf = {
type: "lunasea",
lunaseaDevice: "1234",
};
let monitorConf = {
type: "http",
url: "https://www.google.com",
name: "testing",
};
let heartbeatConf = {
status: UP,
msg: "some message",
time: "example time",
};
NotificationSend.Notification.init();
let res = await NotificationSend.Notification.send(notificationConf, "PassedInMessage", monitorConf, heartbeatConf);
expect(axios.post).toHaveBeenCalledWith("https://notify.lunasea.app/v1/custom/device/1234", {
"body": "[✅ Up] some message\nTime (UTC): example time",
"title": "UptimeKuma Alert: testing",
});
expect(res).toBe("Sent Successfully.");
});
});

Loading…
Cancel
Save