From 1b1e0f6dd942d25c4b97a549ef6f1b28870b95d5 Mon Sep 17 00:00:00 2001 From: jordanbertasso <36979824+jordanbertasso@users.noreply.github.com> Date: Sun, 10 Apr 2022 21:45:19 +1000 Subject: [PATCH] Add Discord tests --- test/backend.spec.js | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/test/backend.spec.js b/test/backend.spec.js index bbfc6897b..7c02d3429 100644 --- a/test/backend.spec.js +++ b/test/backend.spec.js @@ -1,5 +1,9 @@ -const { genSecret, sleep } = require("../src/util"); +const { genSecret, sleep, DOWN } = require("../src/util"); const utilServerRewire = require("../server/util-server"); +const Discord = require("../server/notification-providers/discord"); +const axios = require("axios"); + +jest.mock("axios"); describe("Test parseCertificateInfo", () => { it("should handle undefined", async () => { @@ -164,3 +168,41 @@ describe("Test reset-password", () => { }, 120000); }); +describe("Test Discord Notification Provider", () => { + const sendNotification = async (type) => { + const discordProvider = new Discord(); + + axios.post.mockResolvedValue({}); + + await discordProvider.send( + { + discordUsername: "Uptime Kuma", + discordWebhookUrl: "https://discord.com", + }, + "test message", + { + type, + hostname: "discord.com" + (type === "port" ? ":1337" : ""), + }, + { + status: DOWN, + } + ); + }; + + it("should send hostname for ping monitors", async () => { + await sendNotification("ping"); + + expect(axios.post.mock.lastCall[1].embeds[0].fields[1].value).toBe( + "discord.com" + ); + }); + + it("should send hostname for port monitors", async () => { + await sendNotification("port"); + + expect(axios.post.mock.lastCall[1].embeds[0].fields[1].value).toBe( + "discord.com:1337" + ); + }); +});