From 9354c5a15ed1b48fa2e3e07fbac095e3e6101f27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Sun, 11 Apr 2021 22:40:29 +0200 Subject: [PATCH] fix: simplify e2e tests by leverarging unelash.create method --- src/test/e2e/api/admin/addon.e2e.test.js | 43 ++++++++++++++---------- src/test/e2e/helpers/test-helper-2.js | 42 +++++++++++++++++++++++ 2 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 src/test/e2e/helpers/test-helper-2.js diff --git a/src/test/e2e/api/admin/addon.e2e.test.js b/src/test/e2e/api/admin/addon.e2e.test.js index f6fee50bda..dc7a5638b8 100644 --- a/src/test/e2e/api/admin/addon.e2e.test.js +++ b/src/test/e2e/api/admin/addon.e2e.test.js @@ -2,27 +2,43 @@ const test = require('ava'); -const dbInit = require('../../helpers/database-init'); -const { setupApp } = require('../../helpers/test-helper'); -const getLogger = require('../../../fixtures/no-logger'); +// const dbInit = require('../../helpers/database-init'); +const { setupApp } = require('../../helpers/test-helper-2'); +// const getLogger = require('../../../fixtures/no-logger'); const MASKED_VALUE = '*****'; -let stores; -let db; +// let stores; +let request; +let stop; +let currentUser; test.before(async () => { - db = await dbInit('addon_api_serial', getLogger); - stores = db.stores; + const preHook = app => { + app.use((req, res, next) => { + req.user = currentUser; + next(); + }); + }; + const data = await setupApp('addon_api_serial', preHook); + request = data.request; + currentUser = await data.services.userService.loginUserWithoutPassword( + 'regular-test@getunleash.io', + true, + ); + + stop = data.stop; + // stores = db.stores; }); test.after(async () => { - await db.destroy(); + // await db.destroy(); + await stop(); }); test.serial('gets all addons', async t => { t.plan(3); - const request = await setupApp(stores); + return request .get('/api/admin/addons') .expect('Content-Type', /json/) @@ -36,7 +52,6 @@ test.serial('gets all addons', async t => { test.serial('should not be able to create invalid addon', async t => { t.plan(0); - const request = await setupApp(stores); return request .post('/api/admin/addons') .send({ invalid: 'field' }) @@ -45,7 +60,6 @@ test.serial('should not be able to create invalid addon', async t => { test.serial('should create addon configuration', async t => { t.plan(0); - const request = await setupApp(stores); const config = { provider: 'webhook', @@ -65,7 +79,6 @@ test.serial('should create addon configuration', async t => { test.serial('should delete addon configuration', async t => { t.plan(0); - const request = await setupApp(stores); const config = { provider: 'webhook', @@ -88,7 +101,6 @@ test.serial('should delete addon configuration', async t => { test.serial('should update addon configuration', async t => { t.plan(2); - const request = await setupApp(stores); const config = { provider: 'webhook', @@ -135,7 +147,6 @@ test.serial('should update addon configuration', async t => { test.serial('should not update with invalid addon configuration', async t => { t.plan(0); - const request = await setupApp(stores); const config = { enabled: true, @@ -154,7 +165,6 @@ test.serial('should not update with invalid addon configuration', async t => { test.serial('should not update unknown addon configuration', async t => { t.plan(0); - const request = await setupApp(stores); const config = { provider: 'webhook', @@ -174,7 +184,6 @@ test.serial('should not update unknown addon configuration', async t => { test.serial('should get addon configuration', async t => { t.plan(3); - const request = await setupApp(stores); const config = { provider: 'webhook', @@ -208,14 +217,12 @@ test.serial('should get addon configuration', async t => { test.serial('should not get unknown addon configuration', async t => { t.plan(0); - const request = await setupApp(stores); await request.get('/api/admin/addons/445').expect(404); }); test.serial('should not delete unknown addon configuration', async t => { t.plan(0); - const request = await setupApp(stores); return request.delete('/api/admin/addons/21231').expect(404); }); diff --git a/src/test/e2e/helpers/test-helper-2.js b/src/test/e2e/helpers/test-helper-2.js new file mode 100644 index 0000000000..d07d3bb586 --- /dev/null +++ b/src/test/e2e/helpers/test-helper-2.js @@ -0,0 +1,42 @@ +process.env.NODE_ENV = 'test'; +/* eslint-disable-next-line */ +const supertest = require('supertest'); + +const { create } = require('../../../lib/server-impl'); +const getLogger = require('../../fixtures/no-logger'); +const dbConfig = require('./database-config'); +const migrator = require('../../../migrator'); + +process.setMaxListeners(0); + +async function prepareDatabase(db, databaseSchema) { + await db.raw(`DROP SCHEMA IF EXISTS ${databaseSchema} CASCADE`); + await db.raw(`CREATE SCHEMA IF NOT EXISTS ${databaseSchema}`); + await migrator({ db: dbConfig.getDb(), databaseSchema }); +} + +module.exports = { + async setupApp(databaseSchema, preHook) { + const { app, config, stop, services } = await create({ + preHook, + serverMetrics: false, + databaseSchema, + disableDBMigration: true, + db: dbConfig.getDb(), + session: { + db: false, + age: 4000, + }, + getLogger, + }); + config.stores.clientMetricsStore.setMaxListeners(0); + config.stores.eventStore.setMaxListeners(0); + await prepareDatabase(config.stores.db, databaseSchema); + return { + request: supertest.agent(app), + stop, + config, + services, + }; + }, +};