-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,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) | ||
} |
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,2 @@ | ||
export const MOCK_SITE_NAME_ONE = "test-site-one" | ||
export const MOCK_SITE_NAME_TWO = "test-site-two" |
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,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]" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 oneThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 960bd2e!