Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
10 changes: 10 additions & 0 deletions __mocks__/@tsndr/cloudflare-worker-jwt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

const jwt = Object.create({});

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

module.exports = jwt;
14 changes: 14 additions & 0 deletions __mocks__/@tsndr/cloudflare-worker-jwt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";
import jsonwebtoken from "jsonwebtoken";
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we need this package? It is not being used anywhere so why are we mocking it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes we did it afterwards

Copy link
Contributor Author

@shreya-mishra shreya-mishra Jul 20, 2023

Choose a reason for hiding this comment

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

check in latest commit


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

function sign() {
console.log("ASD");
return "asd";
}

// @ts-ignore
cloudflareWorkerJwt.sign = sign;

module.exports = 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
2 changes: 2 additions & 0 deletions src/constants/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ export const ROLE_ADDED = "Role added successfully";

export const NAME_CHANGED = "User nickname changed successfully";

export const VERIFICATION_STRING =
"Like to verify yourself? click the above link and authorize real dev squad to manage your discord data";
export const ROLE_REMOVED = "Role Removed successfully";
11 changes: 5 additions & 6 deletions src/controllers/verifyCommand.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import config from "../../config/config";
import { RETRY_COMMAND } from "../constants/responses";
import { RETRY_COMMAND, VERIFICATION_STRING } from "../constants/responses";
import { env } from "../typeDefinitions/default.types";
import { discordEphemeralResponse } from "../utils/discordEphemeralResponse";
import { generateUniqueToken } from "../utils/generateUniqueToken";
import { sendUserDiscordData } from "../utils/sendUserDiscordData";
import {sendUserDiscordData} from "../utils/sendUserDiscordData";

export async function verifyCommand(
userId: number,
Expand All @@ -13,9 +13,7 @@ export async function verifyCommand(
env: env
) {
const token = await generateUniqueToken();

const response = await sendUserDiscordData(
token,
const response = await sendUserDiscordData( token,
Copy link
Contributor

Choose a reason for hiding this comment

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

is there too much whitespace before token

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed automatically, via eslint

userId,
userAvatarHash,
userName,
Expand All @@ -24,7 +22,8 @@ export async function verifyCommand(
);
if (response?.status === 201 || response?.status === 200) {
const verificationSiteURL = config(env).VERIFICATION_SITE_URL;
const message = `${verificationSiteURL}/discord?token=${token}`;
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__
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove this blank file

Empty file.
Loading