mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-16 08:04:45 -04:00
Migrate public media API E2E test to global TestSetup
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
21f4ffe2df
commit
1bc3fbb449
2 changed files with 32 additions and 67 deletions
|
@ -3,89 +3,48 @@
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* 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 { promises as fs } from 'fs';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import request from 'supertest';
|
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 { 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 { 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 { User } from '../../src/users/user.entity';
|
||||||
import { UsersService } from '../../src/users/users.service';
|
import { TestSetup } from '../test-setup';
|
||||||
import { ensureDeleted } from '../utils';
|
import { ensureDeleted } from '../utils';
|
||||||
|
|
||||||
describe('Media', () => {
|
describe('Media', () => {
|
||||||
let app: NestExpressApplication;
|
let testSetup: TestSetup;
|
||||||
let mediaService: MediaService;
|
|
||||||
let uploadPath: string;
|
let uploadPath: string;
|
||||||
let testNote: Note;
|
let testNote: Note;
|
||||||
let user: User;
|
let user: User;
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
const moduleRef = await Test.createTestingModule({
|
testSetup = await TestSetup.create();
|
||||||
imports: [
|
|
||||||
ConfigModule.forRoot({
|
uploadPath =
|
||||||
isGlobal: true,
|
testSetup.configService.get('mediaConfig').backend.filesystem.uploadPath;
|
||||||
load: [mediaConfigMock, appConfigMock],
|
|
||||||
}),
|
testSetup.app.useStaticAssets(uploadPath, {
|
||||||
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, {
|
|
||||||
prefix: '/uploads',
|
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');
|
logger.log('Switching logger', 'AppBootstrap');
|
||||||
app.useLogger(logger);
|
testSetup.app.useLogger(logger);
|
||||||
const notesService: NotesService = moduleRef.get(NotesService);
|
|
||||||
const userService = moduleRef.get(UsersService);
|
user = await testSetup.userService.createUser('hardcoded', 'Testy');
|
||||||
user = await userService.createUser('hardcoded', 'Testy');
|
testNote = await testSetup.notesService.createNote(
|
||||||
testNote = await notesService.createNote(
|
|
||||||
'test content',
|
'test content',
|
||||||
'test_upload_media',
|
'test_upload_media',
|
||||||
);
|
);
|
||||||
mediaService = moduleRef.get(MediaService);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('POST /media', () => {
|
describe('POST /media', () => {
|
||||||
it('works', async () => {
|
it('works', async () => {
|
||||||
const uploadResponse = await request(app.getHttpServer())
|
const uploadResponse = await request(testSetup.app.getHttpServer())
|
||||||
.post('/media')
|
.post('/media')
|
||||||
.attach('file', 'test/public-api/fixtures/test.png')
|
.attach('file', 'test/public-api/fixtures/test.png')
|
||||||
.set('HedgeDoc-Note', 'test_upload_media')
|
.set('HedgeDoc-Note', 'test_upload_media')
|
||||||
|
@ -93,7 +52,9 @@ describe('Media', () => {
|
||||||
.expect(201);
|
.expect(201);
|
||||||
const path: string = uploadResponse.body.link;
|
const path: string = uploadResponse.body.link;
|
||||||
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
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);
|
expect(downloadResponse.body).toEqual(testImage);
|
||||||
// Remove /uploads/ from path as we just need the filename.
|
// Remove /uploads/ from path as we just need the filename.
|
||||||
const fileName = path.replace('/uploads/', '');
|
const fileName = path.replace('/uploads/', '');
|
||||||
|
@ -105,7 +66,7 @@ describe('Media', () => {
|
||||||
await ensureDeleted(uploadPath);
|
await ensureDeleted(uploadPath);
|
||||||
});
|
});
|
||||||
it('MIME type not supported', async () => {
|
it('MIME type not supported', async () => {
|
||||||
await request(app.getHttpServer())
|
await request(testSetup.app.getHttpServer())
|
||||||
.post('/media')
|
.post('/media')
|
||||||
.attach('file', 'test/public-api/fixtures/test.zip')
|
.attach('file', 'test/public-api/fixtures/test.zip')
|
||||||
.set('HedgeDoc-Note', 'test_upload_media')
|
.set('HedgeDoc-Note', 'test_upload_media')
|
||||||
|
@ -113,7 +74,7 @@ describe('Media', () => {
|
||||||
await expect(fs.access(uploadPath)).rejects.toBeDefined();
|
await expect(fs.access(uploadPath)).rejects.toBeDefined();
|
||||||
});
|
});
|
||||||
it('note does not exist', async () => {
|
it('note does not exist', async () => {
|
||||||
await request(app.getHttpServer())
|
await request(testSetup.app.getHttpServer())
|
||||||
.post('/media')
|
.post('/media')
|
||||||
.attach('file', 'test/public-api/fixtures/test.zip')
|
.attach('file', 'test/public-api/fixtures/test.zip')
|
||||||
.set('HedgeDoc-Note', 'i_dont_exist')
|
.set('HedgeDoc-Note', 'i_dont_exist')
|
||||||
|
@ -124,7 +85,7 @@ describe('Media', () => {
|
||||||
await fs.mkdir(uploadPath, {
|
await fs.mkdir(uploadPath, {
|
||||||
mode: '444',
|
mode: '444',
|
||||||
});
|
});
|
||||||
await request(app.getHttpServer())
|
await request(testSetup.app.getHttpServer())
|
||||||
.post('/media')
|
.post('/media')
|
||||||
.attach('file', 'test/public-api/fixtures/test.png')
|
.attach('file', 'test/public-api/fixtures/test.png')
|
||||||
.set('HedgeDoc-Note', 'test_upload_media')
|
.set('HedgeDoc-Note', 'test_upload_media')
|
||||||
|
@ -139,9 +100,13 @@ describe('Media', () => {
|
||||||
|
|
||||||
it('DELETE /media/{filename}', async () => {
|
it('DELETE /media/{filename}', async () => {
|
||||||
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
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() || '';
|
const filename = url.split('/').pop() || '';
|
||||||
await request(app.getHttpServer())
|
await request(testSetup.app.getHttpServer())
|
||||||
.delete('/media/' + filename)
|
.delete('/media/' + filename)
|
||||||
.expect(204);
|
.expect(204);
|
||||||
});
|
});
|
||||||
|
@ -149,6 +114,6 @@ describe('Media', () => {
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
// Delete the upload folder
|
// Delete the upload folder
|
||||||
await ensureDeleted(uploadPath);
|
await ensureDeleted(uploadPath);
|
||||||
await app.close();
|
await testSetup.app.close();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
import { INestApplication } from '@nestjs/common';
|
|
||||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ import { UsersService } from '../src/users/users.service';
|
||||||
|
|
||||||
export class TestSetup {
|
export class TestSetup {
|
||||||
moduleRef: TestingModule;
|
moduleRef: TestingModule;
|
||||||
app: INestApplication;
|
app: NestExpressApplication;
|
||||||
|
|
||||||
userService: UsersService;
|
userService: UsersService;
|
||||||
configService: ConfigService;
|
configService: ConfigService;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue