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
|
|
|
|
*/
|
|
|
|
|
2021-02-13 16:58:44 -05:00
|
|
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
2020-10-24 05:50:45 -04:00
|
|
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
|
|
|
import { Test } from '@nestjs/testing';
|
|
|
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
|
|
import { promises as fs } from 'fs';
|
2021-04-29 16:47:18 -04:00
|
|
|
import request from 'supertest';
|
2020-10-24 05:50:45 -04:00
|
|
|
import { PublicApiModule } from '../../src/api/public/public-api.module';
|
2021-03-01 15:12:01 -05:00
|
|
|
import mediaConfigMock from '../../src/config/mock/media.config.mock';
|
|
|
|
import appConfigMock from '../../src/config/mock/app.config.mock';
|
2020-10-24 05:50:45 -04:00
|
|
|
import { GroupsModule } from '../../src/groups/groups.module';
|
|
|
|
import { LoggerModule } from '../../src/logger/logger.module';
|
|
|
|
import { MediaModule } from '../../src/media/media.module';
|
|
|
|
import { MediaService } from '../../src/media/media.service';
|
|
|
|
import { NotesModule } from '../../src/notes/notes.module';
|
|
|
|
import { NotesService } from '../../src/notes/notes.service';
|
|
|
|
import { PermissionsModule } from '../../src/permissions/permissions.module';
|
2021-01-16 13:33:09 -05:00
|
|
|
import { AuthModule } from '../../src/auth/auth.module';
|
|
|
|
import { TokenAuthGuard } from '../../src/auth/token-auth.guard';
|
|
|
|
import { MockAuthGuard } from '../../src/auth/mock-auth.guard';
|
2021-02-13 16:58:44 -05:00
|
|
|
import { join } from 'path';
|
2021-04-02 13:08:30 -04:00
|
|
|
import { ConsoleLoggerService } from '../../src/logger/console-logger.service';
|
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', () => {
|
2020-10-24 05:50:45 -04:00
|
|
|
let app: NestExpressApplication;
|
|
|
|
let mediaService: MediaService;
|
2021-02-13 16:58:44 -05:00
|
|
|
let uploadPath: string;
|
2020-10-24 05:50:45 -04:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
const moduleRef = await Test.createTestingModule({
|
|
|
|
imports: [
|
2021-01-08 06:53:16 -05:00
|
|
|
ConfigModule.forRoot({
|
|
|
|
isGlobal: true,
|
2021-02-20 16:15:04 -05:00
|
|
|
load: [mediaConfigMock, appConfigMock],
|
2021-01-08 06:53:16 -05:00
|
|
|
}),
|
2020-10-24 05:50:45 -04:00
|
|
|
PublicApiModule,
|
|
|
|
MediaModule,
|
|
|
|
TypeOrmModule.forRoot({
|
|
|
|
type: 'sqlite',
|
2020-10-24 06:30:23 -04:00
|
|
|
database: './hedgedoc-e2e-media.sqlite',
|
2020-10-24 05:50:45 -04:00
|
|
|
autoLoadEntities: true,
|
|
|
|
dropSchema: true,
|
|
|
|
synchronize: true,
|
|
|
|
}),
|
|
|
|
NotesModule,
|
|
|
|
PermissionsModule,
|
|
|
|
GroupsModule,
|
|
|
|
LoggerModule,
|
2021-01-16 13:33:09 -05:00
|
|
|
AuthModule,
|
2020-10-24 05:50:45 -04:00
|
|
|
],
|
2021-01-16 13:33:09 -05:00
|
|
|
})
|
|
|
|
.overrideGuard(TokenAuthGuard)
|
|
|
|
.useClass(MockAuthGuard)
|
|
|
|
.compile();
|
2021-02-13 16:58:44 -05:00
|
|
|
const config = moduleRef.get<ConfigService>(ConfigService);
|
|
|
|
uploadPath = config.get('mediaConfig').backend.filesystem.uploadPath;
|
2020-10-24 05:50:45 -04:00
|
|
|
app = moduleRef.createNestApplication<NestExpressApplication>();
|
2021-02-13 16:58:44 -05:00
|
|
|
app.useStaticAssets(uploadPath, {
|
2020-10-24 05:50:45 -04:00
|
|
|
prefix: '/uploads',
|
|
|
|
});
|
|
|
|
await app.init();
|
2021-04-02 13:08:30 -04:00
|
|
|
const logger = await app.resolve(ConsoleLoggerService);
|
2020-10-24 05:50:45 -04:00
|
|
|
logger.log('Switching logger', 'AppBootstrap');
|
|
|
|
app.useLogger(logger);
|
|
|
|
const notesService: NotesService = moduleRef.get('NotesService');
|
|
|
|
await notesService.createNote('test content', 'test_upload_media');
|
|
|
|
mediaService = moduleRef.get('MediaService');
|
|
|
|
});
|
|
|
|
|
2021-03-24 06:24:39 -04:00
|
|
|
describe('POST /media', () => {
|
|
|
|
it('works', async () => {
|
|
|
|
const uploadResponse = await request(app.getHttpServer())
|
|
|
|
.post('/media')
|
|
|
|
.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');
|
|
|
|
const downloadResponse = await request(app.getHttpServer()).get(path);
|
|
|
|
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 () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.post('/media')
|
|
|
|
.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 () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.post('/media')
|
|
|
|
.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',
|
|
|
|
});
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.post('/media')
|
|
|
|
.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');
|
|
|
|
const url = await mediaService.saveFile(
|
|
|
|
testImage,
|
|
|
|
'hardcoded',
|
|
|
|
'test_upload_media',
|
|
|
|
);
|
2021-04-29 16:06:45 -04:00
|
|
|
const filename = url.split('/').pop() || '';
|
2020-10-24 05:50:45 -04:00
|
|
|
await request(app.getHttpServer())
|
|
|
|
.delete('/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-03-31 16:36:14 -04:00
|
|
|
await app.close();
|
2021-02-13 16:58:44 -05:00
|
|
|
});
|
2020-10-24 05:50:45 -04:00
|
|
|
});
|