From aca37fad0fc58f5e735ff37c6b3789da0e16eba5 Mon Sep 17 00:00:00 2001 From: shreya-mishra Date: Sat, 1 Jul 2023 12:35:43 +0530 Subject: [PATCH 1/9] Added description to the message after using /verify #85 --- src/controllers/verifyCommand.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/controllers/verifyCommand.ts b/src/controllers/verifyCommand.ts index f5816cb0..d971be3f 100644 --- a/src/controllers/verifyCommand.ts +++ b/src/controllers/verifyCommand.ts @@ -13,6 +13,8 @@ export async function verifyCommand( env: env ) { const token = await generateUniqueToken(); + const verificationString = + "Like to verify yourself? click the above link and authorize real dev squad to manage your discord data"; const response = await sendUserDiscordData( token, @@ -24,7 +26,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` + verificationString; return discordEphemeralResponse(message); } else { return discordEphemeralResponse(RETRY_COMMAND); From f95a2997faf5b65f6ff25a898a9d01aabfe0eca7 Mon Sep 17 00:00:00 2001 From: shreya-mishra Date: Sat, 1 Jul 2023 16:49:00 +0530 Subject: [PATCH 2/9] moved string to constant file --- src/constants/responses.ts | 3 +++ src/constants/urls.ts | 8 +++++--- src/controllers/verifyCommand.ts | 7 ++----- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/constants/responses.ts b/src/constants/responses.ts index 8f3832bf..4ba36242 100644 --- a/src/constants/responses.ts +++ b/src/constants/responses.ts @@ -25,3 +25,6 @@ export const RETRY_COMMAND = 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"; diff --git a/src/constants/urls.ts b/src/constants/urls.ts index beb0f2ba..475333b5 100644 --- a/src/constants/urls.ts +++ b/src/constants/urls.ts @@ -1,6 +1,8 @@ -export const RDS_BASE_API_URL = "https://api.realdevsquad.com"; -export const RDS_BASE_STAGING_API_URL = "https://staging-api.realdevsquad.com"; -export const RDS_BASE_DEVELOPMENT_API_URL = "YOUR_LOCAL_API_URL"; // If needed, modify the URL to your local API server +export const RDS_BASE_API_URL = "https://66ea-103-77-42-188.ngrok-free.app"; +export const RDS_BASE_STAGING_API_URL = + "https://66ea-103-77-42-188.ngrok-free.app"; +export const RDS_BASE_DEVELOPMENT_API_URL = + "https://66ea-103-77-42-188.ngrok-free.app"; // If needed, modify the URL to your local API server export const DISCORD_BASE_URL = "https://discord.com/api/v10"; export const DISCORD_AVATAR_BASE_URL = "https://cdn.discordapp.com/avatars"; diff --git a/src/controllers/verifyCommand.ts b/src/controllers/verifyCommand.ts index d971be3f..0d892a52 100644 --- a/src/controllers/verifyCommand.ts +++ b/src/controllers/verifyCommand.ts @@ -1,5 +1,5 @@ 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"; @@ -13,9 +13,6 @@ export async function verifyCommand( env: env ) { const token = await generateUniqueToken(); - const verificationString = - "Like to verify yourself? click the above link and authorize real dev squad to manage your discord data"; - const response = await sendUserDiscordData( token, userId, @@ -27,7 +24,7 @@ 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` + verificationString; + `${verificationSiteURL}/discord?token=${token}\n` + VERIFICATION_STRING; return discordEphemeralResponse(message); } else { return discordEphemeralResponse(RETRY_COMMAND); From bdfabf9b633429182c5a0792f9d0dcab830c5d88 Mon Sep 17 00:00:00 2001 From: shreya-mishra Date: Sat, 15 Jul 2023 01:57:01 +0530 Subject: [PATCH 3/9] imported crypto --- src/utils/generateUniqueToken.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils/generateUniqueToken.ts b/src/utils/generateUniqueToken.ts index d7fa803c..cdfbf4ec 100644 --- a/src/utils/generateUniqueToken.ts +++ b/src/utils/generateUniqueToken.ts @@ -1,3 +1,4 @@ +const crypto = require("crypto"); export const generateUniqueToken = async () => { const uuidToken = crypto.randomUUID(); const randomNumber = Math.floor(Math.random() * 1000000); From d9d294972036a122c2092d8cb8851c3f8f1d05fd Mon Sep 17 00:00:00 2001 From: shreya-mishra Date: Sat, 15 Jul 2023 01:58:23 +0530 Subject: [PATCH 4/9] Tried mocking jsonwebtoken, jwt --- __mocks__/@tsndr/cloudflare-worker-jwt.js | 10 ++ __mocks__/@tsndr/cloudflare-worker-jwt.ts | 14 +++ package-lock.json | 132 ++++++++++++++++++++++ package.json | 2 + src/controllers/verifyCommand.ts | 5 +- tests/mocks/__mocks__ | 0 tests/unit/handlers/verifyCommand.test.ts | 112 ++++++++++++++++++ 7 files changed, 272 insertions(+), 3 deletions(-) create mode 100644 __mocks__/@tsndr/cloudflare-worker-jwt.js create mode 100644 __mocks__/@tsndr/cloudflare-worker-jwt.ts create mode 100644 tests/mocks/__mocks__ create mode 100644 tests/unit/handlers/verifyCommand.test.ts diff --git a/__mocks__/@tsndr/cloudflare-worker-jwt.js b/__mocks__/@tsndr/cloudflare-worker-jwt.js new file mode 100644 index 00000000..8e14ab3f --- /dev/null +++ b/__mocks__/@tsndr/cloudflare-worker-jwt.js @@ -0,0 +1,10 @@ +"use strict"; + +const jwt = Object.create({}); + +jwt.sign = jest.fn().mockImplementation(() => { + console.log("MOCKED PACKAGED"); + return "asdasd"; +}); + +module.exports = jwt; diff --git a/__mocks__/@tsndr/cloudflare-worker-jwt.ts b/__mocks__/@tsndr/cloudflare-worker-jwt.ts new file mode 100644 index 00000000..6bf409e1 --- /dev/null +++ b/__mocks__/@tsndr/cloudflare-worker-jwt.ts @@ -0,0 +1,14 @@ +"use strict"; +import jsonwebtoken from "jsonwebtoken"; + +const cloudflareWorkerJwt = jest.createMockFromModule("jsonwebtoken"); + +function sign() { + console.log("ASD"); + return "asd"; +} + +// @ts-ignore +cloudflareWorkerJwt.sign = sign; + +module.exports = cloudflareWorkerJwt; diff --git a/package-lock.json b/package-lock.json index 7f88fd0c..0e1e8e09 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,12 +18,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", "ngrok": "^5.0.0-beta.2", "pre-commit": "^1.2.2", "prettier": "^2.8.1", @@ -1880,6 +1882,15 @@ "integrity": "sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==", "dev": true }, + "node_modules/@types/jsonwebtoken": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/@types/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", + "integrity": "sha512-drE6uz7QBKq1fYqqoFKTDRdFCPHd5TCub75BM+D+cMx7NU9hUz7SESLfC2fSCXVFMO5Yj8sOWHuGqPgjc+fz0Q==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/keyv": { "version": "3.1.4", "resolved": "https://registry.npmjs.org/@types/keyv/-/keyv-3.1.4.tgz", @@ -2571,6 +2582,12 @@ "node": "*" } }, + "node_modules/buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==", + "dev": true + }, "node_modules/buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", @@ -3073,6 +3090,15 @@ "node": ">=12" } }, + "node_modules/ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "dev": true, + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, "node_modules/electron-to-chromium": { "version": "1.4.284", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.284.tgz", @@ -4952,6 +4978,43 @@ "node": ">=6" } }, + "node_modules/jsonwebtoken": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.1.tgz", + "integrity": "sha512-K8wx7eJ5TPvEjuiVSkv167EVboBDv9PZdDoF7BgeQnBLVvZWW9clr2PsQHVJDTKaEIH5JBIwHujGcHp7GgI2eg==", + "dev": true, + "dependencies": { + "jws": "^3.2.2", + "lodash": "^4.17.21", + "ms": "^2.1.1", + "semver": "^7.3.8" + }, + "engines": { + "node": ">=12", + "npm": ">=6" + } + }, + "node_modules/jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "dev": true, + "dependencies": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "dev": true, + "dependencies": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "node_modules/keyv": { "version": "4.5.2", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.2.tgz", @@ -5013,6 +5076,12 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true + }, "node_modules/lodash.clonedeep": { "version": "4.5.0", "resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz", @@ -8372,6 +8441,15 @@ "integrity": "sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==", "dev": true }, + "@types/jsonwebtoken": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/@types/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", + "integrity": "sha512-drE6uz7QBKq1fYqqoFKTDRdFCPHd5TCub75BM+D+cMx7NU9hUz7SESLfC2fSCXVFMO5Yj8sOWHuGqPgjc+fz0Q==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/keyv": { "version": "3.1.4", "resolved": "https://registry.npmjs.org/@types/keyv/-/keyv-3.1.4.tgz", @@ -8855,6 +8933,12 @@ "integrity": "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==", "dev": true }, + "buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==", + "dev": true + }, "buffer-from": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", @@ -9219,6 +9303,15 @@ "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", "integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==" }, + "ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "dev": true, + "requires": { + "safe-buffer": "^5.0.1" + } + }, "electron-to-chromium": { "version": "1.4.284", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.284.tgz", @@ -10641,6 +10734,39 @@ "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true }, + "jsonwebtoken": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.1.tgz", + "integrity": "sha512-K8wx7eJ5TPvEjuiVSkv167EVboBDv9PZdDoF7BgeQnBLVvZWW9clr2PsQHVJDTKaEIH5JBIwHujGcHp7GgI2eg==", + "dev": true, + "requires": { + "jws": "^3.2.2", + "lodash": "^4.17.21", + "ms": "^2.1.1", + "semver": "^7.3.8" + } + }, + "jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "dev": true, + "requires": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "dev": true, + "requires": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, "keyv": { "version": "4.5.2", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.2.tgz", @@ -10687,6 +10813,12 @@ "p-locate": "^5.0.0" } }, + "lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true + }, "lodash.clonedeep": { "version": "4.5.0", "resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz", diff --git a/package.json b/package.json index c2b1425e..3230da25 100644 --- a/package.json +++ b/package.json @@ -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", "ngrok": "^5.0.0-beta.2", "pre-commit": "^1.2.2", "prettier": "^2.8.1", diff --git a/src/controllers/verifyCommand.ts b/src/controllers/verifyCommand.ts index 0d892a52..80cd8d54 100644 --- a/src/controllers/verifyCommand.ts +++ b/src/controllers/verifyCommand.ts @@ -3,7 +3,7 @@ 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, @@ -13,8 +13,7 @@ export async function verifyCommand( env: env ) { const token = await generateUniqueToken(); - const response = await sendUserDiscordData( - token, + const response = await sendUserDiscordData( token, userId, userAvatarHash, userName, diff --git a/tests/mocks/__mocks__ b/tests/mocks/__mocks__ new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/handlers/verifyCommand.test.ts b/tests/unit/handlers/verifyCommand.test.ts new file mode 100644 index 00000000..6e461c8a --- /dev/null +++ b/tests/unit/handlers/verifyCommand.test.ts @@ -0,0 +1,112 @@ +import * as response from "../../../src/constants/responses"; +import JSONResponse from "../../../src/utils/JsonResponse"; +import { verifyCommand } from "../../../src/controllers/verifyCommand"; +import { guildEnv } from "../../fixtures/fixture"; +import config from "../../../config/config"; + +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 data = { + token: 1233434, + userId: "sjkhdkjashdksjh", + userAvatarHash: "test user", + userName: "sndbhsbgdj", + env: env, + }; + + const result = await verifyCommand( + 1233434, + "sjkhdkjashdksjh", + "test user", + "sndbhsbgdj", + env + ); + + // expect(result.data.content).toEqual(response.RETRY_COMMAND); + expect(global.fetch).toHaveBeenCalledWith( + `https://api.realdevsquad.com/external-accounts`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`, + }, + body: JSON.stringify(data), + } + ); + }); + + 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 data = { + token: 1233434, + userId: "sjkhdkjashdksjh", + userAvatarHash: "test user", + userName: "sndbhsbgdj", + env: env, + }; + + const result = await verifyCommand( + 1233434, + "sjkhdkjashdksjh", + "test user", + "sndbhsbgdj", + env + ); + + const verificationSiteURL = config(env).VERIFICATION_SITE_URL; + const message = + `${verificationSiteURL}/discord?token=${guildEnv.DISCORD_TOKEN}\n` + + response.VERIFICATION_STRING; + + expect(global.fetch).toHaveBeenCalledWith( + `https://api.realdevsquad.com/external-accounts`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`, + }, + body: JSON.stringify(data), + } + ); + }); +}); From 63e3d5037f5a8ae7a51724ad39b261b1a6190934 Mon Sep 17 00:00:00 2001 From: shreya-mishra Date: Tue, 18 Jul 2023 23:02:25 +0530 Subject: [PATCH 5/9] resolved comment --- src/controllers/verifyCommand.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/verifyCommand.ts b/src/controllers/verifyCommand.ts index 80cd8d54..d631d0a6 100644 --- a/src/controllers/verifyCommand.ts +++ b/src/controllers/verifyCommand.ts @@ -23,7 +23,7 @@ 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; + `${verificationSiteURL}/discord?token=${token}\n${VERIFICATION_STRING}`; return discordEphemeralResponse(message); } else { return discordEphemeralResponse(RETRY_COMMAND); From 954ad0f33523aa59621bf514560b8a9c747f60ea Mon Sep 17 00:00:00 2001 From: shreya-mishra Date: Wed, 19 Jul 2023 06:30:40 +0530 Subject: [PATCH 6/9] fix: lint issue --- __mocks__/@tsndr/cloudflare-worker-jwt.js | 5 +++-- __mocks__/@tsndr/cloudflare-worker-jwt.ts | 11 ++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/__mocks__/@tsndr/cloudflare-worker-jwt.js b/__mocks__/@tsndr/cloudflare-worker-jwt.js index 8e14ab3f..d0b39154 100644 --- a/__mocks__/@tsndr/cloudflare-worker-jwt.js +++ b/__mocks__/@tsndr/cloudflare-worker-jwt.js @@ -1,10 +1,11 @@ "use strict"; +import jest from 'jest'; + const jwt = Object.create({}); jwt.sign = jest.fn().mockImplementation(() => { - console.log("MOCKED PACKAGED"); return "asdasd"; }); -module.exports = jwt; +export default jwt \ No newline at end of file diff --git a/__mocks__/@tsndr/cloudflare-worker-jwt.ts b/__mocks__/@tsndr/cloudflare-worker-jwt.ts index 6bf409e1..c12cedbd 100644 --- a/__mocks__/@tsndr/cloudflare-worker-jwt.ts +++ b/__mocks__/@tsndr/cloudflare-worker-jwt.ts @@ -1,14 +1,15 @@ "use strict"; -import jsonwebtoken from "jsonwebtoken"; -const cloudflareWorkerJwt = jest.createMockFromModule("jsonwebtoken"); +type cloudflareWorkerJwtType = { + sign: () => void; +}; + +const cloudflareWorkerJwt:cloudflareWorkerJwtType = jest.createMockFromModule("jsonwebtoken"); function sign() { - console.log("ASD"); return "asd"; } -// @ts-ignore cloudflareWorkerJwt.sign = sign; -module.exports = cloudflareWorkerJwt; +export default cloudflareWorkerJwt; From 5cf1c5582beba4f379bd9011dc0fa8dd8d42efab Mon Sep 17 00:00:00 2001 From: shreya-mishra Date: Thu, 20 Jul 2023 23:55:26 +0530 Subject: [PATCH 7/9] resolved comments --- __mocks__/@tsndr/cloudflare-worker-jwt.js | 5 +- tests/unit/handlers/verifyCommand.test.ts | 67 +++++++++++++---------- 2 files changed, 41 insertions(+), 31 deletions(-) diff --git a/__mocks__/@tsndr/cloudflare-worker-jwt.js b/__mocks__/@tsndr/cloudflare-worker-jwt.js index d0b39154..3e9d994c 100644 --- a/__mocks__/@tsndr/cloudflare-worker-jwt.js +++ b/__mocks__/@tsndr/cloudflare-worker-jwt.js @@ -1,6 +1,7 @@ "use strict"; -import jest from 'jest'; +// import jest from 'jest'; +const jest = require('jest') const jwt = Object.create({}); @@ -8,4 +9,4 @@ jwt.sign = jest.fn().mockImplementation(() => { return "asdasd"; }); -export default jwt \ No newline at end of file +module.exports = jwt \ No newline at end of file diff --git a/tests/unit/handlers/verifyCommand.test.ts b/tests/unit/handlers/verifyCommand.test.ts index 6e461c8a..c88c47a9 100644 --- a/tests/unit/handlers/verifyCommand.test.ts +++ b/tests/unit/handlers/verifyCommand.test.ts @@ -1,15 +1,22 @@ import * as response from "../../../src/constants/responses"; -import JSONResponse from "../../../src/utils/JsonResponse"; import { verifyCommand } from "../../../src/controllers/verifyCommand"; -import { guildEnv } from "../../fixtures/fixture"; 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'), + randomUUID: jest.fn(() => "shreya"), subtle: { digest: jest.fn(() => "123") }, }; }); @@ -18,12 +25,12 @@ describe("verifyCommand", () => { generateUniqueToken: () => Promise.resolve("jashdkjahskajhd"), })); - // const mockResponse = response.INTERNAL_SERVER_ERROR; - // jest - // .spyOn(global, "fetch") - // .mockImplementation(() => - // Promise.resolve(new JSONResponse(mockResponse)) - // ); + const mockResponse = response.INTERNAL_SERVER_ERROR; + jest + .spyOn(global, "fetch") + .mockImplementation(() => + Promise.resolve(new JSONResponse(mockResponse)) + ); const env = { BOT_PUBLIC_KEY: "xyz", @@ -47,28 +54,29 @@ describe("verifyCommand", () => { env ); - // expect(result.data.content).toEqual(response.RETRY_COMMAND); expect(global.fetch).toHaveBeenCalledWith( - `https://api.realdevsquad.com/external-accounts`, - { + `http://localhost:3000/external-accounts`, + expect.objectContaining({ method: "POST", headers: { "Content-Type": "application/json", - Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`, + Authorization: `Bearer asd`, }, - body: JSON.stringify(data), - } + }) ); + + 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)) - // ); + jest + .spyOn(global, "fetch") + .mockImplementation(() => + Promise.resolve(new JSONResponse(mockResponse)) + ); const env = { BOT_PUBLIC_KEY: "xyz", @@ -93,20 +101,21 @@ describe("verifyCommand", () => { ); const verificationSiteURL = config(env).VERIFICATION_SITE_URL; - const message = - `${verificationSiteURL}/discord?token=${guildEnv.DISCORD_TOKEN}\n` + - response.VERIFICATION_STRING; + const message = `${verificationSiteURL}/discord?token=`; expect(global.fetch).toHaveBeenCalledWith( - `https://api.realdevsquad.com/external-accounts`, - { + `http://localhost:3000/external-accounts`, + expect.objectContaining({ method: "POST", headers: { "Content-Type": "application/json", - Authorization: `Bot ${guildEnv.DISCORD_TOKEN}`, + Authorization: `Bearer asd`, }, - body: JSON.stringify(data), - } + }) ); + + const response_: Responsetype = await result.json(); + + expect(response_.data.content).toContain(message); }); }); From 0633f27b7f7f0e38036fa848f77e573c158ebfac Mon Sep 17 00:00:00 2001 From: shreya-mishra Date: Thu, 20 Jul 2023 23:59:27 +0530 Subject: [PATCH 8/9] resolved comments --- src/controllers/verifyCommand.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/controllers/verifyCommand.ts b/src/controllers/verifyCommand.ts index 101ccd01..ac67b7f6 100644 --- a/src/controllers/verifyCommand.ts +++ b/src/controllers/verifyCommand.ts @@ -3,7 +3,7 @@ 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, @@ -13,7 +13,8 @@ export async function verifyCommand( env: env ) { const token = await generateUniqueToken(); - const response = await sendUserDiscordData( token, + const response = await sendUserDiscordData( + token, userId, userAvatarHash, userName, From c2e7fda7b57859b05887236ed7270e1b0ad0dd9d Mon Sep 17 00:00:00 2001 From: shreya-mishra Date: Fri, 21 Jul 2023 00:16:33 +0530 Subject: [PATCH 9/9] fix:lint issue --- __mocks__/@tsndr/cloudflare-worker-jwt.js | 5 ++--- src/constants/responses.ts | 2 -- tests/unit/handlers/verifyCommand.test.ts | 16 ---------------- 3 files changed, 2 insertions(+), 21 deletions(-) diff --git a/__mocks__/@tsndr/cloudflare-worker-jwt.js b/__mocks__/@tsndr/cloudflare-worker-jwt.js index 3e9d994c..d0b39154 100644 --- a/__mocks__/@tsndr/cloudflare-worker-jwt.js +++ b/__mocks__/@tsndr/cloudflare-worker-jwt.js @@ -1,7 +1,6 @@ "use strict"; -// import jest from 'jest'; -const jest = require('jest') +import jest from 'jest'; const jwt = Object.create({}); @@ -9,4 +8,4 @@ jwt.sign = jest.fn().mockImplementation(() => { return "asdasd"; }); -module.exports = jwt \ No newline at end of file +export default jwt \ No newline at end of file diff --git a/src/constants/responses.ts b/src/constants/responses.ts index 3cbb892d..b6317d9a 100644 --- a/src/constants/responses.ts +++ b/src/constants/responses.ts @@ -26,8 +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"; export const VERIFICATION_STRING = diff --git a/tests/unit/handlers/verifyCommand.test.ts b/tests/unit/handlers/verifyCommand.test.ts index c88c47a9..48a0f934 100644 --- a/tests/unit/handlers/verifyCommand.test.ts +++ b/tests/unit/handlers/verifyCommand.test.ts @@ -38,14 +38,6 @@ describe("verifyCommand", () => { DISCORD_TOKEN: "abc", }; - const data = { - token: 1233434, - userId: "sjkhdkjashdksjh", - userAvatarHash: "test user", - userName: "sndbhsbgdj", - env: env, - }; - const result = await verifyCommand( 1233434, "sjkhdkjashdksjh", @@ -84,14 +76,6 @@ describe("verifyCommand", () => { DISCORD_TOKEN: "abc", }; - const data = { - token: 1233434, - userId: "sjkhdkjashdksjh", - userAvatarHash: "test user", - userName: "sndbhsbgdj", - env: env, - }; - const result = await verifyCommand( 1233434, "sjkhdkjashdksjh",