pass actual redirect when no state (#3285)

This commit is contained in:
Cilibiu Bogdan
2018-05-09 09:59:03 +03:00
committed by Denys Vuika
parent 4fbacf4860
commit f63614e964
6 changed files with 63 additions and 0 deletions

View File

@@ -91,6 +91,19 @@ describe('AuthGuardService BPM', () => {
expect(authService.getRedirect('BPM')).toEqual(['some-url', { q: '123' }]); expect(authService.getRedirect('BPM')).toEqual(['some-url', { q: '123' }]);
})); }));
it('should set redirect navigation commands with query params', async(() => {
spyOn(authService, 'setRedirect').and.callThrough();
spyOn(routerService, 'navigate').and.stub();
const router: RouterStateSnapshot = <RouterStateSnapshot> { url: '/' };
authGuard.canActivate(null, router);
expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'BPM', navigation: ['/']
});
expect(authService.getRedirect('BPM')).toEqual(['/']);
}));
it('should get redirect url from config if there is one configured', async(() => { it('should get redirect url from config if there is one configured', async(() => {
appConfigService.config.loginRoute = 'fakeLoginRoute'; appConfigService.config.loginRoute = 'fakeLoginRoute';
spyOn(authService, 'setRedirect').and.callThrough(); spyOn(authService, 'setRedirect').and.callThrough();

View File

@@ -60,6 +60,11 @@ export class AuthGuardBpm implements CanActivate, CanActivateChild {
private getNavigationCommands(redirectUrl: string): any[] { private getNavigationCommands(redirectUrl: string): any[] {
const urlTree: UrlTree = this.router.parseUrl(redirectUrl); const urlTree: UrlTree = this.router.parseUrl(redirectUrl);
const urlSegmentGroup: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET]; const urlSegmentGroup: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET];
if (!urlSegmentGroup) {
return [redirectUrl];
}
const urlSegments: UrlSegment[] = urlSegmentGroup.segments; const urlSegments: UrlSegment[] = urlSegmentGroup.segments;
return urlSegments.reduce(function(acc, item) { return urlSegments.reduce(function(acc, item) {

View File

@@ -235,6 +235,28 @@ describe('AuthGuardService ECM', () => {
}); });
}); });
}); });
describe('with no route state', () => {
beforeEach(async(() => {
this.test = new TestConfig({
isLoggedIn: false
});
const { guard, auth, router } = this.test;
guard.canActivate(null, { url: '/' }).then((activate) => {
this.auth = auth;
});
this.navigateSpy = spyOn(router, 'navigate');
}));
it('should set redirect navigation commands with query params', () => {
expect(this.auth.setRedirect).toHaveBeenCalledWith({
provider: 'ECM', navigation: ['/']
});
});
});
}); });
describe('canActivateChild', () => { describe('canActivateChild', () => {

View File

@@ -75,6 +75,11 @@ export class AuthGuardEcm implements CanActivate {
private getNavigationCommands(redirectUrl: string): any[] { private getNavigationCommands(redirectUrl: string): any[] {
const urlTree: UrlTree = this.router.parseUrl(redirectUrl); const urlTree: UrlTree = this.router.parseUrl(redirectUrl);
const urlSegmentGroup: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET]; const urlSegmentGroup: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET];
if (!urlSegmentGroup) {
return [redirectUrl];
}
const urlSegments: UrlSegment[] = urlSegmentGroup.segments; const urlSegments: UrlSegment[] = urlSegmentGroup.segments;
return urlSegments.reduce(function(acc, item) { return urlSegments.reduce(function(acc, item) {

View File

@@ -101,4 +101,17 @@ describe('AuthGuardService', () => {
}); });
expect(router.navigate).toHaveBeenCalledWith(['/fakeLoginRoute']); expect(router.navigate).toHaveBeenCalledWith(['/fakeLoginRoute']);
})); }));
it('should pass actual redirect when no state segments exists', async(() => {
state.url = '/';
spyOn(router, 'navigate');
spyOn(authService, 'setRedirect');
service.canActivate(null, state);
expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'ALL', navigation: ['/']
});
}));
}); });

View File

@@ -63,6 +63,11 @@ export class AuthGuard implements CanActivate, CanActivateChild {
private getNavigationCommands(redirectUrl: string): any[] { private getNavigationCommands(redirectUrl: string): any[] {
const urlTree: UrlTree = this.router.parseUrl(redirectUrl); const urlTree: UrlTree = this.router.parseUrl(redirectUrl);
const urlSegmentGroup: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET]; const urlSegmentGroup: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET];
if (!urlSegmentGroup) {
return [redirectUrl];
}
const urlSegments: UrlSegment[] = urlSegmentGroup.segments; const urlSegments: UrlSegment[] = urlSegmentGroup.segments;
return urlSegments.reduce(function(acc, item) { return urlSegments.reduce(function(acc, item) {