Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
74 changes: 54 additions & 20 deletions src/app/modules/login/services/azure.ad.b2c.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
});
});
25 changes: 19 additions & 6 deletions src/app/modules/login/services/azure.ad.b2c.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
}
}
18 changes: 9 additions & 9 deletions src/app/modules/shared/components/user/user.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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', () => {
Expand All @@ -61,5 +62,4 @@ describe('UserComponent', () => {

expect(azureAdB2CService.logout).toHaveBeenCalled();
});

});
7 changes: 3 additions & 4 deletions src/app/modules/shared/components/user/user.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}