Migrate public media API E2E test to global TestSetup

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-10-14 21:01:29 +02:00
parent 21f4ffe2df
commit 1bc3fbb449
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 32 additions and 67 deletions

View file

@ -3,89 +3,48 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { ConfigModule, ConfigService } from '@nestjs/config';
import { NestExpressApplication } from '@nestjs/platform-express';
import { Test } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import { promises as fs } from 'fs';
import { join } from 'path';
import request from 'supertest';
import { PublicApiModule } from '../../src/api/public/public-api.module';
import { AuthModule } from '../../src/auth/auth.module';
import { MockAuthGuard } from '../../src/auth/mock-auth.guard';
import { TokenAuthGuard } from '../../src/auth/token.strategy';
import appConfigMock from '../../src/config/mock/app.config.mock';
import mediaConfigMock from '../../src/config/mock/media.config.mock';
import { GroupsModule } from '../../src/groups/groups.module';
import { ConsoleLoggerService } from '../../src/logger/console-logger.service';
import { LoggerModule } from '../../src/logger/logger.module';
import { MediaModule } from '../../src/media/media.module';
import { MediaService } from '../../src/media/media.service';
import { Note } from '../../src/notes/note.entity';
import { NotesModule } from '../../src/notes/notes.module';
import { NotesService } from '../../src/notes/notes.service';
import { PermissionsModule } from '../../src/permissions/permissions.module';
import { User } from '../../src/users/user.entity';
import { UsersService } from '../../src/users/users.service';
import { TestSetup } from '../test-setup';
import { ensureDeleted } from '../utils';
describe('Media', () => {
let app: NestExpressApplication;
let mediaService: MediaService;
let testSetup: TestSetup;
let uploadPath: string;
let testNote: Note;
let user: User;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [mediaConfigMock, appConfigMock],
}),
PublicApiModule,
MediaModule,
TypeOrmModule.forRoot({
type: 'sqlite',
database: './hedgedoc-e2e-media.sqlite',
autoLoadEntities: true,
dropSchema: true,
synchronize: true,
}),
NotesModule,
PermissionsModule,
GroupsModule,
LoggerModule,
AuthModule,
],
})
.overrideGuard(TokenAuthGuard)
.useClass(MockAuthGuard)
.compile();
const config = moduleRef.get<ConfigService>(ConfigService);
uploadPath = config.get('mediaConfig').backend.filesystem.uploadPath;
app = moduleRef.createNestApplication<NestExpressApplication>();
app.useStaticAssets(uploadPath, {
testSetup = await TestSetup.create();
uploadPath =
testSetup.configService.get('mediaConfig').backend.filesystem.uploadPath;
testSetup.app.useStaticAssets(uploadPath, {
prefix: '/uploads',
});
await app.init();
const logger = await app.resolve(ConsoleLoggerService);
await testSetup.app.init();
const logger = await testSetup.app.resolve(ConsoleLoggerService);
logger.log('Switching logger', 'AppBootstrap');
app.useLogger(logger);
const notesService: NotesService = moduleRef.get(NotesService);
const userService = moduleRef.get(UsersService);
user = await userService.createUser('hardcoded', 'Testy');
testNote = await notesService.createNote(
testSetup.app.useLogger(logger);
user = await testSetup.userService.createUser('hardcoded', 'Testy');
testNote = await testSetup.notesService.createNote(
'test content',
'test_upload_media',
);
mediaService = moduleRef.get(MediaService);
});
describe('POST /media', () => {
it('works', async () => {
const uploadResponse = await request(app.getHttpServer())
const uploadResponse = await request(testSetup.app.getHttpServer())
.post('/media')
.attach('file', 'test/public-api/fixtures/test.png')
.set('HedgeDoc-Note', 'test_upload_media')
@ -93,7 +52,9 @@ describe('Media', () => {
.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);
const downloadResponse = await request(testSetup.app.getHttpServer()).get(
path,
);
expect(downloadResponse.body).toEqual(testImage);
// Remove /uploads/ from path as we just need the filename.
const fileName = path.replace('/uploads/', '');
@ -105,7 +66,7 @@ describe('Media', () => {
await ensureDeleted(uploadPath);
});
it('MIME type not supported', async () => {
await request(app.getHttpServer())
await request(testSetup.app.getHttpServer())
.post('/media')
.attach('file', 'test/public-api/fixtures/test.zip')
.set('HedgeDoc-Note', 'test_upload_media')
@ -113,7 +74,7 @@ describe('Media', () => {
await expect(fs.access(uploadPath)).rejects.toBeDefined();
});
it('note does not exist', async () => {
await request(app.getHttpServer())
await request(testSetup.app.getHttpServer())
.post('/media')
.attach('file', 'test/public-api/fixtures/test.zip')
.set('HedgeDoc-Note', 'i_dont_exist')
@ -124,7 +85,7 @@ describe('Media', () => {
await fs.mkdir(uploadPath, {
mode: '444',
});
await request(app.getHttpServer())
await request(testSetup.app.getHttpServer())
.post('/media')
.attach('file', 'test/public-api/fixtures/test.png')
.set('HedgeDoc-Note', 'test_upload_media')
@ -139,9 +100,13 @@ describe('Media', () => {
it('DELETE /media/{filename}', async () => {
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
const url = await mediaService.saveFile(testImage, user, testNote);
const url = await testSetup.mediaService.saveFile(
testImage,
user,
testNote,
);
const filename = url.split('/').pop() || '';
await request(app.getHttpServer())
await request(testSetup.app.getHttpServer())
.delete('/media/' + filename)
.expect(204);
});
@ -149,6 +114,6 @@ describe('Media', () => {
afterAll(async () => {
// Delete the upload folder
await ensureDeleted(uploadPath);
await app.close();
await testSetup.app.close();
});
});

View file

@ -3,8 +3,8 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { INestApplication } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { NestExpressApplication } from '@nestjs/platform-express';
import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
@ -32,7 +32,7 @@ import { UsersService } from '../src/users/users.service';
export class TestSetup {
moduleRef: TestingModule;
app: INestApplication;
app: NestExpressApplication;
userService: UsersService;
configService: ConfigService;