|
| 1 | +import config from "../../../config/config"; |
| 2 | +import JSONResponse from "../../../src/utils/JsonResponse"; |
| 3 | +import { |
| 4 | + generateStringToBeSent, |
| 5 | + sendProfileServiceBlockedMessage, |
| 6 | +} from "../../../src/utils/sendProfileServiceBlockedMessage"; |
| 7 | + |
| 8 | +describe("Send Profile Service Blocked Message", () => { |
| 9 | + const mockEnv = { |
| 10 | + BOT_PUBLIC_KEY: "xyz", |
| 11 | + DISCORD_GUILD_ID: "123", |
| 12 | + DISCORD_TOKEN: "abc", |
| 13 | + }; |
| 14 | + |
| 15 | + const mockData = { |
| 16 | + discordId: "12345678910111213", |
| 17 | + reason: "Unauthenticated access to profile service", |
| 18 | + }; |
| 19 | + |
| 20 | + test("should send the message if both userId and reason provided", async () => { |
| 21 | + const url = config(mockEnv).TRACKING_CHANNEL_URL; |
| 22 | + const data = { |
| 23 | + content: generateStringToBeSent( |
| 24 | + mockData.discordId, |
| 25 | + mockData.reason, |
| 26 | + mockEnv |
| 27 | + ), |
| 28 | + }; |
| 29 | + |
| 30 | + jest |
| 31 | + .spyOn(global, "fetch") |
| 32 | + .mockImplementation(() => Promise.resolve(new JSONResponse(""))); |
| 33 | + |
| 34 | + await sendProfileServiceBlockedMessage( |
| 35 | + mockData.discordId, |
| 36 | + mockData.reason, |
| 37 | + mockEnv |
| 38 | + ); |
| 39 | + |
| 40 | + expect(global.fetch).toHaveBeenCalledWith(url, { |
| 41 | + method: "POST", |
| 42 | + headers: { |
| 43 | + "Content-Type": "application/json", |
| 44 | + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, |
| 45 | + }, |
| 46 | + body: JSON.stringify(data), |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + test("should send the message if userId not present", async () => { |
| 51 | + const data = { |
| 52 | + content: generateStringToBeSent("", mockData.reason, mockEnv), |
| 53 | + }; |
| 54 | + const url = config(mockEnv).TRACKING_CHANNEL_URL; |
| 55 | + |
| 56 | + jest |
| 57 | + .spyOn(global, "fetch") |
| 58 | + .mockImplementation(() => Promise.resolve(new JSONResponse(""))); |
| 59 | + |
| 60 | + await sendProfileServiceBlockedMessage("", mockData.reason, mockEnv); |
| 61 | + |
| 62 | + expect(global.fetch).toHaveBeenCalledWith(url, { |
| 63 | + method: "POST", |
| 64 | + headers: { |
| 65 | + "Content-Type": "application/json", |
| 66 | + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, |
| 67 | + }, |
| 68 | + body: JSON.stringify(data), |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + test("should send the message if both are not present", async () => { |
| 73 | + const data = { |
| 74 | + content: generateStringToBeSent("", "", mockEnv), |
| 75 | + }; |
| 76 | + const url = config(mockEnv).TRACKING_CHANNEL_URL; |
| 77 | + |
| 78 | + jest |
| 79 | + .spyOn(global, "fetch") |
| 80 | + .mockImplementation(() => Promise.resolve(new JSONResponse(""))); |
| 81 | + |
| 82 | + await sendProfileServiceBlockedMessage("", "", mockEnv); |
| 83 | + |
| 84 | + expect(global.fetch).toHaveBeenCalledWith(url, { |
| 85 | + method: "POST", |
| 86 | + headers: { |
| 87 | + "Content-Type": "application/json", |
| 88 | + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, |
| 89 | + }, |
| 90 | + body: JSON.stringify(data), |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + test("should send the message if reason is not present", async () => { |
| 95 | + const data = { |
| 96 | + content: generateStringToBeSent(mockData.discordId, "", mockEnv), |
| 97 | + }; |
| 98 | + const url = config(mockEnv).TRACKING_CHANNEL_URL; |
| 99 | + |
| 100 | + jest |
| 101 | + .spyOn(global, "fetch") |
| 102 | + .mockImplementation(() => Promise.resolve(new JSONResponse(""))); |
| 103 | + |
| 104 | + await sendProfileServiceBlockedMessage(mockData.discordId, "", mockEnv); |
| 105 | + |
| 106 | + expect(global.fetch).toHaveBeenCalledWith(url, { |
| 107 | + method: "POST", |
| 108 | + headers: { |
| 109 | + "Content-Type": "application/json", |
| 110 | + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, |
| 111 | + }, |
| 112 | + body: JSON.stringify(data), |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments