NotesService: Add unit tests for forbidden note alias

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-02-20 22:23:52 +01:00
parent 9b25f401f7
commit 4034fa6495

View file

@ -26,6 +26,8 @@ import { NotesService } from './notes.service';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
import { Tag } from './tag.entity'; import { Tag } from './tag.entity';
import { import {
AlreadyInDBError,
ForbiddenIdError,
NotInDBError, NotInDBError,
PermissionsUpdateInconsistentError, PermissionsUpdateInconsistentError,
} from '../errors/errors'; } from '../errors/errors';
@ -46,6 +48,7 @@ describe('NotesService', () => {
let revisionRepo: Repository<Revision>; let revisionRepo: Repository<Revision>;
let userRepo: Repository<User>; let userRepo: Repository<User>;
let groupRepo: Repository<Group>; let groupRepo: Repository<Group>;
let forbiddenNoteId: string;
beforeEach(async () => { beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
@ -95,6 +98,8 @@ describe('NotesService', () => {
.useClass(Repository) .useClass(Repository)
.compile(); .compile();
const config = module.get<ConfigService>(ConfigService);
forbiddenNoteId = config.get('appConfig').forbiddenNoteIds[0];
service = module.get<NotesService>(NotesService); service = module.get<NotesService>(NotesService);
noteRepo = module.get<Repository<Note>>(getRepositoryToken(Note)); noteRepo = module.get<Repository<Note>>(getRepositoryToken(Note));
revisionRepo = module.get<Repository<Revision>>( revisionRepo = module.get<Repository<Revision>>(
@ -135,10 +140,10 @@ describe('NotesService', () => {
}); });
describe('createNote', () => { describe('createNote', () => {
const user = User.create('hardcoded', 'Testy') as User;
const alias = 'alias';
const content = 'testContent';
describe('works', () => { describe('works', () => {
const user = User.create('hardcoded', 'Testy') as User;
const alias = 'alias';
const content = 'testContent';
beforeEach(() => { beforeEach(() => {
jest jest
.spyOn(noteRepo, 'save') .spyOn(noteRepo, 'save')
@ -195,6 +200,26 @@ describe('NotesService', () => {
expect(newNote.alias).toEqual(alias); expect(newNote.alias).toEqual(alias);
}); });
}); });
describe('fails:', () => {
it('alias is forbidden', async () => {
try {
await service.createNote(content, forbiddenNoteId);
} catch (e) {
expect(e).toBeInstanceOf(ForbiddenIdError);
}
});
it('alias is already used', async () => {
jest.spyOn(noteRepo, 'save').mockImplementationOnce(async () => {
throw new Error();
});
try {
await service.createNote(content, alias);
} catch (e) {
expect(e).toBeInstanceOf(AlreadyInDBError);
}
});
});
}); });
describe('getNoteContent', () => { describe('getNoteContent', () => {
@ -252,13 +277,23 @@ describe('NotesService', () => {
const foundNote = await service.getNoteByIdOrAlias('noteThatExists'); const foundNote = await service.getNoteByIdOrAlias('noteThatExists');
expect(foundNote).toEqual(note); expect(foundNote).toEqual(note);
}); });
it('fails: no note found', async () => { describe('fails:', () => {
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined); it('no note found', async () => {
try { jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined);
await service.getNoteByIdOrAlias('noteThatDoesNoteExist'); try {
} catch (e) { await service.getNoteByIdOrAlias('noteThatDoesNoteExist');
expect(e).toBeInstanceOf(NotInDBError); } catch (e) {
} expect(e).toBeInstanceOf(NotInDBError);
}
});
it('id is forbidden', async () => {
jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined);
try {
await service.getNoteByIdOrAlias(forbiddenNoteId);
} catch (e) {
expect(e).toBeInstanceOf(ForbiddenIdError);
}
});
}); });
}); });