test(e2e): add more tests for POST /notes

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2022-10-03 16:13:09 +02:00
parent 8201fb8c32
commit 98e2dd0a7c
2 changed files with 98 additions and 28 deletions

View file

@ -8,6 +8,7 @@ import { join } from 'path';
import request from 'supertest'; import request from 'supertest';
import { NotInDBError } from '../../src/errors/errors'; import { NotInDBError } from '../../src/errors/errors';
import * as utils from '../../src/notes/utils';
import { User } from '../../src/users/user.entity'; import { User } from '../../src/users/user.entity';
import { TestSetup, TestSetupBuilder } from '../test-setup'; import { TestSetup, TestSetupBuilder } from '../test-setup';
@ -55,21 +56,55 @@ describe('Notes', () => {
await testSetup.cleanup(); await testSetup.cleanup();
}); });
it('POST /notes', async () => { describe('POST /notes', () => {
const response = await agent it('creates a note', async () => {
.post('/api/private/notes') const response = await agent
.set('Content-Type', 'text/markdown') .post('/api/private/notes')
.send(content) .set('Content-Type', 'text/markdown')
.expect('Content-Type', /json/) .send(content)
.expect(201); .expect('Content-Type', /json/)
expect(response.body.metadata?.id).toBeDefined(); .expect(201);
expect( expect(response.body.metadata?.id).toBeDefined();
await testSetup.notesService.getNoteContent( expect(
await testSetup.notesService.getNoteByIdOrAlias( await testSetup.notesService.getNoteContent(
response.body.metadata.id, await testSetup.notesService.getNoteByIdOrAlias(
response.body.metadata.id,
),
), ),
), ).toEqual(content);
).toEqual(content); });
describe('does not create a note', () => {
it('if content exceeds maxDocumentLength', async () => {
const content = 'x'.repeat(
(testSetup.configService.get('noteConfig')
.maxDocumentLength as number) + 1,
);
await agent
.post('/api/private/notes')
.set('Content-Type', 'text/markdown')
.send(content)
.expect('Content-Type', /json/)
.expect(413);
});
it('if publicId already exists', async () => {
// This should not happen, but you at least theoretical it's possible
const response = await agent
.post('/api/private/notes')
.set('Content-Type', 'text/markdown')
.send(content)
.expect('Content-Type', /json/)
.expect(201);
jest
.spyOn(utils, 'generatePublicId')
.mockReturnValueOnce(response.body.metadata?.id);
await agent
.post('/api/private/notes')
.set('Content-Type', 'text/markdown')
.send(content)
.expect('Content-Type', /json/)
.expect(409);
});
});
}); });
describe('GET /notes/{note}', () => { describe('GET /notes/{note}', () => {

View file

@ -9,6 +9,7 @@ import request from 'supertest';
import { NotInDBError } from '../../src/errors/errors'; import { NotInDBError } from '../../src/errors/errors';
import { NotePermissionsUpdateDto } from '../../src/notes/note-permissions.dto'; import { NotePermissionsUpdateDto } from '../../src/notes/note-permissions.dto';
import * as utils from '../../src/notes/utils';
import { User } from '../../src/users/user.entity'; import { User } from '../../src/users/user.entity';
import { TestSetup, TestSetupBuilder } from '../test-setup'; import { TestSetup, TestSetupBuilder } from '../test-setup';
@ -46,21 +47,55 @@ describe('Notes', () => {
await testSetup.cleanup(); await testSetup.cleanup();
}); });
it('POST /notes', async () => { describe('POST /notes', () => {
const response = await request(testSetup.app.getHttpServer()) it('creates a note', async () => {
.post('/api/v2/notes') const response = await request(testSetup.app.getHttpServer())
.set('Content-Type', 'text/markdown') .post('/api/v2/notes')
.send(content) .set('Content-Type', 'text/markdown')
.expect('Content-Type', /json/) .send(content)
.expect(201); .expect('Content-Type', /json/)
expect(response.body.metadata?.id).toBeDefined(); .expect(201);
expect( expect(response.body.metadata?.id).toBeDefined();
await testSetup.notesService.getNoteContent( expect(
await testSetup.notesService.getNoteByIdOrAlias( await testSetup.notesService.getNoteContent(
response.body.metadata.id, await testSetup.notesService.getNoteByIdOrAlias(
response.body.metadata.id,
),
), ),
), ).toEqual(content);
).toEqual(content); });
describe('does not create a note', () => {
it('if content exceeds maxDocumentLength', async () => {
const content = 'x'.repeat(
(testSetup.configService.get('noteConfig')
.maxDocumentLength as number) + 1,
);
await request(testSetup.app.getHttpServer())
.post('/api/v2/notes')
.set('Content-Type', 'text/markdown')
.send(content)
.expect('Content-Type', /json/)
.expect(413);
});
it('if publicId already exists', async () => {
// This should not happen, but you at least theoretical it's possible
const response = await request(testSetup.app.getHttpServer())
.post('/api/v2/notes')
.set('Content-Type', 'text/markdown')
.send(content)
.expect('Content-Type', /json/)
.expect(201);
jest
.spyOn(utils, 'generatePublicId')
.mockReturnValueOnce(response.body.metadata?.id);
await request(testSetup.app.getHttpServer())
.post('/api/v2/notes')
.set('Content-Type', 'text/markdown')
.send(content)
.expect('Content-Type', /json/)
.expect(409);
});
});
}); });
describe('GET /notes/{note}', () => { describe('GET /notes/{note}', () => {