forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
139 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
222 changes: 112 additions & 110 deletions
222
...eature-authorization/feature-authorization-guard/some-feature-authorization.guard.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<FeatureID[]> { | ||
// return observableOf(this.featureIds); | ||
// } | ||
// | ||
// getObjectUrl(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<string> { | ||
// return observableOf(this.objectUrl); | ||
// } | ||
// | ||
// getEPersonUuid(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<string> { | ||
// 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<boolean> { | ||
// 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<boolean> { | ||
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) | ||
Check failure on line 61 in src/app/core/data/feature-authorization/feature-authorization-guard/some-feature-authorization.guard.spec.ts GitHub Actions / tests (16.x)
|
||
}) as Observable<boolean | UrlTree>; | ||
|
||
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) | ||
Check failure on line 82 in src/app/core/data/feature-authorization/feature-authorization-guard/some-feature-authorization.guard.spec.ts GitHub Actions / tests (16.x)
|
||
}) as Observable<boolean | UrlTree>; | ||
|
||
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) | ||
Check failure on line 103 in src/app/core/data/feature-authorization/feature-authorization-guard/some-feature-authorization.guard.spec.ts GitHub Actions / tests (16.x)
|
||
}) as Observable<boolean | UrlTree>; | ||
|
||
result$.subscribe((result) => { | ||
expect(result).toEqual(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |