From 9ea0a9c82e7a3818f0a635bd1140da52d9769f2b Mon Sep 17 00:00:00 2001 From: Jeffrey Koehler Date: Mon, 25 Oct 2021 23:32:43 -0500 Subject: [PATCH] telegram tests --- server/notification-providers/telegram.js | 10 +- .../notification-providers/telegram.spec.js | 151 +++++++++++++++++- 2 files changed, 148 insertions(+), 13 deletions(-) diff --git a/server/notification-providers/telegram.js b/server/notification-providers/telegram.js index 54d33bf..4caef56 100644 --- a/server/notification-providers/telegram.js +++ b/server/notification-providers/telegram.js @@ -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); } } } diff --git a/server/notification-providers/telegram.spec.js b/server/notification-providers/telegram.spec.js index 285eef8..ef6e14d 100644 --- a/server/notification-providers/telegram.spec.js +++ b/server/notification-providers/telegram.spec.js @@ -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."); + }); + +});