mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-4994] Move realmRole functions inside JwtHelperService. (#5254)
* Moved Realm and client role function inside jwtHelperService. * Updated unit tests.
This commit is contained in:
committed by
Eugenio Romano
parent
81dcfa4341
commit
ac4679fc10
@@ -44,4 +44,101 @@ describe('JwtHelperService', () => {
|
||||
expect(result['name']).toBe('John Doe');
|
||||
expect(result['email']).toBe('johnDoe@gmail.com');
|
||||
});
|
||||
|
||||
describe('RealmRole ', () => {
|
||||
|
||||
it('Should be true if the realm_access contains the single role', () => {
|
||||
spyOn(jwtHelperService, 'getAccessToken').and.returnValue('my-access_token');
|
||||
|
||||
spyOn(jwtHelperService, 'decodeToken').and.returnValue(
|
||||
{
|
||||
'realm_access': { roles: ['role1'] }
|
||||
});
|
||||
|
||||
const result = jwtHelperService.hasRealmRole('role1');
|
||||
expect(result).toBeTruthy();
|
||||
});
|
||||
|
||||
it('Should be true if the realm_access contains at least one of the roles', () => {
|
||||
spyOn(jwtHelperService, 'getAccessToken').and.returnValue('my-access_token');
|
||||
|
||||
spyOn(jwtHelperService, 'decodeToken').and.returnValue(
|
||||
{
|
||||
'realm_access': { roles: ['role1'] }
|
||||
});
|
||||
|
||||
const result = jwtHelperService.hasRealmRoles(['role1', 'role2']);
|
||||
expect(result).toBeTruthy();
|
||||
});
|
||||
|
||||
it('Should be false if the realm_access does not contain the role', () => {
|
||||
spyOn(jwtHelperService, 'getAccessToken').and.returnValue('my-access_token');
|
||||
spyOn(jwtHelperService, 'decodeToken').and.returnValue(
|
||||
{
|
||||
'realm_access': { roles: ['role3'] }
|
||||
});
|
||||
const result = jwtHelperService.hasRealmRole('role1');
|
||||
expect(result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('Should be false if the realm_access does not contain at least one of the roles', () => {
|
||||
spyOn(jwtHelperService, 'getAccessToken').and.returnValue('my-access_token');
|
||||
spyOn(jwtHelperService, 'decodeToken').and.returnValue(
|
||||
{
|
||||
'realm_access': { roles: ['role1'] }
|
||||
});
|
||||
const result = jwtHelperService.hasRealmRoles(['role3', 'role2']);
|
||||
expect(result).toBeFalsy();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('ClientRole ', () => {
|
||||
|
||||
it('Should be true if the resource_access contains the single role', () => {
|
||||
spyOn(jwtHelperService, 'getAccessToken').and.returnValue('my-access_token');
|
||||
|
||||
spyOn(jwtHelperService, 'decodeToken').and.returnValue(
|
||||
{
|
||||
'resource_access': { fakeapp: { roles: ['role1'] } }
|
||||
});
|
||||
|
||||
const result = jwtHelperService.hasRealmRolesForClientRole('fakeapp', ['role1']);
|
||||
expect(result).toBeTruthy();
|
||||
});
|
||||
|
||||
it('Should be true if the resource_access contains at least one of the roles', () => {
|
||||
spyOn(jwtHelperService, 'getAccessToken').and.returnValue('my-access_token');
|
||||
|
||||
spyOn(jwtHelperService, 'decodeToken').and.returnValue(
|
||||
{
|
||||
'resource_access': { fakeapp: { roles: ['role1'] } }
|
||||
});
|
||||
|
||||
const result = jwtHelperService.hasRealmRolesForClientRole('fakeapp', ['role1', 'role2']);
|
||||
expect(result).toBeTruthy();
|
||||
});
|
||||
|
||||
it('Should be false if the resource_access does not contain the role', () => {
|
||||
spyOn(jwtHelperService, 'getAccessToken').and.returnValue('my-access_token');
|
||||
spyOn(jwtHelperService, 'decodeToken').and.returnValue(
|
||||
{
|
||||
'resource_access': { fakeapp: { roles: ['role3'] } }
|
||||
});
|
||||
const result = jwtHelperService.hasRealmRolesForClientRole('fakeapp', ['role1', 'role2']);
|
||||
expect(result).toBeFalsy();
|
||||
});
|
||||
|
||||
it('Should be false if the resource_access does not contain the client role related to the app', () => {
|
||||
spyOn(jwtHelperService, 'getAccessToken').and.returnValue('my-access_token');
|
||||
spyOn(jwtHelperService, 'decodeToken').and.returnValue(
|
||||
{
|
||||
'resource_access': { anotherfakeapp: { roles: ['role1'] } }
|
||||
});
|
||||
const result = jwtHelperService.hasRealmRolesForClientRole('fakeapp', ['role1', 'role2']);
|
||||
expect(result).toBeFalsy();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
Reference in New Issue
Block a user