test: verify that event system is used

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2022-09-24 16:09:40 +02:00 committed by David Mehren
parent 7798a77f6d
commit 297469b49e
2 changed files with 123 additions and 0 deletions

View file

@ -61,6 +61,7 @@ describe('NotesService', () => {
let forbiddenNoteId: string; let forbiddenNoteId: string;
let everyoneDefaultAccessPermission: string; let everyoneDefaultAccessPermission: string;
let loggedinDefaultAccessPermission: string; let loggedinDefaultAccessPermission: string;
let eventEmitter: EventEmitter2;
const everyone = Group.create( const everyone = Group.create(
SpecialGroup.EVERYONE, SpecialGroup.EVERYONE,
SpecialGroup.EVERYONE, SpecialGroup.EVERYONE,
@ -281,6 +282,7 @@ describe('NotesService', () => {
revisionRepo = module.get<Repository<Revision>>( revisionRepo = module.get<Repository<Revision>>(
getRepositoryToken(Revision), getRepositoryToken(Revision),
); );
eventEmitter = module.get<EventEmitter2>(EventEmitter2);
}); });
it('should be defined', () => { it('should be defined', () => {
@ -584,7 +586,15 @@ describe('NotesService', () => {
expect(entry).toEqual(note); expect(entry).toEqual(note);
return entry; return entry;
}); });
const mockedEventEmitter = jest
.spyOn(eventEmitter, 'emit')
.mockImplementationOnce((event) => {
expect(event).toEqual(NoteEvent.DELETION);
return true;
});
expect(mockedEventEmitter).not.toHaveBeenCalled();
await service.deleteNote(note); await service.deleteNote(note);
expect(mockedEventEmitter).toHaveBeenCalled();
}); });
}); });

View file

