11import { 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
45import { 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
1823const dom = new JSDOM ( )
1924global . 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