From d9c6159e52103b90124ad06ec2ca24f81c07902b Mon Sep 17 00:00:00 2001 From: Hsu Zhong Jun <27919917+dcshzj@users.noreply.github.com> Date: Tue, 22 Nov 2022 13:24:24 +0800 Subject: [PATCH] ref(fixtures): allow generating router for different user types (#574) * ref(fixtures): allow generating router for different user types * fix: switch to specifying the siteName directly * feat: allow adding a default user session --- package-lock.json | 9 +++ package.json | 3 +- src/fixtures/app.js | 32 -------- src/fixtures/app.ts | 142 ++++++++++++++++++++++++++++++++++++ src/fixtures/sessionData.ts | 28 +++++++ src/fixtures/sites.ts | 2 + src/fixtures/users.ts | 9 +++ 7 files changed, 192 insertions(+), 33 deletions(-) delete mode 100644 src/fixtures/app.js create mode 100644 src/fixtures/app.ts create mode 100644 src/fixtures/sites.ts create mode 100644 src/fixtures/users.ts diff --git a/package-lock.json b/package-lock.json index 2c5031ae3..e77d413d4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5492,6 +5492,15 @@ "@types/node": "*" } }, + "@types/cookie-parser": { + "version": "1.4.3", + "resolved": "https://registry.npmjs.org/@types/cookie-parser/-/cookie-parser-1.4.3.tgz", + "integrity": "sha512-CqSKwFwefj4PzZ5n/iwad/bow2hTCh0FlNAeWLtQM3JA/NX/iYagIpWG2cf1bQKQ2c9gU2log5VUCrn7LDOs0w==", + "dev": true, + "requires": { + "@types/express": "*" + } + }, "@types/cookiejar": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/@types/cookiejar/-/cookiejar-2.1.2.tgz", diff --git a/package.json b/package.json index 4ae39e2a7..bc0a83df2 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "prepare": "husky install", "version": "auto-changelog -p && git add CHANGELOG.md", "db:migrate": "source .env && npx sequelize-cli db:migrate", - "db:migrate:undo": "source .env && npx sequelize-cli db:migrate:undo", + "db:migrate:undo": "source .env && npx sequelize-cli db:migrate:undo", "jump:staging": "source .ssh/.env.staging && ssh -L 5433:$DB_HOST:5432 $SSH_USER@$SSH_HOST -i .ssh/isomercms-staging-bastion.pem", "db:migrate:staging": "source .ssh/.env.staging && npx sequelize-cli db:migrate", "jump:prod": "source .ssh/.env.prod && ssh -L 5433:$DB_HOST:5432 $SSH_USER@$SSH_HOST -i .ssh/isomercms-production-bastion.pem", @@ -86,6 +86,7 @@ "@swc/helpers": "^0.3.8", "@tsconfig/recommended": "^1.0.1", "@types/aws-lambda": "^8.10.106", + "@types/cookie-parser": "^1.4.3", "@types/express": "^4.17.13", "@types/jest": "^27.4.1", "@types/lodash": "^4.14.186", diff --git a/src/fixtures/app.js b/src/fixtures/app.js deleted file mode 100644 index 7c84a63d1..000000000 --- a/src/fixtures/app.js +++ /dev/null @@ -1,32 +0,0 @@ -const cookieParser = require("cookie-parser") -const express = require("express") - -const { errorHandler } = require("@middleware/errorHandler") - -const { - mockUserSessionData, - mockUserWithSiteSessionData, - mockGithubSessionData, -} = require("./sessionData") - -const attachSessionData = (req, res, next) => { - res.locals.userSessionData = mockUserSessionData - res.locals.userWithSiteSessionData = mockUserWithSiteSessionData - res.locals.githubSessionData = mockGithubSessionData - next() -} - -function generateRouter(router) { - const app = express() - app.use(express.json({ limit: "7mb" })) - app.use(express.urlencoded({ extended: false })) - app.use(cookieParser()) - app.use(attachSessionData) - app.use(router) - app.use(errorHandler) - return app -} - -module.exports = { - generateRouter, -} diff --git a/src/fixtures/app.ts b/src/fixtures/app.ts new file mode 100644 index 000000000..e3f5dcaa7 --- /dev/null +++ b/src/fixtures/app.ts @@ -0,0 +1,142 @@ +import cookieParser from "cookie-parser" +import express, { Express } from "express" +import _ from "lodash" + +import { errorHandler } from "@middleware/errorHandler" + +import GithubSessionData from "@classes/GithubSessionData" +import UserSessionData from "@classes/UserSessionData" +import UserWithSiteSessionData from "@classes/UserWithSiteSessionData" + +import { RequestHandler } from "@root/types" + +import { + mockUserSessionData, + mockUserWithSiteSessionData, + mockGithubSessionData, + MOCK_USER_SESSION_DATA_ONE, +} from "./sessionData" +import { MOCK_SITE_NAME_ONE } from "./sites" + +/** + * @deprecated + */ +const attachSessionData: RequestHandler< + unknown, + unknown, + unknown, + unknown, + { + userSessionData: UserSessionData + userWithSiteSessionData: UserWithSiteSessionData + githubSessionData: GithubSessionData + } +> = (req, res, next) => { + res.locals.userSessionData = mockUserSessionData + res.locals.userWithSiteSessionData = mockUserWithSiteSessionData + res.locals.githubSessionData = mockGithubSessionData + next() +} + +const attachUserSessionData: ( + userSessionData: UserSessionData +) => RequestHandler< + unknown, + unknown, + unknown, + unknown, + { userSessionData: UserSessionData } +> = (userSessionData) => (req, res, next) => { + res.locals.userSessionData = userSessionData + next() +} + +const attachUserSessionDataWithSite: ( + userSessionData: UserSessionData, + siteName: string +) => RequestHandler< + unknown, + unknown, + unknown, + unknown, + { + userSessionData: UserSessionData + userWithSiteSessionData: UserWithSiteSessionData + } +> = (userSessionData, siteName) => (req, res, next) => { + const userWithSiteSessionData = new UserWithSiteSessionData({ + isomerUserId: userSessionData.isomerUserId, + email: userSessionData.email, + siteName, + }) + res.locals.userSessionData = userSessionData + res.locals.userWithSiteSessionData = userWithSiteSessionData + next() +} + +const attachDefaultUserSessionData: RequestHandler< + unknown, + unknown, + unknown, + unknown, + { userSessionData: UserSessionData } +> = attachUserSessionData(MOCK_USER_SESSION_DATA_ONE) + +const attachDefaultUserSessionDataWithSite: RequestHandler< + unknown, + unknown, + unknown, + unknown, + { + userSessionData: UserSessionData + userWithSiteSessionData: UserWithSiteSessionData + } +> = attachUserSessionDataWithSite( + MOCK_USER_SESSION_DATA_ONE, + MOCK_SITE_NAME_ONE +) + +/** + * @deprecated + */ +export const generateRouter = (router: Express) => { + const app = express() + app.use(express.json({ limit: "7mb" })) + app.use(express.urlencoded({ extended: false })) + app.use(cookieParser()) + app.use(attachSessionData) + app.use(router) + app.use(errorHandler) + return app +} + +const generateFinalRouter = (router: Express) => { + const app = express() + app.use(express.json({ limit: "7mb" })) + app.use(express.urlencoded({ extended: false })) + app.use(cookieParser()) + app.use(router) + app.use(errorHandler) + return app +} + +export const generateRouterForUser = ( + router: Express, + userSessionData: UserSessionData +) => { + const app = express() + app.use(attachUserSessionData(userSessionData)) + app.use(router) + return generateFinalRouter(app) +} + +export const generateRouterForUserWithSite = ( + router: Express, + userSessionData: UserSessionData, + siteName: string +) => { + const app = express() + app.use(attachUserSessionDataWithSite(userSessionData, siteName)) + app.use(router) + return generateFinalRouter(app) +} diff --git a/src/fixtures/sessionData.ts b/src/fixtures/sessionData.ts index 756ddc107..3898ce3b6 100644 --- a/src/fixtures/sessionData.ts +++ b/src/fixtures/sessionData.ts @@ -2,6 +2,17 @@ import GithubSessionData from "@root/classes/GithubSessionData" import UserSessionData from "@root/classes/UserSessionData" import UserWithSiteSessionData from "@root/classes/UserWithSiteSessionData" +import { + MOCK_USER_EMAIL_ONE, + MOCK_USER_EMAIL_TWO, + MOCK_USER_EMAIL_THREE, + MOCK_USER_EMAIL_FOUR, + MOCK_USER_ID_ONE, + MOCK_USER_ID_TWO, + MOCK_USER_ID_THREE, + MOCK_USER_ID_FOUR, +} from "./users" + export const mockAccessToken = "mockAccessToken" export const mockGithubId = "mockGithubId" export const mockIsomerUserId = "1" @@ -43,3 +54,20 @@ export const mockSessionDataEmailUserWithSite = new UserWithSiteSessionData({ email: mockEmail, siteName: mockSiteName, }) + +export const MOCK_USER_SESSION_DATA_ONE = new UserSessionData({ + isomerUserId: String(MOCK_USER_ID_ONE), + email: MOCK_USER_EMAIL_ONE, +}) +export const MOCK_USER_SESSION_DATA_TWO = new UserSessionData({ + isomerUserId: String(MOCK_USER_ID_TWO), + email: MOCK_USER_EMAIL_TWO, +}) +export const MOCK_USER_SESSION_DATA_THREE = new UserSessionData({ + isomerUserId: String(MOCK_USER_ID_THREE), + email: MOCK_USER_EMAIL_THREE, +}) +export const MOCK_USER_SESSION_DATA_FOUR = new UserSessionData({ + isomerUserId: String(MOCK_USER_ID_FOUR), + email: MOCK_USER_EMAIL_FOUR, +}) diff --git a/src/fixtures/sites.ts b/src/fixtures/sites.ts new file mode 100644 index 000000000..5b282c0b4 --- /dev/null +++ b/src/fixtures/sites.ts @@ -0,0 +1,2 @@ +export const MOCK_SITE_NAME_ONE = "test-site-one" +export const MOCK_SITE_NAME_TWO = "test-site-two" diff --git a/src/fixtures/users.ts b/src/fixtures/users.ts new file mode 100644 index 000000000..4212b4b34 --- /dev/null +++ b/src/fixtures/users.ts @@ -0,0 +1,9 @@ +export const MOCK_USER_ID_ONE = 1 +export const MOCK_USER_ID_TWO = 2 +export const MOCK_USER_ID_THREE = 3 +export const MOCK_USER_ID_FOUR = 4 + +export const MOCK_USER_EMAIL_ONE = "one@test.gov.sg" +export const MOCK_USER_EMAIL_TWO = "two@test.gov.sg" +export const MOCK_USER_EMAIL_THREE = "three@test.gov.sg" +export const MOCK_USER_EMAIL_FOUR = "four@test.gov.sg"