diff --git a/src/app/modules/login/services/azure.ad.b2c.service.spec.ts b/src/app/modules/login/services/azure.ad.b2c.service.spec.ts index ed091da69..935d8dab6 100644 --- a/src/app/modules/login/services/azure.ad.b2c.service.spec.ts +++ b/src/app/modules/login/services/azure.ad.b2c.service.spec.ts @@ -5,6 +5,19 @@ import { UserAgentApplication, Account } from 'msal'; describe('AzureAdB2CService', () => { let service: AzureAdB2CService; + const account: Account = { + accountIdentifier: 'abc', + homeAccountIdentifier: 'abc', + userName: 'abc', + name: 'abc', + idToken: { + iss: ' http://hostname.com/12345/v0/', + }, + idTokenClaims: {}, + sid: 'abc', + environment: 'abc', + }; + beforeEach(() => { TestBed.configureTestingModule({ imports: [], @@ -33,16 +46,6 @@ describe('AzureAdB2CService', () => { }); it('should get Account name from UserAgentApplication', () => { - const account: Account = { - accountIdentifier: 'abc', - homeAccountIdentifier: 'abc', - userName: 'abc', - name: 'abc', - idToken: {}, - idTokenClaims: {}, - sid: 'abc', - environment: 'abc', - }; spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValues(account); const name = service.getName(); @@ -52,16 +55,6 @@ describe('AzureAdB2CService', () => { }); it('isLogin returns true if UserAgentApplication has a defined Account', () => { - const account: Account = { - accountIdentifier: 'abc', - homeAccountIdentifier: 'abc', - userName: 'abc', - name: 'abc', - idToken: {}, - idTokenClaims: {}, - sid: 'abc', - environment: 'abc', - }; spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account); const isLogin = service.isLogin(); @@ -76,4 +69,45 @@ describe('AzureAdB2CService', () => { expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled(); expect(isLogin).toEqual(false); }); + + it('setTenantId should save a tenantId in session storage', () => { + spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account); + spyOn(sessionStorage, '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'); + }); + + it('setTenantId should not save tenantId if login is false ', () => { + spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(null); + spyOn(sessionStorage, 'setItem'); + const isLogin = service.isLogin(); + expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled(); + expect(isLogin).toEqual(false); + expect(sessionStorage.setItem).not.toHaveBeenCalled(); + }); + + it('getTenantId should get the tenantId from session storage', () => { + const tenantId = '12345'; + spyOn(sessionStorage, 'getItem').and.returnValue(tenantId); + + const resp = service.getTenantId(); + + expect(sessionStorage.getItem).toHaveBeenCalled(); + expect(resp).toEqual(tenantId); + }); + + it('getBearerToken should get the bearer token from session storage', () => { + const token = '12345abc'; + spyOn(sessionStorage, 'getItem').and.returnValue(token); + + const resp = service.getBearerToken(); + + expect(sessionStorage.getItem).toHaveBeenCalled(); + expect(resp).toEqual(token); + }); }); diff --git a/src/app/modules/login/services/azure.ad.b2c.service.ts b/src/app/modules/login/services/azure.ad.b2c.service.ts index da0c4a9d7..3f2966682 100644 --- a/src/app/modules/login/services/azure.ad.b2c.service.ts +++ b/src/app/modules/login/services/azure.ad.b2c.service.ts @@ -4,21 +4,19 @@ import { CLIENT_ID, AUTHORITY, SCOPES } from '../../../../environments/environme import { UserAgentApplication } from 'msal'; @Injectable({ - providedIn: 'root' + providedIn: 'root', }) - export class AzureAdB2CService { - msalConfig = { auth: { clientId: CLIENT_ID, authority: AUTHORITY, - validateAuthority: false - } + validateAuthority: false, + }, }; tokenRequest = { - scopes: SCOPES + scopes: SCOPES, }; msal = new UserAgentApplication(this.msalConfig); @@ -38,4 +36,19 @@ export class AzureAdB2CService { isLogin() { return this.msal.getAccount() ? true : false; } + + setTenantId() { + if (this.msal.getAccount() && this.msal.getAccount().idToken) { + const pathArray = this.msal.getAccount().idToken.iss.split('/'); + const tenantId = pathArray[3]; + sessionStorage.setItem('tenant_id', tenantId); + } + } + + getTenantId(): string { + return sessionStorage.getItem('tenant_id'); + } + getBearerToken(): string { + return sessionStorage.getItem('msal.idtoken'); + } } diff --git a/src/app/modules/shared/components/user/user.component.spec.ts b/src/app/modules/shared/components/user/user.component.spec.ts index 6be7913c2..6c19c080a 100644 --- a/src/app/modules/shared/components/user/user.component.spec.ts +++ b/src/app/modules/shared/components/user/user.component.spec.ts @@ -14,17 +14,14 @@ describe('UserComponent', () => { }, signIn() { return of(); - } + }, }; beforeEach(async(() => { TestBed.configureTestingModule({ - declarations: [ UserComponent ], - providers: [ - { providers: AzureAdB2CService, useValue: azureAdB2CServiceStub} - ] - }) - .compileComponents(); + declarations: [UserComponent], + providers: [{ providers: AzureAdB2CService, useValue: azureAdB2CServiceStub }], + }).compileComponents(); })); beforeEach(() => { @@ -38,20 +35,24 @@ describe('UserComponent', () => { expect(component).toBeTruthy(); }); - it('onInit checks if isLogin and gets the name', () => { + it('onInit checks if isLogin and gets the name and set tenantIn in the storage', () => { spyOn(azureAdB2CService, 'isLogin').and.returnValue(true); spyOn(azureAdB2CService, 'getName').and.returnValue('Name'); + spyOn(azureAdB2CService, 'setTenantId'); component.ngOnInit(); expect(azureAdB2CService.isLogin).toHaveBeenCalled(); expect(azureAdB2CService.getName).toHaveBeenCalled(); + expect(azureAdB2CService.setTenantId).toHaveBeenCalled(); }); it('onInit does not get the name if isLogin false', () => { spyOn(azureAdB2CService, 'isLogin').and.returnValue(false); spyOn(azureAdB2CService, 'getName').and.returnValue('Name'); + spyOn(azureAdB2CService, 'setTenantId'); component.ngOnInit(); expect(azureAdB2CService.isLogin).toHaveBeenCalled(); expect(azureAdB2CService.getName).toHaveBeenCalledTimes(0); + expect(azureAdB2CService.setTenantId).not.toHaveBeenCalled(); }); it('uses the Azure service on logout', () => { @@ -61,5 +62,4 @@ describe('UserComponent', () => { expect(azureAdB2CService.logout).toHaveBeenCalled(); }); - }); diff --git a/src/app/modules/shared/components/user/user.component.ts b/src/app/modules/shared/components/user/user.component.ts index 8848e9c6e..5f99f7399 100644 --- a/src/app/modules/shared/components/user/user.component.ts +++ b/src/app/modules/shared/components/user/user.component.ts @@ -4,22 +4,21 @@ import { AzureAdB2CService } from '../../../login/services/azure.ad.b2c.service' @Component({ selector: 'app-user', templateUrl: './user.component.html', - styleUrls: ['./user.component.scss'] + styleUrls: ['./user.component.scss'], }) export class UserComponent implements OnInit { - name: string; - constructor(private azureAdB2CService: AzureAdB2CService) { } + constructor(private azureAdB2CService: AzureAdB2CService) {} ngOnInit(): void { if (this.azureAdB2CService.isLogin()) { this.name = this.azureAdB2CService.getName(); + this.azureAdB2CService.setTenantId(); } } logout() { this.azureAdB2CService.logout(); } - }