|
|
|
@ -1,22 +1,57 @@ |
|
|
|
import { APP_BASE_HREF } from '@angular/common'; |
|
|
|
import { Component } from '@angular/core'; |
|
|
|
import { RouterModule } from '@angular/router'; |
|
|
|
import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest'; |
|
|
|
import { Store } from '@ngxs/store'; |
|
|
|
import { Actions, Store } from '@ngxs/store'; |
|
|
|
import { of } from 'rxjs'; |
|
|
|
import { PermissionGuard } from '../guards/permission.guard'; |
|
|
|
import { RestOccurError } from '../actions'; |
|
|
|
import { PermissionGuard } from '../guards/permission.guard'; |
|
|
|
import { RoutesService } from '../services/routes.service'; |
|
|
|
|
|
|
|
describe('PermissionGuard', () => { |
|
|
|
let spectator: SpectatorService<PermissionGuard>; |
|
|
|
let guard: PermissionGuard; |
|
|
|
let routes: SpyObject<RoutesService>; |
|
|
|
let store: SpyObject<Store>; |
|
|
|
|
|
|
|
@Component({ template: '' }) |
|
|
|
class DummyComponent {} |
|
|
|
|
|
|
|
const createService = createServiceFactory({ |
|
|
|
service: PermissionGuard, |
|
|
|
mocks: [Store], |
|
|
|
declarations: [DummyComponent], |
|
|
|
imports: [ |
|
|
|
RouterModule.forRoot([ |
|
|
|
{ |
|
|
|
path: 'test', |
|
|
|
component: DummyComponent, |
|
|
|
data: { |
|
|
|
requiredPolicy: 'TestPolicy', |
|
|
|
}, |
|
|
|
}, |
|
|
|
]), |
|
|
|
], |
|
|
|
providers: [ |
|
|
|
{ |
|
|
|
provide: APP_BASE_HREF, |
|
|
|
useValue: '/', |
|
|
|
}, |
|
|
|
{ |
|
|
|
provide: Actions, |
|
|
|
useValue: { |
|
|
|
pipe() { |
|
|
|
return of(null); |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
], |
|
|
|
}); |
|
|
|
|
|
|
|
beforeEach(() => { |
|
|
|
spectator = createService(); |
|
|
|
guard = spectator.service; |
|
|
|
routes = spectator.inject(RoutesService); |
|
|
|
store = spectator.get(Store); |
|
|
|
}); |
|
|
|
|
|
|
|
@ -41,17 +76,32 @@ describe('PermissionGuard', () => { |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
it('should find the requiredPolicy from child route', done => { |
|
|
|
it('should check the requiredPolicy from RoutesService', done => { |
|
|
|
routes.add([ |
|
|
|
{ |
|
|
|
path: '/test', |
|
|
|
name: 'Test', |
|
|
|
requiredPolicy: 'TestPolicy', |
|
|
|
}, |
|
|
|
]); |
|
|
|
store.select.andReturn(of(false)); |
|
|
|
const spy = jest.spyOn(store, 'select'); |
|
|
|
guard |
|
|
|
.canActivate( |
|
|
|
{ data: {}, routeConfig: { children: [{ path: 'test', data: { requiredPolicy: 'TestPolicy' } }] } } as any, |
|
|
|
{ url: 'test' } as any, |
|
|
|
) |
|
|
|
.subscribe(() => { |
|
|
|
expect(spy.mock.calls[0][0]({ auth: { grantedPolicies: { TestPolicy: true } } })).toBe(true); |
|
|
|
done(); |
|
|
|
}); |
|
|
|
guard.canActivate({ data: {} } as any, { url: 'test' } as any).subscribe(() => { |
|
|
|
expect(spy.mock.calls[0][0]({ auth: { grantedPolicies: { TestPolicy: true } } })).toBe(true); |
|
|
|
done(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
it('should return Observable<true> if RoutesService does not have requiredPolicy for given URL', done => { |
|
|
|
routes.add([ |
|
|
|
{ |
|
|
|
path: '/test', |
|
|
|
name: 'Test', |
|
|
|
}, |
|
|
|
]); |
|
|
|
guard.canActivate({ data: {} } as any, { url: 'test' } as any).subscribe(result => { |
|
|
|
expect(result).toBe(true); |
|
|
|
done(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|