From 9a9b811a7a9eaf2d37695589921a302eb369b35a Mon Sep 17 00:00:00 2001 From: Hsu Zhong Jun <27919917+dcshzj@users.noreply.github.com> Date: Thu, 17 Nov 2022 15:07:21 +0800 Subject: [PATCH 1/3] ref(fixtures): allow generating router for different user types --- package-lock.json | 9 +++ package.json | 3 +- src/fixtures/app.js | 32 ----------- src/fixtures/app.ts | 109 ++++++++++++++++++++++++++++++++++++ src/fixtures/sessionData.ts | 18 ++++++ 5 files changed, 138 insertions(+), 33 deletions(-) delete mode 100644 src/fixtures/app.js create mode 100644 src/fixtures/app.ts diff --git a/package-lock.json b/package-lock.json index 77594ce71..1c5cc52a5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4652,6 +4652,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 d0693fe23..4610054c4 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,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", @@ -83,6 +83,7 @@ "@swc/core": "^1.2.160", "@swc/helpers": "^0.3.8", "@tsconfig/recommended": "^1.0.1", + "@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..b53cfe487 --- /dev/null +++ b/src/fixtures/app.ts @@ -0,0 +1,109 @@ +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, + removeSiteFromSessionData, +} from "./sessionData" + +/** + * @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 attachUserWithSiteSessionData: ( + userWithSiteSessionData: UserWithSiteSessionData +) => RequestHandler< + unknown, + unknown, + unknown, + unknown, + { + userSessionData: UserSessionData + userWithSiteSessionData: UserWithSiteSessionData + } +> = (userWithSiteSessionData) => (req, res, next) => { + const userSessionData = removeSiteFromSessionData(userWithSiteSessionData) + res.locals.userSessionData = userSessionData + res.locals.userWithSiteSessionData = userWithSiteSessionData + next() +} + +/** + * @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 +) => { + router.use(attachUserSessionData(userSessionData)) + return generateFinalRouter(router) +} + +export const generateRouterForUserWithSite = ( + router: Express, + userWithSiteSessionData: UserWithSiteSessionData +) => { + router.use(attachUserWithSiteSessionData(userWithSiteSessionData)) + return generateFinalRouter(router) +} diff --git a/src/fixtures/sessionData.ts b/src/fixtures/sessionData.ts index 756ddc107..a0119667f 100644 --- a/src/fixtures/sessionData.ts +++ b/src/fixtures/sessionData.ts @@ -2,6 +2,24 @@ import GithubSessionData from "@root/classes/GithubSessionData" import UserSessionData from "@root/classes/UserSessionData" import UserWithSiteSessionData from "@root/classes/UserWithSiteSessionData" +export const attachSiteToSessionData = ( + userSessionData: UserSessionData, + siteName: string +) => + new UserWithSiteSessionData({ + isomerUserId: userSessionData.isomerUserId, + email: userSessionData.email, + siteName, + }) + +export const removeSiteFromSessionData = ( + userWithSiteSessionData: UserWithSiteSessionData +) => + new UserSessionData({ + isomerUserId: userWithSiteSessionData.isomerUserId, + email: userWithSiteSessionData.email, + }) + export const mockAccessToken = "mockAccessToken" export const mockGithubId = "mockGithubId" export const mockIsomerUserId = "1" From cc99b876db9061024fe99f085d04dc2a4993dc94 Mon Sep 17 00:00:00 2001 From: Hsu Zhong Jun <27919917+dcshzj@users.noreply.github.com> Date: Fri, 18 Nov 2022 12:12:19 +0800 Subject: [PATCH 2/3] fix: switch to specifying the siteName directly --- src/fixtures/app.ts | 29 +++++++++++++++++++---------- src/fixtures/sessionData.ts | 18 ------------------ 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/src/fixtures/app.ts b/src/fixtures/app.ts index b53cfe487..c8d43b5ad 100644 --- a/src/fixtures/app.ts +++ b/src/fixtures/app.ts @@ -14,7 +14,6 @@ import { mockUserSessionData, mockUserWithSiteSessionData, mockGithubSessionData, - removeSiteFromSessionData, } from "./sessionData" /** @@ -50,8 +49,9 @@ const attachUserSessionData: ( next() } -const attachUserWithSiteSessionData: ( - userWithSiteSessionData: UserWithSiteSessionData +const attachUserSessionDataWithSite: ( + userSessionData: UserSessionData, + siteName: string ) => RequestHandler< unknown, unknown, @@ -61,8 +61,12 @@ const attachUserWithSiteSessionData: ( userSessionData: UserSessionData userWithSiteSessionData: UserWithSiteSessionData } -> = (userWithSiteSessionData) => (req, res, next) => { - const userSessionData = removeSiteFromSessionData(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() @@ -96,14 +100,19 @@ export const generateRouterForUser = ( router: Express, userSessionData: UserSessionData ) => { - router.use(attachUserSessionData(userSessionData)) - return generateFinalRouter(router) + const app = express() + app.use(attachUserSessionData(userSessionData)) + app.use(router) + return generateFinalRouter(app) } export const generateRouterForUserWithSite = ( router: Express, - userWithSiteSessionData: UserWithSiteSessionData + userSessionData: UserSessionData, + siteName: string ) => { - router.use(attachUserWithSiteSessionData(userWithSiteSessionData)) - return generateFinalRouter(router) + 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 a0119667f..756ddc107 100644 --- a/src/fixtures/sessionData.ts +++ b/src/fixtures/sessionData.ts @@ -2,24 +2,6 @@ import GithubSessionData from "@root/classes/GithubSessionData" import UserSessionData from "@root/classes/UserSessionData" import UserWithSiteSessionData from "@root/classes/UserWithSiteSessionData" -export const attachSiteToSessionData = ( - userSessionData: UserSessionData, - siteName: string -) => - new UserWithSiteSessionData({ - isomerUserId: userSessionData.isomerUserId, - email: userSessionData.email, - siteName, - }) - -export const removeSiteFromSessionData = ( - userWithSiteSessionData: UserWithSiteSessionData -) => - new UserSessionData({ - isomerUserId: userWithSiteSessionData.isomerUserId, - email: userWithSiteSessionData.email, - }) - export const mockAccessToken = "mockAccessToken" export const mockGithubId = "mockGithubId" export const mockIsomerUserId = "1" From 960bd2ecb53081981cb1cdbf47d3d6e9a9798386 Mon Sep 17 00:00:00 2001 From: Hsu Zhong Jun <27919917+dcshzj@users.noreply.github.com> Date: Tue, 22 Nov 2022 13:13:43 +0800 Subject: [PATCH 3/3] feat: allow adding a default user session --- src/fixtures/app.ts | 24 ++++++++++++++++++++++++ src/fixtures/sessionData.ts | 28 ++++++++++++++++++++++++++++ src/fixtures/sites.ts | 2 ++ src/fixtures/users.ts | 9 +++++++++ 4 files changed, 63 insertions(+) create mode 100644 src/fixtures/sites.ts create mode 100644 src/fixtures/users.ts diff --git a/src/fixtures/app.ts b/src/fixtures/app.ts index c8d43b5ad..e3f5dcaa7 100644 --- a/src/fixtures/app.ts +++ b/src/fixtures/app.ts @@ -14,7 +14,9 @@ import { mockUserSessionData, mockUserWithSiteSessionData, mockGithubSessionData, + MOCK_USER_SESSION_DATA_ONE, } from "./sessionData" +import { MOCK_SITE_NAME_ONE } from "./sites" /** * @deprecated @@ -72,6 +74,28 @@ const attachUserSessionDataWithSite: ( 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 */ 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"