Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fehmer committed Aug 23, 2024
1 parent cc83896 commit 8f682bf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 29 deletions.
32 changes: 32 additions & 0 deletions backend/__tests__/api/controllers/dev.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import request from "supertest";
import app from "../../../src/app";

import { ObjectId } from "mongodb";
import * as Misc from "../../../src/utils/misc";

const uid = new ObjectId().toHexString();
const mockApp = request(app);

describe("DevController", () => {
describe("generate testData", () => {
const isDevEnvironmentMock = vi.spyOn(Misc, "isDevEnvironment");

beforeEach(() => {
isDevEnvironmentMock.mockReset();
isDevEnvironmentMock.mockReturnValue(true);
});
it("should fail on prod", async () => {
//GIVEN
isDevEnvironmentMock.mockReturnValue(false);
//WHEN
const { body } = await mockApp
.post("/dev/generateData")
.send({ username: "test" })
.expect(503);
//THEN
expect(body.message).toEqual(
"Development endpoints are only available in DEV mode."
);
});
});
});
31 changes: 12 additions & 19 deletions backend/src/api/controllers/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { roundTo2 } from "../../utils/misc";
import { ObjectId } from "mongodb";
import * as LeaderboardDal from "../../dal/leaderboards";
import MonkeyError from "../../utils/error";
import isNumber from "lodash/isNumber";

import {
Mode,
PersonalBest,
Expand All @@ -20,16 +20,9 @@ import {
GenerateDataResponse,
} from "@monkeytype/contracts/dev";

type GenerateDataOptions = {
firstTestTimestamp: Date | number;
lastTestTimestamp: Date | number;
minTestsPerDay: number;
maxTestsPerDay: number;
};

const CREATE_RESULT_DEFAULT_OPTIONS: GenerateDataOptions = {
firstTestTimestamp: DateUtils.startOfDay(new UTCDate(Date.now())),
lastTestTimestamp: DateUtils.endOfDay(new UTCDate(Date.now())),
const CREATE_RESULT_DEFAULT_OPTIONS: Partial<GenerateDataRequest> = {
firstTestTimestamp: DateUtils.startOfDay(new UTCDate(Date.now())).valueOf(),
lastTestTimestamp: DateUtils.endOfDay(new UTCDate(Date.now())).valueOf(),
minTestsPerDay: 0,
maxTestsPerDay: 50,
};
Expand Down Expand Up @@ -77,23 +70,23 @@ async function getOrCreateUser(

async function createTestResults(
user: MonkeyTypes.DBUser,
configOptions: Partial<GenerateDataOptions>
configOptions: GenerateDataRequest
): Promise<void> {
const config = {
...CREATE_RESULT_DEFAULT_OPTIONS,
...configOptions,
};
if (isNumber(config.firstTestTimestamp))
config.firstTestTimestamp = toDate(config.firstTestTimestamp);
if (isNumber(config.lastTestTimestamp))
config.lastTestTimestamp = toDate(config.lastTestTimestamp);
const start = toDate(config.firstTestTimestamp as number);
const end = toDate(config.lastTestTimestamp as number);

const days = DateUtils.eachDayOfInterval({
start: config.firstTestTimestamp,
end: config.lastTestTimestamp,
start,
end,
}).map((day) => ({
timestamp: DateUtils.startOfDay(day),
amount: Math.round(random(config.minTestsPerDay, config.maxTestsPerDay)),
amount: Math.round(
random(config.minTestsPerDay as number, config.maxTestsPerDay as number)
),
}));

for (const day of days) {
Expand Down
13 changes: 3 additions & 10 deletions backend/src/api/routes/dev.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import { devContract } from "@monkeytype/contracts/dev";
import { initServer } from "@ts-rest/express";
import { validate } from "../../middlewares/configuration";
import { isDevEnvironment } from "../../utils/misc";

import * as DevController from "../controllers/dev";
import { callController } from "../ts-rest-adapter";

const onlyAvailableOnDev = validate({
criteria: () => {
return isDevEnvironment();
},
invalidMessage: "Development endpoints are only available in DEV mode.",
});
import { onlyAvailableOnDev } from "../../middlewares/utility";

const s = initServer();

export default s.router(devContract, {
generateData: {
middleware: [onlyAvailableOnDev],
middleware: [onlyAvailableOnDev()],
handler: async (r) => callController(DevController.createTestData)(r),
},
});
10 changes: 10 additions & 0 deletions backend/src/middlewares/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from "lodash";
import type { Request, Response, NextFunction, RequestHandler } from "express";
import { handleMonkeyResponse, MonkeyResponse } from "../utils/monkey-response";
import { recordClientVersion as prometheusRecordClientVersion } from "../utils/prometheus";
import { validate } from "./configuration";

export const emptyMiddleware = (
_req: MonkeyTypes.Request,
Expand Down Expand Up @@ -49,3 +50,12 @@ export function recordClientVersion(): RequestHandler {
next();
};
}

export function onlyAvailableOnDev(): RequestHandler {
return validate({
criteria: () => {
return isDevEnvironment();
},
invalidMessage: "Development endpoints are only available in DEV mode.",
});
}

0 comments on commit 8f682bf

Please sign in to comment.