2021-03-23 20:49:43 -04:00
|
|
|
/*
|
2022-01-30 09:48:59 -05:00
|
|
|
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
2021-03-23 20:49:43 -04:00
|
|
|
*
|
|
|
|
* 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-03-23 20:49:43 -04:00
|
|
|
import { NotInDBError } from '../../src/errors/errors';
|
2023-05-04 11:47:48 -04:00
|
|
|
import { Group } from '../../src/groups/group.entity';
|
2021-03-23 20:49:43 -04:00
|
|
|
import { User } from '../../src/users/user.entity';
|
2022-01-06 15:41:36 -05:00
|
|
|
import { TestSetup, TestSetupBuilder } from '../test-setup';
|
2021-03-23 20:49:43 -04:00
|
|
|
|
|
|
|
describe('Notes', () => {
|
2021-10-14 15:45:13 -04:00
|
|
|
let testSetup: TestSetup;
|
|
|
|
|
2023-05-04 11:47:48 -04:00
|
|
|
let user1: User;
|
2021-03-23 20:49:43 -04:00
|
|
|
let user2: User;
|
2023-05-04 11:47:48 -04:00
|
|
|
let group1: Group;
|
2021-03-23 20:49:43 -04:00
|
|
|
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 () => {
|
2022-01-06 15:41:36 -05:00
|
|
|
testSetup = await TestSetupBuilder.create().build();
|
2021-10-14 15:45:13 -04:00
|
|
|
|
|
|
|
forbiddenNoteId =
|
2022-01-30 09:48:59 -05:00
|
|
|
testSetup.configService.get('noteConfig').forbiddenNoteIds[0];
|
2021-10-14 15:45:13 -04:00
|
|
|
uploadPath =
|
|
|
|
testSetup.configService.get('mediaConfig').backend.filesystem.uploadPath;
|
|
|
|
|
|
|
|
await testSetup.app.init();
|
2022-09-24 20:05:30 -04:00
|
|
|
const username1 = 'hardcoded';
|
|
|
|
const password1 = 'AHardcodedStrongP@ssword123';
|
|
|
|
const username2 = 'hardcoded2';
|
|
|
|
const password2 = 'AHardcodedStrongP@ssword12';
|
2023-05-04 11:47:48 -04:00
|
|
|
const groupname1 = 'groupname1';
|
2021-10-14 15:45:13 -04:00
|
|
|
|
2023-05-04 11:47:48 -04:00
|
|
|
user1 = await testSetup.userService.createUser(username1, 'Testy');
|
|
|
|
await testSetup.identityService.createLocalIdentity(user1, password1);
|
2022-09-24 20:05:30 -04:00
|
|
|
user2 = await testSetup.userService.createUser(username2, 'Max Mustermann');
|
|
|
|
await testSetup.identityService.createLocalIdentity(user2, password2);
|
2023-05-04 11:47:48 -04:00
|
|
|
|
|
|
|
group1 = await testSetup.groupService.createGroup(groupname1, 'Group 1');
|
|
|
|
|
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')
|
2022-09-24 20:05:30 -04:00
|
|
|
.send({ username: username1, password: password1 })
|
2021-10-13 14:43:56 -04:00
|
|
|
.expect(201);
|
2021-03-23 20:49:43 -04:00
|
|
|
});
|
|
|
|
|
2022-03-04 07:13:46 -05:00
|
|
|
afterAll(async () => {
|
|
|
|
await testSetup.app.close();
|
|
|
|
await testSetup.cleanup();
|
|
|
|
});
|
|
|
|
|
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
|
2023-05-04 11:47:48 -04:00
|
|
|
await testSetup.notesService.createNote(content, user1, 'test1');
|
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/)
|
2022-01-23 16:23:00 -05:00
|
|
|
.expect(409);
|
2021-03-23 20:49:43 -04:00
|
|
|
});
|
2022-10-02 17:48:06 -04:00
|
|
|
|
|
|
|
it('fails with a content, that is too long', async () => {
|
|
|
|
const content = 'x'.repeat(
|
|
|
|
(testSetup.configService.get('noteConfig')
|
|
|
|
.maxDocumentLength as number) + 1,
|
|
|
|
);
|
|
|
|
await agent
|
|
|
|
.post('/api/private/notes/test2')
|
|
|
|
.set('Content-Type', 'text/markdown')
|
|
|
|
.send(content)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(413);
|
|
|
|
});
|
2021-03-23 20:49:43 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
2023-05-04 11:47:48 -04:00
|
|
|
user1,
|
2021-11-14 15:44:59 -05:00
|
|
|
noteId,
|
2021-10-14 15:45:13 -04:00
|
|
|
);
|
2023-05-04 11:47:48 -04:00
|
|
|
await testSetup.mediaService.saveFile(testImage, user1, 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(
|
2023-05-04 11:47:48 -04:00
|
|
|
await testSetup.mediaService.listUploadsByUser(user1),
|
2021-10-14 15:45:13 -04:00
|
|
|
).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,
|
2023-05-04 11:47:48 -04:00
|
|
|
user1,
|
2021-11-14 15:44:59 -05:00
|
|
|
noteId,
|
2021-10-14 15:45:13 -04:00
|
|
|
);
|
2022-01-29 12:57:44 -05:00
|
|
|
const upload = await testSetup.mediaService.saveFile(
|
2021-10-14 15:45:13 -04:00
|
|
|
testImage,
|
2023-05-04 11:47:48 -04:00
|
|
|
user1,
|
2021-10-14 15:45:13 -04:00
|
|
|
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(
|
2023-05-04 11:47:48 -04:00
|
|
|
await testSetup.mediaService.listUploadsByUser(user1),
|
2021-10-14 15:45:13 -04:00
|
|
|
).toHaveLength(1);
|
2021-03-31 19:22:34 -04:00
|
|
|
// Remove /upload/ from path as we just need the filename.
|
2022-01-29 12:57:44 -05:00
|
|
|
const fileName = upload.fileUrl.replace('/uploads/', '');
|
2021-03-31 19:22:34 -04:00
|
|
|
// 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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-08-21 07:25:25 -04:00
|
|
|
describe('GET /notes/{note}/metadata', () => {
|
|
|
|
it('returns complete metadata object', async () => {
|
|
|
|
const noteAlias = 'metadata_test_note';
|
2023-05-04 11:47:48 -04:00
|
|
|
await testSetup.notesService.createNote(content, user1, noteAlias);
|
2022-08-21 07:25:25 -04:00
|
|
|
const metadata = await agent
|
|
|
|
.get(`/api/private/notes/${noteAlias}/metadata`)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(typeof metadata.body.id).toEqual('string');
|
|
|
|
expect(metadata.body.aliases[0].name).toEqual(noteAlias);
|
|
|
|
expect(metadata.body.primaryAddress).toEqual(noteAlias);
|
|
|
|
expect(metadata.body.title).toEqual('');
|
|
|
|
expect(metadata.body.description).toEqual('');
|
|
|
|
expect(typeof metadata.body.createdAt).toEqual('string');
|
|
|
|
expect(metadata.body.editedBy).toEqual([]);
|
|
|
|
expect(metadata.body.permissions.owner).toEqual('hardcoded');
|
|
|
|
expect(metadata.body.permissions.sharedToUsers).toEqual([]);
|
|
|
|
expect(metadata.body.permissions.sharedToUsers).toEqual([]);
|
|
|
|
expect(metadata.body.tags).toEqual([]);
|
|
|
|
expect(typeof metadata.body.updatedAt).toEqual('string');
|
|
|
|
expect(typeof metadata.body.updateUsername).toEqual('string');
|
|
|
|
expect(typeof metadata.body.viewCount).toEqual('number');
|
|
|
|
expect(metadata.body.editedBy).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with a forbidden alias', async () => {
|
|
|
|
await agent
|
|
|
|
.get(`/api/private/notes/${forbiddenNoteId}/metadata`)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with non-existing alias', async () => {
|
|
|
|
// check if a missing note correctly returns 404
|
|
|
|
await agent
|
|
|
|
.get('/api/private/notes/i_dont_exist/metadata')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('has the correct update/create dates', async () => {
|
|
|
|
const noteAlias = 'metadata_test_note_date';
|
|
|
|
// create a note
|
|
|
|
const note = await testSetup.notesService.createNote(
|
|
|
|
content,
|
2023-05-04 11:47:48 -04:00
|
|
|
user1,
|
2022-08-21 07:25:25 -04:00
|
|
|
noteAlias,
|
|
|
|
);
|
|
|
|
// save the creation time
|
|
|
|
const createDate = note.createdAt;
|
|
|
|
const revisions = await note.revisions;
|
|
|
|
const updatedDate = revisions[revisions.length - 1].createdAt;
|
|
|
|
// wait one second
|
|
|
|
await new Promise((r) => setTimeout(r, 1000));
|
|
|
|
// update the note
|
|
|
|
await testSetup.notesService.updateNote(note, 'More test content');
|
|
|
|
const metadata = await agent
|
|
|
|
.get(`/api/private/notes/${noteAlias}/metadata`)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(metadata.body.createdAt).toEqual(createDate.toISOString());
|
|
|
|
expect(metadata.body.updatedAt).not.toEqual(updatedDate.toISOString());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-23 20:49:43 -04:00
|
|
|
describe('GET /notes/{note}/revisions', () => {
|
|
|
|
it('works with existing alias', async () => {
|
2023-05-04 11:47:48 -04:00
|
|
|
await testSetup.notesService.createNote(content, user1, 'test4');
|
2022-04-03 11:39:16 -04:00
|
|
|
// create a second note to check for a regression, where typeorm always returned
|
|
|
|
// all revisions in the database
|
2023-05-04 11:47:48 -04:00
|
|
|
await testSetup.notesService.createNote(content, user1, 'test4a');
|
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,
|
2023-05-04 11:47:48 -04:00
|
|
|
user1,
|
2021-11-14 15:44:59 -05:00
|
|
|
noteId,
|
2021-10-14 15:45:13 -04:00
|
|
|
);
|
|
|
|
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,
|
2023-05-04 11:47:48 -04:00
|
|
|
user1,
|
2021-11-14 15:44:59 -05:00
|
|
|
'test5',
|
2021-10-14 15:45:13 -04:00
|
|
|
);
|
2022-06-21 10:05:21 -04:00
|
|
|
const revision = await testSetup.revisionsService.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,
|
2023-05-04 11:47:48 -04:00
|
|
|
user1,
|
2021-11-14 15:44:59 -05:00
|
|
|
alias,
|
2021-10-14 15:45:13 -04:00
|
|
|
);
|
|
|
|
const note2 = await testSetup.notesService.createNote(
|
|
|
|
content,
|
2023-05-04 11:47:48 -04:00
|
|
|
user1,
|
2021-11-14 15:44:59 -05:00
|
|
|
extraAlias,
|
2021-10-14 15:45:13 -04:00
|
|
|
);
|
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');
|
2022-01-29 12:57:44 -05:00
|
|
|
const upload0 = await testSetup.mediaService.saveFile(
|
2021-10-14 15:45:13 -04:00
|
|
|
testImage,
|
2023-05-04 11:47:48 -04:00
|
|
|
user1,
|
2021-10-14 15:45:13 -04:00
|
|
|
note1,
|
|
|
|
);
|
2022-01-29 12:57:44 -05:00
|
|
|
const upload1 = await testSetup.mediaService.saveFile(
|
2021-10-14 15:45:13 -04:00
|
|
|
testImage,
|
2023-05-04 11:47:48 -04:00
|
|
|
user1,
|
2021-10-14 15:45:13 -04:00
|
|
|
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);
|
2022-01-29 12:57:44 -05:00
|
|
|
expect(responseAfter.body[0].url).toEqual(upload0.fileUrl);
|
|
|
|
expect(responseAfter.body[0].url).not.toEqual(upload1.fileUrl);
|
|
|
|
for (const upload of [upload0, upload1]) {
|
|
|
|
const fileName = upload.fileUrl.replace('/uploads/', '');
|
2021-03-23 20:49:43 -04:00
|
|
|
// delete the file afterwards
|
|
|
|
await fs.unlink(join(uploadPath, fileName));
|
|
|
|
}
|
2022-01-30 16:00:17 -05:00
|
|
|
await fs.rm(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.',
|
|
|
|
user2,
|
2021-11-14 15:44:59 -05:00
|
|
|
alias,
|
2021-10-14 15:45:13 -04:00
|
|
|
);
|
2022-08-23 12:01:45 -04:00
|
|
|
// Redact default read permissions
|
|
|
|
const note = await testSetup.notesService.getNoteByIdOrAlias(alias);
|
|
|
|
const everyone = await testSetup.groupService.getEveryoneGroup();
|
|
|
|
const loggedin = await testSetup.groupService.getLoggedInGroup();
|
|
|
|
await testSetup.permissionsService.removeGroupPermission(note, everyone);
|
|
|
|
await testSetup.permissionsService.removeGroupPermission(note, loggedin);
|
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/)
|
2021-11-21 12:03:29 -05:00
|
|
|
.expect(403);
|
2021-03-23 20:49:43 -04:00
|
|
|
});
|
|
|
|
});
|
2023-05-04 11:47:48 -04:00
|
|
|
|
|
|
|
describe('permissions', () => {
|
|
|
|
const user1NoteAlias = 'user1NoteAlias';
|
|
|
|
const user2NoteAlias = 'user2NoteAlias';
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
await testSetup.notesService.createNote(
|
|
|
|
'This is a test note.',
|
|
|
|
user1,
|
|
|
|
user1NoteAlias,
|
|
|
|
);
|
|
|
|
await testSetup.notesService.createNote(
|
|
|
|
'This is a test note.',
|
|
|
|
user2,
|
|
|
|
user2NoteAlias,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('users', () => {
|
|
|
|
describe('PUT /notes/{note}/metadata/permissions/users/{userName}', () => {
|
|
|
|
it('fails, when note does not exist', async () => {
|
|
|
|
await agent
|
|
|
|
.put(
|
|
|
|
`/api/private/notes/notExisting/metadata/permissions/users/${user1.username}`,
|
|
|
|
)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails, when user is not the owner', async () => {
|
|
|
|
await agent
|
|
|
|
.put(
|
|
|
|
`/api/private/notes/${user2NoteAlias}/metadata/permissions/users/${user1.username}`,
|
|
|
|
)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(403);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't do anything if the user is the owner", async () => {
|
2023-08-15 15:29:40 -04:00
|
|
|
const note =
|
|
|
|
await testSetup.notesService.getNoteByIdOrAlias(user1NoteAlias);
|
2023-05-04 11:47:48 -04:00
|
|
|
await testSetup.permissionsService.removeUserPermission(note, user2);
|
|
|
|
|
|
|
|
const response = await agent
|
|
|
|
.put(
|
|
|
|
`/api/private/notes/${user1NoteAlias}/metadata/permissions/users/${user1.username}`,
|
|
|
|
)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({ canEdit: true });
|
|
|
|
expect(response.body.sharedToUsers).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([true, false])('works with edit set to %s', async (canEdit) => {
|
|
|
|
const response = await agent
|
|
|
|
.put(
|
|
|
|
`/api/private/notes/${user1NoteAlias}/metadata/permissions/users/${user2.username}`,
|
|
|
|
)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({ canEdit: canEdit });
|
|
|
|
expect(response.body.sharedToUsers[0].canEdit).toBe(canEdit);
|
|
|
|
expect(response.body.sharedToUsers[0].username).toBe(user2.username);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('DELETE /notes/{note}/metadata/permissions/users/{userName}', () => {
|
|
|
|
it('fails, when note does not exist', async () => {
|
|
|
|
await agent
|
|
|
|
.delete(
|
|
|
|
`/api/private/notes/notExisting/metadata/permissions/users/${user1.username}`,
|
|
|
|
)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails, when user is not the owner', async () => {
|
|
|
|
await agent
|
|
|
|
.delete(
|
|
|
|
`/api/private/notes/${user2NoteAlias}/metadata/permissions/users/${user1.username}`,
|
|
|
|
)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(403);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works', async () => {
|
2023-08-15 15:29:40 -04:00
|
|
|
const note =
|
|
|
|
await testSetup.notesService.getNoteByIdOrAlias(user1NoteAlias);
|
2023-05-04 11:47:48 -04:00
|
|
|
await testSetup.permissionsService.setUserPermission(
|
|
|
|
note,
|
|
|
|
user2,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
const response = await agent
|
|
|
|
.delete(
|
|
|
|
`/api/private/notes/${user1NoteAlias}/metadata/permissions/users/${user2.username}`,
|
|
|
|
)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({ canEdit: true });
|
|
|
|
expect(response.body.sharedToUsers).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('groups', () => {
|
|
|
|
describe('PUT /notes/{note}/metadata/permissions/groups/{groupName}', () => {
|
|
|
|
it('fails, when note does not exist', async () => {
|
|
|
|
await agent
|
|
|
|
.put(
|
|
|
|
`/api/private/notes/notExisting/metadata/permissions/groups/${user1.username}`,
|
|
|
|
)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails, when user is not the owner', async () => {
|
|
|
|
await agent
|
|
|
|
.put(
|
|
|
|
`/api/private/notes/${user2NoteAlias}/metadata/permissions/groups/${group1.name}`,
|
|
|
|
)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(403);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([true, false])('works with edit set to %s', async (canEdit) => {
|
|
|
|
const response = await agent
|
|
|
|
.put(
|
|
|
|
`/api/private/notes/${user1NoteAlias}/metadata/permissions/groups/${group1.name}`,
|
|
|
|
)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({ canEdit: canEdit });
|
|
|
|
expect(response.body.sharedToGroups[2].canEdit).toBe(canEdit);
|
|
|
|
expect(response.body.sharedToGroups[2].groupName).toBe(group1.name);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('DELETE /notes/{note}/metadata/permissions/groups/{groupName}', () => {
|
|
|
|
it('fails, when note does not exist', async () => {
|
|
|
|
await agent
|
|
|
|
.delete(
|
|
|
|
`/api/private/notes/notExisting/metadata/permissions/groups/${group1.name}`,
|
|
|
|
)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails, when user is not the owner', async () => {
|
|
|
|
await agent
|
|
|
|
.delete(
|
|
|
|
`/api/private/notes/${user2NoteAlias}/metadata/permissions/groups/${group1.name}`,
|
|
|
|
)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(403);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works', async () => {
|
2023-08-15 15:29:40 -04:00
|
|
|
const note =
|
|
|
|
await testSetup.notesService.getNoteByIdOrAlias(user1NoteAlias);
|
2023-05-04 11:47:48 -04:00
|
|
|
await testSetup.permissionsService.setGroupPermission(
|
|
|
|
note,
|
|
|
|
group1,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
const response = await agent
|
|
|
|
.delete(
|
|
|
|
`/api/private/notes/${user1NoteAlias}/metadata/permissions/groups/${group1.name}`,
|
|
|
|
)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({ canEdit: true });
|
|
|
|
expect(response.body.sharedToGroups).toHaveLength(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('owner', () => {
|
|
|
|
describe('PUT /notes/{note}/metadata/permissions/owner', () => {
|
|
|
|
it('fails, when note does not exist', async () => {
|
|
|
|
await agent
|
|
|
|
.put(`/api/private/notes/notExisting/metadata/permissions/owner`)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails, when user is not the owner', async () => {
|
|
|
|
await agent
|
|
|
|
.put(
|
|
|
|
`/api/private/notes/${user2NoteAlias}/metadata/permissions/owner`,
|
|
|
|
)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(403);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works', async () => {
|
|
|
|
const alias = 'noteForNewOwner';
|
|
|
|
await testSetup.notesService.createNote(
|
|
|
|
"I'll get a new owner!",
|
|
|
|
user1,
|
|
|
|
alias,
|
|
|
|
);
|
|
|
|
const response = await agent
|
|
|
|
.put(`/api/private/notes/${alias}/metadata/permissions/owner`)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.send({ newOwner: user2.username });
|
|
|
|
expect(response.body.metadata.permissions.owner).toBe(user2.username);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-03-23 20:49:43 -04:00
|
|
|
});
|