Skip to content

Commit 58873ff

Browse files
committed
feat: add cookie utils for e2e tests
1 parent 8a173a7 commit 58873ff

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

src/common/util.ts

+36
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,39 @@ export function logError(prefix: string, err: any): void {
120120
logger.error(`${prefix}: ${err}`)
121121
}
122122
}
123+
124+
// Borrowed from playwright
125+
export interface Cookie {
126+
name: string
127+
value: string
128+
domain: string
129+
path: string
130+
/**
131+
* Unix time in seconds.
132+
*/
133+
expires: number
134+
httpOnly: boolean
135+
secure: boolean
136+
sameSite: "Strict" | "Lax" | "None"
137+
}
138+
139+
/**
140+
* Checks if a cookie exists in array of cookies
141+
*/
142+
export function checkForCookie(cookies: Array<Cookie>, key: string): boolean {
143+
// Check for at least one cookie where the name is equal to key
144+
return cookies.filter((cookie) => cookie.name === key).length > 0
145+
}
146+
147+
/**
148+
* Creates a login cookie if one doesn't already exist
149+
*/
150+
export function createCookieIfDoesntExist(cookies: Array<Cookie>, cookieToStore: Cookie): Array<Cookie> {
151+
const cookieName = cookieToStore.name
152+
const doesCookieExist = checkForCookie(cookies, cookieName)
153+
if (!doesCookieExist) {
154+
const updatedCookies = [...cookies, cookieToStore]
155+
return updatedCookies
156+
}
157+
return cookies
158+
}

src/node/routes/login.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { rootPath } from "../constants"
77
import { authenticated, getCookieDomain, redirect, replaceTemplates } from "../http"
88
import { hash, humanPath } from "../util"
99

10-
enum Cookie {
10+
export enum Cookie {
1111
Key = "key",
1212
}
1313

test/util.test.ts

+60-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { normalize } from "../src/common/util"
1+
import { Cookie } from "playwright"
2+
import { checkForCookie, createCookieIfDoesntExist, normalize } from "../src/common/util"
3+
import { Cookie as CookieEnum } from "../src/node/routes/login"
4+
import { hash } from "../src/node/util"
25

36
describe("util", () => {
47
describe("normalize", () => {
@@ -15,4 +18,60 @@ describe("util", () => {
1518
expect(normalize("qux", true)).toBe("qux")
1619
})
1720
})
21+
22+
describe("checkForCookie", () => {
23+
it("should check if the cookie exists and has a value", () => {
24+
const PASSWORD = "123supersecure!"
25+
const fakeCookies: Cookie[] = [
26+
{
27+
name: CookieEnum.Key,
28+
value: hash(PASSWORD),
29+
domain: "localhost",
30+
secure: false,
31+
sameSite: "Lax",
32+
httpOnly: false,
33+
expires: 18000,
34+
path: "/",
35+
},
36+
]
37+
expect(checkForCookie(fakeCookies, CookieEnum.Key)).toBe(true)
38+
})
39+
it("should return false if there are no cookies", () => {
40+
const fakeCookies: Cookie[] = []
41+
expect(checkForCookie(fakeCookies, "key")).toBe(false)
42+
})
43+
})
44+
45+
describe("createCookieIfDoesntExist", () => {
46+
it("should create a cookie if it doesn't exist", () => {
47+
const PASSWORD = "123supersecure"
48+
const cookies: Cookie[] = []
49+
const cookieToStore = {
50+
name: CookieEnum.Key,
51+
value: hash(PASSWORD),
52+
domain: "localhost",
53+
secure: false,
54+
sameSite: "Lax" as const,
55+
httpOnly: false,
56+
expires: 18000,
57+
path: "/",
58+
}
59+
expect(createCookieIfDoesntExist(cookies, cookieToStore)).toStrictEqual([cookieToStore])
60+
})
61+
it("should return the same cookies if the cookie already exists", () => {
62+
const PASSWORD = "123supersecure"
63+
const cookieToStore = {
64+
name: CookieEnum.Key,
65+
value: hash(PASSWORD),
66+
domain: "localhost",
67+
secure: false,
68+
sameSite: "Lax" as const,
69+
httpOnly: false,
70+
expires: 18000,
71+
path: "/",
72+
}
73+
const cookies: Cookie[] = [cookieToStore]
74+
expect(createCookieIfDoesntExist(cookies, cookieToStore)).toStrictEqual(cookies)
75+
})
76+
})
1877
})

0 commit comments

Comments
 (0)