1
1
import { JSDOM } from "jsdom"
2
+ import { Cookie } from "playwright"
2
3
// Note: we need to import logger from the root
3
4
// because this is the logger used in logError in ../src/common/util
4
5
import { logger } from "../node_modules/@coder/logger"
@@ -8,12 +9,16 @@ import {
8
9
getFirstString ,
9
10
getOptions ,
10
11
logError ,
11
- normalize ,
12
12
plural ,
13
13
resolveBase ,
14
14
split ,
15
15
trimSlashes ,
16
+ checkForCookie ,
17
+ createCookieIfDoesntExist ,
18
+ normalize ,
16
19
} from "../src/common/util"
20
+ import { Cookie as CookieEnum } from "../src/node/routes/login"
21
+ import { hash } from "../src/node/util"
17
22
18
23
const dom = new JSDOM ( )
19
24
global . document = dom . window . document
@@ -255,4 +260,60 @@ describe("util", () => {
255
260
expect ( spy ) . toHaveBeenCalledWith ( "api: oh no" )
256
261
} )
257
262
} )
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
+ } )
258
319
} )
0 commit comments