-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
176 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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." | ||
); | ||
}); | ||
it("should fail without mandatory properties", async () => { | ||
//WHEN | ||
const { body } = await mockApp | ||
.post("/dev/generateData") | ||
.send({}) | ||
.expect(422); | ||
|
||
//THEN | ||
expect(body).toEqual({ | ||
message: "Invalid request data schema", | ||
validationErrors: [`"username" Required`], | ||
}); | ||
}); | ||
it("should fail with unknown properties", async () => { | ||
//WHEN | ||
const { body } = await mockApp | ||
.post("/dev/generateData") | ||
.send({ username: "Bob", extra: "value" }) | ||
.expect(422); | ||
|
||
//THEN | ||
expect(body).toEqual({ | ||
message: "Invalid request data schema", | ||
validationErrors: ["Unrecognized key(s) in object: 'extra'"], | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,15 @@ | ||
import { Router } from "express"; | ||
import joi from "joi"; | ||
import { createTestData } from "../controllers/dev"; | ||
import { isDevEnvironment } from "../../utils/misc"; | ||
import { validate } from "../../middlewares/configuration"; | ||
import { validateRequest } from "../../middlewares/validation"; | ||
import { asyncHandler } from "../../middlewares/utility"; | ||
import { devContract } from "@monkeytype/contracts/dev"; | ||
import { initServer } from "@ts-rest/express"; | ||
|
||
const router = Router(); | ||
import * as DevController from "../controllers/dev"; | ||
import { callController } from "../ts-rest-adapter"; | ||
import { onlyAvailableOnDev } from "../../middlewares/utility"; | ||
|
||
router.use( | ||
validate({ | ||
criteria: () => { | ||
return isDevEnvironment(); | ||
}, | ||
invalidMessage: "Development endpoints are only available in DEV mode.", | ||
}) | ||
); | ||
const s = initServer(); | ||
|
||
router.post( | ||
"/generateData", | ||
validateRequest({ | ||
body: { | ||
username: joi.string().required(), | ||
createUser: joi.boolean().optional(), | ||
firstTestTimestamp: joi.number().optional(), | ||
lastTestTimestamp: joi.number().optional(), | ||
minTestsPerDay: joi.number().optional(), | ||
maxTestsPerDay: joi.number().optional(), | ||
}, | ||
}), | ||
asyncHandler(createTestData) | ||
); | ||
|
||
export default router; | ||
export default s.router(devContract, { | ||
generateData: { | ||
middleware: [onlyAvailableOnDev()], | ||
handler: async (r) => callController(DevController.createTestData)(r), | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
import Quotes from "./quotes"; | ||
import Users from "./users"; | ||
import Dev from "./dev"; | ||
|
||
export default { | ||
Quotes, | ||
Users, | ||
Dev, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { initContract } from "@ts-rest/core"; | ||
import { z } from "zod"; | ||
import { | ||
CommonResponses, | ||
EndpointMetadata, | ||
responseWithData, | ||
} from "./schemas/api"; | ||
import { IdSchema } from "./schemas/util"; | ||
|
||
export const GenerateDataRequestSchema = z.object({ | ||
username: z.string(), | ||
createUser: z | ||
.boolean() | ||
.optional() | ||
.describe( | ||
"If `true` create user with <username>@example.com and password `password`. If false user has to exist." | ||
), | ||
firstTestTimestamp: z.number().int().nonnegative().optional(), | ||
lastTestTimestamp: z.number().int().nonnegative().optional(), | ||
minTestsPerDay: z.number().int().nonnegative().optional(), | ||
maxTestsPerDay: z.number().int().nonnegative().optional(), | ||
}); | ||
export type GenerateDataRequest = z.infer<typeof GenerateDataRequestSchema>; | ||
|
||
export const GenerateDataResponseSchema = responseWithData( | ||
z.object({ | ||
uid: IdSchema, | ||
email: z.string().email(), | ||
}) | ||
); | ||
export type GenerateDataResponse = z.infer<typeof GenerateDataResponseSchema>; | ||
|
||
const c = initContract(); | ||
export const devContract = c.router( | ||
{ | ||
generateData: { | ||
summary: "generate data", | ||
description: "Generate test results for the given user.", | ||
method: "POST", | ||
path: "/generateData", | ||
body: GenerateDataRequestSchema.strict(), | ||
responses: { | ||
200: GenerateDataResponseSchema, | ||
}, | ||
}, | ||
}, | ||
{ | ||
pathPrefix: "/dev", | ||
strictStatusCodes: true, | ||
metadata: { | ||
openApiTags: "dev", | ||
authenticationOptions: { | ||
isPublic: true, | ||
}, | ||
} as EndpointMetadata, | ||
commonResponses: CommonResponses, | ||
} | ||
); |
Oops, something went wrong.