From f74b5058753d9ab390f74cf10c8465e134c61a7b Mon Sep 17 00:00:00 2001 From: Ben Turner Date: Wed, 13 Mar 2024 15:43:31 +0000 Subject: [PATCH] disable location check for now --- src/routers/qr-code.router.test.ts | 27 ++++++++++++++++++--------- src/routers/qr-code.router.ts | 6 +++--- src/utils.test.ts | 14 +++++++++++++- src/utils.ts | 4 ++++ 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/routers/qr-code.router.test.ts b/src/routers/qr-code.router.test.ts index b6c8d90..09bb953 100644 --- a/src/routers/qr-code.router.test.ts +++ b/src/routers/qr-code.router.test.ts @@ -10,18 +10,21 @@ const signSpy = jest.spyOn(jwt, "sign"); describe("qr-code-router.ts", () => { describe("GET /api/qr-code/generate", () => { - it("Should bypass location check in dev environment and generate a QR code as a base64 encoded png and an access code with configured expiration date", async () => { + it("Should generate a QR code as a base64 encoded png and an access code with configured expiration date", async () => { const getLocationCoordinatesSpy = jest.spyOn( locationService, "getLocationCoordinates", ); - const isProductionEnvSpy = jest.spyOn(utils, "isProductionEnv"); - isProductionEnvSpy.mockReturnValue(false); + const isLocationCheckEnabledSpy = jest.spyOn( + utils, + "isLocationCheckEnabled", + ); + isLocationCheckEnabledSpy.mockReturnValue(false); const response = await mockServer().get("/api/qr-code/generate"); expect(response.headers["content-type"]).toContain("application/json"); expect(getLocationCoordinatesSpy).toHaveBeenCalledTimes(0); - expect(isProductionEnvSpy).toHaveReturnedWith(false); + expect(isLocationCheckEnabledSpy).toHaveReturnedWith(false); expect(response.body.dataUrl).toContain( "data:image/png;base64,iVBORw0KGgo", ); @@ -34,15 +37,18 @@ describe("qr-code-router.ts", () => { ); }); - it("Should perform location check in prod environment and generate a QR code as a base64 encoded png and an access code with configured expiration date", async () => { + it("Should perform location check if enabled and generate a QR code as a base64 encoded png and an access code with configured expiration date", async () => { const getLocationCoordinatesSpy = jest.spyOn( locationService, "getLocationCoordinates", ); - const isProductionEnvSpy = jest.spyOn(utils, "isProductionEnv"); + const isLocationCheckEnabledSpy = jest.spyOn( + utils, + "isLocationCheckEnabled", + ); const validateCoordsSpy = jest.spyOn(utils, "validateCoords"); - isProductionEnvSpy.mockReturnValue(true); + isLocationCheckEnabledSpy.mockReturnValue(true); getLocationCoordinatesSpy.mockResolvedValue({ latitude: 100, longitude: 100, @@ -69,10 +75,13 @@ describe("qr-code-router.ts", () => { locationService, "getLocationCoordinates", ); - const isProductionEnvSpy = jest.spyOn(utils, "isProductionEnv"); + const isLocationCheckEnabledSpy = jest.spyOn( + utils, + "isLocationCheckEnabled", + ); const validateCoordsSpy = jest.spyOn(utils, "validateCoords"); - isProductionEnvSpy.mockReturnValue(true); + isLocationCheckEnabledSpy.mockReturnValue(true); getLocationCoordinatesSpy.mockResolvedValue({ latitude: 100, longitude: 100, diff --git a/src/routers/qr-code.router.ts b/src/routers/qr-code.router.ts index 06ef85b..dfbf6f8 100644 --- a/src/routers/qr-code.router.ts +++ b/src/routers/qr-code.router.ts @@ -6,7 +6,7 @@ import { import { generateAccessToken } from "../services/auth.service"; import { validateAccessToken } from "../middleware"; import { getLocationCoordinates } from "../services/location.service"; -import { isProductionEnv, parseIp, validateCoords } from "../utils"; +import { isLocationCheckEnabled, parseIp, validateCoords } from "../utils"; const qrCodeRouter = ex.Router(); @@ -14,9 +14,9 @@ qrCodeRouter.get("/generate", async (req, res, next) => { try { const ip = parseIp(req); - if (isProductionEnv()) { + if (isLocationCheckEnabled()) { const coords = await getLocationCoordinates(ip as string); - console.log('Validating location of IP: ' + ip, coords) + console.log("Validating location of IP: " + ip, coords); if (!validateCoords(coords)) throw Error("Invalid location of request"); } const accessToken = generateAccessToken(); diff --git a/src/utils.test.ts b/src/utils.test.ts index edad7ad..89e14ba 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -1,4 +1,8 @@ -import { parseEntryFilterQueryParams, validateCoords } from "./utils"; +import { + isLocationCheckEnabled, + parseEntryFilterQueryParams, + validateCoords, +} from "./utils"; describe("utils.ts", () => { describe("parseEntryFilterQueryParams()", () => { @@ -32,4 +36,12 @@ describe("utils.ts", () => { ).toBe(false); }); }); + + describe("isLocationCheckEnabled", () => { + it("Should return true or false based on process.env.ENABLE_LOCATION_CHECK value", () => { + expect(isLocationCheckEnabled()).toBe( + JSON.parse(process.env.ENABLE_LOCATION_CHECK as string), + ); + }); + }); }); diff --git a/src/utils.ts b/src/utils.ts index 99d9023..dd85924 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -53,3 +53,7 @@ export const parseIp = (req: Request): string => { export const isProductionEnv = () => !["test", "development"].includes(process.env.NODE_ENV as string); + +export const isLocationCheckEnabled = () => { + return process.env.ENABLE_LOCATION_CHECK === "true"; +};