test: note length check works on note creation

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2022-10-02 23:48:06 +02:00
parent b1c1837dc5
commit 66c02a3875
3 changed files with 71 additions and 0 deletions

View file

@ -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);
});
});
});
});

View file

@ -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}', () => {

View file

@ -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}', () => {