Skip to content

Commit d9d2949

Browse files
Tried mocking jsonwebtoken, jwt
1 parent bdfabf9 commit d9d2949

File tree

7 files changed

+272
-3
lines changed

7 files changed

+272
-3
lines changed
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
3+
const jwt = Object.create({});
4+
5+
jwt.sign = jest.fn().mockImplementation(() => {
6+
console.log("MOCKED PACKAGED");
7+
return "asdasd";
8+
});
9+
10+
module.exports = jwt;
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
import jsonwebtoken from "jsonwebtoken";
3+
4+
const cloudflareWorkerJwt = jest.createMockFromModule("jsonwebtoken");
5+
6+
function sign() {
7+
console.log("ASD");
8+
return "asd";
9+
}
10+
11+
// @ts-ignore
12+
cloudflareWorkerJwt.sign = sign;
13+
14+
module.exports = cloudflareWorkerJwt;

package-lock.json

+132
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
"devDependencies": {
2323
"@cloudflare/workers-types": "^4.20221111.1",
2424
"@types/jest": "^29.2.5",
25+
"@types/jsonwebtoken": "^9.0.2",
2526
"@types/node": "^18.11.18",
2627
"@types/node-fetch": "^2.6.2",
2728
"@typescript-eslint/eslint-plugin": "^5.47.1",
2829
"@typescript-eslint/parser": "^5.47.1",
2930
"eslint": "^8.31.0",
3031
"jest": "^29.3.1",
32+
"jsonwebtoken": "^9.0.1",
3133
"ngrok": "^5.0.0-beta.2",
3234
"pre-commit": "^1.2.2",
3335
"prettier": "^2.8.1",

src/controllers/verifyCommand.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { RETRY_COMMAND, VERIFICATION_STRING } from "../constants/responses";
33
import { env } from "../typeDefinitions/default.types";
44
import { discordEphemeralResponse } from "../utils/discordEphemeralResponse";
55
import { generateUniqueToken } from "../utils/generateUniqueToken";
6-
import { sendUserDiscordData } from "../utils/sendUserDiscordData";
6+
import {sendUserDiscordData} from "../utils/sendUserDiscordData";
77

88
export async function verifyCommand(
99
userId: number,
@@ -13,8 +13,7 @@ export async function verifyCommand(
1313
env: env
1414
) {
1515
const token = await generateUniqueToken();
16-
const response = await sendUserDiscordData(
17-
token,
16+
const response = await sendUserDiscordData( token,
1817
userId,
1918
userAvatarHash,
2019
userName,

tests/mocks/__mocks__

Whitespace-only changes.
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import * as response from "../../../src/constants/responses";
2+
import JSONResponse from "../../../src/utils/JsonResponse";
3+
import { verifyCommand } from "../../../src/controllers/verifyCommand";
4+
import { guildEnv } from "../../fixtures/fixture";
5+
import config from "../../../config/config";
6+
7+
describe("verifyCommand", () => {
8+
test("should return INTERNAL_SERVER_ERROR when response is not ok", async () => {
9+
10+
jest.mock("crypto", () => {
11+
return {
12+
randomUUID: jest.fn(()=>'shreya'),
13+
subtle: { digest: jest.fn(() => "123") },
14+
};
15+
});
16+
17+
jest.mock("../../../src/utils/generateUniqueToken", () => ({
18+
generateUniqueToken: () => Promise.resolve("jashdkjahskajhd"),
19+
}));
20+
21+
// const mockResponse = response.INTERNAL_SERVER_ERROR;
22+
// jest
23+
// .spyOn(global, "fetch")
24+
// .mockImplementation(() =>
25+
// Promise.resolve(new JSONResponse(mockResponse))
26+
// );
27+
28+
const env = {
29+
BOT_PUBLIC_KEY: "xyz",
30+
DISCORD_GUILD_ID: "123",
31+
DISCORD_TOKEN: "abc",
32+
};
33+
34+
const data = {
35+
token: 1233434,
36+
userId: "sjkhdkjashdksjh",
37+
userAvatarHash: "test user",
38+
userName: "sndbhsbgdj",
39+
env: env,
40+
};
41+
42+
const result = await verifyCommand(
43+
1233434,
44+
"sjkhdkjashdksjh",
45+
"test user",
46+
"sndbhsbgdj",
47+
env
48+
);
49+
50+
// expect(result.data.content).toEqual(response.RETRY_COMMAND);
51+
expect(global.fetch).toHaveBeenCalledWith(
52+
`https://api.realdevsquad.com/external-accounts`,
53+
{
54+
method: "POST",
55+
headers: {
56+
"Content-Type": "application/json",
57+
Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`,
58+
},
59+
body: JSON.stringify(data),
60+
}
61+
);
62+
});
63+
64+
test("should return JSON response when response is ok", async () => {
65+
const mockResponse = {};
66+
67+
// jest
68+
// .spyOn(global, "fetch")
69+
// .mockImplementation(() =>
70+
// Promise.resolve(new JSONResponse(mockResponse))
71+
// );
72+
73+
const env = {
74+
BOT_PUBLIC_KEY: "xyz",
75+
DISCORD_GUILD_ID: "123",
76+
DISCORD_TOKEN: "abc",
77+
};
78+
79+
const data = {
80+
token: 1233434,
81+
userId: "sjkhdkjashdksjh",
82+
userAvatarHash: "test user",
83+
userName: "sndbhsbgdj",
84+
env: env,
85+
};
86+
87+
const result = await verifyCommand(
88+
1233434,
89+
"sjkhdkjashdksjh",
90+
"test user",
91+
"sndbhsbgdj",
92+
env
93+
);
94+
95+
const verificationSiteURL = config(env).VERIFICATION_SITE_URL;
96+
const message =
97+
`${verificationSiteURL}/discord?token=${guildEnv.DISCORD_TOKEN}\n` +
98+
response.VERIFICATION_STRING;
99+
100+
expect(global.fetch).toHaveBeenCalledWith(
101+
`https://api.realdevsquad.com/external-accounts`,
102+
{
103+
method: "POST",
104+
headers: {
105+
"Content-Type": "application/json",
106+
Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`,
107+
},
108+
body: JSON.stringify(data),
109+
}
110+
);
111+
});
112+
});

0 commit comments

Comments
 (0)