mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-19 17:14:57 +00:00
improved ecm auth guard (#2402)
This commit is contained in:
parent
7c1bd46642
commit
250aa360d2
@ -17,67 +17,169 @@
|
||||
|
||||
import { async, inject, TestBed } from '@angular/core/testing';
|
||||
import { Router } from '@angular/router';
|
||||
import { RouterTestingModule } from '@angular/router/testing';
|
||||
import { AlfrescoApiService } from 'ng2-alfresco-core';
|
||||
|
||||
import { CookieServiceMock } from './../assets/cookie.service.mock';
|
||||
import { AlfrescoApiService } from './alfresco-api.service';
|
||||
import { AlfrescoSettingsService } from './alfresco-settings.service';
|
||||
import { AppConfigModule } from './app-config.service';
|
||||
import { AuthGuardEcm } from './auth-guard-ecm.service';
|
||||
import { AuthenticationService } from './authentication.service';
|
||||
import { CookieService } from './cookie.service';
|
||||
import { LogService } from './log.service';
|
||||
import { StorageService } from './storage.service';
|
||||
import { UserPreferencesService } from './user-preferences.service';
|
||||
|
||||
describe('AuthGuardService ECM', () => {
|
||||
class RouterProvider {
|
||||
navigate: Function = jasmine.createSpy('RouterProviderNavigate');
|
||||
}
|
||||
|
||||
class AlfrescoApiServiceProvider {
|
||||
private settings: any = {
|
||||
validateTicket: true,
|
||||
isLoggedIn: true
|
||||
};
|
||||
|
||||
constructor(settings: any = {}) {
|
||||
Object.assign(this.settings, settings);
|
||||
}
|
||||
|
||||
getInstance() {
|
||||
return {
|
||||
ecmAuth: this.ecmAuth
|
||||
};
|
||||
}
|
||||
|
||||
private get ecmAuth() {
|
||||
return {
|
||||
validateTicket: this.validateTicket.bind(this),
|
||||
isLoggedIn: this.isLoggedIn.bind(this)
|
||||
};
|
||||
}
|
||||
|
||||
private validateTicket() {
|
||||
const { validateTicket } = this.settings;
|
||||
|
||||
return validateTicket
|
||||
? Promise.resolve('Valid!')
|
||||
: Promise.reject('Invalid');
|
||||
}
|
||||
|
||||
private isLoggedIn() {
|
||||
return this.settings.isLoggedIn;
|
||||
}
|
||||
}
|
||||
|
||||
class TestConfig {
|
||||
router: any;
|
||||
guard: any;
|
||||
|
||||
private settings: any = {
|
||||
validateTicket: true,
|
||||
isLoggedIn: true
|
||||
};
|
||||
|
||||
constructor(settings: any = {}) {
|
||||
Object.assign(this.settings, settings);
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
AppConfigModule,
|
||||
RouterTestingModule
|
||||
],
|
||||
declarations: [
|
||||
],
|
||||
providers: [
|
||||
AuthGuardEcm,
|
||||
AlfrescoSettingsService,
|
||||
AlfrescoApiService,
|
||||
AuthenticationService,
|
||||
StorageService,
|
||||
UserPreferencesService,
|
||||
{ provide: CookieService, useClass: CookieServiceMock },
|
||||
LogService
|
||||
this.routerProvider,
|
||||
this.alfrescoApiServiceProvider,
|
||||
AuthGuardEcm
|
||||
]
|
||||
}).compileComponents();
|
||||
});
|
||||
|
||||
inject([ AuthGuardEcm, Router ], (guard: AuthGuardEcm, router: Router) => {
|
||||
this.guard = guard;
|
||||
this.router = router;
|
||||
})();
|
||||
}
|
||||
|
||||
private get routerProvider() {
|
||||
return {
|
||||
provide: Router,
|
||||
useValue: new RouterProvider()
|
||||
};
|
||||
}
|
||||
|
||||
private get alfrescoApiServiceProvider () {
|
||||
const { validateTicket, isLoggedIn } = this.settings;
|
||||
|
||||
return {
|
||||
provide: AlfrescoApiService,
|
||||
useValue: new AlfrescoApiServiceProvider({
|
||||
validateTicket,
|
||||
isLoggedIn
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
get navigateSpy() {
|
||||
return this.router.navigate;
|
||||
}
|
||||
}
|
||||
|
||||
describe('CanActivateLoggedIn', () => {
|
||||
describe('user is not logged in', () => {
|
||||
beforeEach(async(() => {
|
||||
this.test = new TestConfig({
|
||||
isLoggedIn: false
|
||||
});
|
||||
|
||||
const { guard, router } = this.test;
|
||||
|
||||
guard.canActivate().then((activate) => {
|
||||
this.activate = activate;
|
||||
this.navigateSpy = router.navigate;
|
||||
});
|
||||
}));
|
||||
|
||||
it('if the alfresco js api is logged in should canActivate be true',
|
||||
async(inject([AuthGuardEcm, Router, AlfrescoSettingsService, StorageService, AuthenticationService], (auth, router, settingsService, storage, authService) => {
|
||||
spyOn(router, 'navigate');
|
||||
|
||||
authService.isEcmLoggedIn = () => {
|
||||
return true;
|
||||
};
|
||||
|
||||
expect(auth.canActivate()).toBeTruthy();
|
||||
expect(router.navigate).not.toHaveBeenCalled();
|
||||
}))
|
||||
);
|
||||
|
||||
it('if the alfresco js api is NOT logged in should canActivate be false',
|
||||
async(inject([AuthGuardEcm, Router, AlfrescoSettingsService, StorageService, AuthenticationService], (auth, router, settingsService, storage, authService) => {
|
||||
|
||||
spyOn(router, 'navigate');
|
||||
|
||||
authService.isEcmLoggedIn = () => {
|
||||
return false;
|
||||
};
|
||||
|
||||
expect(auth.canActivate()).toBeFalsy();
|
||||
expect(router.navigate).toHaveBeenCalled();
|
||||
}))
|
||||
);
|
||||
|
||||
it('does not allow route to activate', () => {
|
||||
expect(this.activate).toBe(false);
|
||||
});
|
||||
|
||||
it('redirects to /login', () => {
|
||||
expect(this.navigateSpy).toHaveBeenCalledWith([ '/login' ]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('user is logged in but ticket is invalid', () => {
|
||||
beforeEach(async(() => {
|
||||
this.test = new TestConfig({
|
||||
isLoggedIn: true,
|
||||
validateTicket: false
|
||||
});
|
||||
|
||||
const { guard, router } = this.test;
|
||||
|
||||
guard.canActivate().then((activate) => {
|
||||
this.activate = activate;
|
||||
this.navigateSpy = router.navigate;
|
||||
});
|
||||
}));
|
||||
|
||||
it('does not allow route to activate', () => {
|
||||
expect(this.activate).toBe(false);
|
||||
});
|
||||
|
||||
it('redirects to /login', () => {
|
||||
expect(this.navigateSpy).toHaveBeenCalledWith([ '/login' ]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('user is logged in and ticket is valid', () => {
|
||||
beforeEach(async(() => {
|
||||
this.test = new TestConfig({
|
||||
isLoggedIn: true,
|
||||
validateTicket: true
|
||||
});
|
||||
|
||||
const { guard, router } = this.test;
|
||||
|
||||
guard.canActivate().then((activate) => {
|
||||
this.activate = activate;
|
||||
this.navigateSpy = router.navigate;
|
||||
});
|
||||
}));
|
||||
|
||||
it('allows route to activate', () => {
|
||||
expect(this.activate).toBe(true);
|
||||
});
|
||||
|
||||
it('does not redirect', () => {
|
||||
expect(this.navigateSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -16,38 +16,39 @@
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import {
|
||||
ActivatedRouteSnapshot, CanActivate, CanActivateChild,
|
||||
Router,
|
||||
RouterStateSnapshot
|
||||
} from '@angular/router';
|
||||
import { CanActivate, Router } from '@angular/router';
|
||||
|
||||
import { AuthenticationService } from './authentication.service';
|
||||
import { AlfrescoApiService } from './alfresco-api.service';
|
||||
|
||||
@Injectable()
|
||||
export class AuthGuardEcm implements CanActivate, CanActivateChild {
|
||||
constructor(private authService: AuthenticationService, private router: Router) {}
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
|
||||
// let url: string = state.url;
|
||||
|
||||
return this.checkLogin();
|
||||
export class AuthGuardEcm implements CanActivate {
|
||||
constructor(
|
||||
private apiService: AlfrescoApiService,
|
||||
private router: Router) {
|
||||
}
|
||||
|
||||
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
|
||||
return this.canActivate(route, state);
|
||||
private get authApi() {
|
||||
return this.apiService.getInstance().ecmAuth;
|
||||
}
|
||||
|
||||
checkLogin(): boolean {
|
||||
if (this.authService.isEcmLoggedIn()) {
|
||||
return true;
|
||||
private isLoggedIn(): Promise<boolean> {
|
||||
if (!this.authApi.isLoggedIn()) {
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
|
||||
// Store the attempted URL for redirecting
|
||||
// this.authService.redirectUrl = url;
|
||||
return this.authApi
|
||||
.validateTicket()
|
||||
.then(() => true, () => false)
|
||||
.catch(() => false);
|
||||
}
|
||||
|
||||
// Navigate to the login page with extras
|
||||
canActivate(): Promise<boolean> {
|
||||
return this.isLoggedIn().then(isLoggedIn => {
|
||||
if (!isLoggedIn) {
|
||||
this.router.navigate([ '/login' ]);
|
||||
return false;
|
||||
}
|
||||
|
||||
return isLoggedIn;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user