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 <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-10-15 16:44:43 +02:00
parent 0bb333ca69
commit 4428f3fb39
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 26 additions and 12 deletions

View file

@ -30,7 +30,7 @@ describe('Tokens', () => {
agent = request.agent(testSetup.app.getHttpServer()); agent = request.agent(testSetup.app.getHttpServer());
await agent await agent
.post('/auth/local/login') .post('/api/private/auth/local/login')
.send({ username: 'hardcoded', password: 'test' }) .send({ username: 'hardcoded', password: 'test' })
.expect(201); .expect(201);
}); });
@ -38,7 +38,7 @@ describe('Tokens', () => {
it(`POST /tokens`, async () => { it(`POST /tokens`, async () => {
const tokenName = 'testToken'; const tokenName = 'testToken';
const response = await agent const response = await agent
.post('/tokens') .post('/api/private/tokens')
.send({ .send({
label: tokenName, label: tokenName,
}) })
@ -54,7 +54,7 @@ describe('Tokens', () => {
it(`GET /tokens`, async () => { it(`GET /tokens`, async () => {
const tokenName = 'testToken'; const tokenName = 'testToken';
const response = await agent const response = await agent
.get('/tokens/') .get('/api/private/tokens/')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
expect(response.body[0].label).toBe(tokenName); expect(response.body[0].label).toBe(tokenName);
@ -63,12 +63,14 @@ describe('Tokens', () => {
expect(response.body[0].secret).not.toBeDefined(); expect(response.body[0].secret).not.toBeDefined();
}); });
it(`DELETE /tokens/:keyid`, async () => { 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({}); expect(response.body).toStrictEqual({});
}); });
it(`GET /tokens 2`, async () => { it(`GET /tokens 2`, async () => {
const response = await agent const response = await agent
.get('/tokens/') .get('/api/private/tokens/')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
expect(response.body).toStrictEqual([]); expect(response.body).toStrictEqual([]);

View file

@ -7,6 +7,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
import { NestExpressApplication } from '@nestjs/platform-express'; import { NestExpressApplication } from '@nestjs/platform-express';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
import { RouterModule, Routes } from 'nest-router';
import { PrivateApiModule } from '../src/api/private/private-api.module'; import { PrivateApiModule } from '../src/api/private/private-api.module';
import { PublicApiModule } from '../src/api/public/public-api.module'; import { PublicApiModule } from '../src/api/public/public-api.module';
@ -46,9 +47,27 @@ export class TestSetup {
public static async create(): Promise<TestSetup> { public static async create(): Promise<TestSetup> {
const testSetup = new TestSetup(); const testSetup = new TestSetup();
const routes: Routes = [
{
path: '/api/v2',
module: PublicApiModule,
},
{
path: '/api/private',
module: PrivateApiModule,
},
];
testSetup.moduleRef = await Test.createTestingModule({ testSetup.moduleRef = await Test.createTestingModule({
imports: [ imports: [
RouterModule.forRoutes(routes),
TypeOrmModule.forRoot({
type: 'sqlite',
database: ':memory:',
autoLoadEntities: true,
synchronize: true,
dropSchema: true,
}),
ConfigModule.forRoot({ ConfigModule.forRoot({
isGlobal: true, isGlobal: true,
load: [ load: [
@ -64,13 +83,6 @@ export class TestSetup {
NotesModule, NotesModule,
PermissionsModule, PermissionsModule,
GroupsModule, GroupsModule,
TypeOrmModule.forRoot({
type: 'sqlite',
database: ':memory:',
autoLoadEntities: true,
synchronize: true,
dropSchema: true,
}),
LoggerModule, LoggerModule,
AuthModule, AuthModule,
UsersModule, UsersModule,