@ -52,6 +52,7 @@ describe('PermissionsService', () => {
let noteRepo: Repository<Note>; let noteRepo: Repository<Note>;
let userRepo: Repository<User>; let userRepo: Repository<User>;
let groupRepo: Repository<Group>; let groupRepo: Repository<Group>;
let eventEmitter: EventEmitter2;
const noteMockConfig: NoteConfig = createDefaultMockNoteConfig(); const noteMockConfig: NoteConfig = createDefaultMockNoteConfig();
beforeAll(async () => { beforeAll(async () => {
@ -145,6 +146,11 @@ describe('PermissionsService', () => {
notes = await createNoteUserPermissionNotes(); notes = await createNoteUserPermissionNotes();
groupRepo = module.get<Repository<Group>>(getRepositoryToken(Group)); groupRepo = module.get<Repository<Group>>(getRepositoryToken(Group));
noteRepo = module.get<Repository<Note>>(getRepositoryToken(Note)); noteRepo = module.get<Repository<Note>>(getRepositoryToken(Note));
eventEmitter = module.get<EventEmitter2>(EventEmitter2);
});
afterEach(() => {
jest.clearAllMocks();
}); });
// The two users we test with: // The two users we test with:
@ -730,6 +736,27 @@ describe('PermissionsService', () => {
false, false,
) as Group; ) as Group;
const note = Note.create(user) as Note; const note = Note.create(user) as Note;
it('emits PERMISSION_CHANGE event', async () => {
jest
.spyOn(noteRepo, 'save')
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
.mockImplementationOnce(async (entry: Note) => {
return entry;
});
const mockedEventEmitter = jest
.spyOn(eventEmitter, 'emit')
.mockImplementationOnce((event) => {
expect(event).toEqual(NoteEvent.PERMISSION_CHANGE);
return true;
});
expect(mockedEventEmitter).not.toHaveBeenCalled();
await service.updateNotePermissions(note, {
sharedToUsers: [],
sharedToGroups: [],
});
expect(mockedEventEmitter).toHaveBeenCalled();
});
describe('works', () => { describe('works', () => {
it('with empty GroupPermissions and with empty UserPermissions', async () => { it('with empty GroupPermissions and with empty UserPermissions', async () => {
jest jest
@ -1037,6 +1064,26 @@ describe('PermissionsService', () => {
}); });
describe('setUserPermission', () => { describe('setUserPermission', () => {
it('emits PERMISSION_CHANGE event', async () => {
jest
.spyOn(noteRepo, 'save')
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
.mockImplementationOnce(async (entry: Note) => {
return entry;
});
const note = Note.create(null) as Note;
const user = User.create('test', 'Testy') as User;
const mockedEventEmitter = jest
.spyOn(eventEmitter, 'emit')
.mockImplementationOnce((event) => {
expect(event).toEqual(NoteEvent.PERMISSION_CHANGE);
return true;
});
expect(mockedEventEmitter).not.toHaveBeenCalled();
await service.setUserPermission(note, user, true);
expect(mockedEventEmitter).toHaveBeenCalled();
});
describe('works', () => { describe('works', () => {
it('with user not added before and editable', async () => { it('with user not added before and editable', async () => {
jest jest
@ -1113,6 +1160,29 @@ describe('PermissionsService', () => {
}); });
describe('removeUserPermission', () => { describe('removeUserPermission', () => {
it('emits PERMISSION_CHANGE event', async () => {
jest
.spyOn(noteRepo, 'save')
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
.mockImplementationOnce(async (entry: Note) => {
return entry;
});
const note = Note.create(null) as Note;
const user = User.create('test', 'Testy') as User;
note.userPermissions = Promise.resolve([
NoteUserPermission.create(user, note, true),
]);
const mockedEventEmitter = jest
.spyOn(eventEmitter, 'emit')
.mockImplementationOnce((event) => {
expect(event).toEqual(NoteEvent.PERMISSION_CHANGE);
return true;
});
expect(mockedEventEmitter).not.toHaveBeenCalled();
await service.removeUserPermission(note, user);
expect(mockedEventEmitter).toHaveBeenCalled();
});
describe('works', () => { describe('works', () => {
it('with user added before and editable', async () => { it('with user added before and editable', async () => {
jest jest
@ -1151,6 +1221,26 @@ describe('PermissionsService', () => {
}); });
describe('setGroupPermission', () => { describe('setGroupPermission', () => {
it('emits PERMISSION_CHANGE event', async () => {
jest
.spyOn(noteRepo, 'save')
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
.mockImplementationOnce(async (entry: Note) => {
return entry;
});
const note = Note.create(null) as Note;
const group = Group.create('test', 'Testy', false) as Group;
const mockedEventEmitter = jest
.spyOn(eventEmitter, 'emit')
.mockImplementationOnce((event) => {
expect(event).toEqual(NoteEvent.PERMISSION_CHANGE);
return true;
});
expect(mockedEventEmitter).not.toHaveBeenCalled();
await service.setGroupPermission(note, group, true);
expect(mockedEventEmitter).toHaveBeenCalled();
});
describe('works', () => { describe('works', () => {
it('with group not added before and editable', async () => { it('with group not added before and editable', async () => {
jest jest
@ -1243,6 +1333,29 @@ describe('PermissionsService', () => {
}); });
describe('removeGroupPermission', () => { describe('removeGroupPermission', () => {
it('emits PERMISSION_CHANGE event', async () => {
jest
.spyOn(noteRepo, 'save')
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
.mockImplementationOnce(async (entry: Note) => {
return entry;
});
const note = Note.create(null) as Note;
const group = Group.create('test', 'Testy', false) as Group;
note.groupPermissions = Promise.resolve([
NoteGroupPermission.create(group, note, true),
]);
const mockedEventEmitter = jest
.spyOn(eventEmitter, 'emit')
.mockImplementationOnce((event) => {
expect(event).toEqual(NoteEvent.PERMISSION_CHANGE);
return true;
});
expect(mockedEventEmitter).not.toHaveBeenCalled();
await service.removeGroupPermission(note, group);
expect(mockedEventEmitter).toHaveBeenCalled();
});
describe('works', () => { describe('works', () => {
it('with user added before and editable', async () => { it('with user added before and editable', async () => {
jest jest