mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-01-22 07:54:58 +00:00
Add E2E tests for the /media
route
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
ac2646a74a
commit
fff46f0255
2 changed files with 79 additions and 0 deletions
BIN
test/public-api/fixtures/test.png
Normal file
BIN
test/public-api/fixtures/test.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.7 KiB |
79
test/public-api/media.e2e-spec.ts
Normal file
79
test/public-api/media.e2e-spec.ts
Normal file
|
@ -0,0 +1,79 @@
|
|||
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';
|
||||
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';
|
||||
import { UsersService } from '../../src/users/users.service';
|
||||
|
||||
describe('Notes', () => {
|
||||
let app: NestExpressApplication;
|
||||
let mediaService: MediaService;
|
||||
|
||||
beforeAll(async () => {
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
imports: [
|
||||
PublicApiModule,
|
||||
MediaModule,
|
||||
TypeOrmModule.forRoot({
|
||||
type: 'sqlite',
|
||||
database: './hedgedoc-e2e.sqlite',
|
||||
autoLoadEntities: true,
|
||||
dropSchema: true,
|
||||
synchronize: true,
|
||||
}),
|
||||
NotesModule,
|
||||
PermissionsModule,
|
||||
GroupsModule,
|
||||
LoggerModule,
|
||||
],
|
||||
}).compile();
|
||||
app = moduleRef.createNestApplication<NestExpressApplication>();
|
||||
app.useStaticAssets('uploads', {
|
||||
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');
|
||||
const usersService: UsersService = moduleRef.get('UsersService');
|
||||
await usersService.createUser('hardcoded', 'Hard Coded');
|
||||
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);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue