From 9874426c9af9c20b71bd18b2e9220e7fe44cbd0d Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Wed, 24 Mar 2021 11:18:26 +0100 Subject: [PATCH] PublicE2E: Remove uploaded files after test This way the tests can run in any order as some of the media tests rely on an empty (or non-existing) uploads directory. Also the me e2e test was renamed to the correct name. Signed-off-by: Philip Molares --- test/public-api/me.e2e-spec.ts | 14 ++++++++++++-- test/public-api/notes.e2e-spec.ts | 9 +++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/test/public-api/me.e2e-spec.ts b/test/public-api/me.e2e-spec.ts index dda94556e..5b13f4b52 100644 --- a/test/public-api/me.e2e-spec.ts +++ b/test/public-api/me.e2e-spec.ts @@ -29,7 +29,7 @@ import { LoggerModule } from '../../src/logger/logger.module'; import { AuthModule } from '../../src/auth/auth.module'; import { UsersModule } from '../../src/users/users.module'; import { HistoryModule } from '../../src/history/history.module'; -import { ConfigModule } from '@nestjs/config'; +import { ConfigModule, ConfigService } from '@nestjs/config'; import mediaConfigMock from '../../src/config/mock/media.config.mock'; import appConfigMock from '../../src/config/mock/app.config.mock'; import { User } from '../../src/users/user.entity'; @@ -37,15 +37,17 @@ import { MediaService } from '../../src/media/media.service'; import { MediaModule } from '../../src/media/media.module'; import { promises as fs } from 'fs'; import { NoteMetadataDto } from '../../src/notes/note-metadata.dto'; +import { join } from 'path'; // TODO Tests have to be reworked using UserService functions -describe('Notes', () => { +describe('Me', () => { let app: INestApplication; let historyService: HistoryService; let notesService: NotesService; let userService: UsersService; let mediaService: MediaService; + let uploadPath: string; let user: User; beforeAll(async () => { @@ -76,6 +78,8 @@ describe('Notes', () => { .overrideGuard(TokenAuthGuard) .useClass(MockAuthGuard) .compile(); + const config = moduleRef.get(ConfigService); + uploadPath = config.get('mediaConfig').backend.filesystem.uploadPath; app = moduleRef.createNestApplication(); notesService = moduleRef.get(NotesService); historyService = moduleRef.get(HistoryService); @@ -261,6 +265,12 @@ describe('Notes', () => { expect(response.body[1].url).toEqual(url1); expect(response.body[2].url).toEqual(url2); expect(response.body[3].url).toEqual(url3); + for (const fileUrl of [url0, url1, url2, url3]) { + const fileName = fileUrl.replace('/uploads/', ''); + // delete the file afterwards + await fs.unlink(join(uploadPath, fileName)); + } + await fs.rmdir(uploadPath); }); afterAll(async () => { diff --git a/test/public-api/notes.e2e-spec.ts b/test/public-api/notes.e2e-spec.ts index f61374f1b..b86d187fd 100644 --- a/test/public-api/notes.e2e-spec.ts +++ b/test/public-api/notes.e2e-spec.ts @@ -31,6 +31,7 @@ import { User } from '../../src/users/user.entity'; import { UsersModule } from '../../src/users/users.module'; import { promises as fs } from 'fs'; import { MediaService } from '../../src/media/media.service'; +import { join } from 'path'; describe('Notes', () => { let app: INestApplication; @@ -40,6 +41,7 @@ describe('Notes', () => { let user2: User; let content: string; let forbiddenNoteId: string; + let uploadPath: string; beforeAll(async () => { const moduleRef = await Test.createTestingModule({ @@ -70,6 +72,7 @@ describe('Notes', () => { const config = moduleRef.get(ConfigService); forbiddenNoteId = config.get('appConfig').forbiddenNoteIds[0]; + uploadPath = config.get('mediaConfig').backend.filesystem.uploadPath; app = moduleRef.createNestApplication(); await app.init(); notesService = moduleRef.get(NotesService); @@ -354,6 +357,12 @@ describe('Notes', () => { expect(responseAfter.body).toHaveLength(1); expect(responseAfter.body[0].url).toEqual(url0); expect(responseAfter.body[0].url).not.toEqual(url1); + for (const fileUrl of [url0, url1]) { + const fileName = fileUrl.replace('/uploads/', ''); + // delete the file afterwards + await fs.unlink(join(uploadPath, fileName)); + } + await fs.rmdir(uploadPath); }); it('fails, when note does not exist', async () => { await request(app.getHttpServer())