Media E2E Tests: Delete uploaded files after test

Remove uploaded files after media e2e tests ran
Remove /uploads/ folder after all media e2e tests ran
This way the uploads folder doesn't grow while working on other e2e tests

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-02-13 22:58:44 +01:00 committed by David Mehren
parent 3ebea8ed77
commit 0366cb491f
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { ConfigModule } from '@nestjs/config'; import { ConfigModule, ConfigService } from '@nestjs/config';
import { NestExpressApplication } from '@nestjs/platform-express'; import { NestExpressApplication } from '@nestjs/platform-express';
import { Test } from '@nestjs/testing'; import { Test } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
@ -23,10 +23,12 @@ import { PermissionsModule } from '../../src/permissions/permissions.module';
import { AuthModule } from '../../src/auth/auth.module'; import { AuthModule } from '../../src/auth/auth.module';
import { TokenAuthGuard } from '../../src/auth/token-auth.guard'; import { TokenAuthGuard } from '../../src/auth/token-auth.guard';
import { MockAuthGuard } from '../../src/auth/mock-auth.guard'; import { MockAuthGuard } from '../../src/auth/mock-auth.guard';
import { join } from 'path';
describe('Notes', () => { describe('Notes', () => {
let app: NestExpressApplication; let app: NestExpressApplication;
let mediaService: MediaService; let mediaService: MediaService;
let uploadPath: string;
beforeAll(async () => { beforeAll(async () => {
const moduleRef = await Test.createTestingModule({ const moduleRef = await Test.createTestingModule({
@ -54,8 +56,10 @@ describe('Notes', () => {
.overrideGuard(TokenAuthGuard) .overrideGuard(TokenAuthGuard)
.useClass(MockAuthGuard) .useClass(MockAuthGuard)
.compile(); .compile();
const config = moduleRef.get<ConfigService>(ConfigService);
uploadPath = config.get('mediaConfig').backend.filesystem.uploadPath;
app = moduleRef.createNestApplication<NestExpressApplication>(); app = moduleRef.createNestApplication<NestExpressApplication>();
app.useStaticAssets('uploads', { app.useStaticAssets(uploadPath, {
prefix: '/uploads', prefix: '/uploads',
}); });
await app.init(); await app.init();
@ -78,6 +82,10 @@ describe('Notes', () => {
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(app.getHttpServer()).get(path);
expect(downloadResponse.body).toEqual(testImage); expect(downloadResponse.body).toEqual(testImage);
// 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));
}); });
it('DELETE /media/{filename}', async () => { it('DELETE /media/{filename}', async () => {
@ -92,4 +100,9 @@ describe('Notes', () => {
.delete('/media/' + filename) .delete('/media/' + filename)
.expect(200); .expect(200);
}); });
afterAll(async () => {
// Delete the upload folder
await fs.rmdir(uploadPath);
});
}); });