From b02d2fb3ccabbbb941d06e6606a7abb8113e98a9 Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Thu, 4 Feb 2021 14:53:54 -0700 Subject: [PATCH] feat: add cookie utils for e2e tests --- src/common/util.ts | 36 +++++++++++++++++++++++ src/node/routes/login.ts | 2 +- test/util.test.ts | 63 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 99 insertions(+), 2 deletions(-) diff --git a/src/common/util.ts b/src/common/util.ts index 87ca6f596..d0e03ab3c 100644 --- a/src/common/util.ts +++ b/src/common/util.ts @@ -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, 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, cookieToStore: Cookie): Array { + const cookieName = cookieToStore.name + const doesCookieExist = checkForCookie(cookies, cookieName) + if (!doesCookieExist) { + const updatedCookies = [...cookies, cookieToStore] + return updatedCookies + } + return cookies +} diff --git a/src/node/routes/login.ts b/src/node/routes/login.ts index c3ad12adb..b89470aea 100644 --- a/src/node/routes/login.ts +++ b/src/node/routes/login.ts @@ -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", } diff --git a/test/util.test.ts b/test/util.test.ts index 78985554e..91a4315f6 100644 --- a/test/util.test.ts +++ b/test/util.test.ts @@ -1,4 +1,5 @@ import { JSDOM } from "jsdom" +import { Cookie } from "playwright" // Note: we need to import logger from the root // because this is the logger used in logError in ../src/common/util import { logger } from "../node_modules/@coder/logger" @@ -8,12 +9,16 @@ import { getFirstString, getOptions, logError, - normalize, plural, resolveBase, split, trimSlashes, + checkForCookie, + createCookieIfDoesntExist, + normalize, } from "../src/common/util" +import { Cookie as CookieEnum } from "../src/node/routes/login" +import { hash } from "../src/node/util" const dom = new JSDOM() global.document = dom.window.document @@ -255,4 +260,60 @@ describe("util", () => { 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) + }) + }) })