2021-03-23 20:49:43 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2021-08-29 12:45:46 -04:00
|
|
|
import { promises as fs } from 'fs';
|
|
|
|
import { join } from 'path';
|
2021-04-29 16:47:18 -04:00
|
|
|
import request from 'supertest';
|
2021-08-29 12:45:46 -04:00
|
|
|
|
2021-10-13 14:43:56 -04:00
|
|
|
import { AuthConfig } from '../../src/config/auth.config';
|
2021-03-23 20:49:43 -04:00
|
|
|
import { NotInDBError } from '../../src/errors/errors';
|
|
|
|
import { User } from '../../src/users/user.entity';
|
2021-10-13 14:43:56 -04:00
|
|
|
import { setupSessionMiddleware } from '../../src/utils/session';
|
2021-10-14 15:45:13 -04:00
|
|
|
import { TestSetup } from '../test-setup';
|
2021-03-23 20:49:43 -04:00
|
|
|
|
|
|
|
describe('Notes', () => {
|
2021-10-14 15:45:13 -04:00
|
|
|
let testSetup: TestSetup;
|
|
|
|
|
2021-03-23 20:49:43 -04:00
|
|
|
let user: User;
|
|
|
|
let user2: User;
|
|
|
|
let content: string;
|
|
|
|
let forbiddenNoteId: string;
|
|
|
|
let uploadPath: string;
|
2021-03-31 19:22:34 -04:00
|
|
|
let testImage: Buffer;
|
2021-10-13 14:43:56 -04:00
|
|
|
let agent: request.SuperAgentTest;
|
2021-03-23 20:49:43 -04:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2021-10-14 15:45:13 -04:00
|
|
|
testSetup = await TestSetup.create();
|
|
|
|
|
|
|
|
forbiddenNoteId =
|
|
|
|
testSetup.configService.get('appConfig').forbiddenNoteIds[0];
|
|
|
|
uploadPath =
|
|
|
|
testSetup.configService.get('mediaConfig').backend.filesystem.uploadPath;
|
|
|
|
|
|
|
|
const authConfig = testSetup.configService.get('authConfig') as AuthConfig;
|
|
|
|
setupSessionMiddleware(testSetup.app, authConfig);
|
2021-03-23 20:49:43 -04:00
|
|
|
|
2021-10-14 15:45:13 -04:00
|
|
|
await testSetup.app.init();
|
|
|
|
|
|
|
|
user = await testSetup.userService.createUser('hardcoded', 'Testy');
|
|
|
|
await testSetup.identityService.createLocalIdentity(user, 'test');
|
|
|
|
user2 = await testSetup.userService.createUser(
|
|
|
|
'hardcoded2',
|
|
|
|
'Max Mustermann',
|
|
|
|
);
|
|
|
|
await testSetup.identityService.createLocalIdentity(user2, 'test');
|
2021-03-23 20:49:43 -04:00
|
|
|
content = 'This is a test note.';
|
2021-03-31 19:22:34 -04:00
|
|
|
testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
2021-10-14 15:45:13 -04:00
|
|
|
|
|
|
|
agent = request.agent(testSetup.app.getHttpServer());
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.post('/api/private/auth/local/login')
|
2021-10-13 14:43:56 -04:00
|
|
|
.send({ username: 'hardcoded', password: 'test' })
|
|
|
|
.expect(201);
|
2021-03-23 20:49:43 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('POST /notes', async () => {
|
2021-10-13 14:43:56 -04:00
|
|
|
const response = await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.post('/api/private/notes')
|
2021-03-23 20:49:43 -04:00
|
|
|
.set('Content-Type', 'text/markdown')
|
|
|
|
.send(content)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201);
|
|
|
|
expect(response.body.metadata?.id).toBeDefined();
|
|
|
|
expect(
|
2021-10-14 15:45:13 -04:00
|
|
|
await testSetup.notesService.getNoteContent(
|
|
|
|
await testSetup.notesService.getNoteByIdOrAlias(
|
|
|
|
response.body.metadata.id,
|
|
|
|
),
|
2021-03-23 20:49:43 -04:00
|
|
|
),
|
|
|
|
).toEqual(content);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('GET /notes/{note}', () => {
|
|
|
|
it('works with an existing note', async () => {
|
|
|
|
// check if we can succefully get a note that exists
|
2021-10-14 15:45:13 -04:00
|
|
|
await testSetup.notesService.createNote(content, 'test1', user);
|
2021-10-13 14:43:56 -04:00
|
|
|
const response = await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get('/api/private/notes/test1')
|
2021-03-23 20:49:43 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body.content).toEqual(content);
|
|
|
|
});
|
|
|
|
it('fails with an non-existing note', async () => {
|
|
|
|
// check if a missing note correctly returns 404
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get('/api/private/notes/i_dont_exist')
|
2021-03-23 20:49:43 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST /notes/{note}', () => {
|
|
|
|
it('works with a non-existing alias', async () => {
|
2021-10-13 14:43:56 -04:00
|
|
|
const response = await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.post('/api/private/notes/test2')
|
2021-03-23 20:49:43 -04:00
|
|
|
.set('Content-Type', 'text/markdown')
|
|
|
|
.send(content)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201);
|
|
|
|
expect(response.body.metadata?.id).toBeDefined();
|
|
|
|
return expect(
|
2021-10-14 15:45:13 -04:00
|
|
|
await testSetup.notesService.getNoteContent(
|
|
|
|
await testSetup.notesService.getNoteByIdOrAlias(
|
|
|
|
response.body.metadata?.id,
|
|
|
|
),
|
2021-03-23 20:49:43 -04:00
|
|
|
),
|
|
|
|
).toEqual(content);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with a forbidden alias', async () => {
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.post(`/api/private/notes/${forbiddenNoteId}`)
|
2021-03-23 20:49:43 -04:00
|
|
|
.set('Content-Type', 'text/markdown')
|
|
|
|
.send(content)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with a existing alias', async () => {
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.post('/api/private/notes/test2')
|
2021-03-23 20:49:43 -04:00
|
|
|
.set('Content-Type', 'text/markdown')
|
|
|
|
.send(content)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('DELETE /notes/{note}', () => {
|
2021-03-31 19:22:34 -04:00
|
|
|
describe('works', () => {
|
|
|
|
it('with an existing alias and keepMedia false', async () => {
|
|
|
|
const noteId = 'test3';
|
2021-10-14 15:45:13 -04:00
|
|
|
const note = await testSetup.notesService.createNote(
|
|
|
|
content,
|
|
|
|
noteId,
|
|
|
|
user,
|
|
|
|
);
|
|
|
|
await testSetup.mediaService.saveFile(testImage, user, note);
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.delete(`/api/private/notes/${noteId}`)
|
2021-03-31 19:22:34 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send({
|
|
|
|
keepMedia: false,
|
|
|
|
})
|
|
|
|
.expect(204);
|
2021-10-14 15:45:13 -04:00
|
|
|
await expect(
|
|
|
|
testSetup.notesService.getNoteByIdOrAlias(noteId),
|
|
|
|
).rejects.toEqual(
|
2021-03-31 19:22:34 -04:00
|
|
|
new NotInDBError(`Note with id/alias '${noteId}' not found.`),
|
|
|
|
);
|
2021-10-14 15:45:13 -04:00
|
|
|
expect(
|
|
|
|
await testSetup.mediaService.listUploadsByUser(user),
|
|
|
|
).toHaveLength(0);
|
2021-03-31 19:22:34 -04:00
|
|
|
await fs.rmdir(uploadPath);
|
|
|
|
});
|
|
|
|
it('with an existing alias and keepMedia true', async () => {
|
|
|
|
const noteId = 'test3a';
|
2021-10-14 15:45:13 -04:00
|
|
|
const note = await testSetup.notesService.createNote(
|
|
|
|
content,
|
|
|
|
noteId,
|
|
|
|
user,
|
|
|
|
);
|
|
|
|
const url = await testSetup.mediaService.saveFile(
|
|
|
|
testImage,
|
|
|
|
user,
|
|
|
|
note,
|
|
|
|
);
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.delete(`/api/private/notes/${noteId}`)
|
2021-03-31 19:22:34 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send({
|
|
|
|
keepMedia: true,
|
|
|
|
})
|
|
|
|
.expect(204);
|
2021-10-14 15:45:13 -04:00
|
|
|
await expect(
|
|
|
|
testSetup.notesService.getNoteByIdOrAlias(noteId),
|
|
|
|
).rejects.toEqual(
|
2021-03-31 19:22:34 -04:00
|
|
|
new NotInDBError(`Note with id/alias '${noteId}' not found.`),
|
|
|
|
);
|
2021-10-14 15:45:13 -04:00
|
|
|
expect(
|
|
|
|
await testSetup.mediaService.listUploadsByUser(user),
|
|
|
|
).toHaveLength(1);
|
2021-03-31 19:22:34 -04:00
|
|
|
// Remove /upload/ from path as we just need the filename.
|
|
|
|
const fileName = url.replace('/uploads/', '');
|
|
|
|
// delete the file afterwards
|
|
|
|
await fs.unlink(join(uploadPath, fileName));
|
|
|
|
await fs.rmdir(uploadPath);
|
|
|
|
});
|
2021-03-23 20:49:43 -04:00
|
|
|
});
|
|
|
|
it('fails with a forbidden alias', async () => {
|
2021-10-15 11:06:56 -04:00
|
|
|
await agent.delete(`/api/private/notes/${forbiddenNoteId}`).expect(400);
|
2021-03-23 20:49:43 -04:00
|
|
|
});
|
|
|
|
it('fails with a non-existing alias', async () => {
|
2021-10-15 11:06:56 -04:00
|
|
|
await agent.delete('/api/private/notes/i_dont_exist').expect(404);
|
2021-03-23 20:49:43 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('GET /notes/{note}/revisions', () => {
|
|
|
|
it('works with existing alias', async () => {
|
2021-10-14 15:45:13 -04:00
|
|
|
await testSetup.notesService.createNote(content, 'test4', user);
|
2021-10-13 14:43:56 -04:00
|
|
|
const response = await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get('/api/private/notes/test4/revisions')
|
2021-03-23 20:49:43 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body).toHaveLength(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with a forbidden alias', async () => {
|
2021-10-15 11:06:56 -04:00
|
|
|
await agent
|
|
|
|
.get(`/api/private/notes/${forbiddenNoteId}/revisions`)
|
|
|
|
.expect(400);
|
2021-03-23 20:49:43 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with non-existing alias', async () => {
|
|
|
|
// check if a missing note correctly returns 404
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get('/api/private/notes/i_dont_exist/revisions')
|
2021-03-23 20:49:43 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-08-29 20:07:35 -04:00
|
|
|
describe('DELETE /notes/{note}/revisions', () => {
|
|
|
|
it('works with an existing alias', async () => {
|
|
|
|
const noteId = 'test8';
|
2021-10-14 15:45:13 -04:00
|
|
|
const note = await testSetup.notesService.createNote(
|
|
|
|
content,
|
|
|
|
noteId,
|
|
|
|
user,
|
|
|
|
);
|
|
|
|
await testSetup.notesService.updateNote(note, 'update');
|
2021-10-13 14:43:56 -04:00
|
|
|
const responseBeforeDeleting = await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get('/api/private/notes/test8/revisions')
|
2021-08-29 20:07:35 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(responseBeforeDeleting.body).toHaveLength(2);
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.delete(`/api/private/notes/${noteId}/revisions`)
|
2021-08-29 20:07:35 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(204);
|
2021-10-13 14:43:56 -04:00
|
|
|
const responseAfterDeleting = await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get('/api/private/notes/test8/revisions')
|
2021-08-29 20:07:35 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(responseAfterDeleting.body).toHaveLength(1);
|
|
|
|
});
|
|
|
|
it('fails with a forbidden alias', async () => {
|
2021-10-15 11:06:56 -04:00
|
|
|
await agent
|
|
|
|
.delete(`/api/private/notes/${forbiddenNoteId}/revisions`)
|
|
|
|
.expect(400);
|
2021-08-29 20:07:35 -04:00
|
|
|
});
|
|
|
|
it('fails with non-existing alias', async () => {
|
|
|
|
// check if a missing note correctly returns 404
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.delete('/api/private/notes/i_dont_exist/revisions')
|
2021-08-29 20:07:35 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-23 20:49:43 -04:00
|
|
|
describe('GET /notes/{note}/revisions/{revision-id}', () => {
|
|
|
|
it('works with an existing alias', async () => {
|
2021-10-14 15:45:13 -04:00
|
|
|
const note = await testSetup.notesService.createNote(
|
|
|
|
content,
|
|
|
|
'test5',
|
|
|
|
user,
|
|
|
|
);
|
|
|
|
const revision = await testSetup.notesService.getLatestRevision(note);
|
2021-10-13 14:43:56 -04:00
|
|
|
const response = await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get(`/api/private/notes/test5/revisions/${revision.id}`)
|
2021-03-23 20:49:43 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body.content).toEqual(content);
|
|
|
|
});
|
|
|
|
it('fails with a forbidden alias', async () => {
|
2021-10-15 11:06:56 -04:00
|
|
|
await agent
|
|
|
|
.get(`/api/private/notes/${forbiddenNoteId}/revisions/1`)
|
|
|
|
.expect(400);
|
2021-03-23 20:49:43 -04:00
|
|
|
});
|
|
|
|
it('fails with non-existing alias', async () => {
|
|
|
|
// check if a missing note correctly returns 404
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get('/api/private/notes/i_dont_exist/revisions/1')
|
2021-03-23 20:49:43 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('GET /notes/{note}/media', () => {
|
|
|
|
it('works', async () => {
|
2021-05-16 16:46:02 -04:00
|
|
|
const alias = 'test6';
|
|
|
|
const extraAlias = 'test7';
|
2021-10-14 15:45:13 -04:00
|
|
|
const note1 = await testSetup.notesService.createNote(
|
|
|
|
content,
|
|
|
|
alias,
|
|
|
|
user,
|
|
|
|
);
|
|
|
|
const note2 = await testSetup.notesService.createNote(
|
|
|
|
content,
|
|
|
|
extraAlias,
|
|
|
|
user,
|
|
|
|
);
|
2021-10-13 14:43:56 -04:00
|
|
|
const response = await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get(`/api/private/notes/${alias}/media/`)
|
2021-03-23 20:49:43 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body).toHaveLength(0);
|
|
|
|
|
2021-03-31 16:44:46 -04:00
|
|
|
const testImage = await fs.readFile('test/private-api/fixtures/test.png');
|
2021-10-14 15:45:13 -04:00
|
|
|
const url0 = await testSetup.mediaService.saveFile(
|
|
|
|
testImage,
|
|
|
|
user,
|
|
|
|
note1,
|
|
|
|
);
|
|
|
|
const url1 = await testSetup.mediaService.saveFile(
|
|
|
|
testImage,
|
|
|
|
user,
|
|
|
|
note2,
|
|
|
|
);
|
2021-03-23 20:49:43 -04:00
|
|
|
|
2021-10-13 14:43:56 -04:00
|
|
|
const responseAfter = await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get(`/api/private/notes/${alias}/media/`)
|
2021-03-23 20:49:43 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(responseAfter.body).toHaveLength(1);
|
|
|
|
expect(responseAfter.body[0].url).toEqual(url0);
|
|
|
|
expect(responseAfter.body[0].url).not.toEqual(url1);
|
|
|
|
for (const fileUrl of [url0, url1]) {
|
|
|
|
const fileName = fileUrl.replace('/uploads/', '');
|
|
|
|
// delete the file afterwards
|
|
|
|
await fs.unlink(join(uploadPath, fileName));
|
|
|
|
}
|
2021-03-31 16:42:56 -04:00
|
|
|
await fs.rmdir(uploadPath, { recursive: true });
|
2021-03-23 20:49:43 -04:00
|
|
|
});
|
|
|
|
it('fails, when note does not exist', async () => {
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get(`/api/private/notes/i_dont_exist/media/`)
|
2021-03-23 20:49:43 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
it("fails, when user can't read note", async () => {
|
2021-05-16 16:46:02 -04:00
|
|
|
const alias = 'test11';
|
2021-10-14 15:45:13 -04:00
|
|
|
await testSetup.notesService.createNote(
|
|
|
|
'This is a test note.',
|
|
|
|
alias,
|
|
|
|
user2,
|
|
|
|
);
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-10-15 11:06:56 -04:00
|
|
|
.get(`/api/private/notes/${alias}/media/`)
|
2021-03-23 20:49:43 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(401);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
2021-10-14 15:45:13 -04:00
|
|
|
await testSetup.app.close();
|
2021-03-23 20:49:43 -04:00
|
|
|
});
|
|
|
|
});
|