[ADF-4183] improved auth redirection (#4399)

* base auth guard implementation

* remove code duplication

* unit test fixes
This commit is contained in:
Denys Vuika
2019-03-06 13:14:23 +00:00
committed by Eugenio Romano
parent bf331f1145
commit f6341e31a0
9 changed files with 216 additions and 173 deletions

View File

@@ -27,7 +27,7 @@ describe('AuthGuardService ECM', () => {
let authGuard: AuthGuardEcm;
let authService: AuthenticationService;
let routerService: Router;
let router: Router;
let appConfigService: AppConfigService;
setupTestBed({
@@ -38,7 +38,7 @@ describe('AuthGuardService ECM', () => {
localStorage.clear();
authService = TestBed.get(AuthenticationService);
authGuard = TestBed.get(AuthGuardEcm);
routerService = TestBed.get(Router);
router = TestBed.get(Router);
appConfigService = TestBed.get(AppConfigService);
appConfigService.config.providers = 'ECM';
@@ -47,45 +47,45 @@ describe('AuthGuardService ECM', () => {
it('if the alfresco js api is logged in should canActivate be true', async(() => {
spyOn(authService, 'isEcmLoggedIn').and.returnValue(true);
const router: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};
const route: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};
expect(authGuard.canActivate(null, router)).toBeTruthy();
expect(authGuard.canActivate(null, route)).toBeTruthy();
}));
it('if the alfresco js api is configured with withCredentials true should canActivate be true', async(() => {
spyOn(authService, 'isBpmLoggedIn').and.returnValue(true);
appConfigService.config.auth.withCredentials = true;
const router: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};
const route: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};
expect(authGuard.canActivate(null, router)).toBeTruthy();
expect(authGuard.canActivate(null, route)).toBeTruthy();
}));
it('if the alfresco js api is NOT logged in should canActivate be false', async(() => {
spyOn(authService, 'isEcmLoggedIn').and.returnValue(false);
spyOn(routerService, 'navigate').and.stub();
const router: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
spyOn(router, 'navigateByUrl').and.stub();
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
expect(authGuard.canActivate(null, router)).toBeFalsy();
expect(authGuard.canActivate(null, route)).toBeFalsy();
}));
it('if the alfresco js api is NOT logged in should trigger a redirect event', async(() => {
appConfigService.config.loginRoute = 'login';
spyOn(routerService, 'navigate');
spyOn(router, 'navigateByUrl');
spyOn(authService, 'isEcmLoggedIn').and.returnValue(false);
const router: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};
const route: RouterStateSnapshot = <RouterStateSnapshot> {url : 'some-url'};
expect(authGuard.canActivate(null, router)).toBeFalsy();
expect(routerService.navigate).toHaveBeenCalledWith(['/login']);
expect(authGuard.canActivate(null, route)).toBeFalsy();
expect(router.navigateByUrl).toHaveBeenCalledWith('/login?redirectUrl=some-url');
}));
it('should set redirect navigation commands', async(() => {
spyOn(authService, 'setRedirect').and.callThrough();
spyOn(routerService, 'navigate').and.stub();
const router: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
spyOn(router, 'navigateByUrl').and.stub();
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
authGuard.canActivate(null, router);
authGuard.canActivate(null, route);
expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'ECM', url: 'some-url'
@@ -95,10 +95,10 @@ describe('AuthGuardService ECM', () => {
it('should set redirect navigation commands with query params', async(() => {
spyOn(authService, 'setRedirect').and.callThrough();
spyOn(routerService, 'navigate').and.stub();
const router: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url;q=123' };
spyOn(router, 'navigateByUrl').and.stub();
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url;q=123' };
authGuard.canActivate(null, router);
authGuard.canActivate(null, route);
expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'ECM', url: 'some-url;q=123'
@@ -108,10 +108,10 @@ describe('AuthGuardService ECM', () => {
it('should set redirect navigation commands with query params', async(() => {
spyOn(authService, 'setRedirect').and.callThrough();
spyOn(routerService, 'navigate').and.stub();
const router: RouterStateSnapshot = <RouterStateSnapshot> { url: '/' };
spyOn(router, 'navigateByUrl').and.stub();
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: '/' };
authGuard.canActivate(null, router);
authGuard.canActivate(null, route);
expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'ECM', url: '/'
@@ -122,15 +122,15 @@ describe('AuthGuardService ECM', () => {
it('should get redirect url from config if there is one configured', async(() => {
appConfigService.config.loginRoute = 'fakeLoginRoute';
spyOn(authService, 'setRedirect').and.callThrough();
spyOn(routerService, 'navigate').and.stub();
const router: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
spyOn(router, 'navigateByUrl').and.stub();
const route: RouterStateSnapshot = <RouterStateSnapshot> { url: 'some-url' };
authGuard.canActivate(null, router);
authGuard.canActivate(null, route);
expect(authService.setRedirect).toHaveBeenCalledWith({
provider: 'ECM', url: 'some-url'
});
expect(routerService.navigate).toHaveBeenCalledWith(['/fakeLoginRoute']);
expect(router.navigateByUrl).toHaveBeenCalledWith('/fakeLoginRoute?redirectUrl=some-url');
}));
});