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
Next Next commit
TT-15 fix: unit test
  • Loading branch information
Guido Quezada committed Dec 4, 2020
commit 79ef1bfec7385367dd730739f81612eeba68cd87
7 changes: 6 additions & 1 deletion src/app/guards/login-guard/login.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 @@ -10,6 +11,7 @@ describe('LoginGuard', () => {

let loginGuard: LoginGuard;
let azureAdB2CService: AzureAdB2CService;
let cookieService: CookieService;
const azureAdB2CServiceStub = {
isLogin() {
return true;
Expand All @@ -24,16 +26,19 @@ 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', () => {
it('can activate the route when user is logged-in && the token cookie exists', () => {
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: 3 additions & 2 deletions src/app/guards/login-guard/login.guard.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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) { }
constructor(private azureAdB2CService: AzureAdB2CService, private router: Router, private cookieService: CookieService) { }

canActivate() {
if (this.azureAdB2CService.isLogin()) {
if (this.azureAdB2CService.isLogin() && this.cookieService.check('msal.idtoken')) {
this.azureAdB2CService.setCookies();
return true;
} else {
Expand Down
22 changes: 11 additions & 11 deletions src/app/modules/login/services/azure.ad.b2c.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,44 +91,44 @@ describe('AzureAdB2CService', () => {
expect(isLogin).toEqual(false);
});

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

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

expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
expect(isLogin).toEqual(true);
expect(sessionStorage.setItem).toHaveBeenCalledWith('tenant_id', '12345');
expect(localStorage.setItem).toHaveBeenCalledWith('tenant_id', '12345');
});

it('setTenantId should not save tenantId if login is false ', () => {
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(null);
spyOn(sessionStorage, 'setItem');
spyOn(localStorage, 'setItem');
const isLogin = service.isLogin();
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
expect(isLogin).toEqual(false);
expect(sessionStorage.setItem).not.toHaveBeenCalled();
expect(localStorage.setItem).not.toHaveBeenCalled();
});

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

const resp = service.getTenantId();

expect(sessionStorage.getItem).toHaveBeenCalled();
expect(localStorage.getItem).toHaveBeenCalled();
expect(resp).toEqual(tenantId);
});

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

const resp = service.getBearerToken();

expect(sessionStorage.getItem).toHaveBeenCalled();
expect(localStorage.getItem).toHaveBeenCalled();
expect(resp).toEqual(token);
});

Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/login/services/azure.ad.b2c.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AUTHORITY, CLIENT_ID, SCOPES } from '../../../../environments/environme
})
export class AzureAdB2CService {

constructor(private cookieService: CookieService) {}
constructor(private cookieService?: CookieService) {}

msalConfig: any = {
auth: {
Expand Down Expand Up @@ -48,7 +48,7 @@ export class AzureAdB2CService {
}

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

setCookies() {
Expand Down