mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-13 06:34:39 -04:00
test(e2e/public/media): test using real auth
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
ada90ed30b
commit
1f2cec2f7c
2 changed files with 36 additions and 27 deletions
|
@ -146,7 +146,7 @@ describe('Media', () => {
|
||||||
.expect(201);
|
.expect(201);
|
||||||
|
|
||||||
// try to delete upload with second user
|
// try to delete upload with second user
|
||||||
await agent2.delete('/api/private/media/' + filename).expect(401);
|
await agent2.delete('/api/private/media/' + filename).expect(403);
|
||||||
|
|
||||||
// delete upload for real
|
// delete upload for real
|
||||||
await agent.delete('/api/private/media/' + filename).expect(204);
|
await agent.delete('/api/private/media/' + filename).expect(204);
|
||||||
|
|
|
@ -8,19 +8,15 @@ import { join } from 'path';
|
||||||
import request from 'supertest';
|
import request from 'supertest';
|
||||||
|
|
||||||
import { ConsoleLoggerService } from '../../src/logger/console-logger.service';
|
import { ConsoleLoggerService } from '../../src/logger/console-logger.service';
|
||||||
import { Note } from '../../src/notes/note.entity';
|
|
||||||
import { User } from '../../src/users/user.entity';
|
|
||||||
import { TestSetup, TestSetupBuilder } from '../test-setup';
|
import { TestSetup, TestSetupBuilder } from '../test-setup';
|
||||||
import { ensureDeleted } from '../utils';
|
import { ensureDeleted } from '../utils';
|
||||||
|
|
||||||
describe('Media', () => {
|
describe('Media', () => {
|
||||||
let testSetup: TestSetup;
|
let testSetup: TestSetup;
|
||||||
let uploadPath: string;
|
let uploadPath: string;
|
||||||
let testNote: Note;
|
|
||||||
let user: User;
|
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
testSetup = await TestSetupBuilder.create().withMockAuth().build();
|
testSetup = await TestSetupBuilder.create().withUsers().build();
|
||||||
|
|
||||||
uploadPath =
|
uploadPath =
|
||||||
testSetup.configService.get('mediaConfig').backend.filesystem.uploadPath;
|
testSetup.configService.get('mediaConfig').backend.filesystem.uploadPath;
|
||||||
|
@ -34,13 +30,6 @@ describe('Media', () => {
|
||||||
const logger = await testSetup.app.resolve(ConsoleLoggerService);
|
const logger = await testSetup.app.resolve(ConsoleLoggerService);
|
||||||
logger.log('Switching logger', 'AppBootstrap');
|
logger.log('Switching logger', 'AppBootstrap');
|
||||||
testSetup.app.useLogger(logger);
|
testSetup.app.useLogger(logger);
|
||||||
|
|
||||||
user = await testSetup.userService.createUser('hardcoded', 'Testy');
|
|
||||||
testNote = await testSetup.notesService.createNote(
|
|
||||||
'test content',
|
|
||||||
null,
|
|
||||||
'test_upload_media',
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
|
@ -54,8 +43,9 @@ describe('Media', () => {
|
||||||
it('works', async () => {
|
it('works', async () => {
|
||||||
const uploadResponse = await request(testSetup.app.getHttpServer())
|
const uploadResponse = await request(testSetup.app.getHttpServer())
|
||||||
.post('/api/v2/media')
|
.post('/api/v2/media')
|
||||||
|
.set('Authorization', `Bearer ${testSetup.authTokens[0].secret}`)
|
||||||
.attach('file', 'test/public-api/fixtures/test.png')
|
.attach('file', 'test/public-api/fixtures/test.png')
|
||||||
.set('HedgeDoc-Note', 'test_upload_media')
|
.set('HedgeDoc-Note', 'testAlias1')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect(201);
|
.expect(201);
|
||||||
const path: string = uploadResponse.body.url;
|
const path: string = uploadResponse.body.url;
|
||||||
|
@ -76,14 +66,16 @@ describe('Media', () => {
|
||||||
it('MIME type not supported', async () => {
|
it('MIME type not supported', async () => {
|
||||||
await request(testSetup.app.getHttpServer())
|
await request(testSetup.app.getHttpServer())
|
||||||
.post('/api/v2/media')
|
.post('/api/v2/media')
|
||||||
|
.set('Authorization', `Bearer ${testSetup.authTokens[0].secret}`)
|
||||||
.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', 'testAlias1')
|
||||||
.expect(400);
|
.expect(400);
|
||||||
await expect(fs.access(uploadPath)).rejects.toBeDefined();
|
await expect(fs.access(uploadPath)).rejects.toBeDefined();
|
||||||
});
|
});
|
||||||
it('note does not exist', async () => {
|
it('note does not exist', async () => {
|
||||||
await request(testSetup.app.getHttpServer())
|
await request(testSetup.app.getHttpServer())
|
||||||
.post('/api/v2/media')
|
.post('/api/v2/media')
|
||||||
|
.set('Authorization', `Bearer ${testSetup.authTokens[0].secret}`)
|
||||||
.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(404);
|
.expect(404);
|
||||||
|
@ -95,8 +87,9 @@ describe('Media', () => {
|
||||||
});
|
});
|
||||||
await request(testSetup.app.getHttpServer())
|
await request(testSetup.app.getHttpServer())
|
||||||
.post('/api/v2/media')
|
.post('/api/v2/media')
|
||||||
|
.set('Authorization', `Bearer ${testSetup.authTokens[0].secret}`)
|
||||||
.attach('file', 'test/public-api/fixtures/test.png')
|
.attach('file', 'test/public-api/fixtures/test.png')
|
||||||
.set('HedgeDoc-Note', 'test_upload_media')
|
.set('HedgeDoc-Note', 'testAlias1')
|
||||||
.expect('Content-Type', /json/)
|
.expect('Content-Type', /json/)
|
||||||
.expect(500);
|
.expect(500);
|
||||||
});
|
});
|
||||||
|
@ -106,16 +99,32 @@ describe('Media', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('DELETE /media/{filename}', async () => {
|
describe('DELETE /media/{filename}', () => {
|
||||||
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
it('successfully deletes an uploaded file', async () => {
|
||||||
const upload = await testSetup.mediaService.saveFile(
|
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
||||||
testImage,
|
const upload = await testSetup.mediaService.saveFile(
|
||||||
user,
|
testImage,
|
||||||
testNote,
|
testSetup.users[0],
|
||||||
);
|
testSetup.ownedNotes[0],
|
||||||
const filename = upload.fileUrl.split('/').pop() || '';
|
);
|
||||||
await request(testSetup.app.getHttpServer())
|
const filename = upload.fileUrl.split('/').pop() || '';
|
||||||
.delete('/api/v2/media/' + filename)
|
await request(testSetup.app.getHttpServer())
|
||||||
.expect(204);
|
.delete('/api/v2/media/' + filename)
|
||||||
|
.set('Authorization', `Bearer ${testSetup.authTokens[0].secret}`)
|
||||||
|
.expect(204);
|
||||||
|
});
|
||||||
|
it('returns an error if the user does not own the file', async () => {
|
||||||
|
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
||||||
|
const upload = await testSetup.mediaService.saveFile(
|
||||||
|
testImage,
|
||||||
|
testSetup.users[0],
|
||||||
|
testSetup.ownedNotes[0],
|
||||||
|
);
|
||||||
|
const filename = upload.fileUrl.split('/').pop() || '';
|
||||||
|
await request(testSetup.app.getHttpServer())
|
||||||
|
.delete('/api/v2/media/' + filename)
|
||||||
|
.set('Authorization', `Bearer ${testSetup.authTokens[1].secret}`)
|
||||||
|
.expect(403);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue