Skip to content

Raised New PR : (https://github.com/Real-Dev-Squad/discord-slash-commands/pull/98) Added description to the message after using /verify #85 #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
11 changes: 11 additions & 0 deletions __mocks__/@tsndr/cloudflare-worker-jwt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

import jest from 'jest';

const jwt = Object.create({});

jwt.sign = jest.fn().mockImplementation(() => {
return "asdasd";
});

export default jwt
15 changes: 15 additions & 0 deletions __mocks__/@tsndr/cloudflare-worker-jwt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

type cloudflareWorkerJwtType = {
sign: () => void;
};

const cloudflareWorkerJwt:cloudflareWorkerJwtType = jest.createMockFromModule("jsonwebtoken");

function sign() {
return "asd";
}

cloudflareWorkerJwt.sign = sign;

export default cloudflareWorkerJwt;
132 changes: 132 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
"devDependencies": {
"@cloudflare/workers-types": "^4.20221111.1",
"@types/jest": "^29.2.5",
"@types/jsonwebtoken": "^9.0.2",
"@types/node": "^18.11.18",
"@types/node-fetch": "^2.6.2",
"@typescript-eslint/eslint-plugin": "^5.47.1",
"@typescript-eslint/parser": "^5.47.1",
"eslint": "^8.31.0",
"jest": "^29.3.1",
"jsonwebtoken": "^9.0.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we installing the jsonwebToken package?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to mock cloudfareworker jwt

"ngrok": "^5.0.0-beta.2",
"pre-commit": "^1.2.2",
"prettier": "^2.8.1",
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/verifyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export async function verifyCommand(
env: env
) {
const token = await generateUniqueToken();

const response = await sendUserDiscordData(
token,
userId,
Expand All @@ -24,7 +23,9 @@ 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}`;

return discordEphemeralResponse(message);
} else {
return discordEphemeralResponse(RETRY_COMMAND);
Expand Down
1 change: 1 addition & 0 deletions src/utils/generateUniqueToken.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const crypto = require("crypto");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not require crypto as it is available globally from node 18

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without this line it was giving below error
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but now te issue is, this require is not allowed but when I am using import it gives the same error:/

export const generateUniqueToken = async () => {
const uuidToken = crypto.randomUUID();
const randomNumber = Math.floor(Math.random() * 1000000);
Expand Down
Empty file added tests/mocks/__mocks__
Empty file.
105 changes: 105 additions & 0 deletions tests/unit/handlers/verifyCommand.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import * as response from "../../../src/constants/responses";
import { verifyCommand } from "../../../src/controllers/verifyCommand";
import config from "../../../config/config";
import JSONResponse from "../../../src/utils/JsonResponse";

type Responsetype = {
data: DataType;
type: number;
};

type DataType = {
content: string;
flags: number;
};
describe("verifyCommand", () => {
test("should return INTERNAL_SERVER_ERROR when response is not ok", async () => {
jest.mock("crypto", () => {
return {
randomUUID: jest.fn(() => "shreya"),
subtle: { digest: jest.fn(() => "123") },
};
});

jest.mock("../../../src/utils/generateUniqueToken", () => ({
generateUniqueToken: () => Promise.resolve("jashdkjahskajhd"),
}));

const mockResponse = response.INTERNAL_SERVER_ERROR;
jest
.spyOn(global, "fetch")
.mockImplementation(() =>
Promise.resolve(new JSONResponse(mockResponse))
);

const env = {
BOT_PUBLIC_KEY: "xyz",
DISCORD_GUILD_ID: "123",
DISCORD_TOKEN: "abc",
};

const result = await verifyCommand(
1233434,
"sjkhdkjashdksjh",
"test user",
"sndbhsbgdj",
env
);

expect(global.fetch).toHaveBeenCalledWith(
`http://localhost:3000/external-accounts`,
expect.objectContaining({
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer asd`,
},
})
);

const response_: Responsetype = await result.json();
expect(response_.data.content).toContain("");
});

test("should return JSON response when response is ok", async () => {
const mockResponse = {};

jest
.spyOn(global, "fetch")
.mockImplementation(() =>
Promise.resolve(new JSONResponse(mockResponse))
);

const env = {
BOT_PUBLIC_KEY: "xyz",
DISCORD_GUILD_ID: "123",
DISCORD_TOKEN: "abc",
};

const result = await verifyCommand(
1233434,
"sjkhdkjashdksjh",
"test user",
"sndbhsbgdj",
env
);

const verificationSiteURL = config(env).VERIFICATION_SITE_URL;
const message = `${verificationSiteURL}/discord?token=`;

expect(global.fetch).toHaveBeenCalledWith(
`http://localhost:3000/external-accounts`,
expect.objectContaining({
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer asd`,
},
})
);

const response_: Responsetype = await result.json();

expect(response_.data.content).toContain(message);
});
});