Skip to content

Commit 2c8f2b7

Browse files
authored
Merge pull request #102 from Real-Dev-Squad/develop
Dev to Main Sync
2 parents b12b711 + bfe661e commit 2c8f2b7

File tree

6 files changed

+136
-4
lines changed

6 files changed

+136
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const mockJwt = {
2+
sign: jest.fn().mockImplementation(() => {
3+
return "SIGNED_JWT";
4+
}),
5+
};
6+
7+
export default mockJwt;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"start": "wrangler dev",
77
"test": "echo 'running tests 🧪' && jest --coverage && echo '✅ All tests passed'",
8-
"test-watch": "jest --watch",
8+
"test-watch": "jest . --watch",
99
"login": "wrangler login",
1010
"check": "wrangler whoami",
1111
"deploy": "wrangler deploy",

src/constants/responses.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ export const NAME_CHANGED = "User nickname changed successfully";
2929
export const ROLE_REMOVED = "Role Removed successfully";
3030

3131
export const VERIFICATION_STRING =
32-
"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.";
32+
"Please verify your discord account by clicking the link below 👇";
33+
34+
export const VERIFICATION_SUBSTRING =
35+
"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.";
3336

3437
export const ROLE_FETCH_FAILED =
3538
"Oops! We are experiencing an issue fetching roles.";

src/controllers/verifyCommand.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import config from "../../config/config";
2-
import { RETRY_COMMAND, VERIFICATION_STRING } from "../constants/responses";
2+
import {
3+
RETRY_COMMAND,
4+
VERIFICATION_STRING,
5+
VERIFICATION_SUBSTRING,
6+
} from "../constants/responses";
37
import { env } from "../typeDefinitions/default.types";
48
import { discordEphemeralResponse } from "../utils/discordEphemeralResponse";
59
import { generateUniqueToken } from "../utils/generateUniqueToken";
@@ -24,7 +28,7 @@ export async function verifyCommand(
2428
);
2529
if (response?.status === 201 || response?.status === 200) {
2630
const verificationSiteURL = config(env).VERIFICATION_SITE_URL;
27-
const message = `${verificationSiteURL}/discord?token=${token}\n${VERIFICATION_STRING}`;
31+
const message = `${VERIFICATION_STRING}\n${verificationSiteURL}/discord?token=${token}\n${VERIFICATION_SUBSTRING}`;
2832
return discordEphemeralResponse(message);
2933
} else {
3034
return discordEphemeralResponse(RETRY_COMMAND);

tests/fixtures/fixture.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,22 @@ export const rolesMock = [
135135
mentionable: true,
136136
},
137137
];
138+
export const mockDateNow = 1626512345678;
139+
export const UNIQUE_TOKEN = "UNIQUE_TOKEN";
140+
export const env = {
141+
BOT_PUBLIC_KEY: "BOT_PUBLIC_KEY",
142+
DISCORD_GUILD_ID: "DISCORD_GUILD_ID",
143+
DISCORD_TOKEN: "SIGNED_JWT",
144+
};
145+
146+
export const discordUserData = {
147+
type: "discord",
148+
token: UNIQUE_TOKEN,
149+
attributes: {
150+
discordId: 1,
151+
userAvatar: "https://cdn.discordapp.com/avatars/1/userAvatarHash.jpg",
152+
userName: "userName",
153+
discriminator: "discriminator",
154+
expiry: mockDateNow + 1000 * 60 * 2,
155+
},
156+
};
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import {
2+
RETRY_COMMAND,
3+
VERIFICATION_STRING,
4+
VERIFICATION_SUBSTRING,
5+
} from "../../../src/constants/responses";
6+
import config from "../../../config/config";
7+
import {
8+
UNIQUE_TOKEN,
9+
discordUserData,
10+
env,
11+
mockDateNow,
12+
} from "../../fixtures/fixture";
13+
14+
describe("verifyCommand", () => {
15+
beforeEach(() => {
16+
jest.mock("@tsndr/cloudflare-worker-jwt");
17+
jest.spyOn(Date, "now").mockReturnValue(mockDateNow);
18+
jest.mock("../../../src/utils/generateUniqueToken", () => ({
19+
generateUniqueToken: () => Promise.resolve(UNIQUE_TOKEN),
20+
}));
21+
});
22+
23+
afterEach(() => {
24+
jest.spyOn(Date, "now").mockRestore();
25+
});
26+
27+
test("should return JSON response when response is ok", async () => {
28+
jest.spyOn(global, "fetch").mockResolvedValueOnce({
29+
ok: true,
30+
status: 200,
31+
json: jest.fn().mockResolvedValueOnce(discordUserData),
32+
} as unknown as Response);
33+
34+
const { verifyCommand } = await import(
35+
"../../../src/controllers/verifyCommand"
36+
);
37+
38+
const result = await verifyCommand(
39+
1,
40+
"userAvatarHash",
41+
"userName",
42+
"discriminator",
43+
env
44+
);
45+
46+
expect(global.fetch).toHaveBeenCalledWith(
47+
`http://localhost:3000/external-accounts`,
48+
{
49+
method: "POST",
50+
headers: {
51+
"Content-Type": "application/json",
52+
Authorization: `Bearer ${env.DISCORD_TOKEN}`,
53+
},
54+
body: JSON.stringify(discordUserData),
55+
}
56+
);
57+
const resultText = await result.text();
58+
const resultData = JSON.parse(resultText);
59+
60+
const verificationSiteURL = config(env).VERIFICATION_SITE_URL;
61+
const message = `${VERIFICATION_STRING}\n${verificationSiteURL}/discord?token=${UNIQUE_TOKEN}\n${VERIFICATION_SUBSTRING}`;
62+
expect(resultData.data.content).toEqual(message);
63+
});
64+
65+
test("should return INTERNAL_SERVER_ERROR when response is not ok", async () => {
66+
jest.spyOn(global, "fetch").mockResolvedValueOnce({
67+
ok: true,
68+
status: 400, // ERROR STATUS
69+
json: jest.fn().mockResolvedValueOnce(discordUserData),
70+
} as unknown as Response);
71+
72+
const { verifyCommand } = await import(
73+
"../../../src/controllers/verifyCommand"
74+
);
75+
const result = await verifyCommand(
76+
1233434,
77+
"sjkhdkjashdksjh",
78+
"test user",
79+
"sndbhsbgdj",
80+
env
81+
);
82+
83+
expect(global.fetch).toHaveBeenCalledWith(
84+
`http://localhost:3000/external-accounts`,
85+
{
86+
method: "POST",
87+
headers: {
88+
"Content-Type": "application/json",
89+
Authorization: `Bearer ${env.DISCORD_TOKEN}`,
90+
},
91+
body: JSON.stringify(discordUserData),
92+
}
93+
);
94+
const resultText = await result.text();
95+
const resultData = JSON.parse(resultText);
96+
97+
expect(resultData.data.content).toEqual(RETRY_COMMAND);
98+
});
99+
});

0 commit comments

Comments
 (0)