Merge pull request #1092 from hedgedoc/fix/e2eTests

This commit is contained in:
Yannick Bungers 2021-04-08 12:06:43 +02:00 committed by GitHub
commit ee5a587bcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 33 additions and 23 deletions

1
.gitignore vendored
View file

@ -41,3 +41,4 @@ dist
public/uploads/* public/uploads/*
!public/uploads/.gitkeep !public/uploads/.gitkeep
uploads uploads
test_uploads

View file

@ -13,6 +13,5 @@
"^.+\\.(t|j)s$": "ts-jest" "^.+\\.(t|j)s$": "ts-jest"
}, },
"coverageDirectory": "./coverage-e2e", "coverageDirectory": "./coverage-e2e",
"testTimeout": 10000, "testTimeout": 10000
"maxConcurrency": 1
} }

View file

@ -20,8 +20,8 @@
"test:watch": "jest --watch", "test:watch": "jest --watch",
"test:cov": "jest --coverage", "test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", "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": "jest --config jest-e2e.json --runInBand",
"test:e2e:cov": "jest --config jest-e2e.json --coverage" "test:e2e:cov": "jest --config jest-e2e.json --coverage --runInBand"
}, },
"dependencies": { "dependencies": {
"@azure/storage-blob": "12.5.0", "@azure/storage-blob": "12.5.0",

View file

@ -10,7 +10,7 @@ export default registerAs('mediaConfig', () => ({
backend: { backend: {
use: 'filesystem', use: 'filesystem',
filesystem: { filesystem: {
uploadPath: 'uploads', uploadPath: 'test_uploads',
}, },
}, },
})); }));

View file

@ -36,7 +36,7 @@ export class FilesystemBackend implements MediaBackend {
await this.ensureDirectory(); await this.ensureDirectory();
try { try {
await fs.writeFile(filePath, buffer, null); await fs.writeFile(filePath, buffer, null);
return ['/' + filePath, null]; return ['/uploads/' + fileName, null];
} catch (e) { } catch (e) {
this.logger.error((e as Error).message, (e as Error).stack, 'saveFile'); this.logger.error((e as Error).message, (e as Error).stack, 'saveFile');
throw new MediaBackendError(`Could not save '${filePath}'`); throw new MediaBackendError(`Could not save '${filePath}'`);

View file

@ -93,19 +93,22 @@ describe('Media', () => {
const testImage = await fs.readFile('test/private-api/fixtures/test.png'); const testImage = await fs.readFile('test/private-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. // Remove /uploads/ from path as we just need the filename.
const fileName = path.replace('/uploads/', ''); const fileName = path.replace('/uploads/', '');
// delete the file afterwards // delete the file afterwards
await fs.unlink(join(uploadPath, fileName)); await fs.unlink(join(uploadPath, fileName));
}); });
describe('fails:', () => { describe('fails:', () => {
beforeEach(async () => {
await fs.rmdir(uploadPath, { recursive: true });
});
it('MIME type not supported', async () => { it('MIME type not supported', async () => {
await request(app.getHttpServer()) await request(app.getHttpServer())
.post('/media') .post('/media')
.attach('file', 'test/private-api/fixtures/test.zip') .attach('file', 'test/private-api/fixtures/test.zip')
.set('HedgeDoc-Note', 'test_upload_media') .set('HedgeDoc-Note', 'test_upload_media')
.expect(400); .expect(400);
expect(await fs.access(uploadPath)).toBeFalsy(); 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(app.getHttpServer())
@ -113,10 +116,9 @@ describe('Media', () => {
.attach('file', 'test/private-api/fixtures/test.zip') .attach('file', 'test/private-api/fixtures/test.zip')
.set('HedgeDoc-Note', 'i_dont_exist') .set('HedgeDoc-Note', 'i_dont_exist')
.expect(400); .expect(400);
expect(await fs.access(uploadPath)).toBeFalsy(); await expect(fs.access(uploadPath)).rejects.toBeDefined();
}); });
it('mediaBackend error', async () => { it('mediaBackend error', async () => {
await fs.rmdir(uploadPath);
await fs.mkdir(uploadPath, { await fs.mkdir(uploadPath, {
mode: '444', mode: '444',
}); });
@ -126,13 +128,16 @@ describe('Media', () => {
.set('HedgeDoc-Note', 'test_upload_media') .set('HedgeDoc-Note', 'test_upload_media')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(500); .expect(500);
await fs.rmdir(uploadPath); });
afterEach(async () => {
await fs.rmdir(uploadPath, { recursive: true });
}); });
}); });
}); });
afterAll(async () => { afterAll(async () => {
// Delete the upload folder // Delete the upload folder
await fs.rmdir(uploadPath); await fs.rmdir(uploadPath, { recursive: true });
await app.close();
}); });
}); });

View file

@ -63,7 +63,7 @@ describe('Notes', () => {
GroupsModule, GroupsModule,
TypeOrmModule.forRoot({ TypeOrmModule.forRoot({
type: 'sqlite', type: 'sqlite',
database: './hedgedoc-e2e-notes.sqlite', database: './hedgedoc-e2e-private-notes.sqlite',
autoLoadEntities: true, autoLoadEntities: true,
synchronize: true, synchronize: true,
dropSchema: true, dropSchema: true,
@ -236,7 +236,7 @@ describe('Notes', () => {
.expect(200); .expect(200);
expect(response.body).toHaveLength(0); 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 url0 = await mediaService.saveFile(testImage, 'hardcoded', note.id);
const url1 = await mediaService.saveFile( const url1 = await mediaService.saveFile(
testImage, testImage,
@ -256,7 +256,7 @@ describe('Notes', () => {
// delete the file afterwards // delete the file afterwards
await fs.unlink(join(uploadPath, fileName)); await fs.unlink(join(uploadPath, fileName));
} }
await fs.rmdir(uploadPath); await fs.rmdir(uploadPath, { recursive: true });
}); });
it('fails, when note does not exist', async () => { it('fails, when note does not exist', async () => {
await request(app.getHttpServer()) await request(app.getHttpServer())

View file

@ -270,7 +270,7 @@ describe('Me', () => {
// delete the file afterwards // delete the file afterwards
await fs.unlink(join(uploadPath, fileName)); await fs.unlink(join(uploadPath, fileName));
} }
await fs.rmdir(uploadPath); await fs.rmdir(uploadPath, { recursive: true });
}); });
afterAll(async () => { afterAll(async () => {

View file

@ -89,19 +89,22 @@ describe('Media', () => {
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. // Remove /uploads/ from path as we just need the filename.
const fileName = path.replace('/uploads/', ''); const fileName = path.replace('/uploads/', '');
// delete the file afterwards // delete the file afterwards
await fs.unlink(join(uploadPath, fileName)); await fs.unlink(join(uploadPath, fileName));
}); });
describe('fails:', () => { describe('fails:', () => {
beforeEach(async () => {
await fs.rmdir(uploadPath, { recursive: true });
});
it('MIME type not supported', async () => { it('MIME type not supported', async () => {
await request(app.getHttpServer()) await request(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')
.expect(400); .expect(400);
expect(await fs.access(uploadPath)).toBeFalsy(); 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(app.getHttpServer())
@ -109,10 +112,9 @@ describe('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')
.expect(400); .expect(400);
expect(await fs.access(uploadPath)).toBeFalsy(); await expect(fs.access(uploadPath)).rejects.toBeDefined();
}); });
it('mediaBackend error', async () => { it('mediaBackend error', async () => {
await fs.rmdir(uploadPath);
await fs.mkdir(uploadPath, { await fs.mkdir(uploadPath, {
mode: '444', mode: '444',
}); });
@ -122,7 +124,9 @@ describe('Media', () => {
.set('HedgeDoc-Note', 'test_upload_media') .set('HedgeDoc-Note', 'test_upload_media')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(500); .expect(500);
await fs.rmdir(uploadPath); });
afterEach(async () => {
await fs.rmdir(uploadPath, { recursive: true });
}); });
}); });
}); });
@ -142,6 +146,7 @@ describe('Media', () => {
afterAll(async () => { afterAll(async () => {
// Delete the upload folder // Delete the upload folder
await fs.rmdir(uploadPath); await fs.rmdir(uploadPath, { recursive: true });
await app.close();
}); });
}); });

View file

@ -362,7 +362,7 @@ describe('Notes', () => {
// delete the file afterwards // delete the file afterwards
await fs.unlink(join(uploadPath, fileName)); await fs.unlink(join(uploadPath, fileName));
} }
await fs.rmdir(uploadPath); await fs.rmdir(uploadPath, { recursive: true });
}); });
it('fails, when note does not exist', async () => { it('fails, when note does not exist', async () => {
await request(app.getHttpServer()) await request(app.getHttpServer())