2021-01-05 16:12:38 -05:00
|
|
|
/*
|
2021-01-06 15:36:07 -05:00
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
2021-01-05 16:12:38 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2020-10-24 05:50:45 -04:00
|
|
|
import { promises as fs } from 'fs';
|
2021-08-29 12:45:46 -04:00
|
|
|
import { join } from 'path';
|
2021-04-29 16:47:18 -04:00
|
|
|
import request from 'supertest';
|
2021-08-29 12:45:46 -04:00
|
|
|
|
|
|
|
import { ConsoleLoggerService } from '../../src/logger/console-logger.service';
|
2021-08-29 16:28:21 -04:00
|
|
|
import { Note } from '../../src/notes/note.entity';
|
|
|
|
import { User } from '../../src/users/user.entity';
|
2021-10-14 15:01:29 -04:00
|
|
|
import { TestSetup } from '../test-setup';
|
2021-04-22 15:29:23 -04:00
|
|
|
import { ensureDeleted } from '../utils';
|
2020-10-24 05:50:45 -04:00
|
|
|
|
2021-03-24 06:24:39 -04:00
|
|
|
describe('Media', () => {
|
2021-10-14 15:01:29 -04:00
|
|
|
let testSetup: TestSetup;
|
2021-02-13 16:58:44 -05:00
|
|
|
let uploadPath: string;
|
2021-08-29 16:28:21 -04:00
|
|
|
let testNote: Note;
|
|
|
|
let user: User;
|
2020-10-24 05:50:45 -04:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2021-10-14 15:01:29 -04:00
|
|
|
testSetup = await TestSetup.create();
|
|
|
|
|
|
|
|
uploadPath =
|
|
|
|
testSetup.configService.get('mediaConfig').backend.filesystem.uploadPath;
|
|
|
|
|
|
|
|
testSetup.app.useStaticAssets(uploadPath, {
|
2020-10-24 05:50:45 -04:00
|
|
|
prefix: '/uploads',
|
|
|
|
});
|
2021-10-14 15:01:29 -04:00
|
|
|
|
|
|
|
await testSetup.app.init();
|
|
|
|
|
|
|
|
const logger = await testSetup.app.resolve(ConsoleLoggerService);
|
2020-10-24 05:50:45 -04:00
|
|
|
logger.log('Switching logger', 'AppBootstrap');
|
2021-10-14 15:01:29 -04:00
|
|
|
testSetup.app.useLogger(logger);
|
|
|
|
|
|
|
|
user = await testSetup.userService.createUser('hardcoded', 'Testy');
|
|
|
|
testNote = await testSetup.notesService.createNote(
|
2021-08-29 16:28:21 -04:00
|
|
|
'test content',
|
|
|
|
'test_upload_media',
|
|
|
|
);
|
2020-10-24 05:50:45 -04:00
|
|
|
});
|
|
|
|
|
2021-03-24 06:24:39 -04:00
|
|
|
describe('POST /media', () => {
|
|
|
|
it('works', async () => {
|
2021-10-14 15:01:29 -04:00
|
|
|
const uploadResponse = await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.post('/api/v2/media')
|
2021-03-24 06:24:39 -04:00
|
|
|
.attach('file', 'test/public-api/fixtures/test.png')
|
|
|
|
.set('HedgeDoc-Note', 'test_upload_media')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201);
|
|
|
|
const path: string = uploadResponse.body.link;
|
|
|
|
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
2021-10-14 15:01:29 -04:00
|
|
|
const downloadResponse = await request(testSetup.app.getHttpServer()).get(
|
|
|
|
path,
|
|
|
|
);
|
2021-03-24 06:24:39 -04:00
|
|
|
expect(downloadResponse.body).toEqual(testImage);
|
2021-03-31 16:43:13 -04:00
|
|
|
// Remove /uploads/ from path as we just need the filename.
|
2021-03-24 06:24:39 -04:00
|
|
|
const fileName = path.replace('/uploads/', '');
|
|
|
|
// delete the file afterwards
|
|
|
|
await fs.unlink(join(uploadPath, fileName));
|
|
|
|
});
|
|
|
|
describe('fails:', () => {
|
2021-03-31 16:41:38 -04:00
|
|
|
beforeEach(async () => {
|
2021-04-22 15:29:23 -04:00
|
|
|
await ensureDeleted(uploadPath);
|
2021-03-31 16:41:38 -04:00
|
|
|
});
|
2021-03-24 06:24:39 -04:00
|
|
|
it('MIME type not supported', async () => {
|
2021-10-14 15:01:29 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.post('/api/v2/media')
|
2021-03-24 06:24:39 -04:00
|
|
|
.attach('file', 'test/public-api/fixtures/test.zip')
|
|
|
|
.set('HedgeDoc-Note', 'test_upload_media')
|
|
|
|
.expect(400);
|
2021-03-31 16:41:38 -04:00
|
|
|
await expect(fs.access(uploadPath)).rejects.toBeDefined();
|
2021-03-24 06:24:39 -04:00
|
|
|
});
|
|
|
|
it('note does not exist', async () => {
|
2021-10-14 15:01:29 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.post('/api/v2/media')
|
2021-03-24 06:24:39 -04:00
|
|
|
.attach('file', 'test/public-api/fixtures/test.zip')
|
|
|
|
.set('HedgeDoc-Note', 'i_dont_exist')
|
|
|
|
.expect(400);
|
2021-03-31 16:41:38 -04:00
|
|
|
await expect(fs.access(uploadPath)).rejects.toBeDefined();
|
2021-03-24 06:24:39 -04:00
|
|
|
});
|
|
|
|
it('mediaBackend error', async () => {
|
|
|
|
await fs.mkdir(uploadPath, {
|
|
|
|
mode: '444',
|
|
|
|
});
|
2021-10-14 15:01:29 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.post('/api/v2/media')
|
2021-03-24 06:24:39 -04:00
|
|
|
.attach('file', 'test/public-api/fixtures/test.png')
|
|
|
|
.set('HedgeDoc-Note', 'test_upload_media')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(500);
|
2021-03-31 16:41:38 -04:00
|
|
|
});
|
|
|
|
afterEach(async () => {
|
2021-04-22 15:29:23 -04:00
|
|
|
await ensureDeleted(uploadPath);
|
2021-03-24 06:24:39 -04:00
|
|
|
});
|
|
|
|
});
|
2020-10-24 05:50:45 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('DELETE /media/{filename}', async () => {
|
|
|
|
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
2021-10-14 15:01:29 -04:00
|
|
|
const url = await testSetup.mediaService.saveFile(
|
|
|
|
testImage,
|
|
|
|
user,
|
|
|
|
testNote,
|
|
|
|
);
|
2021-04-29 16:06:45 -04:00
|
|
|
const filename = url.split('/').pop() || '';
|
2021-10-14 15:01:29 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.delete('/api/v2/media/' + filename)
|
2021-03-19 07:10:46 -04:00
|
|
|
.expect(204);
|
2020-10-24 05:50:45 -04:00
|
|
|
});
|
2021-02-13 16:58:44 -05:00
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
// Delete the upload folder
|
2021-04-22 15:29:23 -04:00
|
|
|
await ensureDeleted(uploadPath);
|
2021-10-14 15:01:29 -04:00
|
|
|
await testSetup.app.close();
|
2021-02-13 16:58:44 -05:00
|
|
|
});
|
2020-10-24 05:50:45 -04:00
|
|
|
});
|