Skip to content

Commit

Permalink
disable location check for now
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Turner authored and Ben Turner committed Mar 13, 2024
1 parent 0ad634c commit f74b505
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
27 changes: 18 additions & 9 deletions src/routers/qr-code.router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
Expand All @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/routers/qr-code.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ 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();

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();
Expand Down
14 changes: 13 additions & 1 deletion src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { parseEntryFilterQueryParams, validateCoords } from "./utils";
import {
isLocationCheckEnabled,
parseEntryFilterQueryParams,
validateCoords,
} from "./utils";

describe("utils.ts", () => {
describe("parseEntryFilterQueryParams()", () => {
Expand Down Expand Up @@ -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),
);
});
});
});
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
};

0 comments on commit f74b505

Please sign in to comment.