From 61e0b9efb09288021e5fd9a34cbb560fdfa6748c Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Fri, 28 Aug 2020 11:40:22 +0200 Subject: [PATCH] 72699: Remove redundant subscribe + fix tests --- src/app/core/auth/auth.service.spec.ts | 19 +++++++--------- src/app/core/auth/auth.service.ts | 31 -------------------------- 2 files changed, 8 insertions(+), 42 deletions(-) diff --git a/src/app/core/auth/auth.service.spec.ts b/src/app/core/auth/auth.service.spec.ts index be309b15fb..d3c2b6c44d 100644 --- a/src/app/core/auth/auth.service.spec.ts +++ b/src/app/core/auth/auth.service.spec.ts @@ -322,31 +322,28 @@ describe('AuthService test', () => { expect(storage.remove).toHaveBeenCalled(); }); - it('should set redirect url to previous page', () => { - (storage.get as jasmine.Spy).and.returnValue('/collection/123'); - authService.redirectAfterLoginSuccess(); + it('should redirect to reload with redirect url', () => { + authService.navigateToRedirectUrl('/collection/123'); // Reload with redirect URL set to /collection/123 expect(hardRedirectService.redirect).toHaveBeenCalledWith(jasmine.stringMatching(new RegExp('/reload/[0-9]*\\?redirect=' + encodeURIComponent('/collection/123')))); }); - it('should set redirect url to current page', () => { - (storage.get as jasmine.Spy).and.returnValue('/home'); - authService.redirectAfterLoginSuccess(); + it('should redirect to reload with /home', () => { + authService.navigateToRedirectUrl('/home'); // Reload with redirect URL set to /home expect(hardRedirectService.redirect).toHaveBeenCalledWith(jasmine.stringMatching(new RegExp('/reload/[0-9]*\\?redirect=' + encodeURIComponent('/home')))); }); it('should redirect to regular reload and not to /login', () => { - (storage.get as jasmine.Spy).and.returnValue('/login'); - authService.redirectAfterLoginSuccess(); + authService.navigateToRedirectUrl('/login'); // Reload without a redirect URL expect(hardRedirectService.redirect).toHaveBeenCalledWith(jasmine.stringMatching(new RegExp('/reload/[0-9]*(?!\\?)$'))); }); - it('should not redirect when no redirect url is found', () => { - authService.redirectAfterLoginSuccess(); + it('should redirect to regular reload when no redirect url is found', () => { + authService.navigateToRedirectUrl(undefined); // Reload without a redirect URL - expect(hardRedirectService.redirect).not.toHaveBeenCalled(); + expect(hardRedirectService.redirect).toHaveBeenCalledWith(jasmine.stringMatching(new RegExp('/reload/[0-9]*(?!\\?)$'))); }); describe('impersonate', () => { diff --git a/src/app/core/auth/auth.service.ts b/src/app/core/auth/auth.service.ts index 1a3dfbbdc0..150e44296e 100644 --- a/src/app/core/auth/auth.service.ts +++ b/src/app/core/auth/auth.service.ts @@ -78,37 +78,6 @@ export class AuthService { select(isAuthenticated), startWith(false) ).subscribe((authenticated: boolean) => this._authenticated = authenticated); - - // If current route is different from the one setted in authentication guard - // and is not the login route, clear redirect url and messages - const routeUrl$ = this.store.pipe( - select(routerStateSelector), - filter((routerState: RouterReducerState) => isNotUndefined(routerState) - && isNotUndefined(routerState.state) && isNotEmpty(routerState.state.url)), - filter((routerState: RouterReducerState) => !this.isLoginRoute(routerState.state.url)), - map((routerState: RouterReducerState) => routerState.state.url) - ); - const redirectUrl$ = this.store.pipe(select(getRedirectUrl), distinctUntilChanged()); - routeUrl$.pipe( - withLatestFrom(redirectUrl$), - map(([routeUrl, redirectUrl]) => [routeUrl, redirectUrl]) - ).pipe(filter(([routeUrl, redirectUrl]) => isNotEmpty(redirectUrl) && (routeUrl !== redirectUrl))) - .subscribe(() => { - this.clearRedirectUrl(); - }); - } - - /** - * Check if is a login page route - * - * @param {string} url - * @returns {Boolean}. - */ - protected isLoginRoute(url: string) { - const urlTree: UrlTree = this.router.parseUrl(url); - const g: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET]; - const segment = '/' + g.toString(); - return segment === LOGIN_ROUTE; } /**