Archived
1
0

feat: add cookie utils for e2e tests

This commit is contained in:
Joe Previte
2021-02-04 14:53:54 -07:00
parent e077f2d97f
commit b02d2fb3cc
3 changed files with 99 additions and 2 deletions

View File

@ -120,3 +120,39 @@ export function logError(prefix: string, err: any): void {
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
}

View File

@ -7,7 +7,7 @@ import { rootPath } from "../constants"
import { authenticated, getCookieDomain, redirect, replaceTemplates } from "../http"
import { hash, humanPath } from "../util"
enum Cookie {
export enum Cookie {
Key = "key",
}