diff --git a/.gitignore b/.gitignore index f8aa626e6..99a0704cb 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,4 @@ dist public/uploads/* !public/uploads/.gitkeep uploads +test_uploads diff --git a/jest-e2e.json b/jest-e2e.json index 20f0c2312..7594b2068 100644 --- a/jest-e2e.json +++ b/jest-e2e.json @@ -13,6 +13,5 @@ "^.+\\.(t|j)s$": "ts-jest" }, "coverageDirectory": "./coverage-e2e", - "testTimeout": 10000, - "maxConcurrency": 1 + "testTimeout": 10000 } diff --git a/package.json b/package.json index 69f049786..e50f26b7d 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,8 @@ "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", - "test:e2e": "jest --config jest-e2e.json", - "test:e2e:cov": "jest --config jest-e2e.json --coverage" + "test:e2e": "jest --config jest-e2e.json --runInBand", + "test:e2e:cov": "jest --config jest-e2e.json --coverage --runInBand" }, "dependencies": { "@azure/storage-blob": "12.5.0", diff --git a/src/config/mock/media.config.mock.ts b/src/config/mock/media.config.mock.ts index 3f729d34c..175253e33 100644 --- a/src/config/mock/media.config.mock.ts +++ b/src/config/mock/media.config.mock.ts @@ -10,7 +10,7 @@ export default registerAs('mediaConfig', () => ({ backend: { use: 'filesystem', filesystem: { - uploadPath: 'uploads', + uploadPath: 'test_uploads', }, }, })); diff --git a/src/media/backends/filesystem-backend.ts b/src/media/backends/filesystem-backend.ts index cf9ce7cce..42924d288 100644 --- a/src/media/backends/filesystem-backend.ts +++ b/src/media/backends/filesystem-backend.ts @@ -36,7 +36,7 @@ export class FilesystemBackend implements MediaBackend { await this.ensureDirectory(); try { await fs.writeFile(filePath, buffer, null); - return ['/' + filePath, null]; + return ['/uploads/' + fileName, null]; } catch (e) { this.logger.error((e as Error).message, (e as Error).stack, 'saveFile'); throw new MediaBackendError(`Could not save '${filePath}'`); diff --git a/test/private-api/media.e2e-spec.ts b/test/private-api/media.e2e-spec.ts index 2fc5f7c9a..c6c6e17ce 100644 --- a/test/private-api/media.e2e-spec.ts +++ b/test/private-api/media.e2e-spec.ts @@ -93,19 +93,22 @@ describe('Media', () => { const testImage = await fs.readFile('test/private-api/fixtures/test.png'); const downloadResponse = await request(app.getHttpServer()).get(path); expect(downloadResponse.body).toEqual(testImage); - // Remove /upload/ from path as we just need the filename. + // Remove /uploads/ from path as we just need the filename. const fileName = path.replace('/uploads/', ''); // delete the file afterwards await fs.unlink(join(uploadPath, fileName)); }); describe('fails:', () => { + beforeEach(async () => { + await fs.rmdir(uploadPath, { recursive: true }); + }); it('MIME type not supported', async () => { await request(app.getHttpServer()) .post('/media') .attach('file', 'test/private-api/fixtures/test.zip') .set('HedgeDoc-Note', 'test_upload_media') .expect(400); - expect(await fs.access(uploadPath)).toBeFalsy(); + await expect(fs.access(uploadPath)).rejects.toBeDefined(); }); it('note does not exist', async () => { await request(app.getHttpServer()) @@ -113,10 +116,9 @@ describe('Media', () => { .attach('file', 'test/private-api/fixtures/test.zip') .set('HedgeDoc-Note', 'i_dont_exist') .expect(400); - expect(await fs.access(uploadPath)).toBeFalsy(); + await expect(fs.access(uploadPath)).rejects.toBeDefined(); }); it('mediaBackend error', async () => { - await fs.rmdir(uploadPath); await fs.mkdir(uploadPath, { mode: '444', }); @@ -126,13 +128,16 @@ describe('Media', () => { .set('HedgeDoc-Note', 'test_upload_media') .expect('Content-Type', /json/) .expect(500); - await fs.rmdir(uploadPath); + }); + afterEach(async () => { + await fs.rmdir(uploadPath, { recursive: true }); }); }); }); afterAll(async () => { // Delete the upload folder - await fs.rmdir(uploadPath); + await fs.rmdir(uploadPath, { recursive: true }); + await app.close(); }); }); diff --git a/test/private-api/notes.e2e-spec.ts b/test/private-api/notes.e2e-spec.ts index 7e5806755..0b6b8f6f0 100644 --- a/test/private-api/notes.e2e-spec.ts +++ b/test/private-api/notes.e2e-spec.ts @@ -63,7 +63,7 @@ describe('Notes', () => { GroupsModule, TypeOrmModule.forRoot({ type: 'sqlite', - database: './hedgedoc-e2e-notes.sqlite', + database: './hedgedoc-e2e-private-notes.sqlite', autoLoadEntities: true, synchronize: true, dropSchema: true, @@ -236,7 +236,7 @@ describe('Notes', () => { .expect(200); expect(response.body).toHaveLength(0); - const testImage = await fs.readFile('test/public-api/fixtures/test.png'); + const testImage = await fs.readFile('test/private-api/fixtures/test.png'); const url0 = await mediaService.saveFile(testImage, 'hardcoded', note.id); const url1 = await mediaService.saveFile( testImage, @@ -256,7 +256,7 @@ describe('Notes', () => { // delete the file afterwards await fs.unlink(join(uploadPath, fileName)); } - await fs.rmdir(uploadPath); + await fs.rmdir(uploadPath, { recursive: true }); }); it('fails, when note does not exist', async () => { await request(app.getHttpServer()) diff --git a/test/public-api/me.e2e-spec.ts b/test/public-api/me.e2e-spec.ts index 5b13f4b52..76a3161c9 100644 --- a/test/public-api/me.e2e-spec.ts +++ b/test/public-api/me.e2e-spec.ts @@ -270,7 +270,7 @@ describe('Me', () => { // delete the file afterwards await fs.unlink(join(uploadPath, fileName)); } - await fs.rmdir(uploadPath); + await fs.rmdir(uploadPath, { recursive: true }); }); afterAll(async () => { diff --git a/test/public-api/media.e2e-spec.ts b/test/public-api/media.e2e-spec.ts index 97d35eb86..9ae4379d8 100644 --- a/test/public-api/media.e2e-spec.ts +++ b/test/public-api/media.e2e-spec.ts @@ -89,19 +89,22 @@ describe('Media', () => { const testImage = await fs.readFile('test/public-api/fixtures/test.png'); const downloadResponse = await request(app.getHttpServer()).get(path); expect(downloadResponse.body).toEqual(testImage); - // Remove /upload/ from path as we just need the filename. + // Remove /uploads/ from path as we just need the filename. const fileName = path.replace('/uploads/', ''); // delete the file afterwards await fs.unlink(join(uploadPath, fileName)); }); describe('fails:', () => { + beforeEach(async () => { + await fs.rmdir(uploadPath, { recursive: true }); + }); it('MIME type not supported', async () => { await request(app.getHttpServer()) .post('/media') .attach('file', 'test/public-api/fixtures/test.zip') .set('HedgeDoc-Note', 'test_upload_media') .expect(400); - expect(await fs.access(uploadPath)).toBeFalsy(); + await expect(fs.access(uploadPath)).rejects.toBeDefined(); }); it('note does not exist', async () => { await request(app.getHttpServer()) @@ -109,10 +112,9 @@ describe('Media', () => { .attach('file', 'test/public-api/fixtures/test.zip') .set('HedgeDoc-Note', 'i_dont_exist') .expect(400); - expect(await fs.access(uploadPath)).toBeFalsy(); + await expect(fs.access(uploadPath)).rejects.toBeDefined(); }); it('mediaBackend error', async () => { - await fs.rmdir(uploadPath); await fs.mkdir(uploadPath, { mode: '444', }); @@ -122,7 +124,9 @@ describe('Media', () => { .set('HedgeDoc-Note', 'test_upload_media') .expect('Content-Type', /json/) .expect(500); - await fs.rmdir(uploadPath); + }); + afterEach(async () => { + await fs.rmdir(uploadPath, { recursive: true }); }); }); }); @@ -142,6 +146,7 @@ describe('Media', () => { afterAll(async () => { // Delete the upload folder - await fs.rmdir(uploadPath); + await fs.rmdir(uploadPath, { recursive: true }); + await app.close(); }); }); diff --git a/test/public-api/notes.e2e-spec.ts b/test/public-api/notes.e2e-spec.ts index b86d187fd..b7f2e2e2c 100644 --- a/test/public-api/notes.e2e-spec.ts +++ b/test/public-api/notes.e2e-spec.ts @@ -362,7 +362,7 @@ describe('Notes', () => { // delete the file afterwards await fs.unlink(join(uploadPath, fileName)); } - await fs.rmdir(uploadPath); + await fs.rmdir(uploadPath, { recursive: true }); }); it('fails, when note does not exist', async () => { await request(app.getHttpServer())