From d73bbcaeff0bacce52d2403c2e47657dcc07e298 Mon Sep 17 00:00:00 2001 From: Yannick Bungers Date: Sun, 23 Apr 2023 21:57:35 +0200 Subject: [PATCH] fix: increase test coverage Signed-off-by: Yannick Bungers --- .../src/permissions/permissions.service.ts | 1 - backend/test/private-api/media.e2e-spec.ts | 53 +++++++++++++------ backend/test/public-api/media.e2e-spec.ts | 9 ++++ 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/backend/src/permissions/permissions.service.ts b/backend/src/permissions/permissions.service.ts index c7fc3a21d..6bf3d62ef 100644 --- a/backend/src/permissions/permissions.service.ts +++ b/backend/src/permissions/permissions.service.ts @@ -62,7 +62,6 @@ export class PermissionsService { case Permission.OWNER: return await this.isOwner(user, note); } - return false; } public async checkMediaDeletePermission( diff --git a/backend/test/private-api/media.e2e-spec.ts b/backend/test/private-api/media.e2e-spec.ts index 183cef9e1..16bf34b27 100644 --- a/backend/test/private-api/media.e2e-spec.ts +++ b/backend/test/private-api/media.e2e-spec.ts @@ -63,21 +63,44 @@ describe('Media', () => { }); describe('POST /media', () => { - it('works', async () => { - const uploadResponse = await agent - .post('/api/private/media') - .attach('file', 'test/private-api/fixtures/test.png') - .set('HedgeDoc-Note', 'test_upload_media') - .expect('Content-Type', /json/) - .expect(201); - const path: string = uploadResponse.body.url; - const testImage = await fs.readFile('test/private-api/fixtures/test.png'); - const downloadResponse = await agent.get(path); - expect(downloadResponse.body).toEqual(testImage); - // 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('works', () => { + it('with user', async () => { + const uploadResponse = await agent + .post('/api/private/media') + .attach('file', 'test/private-api/fixtures/test.png') + .set('HedgeDoc-Note', 'test_upload_media') + .expect('Content-Type', /json/) + .expect(201); + const path: string = uploadResponse.body.url; + const testImage = await fs.readFile( + 'test/private-api/fixtures/test.png', + ); + const downloadResponse = await agent.get(path); + expect(downloadResponse.body).toEqual(testImage); + // 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)); + }); + it('without user', async () => { + const agent = request.agent(testSetup.app.getHttpServer()); + const uploadResponse = await agent + .post('/api/private/media') + .attach('file', 'test/private-api/fixtures/test.png') + .set('HedgeDoc-Note', 'test_upload_media') + .expect('Content-Type', /json/) + .expect(201); + const path: string = uploadResponse.body.url; + const testImage = await fs.readFile( + 'test/private-api/fixtures/test.png', + ); + const downloadResponse = await agent.get(path); + expect(downloadResponse.body).toEqual(testImage); + // 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 () => { diff --git a/backend/test/public-api/media.e2e-spec.ts b/backend/test/public-api/media.e2e-spec.ts index 514c3d85c..a4780ae6b 100644 --- a/backend/test/public-api/media.e2e-spec.ts +++ b/backend/test/public-api/media.e2e-spec.ts @@ -93,6 +93,15 @@ describe('Media', () => { .expect('Content-Type', /json/) .expect(500); }); + it('no file uploaded', async () => { + await request(testSetup.app.getHttpServer()) + .post('/api/v2/media') + .set('Authorization', `Bearer ${testSetup.authTokens[0].secret}`) + .set('HedgeDoc-Note', 'testAlias1') + .expect('Content-Type', /json/) + .expect(400); + }); + afterEach(async () => { await ensureDeleted(uploadPath); });