From f63614e964f74d0ea15caac8034f4d86c8c29688 Mon Sep 17 00:00:00 2001 From: Cilibiu Bogdan Date: Wed, 9 May 2018 09:59:03 +0300 Subject: [PATCH] pass actual redirect when no state (#3285) --- .../services/auth-guard-bpm.service.spec.ts | 13 +++++++++++ lib/core/services/auth-guard-bpm.service.ts | 5 +++++ .../services/auth-guard-ecm.service.spec.ts | 22 +++++++++++++++++++ lib/core/services/auth-guard-ecm.service.ts | 5 +++++ lib/core/services/auth-guard.service.spec.ts | 13 +++++++++++ lib/core/services/auth-guard.service.ts | 5 +++++ 6 files changed, 63 insertions(+) diff --git a/lib/core/services/auth-guard-bpm.service.spec.ts b/lib/core/services/auth-guard-bpm.service.spec.ts index e4c58cfa15..e8a376ac4a 100644 --- a/lib/core/services/auth-guard-bpm.service.spec.ts +++ b/lib/core/services/auth-guard-bpm.service.spec.ts @@ -91,6 +91,19 @@ describe('AuthGuardService BPM', () => { 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 = { 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(() => { appConfigService.config.loginRoute = 'fakeLoginRoute'; spyOn(authService, 'setRedirect').and.callThrough(); diff --git a/lib/core/services/auth-guard-bpm.service.ts b/lib/core/services/auth-guard-bpm.service.ts index 723f981196..314d39bb2f 100644 --- a/lib/core/services/auth-guard-bpm.service.ts +++ b/lib/core/services/auth-guard-bpm.service.ts @@ -60,6 +60,11 @@ export class AuthGuardBpm implements CanActivate, CanActivateChild { private getNavigationCommands(redirectUrl: string): any[] { const urlTree: UrlTree = this.router.parseUrl(redirectUrl); const urlSegmentGroup: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET]; + + if (!urlSegmentGroup) { + return [redirectUrl]; + } + const urlSegments: UrlSegment[] = urlSegmentGroup.segments; return urlSegments.reduce(function(acc, item) { diff --git a/lib/core/services/auth-guard-ecm.service.spec.ts b/lib/core/services/auth-guard-ecm.service.spec.ts index aaee0a6980..485e6fa485 100644 --- a/lib/core/services/auth-guard-ecm.service.spec.ts +++ b/lib/core/services/auth-guard-ecm.service.spec.ts @@ -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', () => { diff --git a/lib/core/services/auth-guard-ecm.service.ts b/lib/core/services/auth-guard-ecm.service.ts index 1df1359f75..6c76342ccb 100644 --- a/lib/core/services/auth-guard-ecm.service.ts +++ b/lib/core/services/auth-guard-ecm.service.ts @@ -75,6 +75,11 @@ export class AuthGuardEcm implements CanActivate { private getNavigationCommands(redirectUrl: string): any[] { const urlTree: UrlTree = this.router.parseUrl(redirectUrl); const urlSegmentGroup: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET]; + + if (!urlSegmentGroup) { + return [redirectUrl]; + } + const urlSegments: UrlSegment[] = urlSegmentGroup.segments; return urlSegments.reduce(function(acc, item) { diff --git a/lib/core/services/auth-guard.service.spec.ts b/lib/core/services/auth-guard.service.spec.ts index 7363fb9d4e..70f19afaa3 100644 --- a/lib/core/services/auth-guard.service.spec.ts +++ b/lib/core/services/auth-guard.service.spec.ts @@ -101,4 +101,17 @@ describe('AuthGuardService', () => { }); 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: ['/'] + }); + })); }); diff --git a/lib/core/services/auth-guard.service.ts b/lib/core/services/auth-guard.service.ts index 5cfeeaac1d..5fe92b6d16 100644 --- a/lib/core/services/auth-guard.service.ts +++ b/lib/core/services/auth-guard.service.ts @@ -63,6 +63,11 @@ export class AuthGuard implements CanActivate, CanActivateChild { private getNavigationCommands(redirectUrl: string): any[] { const urlTree: UrlTree = this.router.parseUrl(redirectUrl); const urlSegmentGroup: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET]; + + if (!urlSegmentGroup) { + return [redirectUrl]; + } + const urlSegments: UrlSegment[] = urlSegmentGroup.segments; return urlSegments.reduce(function(acc, item) {