test: fix e2e tests to handle the new aliases

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-06-06 17:55:41 +02:00
parent b95a6f56b6
commit 9f38b9036c
6 changed files with 465 additions and 24 deletions

View file

@ -0,0 +1,219 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { INestApplication } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Test } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import request from 'supertest';
import { PrivateApiModule } from '../../src/api/private/private-api.module';
import { AuthModule } from '../../src/auth/auth.module';
import appConfigMock from '../../src/config/mock/app.config.mock';
import authConfigMock from '../../src/config/mock/auth.config.mock';
import customizationConfigMock from '../../src/config/mock/customization.config.mock';
import externalConfigMock from '../../src/config/mock/external-services.config.mock';
import mediaConfigMock from '../../src/config/mock/media.config.mock';
import { GroupsModule } from '../../src/groups/groups.module';
import { LoggerModule } from '../../src/logger/logger.module';
import { AliasCreateDto } from '../../src/notes/alias-create.dto';
import { AliasUpdateDto } from '../../src/notes/alias-update.dto';
import { AliasService } from '../../src/notes/alias.service';
import { NotesModule } from '../../src/notes/notes.module';
import { NotesService } from '../../src/notes/notes.service';
import { PermissionsModule } from '../../src/permissions/permissions.module';
import { User } from '../../src/users/user.entity';
import { UsersModule } from '../../src/users/users.module';
import { UsersService } from '../../src/users/users.service';
describe('Alias', () => {
let app: INestApplication;
let aliasService: AliasService;
let notesService: NotesService;
let user: User;
let content: string;
let forbiddenNoteId: string;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [
mediaConfigMock,
appConfigMock,
authConfigMock,
customizationConfigMock,
externalConfigMock,
],
}),
PrivateApiModule,
NotesModule,
PermissionsModule,
GroupsModule,
TypeOrmModule.forRoot({
type: 'sqlite',
database: './hedgedoc-e2e-private-alias.sqlite',
autoLoadEntities: true,
synchronize: true,
dropSchema: true,
}),
LoggerModule,
AuthModule,
UsersModule,
],
}).compile();
const config = moduleRef.get<ConfigService>(ConfigService);
forbiddenNoteId = config.get('appConfig').forbiddenNoteIds[0];
app = moduleRef.createNestApplication();
await app.init();
aliasService = moduleRef.get(AliasService);
notesService = moduleRef.get(NotesService);
const userService = moduleRef.get(UsersService);
user = await userService.createUser('hardcoded', 'Testy');
content = 'This is a test note.';
});
describe('POST /alias', () => {
const testAlias = 'aliasTest';
const newAliasDto: AliasCreateDto = {
noteIdOrAlias: testAlias,
newAlias: '',
};
let publicId = '';
beforeAll(async () => {
const note = await notesService.createNote(content, testAlias, user);
publicId = note.publicId;
});
it('create with normal alias', async () => {
const newAlias = 'normalAlias';
newAliasDto.newAlias = newAlias;
const metadata = await request(app.getHttpServer())
.post(`/alias`)
.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);
const note = await request(app.getHttpServer())
.get(`/notes/${newAlias}`)
.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;
await request(app.getHttpServer())
.post(`/alias`)
.set('Content-Type', 'application/json')
.send(newAliasDto)
.expect(400);
});
it('because of a alias that is a public id', async () => {
newAliasDto.newAlias = publicId;
await request(app.getHttpServer())
.post(`/alias`)
.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 () => {
const note = await notesService.createNote(content, testAlias, user);
publicId = note.publicId;
await aliasService.addAlias(note, newAlias);
});
it('updates a note with a normal alias', async () => {
const metadata = await request(app.getHttpServer())
.put(`/alias/${newAlias}`)
.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);
const note = await request(app.getHttpServer())
.get(`/notes/${newAlias}`)
.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 () => {
await request(app.getHttpServer())
.put(`/alias/i_dont_exist`)
.set('Content-Type', 'application/json')
.send(changeAliasDto)
.expect(404);
});
it('if the property primaryAlias is false', async () => {
changeAliasDto.primaryAlias = false;
await request(app.getHttpServer())
.put(`/alias/${newAlias}`)
.set('Content-Type', 'application/json')
.send(changeAliasDto)
.expect(400);
});
});
});
describe('DELETE /alias/{alias}', () => {
const testAlias = 'aliasTest3';
const newAlias = 'normalAlias3';
beforeAll(async () => {
const note = await notesService.createNote(content, testAlias, user);
await aliasService.addAlias(note, newAlias);
});
it('deletes a normal alias', async () => {
await request(app.getHttpServer())
.delete(`/alias/${newAlias}`)
.expect(204);
await request(app.getHttpServer()).get(`/notes/${newAlias}`).expect(404);
});
it('does not delete an unknown alias', async () => {
await request(app.getHttpServer())
.delete(`/alias/i_dont_exist`)
.expect(404);
});
it('does not delete an primary alias (if it is not the only one)', async () => {
const note = await notesService.getNoteByIdOrAlias(testAlias);
await aliasService.addAlias(note, newAlias);
await request(app.getHttpServer())
.delete(`/alias/${testAlias}`)
.expect(400);
await request(app.getHttpServer()).get(`/notes/${newAlias}`).expect(200);
});
it('deletes a primary alias (if it is the only one)', async () => {
await request(app.getHttpServer())
.delete(`/alias/${newAlias}`)
.expect(204);
await request(app.getHttpServer())
.delete(`/alias/${testAlias}`)
.expect(204);
});
});
});

