mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-25 11:16:31 -05:00
refactor: extract mocking in permissions.service.spec.ts
Signed-off-by: Yannick Bungers <git@innay.de>
This commit is contained in:
parent
d73bbcaeff
commit
ed3c688e8b
1 changed files with 23 additions and 18 deletions
|
@ -171,6 +171,23 @@ describe('PermissionsService', () => {
|
||||||
eventEmitter = module.get<EventEmitter2>(EventEmitter2);
|
eventEmitter = module.get<EventEmitter2>(EventEmitter2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function mockMayReadTrue(): void {
|
||||||
|
jest.spyOn(service, 'mayRead').mockImplementationOnce(async () => {
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function mockMayWriteTrue(): void {
|
||||||
|
jest.spyOn(service, 'mayWrite').mockImplementationOnce(async () => {
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function mockIsOwner(isOwner: boolean): void {
|
||||||
|
jest.spyOn(service, 'isOwner').mockImplementationOnce(async () => {
|
||||||
|
return isOwner;
|
||||||
|
});
|
||||||
|
}
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
mockNoteRepo(noteRepo);
|
mockNoteRepo(noteRepo);
|
||||||
eventEmitterEmitSpy = mockedEventEmitter(eventEmitter);
|
eventEmitterEmitSpy = mockedEventEmitter(eventEmitter);
|
||||||
|
@ -774,17 +791,13 @@ describe('PermissionsService', () => {
|
||||||
describe('checkPermissionOnNote', () => {
|
describe('checkPermissionOnNote', () => {
|
||||||
describe('accepts', () => {
|
describe('accepts', () => {
|
||||||
it('with mayRead', async () => {
|
it('with mayRead', async () => {
|
||||||
jest.spyOn(service, 'mayRead').mockImplementationOnce(async () => {
|
mockMayReadTrue();
|
||||||
return true;
|
|
||||||
});
|
|
||||||
expect(
|
expect(
|
||||||
await service.checkPermissionOnNote(Permission.READ, user1, notes[0]),
|
await service.checkPermissionOnNote(Permission.READ, user1, notes[0]),
|
||||||
).toBeTruthy();
|
).toBeTruthy();
|
||||||
});
|
});
|
||||||
it('with mayWrite', async () => {
|
it('with mayWrite', async () => {
|
||||||
jest.spyOn(service, 'mayWrite').mockImplementationOnce(async () => {
|
mockMayWriteTrue();
|
||||||
return true;
|
|
||||||
});
|
|
||||||
expect(
|
expect(
|
||||||
await service.checkPermissionOnNote(
|
await service.checkPermissionOnNote(
|
||||||
Permission.WRITE,
|
Permission.WRITE,
|
||||||
|
@ -794,9 +807,7 @@ describe('PermissionsService', () => {
|
||||||
).toBeTruthy();
|
).toBeTruthy();
|
||||||
});
|
});
|
||||||
it('with isOwner', async () => {
|
it('with isOwner', async () => {
|
||||||
jest.spyOn(service, 'isOwner').mockImplementationOnce(async () => {
|
mockIsOwner(true);
|
||||||
return true;
|
|
||||||
});
|
|
||||||
expect(
|
expect(
|
||||||
await service.checkPermissionOnNote(
|
await service.checkPermissionOnNote(
|
||||||
Permission.OWNER,
|
Permission.OWNER,
|
||||||
|
@ -808,15 +819,9 @@ describe('PermissionsService', () => {
|
||||||
});
|
});
|
||||||
describe('denies', () => {
|
describe('denies', () => {
|
||||||
it('with no permission', async () => {
|
it('with no permission', async () => {
|
||||||
jest.spyOn(service, 'mayRead').mockImplementationOnce(async () => {
|
mockMayReadTrue();
|
||||||
return false;
|
mockMayWriteTrue();
|
||||||
});
|
mockIsOwner(false);
|
||||||
jest.spyOn(service, 'mayWrite').mockImplementationOnce(async () => {
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
jest.spyOn(service, 'isOwner').mockImplementationOnce(async () => {
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
expect(
|
expect(
|
||||||
await service.checkPermissionOnNote(
|
await service.checkPermissionOnNote(
|
||||||
Permission.OWNER,
|
Permission.OWNER,
|
||||||
|
|
Loading…
Reference in a new issue