mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-07-26 18:33:16 +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";
|
||||
|
||||
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
||||
let okMsg = "Sent Successfully.";
|
||||
|
||||
try {
|
||||
await axios.get(`https://api.telegram.org/bot${notification.telegramBotToken}/sendMessage`, {
|
||||
|
@ -14,12 +13,11 @@ class Telegram extends NotificationProvider {
|
|||
chat_id: notification.telegramChatID,
|
||||
text: msg,
|
||||
},
|
||||
})
|
||||
return okMsg;
|
||||
|
||||
});
|
||||
return this.sendSuccess;
|
||||
} catch (error) {
|
||||
let msg = (error.response.data.description) ? error.response.data.description : "Error without description"
|
||||
throw new Error(msg)
|
||||
let msg = (error.response.data.description) ? error.response.data.description : "Error without description";
|
||||
throw new Error(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
// jest.mock("nodemailer", () => ({
|
||||
// createTransport: jest.fn(),
|
||||
// }));
|
||||
jest.mock("axios", () => ({
|
||||
get: jest.fn(),
|
||||
}));
|
||||
|
||||
// const mockNodeMailer = require("nodemailer");
|
||||
|
||||
const Telegram = require("./telegram");
|
||||
const axios = require("axios");
|
||||
const { UP, DOWN } = require("../../src/util");
|
||||
const NotificationSend = require("../notification");
|
||||
|
||||
beforeEach(() => {
|
||||
// mockNodeMailer.createTransport.mockReset();
|
||||
axios.get.mockReset();
|
||||
});
|
||||
const Telegram = require("./telegram");
|
||||
|
||||
describe("notification default information", () => {
|
||||
it("should have the correct name", () => {
|
||||
|
@ -16,3 +17,139 @@ describe("notification default information", () => {
|
|||
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