View file

@ -77,7 +77,7 @@ describe('History', () => {
const userService = moduleRef.get(UsersService); const userService = moduleRef.get(UsersService);
user = await userService.createUser('hardcoded', 'Testy'); user = await userService.createUser('hardcoded', 'Testy');
const notesService = moduleRef.get(NotesService); const notesService = moduleRef.get(NotesService);
note = await notesService.createNote(content, null, user); note = await notesService.createNote(content, 'note', user);
note2 = await notesService.createNote(content, 'note2', user); note2 = await notesService.createNote(content, 'note2', user);
}); });
@ -109,7 +109,9 @@ describe('History', () => {
const pinStatus = true; const pinStatus = true;
const lastVisited = new Date('2020-12-01 12:23:34'); const lastVisited = new Date('2020-12-01 12:23:34');
const postEntryDto = new HistoryEntryImportDto(); const postEntryDto = new HistoryEntryImportDto();
postEntryDto.note = note2.alias; postEntryDto.note = note2.aliases.filter(
(alias) => alias.primary,
)[0].name;
postEntryDto.pinStatus = pinStatus; postEntryDto.pinStatus = pinStatus;
postEntryDto.lastVisited = lastVisited; postEntryDto.lastVisited = lastVisited;
await request(app.getHttpServer()) await request(app.getHttpServer())
@ -119,7 +121,7 @@ describe('History', () => {
.expect(201); .expect(201);
const userEntries = await historyService.getEntriesByUser(user); const userEntries = await historyService.getEntriesByUser(user);
expect(userEntries.length).toEqual(1); expect(userEntries.length).toEqual(1);
expect(userEntries[0].note.alias).toEqual(note2.alias); expect(userEntries[0].note.aliases).toEqual(note2.aliases);
expect(userEntries[0].user.userName).toEqual(user.userName); expect(userEntries[0].user.userName).toEqual(user.userName);
expect(userEntries[0].pinStatus).toEqual(pinStatus); expect(userEntries[0].pinStatus).toEqual(pinStatus);
expect(userEntries[0].updatedAt).toEqual(lastVisited); expect(userEntries[0].updatedAt).toEqual(lastVisited);
@ -136,7 +138,9 @@ describe('History', () => {
pinStatus = !previousHistory[0].pinStatus; pinStatus = !previousHistory[0].pinStatus;
lastVisited = new Date('2020-12-01 23:34:45'); lastVisited = new Date('2020-12-01 23:34:45');
postEntryDto = new HistoryEntryImportDto(); postEntryDto = new HistoryEntryImportDto();
postEntryDto.note = note2.alias; postEntryDto.note = note2.aliases.filter(
(alias) => alias.primary,
)[0].name;
postEntryDto.pinStatus = pinStatus; postEntryDto.pinStatus = pinStatus;
postEntryDto.lastVisited = lastVisited; postEntryDto.lastVisited = lastVisited;
}); });
@ -165,7 +169,7 @@ describe('History', () => {
afterEach(async () => { afterEach(async () => {
const historyEntries = await historyService.getEntriesByUser(user); const historyEntries = await historyService.getEntriesByUser(user);
expect(historyEntries).toHaveLength(1); expect(historyEntries).toHaveLength(1);
expect(historyEntries[0].note.alias).toEqual(prevEntry.note.alias); expect(historyEntries[0].note.aliases).toEqual(prevEntry.note.aliases);
expect(historyEntries[0].user.userName).toEqual( expect(historyEntries[0].user.userName).toEqual(
prevEntry.user.userName, prevEntry.user.userName,
); );
@ -184,8 +188,9 @@ describe('History', () => {
it('PUT /me/history/:note', async () => { it('PUT /me/history/:note', async () => {
const entry = await historyService.updateHistoryEntryTimestamp(note2, user); const entry = await historyService.updateHistoryEntryTimestamp(note2, user);
expect(entry.pinStatus).toBeFalsy(); expect(entry.pinStatus).toBeFalsy();
const alias = entry.note.aliases.filter((alias) => alias.primary)[0].name;
await request(app.getHttpServer()) await request(app.getHttpServer())
.put(`/me/history/${entry.note.alias || 'undefined'}`) .put(`/me/history/${alias || 'undefined'}`)
.send({ pinStatus: true }) .send({ pinStatus: true })
.expect(200); .expect(200);
const userEntries = await historyService.getEntriesByUser(user); const userEntries = await historyService.getEntriesByUser(user);
@ -196,10 +201,11 @@ describe('History', () => {
it('DELETE /me/history/:note', async () => { it('DELETE /me/history/:note', async () => {
const entry = await historyService.updateHistoryEntryTimestamp(note2, user); const entry = await historyService.updateHistoryEntryTimestamp(note2, user);
const alias = entry.note.aliases.filter((alias) => alias.primary)[0].name;
const entry2 = await historyService.updateHistoryEntryTimestamp(note, user); const entry2 = await historyService.updateHistoryEntryTimestamp(note, user);
const entryDto = historyService.toHistoryEntryDto(entry2); const entryDto = historyService.toHistoryEntryDto(entry2);
await request(app.getHttpServer()) await request(app.getHttpServer())
.delete(`/me/history/${entry.note.alias || 'undefined'}`) .delete(`/me/history/${alias || 'undefined'}`)
.expect(200); .expect(200);
const userEntries = await historyService.getEntriesByUser(user); const userEntries = await historyService.getEntriesByUser(user);
expect(userEntries.length).toEqual(1); expect(userEntries.length).toEqual(1);

View file

@ -45,6 +45,7 @@ describe('Me', () => {
let user: User; let user: User;
let content: string; let content: string;
let note1: Note; let note1: Note;
let alias2: string;
let note2: Note; let note2: Note;
beforeAll(async () => { beforeAll(async () => {
@ -88,8 +89,9 @@ describe('Me', () => {
user = await userService.createUser('hardcoded', 'Testy'); user = await userService.createUser('hardcoded', 'Testy');
const notesService = moduleRef.get(NotesService); const notesService = moduleRef.get(NotesService);
content = 'This is a test note.'; content = 'This is a test note.';
note1 = await notesService.createNote(content, null, user); alias2 = 'note2';
note2 = await notesService.createNote(content, 'note2', user); note1 = await notesService.createNote(content, undefined, user);
note2 = await notesService.createNote(content, alias2, user);
}); });
it('GET /me', async () => { it('GET /me', async () => {

View file

@ -0,0 +1,215 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { INestApplication } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Test } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import request from 'supertest';
import { PublicApiModule } from '../../src/api/public/public-api.module';
import { AuthModule } from '../../src/auth/auth.module';
import { MockAuthGuard } from '../../src/auth/mock-auth.guard';
import { TokenAuthGuard } from '../../src/auth/token.strategy';
import appConfigMock from '../../src/config/mock/app.config.mock';
import mediaConfigMock from '../../src/config/mock/media.config.mock';
import { GroupsModule } from '../../src/groups/groups.module';
import { LoggerModule } from '../../src/logger/logger.module';
import { AliasCreateDto } from '../../src/notes/alias-create.dto';
import { AliasUpdateDto } from '../../src/notes/alias-update.dto';
import { AliasService } from '../../src/notes/alias.service';
import { NotesModule } from '../../src/notes/notes.module';
import { NotesService } from '../../src/notes/notes.service';
import { PermissionsModule } from '../../src/permissions/permissions.module';
import { User } from '../../src/users/user.entity';
import { UsersModule } from '../../src/users/users.module';
import { UsersService } from '../../src/users/users.service';
describe('Notes', () => {
let app: INestApplication;
let notesService: NotesService;
let aliasService: AliasService;
let user: User;
let content: string;
let forbiddenNoteId: string;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [mediaConfigMock, appConfigMock],
}),
PublicApiModule,
NotesModule,
PermissionsModule,
GroupsModule,
TypeOrmModule.forRoot({
type: 'sqlite',
database: './hedgedoc-e2e-notes.sqlite',
autoLoadEntities: true,
synchronize: true,
dropSchema: true,
}),
LoggerModule,
AuthModule,
UsersModule,
],
})
.overrideGuard(TokenAuthGuard)
.useClass(MockAuthGuard)
.compile();
const config = moduleRef.get<ConfigService>(ConfigService);
forbiddenNoteId = config.get('appConfig').forbiddenNoteIds[0];
app = moduleRef.createNestApplication();
await app.init();
notesService = moduleRef.get(NotesService);
aliasService = moduleRef.get(AliasService);
const userService = moduleRef.get(UsersService);
user = await userService.createUser('hardcoded', 'Testy');
content = 'This is a test note.';
});
describe('POST /alias', () => {
const testAlias = 'aliasTest';
const newAliasDto: AliasCreateDto = {
noteIdOrAlias: testAlias,
newAlias: '',
};
let publicId = '';
beforeAll(async () => {
const note = await notesService.createNote(content, testAlias, user);
publicId = note.publicId;
});
it('create with normal alias', async () => {
const newAlias = 'normalAlias';
newAliasDto.newAlias = newAlias;
const metadata = await request(app.getHttpServer())
.post(`/alias`)
.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);
const note = await request(app.getHttpServer())
.get(`/notes/${newAlias}`)
.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;
await request(app.getHttpServer())
.post(`/alias`)
.set('Content-Type', 'application/json')
.send(newAliasDto)
.expect(400);
});
it('because of a alias that is a public id', async () => {
newAliasDto.newAlias = publicId;
await request(app.getHttpServer())
.post(`/alias`)
.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 () => {
const note = await notesService.createNote(content, testAlias, user);
publicId = note.publicId;
await aliasService.addAlias(note, newAlias);
});
it('updates a note with a normal alias', async () => {
const metadata = await request(app.getHttpServer())
.put(`/alias/${newAlias}`)
.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);
const note = await request(app.getHttpServer())
.get(`/notes/${newAlias}`)
.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 () => {
await request(app.getHttpServer())
.put(`/alias/i_dont_exist`)
.set('Content-Type', 'application/json')
.send(changeAliasDto)
.expect(404);
});
it('if the property primaryAlias is false', async () => {
changeAliasDto.primaryAlias = false;
await request(app.getHttpServer())
.put(`/alias/${newAlias}`)
.set('Content-Type', 'application/json')
.send(changeAliasDto)
.expect(400);
});
});
});
describe('DELETE /alias/{alias}', () => {
const testAlias = 'aliasTest3';
const newAlias = 'normalAlias3';
beforeAll(async () => {
const note = await notesService.createNote(content, testAlias, user);
await aliasService.addAlias(note, newAlias);
});
it('deletes a normal alias', async () => {
await request(app.getHttpServer())
.delete(`/alias/${newAlias}`)
.expect(204);
await request(app.getHttpServer()).get(`/notes/${newAlias}`).expect(404);
});
it('does not delete an unknown alias', async () => {
await request(app.getHttpServer())
.delete(`/alias/i_dont_exist`)
.expect(404);
});
it('does not delete a primary alias (if it is not the only one)', async () => {
const note = await notesService.getNoteByIdOrAlias(testAlias);
await aliasService.addAlias(note, newAlias);
await request(app.getHttpServer())
.delete(`/alias/${testAlias}`)
.expect(400);
await request(app.getHttpServer()).get(`/notes/${newAlias}`).expect(200);
});
it('deletes a primary alias (if it is the only one)', async () => {
await request(app.getHttpServer())
.delete(`/alias/${newAlias}`)
.expect(204);
await request(app.getHttpServer())
.delete(`/alias/${testAlias}`)
.expect(204);
});
});
});

View file

@ -157,15 +157,15 @@ describe('Me', () => {
.send(historyEntryUpdateDto) .send(historyEntryUpdateDto)
.expect(200); .expect(200);
const history = await historyService.getEntriesByUser(user); const history = await historyService.getEntriesByUser(user);
let historyEntry: HistoryEntryDto = response.body; const historyEntry: HistoryEntryDto = response.body;
expect(historyEntry.pinStatus).toEqual(true); expect(historyEntry.pinStatus).toEqual(true);
historyEntry = null; let theEntry: HistoryEntryDto;
for (const e of history) { for (const entry of history) {
if (e.note.alias === noteName) { if (entry.note.aliases.find((element) => element.name === noteName)) {
historyEntry = historyService.toHistoryEntryDto(e); theEntry = historyService.toHistoryEntryDto(entry);
} }
} }
expect(historyEntry.pinStatus).toEqual(true); expect(theEntry.pinStatus).toEqual(true);
}); });
it('fails with a non-existing note', async () => { it('fails with a non-existing note', async () => {
await request(app.getHttpServer()) await request(app.getHttpServer())
@ -185,13 +185,11 @@ describe('Me', () => {
.expect(204); .expect(204);
expect(response.body).toEqual({}); expect(response.body).toEqual({});
const history = await historyService.getEntriesByUser(user); const history = await historyService.getEntriesByUser(user);
let historyEntry: HistoryEntry = null; for (const entry of history) {
for (const e of history) { if (entry.note.aliases.find((element) => element.name === noteName)) {
if (e.note.alias === noteName) { throw new Error('Deleted history entry still in history');
historyEntry = e;
} }
} }
return expect(historyEntry).toBeNull();
}); });
describe('fails', () => { describe('fails', () => {
it('with a non-existing note', async () => { it('with a non-existing note', async () => {
@ -218,8 +216,8 @@ describe('Me', () => {
.expect(200); .expect(200);
const noteMetaDtos = response.body as NoteMetadataDto[]; const noteMetaDtos = response.body as NoteMetadataDto[];
expect(noteMetaDtos).toHaveLength(1); expect(noteMetaDtos).toHaveLength(1);
expect(noteMetaDtos[0].alias).toEqual(noteName); expect(noteMetaDtos[0].primaryAlias).toEqual(noteName);
expect(noteMetaDtos[0].updateUser.userName).toEqual(user.userName); expect(noteMetaDtos[0].updateUser?.userName).toEqual(user.userName);
}); });
it('GET /me/media', async () => { it('GET /me/media', async () => {

View file

@ -208,7 +208,7 @@ describe('Notes', () => {
updateNotePermission.sharedToGroups = []; updateNotePermission.sharedToGroups = [];
await notesService.updateNotePermissions(note, updateNotePermission); await notesService.updateNotePermissions(note, updateNotePermission);
const updatedNote = await notesService.getNoteByIdOrAlias( const updatedNote = await notesService.getNoteByIdOrAlias(
note.alias ?? '', note.aliases.filter((alias) => alias.primary)[0].name,
); );
expect(updatedNote.userPermissions).toHaveLength(1); expect(updatedNote.userPermissions).toHaveLength(1);
expect(updatedNote.userPermissions[0].canEdit).toEqual( expect(updatedNote.userPermissions[0].canEdit).toEqual(
@ -274,7 +274,8 @@ describe('Notes', () => {
.get('/notes/test5/metadata') .get('/notes/test5/metadata')
.expect(200); .expect(200);
expect(typeof metadata.body.id).toEqual('string'); expect(typeof metadata.body.id).toEqual('string');
expect(metadata.body.alias).toEqual('test5'); expect(metadata.body.aliases).toEqual(['test5']);
expect(metadata.body.primaryAlias).toEqual('test5');
expect(metadata.body.title).toEqual(''); expect(metadata.body.title).toEqual('');
expect(metadata.body.description).toEqual(''); expect(metadata.body.description).toEqual('');
expect(typeof metadata.body.createTime).toEqual('string'); expect(typeof metadata.body.createTime).toEqual('string');