diff --git a/lib/core/src/lib/auth/guard/auth-guard-bpm.service.spec.ts b/lib/core/src/lib/auth/guard/auth-guard-bpm.service.spec.ts index f4c8770798b..077eeefe173 100644 --- a/lib/core/src/lib/auth/guard/auth-guard-bpm.service.spec.ts +++ b/lib/core/src/lib/auth/guard/auth-guard-bpm.service.spec.ts @@ -19,7 +19,7 @@ import { TestBed } from '@angular/core/testing'; import { AppConfigService } from '../../app-config/app-config.service'; import { AuthGuardBpm } from './auth-guard-bpm.service'; import { AuthenticationService } from '../services/authentication.service'; -import { RouterStateSnapshot, Router } from '@angular/router'; +import { RouterStateSnapshot, Router, UrlTree } from '@angular/router'; import { MatDialog, MatDialogModule } from '@angular/material/dialog'; import { TranslateModule } from '@ngx-translate/core'; import { BasicAlfrescoAuthService } from '../basic-auth/basic-alfresco-auth.service'; @@ -29,7 +29,6 @@ import { EMPTY, of } from 'rxjs'; import { OidcAuthenticationService } from '../oidc/oidc-authentication.service'; describe('AuthGuardService BPM', () => { - let authGuard: AuthGuardBpm; let authService: AuthenticationService; let basicAlfrescoAuthService: BasicAlfrescoAuthService; let oidcAuthenticationService: OidcAuthenticationService; @@ -57,7 +56,6 @@ describe('AuthGuardService BPM', () => { basicAlfrescoAuthService = TestBed.inject(BasicAlfrescoAuthService); oidcAuthenticationService = TestBed.inject(OidcAuthenticationService); authService = TestBed.inject(AuthenticationService); - authGuard = TestBed.inject(AuthGuardBpm); router = TestBed.inject(Router); appConfigService = TestBed.inject(AppConfigService); @@ -66,6 +64,22 @@ describe('AuthGuardService BPM', () => { appConfigService.config.oauth2 = {}; }); + const runAuthGuardWithContext = async (state: RouterStateSnapshot): Promise => { + const result = TestBed.runInInjectionContext(() => AuthGuardBpm(state)); + if (result instanceof Observable) { + return handleObservableResult(result); + } else { + return result; + } + }; + + const handleObservableResult = (result: Observable): Promise => + new Promise((resolve) => { + result.subscribe((value) => { + resolve(value); + }); + }); + it('should redirect url if the alfresco js api is NOT logged in and isOAuth with silentLogin', async () => { spyOn(router, 'navigateByUrl').and.stub(); spyOn(authService, 'isBpmLoggedIn').and.returnValue(false); @@ -85,7 +99,7 @@ describe('AuthGuardService BPM', () => { const route = { url: 'abc' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeFalsy(); + expect(await runAuthGuardWithContext(route)).toBeFalsy(); expect(oidcAuthenticationService.ssoLogin).toHaveBeenCalledTimes(1); }); @@ -93,7 +107,7 @@ describe('AuthGuardService BPM', () => { spyOn(authService, 'isBpmLoggedIn').and.returnValue(true); const route = { url: 'some-url' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeTruthy(); + expect(await runAuthGuardWithContext(route)).toBeTruthy(); }); it('if the alfresco js api is configured with withCredentials true should canActivate be true', async () => { @@ -102,7 +116,7 @@ describe('AuthGuardService BPM', () => { const route = { url: 'some-url' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeTruthy(); + expect(await runAuthGuardWithContext(route)).toBeTruthy(); }); it('if the alfresco js api is NOT logged in should canActivate be false', async () => { @@ -110,7 +124,7 @@ describe('AuthGuardService BPM', () => { spyOn(router, 'navigateByUrl').and.stub(); const route = { url: 'some-url' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeFalsy(); + expect(await runAuthGuardWithContext(route)).toBeFalsy(); }); it('if the alfresco js api is NOT logged in should trigger a redirect event', async () => { @@ -120,7 +134,7 @@ describe('AuthGuardService BPM', () => { spyOn(authService, 'isBpmLoggedIn').and.returnValue(false); const route = { url: 'some-url' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeFalsy(); + expect(await runAuthGuardWithContext(route)).toBeFalsy(); expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/login?redirectUrl=some-url')); }); @@ -131,7 +145,7 @@ describe('AuthGuardService BPM', () => { appConfigService.config.oauth2.silentLogin = false; const route = { url: 'some-url' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeFalsy(); + expect(await runAuthGuardWithContext(route)).toBeFalsy(); expect(router.navigateByUrl).toHaveBeenCalled(); }); @@ -142,16 +156,16 @@ describe('AuthGuardService BPM', () => { appConfigService.config.oauth2.silentLogin = undefined; const route = { url: 'some-url' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeFalsy(); + expect(await runAuthGuardWithContext(route)).toBeFalsy(); expect(router.navigateByUrl).toHaveBeenCalled(); }); - it('should set redirect url', () => { + it('should set redirect url', async () => { spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough(); spyOn(router, 'navigateByUrl').and.stub(); const route = { url: 'some-url' } as RouterStateSnapshot; - authGuard.canActivate(null, route); + await runAuthGuardWithContext(route); expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({ provider: 'BPM', @@ -160,12 +174,12 @@ describe('AuthGuardService BPM', () => { expect(basicAlfrescoAuthService.getRedirect()).toEqual('some-url'); }); - it('should set redirect navigation commands with query params', () => { + it('should set redirect navigation commands with query params', async () => { spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough(); spyOn(router, 'navigateByUrl').and.stub(); const route = { url: 'some-url;q=123' } as RouterStateSnapshot; - authGuard.canActivate(null, route); + await runAuthGuardWithContext(route); expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({ provider: 'BPM', @@ -174,12 +188,12 @@ describe('AuthGuardService BPM', () => { expect(basicAlfrescoAuthService.getRedirect()).toEqual('some-url;q=123'); }); - it('should set redirect navigation commands with query params', () => { + it('should set redirect navigation commands with query params', async () => { spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough(); spyOn(router, 'navigateByUrl').and.stub(); const route = { url: '/' } as RouterStateSnapshot; - authGuard.canActivate(null, route); + await runAuthGuardWithContext(route); expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({ provider: 'BPM', @@ -188,13 +202,13 @@ describe('AuthGuardService BPM', () => { expect(basicAlfrescoAuthService.getRedirect()).toEqual('/'); }); - it('should get redirect url from config if there is one configured', () => { + it('should get redirect url from config if there is one configured', async () => { appConfigService.config.loginRoute = 'fakeLoginRoute'; spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough(); spyOn(router, 'navigateByUrl').and.stub(); const route = { url: 'some-url' } as RouterStateSnapshot; - authGuard.canActivate(null, route); + await runAuthGuardWithContext(route); expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({ provider: 'BPM', @@ -203,7 +217,7 @@ describe('AuthGuardService BPM', () => { expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/fakeLoginRoute?redirectUrl=some-url')); }); - it('should to close the material dialog if is redirect to the login', () => { + it('should to close the material dialog if is redirect to the login', async () => { const materialDialog = TestBed.inject(MatDialog); spyOn(materialDialog, 'closeAll'); @@ -212,7 +226,7 @@ describe('AuthGuardService BPM', () => { spyOn(router, 'navigateByUrl').and.stub(); const route = { url: 'some-url' } as RouterStateSnapshot; - authGuard.canActivate(null, route); + await runAuthGuardWithContext(route); expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({ provider: 'BPM', diff --git a/lib/core/src/lib/auth/guard/auth-guard-ecm.service.spec.ts b/lib/core/src/lib/auth/guard/auth-guard-ecm.service.spec.ts index 7946bee14e9..ffb26e6f651 100644 --- a/lib/core/src/lib/auth/guard/auth-guard-ecm.service.spec.ts +++ b/lib/core/src/lib/auth/guard/auth-guard-ecm.service.spec.ts @@ -19,7 +19,7 @@ import { TestBed } from '@angular/core/testing'; import { AppConfigService } from '../../app-config/app-config.service'; import { AuthGuardEcm } from './auth-guard-ecm.service'; import { AuthenticationService } from '../services/authentication.service'; -import { RouterStateSnapshot, Router } from '@angular/router'; +import { RouterStateSnapshot, Router, UrlTree } from '@angular/router'; import { MatDialog, MatDialogModule } from '@angular/material/dialog'; import { TranslateModule } from '@ngx-translate/core'; import { BasicAlfrescoAuthService } from '../basic-auth/basic-alfresco-auth.service'; @@ -30,7 +30,6 @@ import { EMPTY, of } from 'rxjs'; import { OidcAuthenticationService } from '../oidc/oidc-authentication.service'; describe('AuthGuardService ECM', () => { - let authGuard: AuthGuardEcm; let authService: AuthenticationService; let basicAlfrescoAuthService: BasicAlfrescoAuthService; let oidcAuthenticationService: OidcAuthenticationService; @@ -59,7 +58,6 @@ describe('AuthGuardService ECM', () => { oidcAuthenticationService = TestBed.inject(OidcAuthenticationService); basicAlfrescoAuthService = TestBed.inject(BasicAlfrescoAuthService); authService = TestBed.inject(AuthenticationService); - authGuard = TestBed.inject(AuthGuardEcm); router = TestBed.inject(Router); appConfigService = TestBed.inject(AppConfigService); @@ -68,11 +66,27 @@ describe('AuthGuardService ECM', () => { appConfigService.config.oauth2 = {}; }); + const runAuthGuardWithContext = async (state: RouterStateSnapshot): Promise => { + const result = TestBed.runInInjectionContext(() => AuthGuardEcm(state)); + if (result instanceof Observable) { + return handleObservableResult(result); + } else { + return result; + } + }; + + const handleObservableResult = (result: Observable): Promise => + new Promise((resolve) => { + result.subscribe((value) => { + resolve(value); + }); + }); + it('if the alfresco js api is logged in should canActivate be true', async () => { spyOn(authService, 'isEcmLoggedIn').and.returnValue(true); const route = { url: 'some-url' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeTruthy(); + expect(await runAuthGuardWithContext(route)).toBeTruthy(); }); it('if the alfresco js api is configured with withCredentials true should canActivate be true', async () => { @@ -81,7 +95,7 @@ describe('AuthGuardService ECM', () => { const route = { url: 'some-url' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeTruthy(); + expect(await runAuthGuardWithContext(route)).toBeTruthy(); }); it('if the alfresco js api is NOT logged in should canActivate be false', async () => { @@ -89,7 +103,7 @@ describe('AuthGuardService ECM', () => { spyOn(router, 'navigateByUrl').and.stub(); const route = { url: 'some-url' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeFalsy(); + expect(await runAuthGuardWithContext(route)).toBeFalsy(); }); it('if the alfresco js api is NOT logged in should trigger a redirect event', async () => { @@ -99,7 +113,7 @@ describe('AuthGuardService ECM', () => { spyOn(authService, 'isEcmLoggedIn').and.returnValue(false); const route = { url: 'some-url' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeFalsy(); + expect(await runAuthGuardWithContext(route)).toBeFalsy(); expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/login?redirectUrl=some-url')); }); @@ -110,7 +124,7 @@ describe('AuthGuardService ECM', () => { appConfigService.config.oauth2.silentLogin = false; const route = { url: 'some-url' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeFalsy(); + expect(await runAuthGuardWithContext(route)).toBeFalsy(); expect(router.navigateByUrl).toHaveBeenCalled(); }); @@ -131,7 +145,7 @@ describe('AuthGuardService ECM', () => { const route = { url: 'abc' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeFalsy(); + expect(await runAuthGuardWithContext(route)).toBeFalsy(); expect(oidcAuthenticationService.ssoLogin).toHaveBeenCalledTimes(1); }); @@ -142,16 +156,16 @@ describe('AuthGuardService ECM', () => { appConfigService.config.oauth2.silentLogin = undefined; const route = { url: 'some-url' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeFalsy(); + expect(await runAuthGuardWithContext(route)).toBeFalsy(); expect(router.navigateByUrl).toHaveBeenCalled(); }); - it('should set redirect navigation commands', () => { + it('should set redirect navigation commands', async () => { spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough(); spyOn(router, 'navigateByUrl').and.stub(); const route = { url: 'some-url' } as RouterStateSnapshot; - authGuard.canActivate(null, route); + await runAuthGuardWithContext(route); expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({ provider: 'ECM', @@ -160,12 +174,12 @@ describe('AuthGuardService ECM', () => { expect(basicAlfrescoAuthService.getRedirect()).toEqual('some-url'); }); - it('should set redirect navigation commands with query params', () => { + it('should set redirect navigation commands with query params', async () => { spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough(); spyOn(router, 'navigateByUrl').and.stub(); const route = { url: 'some-url;q=123' } as RouterStateSnapshot; - authGuard.canActivate(null, route); + await runAuthGuardWithContext(route); expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({ provider: 'ECM', @@ -174,12 +188,12 @@ describe('AuthGuardService ECM', () => { expect(basicAlfrescoAuthService.getRedirect()).toEqual('some-url;q=123'); }); - it('should set redirect navigation commands with query params', () => { + it('should set redirect navigation commands with query params', async () => { spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough(); spyOn(router, 'navigateByUrl').and.stub(); const route = { url: '/' } as RouterStateSnapshot; - authGuard.canActivate(null, route); + await runAuthGuardWithContext(route); expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({ provider: 'ECM', @@ -188,13 +202,13 @@ describe('AuthGuardService ECM', () => { expect(basicAlfrescoAuthService.getRedirect()).toEqual('/'); }); - it('should get redirect url from config if there is one configured', () => { + it('should get redirect url from config if there is one configured', async () => { appConfigService.config.loginRoute = 'fakeLoginRoute'; spyOn(basicAlfrescoAuthService, 'setRedirect').and.callThrough(); spyOn(router, 'navigateByUrl').and.stub(); const route = { url: 'some-url' } as RouterStateSnapshot; - authGuard.canActivate(null, route); + await runAuthGuardWithContext(route); expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({ provider: 'ECM', @@ -203,7 +217,7 @@ describe('AuthGuardService ECM', () => { expect(router.navigateByUrl).toHaveBeenCalledWith(router.parseUrl('/fakeLoginRoute?redirectUrl=some-url')); }); - it('should to close the material dialog if is redirect to the login', () => { + it('should to close the material dialog if is redirect to the login', async () => { const materialDialog = TestBed.inject(MatDialog); spyOn(materialDialog, 'closeAll'); @@ -212,7 +226,7 @@ describe('AuthGuardService ECM', () => { spyOn(router, 'navigateByUrl').and.stub(); const route = { url: 'some-url' } as RouterStateSnapshot; - authGuard.canActivate(null, route); + await runAuthGuardWithContext(route); expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({ provider: 'ECM', diff --git a/lib/core/src/lib/auth/guard/auth-guard.service.spec.ts b/lib/core/src/lib/auth/guard/auth-guard.service.spec.ts index 0dadf36b860..63157ff1292 100644 --- a/lib/core/src/lib/auth/guard/auth-guard.service.spec.ts +++ b/lib/core/src/lib/auth/guard/auth-guard.service.spec.ts @@ -16,7 +16,7 @@ */ import { TestBed } from '@angular/core/testing'; -import { Router, RouterStateSnapshot } from '@angular/router'; +import { Router, RouterStateSnapshot, UrlTree } from '@angular/router'; import { AppConfigService } from '../../app-config/app-config.service'; import { AuthGuard } from './auth-guard.service'; import { AuthenticationService } from '../services/authentication.service'; @@ -34,7 +34,6 @@ describe('AuthGuardService', () => { let state; let authService: AuthenticationService; let router: Router; - let authGuard: AuthGuard; let storageService: StorageService; let appConfigService: AppConfigService; let basicAlfrescoAuthService: BasicAlfrescoAuthService; @@ -63,7 +62,6 @@ describe('AuthGuardService', () => { basicAlfrescoAuthService = TestBed.inject(BasicAlfrescoAuthService); oidcAuthenticationService = TestBed.inject(OidcAuthenticationService); router = TestBed.inject(Router); - authGuard = TestBed.inject(AuthGuard); appConfigService = TestBed.inject(AppConfigService); appConfigService.config.auth = {}; @@ -71,11 +69,27 @@ describe('AuthGuardService', () => { storageService = TestBed.inject(StorageService); }); + const runAuthGuardWithContext = async (routerState: RouterStateSnapshot): Promise => { + const result = TestBed.runInInjectionContext(() => AuthGuard(routerState)); + if (result instanceof Observable) { + return handleObservableResult(result); + } else { + return result; + } + }; + + const handleObservableResult = (result: Observable): Promise => + new Promise((resolve) => { + result.subscribe((value) => { + resolve(value); + }); + }); + it('if the alfresco js api is logged in should canActivate be true', async () => { spyOn(router, 'navigateByUrl'); spyOn(authService, 'isLoggedIn').and.returnValue(true); - expect(await authGuard.canActivate(null, state)).toBeTruthy(); + expect(await runAuthGuardWithContext(state)).toBeTruthy(); expect(router.navigateByUrl).not.toHaveBeenCalled(); }); @@ -84,7 +98,7 @@ describe('AuthGuardService', () => { spyOn(router, 'navigateByUrl'); spyOn(authService, 'isLoggedIn').and.returnValue(false); - expect(await authGuard.canActivate(null, state)).toBeFalsy(); + expect(await runAuthGuardWithContext(state)).toBeFalsy(); expect(router.navigateByUrl).toHaveBeenCalled(); }); @@ -94,7 +108,7 @@ describe('AuthGuardService', () => { const route = { url: 'some-url' } as RouterStateSnapshot; - expect(await authGuard.canActivate(null, route)).toBeTruthy(); + expect(await runAuthGuardWithContext(route)).toBeTruthy(); }); it('should not redirect to login', async () => { @@ -105,7 +119,7 @@ describe('AuthGuardService', () => { spyOn(authService, 'isOauth').and.returnValue(true); appConfigService.config.oauth2.silentLogin = false; - expect(await authGuard.canActivate(null, state)).toBeTruthy(); + expect(await runAuthGuardWithContext(state)).toBeTruthy(); expect(router.navigateByUrl).not.toHaveBeenCalled(); }); @@ -115,7 +129,7 @@ describe('AuthGuardService', () => { spyOn(authService, 'isOauth').and.returnValue(true); appConfigService.config.oauth2.silentLogin = false; - expect(await authGuard.canActivate(null, state)).toBeFalsy(); + expect(await runAuthGuardWithContext(state)).toBeFalsy(); expect(router.navigateByUrl).toHaveBeenCalled(); }); @@ -125,7 +139,7 @@ describe('AuthGuardService', () => { spyOn(authService, 'isOauth').and.returnValue(true); appConfigService.config.oauth2.silentLogin = undefined; - expect(await authGuard.canActivate(null, state)).toBeFalsy(); + expect(await runAuthGuardWithContext(state)).toBeFalsy(); expect(router.navigateByUrl).toHaveBeenCalled(); }); @@ -135,7 +149,7 @@ describe('AuthGuardService', () => { spyOn(authService, 'isOauth').and.returnValue(true); appConfigService.config.oauth2.silentLogin = true; - expect(await authGuard.canActivate(null, state)).toBeFalsy(); + expect(await runAuthGuardWithContext(state)).toBeFalsy(); expect(oidcAuthenticationService.ssoLogin).toHaveBeenCalledTimes(1); }); @@ -146,7 +160,7 @@ describe('AuthGuardService', () => { spyOn(router, 'navigateByUrl'); spyOn(basicAlfrescoAuthService, 'setRedirect'); - await authGuard.canActivate(null, state); + await runAuthGuardWithContext(state); expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({ provider: 'ALL', @@ -163,7 +177,7 @@ describe('AuthGuardService', () => { spyOn(router, 'navigateByUrl'); spyOn(basicAlfrescoAuthService, 'setRedirect'); - await authGuard.canActivate(null, state); + await runAuthGuardWithContext(state); expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({ provider: 'ALL', @@ -179,7 +193,7 @@ describe('AuthGuardService', () => { spyOn(router, 'navigateByUrl'); spyOn(basicAlfrescoAuthService, 'setRedirect'); - await authGuard.canActivate(null, state); + await runAuthGuardWithContext(state); expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({ provider: 'ALL', @@ -194,7 +208,7 @@ describe('AuthGuardService', () => { spyOn(router, 'navigateByUrl'); spyOn(basicAlfrescoAuthService, 'setRedirect'); - await authGuard.canActivate(null, state); + await runAuthGuardWithContext(state); expect(basicAlfrescoAuthService.setRedirect).toHaveBeenCalledWith({ provider: 'ALL',