mirror of
https://github.com/Alfresco/alfresco-content-app.git
synced 2025-07-24 17:31:52 +00:00
[ACS-8726] Use functional route guards (#4096)
* [ACS-8726] Use functional route guards * [ACS-8726] reduce duplication
This commit is contained in:
committed by
GitHub
parent
49cd06d2f9
commit
3a2d870db1
@@ -22,22 +22,16 @@
|
|||||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Injectable } from '@angular/core';
|
import { inject } from '@angular/core';
|
||||||
import { CanActivate } from '@angular/router';
|
import { CanActivateFn } from '@angular/router';
|
||||||
import { Observable } from 'rxjs';
|
|
||||||
import { AuthenticationService } from '@alfresco/adf-core';
|
import { AuthenticationService } from '@alfresco/adf-core';
|
||||||
|
|
||||||
@Injectable({
|
export const ViewProfileRuleGuard: CanActivateFn = () => {
|
||||||
providedIn: 'root'
|
const authService = inject(AuthenticationService);
|
||||||
})
|
|
||||||
export class ViewProfileRuleGuard implements CanActivate {
|
|
||||||
constructor(private authService: AuthenticationService) {}
|
|
||||||
|
|
||||||
canActivate(): Observable<boolean> | Promise<boolean> | boolean {
|
const isEcmLoggedIn = (): boolean => {
|
||||||
return this.isEcmLoggedIn() || this.authService.isOauth();
|
return authService.isEcmLoggedIn() || (authService.isECMProvider() && authService.isKerberosEnabled());
|
||||||
}
|
};
|
||||||
|
|
||||||
private isEcmLoggedIn(): boolean {
|
return isEcmLoggedIn() || authService.isOauth();
|
||||||
return this.authService.isEcmLoggedIn() || (this.authService.isECMProvider() && this.authService.isKerberosEnabled());
|
};
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@@ -22,9 +22,10 @@
|
|||||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { ExtensionsDataLoaderGuard } from './extensions-data-loader.guard';
|
import { EXTENSION_DATA_LOADERS, ExtensionsDataLoaderGuard, resetInvoked } from './extensions-data-loader.guard';
|
||||||
import { ActivatedRouteSnapshot } from '@angular/router';
|
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
|
||||||
import { Subject, throwError } from 'rxjs';
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
import { Observable, Subject, throwError } from 'rxjs';
|
||||||
|
|
||||||
describe('ExtensionsDataLoaderGuard', () => {
|
describe('ExtensionsDataLoaderGuard', () => {
|
||||||
let route: ActivatedRouteSnapshot;
|
let route: ActivatedRouteSnapshot;
|
||||||
@@ -32,18 +33,22 @@ describe('ExtensionsDataLoaderGuard', () => {
|
|||||||
let completedSpy;
|
let completedSpy;
|
||||||
let erroredSpy;
|
let erroredSpy;
|
||||||
|
|
||||||
describe('canActivate', () => {
|
const setupTest = (extensionDataLoaders: any[]) => {
|
||||||
|
TestBed.overrideProvider(EXTENSION_DATA_LOADERS, { useValue: extensionDataLoaders });
|
||||||
|
return TestBed.runInInjectionContext(() => ExtensionsDataLoaderGuard(route, {} as RouterStateSnapshot)) as Observable<boolean>;
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
route = {} as ActivatedRouteSnapshot;
|
route = {} as ActivatedRouteSnapshot;
|
||||||
emittedSpy = jasmine.createSpy('emitted');
|
emittedSpy = jasmine.createSpy('emitted');
|
||||||
completedSpy = jasmine.createSpy('completed');
|
completedSpy = jasmine.createSpy('completed');
|
||||||
erroredSpy = jasmine.createSpy('errored');
|
erroredSpy = jasmine.createSpy('errored');
|
||||||
|
resetInvoked();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should emit true and complete if no callback are present', () => {
|
it('should emit true and complete if no callback are present', () => {
|
||||||
const guard = new ExtensionsDataLoaderGuard([]);
|
const guard = setupTest([]);
|
||||||
|
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
|
||||||
expect(emittedSpy).toHaveBeenCalledWith(true);
|
expect(emittedSpy).toHaveBeenCalledWith(true);
|
||||||
expect(erroredSpy).not.toHaveBeenCalled();
|
expect(erroredSpy).not.toHaveBeenCalled();
|
||||||
expect(completedSpy).toHaveBeenCalled();
|
expect(completedSpy).toHaveBeenCalled();
|
||||||
@@ -51,10 +56,8 @@ describe('ExtensionsDataLoaderGuard', () => {
|
|||||||
|
|
||||||
it('should emit true and complete in case of only one callback is present, completed', () => {
|
it('should emit true and complete in case of only one callback is present, completed', () => {
|
||||||
const subject = new Subject<true>();
|
const subject = new Subject<true>();
|
||||||
const guard = new ExtensionsDataLoaderGuard([() => subject.asObservable()]);
|
const guard = setupTest([() => subject.asObservable()]);
|
||||||
|
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
|
||||||
|
|
||||||
subject.next(true);
|
subject.next(true);
|
||||||
expect(emittedSpy).not.toHaveBeenCalled();
|
expect(emittedSpy).not.toHaveBeenCalled();
|
||||||
expect(erroredSpy).not.toHaveBeenCalled();
|
expect(erroredSpy).not.toHaveBeenCalled();
|
||||||
@@ -67,9 +70,9 @@ describe('ExtensionsDataLoaderGuard', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should emit true and complete in case of only one callback is present, errored', () => {
|
it('should emit true and complete in case of only one callback is present, errored', () => {
|
||||||
const guard = new ExtensionsDataLoaderGuard([() => throwError(new Error())]);
|
const guard = setupTest([() => throwError(new Error())]);
|
||||||
|
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||||
|
|
||||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
|
||||||
expect(emittedSpy).toHaveBeenCalledWith(true);
|
expect(emittedSpy).toHaveBeenCalledWith(true);
|
||||||
expect(erroredSpy).not.toHaveBeenCalled();
|
expect(erroredSpy).not.toHaveBeenCalled();
|
||||||
expect(completedSpy).toHaveBeenCalled();
|
expect(completedSpy).toHaveBeenCalled();
|
||||||
@@ -78,13 +81,13 @@ describe('ExtensionsDataLoaderGuard', () => {
|
|||||||
it('should NOT complete in case of multiple callbacks are present and not all of them have been completed', () => {
|
it('should NOT complete in case of multiple callbacks are present and not all of them have been completed', () => {
|
||||||
const subject1 = new Subject<true>();
|
const subject1 = new Subject<true>();
|
||||||
const subject2 = new Subject<true>();
|
const subject2 = new Subject<true>();
|
||||||
const guard = new ExtensionsDataLoaderGuard([() => subject1.asObservable(), () => subject2.asObservable()]);
|
const guard = setupTest([() => subject1.asObservable(), () => subject2.asObservable()]);
|
||||||
|
|
||||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
|
||||||
|
|
||||||
|
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||||
subject1.next();
|
subject1.next();
|
||||||
subject2.next();
|
subject2.next();
|
||||||
subject1.complete();
|
subject1.complete();
|
||||||
|
|
||||||
expect(emittedSpy).not.toHaveBeenCalled();
|
expect(emittedSpy).not.toHaveBeenCalled();
|
||||||
expect(erroredSpy).not.toHaveBeenCalled();
|
expect(erroredSpy).not.toHaveBeenCalled();
|
||||||
expect(completedSpy).not.toHaveBeenCalled();
|
expect(completedSpy).not.toHaveBeenCalled();
|
||||||
@@ -93,14 +96,14 @@ describe('ExtensionsDataLoaderGuard', () => {
|
|||||||
it('should emit true and complete in case of multiple callbacks are present and all of them have been completed', () => {
|
it('should emit true and complete in case of multiple callbacks are present and all of them have been completed', () => {
|
||||||
const subject1 = new Subject<true>();
|
const subject1 = new Subject<true>();
|
||||||
const subject2 = new Subject<true>();
|
const subject2 = new Subject<true>();
|
||||||
const guard = new ExtensionsDataLoaderGuard([() => subject1.asObservable(), () => subject2.asObservable()]);
|
const guard = setupTest([() => subject1.asObservable(), () => subject2.asObservable()]);
|
||||||
|
|
||||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
|
||||||
|
|
||||||
|
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||||
subject1.next();
|
subject1.next();
|
||||||
subject2.next();
|
subject2.next();
|
||||||
subject1.complete();
|
subject1.complete();
|
||||||
subject2.complete();
|
subject2.complete();
|
||||||
|
|
||||||
expect(emittedSpy).toHaveBeenCalledWith(true);
|
expect(emittedSpy).toHaveBeenCalledWith(true);
|
||||||
expect(erroredSpy).not.toHaveBeenCalled();
|
expect(erroredSpy).not.toHaveBeenCalled();
|
||||||
expect(completedSpy).toHaveBeenCalled();
|
expect(completedSpy).toHaveBeenCalled();
|
||||||
@@ -108,11 +111,11 @@ describe('ExtensionsDataLoaderGuard', () => {
|
|||||||
|
|
||||||
it('should emit true and complete even if one of the observables are errored, to not block the application loading', () => {
|
it('should emit true and complete even if one of the observables are errored, to not block the application loading', () => {
|
||||||
const subject1 = new Subject<true>();
|
const subject1 = new Subject<true>();
|
||||||
const guard = new ExtensionsDataLoaderGuard([() => subject1.asObservable(), () => throwError(new Error())]);
|
const guard = setupTest([() => subject1.asObservable(), () => throwError(new Error())]);
|
||||||
|
|
||||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
|
||||||
|
|
||||||
|
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||||
subject1.next();
|
subject1.next();
|
||||||
|
|
||||||
expect(emittedSpy).toHaveBeenCalledWith(true);
|
expect(emittedSpy).toHaveBeenCalledWith(true);
|
||||||
expect(erroredSpy).not.toHaveBeenCalled();
|
expect(erroredSpy).not.toHaveBeenCalled();
|
||||||
expect(completedSpy).toHaveBeenCalled();
|
expect(completedSpy).toHaveBeenCalled();
|
||||||
@@ -124,16 +127,16 @@ describe('ExtensionsDataLoaderGuard', () => {
|
|||||||
fct1: () => subject1.asObservable()
|
fct1: () => subject1.asObservable()
|
||||||
};
|
};
|
||||||
const extensionLoaderSpy = spyOn(extensionLoaders, 'fct1').and.callThrough();
|
const extensionLoaderSpy = spyOn(extensionLoaders, 'fct1').and.callThrough();
|
||||||
const guard = new ExtensionsDataLoaderGuard([extensionLoaders.fct1]);
|
const guard = setupTest([extensionLoaders.fct1]);
|
||||||
|
|
||||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||||
expect(extensionLoaderSpy).toHaveBeenCalled();
|
expect(extensionLoaderSpy).toHaveBeenCalled();
|
||||||
|
|
||||||
extensionLoaderSpy.calls.reset();
|
extensionLoaderSpy.calls.reset();
|
||||||
subject1.next(true);
|
subject1.next(true);
|
||||||
subject1.complete();
|
subject1.complete();
|
||||||
guard.canActivate(route).subscribe(emittedSpy, erroredSpy, completedSpy);
|
|
||||||
|
guard.subscribe(emittedSpy, erroredSpy, completedSpy);
|
||||||
expect(extensionLoaderSpy).not.toHaveBeenCalled();
|
expect(extensionLoaderSpy).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
@@ -22,8 +22,8 @@
|
|||||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Injectable, InjectionToken, Inject } from '@angular/core';
|
import { InjectionToken, inject } from '@angular/core';
|
||||||
import { CanActivate, ActivatedRouteSnapshot } from '@angular/router';
|
import { ActivatedRouteSnapshot, CanActivateFn } from '@angular/router';
|
||||||
import { Observable, forkJoin, of } from 'rxjs';
|
import { Observable, forkJoin, of } from 'rxjs';
|
||||||
import { tap, map, catchError } from 'rxjs/operators';
|
import { tap, map, catchError } from 'rxjs/operators';
|
||||||
|
|
||||||
@@ -36,30 +36,24 @@ export const EXTENSION_DATA_LOADERS = new InjectionToken<ExtensionLoaderCallback
|
|||||||
factory: DefaultExtensionLoaderFactory
|
factory: DefaultExtensionLoaderFactory
|
||||||
});
|
});
|
||||||
|
|
||||||
@Injectable({ providedIn: 'root' })
|
let invoked = false;
|
||||||
export class ExtensionsDataLoaderGuard implements CanActivate {
|
|
||||||
private invoked = false;
|
|
||||||
|
|
||||||
constructor(
|
export const ExtensionsDataLoaderGuard: CanActivateFn = (route: ActivatedRouteSnapshot): Observable<boolean> => {
|
||||||
@Inject(EXTENSION_DATA_LOADERS)
|
const extensionDataLoaders = inject(EXTENSION_DATA_LOADERS);
|
||||||
private extensionDataLoaders: ExtensionLoaderCallback[]
|
if (!invoked) {
|
||||||
) {}
|
if (!extensionDataLoaders.length) {
|
||||||
|
invoked = true;
|
||||||
canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
|
|
||||||
if (!this.invoked) {
|
|
||||||
if (!this.extensionDataLoaders.length) {
|
|
||||||
this.invoked = true;
|
|
||||||
return of(true);
|
return of(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
const dataLoaderCallbacks = this.extensionDataLoaders.map((callback) => callback(route));
|
const dataLoaderCallbacks = extensionDataLoaders.map((callback) => callback(route));
|
||||||
|
|
||||||
// Undocumented forkJoin behaviour/bug:
|
// Undocumented forkJoin behaviour/bug:
|
||||||
// https://github.com/ReactiveX/rxjs/issues/3246
|
// https://github.com/ReactiveX/rxjs/issues/3246
|
||||||
// So all callbacks need to emit before completion, otherwise forkJoin will short circuit
|
// So all callbacks need to emit before completion, otherwise forkJoin will short circuit
|
||||||
return forkJoin(...dataLoaderCallbacks).pipe(
|
return forkJoin(...dataLoaderCallbacks).pipe(
|
||||||
map(() => true),
|
map(() => true),
|
||||||
tap(() => (this.invoked = true)),
|
tap(() => (invoked = true)),
|
||||||
catchError((e) => {
|
catchError((e) => {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.error('Some of the extension data loader guards has been errored.');
|
console.error('Some of the extension data loader guards has been errored.');
|
||||||
@@ -71,9 +65,8 @@ export class ExtensionsDataLoaderGuard implements CanActivate {
|
|||||||
} else {
|
} else {
|
||||||
return of(true);
|
return of(true);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
canActivateChild(): Observable<boolean> {
|
export const resetInvoked = () => {
|
||||||
return of(true);
|
invoked = false;
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
@@ -25,11 +25,10 @@
|
|||||||
import { AppConfigService } from '@alfresco/adf-core';
|
import { AppConfigService } from '@alfresco/adf-core';
|
||||||
import { TestBed } from '@angular/core/testing';
|
import { TestBed } from '@angular/core/testing';
|
||||||
import { PluginEnabledGuard } from './plugin-enabled.guard';
|
import { PluginEnabledGuard } from './plugin-enabled.guard';
|
||||||
import { ActivatedRouteSnapshot, Router } from '@angular/router';
|
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
|
||||||
import { HttpClientTestingModule } from '@angular/common/http/testing';
|
import { HttpClientTestingModule } from '@angular/common/http/testing';
|
||||||
|
|
||||||
describe('PluginEnabledGuard', () => {
|
describe('PluginEnabledGuard', () => {
|
||||||
let service: PluginEnabledGuard;
|
|
||||||
let getSpy: jasmine.Spy<(key: string, defaultValue?: boolean) => boolean>;
|
let getSpy: jasmine.Spy<(key: string, defaultValue?: boolean) => boolean>;
|
||||||
let route: ActivatedRouteSnapshot;
|
let route: ActivatedRouteSnapshot;
|
||||||
|
|
||||||
@@ -37,7 +36,6 @@ describe('PluginEnabledGuard', () => {
|
|||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
imports: [HttpClientTestingModule]
|
imports: [HttpClientTestingModule]
|
||||||
});
|
});
|
||||||
service = TestBed.inject(PluginEnabledGuard);
|
|
||||||
getSpy = spyOn(TestBed.inject(AppConfigService), 'get');
|
getSpy = spyOn(TestBed.inject(AppConfigService), 'get');
|
||||||
route = new ActivatedRouteSnapshot();
|
route = new ActivatedRouteSnapshot();
|
||||||
route.data = {
|
route.data = {
|
||||||
@@ -45,41 +43,31 @@ describe('PluginEnabledGuard', () => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('canActivate', () => {
|
|
||||||
it('should call appConfigService.get with correct parameters', () => {
|
it('should call appConfigService.get with correct parameters', () => {
|
||||||
service.canActivate(route);
|
TestBed.runInInjectionContext(() => PluginEnabledGuard(route, {} as RouterStateSnapshot));
|
||||||
expect(getSpy).toHaveBeenCalledWith(route.data.plugin, true);
|
expect(getSpy).toHaveBeenCalledWith(route.data.plugin, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return true if appConfigService.get returns true', () => {
|
it('should return true if appConfigService.get returns true', () => {
|
||||||
getSpy.and.returnValue(true);
|
getSpy.and.returnValue(true);
|
||||||
|
const result = TestBed.runInInjectionContext(() => PluginEnabledGuard(route, {} as RouterStateSnapshot));
|
||||||
|
|
||||||
expect(service.canActivate(route)).toBeTrue();
|
expect(result).toBeTrue();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return false if appConfigService.get returns false', () => {
|
it('should return false if appConfigService.get returns false', () => {
|
||||||
getSpy.and.returnValue(true);
|
getSpy.and.returnValue(false);
|
||||||
|
const result = TestBed.runInInjectionContext(() => PluginEnabledGuard(route, {} as RouterStateSnapshot));
|
||||||
|
|
||||||
expect(service.canActivate(route)).toBeTrue();
|
expect(result).toBeFalse();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should navigate to root if plugin is not enabled', () => {
|
it('should navigate to root if plugin is not enabled', () => {
|
||||||
getSpy.and.returnValue(false);
|
getSpy.and.returnValue(false);
|
||||||
const routerSpy = spyOn(TestBed.inject(Router), 'navigate');
|
const routerSpy = spyOn(TestBed.inject(Router), 'navigate');
|
||||||
|
|
||||||
service.canActivate(route);
|
TestBed.runInInjectionContext(() => PluginEnabledGuard(route, {} as RouterStateSnapshot));
|
||||||
|
|
||||||
expect(routerSpy).toHaveBeenCalledWith(['/']);
|
expect(routerSpy).toHaveBeenCalledWith(['/']);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
describe('canActivateChild', () => {
|
|
||||||
it('should call canActivate with the same route and return its result', () => {
|
|
||||||
spyOn(service, 'canActivate').and.callThrough();
|
|
||||||
const result = service.canActivateChild(route);
|
|
||||||
|
|
||||||
expect(service.canActivate).toHaveBeenCalledWith(route);
|
|
||||||
expect(result).toBe(service.canActivate(route));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
@@ -22,27 +22,19 @@
|
|||||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, Router } from '@angular/router';
|
import { ActivatedRouteSnapshot, Router, CanActivateFn } from '@angular/router';
|
||||||
|
import { inject } from '@angular/core';
|
||||||
import { AppConfigService } from '@alfresco/adf-core';
|
import { AppConfigService } from '@alfresco/adf-core';
|
||||||
import { Injectable } from '@angular/core';
|
|
||||||
|
|
||||||
@Injectable({
|
export const PluginEnabledGuard: CanActivateFn = (route: ActivatedRouteSnapshot) => {
|
||||||
providedIn: 'root'
|
const appConfigService = inject(AppConfigService);
|
||||||
})
|
const router = inject(Router);
|
||||||
export class PluginEnabledGuard implements CanActivate, CanActivateChild {
|
|
||||||
constructor(private appConfigService: AppConfigService, private router: Router) {}
|
|
||||||
|
|
||||||
canActivate(route: ActivatedRouteSnapshot): boolean {
|
const isPluginEnabled = appConfigService.get(route.data.plugin, true);
|
||||||
const isPluginEnabled = this.appConfigService.get(route.data.plugin, true);
|
|
||||||
|
|
||||||
if (!isPluginEnabled) {
|
if (!isPluginEnabled) {
|
||||||
this.router.navigate(['/']);
|
router.navigate(['/']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return isPluginEnabled;
|
return isPluginEnabled;
|
||||||
}
|
};
|
||||||
|
|
||||||
canActivateChild(route: ActivatedRouteSnapshot): boolean {
|
|
||||||
return this.canActivate(route);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@@ -22,29 +22,44 @@
|
|||||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { of } from 'rxjs';
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
|
||||||
|
import { Store } from '@ngrx/store';
|
||||||
|
import { Observable, of } from 'rxjs';
|
||||||
|
import { isQuickShareEnabled } from '@alfresco/aca-shared/store';
|
||||||
import { AppSharedRuleGuard } from './shared.guard';
|
import { AppSharedRuleGuard } from './shared.guard';
|
||||||
|
|
||||||
describe('AppSharedRuleGuard', () => {
|
describe('AppSharedRuleGuard', () => {
|
||||||
it('should allow activation if quick share is enabled', () => {
|
let state: RouterStateSnapshot;
|
||||||
const store: any = {
|
const route: ActivatedRouteSnapshot = new ActivatedRouteSnapshot();
|
||||||
select: () => of(true)
|
const storeSpy = jasmine.createSpyObj('Store', ['select']);
|
||||||
};
|
beforeEach(() => {
|
||||||
const guard = new AppSharedRuleGuard(store);
|
TestBed.configureTestingModule({
|
||||||
const emittedSpy = jasmine.createSpy('emitted');
|
providers: [{ provide: Store, useValue: storeSpy }]
|
||||||
|
|
||||||
guard.canActivate().subscribe(emittedSpy);
|
|
||||||
expect(emittedSpy).toHaveBeenCalledWith(true);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should allow child activation if quick share is enabled', () => {
|
state = { url: 'some-url' } as RouterStateSnapshot;
|
||||||
const store: any = {
|
});
|
||||||
select: () => of(true)
|
|
||||||
};
|
|
||||||
const guard = new AppSharedRuleGuard(store);
|
|
||||||
const emittedSpy = jasmine.createSpy('emitted');
|
|
||||||
|
|
||||||
guard.canActivateChild().subscribe(emittedSpy);
|
it('should allow activation if quick share is enabled', (done) => {
|
||||||
expect(emittedSpy).toHaveBeenCalledWith(true);
|
storeSpy.select.and.returnValue(of(true));
|
||||||
|
const guard = TestBed.runInInjectionContext(() => AppSharedRuleGuard(route, state)) as Observable<boolean>;
|
||||||
|
|
||||||
|
guard.subscribe((response) => {
|
||||||
|
expect(storeSpy.select).toHaveBeenCalledWith(isQuickShareEnabled);
|
||||||
|
expect(response).toBeTrue();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not allow activation if quick share is disabled', (done) => {
|
||||||
|
storeSpy.select.and.returnValue(of(false));
|
||||||
|
const guard = TestBed.runInInjectionContext(() => AppSharedRuleGuard(route, state)) as Observable<boolean>;
|
||||||
|
|
||||||
|
guard.subscribe((response) => {
|
||||||
|
expect(storeSpy.select).toHaveBeenCalledWith(isQuickShareEnabled);
|
||||||
|
expect(response).toBeFalse();
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -22,27 +22,13 @@
|
|||||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Injectable } from '@angular/core';
|
import { CanActivateFn } from '@angular/router';
|
||||||
import { CanActivate } from '@angular/router';
|
import { inject } from '@angular/core';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
import { Store } from '@ngrx/store';
|
import { Store } from '@ngrx/store';
|
||||||
import { AppStore, isQuickShareEnabled } from '@alfresco/aca-shared/store';
|
import { AppStore, isQuickShareEnabled } from '@alfresco/aca-shared/store';
|
||||||
|
|
||||||
@Injectable({
|
export const AppSharedRuleGuard: CanActivateFn = (): Observable<boolean> => {
|
||||||
providedIn: 'root'
|
const store = inject(Store<AppStore>);
|
||||||
})
|
return store.select(isQuickShareEnabled);
|
||||||
export class AppSharedRuleGuard implements CanActivate {
|
};
|
||||||
isQuickShareEnabled$: Observable<boolean>;
|
|
||||||
|
|
||||||
constructor(store: Store<AppStore>) {
|
|
||||||
this.isQuickShareEnabled$ = store.select(isQuickShareEnabled);
|
|
||||||
}
|
|
||||||
|
|
||||||
canActivate(): Observable<boolean> {
|
|
||||||
return this.isQuickShareEnabled$;
|
|
||||||
}
|
|
||||||
|
|
||||||
canActivateChild(): Observable<boolean> {
|
|
||||||
return this.canActivate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user