From 4034fa6495a5e5a6795e2fd82c54d3c89c021fb8 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Sat, 20 Feb 2021 22:23:52 +0100 Subject: [PATCH] NotesService: Add unit tests for forbidden note alias Signed-off-by: Philip Molares --- src/notes/notes.service.spec.ts | 55 +++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/src/notes/notes.service.spec.ts b/src/notes/notes.service.spec.ts index 6ffca14b1..20ebc077a 100644 --- a/src/notes/notes.service.spec.ts +++ b/src/notes/notes.service.spec.ts @@ -26,6 +26,8 @@ import { NotesService } from './notes.service'; import { Repository } from 'typeorm'; import { Tag } from './tag.entity'; import { + AlreadyInDBError, + ForbiddenIdError, NotInDBError, PermissionsUpdateInconsistentError, } from '../errors/errors'; @@ -46,6 +48,7 @@ describe('NotesService', () => { let revisionRepo: Repository; let userRepo: Repository; let groupRepo: Repository; + let forbiddenNoteId: string; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -95,6 +98,8 @@ describe('NotesService', () => { .useClass(Repository) .compile(); + const config = module.get(ConfigService); + forbiddenNoteId = config.get('appConfig').forbiddenNoteIds[0]; service = module.get(NotesService); noteRepo = module.get>(getRepositoryToken(Note)); revisionRepo = module.get>( @@ -135,10 +140,10 @@ describe('NotesService', () => { }); describe('createNote', () => { + const user = User.create('hardcoded', 'Testy') as User; + const alias = 'alias'; + const content = 'testContent'; describe('works', () => { - const user = User.create('hardcoded', 'Testy') as User; - const alias = 'alias'; - const content = 'testContent'; beforeEach(() => { jest .spyOn(noteRepo, 'save') @@ -195,6 +200,26 @@ describe('NotesService', () => { 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', () => { @@ -252,13 +277,23 @@ describe('NotesService', () => { const foundNote = await service.getNoteByIdOrAlias('noteThatExists'); expect(foundNote).toEqual(note); }); - it('fails: no note found', async () => { - jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined); - try { - await service.getNoteByIdOrAlias('noteThatDoesNoteExist'); - } catch (e) { - expect(e).toBeInstanceOf(NotInDBError); - } + describe('fails:', () => { + it('no note found', async () => { + jest.spyOn(noteRepo, 'findOne').mockResolvedValueOnce(undefined); + try { + await service.getNoteByIdOrAlias('noteThatDoesNoteExist'); + } 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); + } + }); }); });