Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
artlowel committed Apr 30, 2024
1 parent 7e04231 commit f63aea1
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)

Check failure on line 54 in src/app/core/data/feature-authorization/feature-authorization-guard/single-feature-authorization.guard.spec.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Missing semicolon

Check failure on line 54 in src/app/core/data/feature-authorization/feature-authorization-guard/single-feature-authorization.guard.spec.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Missing semicolon
}) as Observable<boolean | UrlTree>;

}, 10000);

result$.subscribe(() => {
expect(authorizationService.isAuthorized).toHaveBeenCalledWith(featureId, objectUrl, ePersonUuid);
done();
})

Check failure on line 61 in src/app/core/data/feature-authorization/feature-authorization-guard/single-feature-authorization.guard.spec.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Missing semicolon

Check failure on line 61 in src/app/core/data/feature-authorization/feature-authorization-guard/single-feature-authorization.guard.spec.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Missing semicolon
});

});

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

View workflow job for this annotation

GitHub Actions / tests (16.x)

Missing semicolon

Check failure on line 61 in src/app/core/data/feature-authorization/feature-authorization-guard/some-feature-authorization.guard.spec.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Missing semicolon
}) 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

View workflow job for this annotation

GitHub Actions / tests (16.x)

Missing semicolon

Check failure on line 82 in src/app/core/data/feature-authorization/feature-authorization-guard/some-feature-authorization.guard.spec.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Missing semicolon
}) 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

View workflow job for this annotation

GitHub Actions / tests (16.x)

Missing semicolon

Check failure on line 103 in src/app/core/data/feature-authorization/feature-authorization-guard/some-feature-authorization.guard.spec.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Missing semicolon
}) as Observable<boolean | UrlTree>;

result$.subscribe((result) => {
expect(result).toEqual(true);
done();
});
});
});
});

0 comments on commit f63aea1

Please sign in to comment.