generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement basic project structure (#22)
* feat: implement basic project structure * Update src/middleware/globalErrorHandler/middleware.ts Co-authored-by: Pat Heard <patrick.heard@cds-snc.ca> * feat: add form submission router unit tests --------- Co-authored-by: Pat Heard <patrick.heard@cds-snc.ca>
- Loading branch information
Showing
24 changed files
with
242 additions
and
59 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
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,10 @@ | ||
const config = { | ||
trailingComma: "es5", | ||
tabWidth: 2, | ||
printWidth: 100, | ||
useTabs: false, | ||
semi: true, | ||
singleQuote: false, | ||
}; | ||
|
||
export default config; |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
async function main(formId: string, submissionId: string) { | ||
return { | ||
formId, | ||
submissionId, | ||
}; | ||
} | ||
|
||
export default main; |
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,7 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
|
||
async function main(request: Request, response: Response, next: NextFunction) { | ||
next(); | ||
} | ||
|
||
export default main; |
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,8 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
|
||
function main(error: Error, _request: Request, response: Response, _next: NextFunction) { | ||
console.error(JSON.stringify(error, Object.getOwnPropertyNames(error))); | ||
response.sendStatus(500); | ||
} | ||
|
||
export default main; |
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,7 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
|
||
async function main(request: Request, response: Response, next: NextFunction) { | ||
next(); | ||
} | ||
|
||
export default main; |
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,7 @@ | ||
import { Request, Response } from "express"; | ||
|
||
function main(_request: Request, response: Response) { | ||
response.sendStatus(404); | ||
} | ||
|
||
export default main; |
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,8 +1,17 @@ | ||
import { Router } from "express"; | ||
import { route as formsRoute } from "./routes/forms"; | ||
import { route as healthCheckRoute } from "./routes/healthCheck"; | ||
import formsRouter from "./routes/forms/router"; | ||
import statusRouter from "./routes/status/router"; | ||
import routeNotFoundMiddleware from "./middleware/routeNotFound/middleware"; | ||
import globalErrorHandlerMiddleware from "./middleware/globalErrorHandler/middleware"; | ||
|
||
export const router = Router(); | ||
const router = Router(); | ||
|
||
router.use("/status", healthCheckRoute); | ||
router.use("/forms", formsRoute); | ||
router | ||
.use("/forms", formsRouter) | ||
.use("/status", statusRouter) | ||
// 404: Catches all unmatched routes | ||
.use(routeNotFoundMiddleware) | ||
// 500: Catches all unhandled errors from non async functions and errors passed through next() in async functions | ||
.use(globalErrorHandlerMiddleware); | ||
|
||
export default router; |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Router } from "express"; | ||
import submissionRouter from "./submission/router"; | ||
import authenticationMiddleware from "../../middleware/authentication/middleware"; | ||
import rateLimiterMiddleware from "../../middleware/rateLimiter/middleware"; | ||
|
||
const router = Router(); | ||
|
||
router | ||
.use(authenticationMiddleware) | ||
.use(rateLimiterMiddleware) | ||
.use("/:formId/submission", submissionRouter); | ||
|
||
export default router; |
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,13 @@ | ||
import { Request, Response, Router } from "express"; | ||
|
||
const router = Router({ | ||
mergeParams: true, | ||
}); | ||
|
||
router.get("/", async (request: Request, response: Response) => { | ||
response.json({ | ||
formId: request.params.formId, | ||
}); | ||
}); | ||
|
||
export default router; |
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,13 @@ | ||
import { Request, Response, Router } from "express"; | ||
|
||
const router = Router({ | ||
mergeParams: true, | ||
}); | ||
|
||
router.get("/", async (request: Request, response: Response) => { | ||
response.json({ | ||
formId: request.params.formId, | ||
}); | ||
}); | ||
|
||
export default router; |
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,15 @@ | ||
import { Router } from "express"; | ||
import newRouter from "./new/router"; | ||
import downloadedRouter from "./downloaded/router"; | ||
import submissionIdRouter from "./submissionId/router"; | ||
|
||
const router = Router({ | ||
mergeParams: true, | ||
}); | ||
|
||
router | ||
.use("/new", newRouter) | ||
.use("/downloaded", downloadedRouter) | ||
.use("/:submissionId", submissionIdRouter); | ||
|
||
export default router; |
15 changes: 15 additions & 0 deletions
15
src/routes/forms/submission/submissionId/confirm/confirmationCode/router.ts
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,15 @@ | ||
import { Request, Response, Router } from "express"; | ||
|
||
const router = Router({ | ||
mergeParams: true, | ||
}); | ||
|
||
router.put("/", async (request: Request, response: Response) => { | ||
response.json({ | ||
formId: request.params.formId, | ||
submissionId: request.params.submissionId, | ||
confirmationCode: request.params.confirmationCode, | ||
}); | ||
}); | ||
|
||
export default router; |
10 changes: 10 additions & 0 deletions
10
src/routes/forms/submission/submissionId/confirm/router.ts
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,10 @@ | ||
import { Router } from "express"; | ||
import confirmationCodeRouter from "./confirmationCode/router"; | ||
|
||
const router = Router({ | ||
mergeParams: true, | ||
}); | ||
|
||
router.use("/:confirmationCode", confirmationCodeRouter); | ||
|
||
export default router; |
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,18 @@ | ||
import { Request, Response, Router } from "express"; | ||
import confirmRouter from "./confirm/router"; | ||
import getSubmission from "../../../../lib/vault/getSubmission"; | ||
|
||
const router = Router({ | ||
mergeParams: true, | ||
}); | ||
|
||
router.get("/", async (request: Request, response: Response) => { | ||
const formId = request.params.formId; | ||
const submissionId = request.params.submissionId; | ||
const submission = await getSubmission(formId, submissionId); | ||
response.json(submission); | ||
}); | ||
|
||
router.use("/confirm", confirmRouter); | ||
|
||
export default router; |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Response, Router } from "express"; | ||
|
||
const router = Router(); | ||
|
||
router.get("/", (_, response: Response) => { | ||
response.json({ | ||
status: "running", | ||
}); | ||
}); | ||
|
||
export default router; |
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,13 +1,11 @@ | ||
import express, { Express } from "express"; | ||
import { SERVER_PORT } from "./config"; | ||
import { router } from "./router"; | ||
import router from "./router"; | ||
|
||
const server: Express = express(); | ||
|
||
server.use("/", router); | ||
|
||
server.listen(SERVER_PORT, () => { | ||
console.log("-----------------------------------------"); | ||
console.log(`API server started on port ${SERVER_PORT}`); | ||
console.log("-----------------------------------------"); | ||
console.log(`>>> API server listening on port ${SERVER_PORT} <<<`); | ||
}); |
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,42 @@ | ||
import { vi, describe, it, expect, beforeAll } from "vitest"; | ||
import request from "supertest"; | ||
import express from "express"; | ||
import router from "../../../../src/routes/forms/router"; | ||
|
||
vi.mock("../../../../src/middleware/authentication/middleware", () => ({ | ||
default: (_req, _res, next) => next(), | ||
})); | ||
|
||
vi.mock("../../../../src/middleware/rateLimiter/middleware", () => ({ | ||
default: (_req, _res, next) => next(), | ||
})); | ||
|
||
describe("routes/forms/submission", () => { | ||
let app: express.Express; | ||
|
||
beforeAll(() => { | ||
app = express(); | ||
app.use("/", router); | ||
}); | ||
|
||
it("GET /new", async () => { | ||
const response = await request(app).get("/1234/submission/new"); | ||
expect(response.status).toBe(200); | ||
expect(response.body).toEqual({ formId: "1234" }); | ||
}); | ||
|
||
it("GET /downloaded", async () => { | ||
const response = await request(app).get("/1234/submission/downloaded"); | ||
expect(response.status).toBe(200); | ||
expect(response.body).toEqual({ formId: "1234" }); | ||
}); | ||
|
||
it("GET /:submissionId", async () => { | ||
const response = await request(app).get("/1234/submission/5678"); | ||
expect(response.status).toBe(200); | ||
expect(response.body).toEqual({ | ||
formId: "1234", | ||
submissionId: "5678", | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { describe, it, expect, beforeAll } from "vitest"; | ||
import request from "supertest"; | ||
import express from "express"; | ||
import router from "../../../src/routes/status/router"; | ||
|
||
describe("routes/status", () => { | ||
let app: express.Express; | ||
|
||
beforeAll(() => { | ||
app = express(); | ||
app.use("/", router); | ||
}); | ||
|
||
it("GET /status", async () => { | ||
const response = await request(app).get("/"); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body).toEqual({ status: "running" }); | ||
}); | ||
}); |