feat: add cookie utils for e2e tests
This commit is contained in:
parent
e077f2d97f
commit
b02d2fb3cc
@ -120,3 +120,39 @@ export function logError(prefix: string, err: any): void {
|
|||||||
logger.error(`${prefix}: ${err}`)
|
logger.error(`${prefix}: ${err}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Borrowed from playwright
|
||||||
|
export interface Cookie {
|
||||||
|
name: string
|
||||||
|
value: string
|
||||||
|
domain: string
|
||||||
|
path: string
|
||||||
|
/**
|
||||||
|
* Unix time in seconds.
|
||||||
|
*/
|
||||||
|
expires: number
|
||||||
|
httpOnly: boolean
|
||||||
|
secure: boolean
|
||||||
|
sameSite: "Strict" | "Lax" | "None"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a cookie exists in array of cookies
|
||||||
|
*/
|
||||||
|
export function checkForCookie(cookies: Array<Cookie>, key: string): boolean {
|
||||||
|
// Check for at least one cookie where the name is equal to key
|
||||||
|
return cookies.filter((cookie) => cookie.name === key).length > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a login cookie if one doesn't already exist
|
||||||
|
*/
|
||||||
|
export function createCookieIfDoesntExist(cookies: Array<Cookie>, cookieToStore: Cookie): Array<Cookie> {
|
||||||
|
const cookieName = cookieToStore.name
|
||||||
|
const doesCookieExist = checkForCookie(cookies, cookieName)
|
||||||
|
if (!doesCookieExist) {
|
||||||
|
const updatedCookies = [...cookies, cookieToStore]
|
||||||
|
return updatedCookies
|
||||||
|
}
|
||||||
|
return cookies
|
||||||
|
}
|
||||||
|
@ -7,7 +7,7 @@ import { rootPath } from "../constants"
|
|||||||
import { authenticated, getCookieDomain, redirect, replaceTemplates } from "../http"
|
import { authenticated, getCookieDomain, redirect, replaceTemplates } from "../http"
|
||||||
import { hash, humanPath } from "../util"
|
import { hash, humanPath } from "../util"
|
||||||
|
|
||||||
enum Cookie {
|
export enum Cookie {
|
||||||
Key = "key",
|
Key = "key",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { JSDOM } from "jsdom"
|
import { JSDOM } from "jsdom"
|
||||||
|
import { Cookie } from "playwright"
|
||||||
// Note: we need to import logger from the root
|
// Note: we need to import logger from the root
|
||||||
// because this is the logger used in logError in ../src/common/util
|
// because this is the logger used in logError in ../src/common/util
|
||||||
import { logger } from "../node_modules/@coder/logger"
|
import { logger } from "../node_modules/@coder/logger"
|
||||||
@ -8,12 +9,16 @@ import {
|
|||||||
getFirstString,
|
getFirstString,
|
||||||
getOptions,
|
getOptions,
|
||||||
logError,
|
logError,
|
||||||
normalize,
|
|
||||||
plural,
|
plural,
|
||||||
resolveBase,
|
resolveBase,
|
||||||
split,
|
split,
|
||||||
trimSlashes,
|
trimSlashes,
|
||||||
|
checkForCookie,
|
||||||
|
createCookieIfDoesntExist,
|
||||||
|
normalize,
|
||||||
} from "../src/common/util"
|
} from "../src/common/util"
|
||||||
|
import { Cookie as CookieEnum } from "../src/node/routes/login"
|
||||||
|
import { hash } from "../src/node/util"
|
||||||
|
|
||||||
const dom = new JSDOM()
|
const dom = new JSDOM()
|
||||||
global.document = dom.window.document
|
global.document = dom.window.document
|
||||||
@ -255,4 +260,60 @@ describe("util", () => {
|
|||||||
expect(spy).toHaveBeenCalledWith("api: oh no")
|
expect(spy).toHaveBeenCalledWith("api: oh no")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("checkForCookie", () => {
|
||||||
|
it("should check if the cookie exists and has a value", () => {
|
||||||
|
const PASSWORD = "123supersecure!"
|
||||||
|
const fakeCookies: Cookie[] = [
|
||||||
|
{
|
||||||
|
name: CookieEnum.Key,
|
||||||
|
value: hash(PASSWORD),
|
||||||
|
domain: "localhost",
|
||||||
|
secure: false,
|
||||||
|
sameSite: "Lax",
|
||||||
|
httpOnly: false,
|
||||||
|
expires: 18000,
|
||||||
|
path: "/",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
expect(checkForCookie(fakeCookies, CookieEnum.Key)).toBe(true)
|
||||||
|
})
|
||||||
|
it("should return false if there are no cookies", () => {
|
||||||
|
const fakeCookies: Cookie[] = []
|
||||||
|
expect(checkForCookie(fakeCookies, "key")).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("createCookieIfDoesntExist", () => {
|
||||||
|
it("should create a cookie if it doesn't exist", () => {
|
||||||
|
const PASSWORD = "123supersecure"
|
||||||
|
const cookies: Cookie[] = []
|
||||||
|
const cookieToStore = {
|
||||||
|
name: CookieEnum.Key,
|
||||||
|
value: hash(PASSWORD),
|
||||||
|
domain: "localhost",
|
||||||
|
secure: false,
|
||||||
|
sameSite: "Lax" as const,
|
||||||
|
httpOnly: false,
|
||||||
|
expires: 18000,
|
||||||
|
path: "/",
|
||||||
|
}
|
||||||
|
expect(createCookieIfDoesntExist(cookies, cookieToStore)).toStrictEqual([cookieToStore])
|
||||||
|
})
|
||||||
|
it("should return the same cookies if the cookie already exists", () => {
|
||||||
|
const PASSWORD = "123supersecure"
|
||||||
|
const cookieToStore = {
|
||||||
|
name: CookieEnum.Key,
|
||||||
|
value: hash(PASSWORD),
|
||||||
|
domain: "localhost",
|
||||||
|
secure: false,
|
||||||
|
sameSite: "Lax" as const,
|
||||||
|
httpOnly: false,
|
||||||
|
expires: 18000,
|
||||||
|
path: "/",
|
||||||
|
}
|
||||||
|
const cookies: Cookie[] = [cookieToStore]
|
||||||
|
expect(createCookieIfDoesntExist(cookies, cookieToStore)).toStrictEqual(cookies)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user