diff --git a/src/backend/server.ts b/src/backend/server.ts index a34b3643..697acdb5 100644 --- a/src/backend/server.ts +++ b/src/backend/server.ts @@ -44,21 +44,21 @@ export class Server { return this.instance; } - constructor() { + constructor(listen = true) { if (!(process.env.NODE_ENV === 'production')) { Logger.info( LOG_TAG, 'Running in DEBUG mode, set env variable NODE_ENV=production to disable ' ); } - this.init().catch(console.error); + this.init(listen).catch(console.error); } get Server(): HttpServer { return this.server; } - async init(): Promise { + async init(listen = true): Promise { this.app = express(); LoggerRouter.route(this.app); this.app.set('view engine', 'ejs'); @@ -68,11 +68,11 @@ export class Server { Logger.verbose( LOG_TAG, () => 'using config from ' + - ( - ConfigClassBuilder.attachPrivateInterface(Config) - .__options as ConfigClassOptions - ).configPath + - ':' + ( + ConfigClassBuilder.attachPrivateInterface(Config) + .__options as ConfigClassOptions + ).configPath + + ':' ); Logger.verbose(LOG_TAG, () => JSON.stringify(Config.toJSON({attachDescription: false}), (k, v) => { const MAX_LENGTH = 80; @@ -140,7 +140,9 @@ export class Server { this.server = _http.createServer(this.app); // Listen on provided PORT, on all network interfaces. - this.server.listen(Config.Server.port, Config.Server.host); + if (listen) { + this.server.listen(Config.Server.port, Config.Server.host); + } this.server.on('error', this.onError); this.server.on('listening', this.onListening); this.server.on('close', this.onClose); @@ -153,7 +155,9 @@ export class Server { }); }); - this.onStarted.trigger(); + if (!listen) { + this.onStarted.trigger(); + } } /** @@ -190,6 +194,7 @@ export class Server { const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; Logger.info(LOG_TAG, 'Listening on ' + bind); + this.onStarted.trigger(); }; /** @@ -198,6 +203,20 @@ export class Server { private onClose = () => { Logger.info(LOG_TAG, 'Closed http server'); }; + + public Stop(): Promise { + return new Promise((resolve, reject) => { + if(!this.server.listening){ + return resolve(); + } + this.server.close((err) => { + if (!err) { + return resolve(); + } + reject(err); + }); + }); + } } diff --git a/test/backend/integration/model/sql/typeorm.ts b/test/backend/integration/model/sql/typeorm.ts index d41b77af..faa4f5b6 100644 --- a/test/backend/integration/model/sql/typeorm.ts +++ b/test/backend/integration/model/sql/typeorm.ts @@ -17,24 +17,24 @@ import { import {VersionEntity} from '../../../../../src/backend/model/database/enitites/VersionEntity'; import {DatabaseType} from '../../../../../src/common/config/private/PrivateConfig'; import {ProjectPath} from '../../../../../src/backend/ProjectPath'; +import {TestHelper} from '../../../../TestHelper'; describe('Typeorm integration', () => { - const tempDir = path.join(__dirname, '../../tmp'); const setUpSqlDB = async () => { - await fs.promises.rm(tempDir, {recursive: true, force: true}); + await fs.promises.rm(TestHelper.TMP_DIR, {recursive: true, force: true}); Config.Database.type = DatabaseType.sqlite; - Config.Database.dbFolder = tempDir; + Config.Database.dbFolder = TestHelper.TMP_DIR; ProjectPath.reset(); }; const teardownUpSqlDB = async () => { await SQLConnection.close(); - await fs.promises.rm(tempDir, {recursive: true}); + await fs.promises.rm(TestHelper.TMP_DIR, {recursive: true}); }; beforeEach(async () => { diff --git a/test/backend/integration/routers/GalleryRouter.ts b/test/backend/integration/routers/GalleryRouter.ts index 7c718f99..41a5b42c 100644 --- a/test/backend/integration/routers/GalleryRouter.ts +++ b/test/backend/integration/routers/GalleryRouter.ts @@ -34,7 +34,7 @@ describe('GalleryRouter', (sqlHelper: DBTestHelper) => { Config.Media.folder = path.join(__dirname, '../../assets'); Config.Media.tempFolder = TestHelper.TMP_DIR; ProjectPath.reset(); - server = new Server(); + server = new Server(false); await server.onStarted.wait(); }; const tearDown = async () => { diff --git a/test/backend/integration/routers/PublicRouter.ts b/test/backend/integration/routers/PublicRouter.ts index 89e68dca..faf25fde 100644 --- a/test/backend/integration/routers/PublicRouter.ts +++ b/test/backend/integration/routers/PublicRouter.ts @@ -10,6 +10,9 @@ import {SuperAgentStatic} from 'superagent'; import {RouteTestingHelper} from './RouteTestingHelper'; import {QueryParams} from '../../../../src/common/QueryParams'; import {DatabaseType} from '../../../../src/common/config/private/PrivateConfig'; +import {TestHelper} from '../../../TestHelper'; +import {createLoggerWrapper} from '../../../../src/backend/Logger'; +import {ProjectPath} from '../../../../src/backend/ProjectPath'; process.env.NODE_ENV = 'test'; @@ -29,16 +32,17 @@ describe('PublicRouter', () => { permissions: null }; const {password: pass, ...expectedUser} = testUser; - const tempDir = path.join(__dirname, '../../tmp'); + let server: Server; const setUp = async () => { - await fs.promises.rm(tempDir, {recursive: true, force: true}); + await fs.promises.rm(TestHelper.TMP_DIR, {recursive: true, force: true}); Config.Users.authenticationRequired = true; Config.Sharing.enabled = true; Config.Database.type = DatabaseType.sqlite; - Config.Database.dbFolder = tempDir; + Config.Database.dbFolder = TestHelper.TMP_DIR; + ProjectPath.reset(); - server = new Server(); + server = new Server(false); await server.onStarted.wait(); await ObjectManagers.getInstance().init(); @@ -46,8 +50,9 @@ describe('PublicRouter', () => { await SQLConnection.close(); }; const tearDown = async () => { + await server.Stop(); await ObjectManagers.reset(); - await fs.promises.rm(tempDir, {recursive: true, force: true}); + await fs.promises.rm(TestHelper.TMP_DIR, {recursive: true, force: true}); }; const shouldHaveInjectedUser = (result: any, user: any) => { diff --git a/test/backend/integration/routers/SharingRouter.ts b/test/backend/integration/routers/SharingRouter.ts index 2a8a691a..ec7229b1 100644 --- a/test/backend/integration/routers/SharingRouter.ts +++ b/test/backend/integration/routers/SharingRouter.ts @@ -39,7 +39,7 @@ describe('SharingRouter', () => { Config.Database.type = DatabaseType.sqlite; Config.Database.dbFolder = tempDir; - server = new Server(); + server = new Server(false); await server.onStarted.wait(); await ObjectManagers.getInstance().init(); @@ -47,6 +47,7 @@ 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 6f1f7014..98391104 100644 --- a/test/backend/integration/routers/UserRouter.ts +++ b/test/backend/integration/routers/UserRouter.ts @@ -40,13 +40,14 @@ describe('UserRouter', () => { ProjectPath.reset(); - server = new Server(); + server = new Server(false); await server.onStarted.wait(); await ObjectManagers.getInstance().init(); await ObjectManagers.getInstance().UserManager.createUser(Utils.clone(testUser)); 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 1f68ab31..8fe8120e 100644 --- a/test/backend/integration/routers/admin/SettingsRouter.ts +++ b/test/backend/integration/routers/admin/SettingsRouter.ts @@ -8,6 +8,9 @@ import {TAGS} from '../../../../../src/common/config/public/ClientConfig'; 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'); @@ -17,29 +20,34 @@ chai.use(chaiHttp); describe('SettingsRouter', () => { - const tempDir = path.join(__dirname, '../../tmp'); + let server: Server; beforeEach(async () => { - await ObjectManagers.reset(); - await fs.promises.rm(tempDir, {recursive: true, force: true}); + await fs.promises.rm(TestHelper.TMP_DIR, {recursive: true, force: true}); Config.Database.type = DatabaseType.sqlite; - Config.Database.dbFolder = tempDir; + Config.Database.dbFolder = TestHelper.TMP_DIR; ProjectPath.reset(); + + 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(tempDir, {recursive: true, force: true}); + await fs.promises.rm(TestHelper.TMP_DIR, {recursive: true, force: true}); }); describe('/GET settings', () => { it('it should GET the settings', async () => { Config.Users.authenticationRequired = false; Config.Users.unAuthenticatedUserRole = UserRoles.Admin; - const originalSettings = await ExtensionConfigWrapper.original(); - const srv = new Server(); - await srv.onStarted.wait(); - const result = await chai.request(srv.Server) + const originalSettings = await ExtensionConfigWrapper.original(); + + console.log('testing'); + const result = await chai.request(server.Server) .get(Config.Server.apiPath + '/settings'); result.res.should.have.status(200);