fix unit test

This commit is contained in:
Eugenio Romano
2019-04-02 00:24:31 +01:00
parent e6d3869891
commit ad15cdf028
4 changed files with 164 additions and 179 deletions

View File

@@ -68,7 +68,7 @@ describe('Auth Guard SSO role service', () => {
spyOn(routerService, 'navigate').and.stub();
const router: ActivatedRouteSnapshot = new ActivatedRouteSnapshot();
router.data = { 'roles': ['role1', 'role2']};
router.data = { 'roles': ['role1', 'role2'] };
expect(authGuard.canActivate(router, null)).toBeTruthy();
expect(routerService.navigate).not.toHaveBeenCalled();
@@ -85,7 +85,7 @@ describe('Auth Guard SSO role service', () => {
it('Should canActivate return false if the realm_access is not present', async(() => {
spyOn(storageService, 'getItem').and.returnValue('my-access_token');
spyOn(jwtHelperService, 'decodeToken').and.returnValue({ });
spyOn(jwtHelperService, 'decodeToken').and.returnValue({});
const router: ActivatedRouteSnapshot = new ActivatedRouteSnapshot();
@@ -94,11 +94,11 @@ describe('Auth Guard SSO role service', () => {
it('Should redirect to the redirectURL if canActivate is false and redirectUrl is in data', async(() => {
spyOn(storageService, 'getItem').and.returnValue('my-access_token');
spyOn(jwtHelperService, 'decodeToken').and.returnValue({ });
spyOn(jwtHelperService, 'decodeToken').and.returnValue({});
spyOn(routerService, 'navigate').and.stub();
const router: ActivatedRouteSnapshot = new ActivatedRouteSnapshot();
router.data = { 'roles': ['role1', 'role2'], 'redirectUrl': 'no-role-url'};
router.data = { 'roles': ['role1', 'role2'], 'redirectUrl': 'no-role-url' };
expect(authGuard.canActivate(router, null)).toBeFalsy();
expect(routerService.navigate).toHaveBeenCalledWith(['/no-role-url']);
@@ -106,32 +106,34 @@ describe('Auth Guard SSO role service', () => {
it('Should not redirect if canActivate is false and redirectUrl is not in data', async(() => {
spyOn(storageService, 'getItem').and.returnValue('my-access_token');
spyOn(jwtHelperService, 'decodeToken').and.returnValue({ });
spyOn(jwtHelperService, 'decodeToken').and.returnValue({});
spyOn(routerService, 'navigate').and.stub();
const router: ActivatedRouteSnapshot = new ActivatedRouteSnapshot();
router.data = { 'roles': ['role1', 'role2']};
router.data = { 'roles': ['role1', 'role2'] };
expect(authGuard.canActivate(router, null)).toBeFalsy();
expect(routerService.navigate).not.toHaveBeenCalled();
}));
it('Should canActivate be false hasRealm is true and hasClientRol is false', () => {
it('Should canActivate be false hasRealm is true and hasClientRole is false', () => {
const route: ActivatedRouteSnapshot = new ActivatedRouteSnapshot();
spyOn(this, 'hasRealmRoles').and.returnValue(true);
spyOn(this, 'hasRealmRolesForClientRole').and.returnValue(false);
spyOn(authGuard, 'hasRealmRoles').and.returnValue(true);
spyOn(authGuard, 'hasRealmRolesForClientRole').and.returnValue(false);
route.params = { appName: 'fakeapp' };
route.data = { 'clientRoles': ['appName'], 'roles': ['role1', 'role2'] };
expect(authGuard.canActivate(route, null)).toBeFalsy();
});
it('Should canActivate be false hasRealm is false and hasClientRol is true', () => {
it('Should canActivate be false if hasRealm is false and hasClientRole is true', () => {
const route: ActivatedRouteSnapshot = new ActivatedRouteSnapshot();
spyOn(this, 'hasRealmRoles').and.returnValue(false);
spyOn(this, 'hasRealmRolesForClientRole').and.returnValue(true);
spyOn(authGuard, 'hasRealmRoles').and.returnValue(false);
spyOn(authGuard, 'hasRealmRolesForClientRole').and.returnValue(true);
route.data = { 'clientRoles': ['appName'], 'roles': ['role1', 'role2'] };
route.params = { appName: 'fakeapp' };
route.data = { 'clientRoles': ['fakeapp'], 'roles': ['role1', 'role2'] };
expect(authGuard.canActivate(route, null)).toBeFalsy();
});
@@ -142,10 +144,10 @@ describe('Auth Guard SSO role service', () => {
spyOn(jwtHelperService, 'decodeToken').and.returnValue({
'realm_access': { roles: ['role1'] },
'resource_access': { fakeapp: { roles: ['role2'] }}
'resource_access': { fakeapp: { roles: ['role2'] } }
});
route.params = {appName: 'fakeapp'};
route.params = { appName: 'fakeapp' };
route.data = { 'clientRoles': ['appName'], 'roles': ['role1', 'role2'] };
expect(authGuard.canActivate(route, null)).toBeTruthy();
@@ -157,10 +159,10 @@ describe('Auth Guard SSO role service', () => {
spyOn(jwtHelperService, 'decodeToken').and.returnValue({
'realm_access': { roles: ['role1'] },
'resource_access': { fakeapp: { roles: ['role3'] }}
'resource_access': { fakeapp: { roles: ['role3'] } }
});
route.params = {appName: 'fakeapp'};
route.params = { appName: 'fakeapp' };
route.data = { 'clientRoles': ['appName'], 'roles': ['role1', 'role2'] };
expect(authGuard.canActivate(route, null)).toBeFalsy();
@@ -172,10 +174,11 @@ describe('Auth Guard SSO role service', () => {
spyOn(storageService, 'getItem').and.returnValue('my-access_token');
spyOn(jwtHelperService, 'decodeToken').and.returnValue(
{'resource_access': { fakeapp: { roles: ['role1'] } }
});
{
'resource_access': { fakeapp: { roles: ['role1'] } }
});
const result = authGuard.hasRealmRolesForClientRole('fakeapp', ['role1'] );
const result = authGuard.hasRealmRolesForClientRole('fakeapp', ['role1']);
expect(result).toBeTruthy();
});
@@ -183,18 +186,20 @@ describe('Auth Guard SSO role service', () => {
spyOn(storageService, 'getItem').and.returnValue('my-access_token');
spyOn(jwtHelperService, 'decodeToken').and.returnValue(
{'resource_access': { fakeapp: { roles: ['role1'] } }
});
{
'resource_access': { fakeapp: { roles: ['role1'] } }
});
const result = authGuard.hasRealmRolesForClientRole('fakeapp', ['role1', 'role2'] );
const result = authGuard.hasRealmRolesForClientRole('fakeapp', ['role1', 'role2']);
expect(result).toBeTruthy();
});
it('Should be false if the resource_access does not contain the role', () => {
spyOn(storageService, 'getItem').and.returnValue('my-access_token');
spyOn(jwtHelperService, 'decodeToken').and.returnValue(
{'resource_access': { fakeapp: { roles: ['role3'] } }
});
{
'resource_access': { fakeapp: { roles: ['role3'] } }
});
const result = authGuard.hasRealmRolesForClientRole('fakeapp', ['role1', 'role2']);
expect(result).toBeFalsy();
});
@@ -202,8 +207,9 @@ describe('Auth Guard SSO role service', () => {
it('Should be false if the resource_access does not contain the client role related to the app', () => {
spyOn(storageService, 'getItem').and.returnValue('my-access_token');
spyOn(jwtHelperService, 'decodeToken').and.returnValue(
{'resource_access': { anotherfakeapp: { roles: ['role1'] } }
});
{
'resource_access': { anotherfakeapp: { roles: ['role1'] } }
});
const result = authGuard.hasRealmRolesForClientRole('fakeapp', ['role1', 'role2']);
expect(result).toBeFalsy();
});