@@ -2,6 +2,8 @@ import { TestBed, waitForAsync } from '@angular/core/testing';
22import { Router , RouterStateSnapshot , UrlSegment } from '@angular/router' ;
33import { RouterTestingModule } from '@angular/router/testing' ;
44import { of } from 'rxjs' ;
5+ import { AuthStateService } from '../authState/auth-state.service' ;
6+ import { AuthStateServiceMock } from '../authState/auth-state.service-mock' ;
57import { CheckAuthService } from '../check-auth.service' ;
68import { CheckAuthServiceMock } from '../check-auth.service-mock' ;
79import { LoginService } from '../login/login.service' ;
@@ -13,13 +15,15 @@ describe(`AutoLoginGuard`, () => {
1315 let autoLoginGuard : AutoLoginGuard ;
1416 let checkAuthService : CheckAuthService ;
1517 let loginService : LoginService ;
18+ let authStateService : AuthStateService ;
1619 let router : Router ;
1720
1821 beforeEach ( ( ) => {
1922 TestBed . configureTestingModule ( {
2023 imports : [ RouterTestingModule ] ,
2124 providers : [
2225 AutoLoginService ,
26+ { provide : AuthStateService , useClass : AuthStateServiceMock } ,
2327 {
2428 provide : LoginService ,
2529 useClass : LoginServiceMock ,
@@ -35,6 +39,7 @@ describe(`AutoLoginGuard`, () => {
3539 beforeEach ( ( ) => {
3640 autoLoginGuard = TestBed . inject ( AutoLoginGuard ) ;
3741 checkAuthService = TestBed . inject ( CheckAuthService ) ;
42+ authStateService = TestBed . inject ( AuthStateService ) ;
3843 router = TestBed . inject ( Router ) ;
3944 loginService = TestBed . inject ( LoginService ) ;
4045 } ) ;
@@ -49,8 +54,9 @@ describe(`AutoLoginGuard`, () => {
4954
5055 describe ( 'canActivate' , ( ) => {
5156 it (
52- 'should call checkAuth() ' ,
57+ 'should call checkAuth() if not authenticated already ' ,
5358 waitForAsync ( ( ) => {
59+ spyOn ( authStateService , 'areAuthStorageTokensValid' ) . and . returnValue ( false ) ;
5460 const checkAuthServiceSpy = spyOn ( checkAuthService , 'checkAuth' ) . and . returnValue ( of ( null ) ) ;
5561
5662 autoLoginGuard . canActivate ( null , { url : 'some-url1' } as RouterStateSnapshot ) . subscribe ( ( ) => {
@@ -59,9 +65,22 @@ describe(`AutoLoginGuard`, () => {
5965 } )
6066 ) ;
6167
68+ it (
69+ 'should NOT call checkAuth() if authenticated already' ,
70+ waitForAsync ( ( ) => {
71+ spyOn ( authStateService , 'areAuthStorageTokensValid' ) . and . returnValue ( true ) ;
72+ const checkAuthServiceSpy = spyOn ( checkAuthService , 'checkAuth' ) . and . returnValue ( of ( null ) ) ;
73+
74+ autoLoginGuard . canActivate ( null , { url : 'some-url2' } as RouterStateSnapshot ) . subscribe ( ( ) => {
75+ expect ( checkAuthServiceSpy ) . not . toHaveBeenCalled ( ) ;
76+ } ) ;
77+ } )
78+ ) ;
79+
6280 it (
6381 'should call loginService.login() when not authorized' ,
6482 waitForAsync ( ( ) => {
83+ spyOn ( authStateService , 'areAuthStorageTokensValid' ) . and . returnValue ( false ) ;
6584 spyOn ( checkAuthService , 'checkAuth' ) . and . returnValue ( of ( null ) ) ;
6685 const loginSpy = spyOn ( loginService , 'login' ) ;
6786
@@ -74,6 +93,7 @@ describe(`AutoLoginGuard`, () => {
7493 it (
7594 'should return false when not authorized' ,
7695 waitForAsync ( ( ) => {
96+ spyOn ( authStateService , 'areAuthStorageTokensValid' ) . and . returnValue ( false ) ;
7797 spyOn ( checkAuthService , 'checkAuth' ) . and . returnValue ( of ( null ) ) ;
7898
7999 autoLoginGuard . canActivate ( null , { url : 'some-url4' } as RouterStateSnapshot ) . subscribe ( ( result ) => {
@@ -85,6 +105,7 @@ describe(`AutoLoginGuard`, () => {
85105 it (
86106 'if no route is stored, setItem on localStorage is called' ,
87107 waitForAsync ( ( ) => {
108+ spyOn ( authStateService , 'areAuthStorageTokensValid' ) . and . returnValue ( false ) ;
88109 spyOn ( checkAuthService , 'checkAuth' ) . and . returnValue ( of ( null ) ) ;
89110 const localStorageSpy = spyOn ( localStorage , 'setItem' ) ;
90111
@@ -110,6 +131,7 @@ describe(`AutoLoginGuard`, () => {
110131 it (
111132 'if authorized and stored route exists: remove item, navigate to route and return true' ,
112133 waitForAsync ( ( ) => {
134+ spyOn ( authStateService , 'areAuthStorageTokensValid' ) . and . returnValue ( false ) ;
113135 spyOn ( checkAuthService , 'checkAuth' ) . and . returnValue ( of ( true ) ) ;
114136 spyOn ( localStorage , 'getItem' ) . and . returnValue ( 'stored-route' ) ;
115137 const localStorageSpy = spyOn ( localStorage , 'removeItem' ) ;
@@ -128,18 +150,33 @@ describe(`AutoLoginGuard`, () => {
128150
129151 describe ( 'canLoad' , ( ) => {
130152 it (
131- 'should call checkAuth()' ,
153+ 'should call checkAuth() if not authenticated already ' ,
132154 waitForAsync ( ( ) => {
155+ spyOn ( authStateService , 'areAuthStorageTokensValid' ) . and . returnValue ( false ) ;
133156 const checkAuthServiceSpy = spyOn ( checkAuthService , 'checkAuth' ) . and . returnValue ( of ( null ) ) ;
134157
135158 autoLoginGuard . canLoad ( null , [ ] ) . subscribe ( ( ) => {
136159 expect ( checkAuthServiceSpy ) . toHaveBeenCalledTimes ( 1 ) ;
137160 } ) ;
138161 } )
139162 ) ;
163+
164+ it (
165+ 'should NOT call checkAuth() if authenticated already' ,
166+ waitForAsync ( ( ) => {
167+ const checkAuthServiceSpy = spyOn ( checkAuthService , 'checkAuth' ) . and . returnValue ( of ( null ) ) ;
168+ spyOn ( authStateService , 'areAuthStorageTokensValid' ) . and . returnValue ( true ) ;
169+
170+ autoLoginGuard . canLoad ( null , [ ] ) . subscribe ( ( ) => {
171+ expect ( checkAuthServiceSpy ) . not . toHaveBeenCalled ( ) ;
172+ } ) ;
173+ } )
174+ ) ;
175+
140176 it (
141177 'should call loginService.login() when not authorized' ,
142178 waitForAsync ( ( ) => {
179+ spyOn ( authStateService , 'areAuthStorageTokensValid' ) . and . returnValue ( false ) ;
143180 spyOn ( checkAuthService , 'checkAuth' ) . and . returnValue ( of ( null ) ) ;
144181 const loginSpy = spyOn ( loginService , 'login' ) ;
145182
@@ -152,6 +189,7 @@ describe(`AutoLoginGuard`, () => {
152189 it (
153190 'should return false when not authorized' ,
154191 waitForAsync ( ( ) => {
192+ spyOn ( authStateService , 'areAuthStorageTokensValid' ) . and . returnValue ( false ) ;
155193 spyOn ( checkAuthService , 'checkAuth' ) . and . returnValue ( of ( null ) ) ;
156194
157195 autoLoginGuard . canLoad ( null , [ ] ) . subscribe ( ( result ) => {
@@ -163,6 +201,7 @@ describe(`AutoLoginGuard`, () => {
163201 it (
164202 'if no route is stored, setItem on localStorage is called' ,
165203 waitForAsync ( ( ) => {
204+ spyOn ( authStateService , 'areAuthStorageTokensValid' ) . and . returnValue ( false ) ;
166205 spyOn ( checkAuthService , 'checkAuth' ) . and . returnValue ( of ( null ) ) ;
167206 const localStorageSpy = spyOn ( localStorage , 'setItem' ) ;
168207
@@ -175,6 +214,7 @@ describe(`AutoLoginGuard`, () => {
175214 it (
176215 'if no route is stored, setItem on localStorage is called, multiple params' ,
177216 waitForAsync ( ( ) => {
217+ spyOn ( authStateService , 'areAuthStorageTokensValid' ) . and . returnValue ( false ) ;
178218 spyOn ( checkAuthService , 'checkAuth' ) . and . returnValue ( of ( null ) ) ;
179219 const localStorageSpy = spyOn ( localStorage , 'setItem' ) ;
180220
@@ -202,6 +242,7 @@ describe(`AutoLoginGuard`, () => {
202242 it (
203243 'if authorized and stored route exists: remove item, navigate to route and return true' ,
204244 waitForAsync ( ( ) => {
245+ spyOn ( authStateService , 'areAuthStorageTokensValid' ) . and . returnValue ( false ) ;
205246 spyOn ( checkAuthService , 'checkAuth' ) . and . returnValue ( of ( true ) ) ;
206247 spyOn ( localStorage , 'getItem' ) . and . returnValue ( 'stored-route' ) ;
207248 const localStorageSpy = spyOn ( localStorage , 'removeItem' ) ;
0 commit comments