From 53eea3567d9ec02c32579f64b2eff79c218d714f Mon Sep 17 00:00:00 2001 From: Cilibiu Bogdan Date: Mon, 4 Dec 2017 12:49:40 +0200 Subject: [PATCH] [ACA-413] redirect on AuthGuard (#106) --- src/app/app.routes.ts | 5 +- .../components/login/login.component.spec.ts | 131 +++++------------- src/app/components/login/login.component.ts | 36 +---- 3 files changed, 41 insertions(+), 131 deletions(-) diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index 2ede312a8..acd9e6729 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -129,9 +129,8 @@ export const APP_ROUTES: Routes = [ component: GenericErrorComponent } ], - canActivate: [ - AuthGuardEcm - ] + canActivateChild: [ AuthGuardEcm ], + canActivate: [ AuthGuardEcm ] } ]; diff --git a/src/app/components/login/login.component.spec.ts b/src/app/components/login/login.component.spec.ts index 0f2671257..5da034c50 100644 --- a/src/app/components/login/login.component.spec.ts +++ b/src/app/components/login/login.component.spec.ts @@ -16,120 +16,59 @@ */ import { RouterTestingModule } from '@angular/router/testing'; -import { Router, ActivatedRoute } from '@angular/router'; +import { Router } from '@angular/router'; import { TestBed, async } from '@angular/core/testing'; -import { Observable } from 'rxjs/Rx'; -import { CoreModule, AuthenticationService, UserPreferencesService, LoginModule } from '@alfresco/adf-core'; +import { CoreModule, AuthenticationService, UserPreferencesService } from '@alfresco/adf-core'; import { LoginComponent } from './login.component'; describe('LoginComponent', () => { + let component; + let fixture; + let router; + let userPreference; + let auth; - class TestConfig { - private testBed; - private componentInstance; - private fixture; - - constructor(config: any = {}) { - const routerProvider = { - provide: Router, - useValue: { - navigateByUrl: jasmine.createSpy('navigateByUrl'), - navigate: jasmine.createSpy('navigate') - } - }; - - const authProvider = { - provide: AuthenticationService, - useValue: { - isEcmLoggedIn: jasmine.createSpy('navigateByUrl') - .and.returnValue(config.isEcmLoggedIn || false) - } - }; - - this.testBed = TestBed.configureTestingModule({ - imports: [ - RouterTestingModule, - CoreModule, - LoginModule - ], - declarations: [ - LoginComponent - ], - providers: [ - routerProvider, - authProvider, - { - provide: ActivatedRoute, - useValue: { - params: Observable.of({ redirect: config.redirect }) - } - } - ] - }); - - this.fixture = TestBed.createComponent(LoginComponent); - this.componentInstance = this.fixture.componentInstance; - this.fixture.detectChanges(); - } - - get userPrefService() { - return TestBed.get(UserPreferencesService); - } - - get authService() { - return TestBed.get(AuthenticationService); - } - - get routerService() { - return TestBed.get(Router); - } - - get component() { - return this.componentInstance; - } - } - - it('load app when user is already logged in', () => { - const testConfig = new TestConfig({ - isEcmLoggedIn: true + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [ + RouterTestingModule, + CoreModule + ], + declarations: [ + LoginComponent + ] }); - expect(testConfig.routerService.navigateByUrl).toHaveBeenCalled(); - }); + fixture = TestBed.createComponent(LoginComponent); + component = fixture.componentInstance; - it('requires user to be logged in', () => { - const testConfig = new TestConfig({ - isEcmLoggedIn: false, - redirect: '/personal-files' - }); + router = TestBed.get(Router); + auth = TestBed.get(AuthenticationService); + userPreference = TestBed.get(UserPreferencesService); - expect(testConfig.routerService.navigate).toHaveBeenCalledWith(['/login', {}]); + fixture.detectChanges(); + })); + + beforeEach(() => { + spyOn(userPreference, 'setStoragePrefix'); + spyOn(router, 'navigateByUrl'); + spyOn(auth, 'getRedirectUrl').and.returnValue('some-url'); }); describe('onLoginSuccess()', () => { - let testConfig; - beforeEach(() => { - testConfig = new TestConfig({ - isEcmLoggedIn: false, - redirect: 'somewhere-over-the-rainbow' - }); + it('should redirect on success', () => { + component.onLoginSuccess(); + + expect(router.navigateByUrl).toHaveBeenCalledWith('some-url'); }); - it('redirects on success', () => { - testConfig.component.onLoginSuccess(); + it('should set user preference store prefix', () => { - expect(testConfig.routerService.navigateByUrl).toHaveBeenCalledWith('somewhere-over-the-rainbow'); - }); + component.onLoginSuccess({ username: 'bogus' }); - it('sets user preference store prefix', () => { - const service = testConfig.userPrefService; - spyOn(service, 'setStoragePrefix').and.stub(); - - testConfig.component.onLoginSuccess({ username: 'bogus' }); - - expect(service.setStoragePrefix).toHaveBeenCalledWith('bogus'); + expect(userPreference.setStoragePrefix).toHaveBeenCalledWith('bogus'); }); }); }); diff --git a/src/app/components/login/login.component.ts b/src/app/components/login/login.component.ts index 14636f13b..e9459c797 100644 --- a/src/app/components/login/login.component.ts +++ b/src/app/components/login/login.component.ts @@ -16,53 +16,25 @@ */ import { Component } from '@angular/core'; -import { Router, ActivatedRoute } from '@angular/router'; -import { Validators } from '@angular/forms'; +import { Router } from '@angular/router'; import { AuthenticationService, UserPreferencesService } from '@alfresco/adf-core'; -const skipRedirectUrls: string[] = [ - '/logout', - '/personal-files' -]; - @Component({ templateUrl: './login.component.html' }) export class LoginComponent { - private redirectUrl = ''; - constructor( private router: Router, - private route: ActivatedRoute, private auth: AuthenticationService, private userPreferences: UserPreferencesService - ) { - if (auth.isEcmLoggedIn()) { - this.redirect(); - } - - route.params.subscribe((params: any) => { - if (skipRedirectUrls.indexOf(params.redirect) > -1) { - const remainingParams = Object.assign({}, params); - - delete remainingParams.redirect; - - router.navigate(['/login', remainingParams]); - } - - this.redirectUrl = params.redirect; - }); - } - - redirect() { - this.router.navigateByUrl(this.redirectUrl || ''); - } + ) {} onLoginSuccess(data) { if (data && data.username) { this.userPreferences.setStoragePrefix(data.username); } - this.redirect(); + + this.router.navigateByUrl(this.auth.getRedirectUrl()); } }