Browse Source

mattermost tests

deefdragon/notif-tests
Jeffrey Koehler 4 years ago
parent
commit
ba3fefa7b8
  1. 15
      server/notification-providers/mattermost.js
  2. 289
      server/notification-providers/mattermost.spec.js

15
server/notification-providers/mattermost.js

@ -7,7 +7,6 @@ class Mattermost extends NotificationProvider {
name = "mattermost"; name = "mattermost";
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully.";
try { try {
const mattermostUserName = notification.mattermostusername || "Uptime Kuma"; const mattermostUserName = notification.mattermostusername || "Uptime Kuma";
// If heartbeatJSON is null, assume we're testing. // If heartbeatJSON is null, assume we're testing.
@ -15,9 +14,9 @@ class Mattermost extends NotificationProvider {
let mattermostTestData = { let mattermostTestData = {
username: mattermostUserName, username: mattermostUserName,
text: msg, text: msg,
} };
await axios.post(notification.mattermostWebhookUrl, mattermostTestData) await axios.post(notification.mattermostWebhookUrl, mattermostTestData);
return okMsg; return this.sendSuccess;
} }
const mattermostChannel = notification.mattermostchannel; const mattermostChannel = notification.mattermostchannel;
@ -36,12 +35,12 @@ class Mattermost extends NotificationProvider {
fallback: fallback:
"Your " + "Your " +
monitorJSON["name"] + monitorJSON["name"] +
" service went down.", " service went down!",
color: "#FF0000", color: "#FF0000",
title: title:
"❌ " + "❌ " +
monitorJSON["name"] + monitorJSON["name"] +
" service went down. ❌", " service went down! ❌",
title_link: monitorJSON["url"], title_link: monitorJSON["url"],
fields: [ fields: [
{ {
@ -67,7 +66,7 @@ class Mattermost extends NotificationProvider {
notification.mattermostWebhookUrl, notification.mattermostWebhookUrl,
mattermostdowndata mattermostdowndata
); );
return okMsg; return this.sendSuccess;
} else if (heartbeatJSON["status"] == UP) { } else if (heartbeatJSON["status"] == UP) {
let mattermostupdata = { let mattermostupdata = {
username: mattermostUserName, username: mattermostUserName,
@ -111,7 +110,7 @@ class Mattermost extends NotificationProvider {
notification.mattermostWebhookUrl, notification.mattermostWebhookUrl,
mattermostupdata mattermostupdata
); );
return okMsg; return this.sendSuccess;
} }
} catch (error) { } catch (error) {
this.throwGeneralAxiosError(error); this.throwGeneralAxiosError(error);

289
server/notification-providers/mattermost.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 Mattermost = require("./mattermost"); const Mattermost = require("./mattermost");
beforeEach(() => { beforeEach(() => {
// mockNodeMailer.createTransport.mockReset(); axios.post.mockReset();
}); });
describe("notification default information", () => { describe("notification default information", () => {
@ -16,3 +18,280 @@ describe("notification default information", () => {
expect(notification.name).toBe("mattermost"); expect(notification.name).toBe("mattermost");
}); });
}); });
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 Mattermost();
let notificationConf = {
type: "mattermost",
mattermostchannel: "1234",
mattermosticonemo: "😀",
mattermosticonurl: "www.testing.com",
mattermostWebhookUrl: "www.example.com/webhook",
mattermostusername: "username",
};
let monitorConf = {
type: "http",
url: "https://www.google.com",
name: "testing",
};
let heartbeatConf = {
status: UP,
msg: "some message",
ping: "123",
time: "example time",
};
let msg = "PassedInMessage";
let res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf);
expect(axios.post).toHaveBeenCalledWith("www.example.com/webhook", {
"attachments": [
{
"color": "#32CD32",
"fallback": "Your testing service went up!",
"fields": [
{
"short": true,
"title": "Service Name",
"value": "testing",
},
{
"short": true,
"title": "Time (UTC)",
"value": "example time",
},
{
"short": false,
"title": "Ping",
"value": "123ms",
},
],
"title": "✅ testing service went up! ✅",
"title_link": "https://www.google.com",
},
],
"channel": "1234",
"icon_emoji": "😀",
"icon_url": "www.testing.com",
"text": "Uptime Kuma Alert",
"username": "username",
});
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 Mattermost();
let notificationConf = {
type: "mattermost",
mattermostchannel: "1234",
mattermosticonemo: "😀",
mattermosticonurl: "www.testing.com",
mattermostWebhookUrl: "www.example.com/webhook",
mattermostusername: "username",
};
let monitorConf = {
type: "http",
url: "https://www.google.com",
name: "testing",
};
let heartbeatConf = {
status: DOWN,
msg: "some message",
ping: "123",
time: "example time",
};
let msg = "PassedInMessage";
let res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf);
expect(axios.post).toHaveBeenCalledWith("www.example.com/webhook", {
"attachments": [
{
"color": "#FF0000",
"fallback": "Your testing service went down!",
"fields": [
{
"short": true,
"title": "Service Name",
"value": "testing",
},
{
"short": true,
"title": "Time (UTC)",
"value": "example time",
},
{
"short": false,
"title": "Error",
"value": "some message",
},
],
"title": "❌ testing service went down! ❌",
"title_link": "https://www.google.com",
},
],
"channel": "1234",
"icon_emoji": "😀",
"icon_url": "www.testing.com",
"text": "Uptime Kuma Alert",
"username": "username",
});
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 Mattermost();
let notificationConf = {
type: "mattermost",
mattermostchannel: "1234",
mattermosticonemo: "😀",
mattermosticonurl: "www.testing.com",
mattermostWebhookUrl: "www.example.com/webhook",
mattermostusername: "username",
};
let msg = "PassedInMessage";
let res = await notif.send(notificationConf, msg, null, null);
expect(axios.post).toHaveBeenCalledWith("www.example.com/webhook", {
"text": "PassedInMessage",
"username": "username"
});
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 Mattermost();
let notificationConf = {
type: "mattermost",
mattermostchannel: "1234",
mattermosticonemo: "😀",
mattermosticonurl: "www.testing.com",
mattermostWebhookUrl: "www.example.com/webhook",
mattermostusername: "username",
};
let monitorConf = {
type: "http",
url: "https://www.google.com",
name: "testing",
};
let heartbeatConf = {
status: UP,
msg: "some message",
ping: "123",
time: "example 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.example.com/webhook", {
"text": "PassedInMessage",
"username": "username"
});
});
});
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: "mattermost",
mattermostchannel: "1234",
mattermosticonemo: "😀",
mattermosticonurl: "www.testing.com",
mattermostWebhookUrl: "www.example.com/webhook",
mattermostusername: "username",
};
let monitorConf = {
type: "http",
url: "https://www.google.com",
name: "testing",
};
let heartbeatConf = {
status: UP,
msg: "some message",
ping: "123",
time: "example time",
};
NotificationSend.Notification.init();
let res = await NotificationSend.Notification.send(notificationConf, "PassedInMessage", monitorConf, heartbeatConf);
expect(axios.post).toHaveBeenCalledWith("www.example.com/webhook", {
"attachments": [
{
"color": "#32CD32",
"fallback": "Your testing service went up!",
"fields": [
{
"short": true,
"title": "Service Name",
"value": "testing",
},
{
"short": true,
"title": "Time (UTC)",
"value": "example time",
},
{
"short": false,
"title": "Ping",
"value": "123ms",
},
],
"title": "✅ testing service went up! ✅",
"title_link": "https://www.google.com",
},
],
"channel": "1234",
"icon_emoji": "😀",
"icon_url": "www.testing.com",
"text": "Uptime Kuma Alert",
"username": "username",
});
expect(res).toBe("Sent Successfully.");
});
});

Loading…
Cancel
Save