mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-12 22:26:08 -04:00
PrivateApi: Add option to keep media to DELETE /notes/{note}
This adds a body to the route DELETE /notes/{note} of the private api to specify if the associated media uploads of the note should be kept or deleted. Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
b65297523c
commit
bd4231c1c5
2 changed files with 56 additions and 6 deletions
|
@ -38,6 +38,7 @@ describe('Notes', () => {
|
|||
let content: string;
|
||||
let forbiddenNoteId: string;
|
||||
let uploadPath: string;
|
||||
let testImage: Buffer;
|
||||
|
||||
beforeAll(async () => {
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
|
@ -80,6 +81,7 @@ describe('Notes', () => {
|
|||
user = await userService.createUser('hardcoded', 'Testy');
|
||||
user2 = await userService.createUser('hardcoded2', 'Max Mustermann');
|
||||
content = 'This is a test note.';
|
||||
testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
||||
});
|
||||
|
||||
it('POST /notes', async () => {
|
||||
|
@ -152,12 +154,49 @@ describe('Notes', () => {
|
|||
});
|
||||
|
||||
describe('DELETE /notes/{note}', () => {
|
||||
it('works with an existing alias', async () => {
|
||||
await notesService.createNote(content, 'test3', user);
|
||||
await request(app.getHttpServer()).delete('/notes/test3').expect(204);
|
||||
await expect(notesService.getNoteByIdOrAlias('test3')).rejects.toEqual(
|
||||
new NotInDBError("Note with id/alias 'test3' not found."),
|
||||
);
|
||||
describe('works', () => {
|
||||
it('with an existing alias and keepMedia false', async () => {
|
||||
const noteId = 'test3';
|
||||
await notesService.createNote(content, noteId, user);
|
||||
await mediaService.saveFile(testImage, user.userName, noteId);
|
||||
await request(app.getHttpServer())
|
||||
.delete(`/notes/${noteId}`)
|
||||
.set('Content-Type', 'application/json')
|
||||
.send({
|
||||
keepMedia: false,
|
||||
})
|
||||
.expect(204);
|
||||
await expect(notesService.getNoteByIdOrAlias(noteId)).rejects.toEqual(
|
||||
new NotInDBError(`Note with id/alias '${noteId}' not found.`),
|
||||
);
|
||||
expect(await mediaService.listUploadsByUser(user)).toHaveLength(0);
|
||||
await fs.rmdir(uploadPath);
|
||||
});
|
||||
it('with an existing alias and keepMedia true', async () => {
|
||||
const noteId = 'test3a';
|
||||
await notesService.createNote(content, noteId, user);
|
||||
const url = await mediaService.saveFile(
|
||||
testImage,
|
||||
user.userName,
|
||||
noteId,
|
||||
);
|
||||
await request(app.getHttpServer())
|
||||
.delete(`/notes/${noteId}`)
|
||||
.set('Content-Type', 'application/json')
|
||||
.send({
|
||||
keepMedia: true,
|
||||
})
|
||||
.expect(204);
|
||||
await expect(notesService.getNoteByIdOrAlias(noteId)).rejects.toEqual(
|
||||
new NotInDBError(`Note with id/alias '${noteId}' not found.`),
|
||||
);
|
||||
expect(await mediaService.listUploadsByUser(user)).toHaveLength(1);
|
||||
// Remove /upload/ from path as we just need the filename.
|
||||
const fileName = url.replace('/uploads/', '');
|
||||
// delete the file afterwards
|
||||
await fs.unlink(join(uploadPath, fileName));
|
||||
await fs.rmdir(uploadPath);
|
||||
});
|
||||
});
|
||||
it('fails with a forbidden alias', async () => {
|
||||
await request(app.getHttpServer())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue