fix: increase test coverage

Signed-off-by: Yannick Bungers <git@innay.de>
This commit is contained in:
Yannick Bungers 2023-04-23 21:57:35 +02:00 committed by Yannick Bungers
parent dad60a25ea
commit d73bbcaeff
3 changed files with 47 additions and 16 deletions

View file

@ -62,7 +62,6 @@ export class PermissionsService {
case Permission.OWNER:
return await this.isOwner(user, note);
}
return false;
}
public async checkMediaDeletePermission(

View file

@ -63,7 +63,8 @@ describe('Media', () => {
});
describe('POST /media', () => {
it('works', async () => {
describe('works', () => {
it('with user', async () => {
const uploadResponse = await agent
.post('/api/private/media')
.attach('file', 'test/private-api/fixtures/test.png')
@ -71,7 +72,9 @@ describe('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 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.
@ -79,6 +82,26 @@ describe('Media', () => {
// 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 () => {
await ensureDeleted(uploadPath);

View file

@ -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);
});