2021-03-23 21:41:57 +00:00
|
|
|
/*
|
2022-09-25 00:05:30 +00:00
|
|
|
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
2021-03-23 21:41:57 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
import { promises as fs } from 'fs';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { join } from 'path';
|
2021-10-14 19:57:10 +00:00
|
|
|
import { User } from 'src/users/user.entity';
|
2021-04-29 20:47:18 +00:00
|
|
|
import request from 'supertest';
|
2021-08-29 16:45:46 +00:00
|
|
|
|
|
|
|
import { ConsoleLoggerService } from '../../src/logger/console-logger.service';
|
2022-12-30 11:48:24 +00:00
|
|
|
import {
|
|
|
|
password1,
|
|
|
|
password2,
|
|
|
|
TestSetup,
|
|
|
|
TestSetupBuilder,
|
|
|
|
username1,
|
|
|
|
username2,
|
|
|
|
} from '../test-setup';
|
2021-04-22 19:29:23 +00:00
|
|
|
import { ensureDeleted } from '../utils';
|
2021-03-23 21:41:57 +00:00
|
|
|
|
|
|
|
describe('Media', () => {
|
2021-10-14 19:57:10 +00:00
|
|
|
let testSetup: TestSetup;
|
|
|
|
|
2021-03-23 21:41:57 +00:00
|
|
|
let uploadPath: string;
|
2021-10-13 18:43:56 +00:00
|
|
|
let agent: request.SuperAgentTest;
|
2021-10-18 18:00:28 +00:00
|
|
|
let user: User;
|
2021-03-23 21:41:57 +00:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2022-06-26 19:22:19 +00:00
|
|
|
testSetup = await TestSetupBuilder.create().withUsers().build();
|
2021-10-14 19:57:10 +00:00
|
|
|
|
|
|
|
uploadPath =
|
|
|
|
testSetup.configService.get('mediaConfig').backend.filesystem.uploadPath;
|
|
|
|
|
|
|
|
testSetup.app.useStaticAssets(uploadPath, {
|
2021-03-23 21:41:57 +00:00
|
|
|
prefix: '/uploads',
|
|
|
|
});
|
2021-10-14 19:57:10 +00:00
|
|
|
await testSetup.app.init();
|
|
|
|
|
|
|
|
const logger = await testSetup.app.resolve(ConsoleLoggerService);
|
2021-03-23 21:41:57 +00:00
|
|
|
logger.log('Switching logger', 'AppBootstrap');
|
2021-10-14 19:57:10 +00:00
|
|
|
testSetup.app.useLogger(logger);
|
|
|
|
|
|
|
|
await testSetup.notesService.createNote(
|
2021-10-18 18:00:28 +00:00
|
|
|
'test content',
|
2021-11-14 20:44:59 +00:00
|
|
|
null,
|
2021-10-18 18:00:28 +00:00
|
|
|
'test_upload_media',
|
|
|
|
);
|
2022-06-26 19:22:19 +00:00
|
|
|
user = testSetup.users[0];
|
2021-10-14 19:57:10 +00:00
|
|
|
|
|
|
|
agent = request.agent(testSetup.app.getHttpServer());
|
2021-10-13 18:43:56 +00:00
|
|
|
await agent
|
2021-10-15 15:06:56 +00:00
|
|
|
.post('/api/private/auth/local/login')
|
2022-12-30 11:48:24 +00:00
|
|
|
.send({ username: username1, password: password1 })
|
2021-10-13 18:43:56 +00:00
|
|
|
.expect(201);
|
2021-03-23 21:41:57 +00:00
|
|
|
});
|
|
|
|
|
2022-03-04 12:13:46 +00:00
|
|
|
afterAll(async () => {
|
|
|
|
// Delete the upload folder
|
|
|
|
await ensureDeleted(uploadPath);
|
|
|
|
await testSetup.app.close();
|
|
|
|
await testSetup.cleanup();
|
|
|
|
});
|
|
|
|
|
2021-03-23 21:41:57 +00:00
|
|
|
describe('POST /media', () => {
|
2023-04-23 19:57:35 +00:00
|
|
|
describe('works', () => {
|
|
|
|
it('with user', async () => {
|
|
|
|
const uploadResponse = await agent
|
|
|
|
.post('/api/private/media')
|
|
|
|
.attach('file', 'test/private-api/fixtures/test.png')
|
|
|
|
.set('HedgeDoc-Note', 'test_upload_media')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201);
|
|
|
|
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);
|
|
|
|
// Remove /uploads/ from path as we just need the filename.
|
|
|
|
const fileName = path.replace('/uploads/', '');
|
|
|
|
// delete the file afterwards
|
|
|
|
await fs.unlink(join(uploadPath, fileName));
|
|
|
|
});
|
|
|
|
it('without user', async () => {
|
|
|
|
const agent = request.agent(testSetup.app.getHttpServer());
|
|
|
|
const uploadResponse = await agent
|
|
|
|
.post('/api/private/media')
|
|
|
|
.attach('file', 'test/private-api/fixtures/test.png')
|
|
|
|
.set('HedgeDoc-Note', 'test_upload_media')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201);
|
|
|
|
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);
|
|
|
|
// Remove /uploads/ from path as we just need the filename.
|
|
|
|
const fileName = path.replace('/uploads/', '');
|
|
|
|
// delete the file afterwards
|
|
|
|
await fs.unlink(join(uploadPath, fileName));
|
|
|
|
});
|
2021-03-23 21:41:57 +00:00
|
|
|
});
|
|
|
|
describe('fails:', () => {
|
2021-03-31 20:41:38 +00:00
|
|
|
beforeEach(async () => {
|
2021-04-22 19:29:23 +00:00
|
|
|
await ensureDeleted(uploadPath);
|
2021-03-31 20:41:38 +00:00
|
|
|
});
|
2021-03-23 21:41:57 +00:00
|
|
|
it('MIME type not supported', async () => {
|
2021-10-13 18:43:56 +00:00
|
|
|
await agent
|
2021-10-15 15:06:56 +00:00
|
|
|
.post('/api/private/media')
|
2021-03-23 21:41:57 +00:00
|
|
|
.attach('file', 'test/private-api/fixtures/test.zip')
|
|
|
|
.set('HedgeDoc-Note', 'test_upload_media')
|
|
|
|
.expect(400);
|
2021-03-31 20:41:38 +00:00
|
|
|
await expect(fs.access(uploadPath)).rejects.toBeDefined();
|
2021-03-23 21:41:57 +00:00
|
|
|
});
|
|
|
|
it('note does not exist', async () => {
|
2021-10-13 18:43:56 +00:00
|
|
|
await agent
|
2021-10-15 15:06:56 +00:00
|
|
|
.post('/api/private/media')
|
2021-03-23 21:41:57 +00:00
|
|
|
.attach('file', 'test/private-api/fixtures/test.zip')
|
|
|
|
.set('HedgeDoc-Note', 'i_dont_exist')
|
2022-01-23 21:23:00 +00:00
|
|
|
.expect(404);
|
2021-03-31 20:41:38 +00:00
|
|
|
await expect(fs.access(uploadPath)).rejects.toBeDefined();
|
2021-03-23 21:41:57 +00:00
|
|
|
});
|
|
|
|
it('mediaBackend error', async () => {
|
|
|
|
await fs.mkdir(uploadPath, {
|
|
|
|
mode: '444',
|
|
|
|
});
|
2021-10-13 18:43:56 +00:00
|
|
|
await agent
|
2021-10-15 15:06:56 +00:00
|
|
|
.post('/api/private/media')
|
2021-03-23 21:41:57 +00:00
|
|
|
.attach('file', 'test/private-api/fixtures/test.png')
|
|
|
|
.set('HedgeDoc-Note', 'test_upload_media')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(500);
|
2021-03-31 20:41:38 +00:00
|
|
|
});
|
2022-12-28 17:07:28 +00:00
|
|
|
it('no media uploaded', async () => {
|
|
|
|
await agent
|
|
|
|
.post('/api/private/media')
|
|
|
|
.set('HedgeDoc-Note', 'test_upload_media')
|
|
|
|
.expect(400);
|
|
|
|
await expect(fs.access(uploadPath)).rejects.toBeDefined();
|
|
|
|
});
|
2021-03-31 20:41:38 +00:00
|
|
|
afterEach(async () => {
|
2021-04-22 19:29:23 +00:00
|
|
|
await ensureDeleted(uploadPath);
|
2021-03-23 21:41:57 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-25 16:47:46 +00:00
|
|
|
describe('DELETE /media/{filename}', () => {
|
|
|
|
it('deleting user is owner of file', async () => {
|
|
|
|
// upload a file with the default test user
|
|
|
|
const testNote = await testSetup.notesService.createNote(
|
|
|
|
'test content',
|
|
|
|
null,
|
|
|
|
'test_delete_media_file',
|
|
|
|
);
|
|
|
|
const testImage = await fs.readFile('test/private-api/fixtures/test.png');
|
|
|
|
const upload = await testSetup.mediaService.saveFile(
|
|
|
|
testImage,
|
|
|
|
user,
|
|
|
|
testNote,
|
|
|
|
);
|
|
|
|
const filename = upload.fileUrl.split('/').pop() || '';
|
2022-06-26 19:22:19 +00:00
|
|
|
|
2023-03-25 16:47:46 +00:00
|
|
|
// login with a different user;
|
|
|
|
const agent2 = request.agent(testSetup.app.getHttpServer());
|
|
|
|
await agent2
|
|
|
|
.post('/api/private/auth/local/login')
|
|
|
|
.send({ username: username2, password: password2 })
|
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
// try to delete upload with second user
|
|
|
|
await agent2.delete('/api/private/media/' + filename).expect(403);
|
2022-06-26 19:22:19 +00:00
|
|
|
|
2023-03-25 16:47:46 +00:00
|
|
|
await agent.get('/uploads/' + filename).expect(200);
|
2022-06-26 19:22:19 +00:00
|
|
|
|
2023-03-25 16:47:46 +00:00
|
|
|
// delete upload for real
|
|
|
|
await agent.delete('/api/private/media/' + filename).expect(204);
|
|
|
|
|
|
|
|
// Test if file is really deleted
|
|
|
|
await agent.get('/uploads/' + filename).expect(404);
|
|
|
|
});
|
|
|
|
it('deleting user is owner of note', async () => {
|
|
|
|
// upload a file with the default test user
|
|
|
|
const testNote = await testSetup.notesService.createNote(
|
|
|
|
'test content',
|
|
|
|
await testSetup.userService.getUserByUsername(username2),
|
|
|
|
'test_delete_media_note',
|
|
|
|
);
|
|
|
|
const testImage = await fs.readFile('test/private-api/fixtures/test.png');
|
|
|
|
const upload = await testSetup.mediaService.saveFile(
|
|
|
|
testImage,
|
|
|
|
null,
|
|
|
|
testNote,
|
|
|
|
);
|
|
|
|
const filename = upload.fileUrl.split('/').pop() || '';
|
|
|
|
|
|
|
|
// login with a different user;
|
|
|
|
const agent2 = request.agent(testSetup.app.getHttpServer());
|
|
|
|
await agent2
|
|
|
|
.post('/api/private/auth/local/login')
|
|
|
|
.send({ username: username2, password: password2 })
|
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
const agentGuest = request.agent(testSetup.app.getHttpServer());
|
|
|
|
|
|
|
|
// try to delete upload with second user
|
|
|
|
await agent.delete('/api/private/media/' + filename).expect(403);
|
|
|
|
|
|
|
|
await agent.get('/uploads/' + filename).expect(200);
|
|
|
|
|
|
|
|
await agentGuest.delete('/api/private/media/' + filename).expect(401);
|
|
|
|
|
|
|
|
await agent.get('/uploads/' + filename).expect(200);
|
|
|
|
// delete upload for real
|
|
|
|
await agent2.delete('/api/private/media/' + filename).expect(204);
|
|
|
|
|
|
|
|
// Test if file is really deleted
|
|
|
|
await agent.get('/uploads/' + filename).expect(404);
|
|
|
|
});
|
2021-10-18 18:00:28 +00:00
|
|
|
});
|
2021-03-23 21:41:57 +00:00
|
|
|
});
|