Skip to content

Commit 79ef1bf

Browse files
author
Guido Quezada
committed
TT-15 fix: unit test
1 parent 6e8d180 commit 79ef1bf

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

src/app/guards/login-guard/login.guard.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { TestBed, inject } from '@angular/core/testing';
22
import { RouterTestingModule } from '@angular/router/testing';
33
import { Router } from '@angular/router';
4+
import { CookieService } from 'ngx-cookie-service';
45

56
import { AzureAdB2CService } from '../../modules/login/services/azure.ad.b2c.service';
67
import { LoginGuard } from './login.guard';
@@ -10,6 +11,7 @@ describe('LoginGuard', () => {
1011

1112
let loginGuard: LoginGuard;
1213
let azureAdB2CService: AzureAdB2CService;
14+
let cookieService: CookieService;
1315
const azureAdB2CServiceStub = {
1416
isLogin() {
1517
return true;
@@ -24,16 +26,19 @@ describe('LoginGuard', () => {
2426
});
2527
loginGuard = TestBed.inject(LoginGuard);
2628
azureAdB2CService = TestBed.inject(AzureAdB2CService);
29+
cookieService = TestBed.inject(CookieService);
2730
});
2831

2932
it('should be created', () => {
3033
expect(loginGuard).toBeTruthy();
3134
});
3235

33-
it('can activate the route when user is logged-in', () => {
36+
it('can activate the route when user is logged-in && the token cookie exists', () => {
3437
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
38+
spyOn(cookieService, 'check').and.returnValue(true);
3539
const canActivate = loginGuard.canActivate();
3640
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
41+
expect(cookieService.check).toHaveBeenCalled();
3742
expect(canActivate).toEqual(true);
3843
});
3944

src/app/guards/login-guard/login.guard.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { Injectable } from '@angular/core';
22
import { Router, CanActivate } from '@angular/router';
33
import { AzureAdB2CService } from '../../modules/login/services/azure.ad.b2c.service';
4+
import { CookieService } from 'ngx-cookie-service';
45

56
@Injectable({
67
providedIn: 'root'
78
})
89
export class LoginGuard implements CanActivate {
910

10-
constructor(private azureAdB2CService: AzureAdB2CService, private router: Router) { }
11+
constructor(private azureAdB2CService: AzureAdB2CService, private router: Router, private cookieService: CookieService) { }
1112

1213
canActivate() {
13-
if (this.azureAdB2CService.isLogin()) {
14+
if (this.azureAdB2CService.isLogin() && this.cookieService.check('msal.idtoken')) {
1415
this.azureAdB2CService.setCookies();
1516
return true;
1617
} else {

src/app/modules/login/services/azure.ad.b2c.service.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,44 +91,44 @@ describe('AzureAdB2CService', () => {
9191
expect(isLogin).toEqual(false);
9292
});
9393

94-
it('setTenantId should save a tenantId in session storage', () => {
94+
it('setTenantId should save a tenantId in local storage', () => {
9595
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
96-
spyOn(sessionStorage, 'setItem').withArgs('tenant_id', '12345');
96+
spyOn(localStorage, 'setItem').withArgs('tenant_id', '12345');
9797

9898
const isLogin = service.isLogin();
9999
service.setTenantId();
100100

101101
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
102102
expect(isLogin).toEqual(true);
103-
expect(sessionStorage.setItem).toHaveBeenCalledWith('tenant_id', '12345');
103+
expect(localStorage.setItem).toHaveBeenCalledWith('tenant_id', '12345');
104104
});
105105

106106
it('setTenantId should not save tenantId if login is false ', () => {
107107
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(null);
108-
spyOn(sessionStorage, 'setItem');
108+
spyOn(localStorage, 'setItem');
109109
const isLogin = service.isLogin();
110110
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
111111
expect(isLogin).toEqual(false);
112-
expect(sessionStorage.setItem).not.toHaveBeenCalled();
112+
expect(localStorage.setItem).not.toHaveBeenCalled();
113113
});
114114

115-
it('getTenantId should get the tenantId from session storage', () => {
115+
it('getTenantId should get the tenantId from local storage', () => {
116116
const tenantId = '12345';
117-
spyOn(sessionStorage, 'getItem').and.returnValue(tenantId);
117+
spyOn(localStorage, 'getItem').and.returnValue(tenantId);
118118

119119
const resp = service.getTenantId();
120120

121-
expect(sessionStorage.getItem).toHaveBeenCalled();
121+
expect(localStorage.getItem).toHaveBeenCalled();
122122
expect(resp).toEqual(tenantId);
123123
});
124124

125-
it('getBearerToken should get the bearer token from session storage', () => {
125+
it('getBearerToken should get the bearer token from local storage', () => {
126126
const token = '12345abc';
127-
spyOn(sessionStorage, 'getItem').and.returnValue(token);
127+
spyOn(localStorage, 'getItem').and.returnValue(token);
128128

129129
const resp = service.getBearerToken();
130130

131-
expect(sessionStorage.getItem).toHaveBeenCalled();
131+
expect(localStorage.getItem).toHaveBeenCalled();
132132
expect(resp).toEqual(token);
133133
});
134134

src/app/modules/login/services/azure.ad.b2c.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { AUTHORITY, CLIENT_ID, SCOPES } from '../../../../environments/environme
1010
})
1111
export class AzureAdB2CService {
1212

13-
constructor(private cookieService: CookieService) {}
13+
constructor(private cookieService?: CookieService) {}
1414

1515
msalConfig: any = {
1616
auth: {
@@ -48,7 +48,7 @@ export class AzureAdB2CService {
4848
}
4949

5050
isLogin() {
51-
return this.msal.getAccount() && this.cookieService.check('msal.idtoken') ? true : false;
51+
return this.msal.getAccount() ? true : false;
5252
}
5353

5454
setCookies() {

0 commit comments

Comments
 (0)