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