feat: add cookie utils for e2e tests
This commit is contained in:
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user