Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(fixtures): allow generating router for different user types #574

Merged
merged 3 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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",
Expand Down Expand Up @@ -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",
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()
}
Comment on lines +21 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought - I think it might still be useful to keep this around as a generic option for router tests that don't need to test different user profiles.

As discussed offline, we might consider making one with SCREAMING_SNAKE_CASE (to indicate that these are constants) for future use and deprecate this one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 960bd2e!


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]"