Skip to content

Commit b02d2fb

Browse files
committed
feat: add cookie utils for e2e tests
1 parent e077f2d commit b02d2fb

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-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

+62-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { JSDOM } from "jsdom"
2+
import { Cookie } from "playwright"
23
// Note: we need to import logger from the root
34
// because this is the logger used in logError in ../src/common/util
45
import { logger } from "../node_modules/@coder/logger"
@@ -8,12 +9,16 @@ import {
89
getFirstString,
910
getOptions,
1011
logError,
11-
normalize,
1212
plural,
1313
resolveBase,
1414
split,
1515
trimSlashes,
16+
checkForCookie,
17+
createCookieIfDoesntExist,
18+
normalize,
1619
} from "../src/common/util"
20+
import { Cookie as CookieEnum } from "../src/node/routes/login"
21+
import { hash } from "../src/node/util"
1722

1823
const dom = new JSDOM()
1924
global.document = dom.window.document
@@ -255,4 +260,60 @@ describe("util", () => {
255260
expect(spy).toHaveBeenCalledWith("api: oh no")
256261
})
257262
})
263+
264+
describe("checkForCookie", () => {
265+
it("should check if the cookie exists and has a value", () => {
266+
const PASSWORD = "123supersecure!"
267+
const fakeCookies: Cookie[] = [
268+
{
269+
name: CookieEnum.Key,
270+
value: hash(PASSWORD),
271+
domain: "localhost",
272+
secure: false,
273+
sameSite: "Lax",
274+
httpOnly: false,
275+
expires: 18000,
276+
path: "/",
277+
},
278+
]
279+
expect(checkForCookie(fakeCookies, CookieEnum.Key)).toBe(true)
280+
})
281+
it("should return false if there are no cookies", () => {
282+
const fakeCookies: Cookie[] = []
283+
expect(checkForCookie(fakeCookies, "key")).toBe(false)
284+
})
285+
})
286+
287+
describe("createCookieIfDoesntExist", () => {
288+
it("should create a cookie if it doesn't exist", () => {
289+
const PASSWORD = "123supersecure"
290+
const cookies: Cookie[] = []
291+
const cookieToStore = {
292+
name: CookieEnum.Key,
293+
value: hash(PASSWORD),
294+
domain: "localhost",
295+
secure: false,
296+
sameSite: "Lax" as const,
297+
httpOnly: false,
298+
expires: 18000,
299+
path: "/",
300+
}
301+
expect(createCookieIfDoesntExist(cookies, cookieToStore)).toStrictEqual([cookieToStore])
302+
})
303+
it("should return the same cookies if the cookie already exists", () => {
304+
const PASSWORD = "123supersecure"
305+
const cookieToStore = {
306+
name: CookieEnum.Key,
307+
value: hash(PASSWORD),
308+
domain: "localhost",
309+
secure: false,
310+
sameSite: "Lax" as const,
311+
httpOnly: false,
312+
expires: 18000,
313+
path: "/",
314+
}
315+
const cookies: Cookie[] = [cookieToStore]
316+
expect(createCookieIfDoesntExist(cookies, cookieToStore)).toStrictEqual(cookies)
317+
})
318+
})
258319
})

0 commit comments

Comments
 (0)