mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-06-13 08:02:35 +02:00
rocket chat tests
This commit is contained in:
parent
60ad28592d
commit
8b0116b9f2
2 changed files with 211 additions and 9 deletions
|
@ -9,7 +9,6 @@ class RocketChat extends NotificationProvider {
|
||||||
name = "rocket.chat";
|
name = "rocket.chat";
|
||||||
|
|
||||||
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
||||||
let okMsg = "Sent Successfully.";
|
|
||||||
try {
|
try {
|
||||||
if (heartbeatJSON == null) {
|
if (heartbeatJSON == null) {
|
||||||
let data = {
|
let data = {
|
||||||
|
@ -19,7 +18,7 @@ class RocketChat extends NotificationProvider {
|
||||||
"icon_emoji": notification.rocketiconemo,
|
"icon_emoji": notification.rocketiconemo,
|
||||||
};
|
};
|
||||||
await axios.post(notification.rocketwebhookURL, data);
|
await axios.post(notification.rocketwebhookURL, data);
|
||||||
return okMsg;
|
return this.sendSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
const time = heartbeatJSON["time"];
|
const time = heartbeatJSON["time"];
|
||||||
|
@ -55,7 +54,7 @@ class RocketChat extends NotificationProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
await axios.post(notification.rocketwebhookURL, data);
|
await axios.post(notification.rocketwebhookURL, data);
|
||||||
return okMsg;
|
return this.sendSuccess;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.throwGeneralAxiosError(error);
|
this.throwGeneralAxiosError(error);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,19 @@
|
||||||
// jest.mock("nodemailer", () => ({
|
jest.mock("axios", () => ({
|
||||||
// createTransport: jest.fn(),
|
post: jest.fn(),
|
||||||
// }));
|
}));
|
||||||
|
|
||||||
// const mockNodeMailer = require("nodemailer");
|
jest.mock("../util-server");
|
||||||
|
|
||||||
const RocketChat = require("./rocket-chat");
|
const axios = require("axios");
|
||||||
|
const { setting } = require("../util-server");
|
||||||
|
const { UP, DOWN } = require("../../src/util");
|
||||||
|
const NotificationSend = require("../notification");
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// mockNodeMailer.createTransport.mockReset();
|
axios.post.mockReset();
|
||||||
|
setting.mockReset();
|
||||||
});
|
});
|
||||||
|
const RocketChat = require("./rocket-chat");
|
||||||
|
|
||||||
describe("notification default information", () => {
|
describe("notification default information", () => {
|
||||||
it("should have the correct name", () => {
|
it("should have the correct name", () => {
|
||||||
|
@ -16,3 +21,201 @@ describe("notification default information", () => {
|
||||||
expect(notification.name).toBe("rocket.chat");
|
expect(notification.name).toBe("rocket.chat");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
setting.mockResolvedValueOnce("base.com");
|
||||||
|
let notif = new RocketChat();
|
||||||
|
let notificationConf = {
|
||||||
|
rocketchannel: "channel",
|
||||||
|
rocketusername: "user",
|
||||||
|
rocketiconemo: "😀",
|
||||||
|
rocketwebhookURL: "example.com",
|
||||||
|
};
|
||||||
|
let monitorConf = {
|
||||||
|
id: "123"
|
||||||
|
};
|
||||||
|
let heartbeatConf = {
|
||||||
|
status: UP,
|
||||||
|
time: "some time"
|
||||||
|
};
|
||||||
|
let msg = "PassedInMessage😀";
|
||||||
|
let res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf);
|
||||||
|
|
||||||
|
expect(axios.post).toHaveBeenCalledWith("example.com", {
|
||||||
|
"attachments": [
|
||||||
|
{
|
||||||
|
"color": "#32cd32",
|
||||||
|
"text": "*Message*\nPassedInMessage😀",
|
||||||
|
"title": "Uptime Kuma Alert *Time (UTC)*\nsome time",
|
||||||
|
"title_link": "base.com/dashboard/123",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"channel": "channel",
|
||||||
|
"icon_emoji": "😀",
|
||||||
|
"text": "Uptime Kuma Alert",
|
||||||
|
"username": "user",
|
||||||
|
});
|
||||||
|
expect(res).toBe("Sent Successfully.");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should call axios with the proper default data when DOWN", async () => {
|
||||||
|
|
||||||
|
let response = {
|
||||||
|
data: {
|
||||||
|
Message: "OK"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
setting.mockResolvedValueOnce("base.com");
|
||||||
|
axios.post.mockResolvedValueOnce(response);
|
||||||
|
|
||||||
|
let notif = new RocketChat();
|
||||||
|
let notificationConf = {
|
||||||
|
rocketchannel: "channel",
|
||||||
|
rocketusername: "user",
|
||||||
|
rocketiconemo: "😀",
|
||||||
|
rocketwebhookURL: "example.com",
|
||||||
|
};
|
||||||
|
let monitorConf = {
|
||||||
|
id: "123"
|
||||||
|
};
|
||||||
|
let heartbeatConf = {
|
||||||
|
status: DOWN,
|
||||||
|
time: "some time"
|
||||||
|
};
|
||||||
|
let msg = "PassedInMessage😀";
|
||||||
|
let res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf);
|
||||||
|
|
||||||
|
expect(axios.post).toHaveBeenCalledWith("example.com", {
|
||||||
|
"attachments": [
|
||||||
|
{
|
||||||
|
"color": "#ff0000",
|
||||||
|
"text": "*Message*\nPassedInMessage😀",
|
||||||
|
"title": "Uptime Kuma Alert *Time (UTC)*\nsome time",
|
||||||
|
"title_link": "base.com/dashboard/123",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"channel": "channel",
|
||||||
|
"icon_emoji": "😀",
|
||||||
|
"text": "Uptime Kuma Alert",
|
||||||
|
"username": "user",
|
||||||
|
});
|
||||||
|
expect(res).toBe("Sent Successfully.");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should call axios with the proper data when monitor nil", async () => {
|
||||||
|
let response = {
|
||||||
|
data: {
|
||||||
|
Message: "OK"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
setting.mockResolvedValueOnce("base.com");
|
||||||
|
axios.post.mockResolvedValueOnce(response);
|
||||||
|
|
||||||
|
let notif = new RocketChat();
|
||||||
|
let notificationConf = {
|
||||||
|
rocketchannel: "channel",
|
||||||
|
rocketusername: "user",
|
||||||
|
rocketiconemo: "😀",
|
||||||
|
rocketwebhookURL: "example.com",
|
||||||
|
};
|
||||||
|
let msg = "PassedInMessage😀";
|
||||||
|
|
||||||
|
let res = await notif.send(notificationConf, msg, null, null);
|
||||||
|
|
||||||
|
expect(axios.post).toHaveBeenCalledWith("example.com", {
|
||||||
|
"channel": "channel",
|
||||||
|
"icon_emoji": "😀",
|
||||||
|
"text": "PassedInMessage😀",
|
||||||
|
"username": "user",
|
||||||
|
});
|
||||||
|
expect(res).toBe("Sent Successfully.");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("notification to act properly on error", () => {
|
||||||
|
it("should respond with an axios error on error", async () => {
|
||||||
|
|
||||||
|
setting.mockResolvedValueOnce("base.com");
|
||||||
|
axios.post.mockImplementation(() => {
|
||||||
|
throw new Error("Test Error");
|
||||||
|
});
|
||||||
|
let notif = new RocketChat();
|
||||||
|
let notificationConf = {
|
||||||
|
rocketchannel: "channel",
|
||||||
|
rocketusername: "user",
|
||||||
|
rocketiconemo: "😀",
|
||||||
|
rocketwebhookURL: "example.com",
|
||||||
|
};
|
||||||
|
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("example.com", {
|
||||||
|
"channel": "channel",
|
||||||
|
"icon_emoji": "😀",
|
||||||
|
"text": "PassedInMessage😀",
|
||||||
|
"username": "user",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("notification to get proper data from Notification.send", () => {
|
||||||
|
it("should call axios with proper data", async () => {
|
||||||
|
let response = {
|
||||||
|
data: {
|
||||||
|
Message: "OK"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
setting.mockResolvedValueOnce("base.com");
|
||||||
|
axios.post.mockResolvedValueOnce(response);
|
||||||
|
let notificationConf = {
|
||||||
|
type: "rocket.chat",
|
||||||
|
rocketchannel: "channel",
|
||||||
|
rocketusername: "user",
|
||||||
|
rocketiconemo: "😀",
|
||||||
|
rocketwebhookURL: "example.com",
|
||||||
|
};
|
||||||
|
let monitorConf = {
|
||||||
|
id: "123"
|
||||||
|
};
|
||||||
|
let heartbeatConf = {
|
||||||
|
status: UP,
|
||||||
|
time: "some time"
|
||||||
|
};
|
||||||
|
let msg = "PassedInMessage😀";
|
||||||
|
|
||||||
|
NotificationSend.Notification.init();
|
||||||
|
let res = await NotificationSend.Notification.send(notificationConf, msg, monitorConf, heartbeatConf);
|
||||||
|
expect(axios.post).toHaveBeenCalledWith("example.com", {
|
||||||
|
"attachments": [
|
||||||
|
{
|
||||||
|
"color": "#32cd32",
|
||||||
|
"text": "*Message*\nPassedInMessage😀",
|
||||||
|
"title": "Uptime Kuma Alert *Time (UTC)*\nsome time",
|
||||||
|
"title_link": "base.com/dashboard/123",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"channel": "channel",
|
||||||
|
"icon_emoji": "😀",
|
||||||
|
"text": "Uptime Kuma Alert",
|
||||||
|
"username": "user",
|
||||||
|
});
|
||||||
|
expect(res).toBe("Sent Successfully.");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue