mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-06-14 00:22:34 +02:00
mattermost tests
This commit is contained in:
parent
e0d211cbb4
commit
ba3fefa7b8
2 changed files with 291 additions and 13 deletions
|
@ -7,7 +7,6 @@ class Mattermost extends NotificationProvider {
|
|||
name = "mattermost";
|
||||
|
||||
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
||||
let okMsg = "Sent Successfully.";
|
||||
try {
|
||||
const mattermostUserName = notification.mattermostusername || "Uptime Kuma";
|
||||
// If heartbeatJSON is null, assume we're testing.
|
||||
|
@ -15,9 +14,9 @@ class Mattermost extends NotificationProvider {
|
|||
let mattermostTestData = {
|
||||
username: mattermostUserName,
|
||||
text: msg,
|
||||
}
|
||||
await axios.post(notification.mattermostWebhookUrl, mattermostTestData)
|
||||
return okMsg;
|
||||
};
|
||||
await axios.post(notification.mattermostWebhookUrl, mattermostTestData);
|
||||
return this.sendSuccess;
|
||||
}
|
||||
|
||||
const mattermostChannel = notification.mattermostchannel;
|
||||
|
@ -36,12 +35,12 @@ class Mattermost extends NotificationProvider {
|
|||
fallback:
|
||||
"Your " +
|
||||
monitorJSON["name"] +
|
||||
" service went down.",
|
||||
" service went down!",
|
||||
color: "#FF0000",
|
||||
title:
|
||||
"❌ " +
|
||||
monitorJSON["name"] +
|
||||
" service went down. ❌",
|
||||
" service went down! ❌",
|
||||
title_link: monitorJSON["url"],
|
||||
fields: [
|
||||
{
|
||||
|
@ -67,7 +66,7 @@ class Mattermost extends NotificationProvider {
|
|||
notification.mattermostWebhookUrl,
|
||||
mattermostdowndata
|
||||
);
|
||||
return okMsg;
|
||||
return this.sendSuccess;
|
||||
} else if (heartbeatJSON["status"] == UP) {
|
||||
let mattermostupdata = {
|
||||
username: mattermostUserName,
|
||||
|
@ -111,7 +110,7 @@ class Mattermost extends NotificationProvider {
|
|||
notification.mattermostWebhookUrl,
|
||||
mattermostupdata
|
||||
);
|
||||
return okMsg;
|
||||
return this.sendSuccess;
|
||||
}
|
||||
} catch (error) {
|
||||
this.throwGeneralAxiosError(error);
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
// jest.mock("nodemailer", () => ({
|
||||
// createTransport: jest.fn(),
|
||||
// }));
|
||||
jest.mock("axios", () => ({
|
||||
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");
|
||||
|
||||
beforeEach(() => {
|
||||
// mockNodeMailer.createTransport.mockReset();
|
||||
axios.post.mockReset();
|
||||
});
|
||||
|
||||
describe("notification default information", () => {
|
||||
|
@ -16,3 +18,280 @@ describe("notification default information", () => {
|
|||
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…
Add table
Reference in a new issue