mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-07-27 02:43:17 +02:00
telegram tests
This commit is contained in:
parent
171069fc7c
commit
9ea0a9c82e
2 changed files with 148 additions and 13 deletions
|
@ -6,7 +6,6 @@ class Telegram extends NotificationProvider {
|
||||||
name = "telegram";
|
name = "telegram";
|
||||||
|
|
||||||
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
||||||
let okMsg = "Sent Successfully.";
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await axios.get(`https://api.telegram.org/bot${notification.telegramBotToken}/sendMessage`, {
|
await axios.get(`https://api.telegram.org/bot${notification.telegramBotToken}/sendMessage`, {
|
||||||
|
@ -14,12 +13,11 @@ class Telegram extends NotificationProvider {
|
||||||
chat_id: notification.telegramChatID,
|
chat_id: notification.telegramChatID,
|
||||||
text: msg,
|
text: msg,
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
return okMsg;
|
return this.sendSuccess;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
let msg = (error.response.data.description) ? error.response.data.description : "Error without description"
|
let msg = (error.response.data.description) ? error.response.data.description : "Error without description";
|
||||||
throw new Error(msg)
|
throw new Error(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
// jest.mock("nodemailer", () => ({
|
jest.mock("axios", () => ({
|
||||||
// createTransport: jest.fn(),
|
get: jest.fn(),
|
||||||
// }));
|
}));
|
||||||
|
|
||||||
// const mockNodeMailer = require("nodemailer");
|
const axios = require("axios");
|
||||||
|
const { UP, DOWN } = require("../../src/util");
|
||||||
const Telegram = require("./telegram");
|
const NotificationSend = require("../notification");
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// mockNodeMailer.createTransport.mockReset();
|
axios.get.mockReset();
|
||||||
});
|
});
|
||||||
|
const Telegram = require("./telegram");
|
||||||
|
|
||||||
describe("notification default information", () => {
|
describe("notification default information", () => {
|
||||||
it("should have the correct name", () => {
|
it("should have the correct name", () => {
|
||||||
|
@ -16,3 +17,139 @@ describe("notification default information", () => {
|
||||||
expect(notification.name).toBe("telegram");
|
expect(notification.name).toBe("telegram");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("notification to act properly on send", () => {
|
||||||
|
it("should call axios with the proper default data", async () => {
|
||||||
|
|
||||||
|
let response = {
|
||||||
|
data: {
|
||||||
|
Message: "OK"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
axios.get.mockResolvedValueOnce(response);
|
||||||
|
|
||||||
|
let notif = new Telegram();
|
||||||
|
let notificationConf = {
|
||||||
|
type: "telegram",
|
||||||
|
telegramBotToken: "abc",
|
||||||
|
telegramChatID: "123",
|
||||||
|
|
||||||
|
};
|
||||||
|
let monitorConf = {
|
||||||
|
};
|
||||||
|
let heartbeatConf = {
|
||||||
|
};
|
||||||
|
let msg = "PassedInMessage😀";
|
||||||
|
let res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf);
|
||||||
|
|
||||||
|
expect(axios.get).toHaveBeenCalledWith("https://api.telegram.org/botabc/sendMessage", {
|
||||||
|
"params": {
|
||||||
|
"chat_id": "123",
|
||||||
|
"text": "PassedInMessage😀",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(res).toBe("Sent Successfully.");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should call axios with the proper data when monitor nil", async () => {
|
||||||
|
let response = {
|
||||||
|
data: {
|
||||||
|
Message: "OK"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
axios.get.mockResolvedValueOnce(response);
|
||||||
|
|
||||||
|
let notif = new Telegram();
|
||||||
|
let notificationConf = {
|
||||||
|
type: "telegram",
|
||||||
|
telegramBotToken: "abc",
|
||||||
|
telegramChatID: "123",
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
let msg = "PassedInMessage😀";
|
||||||
|
|
||||||
|
let res = await notif.send(notificationConf, msg, null, null);
|
||||||
|
|
||||||
|
expect(axios.get).toHaveBeenCalledWith("https://api.telegram.org/botabc/sendMessage", {
|
||||||
|
"params": {
|
||||||
|
"chat_id": "123",
|
||||||
|
"text": "PassedInMessage😀",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(res).toBe("Sent Successfully.");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("notification to act properly on error", () => {
|
||||||
|
it("should respond with an axios error on error", async () => {
|
||||||
|
|
||||||
|
axios.get.mockImplementation(() => {
|
||||||
|
throw {
|
||||||
|
response: {
|
||||||
|
data: {
|
||||||
|
description: "Error Description"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
let notif = new Telegram();
|
||||||
|
let notificationConf = {
|
||||||
|
type: "telegram",
|
||||||
|
telegramBotToken: "abc",
|
||||||
|
telegramChatID: "123",
|
||||||
|
};
|
||||||
|
let msg = "PassedInMessage😀";
|
||||||
|
|
||||||
|
try {
|
||||||
|
await notif.send(notificationConf, msg, null, null);
|
||||||
|
expect("Error thrown").toBe(false);
|
||||||
|
} catch (e) {
|
||||||
|
expect(e.message).toBe("Error Description");
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(axios.get).toHaveBeenCalledWith("https://api.telegram.org/botabc/sendMessage", {
|
||||||
|
"params": {
|
||||||
|
"chat_id": "123",
|
||||||
|
"text": "PassedInMessage😀",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("notification to get proper data from Notification.send", () => {
|
||||||
|
it("should call axios with proper data", async () => {
|
||||||
|
let response = {
|
||||||
|
data: {
|
||||||
|
Message: "OK"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
axios.get.mockResolvedValueOnce(response);
|
||||||
|
let notificationConf = {
|
||||||
|
type: "telegram",
|
||||||
|
telegramBotToken: "abc",
|
||||||
|
telegramChatID: "123",
|
||||||
|
|
||||||
|
};
|
||||||
|
let monitorConf = {
|
||||||
|
};
|
||||||
|
let heartbeatConf = {
|
||||||
|
};
|
||||||
|
let msg = "PassedInMessage😀";
|
||||||
|
|
||||||
|
NotificationSend.Notification.init();
|
||||||
|
let res = await NotificationSend.Notification.send(notificationConf, msg, monitorConf, heartbeatConf);
|
||||||
|
expect(axios.get).toHaveBeenCalledWith("https://api.telegram.org/botabc/sendMessage", {
|
||||||
|
|
||||||
|
"params": {
|
||||||
|
"chat_id": "123",
|
||||||
|
"text": "PassedInMessage😀",
|
||||||
|
},
|
||||||
|
|
||||||
|
});
|
||||||
|
expect(res).toBe("Sent Successfully.");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue