2021-06-06 15:55:41 +00:00
|
|
|
/*
|
2022-01-30 14:48:59 +00:00
|
|
|
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
2021-06-06 15:55:41 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
import request from 'supertest';
|
|
|
|
|
|
|
|
import { AliasCreateDto } from '../../src/notes/alias-create.dto';
|
|
|
|
import { AliasUpdateDto } from '../../src/notes/alias-update.dto';
|
|
|
|
import { User } from '../../src/users/user.entity';
|
2022-09-25 00:05:30 +00:00
|
|
|
import {
|
|
|
|
password1,
|
|
|
|
password2,
|
|
|
|
TestSetup,
|
|
|
|
TestSetupBuilder,
|
|
|
|
username1,
|
|
|
|
username2,
|
|
|
|
} from '../test-setup';
|
2021-06-06 15:55:41 +00:00
|
|
|
|
|
|
|
describe('Alias', () => {
|
2021-10-15 15:06:56 +00:00
|
|
|
let testSetup: TestSetup;
|
|
|
|
|
2021-12-07 16:20:28 +00:00
|
|
|
let users: User[];
|
|
|
|
const content = 'This is a test note.';
|
2021-06-06 15:55:41 +00:00
|
|
|
let forbiddenNoteId: string;
|
2021-10-15 15:06:56 +00:00
|
|
|
|
2021-12-07 16:20:28 +00:00
|
|
|
let agent1: request.SuperAgentTest;
|
|
|
|
let agent2: request.SuperAgentTest;
|
2021-06-06 15:55:41 +00:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2022-01-06 20:41:36 +00:00
|
|
|
testSetup = await TestSetupBuilder.create().withUsers().build();
|
2021-12-07 16:20:28 +00:00
|
|
|
await testSetup.app.init();
|
2021-10-15 15:06:56 +00:00
|
|
|
|
|
|
|
forbiddenNoteId =
|
2022-01-30 14:48:59 +00:00
|
|
|
testSetup.configService.get('noteConfig').forbiddenNoteIds[0];
|
2021-12-07 16:20:28 +00:00
|
|
|
users = testSetup.users;
|
2021-12-07 15:40:34 +00:00
|
|
|
|
2021-12-07 16:20:28 +00:00
|
|
|
agent1 = request.agent(testSetup.app.getHttpServer());
|
|
|
|
await agent1
|
2021-10-15 15:06:56 +00:00
|
|
|
.post('/api/private/auth/local/login')
|
2022-09-25 00:05:30 +00:00
|
|
|
.send({ username: username1, password: password1 })
|
2021-10-13 18:43:56 +00:00
|
|
|
.expect(201);
|
2021-12-07 16:20:28 +00:00
|
|
|
|
|
|
|
agent2 = request.agent(testSetup.app.getHttpServer());
|
|
|
|
await agent2
|
|
|
|
.post('/api/private/auth/local/login')
|
2022-09-25 00:05:30 +00:00
|
|
|
.send({ username: username2, password: password2 })
|
2021-12-07 16:20:28 +00:00
|
|
|
.expect(201);
|
2021-06-06 15:55:41 +00:00
|
|
|
});
|
|
|
|
|
2022-03-04 12:13:46 +00:00
|
|
|
afterAll(async () => {
|
|
|
|
await testSetup.cleanup();
|
|
|
|
});
|
|
|
|
|
2021-06-06 15:55:41 +00:00
|
|
|
describe('POST /alias', () => {
|
|
|
|
const testAlias = 'aliasTest';
|
|
|
|
const newAliasDto: AliasCreateDto = {
|
|
|
|
noteIdOrAlias: testAlias,
|
|
|
|
newAlias: '',
|
|
|
|
};
|
|
|
|
let publicId = '';
|
|
|
|
beforeAll(async () => {
|
2021-10-15 15:06:56 +00:00
|
|
|
const note = await testSetup.notesService.createNote(
|
|
|
|
content,
|
2021-12-07 16:20:28 +00:00
|
|
|
users[0],
|
2021-11-14 20:44:59 +00:00
|
|
|
testAlias,
|
2021-10-15 15:06:56 +00:00
|
|
|
);
|
2021-06-06 15:55:41 +00:00
|
|
|
publicId = note.publicId;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('create with normal alias', async () => {
|
|
|
|
const newAlias = 'normalAlias';
|
|
|
|
newAliasDto.newAlias = newAlias;
|
2021-12-07 16:20:28 +00:00
|
|
|
const metadata = await agent1
|
2021-10-15 15:06:56 +00:00
|
|
|
.post(`/api/private/alias`)
|
2021-06-06 15:55:41 +00:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(newAliasDto)
|
|
|
|
.expect(201);
|
|
|
|
expect(metadata.body.name).toEqual(newAlias);
|
|
|
|
expect(metadata.body.primaryAlias).toBeFalsy();
|
|
|
|
expect(metadata.body.noteId).toEqual(publicId);
|
2021-12-07 16:20:28 +00:00
|
|
|
const note = await agent1
|
2021-10-15 15:06:56 +00:00
|
|
|
.get(`/api/private/notes/${newAlias}`)
|
|
|
|
.expect(200);
|
2022-02-14 14:26:29 +00:00
|
|
|
expect(note.body.metadata.aliases).toContainEqual({
|
|
|
|
name: 'normalAlias',
|
|
|
|
primaryAlias: false,
|
|
|
|
noteId: publicId,
|
|
|
|
});
|
|
|
|
expect(note.body.metadata.primaryAddress).toEqual(testAlias);
|
2021-06-06 15:55:41 +00:00
|
|
|
expect(note.body.metadata.id).toEqual(publicId);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('does not create an alias', () => {
|
|
|
|
it('because of a forbidden alias', async () => {
|
|
|
|
newAliasDto.newAlias = forbiddenNoteId;
|
2021-12-07 16:20:28 +00:00
|
|
|
await agent1
|
2021-10-15 15:06:56 +00:00
|
|
|
.post(`/api/private/alias`)
|
2021-06-06 15:55:41 +00:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(newAliasDto)
|
2021-12-07 16:20:28 +00:00
|
|
|
.expect(400)
|
|
|
|
.then((response) => {
|
|
|
|
expect(response.body.message).toContain(
|
|
|
|
'is forbidden by the administrator',
|
|
|
|
);
|
|
|
|
});
|
2021-06-06 15:55:41 +00:00
|
|
|
});
|
|
|
|
it('because of a alias that is a public id', async () => {
|
|
|
|
newAliasDto.newAlias = publicId;
|
2021-12-07 16:20:28 +00:00
|
|
|
await agent1
|
2021-10-15 15:06:56 +00:00
|
|
|
.post(`/api/private/alias`)
|
2021-06-06 15:55:41 +00:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(newAliasDto)
|
2022-01-23 21:23:00 +00:00
|
|
|
.expect(409);
|
2021-06-06 15:55:41 +00:00
|
|
|
});
|
2021-12-07 16:20:28 +00:00
|
|
|
it('because the user is not an owner', async () => {
|
|
|
|
newAliasDto.newAlias = publicId;
|
|
|
|
await agent2
|
|
|
|
.post(`/api/private/alias`)
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(newAliasDto)
|
|
|
|
.expect(401);
|
|
|
|
});
|
2021-06-06 15:55:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('PUT /alias/{alias}', () => {
|
|
|
|
const testAlias = 'aliasTest2';
|
|
|
|
const newAlias = 'normalAlias2';
|
|
|
|
const changeAliasDto: AliasUpdateDto = {
|
|
|
|
primaryAlias: true,
|
|
|
|
};
|
|
|
|
let publicId = '';
|
|
|
|
beforeAll(async () => {
|
2021-10-15 15:06:56 +00:00
|
|
|
const note = await testSetup.notesService.createNote(
|
|
|
|
content,
|
2021-12-07 16:20:28 +00:00
|
|
|
users[0],
|
2021-11-14 20:44:59 +00:00
|
|
|
testAlias,
|
2021-10-15 15:06:56 +00:00
|
|
|
);
|
2021-06-06 15:55:41 +00:00
|
|
|
publicId = note.publicId;
|
2021-10-15 15:06:56 +00:00
|
|
|
await testSetup.aliasService.addAlias(note, newAlias);
|
2021-06-06 15:55:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('updates a note with a normal alias', async () => {
|
2021-12-07 16:20:28 +00:00
|
|
|
const metadata = await agent1
|
2021-10-15 15:06:56 +00:00
|
|
|
.put(`/api/private/alias/${newAlias}`)
|
2021-06-06 15:55:41 +00:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(changeAliasDto)
|
|
|
|
.expect(200);
|
|
|
|
expect(metadata.body.name).toEqual(newAlias);
|
|
|
|
expect(metadata.body.primaryAlias).toBeTruthy();
|
|
|
|
expect(metadata.body.noteId).toEqual(publicId);
|
2021-12-07 16:20:28 +00:00
|
|
|
const note = await agent1
|
2021-10-15 15:06:56 +00:00
|
|
|
.get(`/api/private/notes/${newAlias}`)
|
|
|
|
.expect(200);
|
2022-02-14 14:26:29 +00:00
|
|
|
expect(note.body.metadata.aliases).toContainEqual({
|
|
|
|
name: newAlias,
|
|
|
|
primaryAlias: true,
|
|
|
|
noteId: publicId,
|
|
|
|
});
|
|
|
|
expect(note.body.metadata.primaryAddress).toEqual(newAlias);
|
2021-06-06 15:55:41 +00:00
|
|
|
expect(note.body.metadata.id).toEqual(publicId);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('does not update', () => {
|
|
|
|
it('a note with unknown alias', async () => {
|
2021-12-07 16:20:28 +00:00
|
|
|
await agent1
|
2021-10-15 15:06:56 +00:00
|
|
|
.put(`/api/private/alias/i_dont_exist`)
|
2021-06-06 15:55:41 +00:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(changeAliasDto)
|
|
|
|
.expect(404);
|
|
|
|
});
|
2021-12-07 16:20:28 +00:00
|
|
|
it('a note with a forbidden ID', async () => {
|
|
|
|
await agent1
|
|
|
|
.put(`/api/private/alias/${forbiddenNoteId}`)
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(changeAliasDto)
|
|
|
|
.expect(400)
|
|
|
|
.then((response) => {
|
|
|
|
expect(response.body.message).toContain(
|
|
|
|
'is forbidden by the administrator',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2021-06-06 15:55:41 +00:00
|
|
|
it('if the property primaryAlias is false', async () => {
|
|
|
|
changeAliasDto.primaryAlias = false;
|
2021-12-07 16:20:28 +00:00
|
|
|
await agent1
|
2021-10-15 15:06:56 +00:00
|
|
|
.put(`/api/private/alias/${newAlias}`)
|
2021-06-06 15:55:41 +00:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(changeAliasDto)
|
|
|
|
.expect(400);
|
|
|
|
});
|
2021-12-07 16:20:28 +00:00
|
|
|
it('if the user is not an owner', async () => {
|
|
|
|
changeAliasDto.primaryAlias = true;
|
|
|
|
await agent2
|
|
|
|
.put(`/api/private/alias/${newAlias}`)
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(changeAliasDto)
|
|
|
|
.expect(401);
|
|
|
|
});
|
2021-06-06 15:55:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('DELETE /alias/{alias}', () => {
|
|
|
|
const testAlias = 'aliasTest3';
|
|
|
|
const newAlias = 'normalAlias3';
|
2021-12-07 16:20:28 +00:00
|
|
|
let note;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
note = await testSetup.notesService.createNote(
|
2021-10-15 15:06:56 +00:00
|
|
|
content,
|
2021-12-07 16:20:28 +00:00
|
|
|
users[0],
|
2021-11-14 20:44:59 +00:00
|
|
|
testAlias,
|
2021-10-15 15:06:56 +00:00
|
|
|
);
|
|
|
|
await testSetup.aliasService.addAlias(note, newAlias);
|
2021-06-06 15:55:41 +00:00
|
|
|
});
|
|
|
|
|
2021-12-07 16:20:28 +00:00
|
|
|
afterEach(async () => {
|
|
|
|
try {
|
|
|
|
await testSetup.aliasService.removeAlias(note, newAlias);
|
|
|
|
// Ignore errors on removing alias
|
|
|
|
// eslint-disable-next-line no-empty
|
|
|
|
} catch (e) {}
|
|
|
|
await testSetup.notesService.deleteNote(note);
|
|
|
|
});
|
|
|
|
|
2021-06-06 15:55:41 +00:00
|
|
|
it('deletes a normal alias', async () => {
|
2021-12-07 16:20:28 +00:00
|
|
|
await agent1.delete(`/api/private/alias/${newAlias}`).expect(204);
|
|
|
|
await agent1.get(`/api/private/notes/${newAlias}`).expect(404);
|
2021-06-06 15:55:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not delete an unknown alias', async () => {
|
2021-12-07 16:20:28 +00:00
|
|
|
await agent1.delete(`/api/private/alias/i_dont_exist`).expect(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not delete an alias of a forbidden note', async () => {
|
|
|
|
await agent1
|
|
|
|
.delete(`/api/private/alias/${forbiddenNoteId}`)
|
|
|
|
.expect(400)
|
|
|
|
.then((response) => {
|
|
|
|
expect(response.body.message).toContain(
|
|
|
|
'is forbidden by the administrator',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails if the user does not own the note', async () => {
|
|
|
|
await agent2.delete(`/api/private/alias/${newAlias}`).expect(401);
|
2021-06-06 15:55:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not delete an primary alias (if it is not the only one)', async () => {
|
2021-12-07 16:20:28 +00:00
|
|
|
await agent1.delete(`/api/private/alias/${testAlias}`).expect(400);
|
|
|
|
await agent1.get(`/api/private/notes/${newAlias}`).expect(200);
|
2021-06-06 15:55:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('deletes a primary alias (if it is the only one)', async () => {
|
2021-12-07 16:20:28 +00:00
|
|
|
await agent1.delete(`/api/private/alias/${newAlias}`).expect(204);
|
|
|
|
await agent1.delete(`/api/private/alias/${testAlias}`).expect(204);
|
2021-06-06 15:55:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|