Browse Source

slack tests

deefdragon/notif-tests
Jeffrey Koehler 3 years ago
parent
commit
96740bc615
  1. 5
      server/notification-providers/slack.js
  2. 246
      server/notification-providers/slack.spec.js

5
server/notification-providers/slack.js

@ -25,7 +25,6 @@ class Slack extends NotificationProvider {
}
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
try {
if (heartbeatJSON == null) {
let data = {
@ -35,7 +34,7 @@ class Slack extends NotificationProvider {
"icon_emoji": notification.slackiconemo,
};
await axios.post(notification.slackwebhookURL, data);
return okMsg;
return this.sendSuccess;
}
const time = heartbeatJSON["time"];
@ -88,7 +87,7 @@ class Slack extends NotificationProvider {
}
await axios.post(notification.slackwebhookURL, data);
return okMsg;
return this.sendSuccess;
} catch (error) {
this.throwGeneralAxiosError(error);
}

246
server/notification-providers/slack.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 { setting } = require("../util-server");
const Slack = require("./slack");
const axios = require("axios");
const { UP, DOWN } = require("../../src/util");
const NotificationSend = require("../notification");
beforeEach(() => {
// mockNodeMailer.createTransport.mockReset();
setting.mockReset();
axios.post.mockReset();
});
const Slack = require("./slack");
describe("notification default information", () => {
it("should have the correct name", () => {
@ -16,3 +21,232 @@ describe("notification default information", () => {
expect(notification.name).toBe("slack");
});
});
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);
setting.mockResolvedValueOnce("base.com");
let notif = new Slack();
let notificationConf = {
type: "slack",
slackchannel: "chan",
slackusername: "name",
slackiconemo: "😀",
slackwebhookURL: "www.slack.com/webhook"
};
let monitorConf = {
name: "testing monitor",
id: "123",
};
let heartbeatConf = {
time: "test time"
};
let msg = "PassedInMessage😀";
let res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf);
expect(axios.post).toHaveBeenCalledWith("www.slack.com/webhook", {
"blocks": [
{
"text": {
"text": "Uptime Kuma Alert",
"type": "plain_text",
},
"type": "header",
},
{
"fields": [
{
"text": "*Message*\nPassedInMessage😀",
"type": "mrkdwn",
},
{
"text": "*Time (UTC)*\ntest time",
"type": "mrkdwn",
},
],
"type": "section",
},
{
"elements": [
{
"text": {
"text": "Visit Uptime Kuma",
"type": "plain_text",
},
"type": "button",
"url": "base.com/dashboard/123",
"value": "Uptime-Kuma",
},
],
"type": "actions",
},
],
"channel": "chan",
"icon_emoji": "😀",
"text": "Uptime Kuma Alert: testing monitor",
"username": "name",
});
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);
setting.mockResolvedValueOnce("base.com");
let notif = new Slack();
let notificationConf = {
type: "slack",
slackchannel: "chan",
slackusername: "name",
slackiconemo: "😀",
slackwebhookURL: "www.slack.com/webhook"
};
let msg = "PassedInMessage😀";
let res = await notif.send(notificationConf, msg, null, null);
expect(axios.post).toHaveBeenCalledWith("www.slack.com/webhook", {
"channel": "chan",
"icon_emoji": "😀",
"text": "PassedInMessage😀",
"username": "name",
});
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 Slack();
let notificationConf = {
type: "slack",
slackchannel: "chan",
slackusername: "name",
slackiconemo: "😀",
slackwebhookURL: "www.slack.com/webhook"
};
let monitorConf = {
name: "testing monitor",
id: "123",
};
let heartbeatConf = {
time: "test time"
};
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("www.slack.com/webhook", {
"channel": "chan",
"icon_emoji": "😀",
"text": "PassedInMessage😀",
"username": "name",
});
});
});
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);
setting.mockResolvedValueOnce("base.com");
let notificationConf = {
type: "slack",
slackchannel: "chan",
slackusername: "name",
slackiconemo: "😀",
slackwebhookURL: "www.slack.com/webhook"
};
let monitorConf = {
name: "testing monitor",
id: "123",
};
let heartbeatConf = {
time: "test time"
};
let msg = "PassedInMessage😀";
NotificationSend.Notification.init();
let res = await NotificationSend.Notification.send(notificationConf, msg, monitorConf, heartbeatConf);
expect(axios.post).toHaveBeenCalledWith("www.slack.com/webhook", {
"blocks": [
{
"text": {
"text": "Uptime Kuma Alert",
"type": "plain_text",
},
"type": "header",
},
{
"fields": [
{
"text": "*Message*\nPassedInMessage😀",
"type": "mrkdwn",
},
{
"text": "*Time (UTC)*\ntest time",
"type": "mrkdwn",
},
],
"type": "section",
},
{
"elements": [
{
"text": {
"text": "Visit Uptime Kuma",
"type": "plain_text",
},
"type": "button",
"url": "base.com/dashboard/123",
"value": "Uptime-Kuma",
},
],
"type": "actions",
},
],
"channel": "chan",
"icon_emoji": "😀",
"text": "Uptime Kuma Alert: testing monitor",
"username": "name",
});
expect(res).toBe("Sent Successfully.");
});
});

Loading…
Cancel
Save