Skip to content

Commit 3014c17

Browse files
fixed tests
1 parent b9da9d6 commit 3014c17

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

projects/angular-auth-oidc-client/src/lib/auto-login/auto-login.guard.spec.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { TestBed, waitForAsync } from '@angular/core/testing';
22
import { Router, RouterStateSnapshot, UrlSegment } from '@angular/router';
33
import { RouterTestingModule } from '@angular/router/testing';
44
import { of } from 'rxjs';
5+
import { AuthStateService } from '../authState/auth-state.service';
6+
import { AuthStateServiceMock } from '../authState/auth-state.service-mock';
57
import { CheckAuthService } from '../check-auth.service';
68
import { CheckAuthServiceMock } from '../check-auth.service-mock';
79
import { 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');

projects/angular-auth-oidc-client/src/lib/auto-login/auto-login.guard.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Injectable } from '@angular/core';
22
import { ActivatedRouteSnapshot, CanActivate, CanLoad, Route, Router, RouterStateSnapshot, UrlSegment } from '@angular/router';
3-
import { Observable } from 'rxjs';
3+
import { Observable, of } from 'rxjs';
44
import { map } from 'rxjs/operators';
5+
import { AuthStateService } from '../authState/auth-state.service';
56
import { AutoLoginService } from '../auto-login/auto-login-service';
67
import { CheckAuthService } from '../check-auth.service';
78
import { LoginService } from '../login/login.service';
@@ -10,6 +11,7 @@ import { LoginService } from '../login/login.service';
1011
export class AutoLoginGuard implements CanActivate, CanLoad {
1112
constructor(
1213
private autoLoginService: AutoLoginService,
14+
private authStateService: AuthStateService,
1315
private checkAuthService: CheckAuthService,
1416
private loginService: LoginService,
1517
private router: Router
@@ -26,6 +28,12 @@ export class AutoLoginGuard implements CanActivate, CanLoad {
2628
}
2729

2830
private checkAuth(url: string) {
31+
const isAuthenticated = this.authStateService.areAuthStorageTokensValid();
32+
33+
if (isAuthenticated) {
34+
return of(true);
35+
}
36+
2937
return this.checkAuthService.checkAuth().pipe(
3038
map((isAuthorized) => {
3139
const storedRoute = this.autoLoginService.getStoredRedirectRoute();

0 commit comments

Comments
 (0)