Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
0a45049
adding tests for roles endpoint
bksh05 Jun 26, 2023
a1903ff
Feature: Create Discord API Endpoint to Remove Role from User (#82)
RitikJaiswal75 Jun 27, 2023
89d30d0
adding tests for roles endpoint
bksh05 Jun 26, 2023
5730062
[Feature] Add APIs for fetching role information
bksh05 Jun 29, 2023
34ca632
resolving merge conflicts
bksh05 Jun 29, 2023
1a07a23
removing unnecessary constants
bksh05 Jun 30, 2023
aaab327
Adding headers in response objects
bksh05 Jun 30, 2023
86c08ce
Adding extra try blocks in utils to handle errors
bksh05 Jul 1, 2023
7010760
Throwing errors when something goes wrong
bksh05 Jul 4, 2023
5800e73
moving test constants to fixtures
bksh05 Jul 4, 2023
abed9b9
changing error type thrown on bad request
bksh05 Jul 6, 2023
a4a190b
Using roles url of discord to get roles details
bksh05 Jul 9, 2023
7e9415e
Merge pull request #86 from bksh05/feature/endpoint-to-fetch-role-id
sahsisunny Jul 16, 2023
efcd4d0
Raising different PR for /verify string (#94)
shreya-mishra Jul 18, 2023
a504c1e
Merge branch 'main' into develop
RitikJaiswal75 Jul 19, 2023
cd7cf9a
Added description to the message after using /verify #85
shreya-mishra11 Jul 1, 2023
9e787d8
imported crypto
shreya-mishra11 Jul 14, 2023
c8076ed
Tried mocking jsonwebtoken, jwt
shreya-mishra11 Jul 14, 2023
6a988a2
add mock for @tsndr/cloudflare-worker-jwt and generateUniqueToken, fi…
prakashchoudhary07 Jul 17, 2023
51f8de0
remove jsonwebtoken from package.json
prakashchoudhary07 Jul 20, 2023
b117aea
reverted some extra changes
shreya-mishra11 Jul 21, 2023
6321376
resolved the comments
shreya-mishra11 Jul 21, 2023
6869787
fix: prettier issue
shreya-mishra11 Jul 21, 2023
7666e49
fix: prettier issue
shreya-mishra11 Jul 21, 2023
886a8f1
fix: prettier issue
shreya-mishra11 Jul 21, 2023
26229bf
lint fix
shreya-mishra11 Jul 21, 2023
2c34c7d
Issue#85 test (#98)
shreya-mishra Jul 22, 2023
515382d
updated verifications string
shreya-mishra11 Jul 31, 2023
80b324b
added the substring as well
shreya-mishra11 Jul 31, 2023
aad250c
Merge branch 'develop' into VerifyString
shreya-mishra Jul 31, 2023
d35ee36
format-fix
shreya-mishra11 Jul 31, 2023
d837511
fix te substring
shreya-mishra11 Aug 3, 2023
ba05f25
Merge pull request #101 from Real-Dev-Squad/VerifyString
shreya-mishra Aug 4, 2023
0f9db06
Merge branch 'main' into mergeIssueFix
lakshayman Aug 16, 2023
bfe661e
Merge pull request #114 from Real-Dev-Squad/mergeIssueFix
iamitprakash Aug 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions __mocks__/@tsndr/cloudflare-worker-jwt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const mockJwt = {
sign: jest.fn().mockImplementation(() => {
return "SIGNED_JWT";
}),
};

export default mockJwt;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"start": "wrangler dev",
"test": "echo 'running tests 🧪' && jest --coverage && echo '✅ All tests passed'",
"test-watch": "jest --watch",
"test-watch": "jest . --watch",
"login": "wrangler login",
"check": "wrangler whoami",
"deploy": "wrangler deploy",
Expand Down
5 changes: 4 additions & 1 deletion src/constants/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export const NAME_CHANGED = "User nickname changed successfully";
export const ROLE_REMOVED = "Role Removed successfully";

export const VERIFICATION_STRING =
"Please verify your identity by clicking the link above and granting authorization to the Real Dev Squad. This will allow us to manage your Discord data.";
"Please verify your discord account by clicking the link below 👇";

export const VERIFICATION_SUBSTRING =
"By granting authorization, you agree to permit us to manage your server nickname displayed ONLY in the Real Dev Squad server and to sync your joining data with your user account on our platform.";

export const ROLE_FETCH_FAILED =
"Oops! We are experiencing an issue fetching roles.";
Expand Down
8 changes: 6 additions & 2 deletions src/controllers/verifyCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import config from "../../config/config";
import { RETRY_COMMAND, VERIFICATION_STRING } from "../constants/responses";
import {
RETRY_COMMAND,
VERIFICATION_STRING,
VERIFICATION_SUBSTRING,
} from "../constants/responses";
import { env } from "../typeDefinitions/default.types";
import { discordEphemeralResponse } from "../utils/discordEphemeralResponse";
import { generateUniqueToken } from "../utils/generateUniqueToken";
Expand All @@ -24,7 +28,7 @@ export async function verifyCommand(
);
if (response?.status === 201 || response?.status === 200) {
const verificationSiteURL = config(env).VERIFICATION_SITE_URL;
const message = `${verificationSiteURL}/discord?token=${token}\n${VERIFICATION_STRING}`;
const message = `${VERIFICATION_STRING}\n${verificationSiteURL}/discord?token=${token}\n${VERIFICATION_SUBSTRING}`;
return discordEphemeralResponse(message);
} else {
return discordEphemeralResponse(RETRY_COMMAND);
Expand Down
19 changes: 19 additions & 0 deletions tests/fixtures/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,22 @@ export const rolesMock = [
mentionable: true,
},
];
export const mockDateNow = 1626512345678;
export const UNIQUE_TOKEN = "UNIQUE_TOKEN";
export const env = {
BOT_PUBLIC_KEY: "BOT_PUBLIC_KEY",
DISCORD_GUILD_ID: "DISCORD_GUILD_ID",
DISCORD_TOKEN: "SIGNED_JWT",
};

export const discordUserData = {
type: "discord",
token: UNIQUE_TOKEN,
attributes: {
discordId: 1,
userAvatar: "https://cdn.discordapp.com/avatars/1/userAvatarHash.jpg",
userName: "userName",
discriminator: "discriminator",
expiry: mockDateNow + 1000 * 60 * 2,
},
};
99 changes: 99 additions & 0 deletions tests/unit/handlers/verifyCommand.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import {
RETRY_COMMAND,
VERIFICATION_STRING,
VERIFICATION_SUBSTRING,
} from "../../../src/constants/responses";
import config from "../../../config/config";
import {
UNIQUE_TOKEN,
discordUserData,
env,
mockDateNow,
} from "../../fixtures/fixture";

describe("verifyCommand", () => {
beforeEach(() => {
jest.mock("@tsndr/cloudflare-worker-jwt");
jest.spyOn(Date, "now").mockReturnValue(mockDateNow);
jest.mock("../../../src/utils/generateUniqueToken", () => ({
generateUniqueToken: () => Promise.resolve(UNIQUE_TOKEN),
}));
});

afterEach(() => {
jest.spyOn(Date, "now").mockRestore();
});

test("should return JSON response when response is ok", async () => {
jest.spyOn(global, "fetch").mockResolvedValueOnce({
ok: true,
status: 200,
json: jest.fn().mockResolvedValueOnce(discordUserData),
} as unknown as Response);

const { verifyCommand } = await import(
"../../../src/controllers/verifyCommand"
);

const result = await verifyCommand(
1,
"userAvatarHash",
"userName",
"discriminator",
env
);

expect(global.fetch).toHaveBeenCalledWith(
`http://localhost:3000/external-accounts`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${env.DISCORD_TOKEN}`,
},
body: JSON.stringify(discordUserData),
}
);
const resultText = await result.text();
const resultData = JSON.parse(resultText);

const verificationSiteURL = config(env).VERIFICATION_SITE_URL;
const message = `${VERIFICATION_STRING}\n${verificationSiteURL}/discord?token=${UNIQUE_TOKEN}\n${VERIFICATION_SUBSTRING}`;
expect(resultData.data.content).toEqual(message);
});

test("should return INTERNAL_SERVER_ERROR when response is not ok", async () => {
jest.spyOn(global, "fetch").mockResolvedValueOnce({
ok: true,
status: 400, // ERROR STATUS
json: jest.fn().mockResolvedValueOnce(discordUserData),
} as unknown as Response);

const { verifyCommand } = await import(
"../../../src/controllers/verifyCommand"
);
const result = await verifyCommand(
1233434,
"sjkhdkjashdksjh",
"test user",
"sndbhsbgdj",
env
);

expect(global.fetch).toHaveBeenCalledWith(
`http://localhost:3000/external-accounts`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${env.DISCORD_TOKEN}`,
},
body: JSON.stringify(discordUserData),
}
);
const resultText = await result.text();
const resultData = JSON.parse(resultText);

expect(resultData.data.content).toEqual(RETRY_COMMAND);
});
});