From 56eaddff8ce73776f5f680774271a2ee41fa7ee1 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Sun, 6 Jun 2021 17:55:41 +0200 Subject: [PATCH] test: fix e2e tests to handle the new aliases Signed-off-by: Philip Molares --- test/private-api/alias.e2e-spec.ts | 219 +++++++++++++++++++++++++++ test/private-api/history.e2e-spec.ts | 20 ++- test/private-api/me.e2e-spec.ts | 6 +- test/public-api/alias.e2e-spec.ts | 215 ++++++++++++++++++++++++++ test/public-api/me.e2e-spec.ts | 24 ++- test/public-api/notes.e2e-spec.ts | 5 +- 6 files changed, 465 insertions(+), 24 deletions(-) create mode 100644 test/private-api/alias.e2e-spec.ts create mode 100644 test/public-api/alias.e2e-spec.ts diff --git a/test/private-api/alias.e2e-spec.ts b/test/private-api/alias.e2e-spec.ts new file mode 100644 index 000000000..9b620b04d --- /dev/null +++ b/test/private-api/alias.e2e-spec.ts @@ -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); + 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); + }); + }); +}); diff --git a/test/private-api/history.e2e-spec.ts b/test/private-api/history.e2e-spec.ts index 1aae79ffe..4a6b2878a 100644 --- a/test/private-api/history.e2e-spec.ts +++ b/test/private-api/history.e2e-spec.ts @@ -77,7 +77,7 @@ describe('History', () => { const userService = moduleRef.get(UsersService); user = await userService.createUser('hardcoded', 'Testy'); 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); }); @@ -109,7 +109,9 @@ describe('History', () => { const pinStatus = true; const lastVisited = new Date('2020-12-01 12:23:34'); const postEntryDto = new HistoryEntryImportDto(); - postEntryDto.note = note2.alias; + postEntryDto.note = note2.aliases.filter( + (alias) => alias.primary, + )[0].name; postEntryDto.pinStatus = pinStatus; postEntryDto.lastVisited = lastVisited; await request(app.getHttpServer()) @@ -119,7 +121,7 @@ describe('History', () => { .expect(201); const userEntries = await historyService.getEntriesByUser(user); 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].pinStatus).toEqual(pinStatus); expect(userEntries[0].updatedAt).toEqual(lastVisited); @@ -136,7 +138,9 @@ describe('History', () => { pinStatus = !previousHistory[0].pinStatus; lastVisited = new Date('2020-12-01 23:34:45'); postEntryDto = new HistoryEntryImportDto(); - postEntryDto.note = note2.alias; + postEntryDto.note = note2.aliases.filter( + (alias) => alias.primary, + )[0].name; postEntryDto.pinStatus = pinStatus; postEntryDto.lastVisited = lastVisited; }); @@ -165,7 +169,7 @@ describe('History', () => { afterEach(async () => { const historyEntries = await historyService.getEntriesByUser(user); 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( prevEntry.user.userName, ); @@ -184,8 +188,9 @@ describe('History', () => { it('PUT /me/history/:note', async () => { const entry = await historyService.updateHistoryEntryTimestamp(note2, user); expect(entry.pinStatus).toBeFalsy(); + const alias = entry.note.aliases.filter((alias) => alias.primary)[0].name; await request(app.getHttpServer()) - .put(`/me/history/${entry.note.alias || 'undefined'}`) + .put(`/me/history/${alias || 'undefined'}`) .send({ pinStatus: true }) .expect(200); const userEntries = await historyService.getEntriesByUser(user); @@ -196,10 +201,11 @@ describe('History', () => { it('DELETE /me/history/:note', async () => { 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 entryDto = historyService.toHistoryEntryDto(entry2); await request(app.getHttpServer()) - .delete(`/me/history/${entry.note.alias || 'undefined'}`) + .delete(`/me/history/${alias || 'undefined'}`) .expect(200); const userEntries = await historyService.getEntriesByUser(user); expect(userEntries.length).toEqual(1); diff --git a/test/private-api/me.e2e-spec.ts b/test/private-api/me.e2e-spec.ts index d0d35499e..b138bab47 100644 --- a/test/private-api/me.e2e-spec.ts +++ b/test/private-api/me.e2e-spec.ts @@ -45,6 +45,7 @@ describe('Me', () => { let user: User; let content: string; let note1: Note; + let alias2: string; let note2: Note; beforeAll(async () => { @@ -88,8 +89,9 @@ describe('Me', () => { user = await userService.createUser('hardcoded', 'Testy'); const notesService = moduleRef.get(NotesService); content = 'This is a test note.'; - note1 = await notesService.createNote(content, null, user); - note2 = await notesService.createNote(content, 'note2', user); + alias2 = 'note2'; + note1 = await notesService.createNote(content, undefined, user); + note2 = await notesService.createNote(content, alias2, user); }); it('GET /me', async () => { diff --git a/test/public-api/alias.e2e-spec.ts b/test/public-api/alias.e2e-spec.ts new file mode 100644 index 000000000..5bc359c61 --- /dev/null +++ b/test/public-api/alias.e2e-spec.ts @@ -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); + 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); + }); + }); +}); diff --git a/test/public-api/me.e2e-spec.ts b/test/public-api/me.e2e-spec.ts index 1c89b2b3b..8a5c7a241 100644 --- a/test/public-api/me.e2e-spec.ts +++ b/test/public-api/me.e2e-spec.ts @@ -157,15 +157,15 @@ describe('Me', () => { .send(historyEntryUpdateDto) .expect(200); const history = await historyService.getEntriesByUser(user); - let historyEntry: HistoryEntryDto = response.body; + const historyEntry: HistoryEntryDto = response.body; expect(historyEntry.pinStatus).toEqual(true); - historyEntry = null; - for (const e of history) { - if (e.note.alias === noteName) { - historyEntry = historyService.toHistoryEntryDto(e); + let theEntry: HistoryEntryDto; + for (const entry of history) { + if (entry.note.aliases.find((element) => element.name === noteName)) { + theEntry = historyService.toHistoryEntryDto(entry); } } - expect(historyEntry.pinStatus).toEqual(true); + expect(theEntry.pinStatus).toEqual(true); }); it('fails with a non-existing note', async () => { await request(app.getHttpServer()) @@ -185,13 +185,11 @@ describe('Me', () => { .expect(204); expect(response.body).toEqual({}); const history = await historyService.getEntriesByUser(user); - let historyEntry: HistoryEntry = null; - for (const e of history) { - if (e.note.alias === noteName) { - historyEntry = e; + for (const entry of history) { + if (entry.note.aliases.find((element) => element.name === noteName)) { + throw new Error('Deleted history entry still in history'); } } - return expect(historyEntry).toBeNull(); }); describe('fails', () => { it('with a non-existing note', async () => { @@ -218,8 +216,8 @@ describe('Me', () => { .expect(200); const noteMetaDtos = response.body as NoteMetadataDto[]; expect(noteMetaDtos).toHaveLength(1); - expect(noteMetaDtos[0].alias).toEqual(noteName); - expect(noteMetaDtos[0].updateUser.userName).toEqual(user.userName); + expect(noteMetaDtos[0].primaryAlias).toEqual(noteName); + expect(noteMetaDtos[0].updateUser?.userName).toEqual(user.userName); }); it('GET /me/media', async () => { diff --git a/test/public-api/notes.e2e-spec.ts b/test/public-api/notes.e2e-spec.ts index 531a527a5..400f5d3c1 100644 --- a/test/public-api/notes.e2e-spec.ts +++ b/test/public-api/notes.e2e-spec.ts @@ -208,7 +208,7 @@ describe('Notes', () => { updateNotePermission.sharedToGroups = []; await notesService.updateNotePermissions(note, updateNotePermission); const updatedNote = await notesService.getNoteByIdOrAlias( - note.alias ?? '', + note.aliases.filter((alias) => alias.primary)[0].name, ); expect(updatedNote.userPermissions).toHaveLength(1); expect(updatedNote.userPermissions[0].canEdit).toEqual( @@ -274,7 +274,8 @@ describe('Notes', () => { .get('/notes/test5/metadata') .expect(200); 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.description).toEqual(''); expect(typeof metadata.body.createTime).toEqual('string');