From 2dac1e95a2ea0f21620ddc591b4117becba25ba8 Mon Sep 17 00:00:00 2001 From: Jeffrey Koehler Date: Thu, 21 Oct 2021 22:45:15 -0500 Subject: [PATCH] discord tests --- server/notification-providers/discord.js | 23 +- server/notification-providers/discord.spec.js | 252 +++++++++++++++++- 2 files changed, 258 insertions(+), 17 deletions(-) diff --git a/server/notification-providers/discord.js b/server/notification-providers/discord.js index 881ad21..06b0273 100644 --- a/server/notification-providers/discord.js +++ b/server/notification-providers/discord.js @@ -7,7 +7,6 @@ class Discord extends NotificationProvider { name = "discord"; async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { - let okMsg = "Sent Successfully."; try { const discordDisplayName = notification.discordUsername || "Uptime Kuma"; @@ -17,9 +16,9 @@ class Discord extends NotificationProvider { let discordtestdata = { username: discordDisplayName, content: msg, - } - await axios.post(notification.discordWebhookUrl, discordtestdata) - return okMsg; + }; + await axios.post(notification.discordWebhookUrl, discordtestdata); + return this.sendSuccess; } let url; @@ -49,7 +48,7 @@ class Discord extends NotificationProvider { }, { name: "Service URL", - value: url, + value: url.startsWith("http") ? "[Visit Service](" + url + ")" : url, }, { name: "Time (UTC)", @@ -61,14 +60,14 @@ class Discord extends NotificationProvider { }, ], }], - } + }; if (notification.discordPrefixMessage) { discorddowndata.content = notification.discordPrefixMessage; } - await axios.post(notification.discordWebhookUrl, discorddowndata) - return okMsg; + await axios.post(notification.discordWebhookUrl, discorddowndata); + return this.sendSuccess; } else if (heartbeatJSON["status"] == UP) { let discordupdata = { @@ -96,17 +95,17 @@ class Discord extends NotificationProvider { }, ], }], - } + }; if (notification.discordPrefixMessage) { discordupdata.content = notification.discordPrefixMessage; } - await axios.post(notification.discordWebhookUrl, discordupdata) - return okMsg; + await axios.post(notification.discordWebhookUrl, discordupdata); + return this.sendSuccess; } } catch (error) { - this.throwGeneralAxiosError(error) + this.throwGeneralAxiosError(error); } } diff --git a/server/notification-providers/discord.spec.js b/server/notification-providers/discord.spec.js index 1f9feb0..1583005 100644 --- a/server/notification-providers/discord.spec.js +++ b/server/notification-providers/discord.spec.js @@ -1,13 +1,15 @@ -// jest.mock("nodemailer", () => ({ -// createTransport: jest.fn(), -// })); +jest.mock("axios", () => ({ + post: jest.fn(), +})); -// const mockNodeMailer = require("nodemailer"); +const axios = require("axios"); +const { UP, DOWN } = require("../../src/util"); +const NotificationSend = require("../notification"); const Discord = require("./discord"); beforeEach(() => { - // mockNodeMailer.createTransport.mockReset(); + axios.post.mockReset(); }); describe("notification default information", () => { @@ -16,3 +18,243 @@ describe("notification default information", () => { expect(notification.name).toBe("discord"); }); }); + +describe("notification to act properly on send", () => { + + it("should call axios with the proper data when missing heartbeat/monitor", async () => { + + let response = {}; + axios.post.mockResolvedValueOnce(response); + + let notif = new Discord(); + let url = "https://example.com/webhook"; + let notificationConf = { + discordUsername: "username", + discordWebhookUrl: url, + }; + let msg = "PassedInMessage"; + + let res = await notif.send(notificationConf, msg, null, null); + + expect(axios.post).toHaveBeenCalledWith(url, { + content: "PassedInMessage", + username: "username" + }); + expect(res).toBe("Sent Successfully."); + }); + + it("should call axios with the proper data when having heartbeat & monitor & service up", async () => { + + jest.spyOn(global.Date, "now") + .mockImplementationOnce(() => + new Date("2019-05-14T11:01:58.135Z").valueOf() + ); + + let response = {}; + axios.post.mockResolvedValueOnce(response); + + let notif = new Discord(); + let url = "https://example.com/webhook"; + let notificationConf = { + discordUsername: "username", + discordWebhookUrl: url, + discordPrefixMessage: "prefix", + }; + let msg = "PassedInMessage"; + let monitorConf = { + type: "http", + url: "https://www.google.com", + name: "testing monitor", + }; + let heartbeatConf = { + status: UP, + msg: "some message", + time: "example time", + ping: "111" + }; + let res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf); + + expect(axios.post).toHaveBeenCalledWith(url, { + content: "prefix", + embeds: [ + { + color: 65280, + fields: [ + { + name: "Service Name", + value: "testing monitor", + }, + { + name: "Service URL", + value: "[Visit Service](https://www.google.com)", + }, + { + name: "Time (UTC)", + value: "example time", + }, + { + name: "Ping", + value: "111ms", + }, + ], + timestamp: "example time", + title: "✅ Your service testing monitor is up! ✅", + }, + ], + username: "username" + }); + expect(res).toBe("Sent Successfully."); + }); + + it("should call axios with the proper data when having heartbeat & monitor & service down", async () => { + + let response = {}; + axios.post.mockResolvedValueOnce(response); + + let notif = new Discord(); + let url = "https://example.com/webhook"; + let notificationConf = { + discordUsername: "username", + discordWebhookUrl: url, + discordPrefixMessage: "prefix", + }; + let msg = "PassedInMessage"; + let monitorConf = { + type: "http", + url: "https://www.google.com", + name: "testing monitor", + }; + let heartbeatConf = { + status: DOWN, + msg: "some message", + time: "example time", + ping: "111" + }; + let res = await notif.send(notificationConf, msg, monitorConf, heartbeatConf); + + expect(axios.post).toHaveBeenCalledWith(url, { + content: "prefix", + embeds: [ + { + color: 16711680, + fields: [ + { + name: "Service Name", + value: "testing monitor", + }, + { + name: "Service URL", + value: "[Visit Service](https://www.google.com)", + }, + { + name: "Time (UTC)", + value: "example time", + }, + { + name: "Error", + value: "some message", + }, + ], + timestamp: "example time", + title: "❌ Your service testing monitor went down. ❌", + }, + ], + username: "username" + }); + 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 notif = new Discord(); + let notificationConf = { + appriseURL: "appriseURL", + secretKey: "abc", + discordWebhookUrl: "https://example.com/webhook", + }; + let msg = "PassedInMessage"; + + try { + await notif.send(notificationConf, msg, null, null); + 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.post).toHaveBeenCalledWith("https://example.com/webhook", { + content: "PassedInMessage", + username: "Uptime Kuma" + }); + }); + +}); + +describe("notification to get proper data from Notification.send", () => { + it("should call sendMail with proper data", async () => { + + let response = {}; + axios.post.mockResolvedValueOnce(response); + + let url = "https://example.com/webhook"; + let notificationConf = { + type: "discord", + discordUsername: "username", + discordWebhookUrl: url, + discordPrefixMessage: "prefix", + }; + let monitorConf = { + type: "http", + url: "https://www.google.com", + name: "testing monitor", + }; + let heartbeatConf = { + status: UP, + msg: "some message", + time: "example time", + ping: "111" + }; + + NotificationSend.Notification.init(); + let res = await NotificationSend.Notification.send(notificationConf, "PassedInMessage", monitorConf, heartbeatConf); + expect(axios.post).toHaveBeenCalledWith(url, { + content: "prefix", + embeds: [ + { + color: 65280, + fields: [ + { + name: "Service Name", + value: "testing monitor", + }, + { + name: "Service URL", + value: "[Visit Service](https://www.google.com)", + }, + { + name: "Time (UTC)", + value: "example time", + }, + { + name: "Ping", + value: "111ms", + }, + ], + timestamp: "example time", + title: "✅ Your service testing monitor is up! ✅", + }, + ], + username: "username" + }); + expect(res).toBe("Sent Successfully."); + }); + +});