Skip to content

Commit 2c34c7d

Browse files
Issue#85 test (#98)
* Added description to the message after using /verify #85 * imported crypto * Tried mocking jsonwebtoken, jwt * add mock for @tsndr/cloudflare-worker-jwt and generateUniqueToken, fix test cases * remove jsonwebtoken from package.json * reverted some extra changes * resolved the comments * fix: prettier issue * fix: prettier issue * fix: prettier issue * lint fix --------- Co-authored-by: Prakash <[email protected]>
1 parent a504c1e commit 2c34c7d

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-1
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",

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

0 commit comments

Comments
 (0)