mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-06-15 08:56:47 +02:00
gotify tests
This commit is contained in:
parent
66f2847937
commit
266333e4f9
2 changed files with 142 additions and 9 deletions
|
@ -6,7 +6,6 @@ class Gotify extends NotificationProvider {
|
||||||
name = "gotify";
|
name = "gotify";
|
||||||
|
|
||||||
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
||||||
let okMsg = "Sent Successfully.";
|
|
||||||
try {
|
try {
|
||||||
if (notification.gotifyserverurl && notification.gotifyserverurl.endsWith("/")) {
|
if (notification.gotifyserverurl && notification.gotifyserverurl.endsWith("/")) {
|
||||||
notification.gotifyserverurl = notification.gotifyserverurl.slice(0, -1);
|
notification.gotifyserverurl = notification.gotifyserverurl.slice(0, -1);
|
||||||
|
@ -15,9 +14,9 @@ class Gotify extends NotificationProvider {
|
||||||
"message": msg,
|
"message": msg,
|
||||||
"priority": notification.gotifyPriority || 8,
|
"priority": notification.gotifyPriority || 8,
|
||||||
"title": "Uptime-Kuma",
|
"title": "Uptime-Kuma",
|
||||||
})
|
});
|
||||||
|
|
||||||
return okMsg;
|
return this.sendSuccess;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.throwGeneralAxiosError(error);
|
this.throwGeneralAxiosError(error);
|
||||||
|
|
|
@ -1,18 +1,152 @@
|
||||||
// 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 Gotify = require("./gotify");
|
const Gotify = require("./gotify");
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// mockNodeMailer.createTransport.mockReset();
|
axios.post.mockReset();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("notification default information", () => {
|
describe("notification default information", () => {
|
||||||
it("should have the correct name", () => {
|
it("should have the correct name", () => {
|
||||||
let notification = new Gotify();
|
let notification = new Gotify();
|
||||||
expect(notification.name).toBe("gotify");
|
expect(notification.name).toBe("gotify");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("notification to act properly on send", () => {
|
||||||
|
it("should call axios with the proper default data", async () => {
|
||||||
|
|
||||||
|
let response = {
|
||||||
|
data: {
|
||||||
|
Message: "OK"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
axios.post.mockResolvedValueOnce(response);
|
||||||
|
|
||||||
|
let notif = new Gotify();
|
||||||
|
let notificationConf = {
|
||||||
|
gotifyserverurl: "url/",
|
||||||
|
gotifyPriority: 4,
|
||||||
|
gotifyapplicationToken: "token"
|
||||||
|
};
|
||||||
|
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("url/message?token=token", {
|
||||||
|
message: "PassedInMessage",
|
||||||
|
priority: 4,
|
||||||
|
title: "Uptime-Kuma",
|
||||||
|
});
|
||||||
|
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 Gotify();
|
||||||
|
let notificationConf = {
|
||||||
|
gotifyserverurl: "url",
|
||||||
|
gotifyPriority: 4,
|
||||||
|
gotifyapplicationToken: "token"
|
||||||
|
};
|
||||||
|
let msg = "PassedInMessage";
|
||||||
|
|
||||||
|
let res = await notif.send(notificationConf, msg, null, null);
|
||||||
|
|
||||||
|
expect(axios.post).toHaveBeenCalledWith("url/message?token=token", {
|
||||||
|
message: "PassedInMessage",
|
||||||
|
priority: 4,
|
||||||
|
title: "Uptime-Kuma",
|
||||||
|
});
|
||||||
|
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 notificationConf = {
|
||||||
|
gotifyserverurl: "url",
|
||||||
|
gotifyPriority: 4,
|
||||||
|
gotifyapplicationToken: "token"
|
||||||
|
};
|
||||||
|
let msg = "PassedInMessage";
|
||||||
|
let notif = new Gotify();
|
||||||
|
|
||||||
|
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("url/message?token=token", {
|
||||||
|
message: "PassedInMessage",
|
||||||
|
priority: 4,
|
||||||
|
title: "Uptime-Kuma",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
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: "gotify",
|
||||||
|
gotifyserverurl: "url",
|
||||||
|
gotifyPriority: 4,
|
||||||
|
gotifyapplicationToken: "token"
|
||||||
|
};
|
||||||
|
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("url/message?token=token", {
|
||||||
|
message: "PassedInMessage",
|
||||||
|
priority: 4,
|
||||||
|
title: "Uptime-Kuma",
|
||||||
|
});
|
||||||
|
expect(res).toBe("Sent Successfully.");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue