From 66c02a387570bd84c7dbea61ddbeb90b2a4aace2 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Sun, 2 Oct 2022 23:48:06 +0200 Subject: [PATCH] test: note length check works on note creation Signed-off-by: Philip Molares --- src/notes/notes.service.spec.ts | 45 ++++++++++++++++++++++++++++++ test/private-api/notes.e2e-spec.ts | 13 +++++++++ test/public-api/notes.e2e-spec.ts | 13 +++++++++ 3 files changed, 71 insertions(+) diff --git a/src/notes/notes.service.spec.ts b/src/notes/notes.service.spec.ts index aeaf1064c..ff1bcafa5 100644 --- a/src/notes/notes.service.spec.ts +++ b/src/notes/notes.service.spec.ts @@ -28,6 +28,7 @@ import { NoteConfig } from '../config/note.config'; import { AlreadyInDBError, ForbiddenIdError, + MaximumDocumentLengthExceededError, NotInDBError, } from '../errors/errors'; import { eventModuleConfig, NoteEvent } from '../events'; @@ -464,6 +465,37 @@ describe('NotesService', () => { expect(await newNote.aliases).toHaveLength(1); expect((await newNote.aliases)[0].name).toEqual(alias); }); + describe('with maxDocumentLength 1000', () => { + beforeEach(() => (noteMockConfig.maxDocumentLength = 1000)); + it('and content has length maxDocumentLength', async () => { + const content = 'x'.repeat(noteMockConfig.maxDocumentLength); + const newNote = await service.createNote(content, user, alias); + const revisions = await newNote.revisions; + expect(revisions).toHaveLength(1); + expect(revisions[0].content).toEqual(content); + expect(await newNote.historyEntries).toHaveLength(1); + expect(await (await newNote.historyEntries)[0].user).toEqual(user); + expect(await newNote.userPermissions).toHaveLength(0); + const groupPermissions = await newNote.groupPermissions; + expect(groupPermissions).toHaveLength(2); + expect(groupPermissions[0].canEdit).toEqual( + everyoneDefaultAccessPermission === DefaultAccessPermission.WRITE, + ); + expect((await groupPermissions[0].group).name).toEqual( + SpecialGroup.EVERYONE, + ); + expect(groupPermissions[1].canEdit).toEqual( + loggedinDefaultAccessPermission === DefaultAccessPermission.WRITE, + ); + expect((await groupPermissions[1].group).name).toEqual( + SpecialGroup.LOGGED_IN, + ); + expect(await newNote.tags).toHaveLength(0); + expect(await newNote.owner).toEqual(user); + expect(await newNote.aliases).toHaveLength(1); + expect((await newNote.aliases)[0].name).toEqual(alias); + }); + }); describe('with other', () => { beforeEach( () => @@ -510,6 +542,19 @@ describe('NotesService', () => { AlreadyInDBError, ); }); + describe('with maxDocumentLength 1000', () => { + beforeEach(() => (noteMockConfig.maxDocumentLength = 1000)); + it('document is too long', async () => { + mockGroupRepo(); + jest.spyOn(noteRepo, 'save').mockImplementationOnce(async () => { + throw new Error(); + }); + const content = 'x'.repeat(noteMockConfig.maxDocumentLength + 1); + await expect( + service.createNote(content, user, alias), + ).rejects.toThrow(MaximumDocumentLengthExceededError); + }); + }); }); }); diff --git a/test/private-api/notes.e2e-spec.ts b/test/private-api/notes.e2e-spec.ts index 365865642..870c466eb 100644 --- a/test/private-api/notes.e2e-spec.ts +++ b/test/private-api/notes.e2e-spec.ts @@ -126,6 +126,19 @@ describe('Notes', () => { .expect('Content-Type', /json/) .expect(409); }); + + it('fails with a content, that is too long', async () => { + const content = 'x'.repeat( + (testSetup.configService.get('noteConfig') + .maxDocumentLength as number) + 1, + ); + await agent + .post('/api/private/notes/test2') + .set('Content-Type', 'text/markdown') + .send(content) + .expect('Content-Type', /json/) + .expect(413); + }); }); describe('DELETE /notes/{note}', () => { diff --git a/test/public-api/notes.e2e-spec.ts b/test/public-api/notes.e2e-spec.ts index fe89c517e..865dc189b 100644 --- a/test/public-api/notes.e2e-spec.ts +++ b/test/public-api/notes.e2e-spec.ts @@ -124,6 +124,19 @@ describe('Notes', () => { .expect('Content-Type', /json/) .expect(409); }); + + it('fails with a content, that is too long', async () => { + const content = 'x'.repeat( + (testSetup.configService.get('noteConfig') + .maxDocumentLength as number) + 1, + ); + await request(testSetup.app.getHttpServer()) + .post('/api/v2/notes/test2') + .set('Content-Type', 'text/markdown') + .send(content) + .expect('Content-Type', /json/) + .expect(413); + }); }); describe('DELETE /notes/{note}', () => {