mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-06-14 16:42:35 +02:00
dingding tests and minor fix
This commit is contained in:
parent
833feaf073
commit
965ce6213b
2 changed files with 204 additions and 7 deletions
|
@ -7,7 +7,6 @@ class DingDing extends NotificationProvider {
|
|||
name = "DingDing";
|
||||
|
||||
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
||||
let okMsg = "Sent Successfully.";
|
||||
|
||||
try {
|
||||
if (heartbeatJSON != null) {
|
||||
|
@ -18,8 +17,8 @@ class DingDing extends NotificationProvider {
|
|||
text: `## [${this.statusToString(heartbeatJSON["status"])}] \n > ${heartbeatJSON["msg"]} \n > Time(UTC):${heartbeatJSON["time"]}`,
|
||||
}
|
||||
};
|
||||
if (this.sendToDingDing(notification, params)) {
|
||||
return okMsg;
|
||||
if (await this.sendToDingDing(notification, params)) {
|
||||
return this.sendSuccess;
|
||||
}
|
||||
} else {
|
||||
let params = {
|
||||
|
@ -28,8 +27,8 @@ class DingDing extends NotificationProvider {
|
|||
content: msg
|
||||
}
|
||||
};
|
||||
if (this.sendToDingDing(notification, params)) {
|
||||
return okMsg;
|
||||
if (await this.sendToDingDing(notification, params)) {
|
||||
return this.sendSuccess;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
|
|
|
@ -1,13 +1,22 @@
|
|||
// jest.mock("nodemailer", () => ({
|
||||
// jest.mock("crypto", () => ({
|
||||
// createTransport: jest.fn(),
|
||||
// createHmac: jest.fn(),
|
||||
// digest: jest.fn(),
|
||||
// }));
|
||||
|
||||
jest.mock("axios");
|
||||
|
||||
const { UP } = require("../../src/util");
|
||||
const NotificationSend = require("../notification");
|
||||
|
||||
// const mockNodeMailer = require("nodemailer");
|
||||
|
||||
const DingDing = require("./dingding");
|
||||
|
||||
const axios = require("axios");
|
||||
|
||||
beforeEach(() => {
|
||||
// mockNodeMailer.createTransport.mockReset();
|
||||
axios.mockReset();
|
||||
});
|
||||
|
||||
describe("notification default information", () => {
|
||||
|
@ -16,3 +25,192 @@ describe("notification default information", () => {
|
|||
expect(notification.name).toBe("DingDing");
|
||||
});
|
||||
});
|
||||
|
||||
describe("notification to act properly on send", () => {
|
||||
it("should call axios with the proper default data", async () => {
|
||||
|
||||
jest.spyOn(global.Date, "now")
|
||||
.mockImplementationOnce(() =>
|
||||
new Date("2019-05-14T11:01:58.135Z").valueOf()
|
||||
);
|
||||
|
||||
let response = {
|
||||
data: {
|
||||
errmsg: "ok"
|
||||
}
|
||||
};
|
||||
axios.mockResolvedValueOnce(response);
|
||||
|
||||
let notif = new DingDing();
|
||||
let notificationConf = {
|
||||
appriseURL: "appriseURL",
|
||||
secretKey: "abc",
|
||||
webHookUrl: "https://example.com/webhook",
|
||||
};
|
||||
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).toHaveBeenCalledWith({
|
||||
data: "{\"msgtype\":\"markdown\",\"markdown\":{\"title\":\"testing\",\"text\":\"## [UP] \\n > some message \\n > Time(UTC):example time\"}}",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
method: "POST",
|
||||
url: "https://example.com/webhook×tamp=1557831718135&sign=lCTIn3sYpAYFAw3B2LeTLr7BvcOMAcmZu%2F6rb7kC8Io%3D",
|
||||
});
|
||||
expect(res).toBe("Sent Successfully.");
|
||||
});
|
||||
|
||||
it("should call axios with the proper data when missing heartbeat", async () => {
|
||||
|
||||
jest.spyOn(global.Date, "now")
|
||||
.mockImplementationOnce(() =>
|
||||
new Date("2019-05-14T11:01:58.135Z").valueOf()
|
||||
);
|
||||
|
||||
let response = {
|
||||
data: {
|
||||
errmsg: "ok"
|
||||
}
|
||||
};
|
||||
axios.mockResolvedValueOnce(response);
|
||||
|
||||
let notif = new DingDing();
|
||||
let notificationConf = {
|
||||
appriseURL: "appriseURL",
|
||||
secretKey: "abc",
|
||||
webHookUrl: "https://example.com/webhook",
|
||||
};
|
||||
let msg = "PassedInMessage";
|
||||
let monitorConf = {
|
||||
type: "http",
|
||||
url: "https://www.google.com",
|
||||
name: "testing",
|
||||
};
|
||||
let res = await notif.send(notificationConf, msg, monitorConf, null);
|
||||
|
||||
expect(axios).toHaveBeenCalledWith({
|
||||
data: "{\"msgtype\":\"text\",\"text\":{\"content\":\"PassedInMessage\"}}",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
method: "POST",
|
||||
url: "https://example.com/webhook×tamp=1557831718135&sign=lCTIn3sYpAYFAw3B2LeTLr7BvcOMAcmZu%2F6rb7kC8Io%3D",
|
||||
});
|
||||
expect(res).toBe("Sent Successfully.");
|
||||
});
|
||||
|
||||
//TODO need to get correct response when sendToDingDing fails, but no axios error.
|
||||
|
||||
});
|
||||
|
||||
describe("notification to act properly on error", () => {
|
||||
it("should respond with an axios error on error", async () => {
|
||||
|
||||
jest.spyOn(global.Date, "now")
|
||||
.mockImplementationOnce(() =>
|
||||
new Date("2019-05-14T11:01:58.135Z").valueOf()
|
||||
);
|
||||
|
||||
axios.mockImplementationOnce(() => {
|
||||
throw new Error("Test Error");
|
||||
});
|
||||
|
||||
let notif = new DingDing();
|
||||
let notificationConf = {
|
||||
appriseURL: "appriseURL",
|
||||
secretKey: "abc",
|
||||
webHookUrl: "https://example.com/webhook",
|
||||
};
|
||||
let msg = "PassedInMessage";
|
||||
let monitorConf = {
|
||||
type: "http",
|
||||
url: "https://www.google.com",
|
||||
name: "testing",
|
||||
};
|
||||
let heartbeatConf = {
|
||||
status: UP,
|
||||
msg: "some message",
|
||||
time: "example time",
|
||||
};
|
||||
|
||||
try {
|
||||
await notif.send(notificationConf, msg, monitorConf, heartbeatConf);
|
||||
console.log("fail");
|
||||
expect("Error thrown").toBe(false);
|
||||
} catch (e) {
|
||||
//axios general error on catching another error is not the cleanest, but works.
|
||||
expect(e.message).toBe("Error: Error: Test Error ");
|
||||
}
|
||||
|
||||
expect(axios).toHaveBeenCalledWith({
|
||||
data: "{\"msgtype\":\"markdown\",\"markdown\":{\"title\":\"testing\",\"text\":\"## [UP] \\n > some message \\n > Time(UTC):example time\"}}",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
method: "POST",
|
||||
url: "https://example.com/webhook×tamp=1557831718135&sign=lCTIn3sYpAYFAw3B2LeTLr7BvcOMAcmZu%2F6rb7kC8Io%3D",
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("notification to get proper data from Notification.send", () => {
|
||||
it("should call sendMail with proper data", async () => {
|
||||
jest.spyOn(global.Date, "now")
|
||||
.mockImplementationOnce(() =>
|
||||
new Date("2019-05-14T11:01:58.135Z").valueOf()
|
||||
);
|
||||
|
||||
let response = {
|
||||
data: {
|
||||
errmsg: "ok"
|
||||
}
|
||||
};
|
||||
axios.mockResolvedValueOnce(response);
|
||||
|
||||
let notif = new DingDing();
|
||||
let notificationConf = {
|
||||
type: "DingDing",
|
||||
appriseURL: "appriseURL",
|
||||
secretKey: "abc",
|
||||
webHookUrl: "https://example.com/webhook",
|
||||
};
|
||||
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(res).toBe("Sent Successfully.");
|
||||
|
||||
expect(axios).toHaveBeenCalledWith({
|
||||
data: "{\"msgtype\":\"markdown\",\"markdown\":{\"title\":\"testing\",\"text\":\"## [UP] \\n > some message \\n > Time(UTC):example time\"}}",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
method: "POST",
|
||||
url: "https://example.com/webhook×tamp=1557831718135&sign=lCTIn3sYpAYFAw3B2LeTLr7BvcOMAcmZu%2F6rb7kC8Io%3D",
|
||||
|
||||
});
|
||||
expect(res).toBe("Sent Successfully.");
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue