Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
TT-15 fix: add test for login when cookie does not exist
  • Loading branch information
Guido Quezada committed Dec 9, 2020
commit 8d7d9757436ae90a213f6278b6a3b5596b755e4b
7 changes: 1 addition & 6 deletions src/app/guards/login-guard/login.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TestBed, inject } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { Router } from '@angular/router';
import { CookieService } from 'ngx-cookie-service';

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

let loginGuard: LoginGuard;
let azureAdB2CService: AzureAdB2CService;
let cookieService: CookieService;
const azureAdB2CServiceStub = {
isLogin() {
return true;
Expand All @@ -26,19 +24,16 @@ describe('LoginGuard', () => {
});
loginGuard = TestBed.inject(LoginGuard);
azureAdB2CService = TestBed.inject(AzureAdB2CService);
cookieService = TestBed.inject(CookieService);
});

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

it('can activate the route when user is logged-in && the token cookie exists', () => {
it('can activate the route when user is logged-in', () => {
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
spyOn(cookieService, 'check').and.returnValue(true);
const canActivate = loginGuard.canActivate();
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
expect(cookieService.check).toHaveBeenCalled();
expect(canActivate).toEqual(true);
});

Expand Down
5 changes: 2 additions & 3 deletions src/app/guards/login-guard/login.guard.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { AzureAdB2CService } from '../../modules/login/services/azure.ad.b2c.service';
import { CookieService } from 'ngx-cookie-service';

@Injectable({
providedIn: 'root'
})
export class LoginGuard implements CanActivate {

constructor(private azureAdB2CService: AzureAdB2CService, private router: Router, private cookieService: CookieService) { }
constructor(private azureAdB2CService: AzureAdB2CService, private router: Router) { }

canActivate() {
if (this.azureAdB2CService.isLogin() && this.cookieService.check('msal.idtoken')) {
if (this.azureAdB2CService.isLogin()) {
this.azureAdB2CService.setCookies();
return true;
} else {
Expand Down
24 changes: 21 additions & 3 deletions src/app/modules/login/services/azure.ad.b2c.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { inject, TestBed } from '@angular/core/testing';
import { Account, UserAgentApplication } from 'msal';
import { AzureAdB2CService } from './azure.ad.b2c.service';

import { CookieService } from 'ngx-cookie-service';

describe('AzureAdB2CService', () => {
let service: AzureAdB2CService;

let cookieService: CookieService;
let account: Account;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [],
});
service = TestBed.inject(AzureAdB2CService);
cookieService = TestBed.inject(CookieService);
account = {
accountIdentifier: 'abc',
homeAccountIdentifier: 'abc',
Expand Down Expand Up @@ -75,30 +76,47 @@ describe('AzureAdB2CService', () => {
expect(isAdmin).toBeTruthy();
});

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

const isLogin = service.isLogin();

expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
expect(cookieService.check).toHaveBeenCalled();
expect(isLogin).toEqual(true);
});

it('isLogin returns false if UserAgentApplication has a defined Account and token cookie does not exist', () => {
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
spyOn(cookieService, 'check').and.returnValue(false);

const isLogin = service.isLogin();

expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
expect(cookieService.check).toHaveBeenCalled();
expect(isLogin).toEqual(false);
});

it('isLogin returns false if UserAgentApplication has a null value for Account', () => {
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(null);

const isLogin = service.isLogin();

expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
expect(isLogin).toEqual(false);
});

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

const isLogin = service.isLogin();
service.setTenantId();

expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
expect(cookieService.check).toHaveBeenCalled();
expect(isLogin).toEqual(true);
expect(localStorage.setItem).toHaveBeenCalledWith('tenant_id', '12345');
});
Expand Down
6 changes: 3 additions & 3 deletions src/app/modules/login/services/azure.ad.b2c.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class AzureAdB2CService {

logout() {
this.cookieService.delete('msal.idtoken');
this.cookieService.delete('msal.idtoken');
this.cookieService.delete('msal.client.info');
this.msal.logout();
}

Expand All @@ -48,12 +48,12 @@ export class AzureAdB2CService {
}

isLogin() {
return this.msal.getAccount() ? true : false;
return this.msal.getAccount() && this.cookieService.check('msal.idtoken') ? true : false;
}

setCookies() {
this.cookieService.set('msal.client.info', this.getBearerClientInfo(), 2);
this.cookieService.set('msal.idtoken', this.getBearerToken(), 2);
this.cookieService.set('msal.client.info', this.getBearerClientInfo(), 2);
}

setTenantId() {
Expand Down