114858: Add Tests for new Guards - Done

This commit is contained in:
Zahraa Chreim
2024-05-10 16:00:27 +03:00
parent 0eb61dd1de
commit f1cac6e385
5 changed files with 378 additions and 10 deletions

View File

@@ -0,0 +1,82 @@
import { TestBed } from '@angular/core/testing';
import {
Router,
UrlTree,
} from '@angular/router';
import { Store } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import {
Observable,
of as observableOf,
} from 'rxjs';
import { AuthService } from 'src/app/core/auth/auth.service';
import { AuthorizationDataService } from 'src/app/core/data/feature-authorization/authorization-data.service';
import { FeatureID } from 'src/app/core/data/feature-authorization/feature-id';
import { bitstreamPageAuthorizationsGuard } from './bitstream-page-authorizations.guard';
import { BitstreamDataService } from '../core/data/bitstream-data.service';
import { Bitstream } from '../core/shared/bitstream.model';
import { createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils';
describe('bitstreamPageAuthorizationsGuard', () => {
let authorizationService: AuthorizationDataService;
let authService: AuthService;
let router: Router;
let route;
let parentRoute;
let bitstreamService: BitstreamDataService;
let bitstream: Bitstream;
let uuid = '1234-abcdef-54321-fedcba';
let bitstreamSelfLink = 'test.url/1234-abcdef-54321-fedcba';
beforeEach(() => {
authorizationService = jasmine.createSpyObj('authorizationService', {
isAuthorized: observableOf(true),
});
router = jasmine.createSpyObj('router', {
parseUrl: {},
navigateByUrl: undefined,
});
authService = jasmine.createSpyObj('authService', {
isAuthenticated: observableOf(true),
});
parentRoute = {
params: {
id: '3e1a5327-dabb-41ff-af93-e6cab9d032f0',
},
};
route = {
params: {},
parent: parentRoute,
};
bitstream = new Bitstream();
bitstream.uuid = uuid;
bitstream._links = { self: { href: bitstreamSelfLink } } as any;
bitstreamService = jasmine.createSpyObj('bitstreamService', { findById: createSuccessfulRemoteDataObject$(bitstream) });
TestBed.configureTestingModule({
providers: [
{ provide: AuthorizationDataService, useValue: authorizationService },
{ provide: Router, useValue: router },
{ provide: AuthService, useValue: authService },
{ provide: BitstreamDataService, useValue: bitstreamService },
],
});
});
it('should call authorizationService.isAuthorized with the appropriate arguments', (done) => {
const result$ = TestBed.runInInjectionContext(() => {
return bitstreamPageAuthorizationsGuard(route, { url: 'current-url' } as any);
}) as Observable<boolean | UrlTree>;
result$.subscribe((result) => {
expect(authorizationService.isAuthorized).toHaveBeenCalledWith(
FeatureID.CanManagePolicies,
bitstreamSelfLink,
undefined,
);
done();
});
});
});

View File

@@ -0,0 +1,94 @@
import { TestBed } from '@angular/core/testing';
import {
Router,
UrlTree,
} from '@angular/router';
import { Store } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import {
Observable,
of as observableOf,
} from 'rxjs';
import { AuthService } from 'src/app/core/auth/auth.service';
import { AuthorizationDataService } from 'src/app/core/data/feature-authorization/authorization-data.service';
import { FeatureID } from 'src/app/core/data/feature-authorization/feature-id';
import { APP_DATA_SERVICES_MAP } from '../../../config/app-config.interface';
import { ItemDataService } from '../../core/data/item-data.service';
import { Item } from '../../core/shared/item.model';
import { getMockTranslateService } from '../../shared/mocks/translate.service.mock';
import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
import { itemPageDeleteGuard } from './item-page-delete.guard';
describe('itemPageDeleteGuard', () => {
let authorizationService: AuthorizationDataService;
let authService: AuthService;
let router: Router;
let route;
let parentRoute;
let store: Store;
let itemService: ItemDataService;
let item: Item;
let uuid = '1234-abcdef-54321-fedcba';
let itemSelfLink = 'test.url/1234-abcdef-54321-fedcba';
beforeEach(() => {
store = jasmine.createSpyObj('store', {
dispatch: {},
pipe: observableOf(true),
});
authorizationService = jasmine.createSpyObj('authorizationService', {
isAuthorized: observableOf(true),
});
router = jasmine.createSpyObj('router', {
parseUrl: {},
navigateByUrl: undefined,
});
authService = jasmine.createSpyObj('authService', {
isAuthenticated: observableOf(true),
});
parentRoute = {
params: {
id: '3e1a5327-dabb-41ff-af93-e6cab9d032f0',
},
};
route = {
params: {},
parent: parentRoute,
};
item = new Item();
item.uuid = uuid;
item._links = { self: { href: itemSelfLink } } as any;
itemService = jasmine.createSpyObj('itemService', { findById: createSuccessfulRemoteDataObject$(item) });
TestBed.configureTestingModule({
providers: [
{ provide: AuthorizationDataService, useValue: authorizationService },
{ provide: Router, useValue: router },
{ provide: AuthService, useValue: authService },
{ provide: Store, useValue: store },
{ provide: APP_DATA_SERVICES_MAP, useValue: {} },
{ provide: TranslateService, useValue: getMockTranslateService() },
{ provide: ItemDataService, useValue: itemService },
],
});
});
it('should call authorizationService.isAuthorized with the appropriate arguments', (done) => {
const result$ = TestBed.runInInjectionContext(() => {
return itemPageDeleteGuard(route, { url: 'current-url' } as any);
}) as Observable<boolean | UrlTree>;
result$.subscribe((result) => {
expect(authorizationService.isAuthorized).toHaveBeenCalledWith(
FeatureID.CanDelete,
itemSelfLink, // This value is retrieved from the itemDataService.findById's return item's self link
undefined, // dsoPageSingleFeatureGuard never provides a function to retrieve a person ID
);
done();
});
});
});

View File

@@ -1,18 +1,24 @@
import { TestBed } from '@angular/core/testing';
import { Router, UrlTree } from '@angular/router';
import { Observable, of as observableOf } from 'rxjs';
import {
Router,
UrlTree,
} from '@angular/router';
import { Store } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import {
Observable,
of as observableOf,
} from 'rxjs';
import { AuthService } from 'src/app/core/auth/auth.service';
import { AuthorizationDataService } from 'src/app/core/data/feature-authorization/authorization-data.service';
import { FeatureID } from 'src/app/core/data/feature-authorization/feature-id';
import { Store, } from '@ngrx/store';
import { itemPageEditAuthorizationsGuard } from './item-page-edit-authorizations.guard';
import { APP_DATA_SERVICES_MAP } from '../../../config/app-config.interface';
import { TranslateService } from '@ngx-translate/core';
import { getMockTranslateService } from '../../shared/mocks/translate.service.mock';
import { ItemDataService } from '../../core/data/item-data.service';
import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
import { Item } from '../../core/shared/item.model';
import { getMockTranslateService } from '../../shared/mocks/translate.service.mock';
import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
import { itemPageEditAuthorizationsGuard } from './item-page-edit-authorizations.guard';
describe('itemPageEditAuthorizationsGuard', () => {
let authorizationService: AuthorizationDataService;
@@ -55,7 +61,7 @@ describe('itemPageEditAuthorizationsGuard', () => {
item = new Item();
item.uuid = uuid;
item._links = { self: { href: itemSelfLink } } as any;
itemService = jasmine.createSpyObj('itemService', { findById: createSuccessfulRemoteDataObject$(item) })
itemService = jasmine.createSpyObj('itemService', { findById: createSuccessfulRemoteDataObject$(item) });
TestBed.configureTestingModule({
providers: [
@@ -75,9 +81,7 @@ describe('itemPageEditAuthorizationsGuard', () => {
return itemPageEditAuthorizationsGuard(route, { url: 'current-url' } as any);
}) as Observable<boolean | UrlTree>;
console.log('will subscribe');
result$.subscribe((result) => {
console.log('result inside subscribe:', result);
expect(authorizationService.isAuthorized).toHaveBeenCalledWith(
FeatureID.CanManagePolicies,
itemSelfLink, // This value is retrieved from the itemDataService.findById's return item's self link

View File

@@ -0,0 +1,94 @@
import { TestBed } from '@angular/core/testing';
import {
Router,
UrlTree,
} from '@angular/router';
import { Store } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import {
Observable,
of as observableOf,
} from 'rxjs';
import { AuthService } from 'src/app/core/auth/auth.service';
import { AuthorizationDataService } from 'src/app/core/data/feature-authorization/authorization-data.service';
import { FeatureID } from 'src/app/core/data/feature-authorization/feature-id';
import { APP_DATA_SERVICES_MAP } from '../../../config/app-config.interface';
import { ItemDataService } from '../../core/data/item-data.service';
import { Item } from '../../core/shared/item.model';
import { getMockTranslateService } from '../../shared/mocks/translate.service.mock';
import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
import { itemPageMoveGuard } from './item-page-move.guard';
describe('itemPageMoveGuard', () => {
let authorizationService: AuthorizationDataService;
let authService: AuthService;
let router: Router;
let route;
let parentRoute;
let store: Store;
let itemService: ItemDataService;
let item: Item;
let uuid = '1234-abcdef-54321-fedcba';
let itemSelfLink = 'test.url/1234-abcdef-54321-fedcba';
beforeEach(() => {
store = jasmine.createSpyObj('store', {
dispatch: {},
pipe: observableOf(true),
});
authorizationService = jasmine.createSpyObj('authorizationService', {
isAuthorized: observableOf(true),
});
router = jasmine.createSpyObj('router', {
parseUrl: {},
navigateByUrl: undefined,
});
authService = jasmine.createSpyObj('authService', {
isAuthenticated: observableOf(true),
});
parentRoute = {
params: {
id: '3e1a5327-dabb-41ff-af93-e6cab9d032f0',
},
};
route = {
params: {},
parent: parentRoute,
};
item = new Item();
item.uuid = uuid;
item._links = { self: { href: itemSelfLink } } as any;
itemService = jasmine.createSpyObj('itemService', { findById: createSuccessfulRemoteDataObject$(item) });
TestBed.configureTestingModule({
providers: [
{ provide: AuthorizationDataService, useValue: authorizationService },
{ provide: Router, useValue: router },
{ provide: AuthService, useValue: authService },
{ provide: Store, useValue: store },
{ provide: APP_DATA_SERVICES_MAP, useValue: {} },
{ provide: TranslateService, useValue: getMockTranslateService() },
{ provide: ItemDataService, useValue: itemService },
],
});
});
it('should call authorizationService.isAuthorized with the appropriate arguments', (done) => {
const result$ = TestBed.runInInjectionContext(() => {
return itemPageMoveGuard(route, { url: 'current-url' } as any);
}) as Observable<boolean | UrlTree>;
result$.subscribe((result) => {
expect(authorizationService.isAuthorized).toHaveBeenCalledWith(
FeatureID.CanMove,
itemSelfLink, // This value is retrieved from the itemDataService.findById's return item's self link
undefined, // dsoPageSingleFeatureGuard never provides a function to retrieve a person ID
);
done();
});
});
});

View File

@@ -0,0 +1,94 @@
import { TestBed } from '@angular/core/testing';
import {
Router,
UrlTree,
} from '@angular/router';
import { Store } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import {
Observable,
of as observableOf,
} from 'rxjs';
import { AuthService } from 'src/app/core/auth/auth.service';
import { AuthorizationDataService } from 'src/app/core/data/feature-authorization/authorization-data.service';
import { FeatureID } from 'src/app/core/data/feature-authorization/feature-id';
import { APP_DATA_SERVICES_MAP } from '../../../config/app-config.interface';
import { ItemDataService } from '../../core/data/item-data.service';
import { Item } from '../../core/shared/item.model';
import { getMockTranslateService } from '../../shared/mocks/translate.service.mock';
import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
import { itemPagePrivateGuard } from './item-page-private.guard';
describe('itemPagePrivateGuard', () => {
let authorizationService: AuthorizationDataService;
let authService: AuthService;
let router: Router;
let route;
let parentRoute;
let store: Store;
let itemService: ItemDataService;
let item: Item;
let uuid = '1234-abcdef-54321-fedcba';
let itemSelfLink = 'test.url/1234-abcdef-54321-fedcba';
beforeEach(() => {
store = jasmine.createSpyObj('store', {
dispatch: {},
pipe: observableOf(true),
});
authorizationService = jasmine.createSpyObj('authorizationService', {
isAuthorized: observableOf(true),
});
router = jasmine.createSpyObj('router', {
parseUrl: {},
navigateByUrl: undefined,
});
authService = jasmine.createSpyObj('authService', {
isAuthenticated: observableOf(true),
});
parentRoute = {
params: {
id: '3e1a5327-dabb-41ff-af93-e6cab9d032f0',
},
};
route = {
params: {},
parent: parentRoute,
};
item = new Item();
item.uuid = uuid;
item._links = { self: { href: itemSelfLink } } as any;
itemService = jasmine.createSpyObj('itemService', { findById: createSuccessfulRemoteDataObject$(item) });
TestBed.configureTestingModule({
providers: [
{ provide: AuthorizationDataService, useValue: authorizationService },
{ provide: Router, useValue: router },
{ provide: AuthService, useValue: authService },
{ provide: Store, useValue: store },
{ provide: APP_DATA_SERVICES_MAP, useValue: {} },
{ provide: TranslateService, useValue: getMockTranslateService() },
{ provide: ItemDataService, useValue: itemService },
],
});
});
it('should call authorizationService.isAuthorized with the appropriate arguments', (done) => {
const result$ = TestBed.runInInjectionContext(() => {
return itemPagePrivateGuard(route, { url: 'current-url' } as any);
}) as Observable<boolean | UrlTree>;
result$.subscribe((result) => {
expect(authorizationService.isAuthorized).toHaveBeenCalledWith(
FeatureID.CanMakePrivate,
itemSelfLink, // This value is retrieved from the itemDataService.findById's return item's self link
undefined, // dsoPageSingleFeatureGuard never provides a function to retrieve a person ID
);
done();
});
});
});