diff --git a/src/app/core/data/feature-authorization/feature-authorization-guard/single-feature-authorization.guard.spec.ts b/src/app/core/data/feature-authorization/feature-authorization-guard/single-feature-authorization.guard.spec.ts index 92d1118ea9c..fc631ee9acb 100644 --- a/src/app/core/data/feature-authorization/feature-authorization-guard/single-feature-authorization.guard.spec.ts +++ b/src/app/core/data/feature-authorization/feature-authorization-guard/single-feature-authorization.guard.spec.ts @@ -1,12 +1,13 @@ import { AuthorizationDataService } from '../authorization-data.service'; import { FeatureID } from '../feature-id'; -import { of as observableOf } from 'rxjs'; -import { Router } from '@angular/router'; +import { of as observableOf, Observable } from 'rxjs'; +import { Router, UrlTree } from '@angular/router'; import { AuthService } from '../../../auth/auth.service'; import { singleFeatureAuthorizationGuard } from './single-feature-authorization.guard'; +import { waitForAsync, TestBed } from '@angular/core/testing'; -describe('SingleFeatureAuthorizationGuard', () => { +describe('singleFeatureAuthorizationGuard', () => { let guard: any; let authorizationService: AuthorizationDataService; let router: Router; @@ -31,21 +32,34 @@ describe('SingleFeatureAuthorizationGuard', () => { isAuthenticated: observableOf(true), }); - guard = singleFeatureAuthorizationGuard; + TestBed.configureTestingModule({ + providers: [ + { provide: AuthorizationDataService, useValue: authorizationService }, + { provide: Router, useValue: router }, + { provide: AuthService, useValue: authService }, + ] + }); } - beforeEach(() => { + beforeEach(waitForAsync(() => { init(); - }); + })); - describe('canActivate', () => { - it('should call authorizationService.isAuthorized with the appropriate arguments', (done) => { - guard(observableOf(featureId), observableOf(objectUrl), observableOf(ePersonUuid))(undefined, { url: 'current-url' } as any).subscribe(() => { - expect(authorizationService.isAuthorized).toHaveBeenCalledWith(featureId, objectUrl, ePersonUuid); - done(); - }); + it('should call authorizationService.isAuthenticated with the appropriate arguments', (done: DoneFn) => { + const result$ = TestBed.runInInjectionContext(() => { + return singleFeatureAuthorizationGuard( + () => observableOf(featureId), + () => observableOf(objectUrl), + () => observableOf(ePersonUuid), + )(undefined, { url: 'current-url' } as any) + }) as Observable; - }, 10000); + + result$.subscribe(() => { + expect(authorizationService.isAuthorized).toHaveBeenCalledWith(featureId, objectUrl, ePersonUuid); + done(); + }) }); + }); diff --git a/src/app/core/data/feature-authorization/feature-authorization-guard/some-feature-authorization.guard.spec.ts b/src/app/core/data/feature-authorization/feature-authorization-guard/some-feature-authorization.guard.spec.ts index a3578d3eda8..9239eb998d3 100644 --- a/src/app/core/data/feature-authorization/feature-authorization-guard/some-feature-authorization.guard.spec.ts +++ b/src/app/core/data/feature-authorization/feature-authorization-guard/some-feature-authorization.guard.spec.ts @@ -1,110 +1,112 @@ -// import { AuthorizationDataService } from '../authorization-data.service'; -// import { FeatureID } from '../feature-id'; -// import { Observable, of as observableOf } from 'rxjs'; -// import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router'; -// import { AuthService } from '../../../auth/auth.service'; -// import { SomeFeatureAuthorizationGuard } from './some-feature-authorization.guard'; -// -// /** -// * Test implementation of abstract class SomeFeatureAuthorizationGuard -// * Provide the return values of the overwritten getters as constructor arguments -// */ -// class SomeFeatureAuthorizationGuardImpl extends SomeFeatureAuthorizationGuard { -// constructor(protected authorizationService: AuthorizationDataService, -// protected router: Router, -// protected authService: AuthService, -// protected featureIds: FeatureID[], -// protected objectUrl: string, -// protected ePersonUuid: string) { -// super(authorizationService, router, authService); -// } -// -// getFeatureIDs(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { -// return observableOf(this.featureIds); -// } -// -// getObjectUrl(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { -// return observableOf(this.objectUrl); -// } -// -// getEPersonUuid(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { -// return observableOf(this.ePersonUuid); -// } -// } -// -// describe('SomeFeatureAuthorizationGuard', () => { -// let guard: SomeFeatureAuthorizationGuard; -// let authorizationService: AuthorizationDataService; -// let router: Router; -// let authService: AuthService; -// -// let featureIds: FeatureID[]; -// let authorizedFeatureIds: FeatureID[]; -// let objectUrl: string; -// let ePersonUuid: string; -// -// function init() { -// featureIds = [FeatureID.LoginOnBehalfOf, FeatureID.CanDelete]; -// authorizedFeatureIds = []; -// objectUrl = 'fake-object-url'; -// ePersonUuid = 'fake-eperson-uuid'; -// -// authorizationService = Object.assign({ -// isAuthorized(featureId?: FeatureID): Observable { -// return observableOf(authorizedFeatureIds.indexOf(featureId) > -1); -// } -// }); -// router = jasmine.createSpyObj('router', { -// parseUrl: {} -// }); -// authService = jasmine.createSpyObj('authService', { -// isAuthenticated: observableOf(true) -// }); -// guard = new SomeFeatureAuthorizationGuardImpl(authorizationService, router, authService, featureIds, objectUrl, ePersonUuid); -// } -// -// beforeEach(() => { -// init(); -// }); -// -// describe('canActivate', () => { -// describe('when the user isn\'t authorized', () => { -// beforeEach(() => { -// authorizedFeatureIds = []; -// }); -// -// it('should not return true', (done) => { -// guard.canActivate(undefined, { url: 'current-url' } as any).subscribe((result) => { -// expect(result).not.toEqual(true); -// done(); -// }); -// }); -// }); -// -// describe('when the user is authorized for at least one of the guard\'s features', () => { -// beforeEach(() => { -// authorizedFeatureIds = [featureIds[0]]; -// }); -// -// it('should return true', (done) => { -// guard.canActivate(undefined, { url: 'current-url' } as any).subscribe((result) => { -// expect(result).toEqual(true); -// done(); -// }); -// }); -// }); -// -// describe('when the user is authorized for all of the guard\'s features', () => { -// beforeEach(() => { -// authorizedFeatureIds = featureIds; -// }); -// -// it('should return true', (done) => { -// guard.canActivate(undefined, { url: 'current-url' } as any).subscribe((result) => { -// expect(result).toEqual(true); -// done(); -// }); -// }); -// }); -// }); -// }); +import { AuthorizationDataService } from '../authorization-data.service'; +import { FeatureID } from '../feature-id'; +import { Observable, of as observableOf } from 'rxjs'; +import { Router, UrlTree } from '@angular/router'; +import { AuthService } from '../../../auth/auth.service'; +import { waitForAsync, TestBed } from '@angular/core/testing'; +import { someFeatureAuthorizationGuard } from './some-feature-authorization.guard'; + +describe('someFeatureAuthorizationGuard', () => { + let authorizationService: AuthorizationDataService; + let router: Router; + let authService: AuthService; + + let featureIds: FeatureID[]; + let authorizedFeatureIds: FeatureID[]; + let objectUrl: string; + let ePersonUuid: string; + + function init() { + featureIds = [FeatureID.LoginOnBehalfOf, FeatureID.CanDelete]; + authorizedFeatureIds = []; + objectUrl = 'fake-object-url'; + ePersonUuid = 'fake-eperson-uuid'; + + authorizationService = Object.assign({ + isAuthorized(featureId?: FeatureID): Observable { + return observableOf(authorizedFeatureIds.indexOf(featureId) > -1); + } + }); + router = jasmine.createSpyObj('router', { + parseUrl: {} + }); + authService = jasmine.createSpyObj('authService', { + isAuthenticated: observableOf(true) + }); + + TestBed.configureTestingModule({ + providers: [ + { provide: AuthorizationDataService, useValue: authorizationService }, + { provide: Router, useValue: router }, + { provide: AuthService, useValue: authService }, + ] + }); + } + + beforeEach(waitForAsync(() => { + init(); + })); + + describe('when the user isn\'t authorized', () => { + beforeEach(() => { + authorizedFeatureIds = []; + }); + + it('should not return true', (done: DoneFn) => { + const result$ = TestBed.runInInjectionContext(() => { + return someFeatureAuthorizationGuard( + () => observableOf(featureIds), + () => observableOf(objectUrl), + () => observableOf(ePersonUuid), + )(undefined, { url: 'current-url' } as any) + }) as Observable; + + result$.subscribe((result) => { + expect(result).not.toEqual(true); + done(); + }); + }); + }); + + describe('when the user is authorized for at least one of the guard\'s features', () => { + beforeEach(() => { + authorizedFeatureIds = [featureIds[0]]; + }); + + it('should return true', (done) => { + const result$ = TestBed.runInInjectionContext(() => { + return someFeatureAuthorizationGuard( + () => observableOf(featureIds), + () => observableOf(objectUrl), + () => observableOf(ePersonUuid), + )(undefined, { url: 'current-url' } as any) + }) as Observable; + + result$.subscribe((result) => { + expect(result).toEqual(true); + done(); + }); + }); + }); + + describe('when the user is authorized for all of the guard\'s features', () => { + beforeEach(() => { + authorizedFeatureIds = featureIds; + }); + + it('should return true', (done) => { + const result$ = TestBed.runInInjectionContext(() => { + return someFeatureAuthorizationGuard( + () => observableOf(featureIds), + () => observableOf(objectUrl), + () => observableOf(ePersonUuid), + )(undefined, { url: 'current-url' } as any) + }) as Observable; + + result$.subscribe((result) => { + expect(result).toEqual(true); + done(); + }); + }); + }); +});