11import { TestBed } from '@angular/core/testing' ;
2+ import { Router } from '@angular/router' ;
23import { AuthService } from '../services/auth.service' ;
34import { authGuard } from './auth.guard' ;
45
@@ -7,50 +8,69 @@ describe('authGuard', () => {
78 const authServiceMock = {
89 isAuthenticated : ( ) => true ,
910 authSession : ( ) => null ,
10- canStartDebugSession : ( ) => false ,
11- startLoginRedirect : jasmine . createSpy ( 'startLoginRedirect' ) . and . resolveTo ( )
11+ canStartDebugSession : ( ) => false
12+ } ;
13+
14+ const routerMock = {
15+ createUrlTree : jasmine . createSpy ( 'createUrlTree' )
1216 } ;
1317
1418 TestBed . configureTestingModule ( {
15- providers : [ { provide : AuthService , useValue : authServiceMock } ]
19+ providers : [
20+ { provide : AuthService , useValue : authServiceMock } ,
21+ { provide : Router , useValue : routerMock }
22+ ]
1623 } ) ;
1724
1825 const canActivate = TestBed . runInInjectionContext ( ( ) => authGuard ( { } as never , { } as never ) ) ;
1926 expect ( canActivate ) . toBeTrue ( ) ;
20- expect ( authServiceMock . startLoginRedirect ) . not . toHaveBeenCalled ( ) ;
27+ expect ( routerMock . createUrlTree ) . not . toHaveBeenCalled ( ) ;
2128 } ) ;
2229
23- it ( 'starts login redirect when user is not authenticated' , ( ) => {
30+ it ( 'redirects to unauthorized when user is not authenticated' , ( ) => {
31+ const redirectTree = { } as never ;
2432 const authServiceMock = {
2533 isAuthenticated : ( ) => false ,
2634 authSession : ( ) => null ,
27- canStartDebugSession : ( ) => false ,
28- startLoginRedirect : jasmine . createSpy ( 'startLoginRedirect' ) . and . resolveTo ( )
35+ canStartDebugSession : ( ) => false
36+ } ;
37+
38+ const routerMock = {
39+ createUrlTree : jasmine . createSpy ( 'createUrlTree' ) . and . returnValue ( redirectTree )
2940 } ;
3041
3142 TestBed . configureTestingModule ( {
32- providers : [ { provide : AuthService , useValue : authServiceMock } ]
43+ providers : [
44+ { provide : AuthService , useValue : authServiceMock } ,
45+ { provide : Router , useValue : routerMock }
46+ ]
3347 } ) ;
3448
3549 const canActivate = TestBed . runInInjectionContext ( ( ) => authGuard ( { } as never , { } as never ) ) ;
36- expect ( canActivate ) . toBeFalse ( ) ;
37- expect ( authServiceMock . startLoginRedirect ) . toHaveBeenCalled ( ) ;
50+ expect ( canActivate ) . toBe ( redirectTree ) ;
51+ expect ( routerMock . createUrlTree ) . toHaveBeenCalledWith ( [ '/unauthorized' ] ) ;
3852 } ) ;
3953
4054 it ( 'allows navigation when debug session is active in preview mode' , ( ) => {
4155 const authServiceMock = {
4256 isAuthenticated : ( ) => false ,
4357 authSession : ( ) => ( { isDebugSession : true } ) ,
44- canStartDebugSession : ( ) => true ,
45- startLoginRedirect : jasmine . createSpy ( 'startLoginRedirect' ) . and . resolveTo ( )
58+ canStartDebugSession : ( ) => true
59+ } ;
60+
61+ const routerMock = {
62+ createUrlTree : jasmine . createSpy ( 'createUrlTree' )
4663 } ;
4764
4865 TestBed . configureTestingModule ( {
49- providers : [ { provide : AuthService , useValue : authServiceMock } ]
66+ providers : [
67+ { provide : AuthService , useValue : authServiceMock } ,
68+ { provide : Router , useValue : routerMock }
69+ ]
5070 } ) ;
5171
5272 const canActivate = TestBed . runInInjectionContext ( ( ) => authGuard ( { } as never , { } as never ) ) ;
5373 expect ( canActivate ) . toBeTrue ( ) ;
54- expect ( authServiceMock . startLoginRedirect ) . not . toHaveBeenCalled ( ) ;
74+ expect ( routerMock . createUrlTree ) . not . toHaveBeenCalled ( ) ;
5575 } ) ;
5676} ) ;
0 commit comments