Browse Source

rocket chat tests

deefdragon/notif-tests
Jeffrey Koehler 3 years ago
parent
commit
8b0116b9f2
  1. 5
      server/notification-providers/rocket-chat.js
  2. 215
      server/notification-providers/rocket-chat.spec.js

5
server/notification-providers/rocket-chat.js

@ -9,7 +9,6 @@ class RocketChat extends NotificationProvider {
name = "rocket.chat";
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
try {
if (heartbeatJSON == null) {
let data = {
@ -19,7 +18,7 @@ class RocketChat extends NotificationProvider {
"icon_emoji": notification.rocketiconemo,
};
await axios.post(notification.rocketwebhookURL, data);
return okMsg;
return this.sendSuccess;
}
const time = heartbeatJSON["time"];
@ -55,7 +54,7 @@ class RocketChat extends NotificationProvider {
}
await axios.post(notification.rocketwebhookURL, data);
return okMsg;
return this.sendSuccess;
} catch (error) {
this.throwGeneralAxiosError(error);
}

215
server/notification-providers/rocket-chat.spec.js

@ -1,14 +1,19 @@
// jest.mock("nodemailer", () => ({
// createTransport: jest.fn(),
// }));
jest.mock("axios", () => ({
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(() => {
// mockNodeMailer.createTransport.mockReset();
axios.post.mockReset();
setting.mockReset();
});
const RocketChat = require("./rocket-chat");
describe("notification default information", () => {
it("should have the correct name", () => {
@ -16,3 +21,201 @@ describe("notification default information", () => {
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…
Cancel
Save