From 4428f3fb394c3e0f30b771d3704f5599ec645e10 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Fri, 15 Oct 2021 16:44:43 +0200 Subject: [PATCH] Separate private and public API in TestSetup Including both PublicApiModule and PrivateApiModule in the test setup lead to the API routes overwriting each other. This adds a router to separate the APIs as they are in the normal app. Signed-off-by: David Mehren --- .../tokens.e2e-spec.ts | 12 +++++---- test/test-setup.ts | 26 ++++++++++++++----- 2 files changed, 26 insertions(+), 12 deletions(-) rename test/{public-api => private-api}/tokens.e2e-spec.ts (89%) diff --git a/test/public-api/tokens.e2e-spec.ts b/test/private-api/tokens.e2e-spec.ts similarity index 89% rename from test/public-api/tokens.e2e-spec.ts rename to test/private-api/tokens.e2e-spec.ts index 7bb96cc91..b49e769cf 100644 --- a/test/public-api/tokens.e2e-spec.ts +++ b/test/private-api/tokens.e2e-spec.ts @@ -30,7 +30,7 @@ describe('Tokens', () => { agent = request.agent(testSetup.app.getHttpServer()); await agent - .post('/auth/local/login') + .post('/api/private/auth/local/login') .send({ username: 'hardcoded', password: 'test' }) .expect(201); }); @@ -38,7 +38,7 @@ describe('Tokens', () => { it(`POST /tokens`, async () => { const tokenName = 'testToken'; const response = await agent - .post('/tokens') + .post('/api/private/tokens') .send({ label: tokenName, }) @@ -54,7 +54,7 @@ describe('Tokens', () => { it(`GET /tokens`, async () => { const tokenName = 'testToken'; const response = await agent - .get('/tokens/') + .get('/api/private/tokens/') .expect('Content-Type', /json/) .expect(200); expect(response.body[0].label).toBe(tokenName); @@ -63,12 +63,14 @@ describe('Tokens', () => { expect(response.body[0].secret).not.toBeDefined(); }); it(`DELETE /tokens/:keyid`, async () => { - const response = await agent.delete('/tokens/' + keyId).expect(204); + const response = await agent + .delete('/api/private/tokens/' + keyId) + .expect(204); expect(response.body).toStrictEqual({}); }); it(`GET /tokens 2`, async () => { const response = await agent - .get('/tokens/') + .get('/api/private/tokens/') .expect('Content-Type', /json/) .expect(200); expect(response.body).toStrictEqual([]); diff --git a/test/test-setup.ts b/test/test-setup.ts index 960c379fc..d283c9c45 100644 --- a/test/test-setup.ts +++ b/test/test-setup.ts @@ -7,6 +7,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config'; import { NestExpressApplication } from '@nestjs/platform-express'; import { Test, TestingModule } from '@nestjs/testing'; import { TypeOrmModule } from '@nestjs/typeorm'; +import { RouterModule, Routes } from 'nest-router'; import { PrivateApiModule } from '../src/api/private/private-api.module'; import { PublicApiModule } from '../src/api/public/public-api.module'; @@ -46,9 +47,27 @@ export class TestSetup { public static async create(): Promise { const testSetup = new TestSetup(); + const routes: Routes = [ + { + path: '/api/v2', + module: PublicApiModule, + }, + { + path: '/api/private', + module: PrivateApiModule, + }, + ]; testSetup.moduleRef = await Test.createTestingModule({ imports: [ + RouterModule.forRoutes(routes), + TypeOrmModule.forRoot({ + type: 'sqlite', + database: ':memory:', + autoLoadEntities: true, + synchronize: true, + dropSchema: true, + }), ConfigModule.forRoot({ isGlobal: true, load: [ @@ -64,13 +83,6 @@ export class TestSetup { NotesModule, PermissionsModule, GroupsModule, - TypeOrmModule.forRoot({ - type: 'sqlite', - database: ':memory:', - autoLoadEntities: true, - synchronize: true, - dropSchema: true, - }), LoggerModule, AuthModule, UsersModule,