2021-06-06 11:55:41 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* 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';
|
2021-10-14 15:25:17 -04:00
|
|
|
import { TestSetup } from '../test-setup';
|
2021-06-06 11:55:41 -04:00
|
|
|
|
2021-11-21 12:06:41 -05:00
|
|
|
describe('Alias', () => {
|
2021-10-14 15:25:17 -04:00
|
|
|
let testSetup: TestSetup;
|
|
|
|
|
2021-06-06 11:55:41 -04:00
|
|
|
let user: User;
|
|
|
|
let content: string;
|
|
|
|
let forbiddenNoteId: string;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2021-10-14 15:25:17 -04:00
|
|
|
testSetup = await TestSetup.create();
|
|
|
|
|
|
|
|
forbiddenNoteId =
|
|
|
|
testSetup.configService.get('appConfig').forbiddenNoteIds[0];
|
|
|
|
|
|
|
|
await testSetup.app.init();
|
|
|
|
|
|
|
|
user = await testSetup.userService.createUser('hardcoded', 'Testy');
|
2021-06-06 11:55:41 -04:00
|
|
|
content = 'This is a test note.';
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST /alias', () => {
|
|
|
|
const testAlias = 'aliasTest';
|
|
|
|
const newAliasDto: AliasCreateDto = {
|
|
|
|
noteIdOrAlias: testAlias,
|
|
|
|
newAlias: '',
|
|
|
|
};
|
|
|
|
let publicId = '';
|
|
|
|
beforeAll(async () => {
|
2021-10-14 15:25:17 -04:00
|
|
|
const note = await testSetup.notesService.createNote(
|
|
|
|
content,
|
|
|
|
user,
|
2021-11-14 15:44:59 -05:00
|
|
|
testAlias,
|
2021-10-14 15:25:17 -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-10-14 15:25:17 -04:00
|
|
|
const metadata = await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.post(`/api/v2/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-10-14 15:25:17 -04:00
|
|
|
const note = await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.get(`/api/v2/notes/${newAlias}`)
|
2021-06-06 11:55:41 -04:00
|
|
|
.expect(200);
|
|
|
|
expect(note.body.metadata.aliases).toContain(newAlias);
|
|
|
|
expect(note.body.metadata.primaryAlias).toBeTruthy();
|
|
|
|
expect(note.body.metadata.id).toEqual(publicId);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('does not create an alias', () => {
|
|
|
|
it('because of a forbidden alias', async () => {
|
|
|
|
newAliasDto.newAlias = forbiddenNoteId;
|
2021-10-14 15:25:17 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.post(`/api/v2/alias`)
|
2021-06-06 11:55:41 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(newAliasDto)
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
it('because of a alias that is a public id', async () => {
|
|
|
|
newAliasDto.newAlias = publicId;
|
2021-10-14 15:25:17 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.post(`/api/v2/alias`)
|
2021-06-06 11:55:41 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(newAliasDto)
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('PUT /alias/{alias}', () => {
|
|
|
|
const testAlias = 'aliasTest2';
|
|
|
|
const newAlias = 'normalAlias2';
|
|
|
|
const changeAliasDto: AliasUpdateDto = {
|
|
|
|
primaryAlias: true,
|
|
|
|
};
|
|
|
|
let publicId = '';
|
|
|
|
beforeAll(async () => {
|
2021-10-14 15:25:17 -04:00
|
|
|
const note = await testSetup.notesService.createNote(
|
|
|
|
content,
|
|
|
|
user,
|
2021-11-14 15:44:59 -05:00
|
|
|
testAlias,
|
2021-10-14 15:25:17 -04:00
|
|
|
);
|
2021-06-06 11:55:41 -04:00
|
|
|
publicId = note.publicId;
|
2021-10-14 15:25:17 -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-10-14 15:25:17 -04:00
|
|
|
const metadata = await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.put(`/api/v2/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-10-14 15:25:17 -04:00
|
|
|
const note = await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.get(`/api/v2/notes/${newAlias}`)
|
2021-06-06 11:55:41 -04:00
|
|
|
.expect(200);
|
|
|
|
expect(note.body.metadata.aliases).toContain(newAlias);
|
|
|
|
expect(note.body.metadata.primaryAlias).toBeTruthy();
|
|
|
|
expect(note.body.metadata.id).toEqual(publicId);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('does not update', () => {
|
|
|
|
it('a note with unknown alias', async () => {
|
2021-10-14 15:25:17 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.put(`/api/v2/alias/i_dont_exist`)
|
2021-06-06 11:55:41 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(changeAliasDto)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
it('if the property primaryAlias is false', async () => {
|
|
|
|
changeAliasDto.primaryAlias = false;
|
2021-10-14 15:25:17 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.put(`/api/v2/alias/${newAlias}`)
|
2021-06-06 11:55:41 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(changeAliasDto)
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('DELETE /alias/{alias}', () => {
|
|
|
|
const testAlias = 'aliasTest3';
|
|
|
|
const newAlias = 'normalAlias3';
|
|
|
|
beforeAll(async () => {
|
2021-10-14 15:25:17 -04:00
|
|
|
const note = await testSetup.notesService.createNote(
|
|
|
|
content,
|
|
|
|
user,
|
2021-11-14 15:44:59 -05:00
|
|
|
testAlias,
|
2021-10-14 15:25:17 -04:00
|
|
|
);
|
|
|
|
await testSetup.aliasService.addAlias(note, newAlias);
|
2021-06-06 11:55:41 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('deletes a normal alias', async () => {
|
2021-10-14 15:25:17 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.delete(`/api/v2/alias/${newAlias}`)
|
2021-06-06 11:55:41 -04:00
|
|
|
.expect(204);
|
2021-10-14 15:25:17 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.get(`/api/v2/notes/${newAlias}`)
|
2021-10-14 15:25:17 -04:00
|
|
|
.expect(404);
|
2021-06-06 11:55:41 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not delete an unknown alias', async () => {
|
2021-10-14 15:25:17 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.delete(`/api/v2/alias/i_dont_exist`)
|
2021-06-06 11:55:41 -04:00
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not delete a primary alias (if it is not the only one)', async () => {
|
2021-10-14 15:25:17 -04:00
|
|
|
const note = await testSetup.notesService.getNoteByIdOrAlias(testAlias);
|
|
|
|
await testSetup.aliasService.addAlias(note, newAlias);
|
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.delete(`/api/v2/alias/${testAlias}`)
|
2021-06-06 11:55:41 -04:00
|
|
|
.expect(400);
|
2021-10-14 15:25:17 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.get(`/api/v2/notes/${newAlias}`)
|
2021-10-14 15:25:17 -04:00
|
|
|
.expect(200);
|
2021-06-06 11:55:41 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('deletes a primary alias (if it is the only one)', async () => {
|
2021-10-14 15:25:17 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.delete(`/api/v2/alias/${newAlias}`)
|
2021-06-06 11:55:41 -04:00
|
|
|
.expect(204);
|
2021-10-14 15:25:17 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 10:53:10 -04:00
|
|
|
.delete(`/api/v2/alias/${testAlias}`)
|
2021-06-06 11:55:41 -04:00
|
|
|
.expect(204);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|