From e4c98ebf613677e15a5a774ab3070d949277de38 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Sat, 13 Jan 2024 19:39:46 +0530 Subject: [PATCH 1/3] Added the header to passed down the API --- src/controllers/changeNickname.ts | 3 +- src/controllers/generateDiscordInvite.ts | 4 +- src/controllers/guildRoleHandler.ts | 35 +++++--- src/utils/createDiscordHeaders.ts | 17 ++++ src/utils/generateDiscordInvite.ts | 16 ++-- src/utils/guildRole.ts | 52 +++++++---- src/utils/updateNickname.ts | 13 +-- .../handlers/generateDiscordInvite.test.ts | 39 ++++++-- tests/unit/utils/createDiscordHeaders.test.ts | 14 +++ .../unit/utils/generateDiscordInvite.test.ts | 25 ++++++ tests/unit/utils/guildRole.test.ts | 89 +++++++++++++++++++ tests/unit/utils/updateNickname.test.ts | 25 ++++++ 12 files changed, 284 insertions(+), 48 deletions(-) create mode 100644 src/utils/createDiscordHeaders.ts create mode 100644 tests/unit/utils/createDiscordHeaders.test.ts diff --git a/src/controllers/changeNickname.ts b/src/controllers/changeNickname.ts index 191208b3..f5b785b2 100644 --- a/src/controllers/changeNickname.ts +++ b/src/controllers/changeNickname.ts @@ -7,6 +7,7 @@ import { updateNickName } from "../utils/updateNickname"; export async function changeNickname(request: IRequest, env: env) { const authHeader = await request.headers.get("Authorization"); + const reson = request.headers.get("X-Audit-Log-Reason"); if (!authHeader) { return new JSONResponse(response.BAD_SIGNATURE); @@ -15,7 +16,7 @@ export async function changeNickname(request: IRequest, env: env) { try { await verifyAuthToken(authHeader, env); const { discordId, userName } = await request.json(); - const res = await updateNickName(discordId, userName, env); + const res = await updateNickName(discordId, userName, env, reson); return new JSONResponse(res); } catch { return new JSONResponse(response.BAD_SIGNATURE); diff --git a/src/controllers/generateDiscordInvite.ts b/src/controllers/generateDiscordInvite.ts index 4bee3572..70e17164 100644 --- a/src/controllers/generateDiscordInvite.ts +++ b/src/controllers/generateDiscordInvite.ts @@ -13,8 +13,10 @@ export async function generateInviteLink(request: IRequest, env: env) { } try { await verifyAuthToken(authHeader, env); + const reson = request.headers.get("X-Audit-Log-Reason"); + const body: inviteLinkBody = await request.json(); - const res = await generateDiscordLink(body, env); + const res = await generateDiscordLink(body, env, reson); return new JSONResponse(res); } catch (err) { return new JSONResponse(response.BAD_SIGNATURE); diff --git a/src/controllers/guildRoleHandler.ts b/src/controllers/guildRoleHandler.ts index d3a60671..d4e75ac3 100644 --- a/src/controllers/guildRoleHandler.ts +++ b/src/controllers/guildRoleHandler.ts @@ -17,6 +17,7 @@ import { verifyAuthToken, verifyCronJobsToken } from "../utils/verifyAuthToken"; import { batchDiscordRequests } from "../utils/batchDiscordRequests"; import { DISCORD_BASE_URL } from "../constants/urls"; import { GROUP_ROLE_ADD } from "../constants/requestsActions"; +import createDiscordHeaders from "../utils/createDiscordHeaders"; export async function createGuildRoleHandler(request: IRequest, env: env) { const authHeader = request.headers.get("Authorization"); @@ -26,11 +27,13 @@ export async function createGuildRoleHandler(request: IRequest, env: env) { try { await verifyAuthToken(authHeader, env); const body: createNewRole = await request.json(); - - const res = await createGuildRole(body, env); + const reson = request.headers.get("X-Audit-Log-Reason"); + const res = await createGuildRole(body, env, reson); return new JSONResponse(res); } catch (err) { - return new JSONResponse(response.BAD_SIGNATURE); + return new JSONResponse({ + error: err, + }); } } export async function addGroupRoleHandler(request: IRequest, env: env) { @@ -41,8 +44,9 @@ export async function addGroupRoleHandler(request: IRequest, env: env) { try { await verifyAuthToken(authHeader, env); const body: memberGroupRole = await request.json(); + const reson = request.headers.get("X-Audit-Log-Reason"); - const res = await addGroupRole(body, env); + const res = await addGroupRole(body, env, reson); return new JSONResponse(res); } catch (err) { return new JSONResponse(response.BAD_SIGNATURE); @@ -54,6 +58,7 @@ export async function getGuildRolesPostHandler(request: IRequest, env: env) { if (!authHeader) { return new JSONResponse(response.BAD_SIGNATURE); } + const reson = request.headers.get("X-Audit-Log-Reason"); try { await verifyCronJobsToken(authHeader, env); @@ -62,7 +67,11 @@ export async function getGuildRolesPostHandler(request: IRequest, env: env) { switch (action) { case GROUP_ROLE_ADD.ADD_ROLE: { const memberGroupRoleList = await request.json(); - const res = await bulkAddGroupRoleHandler(memberGroupRoleList, env); + const res = await bulkAddGroupRoleHandler( + memberGroupRoleList, + env, + reson + ); return res; } default: { @@ -77,7 +86,8 @@ export async function getGuildRolesPostHandler(request: IRequest, env: env) { export async function bulkAddGroupRoleHandler( memberGroupRoleList: memberGroupRole[], - env: env + env: env, + reson?: string ): Promise { try { if (!Array.isArray(memberGroupRoleList)) { @@ -105,12 +115,13 @@ export async function bulkAddGroupRoleHandler( const { userid, roleid } = memberGroupRole; try { const createGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/members/${userid}/roles/${roleid}`; + const headers: HeadersInit = createDiscordHeaders({ + reson, + token: env.DISCORD_TOKEN, + }); const options = { method: "PUT", - headers: { - "Content-Type": "application/json", - Authorization: `Bot ${env.DISCORD_TOKEN}`, - }, + headers, }; return await fetch(createGuildRoleUrl, options); } catch (error) { @@ -141,13 +152,15 @@ export async function bulkAddGroupRoleHandler( export async function removeGuildRoleHandler(request: IRequest, env: env) { const authHeader = request.headers.get("Authorization"); + const reson = request.headers.get("X-Audit-Log-Reason"); + if (!authHeader) { return new JSONResponse(response.BAD_SIGNATURE, { status: 401 }); } try { await verifyAuthToken(authHeader, env); const body: memberGroupRole = await request.json(); - const res = await removeGuildRole(body, env); + const res = await removeGuildRole(body, env, reson); return new JSONResponse(res, { status: 200, headers: { diff --git a/src/utils/createDiscordHeaders.ts b/src/utils/createDiscordHeaders.ts new file mode 100644 index 00000000..424065e2 --- /dev/null +++ b/src/utils/createDiscordHeaders.ts @@ -0,0 +1,17 @@ +const createDiscordHeaders = ({ + reson, + token, +}: { + reson?: string; + token: string; +}) => { + const headers: HeadersInit = { + "Content-Type": "application/json", + Authorization: `Bot ${token}`, + }; + if (reson) { + headers["X-Audit-Log-Reason"] = reson; + } + return headers; +}; +export default createDiscordHeaders; diff --git a/src/utils/generateDiscordInvite.ts b/src/utils/generateDiscordInvite.ts index efaecd67..5db3b401 100644 --- a/src/utils/generateDiscordInvite.ts +++ b/src/utils/generateDiscordInvite.ts @@ -10,8 +10,13 @@ import { import { DISCORD_BASE_URL } from "../constants/urls"; import { env } from "../typeDefinitions/default.types"; import { inviteLinkBody } from "../typeDefinitions/discordLink.types"; +import createDiscordHeaders from "./createDiscordHeaders"; -export async function generateDiscordLink(body: inviteLinkBody, env: env) { +export async function generateDiscordLink( + body: inviteLinkBody, + env: env, + reson?: string +) { const { channelId } = body; const generateInviteUrl = `${DISCORD_BASE_URL}/channels/${channelId}/invites`; @@ -20,13 +25,14 @@ export async function generateDiscordLink(body: inviteLinkBody, env: env) { unique: INVITE_OPTIONS.UNIQUE, // Whether to create a unique invite or not }; try { + const headers: HeadersInit = createDiscordHeaders({ + reson, + token: env.DISCORD_TOKEN, + }); const response = await fetch(generateInviteUrl, { method: "POST", body: JSON.stringify(inviteOptions), - headers: { - "Content-Type": "application/json", - Authorization: `Bot ${env.DISCORD_TOKEN}`, - }, + headers, }); if (response.ok) { diff --git a/src/utils/guildRole.ts b/src/utils/guildRole.ts index b0531ffc..e43c3c6b 100644 --- a/src/utils/guildRole.ts +++ b/src/utils/guildRole.ts @@ -1,3 +1,4 @@ +import { HeadersInit } from "node-fetch"; import { INTERNAL_SERVER_ERROR, ROLE_ADDED, @@ -12,23 +13,26 @@ import { memberGroupRole, } from "../typeDefinitions/discordMessage.types"; import { GuildRole, Role } from "../typeDefinitions/role.types"; +import createDiscordHeaders from "./createDiscordHeaders"; export async function createGuildRole( body: createNewRole, - env: env + env: env, + reson?: string ): Promise { const createGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/roles`; const data = { ...body, name: body.rolename, }; + const headers: HeadersInit = createDiscordHeaders({ + reson, + token: env.DISCORD_TOKEN, + }); try { const response = await fetch(createGuildRoleUrl, { method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bot ${env.DISCORD_TOKEN}`, - }, + headers, body: JSON.stringify(data), }); if (response.ok) { @@ -41,16 +45,21 @@ export async function createGuildRole( } } -export async function addGroupRole(body: memberGroupRole, env: env) { +export async function addGroupRole( + body: memberGroupRole, + env: env, + reson?: string +) { const { userid, roleid } = body; const createGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/members/${userid}/roles/${roleid}`; try { + const headers: HeadersInit = createDiscordHeaders({ + reson, + token: env.DISCORD_TOKEN, + }); const response = await fetch(createGuildRoleUrl, { method: "PUT", - headers: { - "Content-Type": "application/json", - Authorization: `Bot ${env.DISCORD_TOKEN}`, - }, + headers, }); if (response.ok) { return { message: ROLE_ADDED }; @@ -62,16 +71,21 @@ export async function addGroupRole(body: memberGroupRole, env: env) { } } -export async function removeGuildRole(details: memberGroupRole, env: env) { +export async function removeGuildRole( + details: memberGroupRole, + env: env, + reson?: string +) { const { userid, roleid } = details; const removeGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/members/${userid}/roles/${roleid}`; try { + const headers: HeadersInit = createDiscordHeaders({ + reson, + token: env.DISCORD_TOKEN, + }); const response = await fetch(removeGuildRoleUrl, { method: "DELETE", - headers: { - "Content-Type": "application/json", - Authorization: `Bot ${env.DISCORD_TOKEN}`, - }, + headers, }); if (response.ok) { return { @@ -91,12 +105,12 @@ export async function getGuildRoles(env: env): Promise> { const guildRolesUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/roles`; try { + const headers: HeadersInit = createDiscordHeaders({ + token: env.DISCORD_TOKEN, + }); const response = await fetch(guildRolesUrl, { method: "GET", - headers: { - "Content-Type": "application/json", - Authorization: `Bot ${env.DISCORD_TOKEN}`, - }, + headers, }); if (!response.ok) { diff --git a/src/utils/updateNickname.ts b/src/utils/updateNickname.ts index 860d89d3..6601ba42 100644 --- a/src/utils/updateNickname.ts +++ b/src/utils/updateNickname.ts @@ -1,21 +1,24 @@ import { INTERNAL_SERVER_ERROR, NAME_CHANGED } from "../constants/responses"; import { DISCORD_BASE_URL } from "../constants/urls"; import { env } from "../typeDefinitions/default.types"; +import createDiscordHeaders from "./createDiscordHeaders"; export async function updateNickName( discordId: string, nickname: string, - env: env + env: env, + reson?: string ) { const changeNickNameURL = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/members/${discordId}`; const data = { nick: nickname }; + const headers: HeadersInit = createDiscordHeaders({ + reson, + token: env.DISCORD_TOKEN, + }); try { const nameChangeResponse = await fetch(changeNickNameURL, { method: "PATCH", - headers: { - "Content-Type": "application/json", - Authorization: `Bot ${env.DISCORD_TOKEN}`, - }, + headers, body: JSON.stringify(data), }); if (nameChangeResponse.ok) { diff --git a/tests/unit/handlers/generateDiscordInvite.test.ts b/tests/unit/handlers/generateDiscordInvite.test.ts index ee4b1f7e..ae4468e7 100644 --- a/tests/unit/handlers/generateDiscordInvite.test.ts +++ b/tests/unit/handlers/generateDiscordInvite.test.ts @@ -1,3 +1,9 @@ +const generateDiscordLink = jest + .fn() + .mockReturnValue({ data: {}, message: "Invite created successfully!" }); +jest.mock("../../../src/utils/generateDiscordInvite", () => ({ + generateDiscordLink, +})); import { generateInviteLink } from "../../../src/controllers/generateDiscordInvite"; import JSONResponse from "../../../src/utils/JsonResponse"; import { generateDummyRequestObject, guildEnv } from "../../fixtures/fixture"; @@ -8,13 +14,34 @@ jest.mock("../../../src/utils/verifyAuthToken", () => ({ verifyAuthToken: jest.fn().mockReturnValue(true), })); -jest.mock("../../../src/utils/generateDiscordInvite", () => ({ - generateDiscordLink: jest - .fn() - .mockReturnValue({ data: {}, message: "Invite created successfully!" }), -})); - describe("generate discord link", () => { + it("should return data object with message on success ", async () => { + const mockRequest = generateDummyRequestObject({ + method: "PUT", + url: "/invite", + headers: { + Authorization: "Bearer testtoken", + "Content-Type": "application/json", + "X-Audit-Log-Reason": "This is a reson", + }, + json: async () => { + return { channelId: "xyz" }; + }, + }); + + const response: JSONResponse = await generateInviteLink( + mockRequest, + guildEnv + ); + + await response.json(); + const body = await mockRequest.json(); + expect(generateDiscordLink).toHaveBeenLastCalledWith( + body, + guildEnv, + "This is a reson" + ); + }); it("should return 🚫 Bad Request Signature' if authtoken is there in the header", async () => { const mockRequest = generateDummyRequestObject({ url: "/invite", diff --git a/tests/unit/utils/createDiscordHeaders.test.ts b/tests/unit/utils/createDiscordHeaders.test.ts new file mode 100644 index 00000000..8dae3441 --- /dev/null +++ b/tests/unit/utils/createDiscordHeaders.test.ts @@ -0,0 +1,14 @@ +import createDiscordHeaders from "../../../src/utils/createDiscordHeaders"; + +describe("createDiscordHeaders", () => { + it("should return an object with Authorization key if only token is passed", () => { + const header = createDiscordHeaders({ token: "1234567890" }); + expect(header.Authorization).toBe("Bot 1234567890"); + expect(header["X-Audit-Log-Reason"]).toBeFalsy(); + }); + it("should return an object with both prop Authorization and X-Audit-Log-Reason if resona and token prop are passed", () => { + const header = createDiscordHeaders({ token: "1234567890", reson: "456" }); + expect(header.Authorization).toBe("Bot 1234567890"); + expect(header["X-Audit-Log-Reason"]).toBe("456"); + }); +}); diff --git a/tests/unit/utils/generateDiscordInvite.test.ts b/tests/unit/utils/generateDiscordInvite.test.ts index 8b18d5c9..1f2cbf52 100644 --- a/tests/unit/utils/generateDiscordInvite.test.ts +++ b/tests/unit/utils/generateDiscordInvite.test.ts @@ -5,6 +5,31 @@ import { generateDiscordLink } from "../../../src/utils/generateDiscordInvite"; import { dummyInviteBody, guildEnv } from "../../fixtures/fixture"; describe("generate invite link", () => { + it("should pass the reson to discord as a X-Audit-Log-Reason header if provided", async () => { + jest + .spyOn(global, "fetch") + .mockImplementation(() => Promise.resolve(new JSONResponse({}))); + + await generateDiscordLink(dummyInviteBody, guildEnv); + + await generateDiscordLink(dummyInviteBody, guildEnv, "This is a reson"); + + expect(global.fetch).toHaveBeenCalledWith( + `https://discord.com/api/v10/channels/${dummyInviteBody.channelId}/invites`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`, + "X-Audit-Log-Reason": "This is a reson", + }, + body: JSON.stringify({ + max_uses: INVITE_OPTIONS.MAX_USE, + unique: INVITE_OPTIONS.UNIQUE, + }), + } + ); + }); test("should return INTERNAL_SERVER_ERROR when response is not ok", async () => { const mockResponse = new Response(null, { status: 500 }); jest diff --git a/tests/unit/utils/guildRole.test.ts b/tests/unit/utils/guildRole.test.ts index d518ecca..66b0108c 100644 --- a/tests/unit/utils/guildRole.test.ts +++ b/tests/unit/utils/guildRole.test.ts @@ -15,6 +15,33 @@ import { } from "../../fixtures/fixture"; describe("createGuildRole", () => { + it("should pass the reson to discord as a X-Audit-Log-Reason header if provided", async () => { + jest + .spyOn(global, "fetch") + .mockImplementation((inp) => Promise.resolve(new JSONResponse(inp))); + + await createGuildRole( + dummyCreateBody, + guildEnv, + "This is reson for this action" + ); + + expect(global.fetch).toHaveBeenCalledWith( + `https://discord.com/api/v10/guilds/${guildEnv.DISCORD_GUILD_ID}/roles`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`, + "X-Audit-Log-Reason": "This is reson for this action", + }, + body: JSON.stringify({ + ...dummyCreateBody, + name: dummyCreateBody.rolename, + }), + } + ); + }); test("should return INTERNAL_SERVER_ERROR when response is not ok", async () => { const mockResponse = response.INTERNAL_SERVER_ERROR; jest @@ -71,6 +98,35 @@ describe("createGuildRole", () => { }); describe("addGroupRole", () => { + it("should pass the reson to discord as a X-Audit-Log-Reason header if provided", async () => { + const mockResponse = { + ok: true, + }; + jest + .spyOn(global, "fetch") + .mockImplementation(() => + Promise.resolve(new JSONResponse(mockResponse)) + ); + + const result = await addGroupRole( + dummyAddRoleBody, + guildEnv, + "This is a reson" + ); + + expect(result).toEqual({ message: "Role added successfully" }); + expect(global.fetch).toHaveBeenCalledWith( + `https://discord.com/api/v10/guilds/${guildEnv.DISCORD_GUILD_ID}/members/${dummyAddRoleBody.userid}/roles/${dummyAddRoleBody.roleid}`, + { + method: "PUT", + headers: { + "Content-Type": "application/json", + Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`, + "X-Audit-Log-Reason": "This is a reson", + }, + } + ); + }); test("should return success message when response is ok", async () => { const mockResponse = { ok: true, @@ -126,6 +182,39 @@ describe("removeGuildRole", () => { }, }); }); + test("should pass the reson to discord as a X-Audit-Log-Reason header if provided", async () => { + const mockResponse = { + ok: true, + }; + jest + .spyOn(global, "fetch") + .mockImplementation(() => + Promise.resolve(new JSONResponse(mockResponse)) + ); + const result = await removeGuildRole( + dummyAddRoleBody, + guildEnv, + "this is reson" + ); + expect(global.fetch).toHaveBeenCalledWith( + `https://discord.com/api/v10/guilds/${guildEnv.DISCORD_GUILD_ID}/members/${dummyAddRoleBody.userid}/roles/${dummyAddRoleBody.roleid}`, + { + method: "DELETE", + headers: { + "Content-Type": "application/json", + Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`, + "X-Audit-Log-Reason": "this is reson", + }, + } + ); + expect(result).toEqual({ + message: response.ROLE_REMOVED, + userAffected: { + userid: dummyAddRoleBody.userid, + roleid: dummyAddRoleBody.roleid, + }, + }); + }); test("Should return internal error response on api failure", async () => { jest.spyOn(global, "fetch").mockRejectedValue("Oops some error"); const result = await removeGuildRole(dummyAddRoleBody, guildEnv); diff --git a/tests/unit/utils/updateNickname.test.ts b/tests/unit/utils/updateNickname.test.ts index dc78fef8..6c24fb23 100644 --- a/tests/unit/utils/updateNickname.test.ts +++ b/tests/unit/utils/updateNickname.test.ts @@ -14,7 +14,32 @@ describe("Update nickname", () => { }; const mockData = { discordId: "12345678910111213", nickname: "jhon" }; + it("should pass the reson to discord as a X-Audit-Log-Reason header if provided", async () => { + jest + .spyOn(global, "fetch") + .mockRejectedValue(() => + Promise.resolve(new JSONResponse({ INTERNAL_SERVER_ERROR })) + ); + await updateNickName( + mockData.discordId, + "", + mockEnv, + "This is reson for this action" + ); + expect(global.fetch).toHaveBeenCalledWith( + `${DISCORD_BASE_URL}/guilds/${mockEnv.DISCORD_GUILD_ID}/members/${mockData.discordId}`, + { + method: "PATCH", + headers: { + "Content-Type": "application/json", + Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, + "X-Audit-Log-Reason": "This is reson for this action", + }, + body: JSON.stringify({ nick: "" }), + } + ); + }); test("updatenickname fetch is called with expected parameters", async () => { const data = { nick: mockData.nickname }; From 81261884e885f662355f1b617635e315511059f3 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Sat, 13 Jan 2024 20:18:46 +0530 Subject: [PATCH 2/3] Corrected the spelling --- src/controllers/changeNickname.ts | 4 ++-- src/controllers/generateDiscordInvite.ts | 4 ++-- src/controllers/guildRoleHandler.ts | 20 +++++++++---------- src/utils/createDiscordHeaders.ts | 8 ++++---- src/utils/generateDiscordInvite.ts | 4 ++-- src/utils/guildRole.ts | 12 +++++------ src/utils/updateNickname.ts | 4 ++-- .../handlers/generateDiscordInvite.test.ts | 4 ++-- tests/unit/utils/createDiscordHeaders.test.ts | 4 ++-- .../unit/utils/generateDiscordInvite.test.ts | 6 +++--- tests/unit/utils/guildRole.test.ts | 18 ++++++++--------- tests/unit/utils/updateNickname.test.ts | 6 +++--- 12 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/controllers/changeNickname.ts b/src/controllers/changeNickname.ts index f5b785b2..61358708 100644 --- a/src/controllers/changeNickname.ts +++ b/src/controllers/changeNickname.ts @@ -7,7 +7,7 @@ import { updateNickName } from "../utils/updateNickname"; export async function changeNickname(request: IRequest, env: env) { const authHeader = await request.headers.get("Authorization"); - const reson = request.headers.get("X-Audit-Log-Reason"); + const reason = request.headers.get("X-Audit-Log-Reason"); if (!authHeader) { return new JSONResponse(response.BAD_SIGNATURE); @@ -16,7 +16,7 @@ export async function changeNickname(request: IRequest, env: env) { try { await verifyAuthToken(authHeader, env); const { discordId, userName } = await request.json(); - const res = await updateNickName(discordId, userName, env, reson); + const res = await updateNickName(discordId, userName, env, reason); return new JSONResponse(res); } catch { return new JSONResponse(response.BAD_SIGNATURE); diff --git a/src/controllers/generateDiscordInvite.ts b/src/controllers/generateDiscordInvite.ts index 70e17164..a7533b91 100644 --- a/src/controllers/generateDiscordInvite.ts +++ b/src/controllers/generateDiscordInvite.ts @@ -13,10 +13,10 @@ export async function generateInviteLink(request: IRequest, env: env) { } try { await verifyAuthToken(authHeader, env); - const reson = request.headers.get("X-Audit-Log-Reason"); + const reason = request.headers.get("X-Audit-Log-Reason"); const body: inviteLinkBody = await request.json(); - const res = await generateDiscordLink(body, env, reson); + const res = await generateDiscordLink(body, env, reason); return new JSONResponse(res); } catch (err) { return new JSONResponse(response.BAD_SIGNATURE); diff --git a/src/controllers/guildRoleHandler.ts b/src/controllers/guildRoleHandler.ts index d4e75ac3..b4e33134 100644 --- a/src/controllers/guildRoleHandler.ts +++ b/src/controllers/guildRoleHandler.ts @@ -27,8 +27,8 @@ export async function createGuildRoleHandler(request: IRequest, env: env) { try { await verifyAuthToken(authHeader, env); const body: createNewRole = await request.json(); - const reson = request.headers.get("X-Audit-Log-Reason"); - const res = await createGuildRole(body, env, reson); + const reason = request.headers.get("X-Audit-Log-Reason"); + const res = await createGuildRole(body, env, reason); return new JSONResponse(res); } catch (err) { return new JSONResponse({ @@ -44,9 +44,9 @@ export async function addGroupRoleHandler(request: IRequest, env: env) { try { await verifyAuthToken(authHeader, env); const body: memberGroupRole = await request.json(); - const reson = request.headers.get("X-Audit-Log-Reason"); + const reason = request.headers.get("X-Audit-Log-Reason"); - const res = await addGroupRole(body, env, reson); + const res = await addGroupRole(body, env, reason); return new JSONResponse(res); } catch (err) { return new JSONResponse(response.BAD_SIGNATURE); @@ -58,7 +58,7 @@ export async function getGuildRolesPostHandler(request: IRequest, env: env) { if (!authHeader) { return new JSONResponse(response.BAD_SIGNATURE); } - const reson = request.headers.get("X-Audit-Log-Reason"); + const reason = request.headers.get("X-Audit-Log-Reason"); try { await verifyCronJobsToken(authHeader, env); @@ -70,7 +70,7 @@ export async function getGuildRolesPostHandler(request: IRequest, env: env) { const res = await bulkAddGroupRoleHandler( memberGroupRoleList, env, - reson + reason ); return res; } @@ -87,7 +87,7 @@ export async function getGuildRolesPostHandler(request: IRequest, env: env) { export async function bulkAddGroupRoleHandler( memberGroupRoleList: memberGroupRole[], env: env, - reson?: string + reason?: string ): Promise { try { if (!Array.isArray(memberGroupRoleList)) { @@ -116,7 +116,7 @@ export async function bulkAddGroupRoleHandler( try { const createGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/members/${userid}/roles/${roleid}`; const headers: HeadersInit = createDiscordHeaders({ - reson, + reason, token: env.DISCORD_TOKEN, }); const options = { @@ -152,7 +152,7 @@ export async function bulkAddGroupRoleHandler( export async function removeGuildRoleHandler(request: IRequest, env: env) { const authHeader = request.headers.get("Authorization"); - const reson = request.headers.get("X-Audit-Log-Reason"); + const reason = request.headers.get("X-Audit-Log-Reason"); if (!authHeader) { return new JSONResponse(response.BAD_SIGNATURE, { status: 401 }); @@ -160,7 +160,7 @@ export async function removeGuildRoleHandler(request: IRequest, env: env) { try { await verifyAuthToken(authHeader, env); const body: memberGroupRole = await request.json(); - const res = await removeGuildRole(body, env, reson); + const res = await removeGuildRole(body, env, reason); return new JSONResponse(res, { status: 200, headers: { diff --git a/src/utils/createDiscordHeaders.ts b/src/utils/createDiscordHeaders.ts index 424065e2..e07f6587 100644 --- a/src/utils/createDiscordHeaders.ts +++ b/src/utils/createDiscordHeaders.ts @@ -1,16 +1,16 @@ const createDiscordHeaders = ({ - reson, + reason, token, }: { - reson?: string; + reason?: string; token: string; }) => { const headers: HeadersInit = { "Content-Type": "application/json", Authorization: `Bot ${token}`, }; - if (reson) { - headers["X-Audit-Log-Reason"] = reson; + if (reason) { + headers["X-Audit-Log-Reason"] = reason; } return headers; }; diff --git a/src/utils/generateDiscordInvite.ts b/src/utils/generateDiscordInvite.ts index 5db3b401..f12ea903 100644 --- a/src/utils/generateDiscordInvite.ts +++ b/src/utils/generateDiscordInvite.ts @@ -15,7 +15,7 @@ import createDiscordHeaders from "./createDiscordHeaders"; export async function generateDiscordLink( body: inviteLinkBody, env: env, - reson?: string + reason?: string ) { const { channelId } = body; const generateInviteUrl = `${DISCORD_BASE_URL}/channels/${channelId}/invites`; @@ -26,7 +26,7 @@ export async function generateDiscordLink( }; try { const headers: HeadersInit = createDiscordHeaders({ - reson, + reason, token: env.DISCORD_TOKEN, }); const response = await fetch(generateInviteUrl, { diff --git a/src/utils/guildRole.ts b/src/utils/guildRole.ts index e43c3c6b..389762f2 100644 --- a/src/utils/guildRole.ts +++ b/src/utils/guildRole.ts @@ -18,7 +18,7 @@ import createDiscordHeaders from "./createDiscordHeaders"; export async function createGuildRole( body: createNewRole, env: env, - reson?: string + reason?: string ): Promise { const createGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/roles`; const data = { @@ -26,7 +26,7 @@ export async function createGuildRole( name: body.rolename, }; const headers: HeadersInit = createDiscordHeaders({ - reson, + reason, token: env.DISCORD_TOKEN, }); try { @@ -48,13 +48,13 @@ export async function createGuildRole( export async function addGroupRole( body: memberGroupRole, env: env, - reson?: string + reason?: string ) { const { userid, roleid } = body; const createGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/members/${userid}/roles/${roleid}`; try { const headers: HeadersInit = createDiscordHeaders({ - reson, + reason, token: env.DISCORD_TOKEN, }); const response = await fetch(createGuildRoleUrl, { @@ -74,13 +74,13 @@ export async function addGroupRole( export async function removeGuildRole( details: memberGroupRole, env: env, - reson?: string + reason?: string ) { const { userid, roleid } = details; const removeGuildRoleUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/members/${userid}/roles/${roleid}`; try { const headers: HeadersInit = createDiscordHeaders({ - reson, + reason, token: env.DISCORD_TOKEN, }); const response = await fetch(removeGuildRoleUrl, { diff --git a/src/utils/updateNickname.ts b/src/utils/updateNickname.ts index 6601ba42..a14dbfa3 100644 --- a/src/utils/updateNickname.ts +++ b/src/utils/updateNickname.ts @@ -7,12 +7,12 @@ export async function updateNickName( discordId: string, nickname: string, env: env, - reson?: string + reason?: string ) { const changeNickNameURL = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}/members/${discordId}`; const data = { nick: nickname }; const headers: HeadersInit = createDiscordHeaders({ - reson, + reason, token: env.DISCORD_TOKEN, }); try { diff --git a/tests/unit/handlers/generateDiscordInvite.test.ts b/tests/unit/handlers/generateDiscordInvite.test.ts index ae4468e7..c125b269 100644 --- a/tests/unit/handlers/generateDiscordInvite.test.ts +++ b/tests/unit/handlers/generateDiscordInvite.test.ts @@ -22,7 +22,7 @@ describe("generate discord link", () => { headers: { Authorization: "Bearer testtoken", "Content-Type": "application/json", - "X-Audit-Log-Reason": "This is a reson", + "X-Audit-Log-Reason": "This is a reason", }, json: async () => { return { channelId: "xyz" }; @@ -39,7 +39,7 @@ describe("generate discord link", () => { expect(generateDiscordLink).toHaveBeenLastCalledWith( body, guildEnv, - "This is a reson" + "This is a reason" ); }); it("should return 🚫 Bad Request Signature' if authtoken is there in the header", async () => { diff --git a/tests/unit/utils/createDiscordHeaders.test.ts b/tests/unit/utils/createDiscordHeaders.test.ts index 8dae3441..6376ec63 100644 --- a/tests/unit/utils/createDiscordHeaders.test.ts +++ b/tests/unit/utils/createDiscordHeaders.test.ts @@ -6,8 +6,8 @@ describe("createDiscordHeaders", () => { expect(header.Authorization).toBe("Bot 1234567890"); expect(header["X-Audit-Log-Reason"]).toBeFalsy(); }); - it("should return an object with both prop Authorization and X-Audit-Log-Reason if resona and token prop are passed", () => { - const header = createDiscordHeaders({ token: "1234567890", reson: "456" }); + it("should return an object with both prop Authorization and X-Audit-Log-Reason if reason and token prop are passed", () => { + const header = createDiscordHeaders({ token: "1234567890", reason: "456" }); expect(header.Authorization).toBe("Bot 1234567890"); expect(header["X-Audit-Log-Reason"]).toBe("456"); }); diff --git a/tests/unit/utils/generateDiscordInvite.test.ts b/tests/unit/utils/generateDiscordInvite.test.ts index 1f2cbf52..48333ff5 100644 --- a/tests/unit/utils/generateDiscordInvite.test.ts +++ b/tests/unit/utils/generateDiscordInvite.test.ts @@ -5,14 +5,14 @@ import { generateDiscordLink } from "../../../src/utils/generateDiscordInvite"; import { dummyInviteBody, guildEnv } from "../../fixtures/fixture"; describe("generate invite link", () => { - it("should pass the reson to discord as a X-Audit-Log-Reason header if provided", async () => { + it("should pass the reason to discord as a X-Audit-Log-Reason header if provided", async () => { jest .spyOn(global, "fetch") .mockImplementation(() => Promise.resolve(new JSONResponse({}))); await generateDiscordLink(dummyInviteBody, guildEnv); - await generateDiscordLink(dummyInviteBody, guildEnv, "This is a reson"); + await generateDiscordLink(dummyInviteBody, guildEnv, "This is a reason"); expect(global.fetch).toHaveBeenCalledWith( `https://discord.com/api/v10/channels/${dummyInviteBody.channelId}/invites`, @@ -21,7 +21,7 @@ describe("generate invite link", () => { headers: { "Content-Type": "application/json", Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`, - "X-Audit-Log-Reason": "This is a reson", + "X-Audit-Log-Reason": "This is a reason", }, body: JSON.stringify({ max_uses: INVITE_OPTIONS.MAX_USE, diff --git a/tests/unit/utils/guildRole.test.ts b/tests/unit/utils/guildRole.test.ts index 66b0108c..618e0f17 100644 --- a/tests/unit/utils/guildRole.test.ts +++ b/tests/unit/utils/guildRole.test.ts @@ -15,7 +15,7 @@ import { } from "../../fixtures/fixture"; describe("createGuildRole", () => { - it("should pass the reson to discord as a X-Audit-Log-Reason header if provided", async () => { + it("should pass the reason to discord as a X-Audit-Log-Reason header if provided", async () => { jest .spyOn(global, "fetch") .mockImplementation((inp) => Promise.resolve(new JSONResponse(inp))); @@ -23,7 +23,7 @@ describe("createGuildRole", () => { await createGuildRole( dummyCreateBody, guildEnv, - "This is reson for this action" + "This is reason for this action" ); expect(global.fetch).toHaveBeenCalledWith( @@ -33,7 +33,7 @@ describe("createGuildRole", () => { headers: { "Content-Type": "application/json", Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`, - "X-Audit-Log-Reason": "This is reson for this action", + "X-Audit-Log-Reason": "This is reason for this action", }, body: JSON.stringify({ ...dummyCreateBody, @@ -98,7 +98,7 @@ describe("createGuildRole", () => { }); describe("addGroupRole", () => { - it("should pass the reson to discord as a X-Audit-Log-Reason header if provided", async () => { + it("should pass the reason to discord as a X-Audit-Log-Reason header if provided", async () => { const mockResponse = { ok: true, }; @@ -111,7 +111,7 @@ describe("addGroupRole", () => { const result = await addGroupRole( dummyAddRoleBody, guildEnv, - "This is a reson" + "This is a reason" ); expect(result).toEqual({ message: "Role added successfully" }); @@ -122,7 +122,7 @@ describe("addGroupRole", () => { headers: { "Content-Type": "application/json", Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`, - "X-Audit-Log-Reason": "This is a reson", + "X-Audit-Log-Reason": "This is a reason", }, } ); @@ -182,7 +182,7 @@ describe("removeGuildRole", () => { }, }); }); - test("should pass the reson to discord as a X-Audit-Log-Reason header if provided", async () => { + test("should pass the reason to discord as a X-Audit-Log-Reason header if provided", async () => { const mockResponse = { ok: true, }; @@ -194,7 +194,7 @@ describe("removeGuildRole", () => { const result = await removeGuildRole( dummyAddRoleBody, guildEnv, - "this is reson" + "this is reason" ); expect(global.fetch).toHaveBeenCalledWith( `https://discord.com/api/v10/guilds/${guildEnv.DISCORD_GUILD_ID}/members/${dummyAddRoleBody.userid}/roles/${dummyAddRoleBody.roleid}`, @@ -203,7 +203,7 @@ describe("removeGuildRole", () => { headers: { "Content-Type": "application/json", Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`, - "X-Audit-Log-Reason": "this is reson", + "X-Audit-Log-Reason": "this is reason", }, } ); diff --git a/tests/unit/utils/updateNickname.test.ts b/tests/unit/utils/updateNickname.test.ts index 6c24fb23..fea8abea 100644 --- a/tests/unit/utils/updateNickname.test.ts +++ b/tests/unit/utils/updateNickname.test.ts @@ -14,7 +14,7 @@ describe("Update nickname", () => { }; const mockData = { discordId: "12345678910111213", nickname: "jhon" }; - it("should pass the reson to discord as a X-Audit-Log-Reason header if provided", async () => { + it("should pass the reason to discord as a X-Audit-Log-Reason header if provided", async () => { jest .spyOn(global, "fetch") .mockRejectedValue(() => @@ -24,7 +24,7 @@ describe("Update nickname", () => { mockData.discordId, "", mockEnv, - "This is reson for this action" + "This is reason for this action" ); expect(global.fetch).toHaveBeenCalledWith( @@ -34,7 +34,7 @@ describe("Update nickname", () => { headers: { "Content-Type": "application/json", Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`, - "X-Audit-Log-Reason": "This is reson for this action", + "X-Audit-Log-Reason": "This is reason for this action", }, body: JSON.stringify({ nick: "" }), } From 9f293dc44eb5f8c4d07d3297ea7b1e2fbd04d950 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Sat, 13 Jan 2024 20:22:20 +0530 Subject: [PATCH 3/3] Fixed wrong change --- src/controllers/guildRoleHandler.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/controllers/guildRoleHandler.ts b/src/controllers/guildRoleHandler.ts index b4e33134..8bcce5b7 100644 --- a/src/controllers/guildRoleHandler.ts +++ b/src/controllers/guildRoleHandler.ts @@ -31,9 +31,7 @@ export async function createGuildRoleHandler(request: IRequest, env: env) { const res = await createGuildRole(body, env, reason); return new JSONResponse(res); } catch (err) { - return new JSONResponse({ - error: err, - }); + return new JSONResponse(response.BAD_SIGNATURE); } } export async function addGroupRoleHandler(request: IRequest, env: env) {