mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-27 13:34:28 -04:00
refactor(api/private/media): return MediaUpload object instead of url
This ensures the private POST /media API behaves in the same way as /me/media Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
4f10e17d40
commit
8e31f3a393
10 changed files with 50 additions and 43 deletions
|
@ -69,16 +69,16 @@ describe('Me', () => {
|
|||
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
||||
const imageUrls = [];
|
||||
imageUrls.push(
|
||||
await testSetup.mediaService.saveFile(testImage, user, note1),
|
||||
(await testSetup.mediaService.saveFile(testImage, user, note1)).fileUrl,
|
||||
);
|
||||
imageUrls.push(
|
||||
await testSetup.mediaService.saveFile(testImage, user, note1),
|
||||
(await testSetup.mediaService.saveFile(testImage, user, note1)).fileUrl,
|
||||
);
|
||||
imageUrls.push(
|
||||
await testSetup.mediaService.saveFile(testImage, user, note2),
|
||||
(await testSetup.mediaService.saveFile(testImage, user, note2)).fileUrl,
|
||||
);
|
||||
imageUrls.push(
|
||||
await testSetup.mediaService.saveFile(testImage, user, note2),
|
||||
(await testSetup.mediaService.saveFile(testImage, user, note2)).fileUrl,
|
||||
);
|
||||
|
||||
const response = await agent
|
||||
|
@ -112,12 +112,16 @@ describe('Me', () => {
|
|||
|
||||
it('DELETE /me', async () => {
|
||||
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
||||
const url0 = await testSetup.mediaService.saveFile(testImage, user, note1);
|
||||
const upload = await testSetup.mediaService.saveFile(
|
||||
testImage,
|
||||
user,
|
||||
note1,
|
||||
);
|
||||
const dbUser = await testSetup.userService.getUserByUsername('hardcoded');
|
||||
expect(dbUser).toBeInstanceOf(User);
|
||||
const mediaUploads = await testSetup.mediaService.listUploadsByUser(dbUser);
|
||||
expect(mediaUploads).toHaveLength(1);
|
||||
expect(mediaUploads[0].fileUrl).toEqual(url0);
|
||||
expect(mediaUploads[0].fileUrl).toEqual(upload.fileUrl);
|
||||
await agent.delete('/api/private/me').expect(204);
|
||||
await expect(
|
||||
testSetup.userService.getUserByUsername('hardcoded'),
|
||||
|
|
|
@ -64,7 +64,7 @@ describe('Media', () => {
|
|||
.set('HedgeDoc-Note', 'test_upload_media')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(201);
|
||||
const path: string = uploadResponse.body.link;
|
||||
const path: string = uploadResponse.body.url;
|
||||
const testImage = await fs.readFile('test/private-api/fixtures/test.png');
|
||||
const downloadResponse = await agent.get(path);
|
||||
expect(downloadResponse.body).toEqual(testImage);
|
||||
|
@ -117,12 +117,12 @@ describe('Media', () => {
|
|||
'test_delete_media',
|
||||
);
|
||||
const testImage = await fs.readFile('test/private-api/fixtures/test.png');
|
||||
const url = await testSetup.mediaService.saveFile(
|
||||
const upload = await testSetup.mediaService.saveFile(
|
||||
testImage,
|
||||
user,
|
||||
testNote,
|
||||
);
|
||||
const filename = url.split('/').pop() || '';
|
||||
const filename = upload.fileUrl.split('/').pop() || '';
|
||||
await agent.delete('/api/private/media/' + filename).expect(204);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -161,7 +161,7 @@ describe('Notes', () => {
|
|||
user,
|
||||
noteId,
|
||||
);
|
||||
const url = await testSetup.mediaService.saveFile(
|
||||
const upload = await testSetup.mediaService.saveFile(
|
||||
testImage,
|
||||
user,
|
||||
note,
|
||||
|
@ -182,7 +182,7 @@ describe('Notes', () => {
|
|||
await testSetup.mediaService.listUploadsByUser(user),
|
||||
).toHaveLength(1);
|
||||
// Remove /upload/ from path as we just need the filename.
|
||||
const fileName = url.replace('/uploads/', '');
|
||||
const fileName = upload.fileUrl.replace('/uploads/', '');
|
||||
// delete the file afterwards
|
||||
await fs.unlink(join(uploadPath, fileName));
|
||||
await fs.rmdir(uploadPath);
|
||||
|
@ -308,12 +308,12 @@ describe('Notes', () => {
|
|||
expect(response.body).toHaveLength(0);
|
||||
|
||||
const testImage = await fs.readFile('test/private-api/fixtures/test.png');
|
||||
const url0 = await testSetup.mediaService.saveFile(
|
||||
const upload0 = await testSetup.mediaService.saveFile(
|
||||
testImage,
|
||||
user,
|
||||
note1,
|
||||
);
|
||||
const url1 = await testSetup.mediaService.saveFile(
|
||||
const upload1 = await testSetup.mediaService.saveFile(
|
||||
testImage,
|
||||
user,
|
||||
note2,
|
||||
|
@ -324,10 +324,10 @@ describe('Notes', () => {
|
|||
.expect('Content-Type', /json/)
|
||||
.expect(200);
|
||||
expect(responseAfter.body).toHaveLength(1);
|
||||
expect(responseAfter.body[0].url).toEqual(url0);
|
||||
expect(responseAfter.body[0].url).not.toEqual(url1);
|
||||
for (const fileUrl of [url0, url1]) {
|
||||
const fileName = fileUrl.replace('/uploads/', '');
|
||||
expect(responseAfter.body[0].url).toEqual(upload0.fileUrl);
|
||||
expect(responseAfter.body[0].url).not.toEqual(upload1.fileUrl);
|
||||
for (const upload of [upload0, upload1]) {
|
||||
const fileName = upload.fileUrl.replace('/uploads/', '');
|
||||
// delete the file afterwards
|
||||
await fs.unlink(join(uploadPath, fileName));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue