[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
}
],
canActivate: [
AuthGuardEcm
]
canActivateChild: [ AuthGuardEcm ],
canActivate: [ AuthGuardEcm ]
}
];

View File

@@ -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');
});
});
});

View File

@@ -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());
}
}