-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add integration tests for cookie handling (#36)
* chore: add tests for cookie handling * chore: simplify tests * chore: improve test performance
- Loading branch information
Showing
17 changed files
with
364 additions
and
119 deletions.
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.
Binary file not shown.
Binary file not shown.
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
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
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 |
---|---|---|
@@ -1,53 +1,4 @@ | ||
import { rest } from "msw"; | ||
import { | ||
UsersApiParams, | ||
UsersApiResponse, | ||
LoginApiRequestBody, | ||
LoginApiParams, | ||
LoginApiResponse, | ||
} from "../types/api"; | ||
import sessionHandlers from "./handlers/session"; | ||
import usersHandlers from "./handlers/users"; | ||
|
||
export default [ | ||
rest.get<null, UsersApiParams, UsersApiResponse>( | ||
"/api/users", | ||
(_, response, context) => | ||
response( | ||
context.delay(500), | ||
context.status(200), | ||
context.json([ | ||
{ | ||
id: "bcff5c0e-10b6-407b-94d1-90d741363885", | ||
firstName: "Rhydian", | ||
lastName: "Greig", | ||
}, | ||
{ | ||
id: "b44e89e4-3254-415e-b14a-441166616b20", | ||
firstName: "Alessandro", | ||
lastName: "Metcalfe", | ||
}, | ||
{ | ||
id: "6e369942-6b5d-4159-9b39-729646549183", | ||
firstName: "Erika", | ||
lastName: "Richards", | ||
}, | ||
]) | ||
) | ||
), | ||
rest.post<LoginApiRequestBody, LoginApiParams, LoginApiResponse>( | ||
"/api/login", | ||
async (request, response, context) => { | ||
const { username, password } = await request.json<LoginApiRequestBody>(); | ||
if (username === "peter" && password === "secret") { | ||
return response( | ||
context.delay(500), | ||
context.status(200), | ||
context.json({ | ||
userId: "9138123", | ||
}) | ||
); | ||
} | ||
|
||
return response(context.delay(500), context.status(401)); | ||
} | ||
), | ||
]; | ||
export default [...sessionHandlers, ...usersHandlers]; |
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,83 @@ | ||
import { | ||
SessionData, | ||
GetSessionParams, | ||
GetSessionResponse, | ||
PostSessionRequestBody, | ||
PostSessionParams, | ||
PostSessionResponse, | ||
} from "../../types/session"; | ||
import { rest } from "msw"; | ||
|
||
const VALID_USERNAME = "peter"; | ||
const VALID_PASSWORD = "secret"; | ||
const SESSION_COOKIE_KEY = "x-session"; | ||
|
||
const sessionData: SessionData = { | ||
userId: "9138123", | ||
}; | ||
|
||
const encodeSessionCookie = (username: string, password: string) => | ||
// This isn't secure, please don't do this in production code 😇 | ||
Buffer.from(`${username}:${password}`).toString("base64"); | ||
|
||
const decodeSessionCookie = ( | ||
cookie: string | ||
): { username: string; password: string } => { | ||
const [username, password] = Buffer.from(cookie ?? "", "base64") | ||
.toString() | ||
.split(":"); | ||
return { | ||
username: username ?? null, | ||
password: password ?? null, | ||
}; | ||
}; | ||
|
||
const isValidCredentials = (username: string, password: string): boolean => | ||
username === VALID_USERNAME && password === VALID_PASSWORD; | ||
|
||
const isValidSession = (cookie: string): boolean => { | ||
const { username, password } = decodeSessionCookie(cookie); | ||
return isValidCredentials(username, password); | ||
}; | ||
|
||
export default [ | ||
rest.get<null, GetSessionParams, GetSessionResponse>( | ||
"/api/session", | ||
async (request, response, context) => { | ||
const sessionCookie = request.cookies[SESSION_COOKIE_KEY]; | ||
return isValidSession(sessionCookie) | ||
? response( | ||
context.delay(150), | ||
context.status(200), | ||
context.json(sessionData) | ||
) | ||
: response(context.delay(150), context.status(401)); | ||
} | ||
), | ||
rest.post<PostSessionRequestBody, PostSessionParams, PostSessionResponse>( | ||
"/api/session", | ||
async (request, response, context) => { | ||
const { username, password } = | ||
await request.json<PostSessionRequestBody>(); | ||
if (isValidCredentials(username, password)) { | ||
return response( | ||
context.delay(500), | ||
context.status(200), | ||
context.cookie( | ||
SESSION_COOKIE_KEY, | ||
encodeSessionCookie(username, password) | ||
), | ||
context.json(sessionData) | ||
); | ||
} | ||
|
||
return response(context.delay(500), context.status(401)); | ||
} | ||
), | ||
rest.delete("/api/session", async (request, response, context) => { | ||
const sessionCookie = request.cookies[SESSION_COOKIE_KEY]; | ||
return isValidSession(sessionCookie) | ||
? response(context.status(200), context.cookie(SESSION_COOKIE_KEY, "")) | ||
: response(context.status(401)); | ||
}), | ||
]; |
Oops, something went wrong.