mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-06-15 08:56:47 +02:00
Feishu tests
This commit is contained in:
parent
9a2d47679f
commit
66f2847937
2 changed files with 164 additions and 9 deletions
|
@ -6,7 +6,6 @@ class Feishu extends NotificationProvider {
|
||||||
name = "Feishu";
|
name = "Feishu";
|
||||||
|
|
||||||
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
||||||
let okMsg = "Sent Successfully.";
|
|
||||||
let feishuWebHookUrl = notification.feishuWebHookUrl;
|
let feishuWebHookUrl = notification.feishuWebHookUrl;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -18,7 +17,7 @@ class Feishu extends NotificationProvider {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
await axios.post(feishuWebHookUrl, testdata);
|
await axios.post(feishuWebHookUrl, testdata);
|
||||||
return okMsg;
|
return this.sendSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (heartbeatJSON["status"] == DOWN) {
|
if (heartbeatJSON["status"] == DOWN) {
|
||||||
|
@ -45,7 +44,7 @@ class Feishu extends NotificationProvider {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
await axios.post(feishuWebHookUrl, downdata);
|
await axios.post(feishuWebHookUrl, downdata);
|
||||||
return okMsg;
|
return this.sendSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (heartbeatJSON["status"] == UP) {
|
if (heartbeatJSON["status"] == UP) {
|
||||||
|
@ -72,7 +71,7 @@ class Feishu extends NotificationProvider {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
await axios.post(feishuWebHookUrl, updata);
|
await axios.post(feishuWebHookUrl, updata);
|
||||||
return okMsg;
|
return this.sendSuccess;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.throwGeneralAxiosError(error);
|
this.throwGeneralAxiosError(error);
|
||||||
|
|
|
@ -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 Feishu = require("./feishu");
|
const Feishu = require("./feishu");
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// mockNodeMailer.createTransport.mockReset();
|
axios.post.mockReset();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("notification default information", () => {
|
describe("notification default information", () => {
|
||||||
|
@ -16,3 +18,157 @@ describe("notification default information", () => {
|
||||||
expect(notification.name).toBe("Feishu");
|
expect(notification.name).toBe("Feishu");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
let notif = new Feishu();
|
||||||
|
let notificationConf = {
|
||||||
|
feishuWebHookUrl: "feishuWebHookUrl"
|
||||||
|
};
|
||||||
|
let msg = "PassedInMessage";
|
||||||
|
let monitorConf = {
|
||||||
|
type: "http",
|
||||||
|
url: "https://www.google.com",
|
||||||
|
name: "testing",
|
||||||
|
};
|
||||||
|
let heartbeatConf = {
|
||||||
|
status: UP,
|
||||||
|
msg: "some message",
|
||||||
|
time: "example time",
|
||||||
|
};
|
||||||
|
let res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf);
|
||||||
|
|
||||||
|
expect(axios.post).toHaveBeenCalledWith("feishuWebHookUrl", {
|
||||||
|
content: {
|
||||||
|
post: {
|
||||||
|
zh_cn: {
|
||||||
|
content: [
|
||||||
|
[
|
||||||
|
{
|
||||||
|
tag: "text",
|
||||||
|
text: "[Up] some message\nTime (UTC): example time",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
],
|
||||||
|
title: "UptimeKuma Alert: testing",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg_type: "post",
|
||||||
|
});
|
||||||
|
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 Feishu();
|
||||||
|
let notificationConf = {
|
||||||
|
feishuWebHookUrl: "feishuWebHookUrl"
|
||||||
|
};
|
||||||
|
let msg = "PassedInMessage";
|
||||||
|
|
||||||
|
let res = await notif.send(notificationConf, msg, null, null);
|
||||||
|
|
||||||
|
expect(axios.post).toHaveBeenCalledWith("feishuWebHookUrl", {
|
||||||
|
"content": {
|
||||||
|
"text": "PassedInMessage"
|
||||||
|
},
|
||||||
|
"msg_type": "text"
|
||||||
|
});
|
||||||
|
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 notificationConf = {
|
||||||
|
feishuWebHookUrl: "feishuWebHookUrl"
|
||||||
|
|
||||||
|
};
|
||||||
|
let msg = "PassedInMessage";
|
||||||
|
let notif = new Feishu();
|
||||||
|
|
||||||
|
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("feishuWebHookUrl", {
|
||||||
|
content: {
|
||||||
|
text: "PassedInMessage",
|
||||||
|
},
|
||||||
|
msg_type: "text",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
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: "Feishu",
|
||||||
|
feishuWebHookUrl: "feishuWebHookUrl"
|
||||||
|
};
|
||||||
|
let monitorConf = {
|
||||||
|
type: "http",
|
||||||
|
url: "https://www.google.com",
|
||||||
|
name: "testing",
|
||||||
|
};
|
||||||
|
let heartbeatConf = {
|
||||||
|
status: UP,
|
||||||
|
msg: "some message",
|
||||||
|
time: "example time",
|
||||||
|
};
|
||||||
|
|
||||||
|
NotificationSend.Notification.init();
|
||||||
|
let res = await NotificationSend.Notification.send(notificationConf, "PassedInMessage", monitorConf, heartbeatConf);
|
||||||
|
expect(axios.post).toHaveBeenCalledWith("feishuWebHookUrl", {
|
||||||
|
content: {
|
||||||
|
post: {
|
||||||
|
zh_cn: {
|
||||||
|
content: [
|
||||||
|
[
|
||||||
|
{
|
||||||
|
tag: "text",
|
||||||
|
text: "[Up] some message\nTime (UTC): example time",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
],
|
||||||
|
title: "UptimeKuma Alert: testing",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg_type: "post",
|
||||||
|
});
|
||||||
|
expect(res).toBe("Sent Successfully.");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue