Skip to content

Commit

Permalink
test: note length check works on note creation
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Molares <philip.molares@udo.edu>
  • Loading branch information
DerMolly committed Oct 3, 2022
1 parent b1c1837 commit 66c02a3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/notes/notes.service.spec.ts
Expand Up @@ -28,6 +28,7 @@ import { NoteConfig } from '../config/note.config';
import {
AlreadyInDBError,
ForbiddenIdError,
MaximumDocumentLengthExceededError,
NotInDBError,
} from '../errors/errors';
import { eventModuleConfig, NoteEvent } from '../events';
Expand Down Expand Up @@ -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(
() =>
Expand Down Expand Up @@ -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);
});
});
});
});

Expand Down
13 changes: 13 additions & 0 deletions test/private-api/notes.e2e-spec.ts
Expand Up @@ -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}', () => {
Expand Down
13 changes: 13 additions & 0 deletions test/public-api/notes.e2e-spec.ts
Expand Up @@ -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}', () => {
Expand Down

0 comments on commit 66c02a3

Please sign in to comment.