Skip to content

Commit

Permalink
ref(fixtures): allow generating router for different user types (#574)
Browse files Browse the repository at this point in the history
* ref(fixtures): allow generating router for different user types

* fix: switch to specifying the siteName directly

* feat: allow adding a default user session
  • Loading branch information
dcshzj authored and alexanderleegs committed Mar 16, 2023
1 parent 77d780d commit d9c6159
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 33 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
32 changes: 0 additions & 32 deletions src/fixtures/app.js

This file was deleted.

142 changes: 142 additions & 0 deletions src/fixtures/app.ts
Original file line number Diff line number Diff line change
@@ -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)
}
28 changes: 28 additions & 0 deletions src/fixtures/sessionData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
})
2 changes: 2 additions & 0 deletions src/fixtures/sites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const MOCK_SITE_NAME_ONE = "test-site-one"
export const MOCK_SITE_NAME_TWO = "test-site-two"
9 changes: 9 additions & 0 deletions src/fixtures/users.ts
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"
export const MOCK_USER_EMAIL_TWO = "[email protected]"
export const MOCK_USER_EMAIL_THREE = "[email protected]"
export const MOCK_USER_EMAIL_FOUR = "[email protected]"

0 comments on commit d9c6159

Please sign in to comment.