@@ -62,7 +62,7 @@ describe('CookieAuthGuard', () => {
6262 it ( 'should return true for non-cookie auth routes' , ( ) => {
6363 reflector . getAllAndOverride
6464 . mockReturnValueOnce ( false ) // isPublic = false
65- . mockReturnValueOnce ( false ) ; // isCookieAuth = false
65+ . mockReturnValueOnce ( null ) ; // cookieAuthOptions = null
6666 const context = createMockExecutionContext ( ) ;
6767
6868 const result = guard . canActivate ( context ) ;
@@ -73,7 +73,7 @@ describe('CookieAuthGuard', () => {
7373 it ( 'should throw UnauthorizedException when no token cookie is provided' , ( ) => {
7474 reflector . getAllAndOverride
7575 . mockReturnValueOnce ( false ) // isPublic = false
76- . mockReturnValueOnce ( true ) ; // isCookieAuth = true
76+ . mockReturnValueOnce ( { enabled : true } ) ; // cookieAuthOptions with default onAuthFail = 'reject'
7777 const context = createMockExecutionContext ( ) ;
7878
7979 expect ( ( ) => guard . canActivate ( context ) ) . toThrow (
@@ -84,7 +84,7 @@ describe('CookieAuthGuard', () => {
8484 it ( 'should throw UnauthorizedException when token is invalid' , ( ) => {
8585 reflector . getAllAndOverride
8686 . mockReturnValueOnce ( false ) // isPublic = false
87- . mockReturnValueOnce ( true ) ; // isCookieAuth = true
87+ . mockReturnValueOnce ( { enabled : true } ) ; // cookieAuthOptions with default onAuthFail = 'reject'
8888 const context = createMockExecutionContext ( { token : 'invalid-token' } ) ;
8989 authService . jwtVerify . mockImplementation ( ( ) => {
9090 throw new Error ( 'Invalid token' ) ;
@@ -98,7 +98,7 @@ describe('CookieAuthGuard', () => {
9898 it ( 'should throw UnauthorizedException when token payload is invalid' , ( ) => {
9999 reflector . getAllAndOverride
100100 . mockReturnValueOnce ( false ) // isPublic = false
101- . mockReturnValueOnce ( true ) ; // isCookieAuth = true
101+ . mockReturnValueOnce ( { enabled : true } ) ; // cookieAuthOptions with default onAuthFail = 'reject'
102102 const context = createMockExecutionContext ( { token : 'valid-token' } ) ;
103103 authService . jwtVerify . mockReturnValue ( { } ) ; // No sub field
104104
@@ -110,7 +110,7 @@ describe('CookieAuthGuard', () => {
110110 it ( 'should successfully authenticate with valid token and set cookie auth data' , ( ) => {
111111 reflector . getAllAndOverride
112112 . mockReturnValueOnce ( false ) // isPublic = false
113- . mockReturnValueOnce ( true ) ; // isCookieAuth = true
113+ . mockReturnValueOnce ( { enabled : true } ) ; // cookieAuthOptions with default onAuthFail = 'reject'
114114 const context = createMockExecutionContext ( { token : 'valid-token' } ) ;
115115 authService . jwtVerify . mockReturnValue ( {
116116 sub : 'user-123' ,
@@ -133,7 +133,7 @@ describe('CookieAuthGuard', () => {
133133 it ( 'should handle token with missing optional fields' , ( ) => {
134134 reflector . getAllAndOverride
135135 . mockReturnValueOnce ( false ) // isPublic = false
136- . mockReturnValueOnce ( true ) ; // isCookieAuth = true
136+ . mockReturnValueOnce ( { enabled : true } ) ; // cookieAuthOptions with default onAuthFail = 'reject'
137137 const context = createMockExecutionContext ( { token : 'valid-token' } ) ;
138138 authService . jwtVerify . mockReturnValue ( {
139139 sub : 'user-123' ,
@@ -150,4 +150,41 @@ describe('CookieAuthGuard', () => {
150150 username : undefined ,
151151 } ) ;
152152 } ) ;
153+
154+ it ( 'should continue without authentication when onAuthFail is continue and no token' , ( ) => {
155+ reflector . getAllAndOverride
156+ . mockReturnValueOnce ( false ) // isPublic = false
157+ . mockReturnValueOnce ( { enabled : true , onAuthFail : 'continue' } ) ; // cookieAuthOptions with onAuthFail = 'continue'
158+ const context = createMockExecutionContext ( ) ;
159+
160+ const result = guard . canActivate ( context ) ;
161+
162+ expect ( result ) . toBe ( true ) ;
163+ } ) ;
164+
165+ it ( 'should continue without authentication when onAuthFail is continue and token is invalid' , ( ) => {
166+ reflector . getAllAndOverride
167+ . mockReturnValueOnce ( false ) // isPublic = false
168+ . mockReturnValueOnce ( { enabled : true , onAuthFail : 'continue' } ) ; // cookieAuthOptions with onAuthFail = 'continue'
169+ const context = createMockExecutionContext ( { token : 'invalid-token' } ) ;
170+ authService . jwtVerify . mockImplementation ( ( ) => {
171+ throw new Error ( 'Invalid token' ) ;
172+ } ) ;
173+
174+ const result = guard . canActivate ( context ) ;
175+
176+ expect ( result ) . toBe ( true ) ;
177+ } ) ;
178+
179+ it ( 'should continue without authentication when onAuthFail is continue and token payload is invalid' , ( ) => {
180+ reflector . getAllAndOverride
181+ . mockReturnValueOnce ( false ) // isPublic = false
182+ . mockReturnValueOnce ( { enabled : true , onAuthFail : 'continue' } ) ; // cookieAuthOptions with onAuthFail = 'continue'
183+ const context = createMockExecutionContext ( { token : 'valid-token' } ) ;
184+ authService . jwtVerify . mockReturnValue ( { } ) ; // No sub field
185+
186+ const result = guard . canActivate ( context ) ;
187+
188+ expect ( result ) . toBe ( true ) ;
189+ } ) ;
153190} ) ;
0 commit comments