1- import { normalize } from "../src/common/util"
1+ import { Cookie } from "playwright"
2+ import { checkForCookie , createCookieIfDoesntExist , normalize } from "../src/common/util"
3+ import { Cookie as CookieEnum } from "../src/node/routes/login"
4+ import { hash } from "../src/node/util"
25
36describe ( "util" , ( ) => {
47 describe ( "normalize" , ( ) => {
@@ -15,4 +18,60 @@ describe("util", () => {
1518 expect ( normalize ( "qux" , true ) ) . toBe ( "qux" )
1619 } )
1720 } )
21+
22+ describe ( "checkForCookie" , ( ) => {
23+ it ( "should check if the cookie exists and has a value" , ( ) => {
24+ const PASSWORD = "123supersecure!"
25+ const fakeCookies : Cookie [ ] = [
26+ {
27+ name : CookieEnum . Key ,
28+ value : hash ( PASSWORD ) ,
29+ domain : "localhost" ,
30+ secure : false ,
31+ sameSite : "Lax" ,
32+ httpOnly : false ,
33+ expires : 18000 ,
34+ path : "/" ,
35+ } ,
36+ ]
37+ expect ( checkForCookie ( fakeCookies , CookieEnum . Key ) ) . toBe ( true )
38+ } )
39+ it ( "should return false if there are no cookies" , ( ) => {
40+ const fakeCookies : Cookie [ ] = [ ]
41+ expect ( checkForCookie ( fakeCookies , "key" ) ) . toBe ( false )
42+ } )
43+ } )
44+
45+ describe ( "createCookieIfDoesntExist" , ( ) => {
46+ it ( "should create a cookie if it doesn't exist" , ( ) => {
47+ const PASSWORD = "123supersecure"
48+ const cookies : Cookie [ ] = [ ]
49+ const cookieToStore = {
50+ name : CookieEnum . Key ,
51+ value : hash ( PASSWORD ) ,
52+ domain : "localhost" ,
53+ secure : false ,
54+ sameSite : "Lax" as const ,
55+ httpOnly : false ,
56+ expires : 18000 ,
57+ path : "/" ,
58+ }
59+ expect ( createCookieIfDoesntExist ( cookies , cookieToStore ) ) . toStrictEqual ( [ cookieToStore ] )
60+ } )
61+ it ( "should return the same cookies if the cookie already exists" , ( ) => {
62+ const PASSWORD = "123supersecure"
63+ const cookieToStore = {
64+ name : CookieEnum . Key ,
65+ value : hash ( PASSWORD ) ,
66+ domain : "localhost" ,
67+ secure : false ,
68+ sameSite : "Lax" as const ,
69+ httpOnly : false ,
70+ expires : 18000 ,
71+ path : "/" ,
72+ }
73+ const cookies : Cookie [ ] = [ cookieToStore ]
74+ expect ( createCookieIfDoesntExist ( cookies , cookieToStore ) ) . toStrictEqual ( cookies )
75+ } )
76+ } )
1877} )
0 commit comments