generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added AWS env variable in the readme file (#295) * feat: Added tests to support onboarding-extension-command - Added command and handler - Changed required default value - Added constant message - Changed field name and argument - Fixed function argument and types - Added super user field - Added validation for super-user and fixed reply - Changed field, expectation message and test name as it was failing - Restore all mocks in after-each hook - Fixed lint issue - Added assert statement to verify the function call signature and fixed test name - Added tests to handle edge cases and fixed existing failing tests --------- Co-authored-by: Vikhyat Bhatnagar <52795644+vikhyat187@users.noreply.github.com> Co-authored-by: Pankaj Sha <pankajshadev@gmail.com> Co-authored-by: Amit Prakash <34869115+iamitprakash@users.noreply.github.com>
- Loading branch information
Showing
13 changed files
with
424 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { env } from "../typeDefinitions/default.types"; | ||
import { | ||
messageRequestDataOptions, | ||
messageRequestMember, | ||
} from "../typeDefinitions/discordMessage.types"; | ||
import { DevFlag } from "../typeDefinitions/filterUsersByRole"; | ||
import { discordTextResponse } from "../utils/discordResponse"; | ||
import { | ||
createOnboardingExtension, | ||
CreateOnboardingExtensionArgs, | ||
} from "../utils/onboardingExtension"; | ||
|
||
export async function onboardingExtensionCommand( | ||
transformedArgument: { | ||
memberObj: messageRequestMember; | ||
userIdObj?: messageRequestDataOptions; | ||
numberOfDaysObj: messageRequestDataOptions; | ||
reasonObj: messageRequestDataOptions; | ||
channelId: number; | ||
devObj?: DevFlag; | ||
}, | ||
env: env, | ||
ctx: ExecutionContext | ||
) { | ||
const dev = transformedArgument.devObj?.value || false; | ||
const discordId = transformedArgument.memberObj.user.id.toString(); | ||
|
||
if (!dev) { | ||
return discordTextResponse(`<@${discordId}> Feature not implemented`); | ||
} | ||
|
||
const args: CreateOnboardingExtensionArgs = { | ||
channelId: transformedArgument.channelId, | ||
userId: transformedArgument.userIdObj?.value, | ||
numberOfDays: Number(transformedArgument.numberOfDaysObj.value), | ||
reason: transformedArgument.reasonObj.value, | ||
discordId: discordId, | ||
}; | ||
|
||
const initialResponse = `<@${discordId}> Processing your request for onboarding extension`; | ||
|
||
ctx.waitUntil(createOnboardingExtension(args, env)); | ||
|
||
return discordTextResponse(initialResponse); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import config from "../../config/config"; | ||
import { UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST } from "../constants/responses"; | ||
import { DISCORD_BASE_URL } from "../constants/urls"; | ||
import { env } from "../typeDefinitions/default.types"; | ||
import { generateDiscordAuthToken } from "./authTokenGenerator"; | ||
import { getUserDetails } from "./getUserDetails"; | ||
import { sendReplyInDiscordChannel } from "./sendReplyInDiscordChannel"; | ||
|
||
export type CreateOnboardingExtensionArgs = { | ||
userId?: string; | ||
channelId: number; | ||
reason: string; | ||
numberOfDays: number; | ||
discordId: string; | ||
}; | ||
|
||
export const createOnboardingExtension = async ( | ||
args: CreateOnboardingExtensionArgs, | ||
env: env | ||
) => { | ||
const { channelId } = args; | ||
|
||
const authToken = await generateDiscordAuthToken( | ||
"Cloudflare Worker", | ||
Math.floor(Date.now() / 1000) + 2, | ||
env.BOT_PRIVATE_KEY, | ||
"RS256" | ||
); | ||
|
||
let content: string; | ||
const discordReplyUrl = `${DISCORD_BASE_URL}/channels/${channelId}/messages`; | ||
|
||
if (args.userId && args.discordId !== args.userId) { | ||
const userResponse = await getUserDetails(args.discordId); | ||
if (!userResponse?.user?.roles?.super_user) { | ||
content = `<@${args.discordId}> ${UNAUTHORIZED_TO_CREATE_ONBOARDING_EXTENSION_REQUEST}`; | ||
return await sendReplyInDiscordChannel(discordReplyUrl, content, env); | ||
} | ||
} | ||
|
||
const userDiscordId = args.userId ? args.userId : args.discordId; | ||
const base_url = config(env).RDS_BASE_API_URL; | ||
const createOnboardingExtensionUrl = `${base_url}/requests?dev=true`; | ||
|
||
const requestBody = { | ||
userId: userDiscordId, | ||
type: "ONBOARDING", | ||
numberOfDays: args.numberOfDays, | ||
reason: args.reason, | ||
}; | ||
|
||
try { | ||
const response = await fetch(createOnboardingExtensionUrl, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${authToken}`, | ||
}, | ||
body: JSON.stringify(requestBody), | ||
}); | ||
const jsonResponse = (await response.json()) as unknown as { | ||
message: string; | ||
}; | ||
content = `<@${args.discordId}> ${jsonResponse.message}`; | ||
return await sendReplyInDiscordChannel(discordReplyUrl, content, env); | ||
} catch (err) { | ||
content = `<@${args.discordId}> Error occurred while creating onboarding extension request.`; | ||
return await sendReplyInDiscordChannel(discordReplyUrl, content, env); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { env } from "../typeDefinitions/default.types"; | ||
|
||
export const sendReplyInDiscordChannel = async ( | ||
discordReplyUrl: string, | ||
body: any, | ||
env: env | ||
) => { | ||
await fetch(discordReplyUrl, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bot ${env.DISCORD_TOKEN}`, | ||
}, | ||
body: JSON.stringify({ | ||
content: body, | ||
}), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { onboardingExtensionCommand } from "../../../src/controllers/onboardingExtensionCommand"; | ||
import { discordTextResponse } from "../../../src/utils/discordResponse"; | ||
import { | ||
ctx, | ||
guildEnv, | ||
transformedArgsForOnboardingExtension, | ||
} from "../../fixtures/fixture"; | ||
import * as utils from "../../../src/utils/onboardingExtension"; | ||
|
||
describe("onboardingExtensionCommand", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
const discordId = | ||
transformedArgsForOnboardingExtension.memberObj.user.id.toString(); | ||
|
||
it("should return Feature not implemented", async () => { | ||
const expectedRes = await onboardingExtensionCommand( | ||
transformedArgsForOnboardingExtension, | ||
guildEnv, | ||
ctx | ||
); | ||
const jsonResponse = await expectedRes.json(); | ||
const mockResponse = discordTextResponse( | ||
`<@${discordId}> Feature not implemented` | ||
); | ||
const mockJsonResponse = await mockResponse.json(); | ||
expect(jsonResponse).toStrictEqual(mockJsonResponse); | ||
}); | ||
|
||
it("should return initial response", async () => { | ||
transformedArgsForOnboardingExtension.devObj.value = true; | ||
const expectedRes = await onboardingExtensionCommand( | ||
transformedArgsForOnboardingExtension, | ||
guildEnv, | ||
ctx | ||
); | ||
const jsonResponse = await expectedRes.json(); | ||
const mockResponse = discordTextResponse( | ||
`<@${discordId}> Processing your request for onboarding extension` | ||
); | ||
const mockJsonResponse = await mockResponse.json(); | ||
expect(jsonResponse).toStrictEqual(mockJsonResponse); | ||
}); | ||
|
||
it("should call createOnboardingExtension", async () => { | ||
jest.spyOn(utils, "createOnboardingExtension"); | ||
transformedArgsForOnboardingExtension.devObj.value = true; | ||
await onboardingExtensionCommand( | ||
transformedArgsForOnboardingExtension, | ||
guildEnv, | ||
ctx | ||
); | ||
expect(utils.createOnboardingExtension).toHaveBeenCalledTimes(1); | ||
expect(utils.createOnboardingExtension).toHaveBeenCalledWith( | ||
{ | ||
channelId: transformedArgsForOnboardingExtension.channelId, | ||
userId: transformedArgsForOnboardingExtension.userIdObj?.value, | ||
numberOfDays: Number( | ||
transformedArgsForOnboardingExtension.numberOfDaysObj.value | ||
), | ||
reason: transformedArgsForOnboardingExtension.reasonObj.value, | ||
discordId: discordId, | ||
}, | ||
guildEnv | ||
); | ||
}); | ||
}); |
Oops, something went wrong.