Migrate public alias API E2E test to global TestSetup

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-10-14 21:25:17 +02:00
parent ee946877e1
commit df97845e19
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 52 additions and 76 deletions

View file

@ -3,73 +3,29 @@
* *
* SPDX-License-Identifier: AGPL-3.0-only * 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 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 { AliasCreateDto } from '../../src/notes/alias-create.dto';
import { AliasUpdateDto } from '../../src/notes/alias-update.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 { User } from '../../src/users/user.entity';
import { UsersModule } from '../../src/users/users.module'; import { TestSetup } from '../test-setup';
import { UsersService } from '../../src/users/users.service';
describe('Notes', () => { describe('Notes', () => {
let app: INestApplication; let testSetup: TestSetup;
let notesService: NotesService;
let aliasService: AliasService;
let user: User; let user: User;
let content: string; let content: string;
let forbiddenNoteId: string; let forbiddenNoteId: string;
beforeAll(async () => { beforeAll(async () => {
const moduleRef = await Test.createTestingModule({ testSetup = await TestSetup.create();
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 =
forbiddenNoteId = config.get('appConfig').forbiddenNoteIds[0]; testSetup.configService.get('appConfig').forbiddenNoteIds[0];
app = moduleRef.createNestApplication();
await app.init(); await testSetup.app.init();
notesService = moduleRef.get(NotesService);
aliasService = moduleRef.get(AliasService); user = await testSetup.userService.createUser('hardcoded', 'Testy');
const userService = moduleRef.get(UsersService);
user = await userService.createUser('hardcoded', 'Testy');
content = 'This is a test note.'; content = 'This is a test note.';
}); });
@ -81,14 +37,18 @@ describe('Notes', () => {
}; };
let publicId = ''; let publicId = '';
beforeAll(async () => { beforeAll(async () => {
const note = await notesService.createNote(content, testAlias, user); const note = await testSetup.notesService.createNote(
content,
testAlias,
user,
);
publicId = note.publicId; publicId = note.publicId;
}); });
it('create with normal alias', async () => { it('create with normal alias', async () => {
const newAlias = 'normalAlias'; const newAlias = 'normalAlias';
newAliasDto.newAlias = newAlias; newAliasDto.newAlias = newAlias;
const metadata = await request(app.getHttpServer()) const metadata = await request(testSetup.app.getHttpServer())
.post(`/alias`) .post(`/alias`)
.set('Content-Type', 'application/json') .set('Content-Type', 'application/json')
.send(newAliasDto) .send(newAliasDto)
@ -96,7 +56,7 @@ describe('Notes', () => {
expect(metadata.body.name).toEqual(newAlias); expect(metadata.body.name).toEqual(newAlias);
expect(metadata.body.primaryAlias).toBeFalsy(); expect(metadata.body.primaryAlias).toBeFalsy();
expect(metadata.body.noteId).toEqual(publicId); expect(metadata.body.noteId).toEqual(publicId);
const note = await request(app.getHttpServer()) const note = await request(testSetup.app.getHttpServer())
.get(`/notes/${newAlias}`) .get(`/notes/${newAlias}`)
.expect(200); .expect(200);
expect(note.body.metadata.aliases).toContain(newAlias); expect(note.body.metadata.aliases).toContain(newAlias);
@ -107,7 +67,7 @@ describe('Notes', () => {
describe('does not create an alias', () => { describe('does not create an alias', () => {
it('because of a forbidden alias', async () => { it('because of a forbidden alias', async () => {
newAliasDto.newAlias = forbiddenNoteId; newAliasDto.newAlias = forbiddenNoteId;
await request(app.getHttpServer()) await request(testSetup.app.getHttpServer())
.post(`/alias`) .post(`/alias`)
.set('Content-Type', 'application/json') .set('Content-Type', 'application/json')
.send(newAliasDto) .send(newAliasDto)
@ -115,7 +75,7 @@ describe('Notes', () => {
}); });
it('because of a alias that is a public id', async () => { it('because of a alias that is a public id', async () => {
newAliasDto.newAlias = publicId; newAliasDto.newAlias = publicId;
await request(app.getHttpServer()) await request(testSetup.app.getHttpServer())
.post(`/alias`) .post(`/alias`)
.set('Content-Type', 'application/json') .set('Content-Type', 'application/json')
.send(newAliasDto) .send(newAliasDto)
@ -132,13 +92,17 @@ describe('Notes', () => {
}; };
let publicId = ''; let publicId = '';
beforeAll(async () => { beforeAll(async () => {
const note = await notesService.createNote(content, testAlias, user); const note = await testSetup.notesService.createNote(
content,
testAlias,
user,
);
publicId = note.publicId; publicId = note.publicId;
await aliasService.addAlias(note, newAlias); await testSetup.aliasService.addAlias(note, newAlias);
}); });
it('updates a note with a normal alias', async () => { it('updates a note with a normal alias', async () => {
const metadata = await request(app.getHttpServer()) const metadata = await request(testSetup.app.getHttpServer())
.put(`/alias/${newAlias}`) .put(`/alias/${newAlias}`)
.set('Content-Type', 'application/json') .set('Content-Type', 'application/json')
.send(changeAliasDto) .send(changeAliasDto)
@ -146,7 +110,7 @@ describe('Notes', () => {
expect(metadata.body.name).toEqual(newAlias); expect(metadata.body.name).toEqual(newAlias);
expect(metadata.body.primaryAlias).toBeTruthy(); expect(metadata.body.primaryAlias).toBeTruthy();
expect(metadata.body.noteId).toEqual(publicId); expect(metadata.body.noteId).toEqual(publicId);
const note = await request(app.getHttpServer()) const note = await request(testSetup.app.getHttpServer())
.get(`/notes/${newAlias}`) .get(`/notes/${newAlias}`)
.expect(200); .expect(200);
expect(note.body.metadata.aliases).toContain(newAlias); expect(note.body.metadata.aliases).toContain(newAlias);
@ -156,7 +120,7 @@ describe('Notes', () => {
describe('does not update', () => { describe('does not update', () => {
it('a note with unknown alias', async () => { it('a note with unknown alias', async () => {
await request(app.getHttpServer()) await request(testSetup.app.getHttpServer())
.put(`/alias/i_dont_exist`) .put(`/alias/i_dont_exist`)
.set('Content-Type', 'application/json') .set('Content-Type', 'application/json')
.send(changeAliasDto) .send(changeAliasDto)
@ -164,7 +128,7 @@ describe('Notes', () => {
}); });
it('if the property primaryAlias is false', async () => { it('if the property primaryAlias is false', async () => {
changeAliasDto.primaryAlias = false; changeAliasDto.primaryAlias = false;
await request(app.getHttpServer()) await request(testSetup.app.getHttpServer())
.put(`/alias/${newAlias}`) .put(`/alias/${newAlias}`)
.set('Content-Type', 'application/json') .set('Content-Type', 'application/json')
.send(changeAliasDto) .send(changeAliasDto)
@ -177,37 +141,45 @@ describe('Notes', () => {
const testAlias = 'aliasTest3'; const testAlias = 'aliasTest3';
const newAlias = 'normalAlias3'; const newAlias = 'normalAlias3';
beforeAll(async () => { beforeAll(async () => {
const note = await notesService.createNote(content, testAlias, user); const note = await testSetup.notesService.createNote(
await aliasService.addAlias(note, newAlias); content,
testAlias,
user,
);
await testSetup.aliasService.addAlias(note, newAlias);
}); });
it('deletes a normal alias', async () => { it('deletes a normal alias', async () => {
await request(app.getHttpServer()) await request(testSetup.app.getHttpServer())
.delete(`/alias/${newAlias}`) .delete(`/alias/${newAlias}`)
.expect(204); .expect(204);
await request(app.getHttpServer()).get(`/notes/${newAlias}`).expect(404); await request(testSetup.app.getHttpServer())
.get(`/notes/${newAlias}`)
.expect(404);
}); });
it('does not delete an unknown alias', async () => { it('does not delete an unknown alias', async () => {
await request(app.getHttpServer()) await request(testSetup.app.getHttpServer())
.delete(`/alias/i_dont_exist`) .delete(`/alias/i_dont_exist`)
.expect(404); .expect(404);
}); });
it('does not delete a primary alias (if it is not the only one)', async () => { it('does not delete a primary alias (if it is not the only one)', async () => {
const note = await notesService.getNoteByIdOrAlias(testAlias); const note = await testSetup.notesService.getNoteByIdOrAlias(testAlias);
await aliasService.addAlias(note, newAlias); await testSetup.aliasService.addAlias(note, newAlias);
await request(app.getHttpServer()) await request(testSetup.app.getHttpServer())
.delete(`/alias/${testAlias}`) .delete(`/alias/${testAlias}`)
.expect(400); .expect(400);
await request(app.getHttpServer()).get(`/notes/${newAlias}`).expect(200); await request(testSetup.app.getHttpServer())
.get(`/notes/${newAlias}`)
.expect(200);
}); });
it('deletes a primary alias (if it is the only one)', async () => { it('deletes a primary alias (if it is the only one)', async () => {
await request(app.getHttpServer()) await request(testSetup.app.getHttpServer())
.delete(`/alias/${newAlias}`) .delete(`/alias/${newAlias}`)
.expect(204); .expect(204);
await request(app.getHttpServer()) await request(testSetup.app.getHttpServer())
.delete(`/alias/${testAlias}`) .delete(`/alias/${testAlias}`)
.expect(204); .expect(204);
}); });

View file

@ -25,6 +25,7 @@ import { IdentityService } from '../src/identity/identity.service';
import { LoggerModule } from '../src/logger/logger.module'; import { LoggerModule } from '../src/logger/logger.module';
import { MediaModule } from '../src/media/media.module'; import { MediaModule } from '../src/media/media.module';
import { MediaService } from '../src/media/media.service'; import { MediaService } from '../src/media/media.service';
import { AliasService } from '../src/notes/alias.service';
import { NotesModule } from '../src/notes/notes.module'; import { NotesModule } from '../src/notes/notes.module';
import { NotesService } from '../src/notes/notes.service'; import { NotesService } from '../src/notes/notes.service';
import { PermissionsModule } from '../src/permissions/permissions.module'; import { PermissionsModule } from '../src/permissions/permissions.module';
@ -41,6 +42,7 @@ export class TestSetup {
notesService: NotesService; notesService: NotesService;
mediaService: MediaService; mediaService: MediaService;
historyService: HistoryService; historyService: HistoryService;
aliasService: AliasService;
public static async create(): Promise<TestSetup> { public static async create(): Promise<TestSetup> {
const testSetup = new TestSetup(); const testSetup = new TestSetup();
@ -91,6 +93,8 @@ export class TestSetup {
testSetup.moduleRef.get<MediaService>(MediaService); testSetup.moduleRef.get<MediaService>(MediaService);
testSetup.historyService = testSetup.historyService =
testSetup.moduleRef.get<HistoryService>(HistoryService); testSetup.moduleRef.get<HistoryService>(HistoryService);
testSetup.aliasService =
testSetup.moduleRef.get<AliasService>(AliasService);
testSetup.app = testSetup.moduleRef.createNestApplication(); testSetup.app = testSetup.moduleRef.createNestApplication();