[ACA-413] redirect on AuthGuard (#106)

This commit is contained in:
Cilibiu Bogdan
2017-12-04 12:49:40 +02:00
committed by Denys Vuika
parent 43b020ca51
commit 53eea3567d
3 changed files with 41 additions and 131 deletions

View File

@@ -129,9 +129,8 @@ export const APP_ROUTES: Routes = [
component: GenericErrorComponent component: GenericErrorComponent
} }
], ],
canActivate: [ canActivateChild: [ AuthGuardEcm ],
AuthGuardEcm canActivate: [ AuthGuardEcm ]
]
} }
]; ];

View File

@@ -16,120 +16,59 @@
*/ */
import { RouterTestingModule } from '@angular/router/testing'; 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 { TestBed, async } from '@angular/core/testing';
import { Observable } from 'rxjs/Rx'; import { CoreModule, AuthenticationService, UserPreferencesService } from '@alfresco/adf-core';
import { CoreModule, AuthenticationService, UserPreferencesService, LoginModule } from '@alfresco/adf-core';
import { LoginComponent } from './login.component'; import { LoginComponent } from './login.component';
describe('LoginComponent', () => { describe('LoginComponent', () => {
let component;
let fixture;
let router;
let userPreference;
let auth;
class TestConfig { beforeEach(async(() => {
private testBed; TestBed.configureTestingModule({
private componentInstance; imports: [
private fixture; RouterTestingModule,
CoreModule
constructor(config: any = {}) { ],
const routerProvider = { declarations: [
provide: Router, LoginComponent
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
}); });
expect(testConfig.routerService.navigateByUrl).toHaveBeenCalled(); fixture = TestBed.createComponent(LoginComponent);
}); component = fixture.componentInstance;
it('requires user to be logged in', () => { router = TestBed.get(Router);
const testConfig = new TestConfig({ auth = TestBed.get(AuthenticationService);
isEcmLoggedIn: false, userPreference = TestBed.get(UserPreferencesService);
redirect: '/personal-files'
});
expect(testConfig.routerService.navigate).toHaveBeenCalledWith(['/login', {}]); fixture.detectChanges();
}));
beforeEach(() => {
spyOn(userPreference, 'setStoragePrefix');
spyOn(router, 'navigateByUrl');
spyOn(auth, 'getRedirectUrl').and.returnValue('some-url');
}); });
describe('onLoginSuccess()', () => { describe('onLoginSuccess()', () => {
let testConfig;
beforeEach(() => { it('should redirect on success', () => {
testConfig = new TestConfig({ component.onLoginSuccess();
isEcmLoggedIn: false,
redirect: 'somewhere-over-the-rainbow' expect(router.navigateByUrl).toHaveBeenCalledWith('some-url');
});
}); });
it('redirects on success', () => { it('should set user preference store prefix', () => {
testConfig.component.onLoginSuccess();
expect(testConfig.routerService.navigateByUrl).toHaveBeenCalledWith('somewhere-over-the-rainbow'); component.onLoginSuccess({ username: 'bogus' });
});
it('sets user preference store prefix', () => { expect(userPreference.setStoragePrefix).toHaveBeenCalledWith('bogus');
const service = testConfig.userPrefService;
spyOn(service, 'setStoragePrefix').and.stub();
testConfig.component.onLoginSuccess({ username: 'bogus' });
expect(service.setStoragePrefix).toHaveBeenCalledWith('bogus');
}); });
}); });
}); });

View File

@@ -16,53 +16,25 @@
*/ */
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router'; import { Router } from '@angular/router';
import { Validators } from '@angular/forms';
import { AuthenticationService, UserPreferencesService } from '@alfresco/adf-core'; import { AuthenticationService, UserPreferencesService } from '@alfresco/adf-core';
const skipRedirectUrls: string[] = [
'/logout',
'/personal-files'
];
@Component({ @Component({
templateUrl: './login.component.html' templateUrl: './login.component.html'
}) })
export class LoginComponent { export class LoginComponent {
private redirectUrl = '';
constructor( constructor(
private router: Router, private router: Router,
private route: ActivatedRoute,
private auth: AuthenticationService, private auth: AuthenticationService,
private userPreferences: UserPreferencesService 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) { onLoginSuccess(data) {
if (data && data.username) { if (data && data.username) {
this.userPreferences.setStoragePrefix(data.username); this.userPreferences.setStoragePrefix(data.username);
} }
this.redirect();
this.router.navigateByUrl(this.auth.getRedirectUrl());
} }
} }