2021-01-05 21:12:38 +00:00
|
|
|
/*
|
2021-01-06 20:36:07 +00:00
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
2021-01-05 21:12:38 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2021-02-13 21:58:44 +00:00
|
|
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
2020-10-24 09:50:45 +00:00
|
|
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
|
|
|
import { Test } from '@nestjs/testing';
|
|
|
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
|
|
import { promises as fs } from 'fs';
|
|
|
|
import * as request from 'supertest';
|
|
|
|
import { PublicApiModule } from '../../src/api/public/public-api.module';
|
2021-01-15 15:57:04 +00:00
|
|
|
import mediaConfigMock from '../../src/config/media.config.mock';
|
2020-10-24 09:50:45 +00:00
|
|
|
import { GroupsModule } from '../../src/groups/groups.module';
|
|
|
|
import { LoggerModule } from '../../src/logger/logger.module';
|
|
|
|
import { NestConsoleLoggerService } from '../../src/logger/nest-console-logger.service';
|
|
|
|
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 18:33:09 +00: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 21:58:44 +00:00
|
|
|
import { join } from 'path';
|
2020-10-24 09:50:45 +00:00
|
|
|
|
|
|
|
describe('Notes', () => {
|
|
|
|
let app: NestExpressApplication;
|
|
|
|
let mediaService: MediaService;
|
2021-02-13 21:58:44 +00:00
|
|
|
let uploadPath: string;
|
2020-10-24 09:50:45 +00:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
const moduleRef = await Test.createTestingModule({
|
|
|
|
imports: [
|
2021-01-08 11:53:16 +00:00
|
|
|
ConfigModule.forRoot({
|
|
|
|
isGlobal: true,
|
2021-01-19 11:45:36 +00:00
|
|
|
load: [mediaConfigMock],
|
2021-01-08 11:53:16 +00:00
|
|
|
}),
|
2020-10-24 09:50:45 +00:00
|
|
|
PublicApiModule,
|
|
|
|
MediaModule,
|
|
|
|
TypeOrmModule.forRoot({
|
|
|
|
type: 'sqlite',
|
2020-10-24 10:30:23 +00:00
|
|
|
database: './hedgedoc-e2e-media.sqlite',
|
2020-10-24 09:50:45 +00:00
|
|
|
autoLoadEntities: true,
|
|
|
|
dropSchema: true,
|
|
|
|
synchronize: true,
|
|
|
|
}),
|
|
|
|
NotesModule,
|
|
|
|
PermissionsModule,
|
|
|
|
GroupsModule,
|
|
|
|
LoggerModule,
|
2021-01-16 18:33:09 +00:00
|
|
|
AuthModule,
|
2020-10-24 09:50:45 +00:00
|
|
|
],
|
2021-01-16 18:33:09 +00:00
|
|
|
})
|
|
|
|
.overrideGuard(TokenAuthGuard)
|
|
|
|
.useClass(MockAuthGuard)
|
|
|
|
.compile();
|
2021-02-13 21:58:44 +00:00
|
|
|
const config = moduleRef.get<ConfigService>(ConfigService);
|
|
|
|
uploadPath = config.get('mediaConfig').backend.filesystem.uploadPath;
|
2020-10-24 09:50:45 +00:00
|
|
|
app = moduleRef.createNestApplication<NestExpressApplication>();
|
2021-02-13 21:58:44 +00:00
|
|
|
app.useStaticAssets(uploadPath, {
|
2020-10-24 09:50:45 +00:00
|
|
|
prefix: '/uploads',
|
|
|
|
});
|
|
|
|
await app.init();
|
|
|
|
const logger = await app.resolve(NestConsoleLoggerService);
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('POST /media', 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 = 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-02-13 21:58:44 +00:00
|
|
|
// Remove /upload/ from path as we just need the filename.
|
|
|
|
const fileName = path.replace('/uploads/', '');
|
|
|
|
// delete the file afterwards
|
|
|
|
await fs.unlink(join(uploadPath, fileName));
|
2020-10-24 09:50:45 +00: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',
|
|
|
|
);
|
|
|
|
const filename = url.split('/').pop();
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.delete('/media/' + filename)
|
|
|
|
.expect(200);
|
|
|
|
});
|
2021-02-13 21:58:44 +00:00
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
// Delete the upload folder
|
|
|
|
await fs.rmdir(uploadPath);
|
|
|
|
});
|
2020-10-24 09:50:45 +00:00
|
|
|
});
|