mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-08 10:34:15 +00:00
add tests
This commit is contained in:
@@ -1,12 +1,13 @@
|
|||||||
import { AuthorizationDataService } from '../authorization-data.service';
|
import { AuthorizationDataService } from '../authorization-data.service';
|
||||||
import { FeatureID } from '../feature-id';
|
import { FeatureID } from '../feature-id';
|
||||||
import { of as observableOf } from 'rxjs';
|
import { of as observableOf, Observable } from 'rxjs';
|
||||||
import { Router } from '@angular/router';
|
import { Router, UrlTree } from '@angular/router';
|
||||||
import { AuthService } from '../../../auth/auth.service';
|
import { AuthService } from '../../../auth/auth.service';
|
||||||
import { singleFeatureAuthorizationGuard } from './single-feature-authorization.guard';
|
import { singleFeatureAuthorizationGuard } from './single-feature-authorization.guard';
|
||||||
|
import { waitForAsync, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
|
||||||
describe('SingleFeatureAuthorizationGuard', () => {
|
describe('singleFeatureAuthorizationGuard', () => {
|
||||||
let guard: any;
|
let guard: any;
|
||||||
let authorizationService: AuthorizationDataService;
|
let authorizationService: AuthorizationDataService;
|
||||||
let router: Router;
|
let router: Router;
|
||||||
@@ -31,21 +32,34 @@ describe('SingleFeatureAuthorizationGuard', () => {
|
|||||||
isAuthenticated: observableOf(true),
|
isAuthenticated: observableOf(true),
|
||||||
});
|
});
|
||||||
|
|
||||||
guard = singleFeatureAuthorizationGuard;
|
TestBed.configureTestingModule({
|
||||||
|
providers: [
|
||||||
|
{ provide: AuthorizationDataService, useValue: authorizationService },
|
||||||
|
{ provide: Router, useValue: router },
|
||||||
|
{ provide: AuthService, useValue: authService },
|
||||||
|
]
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(waitForAsync(() => {
|
||||||
init();
|
init();
|
||||||
|
}));
|
||||||
|
|
||||||
|
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<boolean | UrlTree>;
|
||||||
|
|
||||||
|
|
||||||
|
result$.subscribe(() => {
|
||||||
|
expect(authorizationService.isAuthorized).toHaveBeenCalledWith(featureId, objectUrl, ePersonUuid);
|
||||||
|
done();
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
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();
|
|
||||||
});
|
|
||||||
|
|
||||||
}, 10000);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -1,110 +1,112 @@
|
|||||||
// import { AuthorizationDataService } from '../authorization-data.service';
|
import { AuthorizationDataService } from '../authorization-data.service';
|
||||||
// import { FeatureID } from '../feature-id';
|
import { FeatureID } from '../feature-id';
|
||||||
// import { Observable, of as observableOf } from 'rxjs';
|
import { Observable, of as observableOf } from 'rxjs';
|
||||||
// import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
|
import { Router, UrlTree } from '@angular/router';
|
||||||
// import { AuthService } from '../../../auth/auth.service';
|
import { AuthService } from '../../../auth/auth.service';
|
||||||
// import { SomeFeatureAuthorizationGuard } from './some-feature-authorization.guard';
|
import { waitForAsync, TestBed } from '@angular/core/testing';
|
||||||
//
|
import { someFeatureAuthorizationGuard } from './some-feature-authorization.guard';
|
||||||
// /**
|
|
||||||
// * Test implementation of abstract class SomeFeatureAuthorizationGuard
|
describe('someFeatureAuthorizationGuard', () => {
|
||||||
// * Provide the return values of the overwritten getters as constructor arguments
|
let authorizationService: AuthorizationDataService;
|
||||||
// */
|
let router: Router;
|
||||||
// class SomeFeatureAuthorizationGuardImpl extends SomeFeatureAuthorizationGuard {
|
let authService: AuthService;
|
||||||
// constructor(protected authorizationService: AuthorizationDataService,
|
|
||||||
// protected router: Router,
|
let featureIds: FeatureID[];
|
||||||
// protected authService: AuthService,
|
let authorizedFeatureIds: FeatureID[];
|
||||||
// protected featureIds: FeatureID[],
|
let objectUrl: string;
|
||||||
// protected objectUrl: string,
|
let ePersonUuid: string;
|
||||||
// protected ePersonUuid: string) {
|
|
||||||
// super(authorizationService, router, authService);
|
function init() {
|
||||||
// }
|
featureIds = [FeatureID.LoginOnBehalfOf, FeatureID.CanDelete];
|
||||||
//
|
authorizedFeatureIds = [];
|
||||||
// getFeatureIDs(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<FeatureID[]> {
|
objectUrl = 'fake-object-url';
|
||||||
// return observableOf(this.featureIds);
|
ePersonUuid = 'fake-eperson-uuid';
|
||||||
// }
|
|
||||||
//
|
authorizationService = Object.assign({
|
||||||
// getObjectUrl(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<string> {
|
isAuthorized(featureId?: FeatureID): Observable<boolean> {
|
||||||
// return observableOf(this.objectUrl);
|
return observableOf(authorizedFeatureIds.indexOf(featureId) > -1);
|
||||||
// }
|
}
|
||||||
//
|
});
|
||||||
// getEPersonUuid(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<string> {
|
router = jasmine.createSpyObj('router', {
|
||||||
// return observableOf(this.ePersonUuid);
|
parseUrl: {}
|
||||||
// }
|
});
|
||||||
// }
|
authService = jasmine.createSpyObj('authService', {
|
||||||
//
|
isAuthenticated: observableOf(true)
|
||||||
// describe('SomeFeatureAuthorizationGuard', () => {
|
});
|
||||||
// let guard: SomeFeatureAuthorizationGuard;
|
|
||||||
// let authorizationService: AuthorizationDataService;
|
TestBed.configureTestingModule({
|
||||||
// let router: Router;
|
providers: [
|
||||||
// let authService: AuthService;
|
{ provide: AuthorizationDataService, useValue: authorizationService },
|
||||||
//
|
{ provide: Router, useValue: router },
|
||||||
// let featureIds: FeatureID[];
|
{ provide: AuthService, useValue: authService },
|
||||||
// let authorizedFeatureIds: FeatureID[];
|
]
|
||||||
// let objectUrl: string;
|
});
|
||||||
// let ePersonUuid: string;
|
}
|
||||||
//
|
|
||||||
// function init() {
|
beforeEach(waitForAsync(() => {
|
||||||
// featureIds = [FeatureID.LoginOnBehalfOf, FeatureID.CanDelete];
|
init();
|
||||||
// authorizedFeatureIds = [];
|
}));
|
||||||
// objectUrl = 'fake-object-url';
|
|
||||||
// ePersonUuid = 'fake-eperson-uuid';
|
describe('when the user isn\'t authorized', () => {
|
||||||
//
|
beforeEach(() => {
|
||||||
// authorizationService = Object.assign({
|
authorizedFeatureIds = [];
|
||||||
// isAuthorized(featureId?: FeatureID): Observable<boolean> {
|
});
|
||||||
// return observableOf(authorizedFeatureIds.indexOf(featureId) > -1);
|
|
||||||
// }
|
it('should not return true', (done: DoneFn) => {
|
||||||
// });
|
const result$ = TestBed.runInInjectionContext(() => {
|
||||||
// router = jasmine.createSpyObj('router', {
|
return someFeatureAuthorizationGuard(
|
||||||
// parseUrl: {}
|
() => observableOf(featureIds),
|
||||||
// });
|
() => observableOf(objectUrl),
|
||||||
// authService = jasmine.createSpyObj('authService', {
|
() => observableOf(ePersonUuid),
|
||||||
// isAuthenticated: observableOf(true)
|
)(undefined, { url: 'current-url' } as any)
|
||||||
// });
|
}) as Observable<boolean | UrlTree>;
|
||||||
// guard = new SomeFeatureAuthorizationGuardImpl(authorizationService, router, authService, featureIds, objectUrl, ePersonUuid);
|
|
||||||
// }
|
result$.subscribe((result) => {
|
||||||
//
|
expect(result).not.toEqual(true);
|
||||||
// beforeEach(() => {
|
done();
|
||||||
// init();
|
});
|
||||||
// });
|
});
|
||||||
//
|
});
|
||||||
// describe('canActivate', () => {
|
|
||||||
// describe('when the user isn\'t authorized', () => {
|
describe('when the user is authorized for at least one of the guard\'s features', () => {
|
||||||
// beforeEach(() => {
|
beforeEach(() => {
|
||||||
// authorizedFeatureIds = [];
|
authorizedFeatureIds = [featureIds[0]];
|
||||||
// });
|
});
|
||||||
//
|
|
||||||
// it('should not return true', (done) => {
|
it('should return true', (done) => {
|
||||||
// guard.canActivate(undefined, { url: 'current-url' } as any).subscribe((result) => {
|
const result$ = TestBed.runInInjectionContext(() => {
|
||||||
// expect(result).not.toEqual(true);
|
return someFeatureAuthorizationGuard(
|
||||||
// done();
|
() => observableOf(featureIds),
|
||||||
// });
|
() => observableOf(objectUrl),
|
||||||
// });
|
() => observableOf(ePersonUuid),
|
||||||
// });
|
)(undefined, { url: 'current-url' } as any)
|
||||||
//
|
}) as Observable<boolean | UrlTree>;
|
||||||
// describe('when the user is authorized for at least one of the guard\'s features', () => {
|
|
||||||
// beforeEach(() => {
|
result$.subscribe((result) => {
|
||||||
// authorizedFeatureIds = [featureIds[0]];
|
expect(result).toEqual(true);
|
||||||
// });
|
done();
|
||||||
//
|
});
|
||||||
// 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;
|
||||||
// });
|
});
|
||||||
//
|
|
||||||
// describe('when the user is authorized for all of the guard\'s features', () => {
|
it('should return true', (done) => {
|
||||||
// beforeEach(() => {
|
const result$ = TestBed.runInInjectionContext(() => {
|
||||||
// authorizedFeatureIds = featureIds;
|
return someFeatureAuthorizationGuard(
|
||||||
// });
|
() => observableOf(featureIds),
|
||||||
//
|
() => observableOf(objectUrl),
|
||||||
// it('should return true', (done) => {
|
() => observableOf(ePersonUuid),
|
||||||
// guard.canActivate(undefined, { url: 'current-url' } as any).subscribe((result) => {
|
)(undefined, { url: 'current-url' } as any)
|
||||||
// expect(result).toEqual(true);
|
}) as Observable<boolean | UrlTree>;
|
||||||
// done();
|
|
||||||
// });
|
result$.subscribe((result) => {
|
||||||
// });
|
expect(result).toEqual(true);
|
||||||
// });
|
done();
|
||||||
// });
|
});
|
||||||
// });
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
Reference in New Issue
Block a user