Skip to content

Commit 8d7d975

Browse files
author
Guido Quezada
committed
TT-15 fix: add test for login when cookie does not exist
1 parent 8fd812b commit 8d7d975

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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';
54

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

1211
let loginGuard: LoginGuard;
1312
let azureAdB2CService: AzureAdB2CService;
14-
let cookieService: CookieService;
1513
const azureAdB2CServiceStub = {
1614
isLogin() {
1715
return true;
@@ -26,19 +24,16 @@ describe('LoginGuard', () => {
2624
});
2725
loginGuard = TestBed.inject(LoginGuard);
2826
azureAdB2CService = TestBed.inject(AzureAdB2CService);
29-
cookieService = TestBed.inject(CookieService);
3027
});
3128

3229
it('should be created', () => {
3330
expect(loginGuard).toBeTruthy();
3431
});
3532

36-
it('can activate the route when user is logged-in && the token cookie exists', () => {
33+
it('can activate the route when user is logged-in', () => {
3734
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
38-
spyOn(cookieService, 'check').and.returnValue(true);
3935
const canActivate = loginGuard.canActivate();
4036
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
41-
expect(cookieService.check).toHaveBeenCalled();
4237
expect(canActivate).toEqual(true);
4338
});
4439

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
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';
54

65
@Injectable({
76
providedIn: 'root'
87
})
98
export class LoginGuard implements CanActivate {
109

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

1312
canActivate() {
14-
if (this.azureAdB2CService.isLogin() && this.cookieService.check('msal.idtoken')) {
13+
if (this.azureAdB2CService.isLogin()) {
1514
this.azureAdB2CService.setCookies();
1615
return true;
1716
} else {

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { inject, TestBed } from '@angular/core/testing';
22
import { Account, UserAgentApplication } from 'msal';
33
import { AzureAdB2CService } from './azure.ad.b2c.service';
4-
4+
import { CookieService } from 'ngx-cookie-service';
55

66
describe('AzureAdB2CService', () => {
77
let service: AzureAdB2CService;
8-
8+
let cookieService: CookieService;
99
let account: Account;
1010

1111
beforeEach(() => {
1212
TestBed.configureTestingModule({
1313
imports: [],
1414
});
1515
service = TestBed.inject(AzureAdB2CService);
16+
cookieService = TestBed.inject(CookieService);
1617
account = {
1718
accountIdentifier: 'abc',
1819
homeAccountIdentifier: 'abc',
@@ -75,30 +76,47 @@ describe('AzureAdB2CService', () => {
7576
expect(isAdmin).toBeTruthy();
7677
});
7778

78-
it('isLogin returns true if UserAgentApplication has a defined Account', () => {
79+
it('isLogin returns true if UserAgentApplication has a defined Account and token cookie exist', () => {
7980
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
81+
spyOn(cookieService, 'check').and.returnValue(true);
8082

8183
const isLogin = service.isLogin();
8284

8385
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
86+
expect(cookieService.check).toHaveBeenCalled();
8487
expect(isLogin).toEqual(true);
8588
});
8689

90+
it('isLogin returns false if UserAgentApplication has a defined Account and token cookie does not exist', () => {
91+
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
92+
spyOn(cookieService, 'check').and.returnValue(false);
93+
94+
const isLogin = service.isLogin();
95+
96+
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
97+
expect(cookieService.check).toHaveBeenCalled();
98+
expect(isLogin).toEqual(false);
99+
});
100+
87101
it('isLogin returns false if UserAgentApplication has a null value for Account', () => {
88102
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(null);
103+
89104
const isLogin = service.isLogin();
105+
90106
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
91107
expect(isLogin).toEqual(false);
92108
});
93109

94110
it('setTenantId should save a tenantId in local storage', () => {
95111
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
112+
spyOn(cookieService, 'check').and.returnValue(true);
96113
spyOn(localStorage, 'setItem').withArgs('tenant_id', '12345');
97114

98115
const isLogin = service.isLogin();
99116
service.setTenantId();
100117

101118
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
119+
expect(cookieService.check).toHaveBeenCalled();
102120
expect(isLogin).toEqual(true);
103121
expect(localStorage.setItem).toHaveBeenCalledWith('tenant_id', '12345');
104122
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class AzureAdB2CService {
3535

3636
logout() {
3737
this.cookieService.delete('msal.idtoken');
38-
this.cookieService.delete('msal.idtoken');
38+
this.cookieService.delete('msal.client.info');
3939
this.msal.logout();
4040
}
4141

@@ -48,12 +48,12 @@ export class AzureAdB2CService {
4848
}
4949

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

5454
setCookies() {
55-
this.cookieService.set('msal.client.info', this.getBearerClientInfo(), 2);
5655
this.cookieService.set('msal.idtoken', this.getBearerToken(), 2);
56+
this.cookieService.set('msal.client.info', this.getBearerClientInfo(), 2);
5757
}
5858

5959
setTenantId() {

0 commit comments

Comments
 (0)