From ed0ea40758c05f54bad2d5eeea28d7a18a02b9b6 Mon Sep 17 00:00:00 2001 From: "Patrik J. Braun" Date: Mon, 25 Mar 2024 22:51:27 +0100 Subject: [PATCH] Improving integration tests. --- src/backend/model/extension/ExtensionManager.ts | 4 ++-- src/backend/server.ts | 17 ++++++++++------- src/common/config/private/PrivateConfigClass.ts | 3 +-- .../backend/integration/routers/PublicRouter.ts | 1 - .../integration/routers/SharingRouter.ts | 3 ++- test/backend/integration/routers/UserRouter.ts | 2 -- .../integration/routers/admin/SettingsRouter.ts | 8 +------- 7 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/backend/model/extension/ExtensionManager.ts b/src/backend/model/extension/ExtensionManager.ts index 36e8bb06..ab203a41 100644 --- a/src/backend/model/extension/ExtensionManager.ts +++ b/src/backend/model/extension/ExtensionManager.ts @@ -36,7 +36,7 @@ export class ExtensionManager implements IObjectManager { return; } this.router = express.Router(); - Server.getInstance().app.use(ExtensionManager.EXTENSION_API_PATH, this.router); + Server.instance?.app.use(ExtensionManager.EXTENSION_API_PATH, this.router); this.loadExtensionsList(); await this.initExtensions(); } @@ -163,7 +163,7 @@ export class ExtensionManager implements IObjectManager { } this.initEvents(); // reset events await this.cleanUpExtensions(); - Server.getInstance().app.use(ExtensionManager.EXTENSION_API_PATH, express.Router()); + Server.instance?.app.use(ExtensionManager.EXTENSION_API_PATH, this.router); this.extObjects = {}; } } diff --git a/src/backend/server.ts b/src/backend/server.ts index 697acdb5..382330e6 100644 --- a/src/backend/server.ts +++ b/src/backend/server.ts @@ -35,7 +35,7 @@ export class Server { public app: express.Express; private server: HttpServer; - static instance: Server = null; + public static instance: Server = null; public static getInstance(): Server { if (!this.instance) { @@ -148,18 +148,21 @@ export class Server { this.server.on('close', this.onClose); // Sigterm handler - process.on('SIGTERM', () => { - Logger.info(LOG_TAG, 'SIGTERM signal received'); - this.server.close(() => { - process.exit(0); - }); - }); + process.removeAllListeners('SIGTERM'); + process.on('SIGTERM', this.SIGTERM); if (!listen) { this.onStarted.trigger(); } } + private SIGTERM = () =>{ + Logger.info(LOG_TAG, 'SIGTERM signal received'); + this.server.close(() => { + process.exit(0); + }); + } + /** * Event listener for HTTP server "error" event. */ diff --git a/src/common/config/private/PrivateConfigClass.ts b/src/common/config/private/PrivateConfigClass.ts index 6ffb6c1f..f9d34834 100644 --- a/src/common/config/private/PrivateConfigClass.ts +++ b/src/common/config/private/PrivateConfigClass.ts @@ -2,11 +2,10 @@ import {ServerConfig} from './PrivateConfig'; import * as crypto from 'crypto'; import * as path from 'path'; -import {ConfigClass, ConfigClassBuilder} from 'typeconfig/node'; +import {ConfigClass} from 'typeconfig/node'; import {IConfigClass} from 'typeconfig/common'; import {PasswordHelper} from '../../../backend/model/PasswordHelper'; import {TAGS} from '../public/ClientConfig'; -import {NotificationManager} from '../../../backend/model/NotifocationManager'; declare const process: any; const upTime = new Date().toISOString(); diff --git a/test/backend/integration/routers/PublicRouter.ts b/test/backend/integration/routers/PublicRouter.ts index faf25fde..30af4e47 100644 --- a/test/backend/integration/routers/PublicRouter.ts +++ b/test/backend/integration/routers/PublicRouter.ts @@ -50,7 +50,6 @@ describe('PublicRouter', () => { await SQLConnection.close(); }; const tearDown = async () => { - await server.Stop(); await ObjectManagers.reset(); await fs.promises.rm(TestHelper.TMP_DIR, {recursive: true, force: true}); }; diff --git a/test/backend/integration/routers/SharingRouter.ts b/test/backend/integration/routers/SharingRouter.ts index ec7229b1..7e390c49 100644 --- a/test/backend/integration/routers/SharingRouter.ts +++ b/test/backend/integration/routers/SharingRouter.ts @@ -12,6 +12,7 @@ import {RouteTestingHelper} from './RouteTestingHelper'; import {QueryParams} from '../../../../src/common/QueryParams'; import {ErrorCodes} from '../../../../src/common/entities/Error'; import {DatabaseType} from '../../../../src/common/config/private/PrivateConfig'; +import {ProjectPath} from '../../../../src/backend/ProjectPath'; process.env.NODE_ENV = 'test'; @@ -38,6 +39,7 @@ describe('SharingRouter', () => { Config.Sharing.enabled = true; Config.Database.type = DatabaseType.sqlite; Config.Database.dbFolder = tempDir; + ProjectPath.reset(); server = new Server(false); await server.onStarted.wait(); @@ -47,7 +49,6 @@ describe('SharingRouter', () => { await SQLConnection.close(); }; const tearDown = async () => { - await server.Stop(); await ObjectManagers.reset(); await fs.promises.rm(tempDir, {recursive: true, force: true}); }; diff --git a/test/backend/integration/routers/UserRouter.ts b/test/backend/integration/routers/UserRouter.ts index 98391104..921b29bd 100644 --- a/test/backend/integration/routers/UserRouter.ts +++ b/test/backend/integration/routers/UserRouter.ts @@ -39,7 +39,6 @@ describe('UserRouter', () => { Config.Database.dbFolder = tempDir; ProjectPath.reset(); - server = new Server(false); await server.onStarted.wait(); await ObjectManagers.getInstance().init(); @@ -47,7 +46,6 @@ describe('UserRouter', () => { await SQLConnection.close(); }; const tearDown = async () => { - await server.Stop(); await ObjectManagers.reset(); await fs.promises.rm(tempDir, {recursive: true, force: true}); }; diff --git a/test/backend/integration/routers/admin/SettingsRouter.ts b/test/backend/integration/routers/admin/SettingsRouter.ts index 8fe8120e..c57c5b37 100644 --- a/test/backend/integration/routers/admin/SettingsRouter.ts +++ b/test/backend/integration/routers/admin/SettingsRouter.ts @@ -1,4 +1,3 @@ -import * as path from 'path'; import * as fs from 'fs'; import {Config} from '../../../../../src/common/config/private/Config'; import {Server} from '../../../../../src/backend/server'; @@ -9,8 +8,6 @@ import {ObjectManagers} from '../../../../../src/backend/model/ObjectManagers'; import {UserRoles} from '../../../../../src/common/entities/UserDTO'; import {ExtensionConfigWrapper} from '../../../../../src/backend/model/extension/ExtensionConfigWrapper'; import {TestHelper} from '../../../../TestHelper'; -import {Utils} from '../../../../../src/common/Utils'; -import {SQLConnection} from '../../../../../src/backend/model/database/SQLConnection'; process.env.NODE_ENV = 'test'; const chai: any = require('chai'); @@ -29,13 +26,11 @@ describe('SettingsRouter', () => { server = new Server(false); await server.onStarted.wait(); - console.log('done'); await ObjectManagers.getInstance().init(); }); afterEach(async () => { - await server.Stop(); await ObjectManagers.reset(); await fs.promises.rm(TestHelper.TMP_DIR, {recursive: true, force: true}); }); @@ -44,9 +39,8 @@ describe('SettingsRouter', () => { it('it should GET the settings', async () => { Config.Users.authenticationRequired = false; Config.Users.unAuthenticatedUserRole = UserRoles.Admin; - const originalSettings = await ExtensionConfigWrapper.original(); + const originalSettings = await ExtensionConfigWrapper.original(); - console.log('testing'); const result = await chai.request(server.Server) .get(Config.Server.apiPath + '/settings');