From d1fd5d3900eb5149b8ebfb48c9665de447ba8258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Thu, 3 Oct 2019 15:01:33 +0200 Subject: [PATCH] fix: e2e tests should only set up one database per test file (#504) * fix: e2e tests should only set up one database per test file * fix: Allow e2e-tests to reset db content --- test/e2e/api/admin/event.e2e.test.js | 25 +++- .../e2e/api/admin/feature-archive.e2e.test.js | 37 +++--- test/e2e/api/admin/feature.auth.e2e.test.js | 31 +++-- .../api/admin/feature.custom-auth.e2e.test.js | 38 +++--- test/e2e/api/admin/feature.e2e.test.js | 122 ++++++++---------- test/e2e/api/admin/metrics.e2e.test.js | 48 ++++--- test/e2e/api/admin/state.e2e.test.js | 50 +++---- test/e2e/api/admin/strategy.e2e.test.js | 70 +++++----- test/e2e/api/client/feature.e2e.test.js | 30 +++-- test/e2e/api/client/metrics.e2e.test.js | 23 +++- test/e2e/health.e2e.test.js | 18 ++- test/e2e/helpers/database-init.js | 8 +- test/e2e/helpers/test-helper.js | 47 +------ 13 files changed, 294 insertions(+), 253 deletions(-) diff --git a/test/e2e/api/admin/event.e2e.test.js b/test/e2e/api/admin/event.e2e.test.js index 5f33eebf17..0bcdea67d7 100644 --- a/test/e2e/api/admin/event.e2e.test.js +++ b/test/e2e/api/admin/event.e2e.test.js @@ -1,24 +1,35 @@ 'use strict'; const test = require('ava'); -const { setupApp } = require('./../../helpers/test-helper'); +const { setupApp } = require('../../helpers/test-helper'); +const dbInit = require('../../helpers/database-init'); +const getLogger = require('../../../fixtures/no-logger'); + +let stores; + +test.before(async () => { + const db = await dbInit('event_api_serial', getLogger); + stores = db.stores; +}); + +test.after(async () => { + await stores.db.destroy(); +}); test.serial('returns events', async t => { t.plan(0); - const { request, destroy } = await setupApp('event_api_serial'); + const request = await setupApp(stores); return request .get('/api/admin/events') .expect('Content-Type', /json/) - .expect(200) - .then(destroy); + .expect(200); }); test.serial('returns events given a name', async t => { t.plan(0); - const { request, destroy } = await setupApp('event_api_serial'); + const request = await setupApp(stores); return request .get('/api/admin/events/myname') .expect('Content-Type', /json/) - .expect(200) - .then(destroy); + .expect(200); }); diff --git a/test/e2e/api/admin/feature-archive.e2e.test.js b/test/e2e/api/admin/feature-archive.e2e.test.js index 17a88e8ccc..70bbc3709e 100644 --- a/test/e2e/api/admin/feature-archive.e2e.test.js +++ b/test/e2e/api/admin/feature-archive.e2e.test.js @@ -2,49 +2,56 @@ const test = require('ava'); const { setupApp } = require('./../../helpers/test-helper'); +const dbInit = require('../../helpers/database-init'); +const getLogger = require('../../../fixtures/no-logger'); + +let stores; + +test.before(async () => { + const db = await dbInit('archive_serial', getLogger); + stores = db.stores; +}); + +test.after(async () => { + await stores.db.destroy(); +}); test.serial('returns three archived toggles', async t => { t.plan(1); - const { request, destroy } = await setupApp('archive_serial'); + const request = await setupApp(stores); return request .get('/api/admin/archive/features') .expect('Content-Type', /json/) .expect(200) .expect(res => { t.true(res.body.features.length === 3); - }) - .then(destroy); + }); }); test.serial('revives a feature by name', async t => { t.plan(0); - const { request, destroy } = await setupApp('archive_serial'); + const request = await setupApp(stores); return request .post('/api/admin/archive/revive/featureArchivedX') .set('Content-Type', 'application/json') - .expect(200) - .then(destroy); + .expect(200); }); test.serial( 'archived feature is not accessible via /features/:featureName', async t => { t.plan(0); - const { request, destroy } = await setupApp('archive_serial2'); + const request = await setupApp(stores); return request - .get('/api/admin/features/featureArchivedX') + .get('/api/admin/features/featureArchivedZ') .set('Content-Type', 'application/json') - .expect(404) - .then(destroy); + .expect(404); } ); test.serial('must set name when reviving toggle', async t => { t.plan(0); - const { request, destroy } = await setupApp('archive_serial'); - return request - .post('/api/admin/archive/revive/') - .expect(404) - .then(destroy); + const request = await setupApp(stores); + return request.post('/api/admin/archive/revive/').expect(404); }); diff --git a/test/e2e/api/admin/feature.auth.e2e.test.js b/test/e2e/api/admin/feature.auth.e2e.test.js index 92b85d2b6f..499b9a70cd 100644 --- a/test/e2e/api/admin/feature.auth.e2e.test.js +++ b/test/e2e/api/admin/feature.auth.e2e.test.js @@ -2,10 +2,23 @@ const test = require('ava'); const { setupAppWithAuth } = require('./../../helpers/test-helper'); +const dbInit = require('../../helpers/database-init'); +const getLogger = require('../../../fixtures/no-logger'); + +let stores; + +test.before(async () => { + const db = await dbInit('feature_api_auth', getLogger); + stores = db.stores; +}); + +test.after(async () => { + await stores.db.destroy(); +}); test.serial('creates new feature toggle with createdBy', async t => { t.plan(1); - const { request, destroy } = await setupAppWithAuth('feature_api_auth'); + const request = await setupAppWithAuth(stores); // Login await request.post('/api/admin/login').send({ email: 'user@mail.com', @@ -18,19 +31,13 @@ test.serial('creates new feature toggle with createdBy', async t => { strategies: [{ name: 'default' }], }); - await request - .get('/api/admin/events') - .expect(res => { - t.true(res.body.events[0].createdBy === 'user@mail.com'); - }) - .then(destroy); + await request.get('/api/admin/events').expect(res => { + t.true(res.body.events[0].createdBy === 'user@mail.com'); + }); }); test.serial('should require authenticated user', async t => { t.plan(0); - const { request, destroy } = await setupAppWithAuth('feature_api_auth'); - return request - .get('/api/admin/features') - .expect(401) - .then(destroy); + const request = await setupAppWithAuth(stores); + return request.get('/api/admin/features').expect(401); }); diff --git a/test/e2e/api/admin/feature.custom-auth.e2e.test.js b/test/e2e/api/admin/feature.custom-auth.e2e.test.js index 2f3d3472e2..8c21156584 100644 --- a/test/e2e/api/admin/feature.custom-auth.e2e.test.js +++ b/test/e2e/api/admin/feature.custom-auth.e2e.test.js @@ -5,6 +5,20 @@ const { setupAppWithCustomAuth } = require('./../../helpers/test-helper'); const AuthenticationRequired = require('./../../../../lib/authentication-required'); const User = require('./../../../../lib/user'); +const dbInit = require('../../helpers/database-init'); +const getLogger = require('../../../fixtures/no-logger'); + +let stores; + +test.before(async () => { + const db = await dbInit('feature_api_custom_auth', getLogger); + stores = db.stores; +}); + +test.after(async () => { + await stores.db.destroy(); +}); + test.serial('should require authenticated user', async t => { t.plan(0); const preHook = app => { @@ -21,14 +35,8 @@ test.serial('should require authenticated user', async t => { .end() ); }; - const { request, destroy } = await setupAppWithCustomAuth( - 'feature_api_custom_auth', - preHook - ); - return request - .get('/api/admin/features') - .expect(401) - .then(destroy); + const request = await setupAppWithCustomAuth(stores, preHook); + return request.get('/api/admin/features').expect(401); }); test.serial('creates new feature toggle with createdBy', async t => { @@ -41,10 +49,7 @@ test.serial('creates new feature toggle with createdBy', async t => { next(); }); }; - const { request, destroy } = await setupAppWithCustomAuth( - 'feature_api_custom_auth', - preHook - ); + const request = await setupAppWithCustomAuth(stores, preHook); // create toggle await request.post('/api/admin/features').send({ @@ -53,10 +58,7 @@ test.serial('creates new feature toggle with createdBy', async t => { strategies: [{ name: 'default' }], }); - await request - .get('/api/admin/events') - .expect(res => { - t.true(res.body.events[0].createdBy === user.email); - }) - .then(destroy); + await request.get('/api/admin/events').expect(res => { + t.true(res.body.events[0].createdBy === user.email); + }); }); diff --git a/test/e2e/api/admin/feature.e2e.test.js b/test/e2e/api/admin/feature.e2e.test.js index 1799d54069..d3412981a2 100644 --- a/test/e2e/api/admin/feature.e2e.test.js +++ b/test/e2e/api/admin/feature.e2e.test.js @@ -1,43 +1,53 @@ 'use strict'; const test = require('ava'); -const { setupApp } = require('./../../helpers/test-helper'); +const dbInit = require('../../helpers/database-init'); +const { setupApp } = require('../../helpers/test-helper'); +const getLogger = require('../../../fixtures/no-logger'); + +let stores; + +test.before(async () => { + const db = await dbInit('feature_api_serial', getLogger); + stores = db.stores; +}); + +test.after(async () => { + await stores.db.destroy(); +}); test.serial('returns list of feature toggles', async t => { - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .get('/api/admin/features') .expect('Content-Type', /json/) .expect(200) .expect(res => { t.true(res.body.features.length === 4); - }) - .then(destroy); + }); }); test.serial('gets a feature by name', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .get('/api/admin/features/featureX') .expect('Content-Type', /json/) - .expect(200) - .then(destroy); + .expect(200); }); test.serial('cant get feature that dose not exist', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .get('/api/admin/features/myfeature') .expect('Content-Type', /json/) - .expect(404) - .then(destroy); + .expect(404); }); test.serial('creates new feature toggle', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/features') .send({ @@ -46,13 +56,12 @@ test.serial('creates new feature toggle', async t => { strategies: [{ name: 'default' }], }) .set('Content-Type', 'application/json') - .expect(201) - .then(destroy); + .expect(201); }); test.serial('creates new feature toggle with variants', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/features') .send({ @@ -65,65 +74,58 @@ test.serial('creates new feature toggle with variants', async t => { ], }) .set('Content-Type', 'application/json') - .expect(201) - .then(destroy); + .expect(201); }); test.serial('fetch feature toggle with variants', async t => { t.plan(1); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .get('/api/admin/features/feature.with.variants') .expect(res => { t.true(res.body.variants.length === 2); - }) - .then(destroy); + }); }); test.serial('creates new feature toggle with createdBy unknown', async t => { t.plan(1); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); await request.post('/api/admin/features').send({ name: 'com.test.Username', enabled: false, strategies: [{ name: 'default' }], }); - await request - .get('/api/admin/events') - .expect(res => { - t.true(res.body.events[0].createdBy === 'none@unknown.com'); - }) - .then(destroy); + await request.get('/api/admin/events').expect(res => { + t.true(res.body.events[0].createdBy === 'none@unknown.com'); + }); }); test.serial('require new feature toggle to have a name', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/features') .send({ name: '' }) .set('Content-Type', 'application/json') - .expect(400) - .then(destroy); + .expect(400); }); test.serial( 'can not change status of feature toggle that does not exist', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .put('/api/admin/features/should-not-exist') .send({ name: 'should-not-exist', enabled: false }) .set('Content-Type', 'application/json') - .expect(404) - .then(destroy); + .expect(404); } ); test.serial('can change status of feature toggle that does exist', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .put('/api/admin/features/featureY') .send({ @@ -132,75 +134,64 @@ test.serial('can change status of feature toggle that does exist', async t => { strategies: [{ name: 'default' }], }) .set('Content-Type', 'application/json') - .expect(200) - .then(destroy); + .expect(200); }); test.serial('can not toggle of feature that does not exist', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/features/should-not-exist/toggle') .set('Content-Type', 'application/json') - .expect(404) - .then(destroy); + .expect(404); }); test.serial('can toggle a feature that does exist', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/features/featureY/toggle') .set('Content-Type', 'application/json') - .expect(200) - .then(destroy); + .expect(200); }); test.serial('archives a feature by name', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); - return request - .delete('/api/admin/features/featureX') - .expect(200) - .then(destroy); + const request = await setupApp(stores); + return request.delete('/api/admin/features/featureX').expect(200); }); test.serial('can not archive unknown feature', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); - return request - .delete('/api/admin/features/featureUnknown') - .expect(404) - .then(destroy); + const request = await setupApp(stores); + return request.delete('/api/admin/features/featureUnknown').expect(404); }); test.serial('refuses to create a feature with an existing name', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/features') .send({ name: 'featureX' }) .set('Content-Type', 'application/json') - .expect(400) - .then(destroy); + .expect(400); }); test.serial('refuses to validate a feature with an existing name', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/features/validate') .send({ name: 'featureX' }) .set('Content-Type', 'application/json') - .expect(400) - .then(destroy); + .expect(400); }); test.serial( 'new strategies api can add two strategies to a feature toggle', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .put('/api/admin/features/featureY') .send({ @@ -215,14 +206,13 @@ test.serial( ], }) .set('Content-Type', 'application/json') - .expect(200) - .then(destroy); + .expect(200); } ); test.serial('should not be possible to create archived toggle', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/features') .send({ @@ -231,17 +221,16 @@ test.serial('should not be possible to create archived toggle', async t => { strategies: [{ name: 'default' }], }) .set('Content-Type', 'application/json') - .expect(400) - .then(destroy); + .expect(400); }); test.serial('creates new feature toggle with variant overrides', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/features') .send({ - name: 'com.test.variants', + name: 'com.test.variants.overrieds', enabled: false, strategies: [{ name: 'default' }], variants: [ @@ -259,6 +248,5 @@ test.serial('creates new feature toggle with variant overrides', async t => { ], }) .set('Content-Type', 'application/json') - .expect(201) - .then(destroy); + .expect(201); }); diff --git a/test/e2e/api/admin/metrics.e2e.test.js b/test/e2e/api/admin/metrics.e2e.test.js index f10e4cfff5..9109103c66 100644 --- a/test/e2e/api/admin/metrics.e2e.test.js +++ b/test/e2e/api/admin/metrics.e2e.test.js @@ -1,11 +1,30 @@ 'use strict'; const test = require('ava'); -const { setupApp } = require('./../../helpers/test-helper'); +const dbInit = require('../../helpers/database-init'); +const { setupApp } = require('../../helpers/test-helper'); +const getLogger = require('../../../fixtures/no-logger'); + +let stores; +let reset = () => {}; + +test.before(async () => { + const db = await dbInit('metrics_serial', getLogger); + stores = db.stores; + reset = db.reset; +}); + +test.after(async () => { + await stores.db.destroy(); +}); + +test.afterEach(async () => { + await reset(); +}); test.serial('should register client', async t => { t.plan(0); - const { request, destroy } = await setupApp('metrics_serial'); + const request = await setupApp(stores); return request .post('/api/client/register') .send({ @@ -15,13 +34,12 @@ test.serial('should register client', async t => { started: Date.now(), interval: 10, }) - .expect(202) - .then(destroy); + .expect(202); }); test.serial('should allow client to register multiple times', async t => { t.plan(0); - const { request, destroy } = await setupApp('metrics_serial'); + const request = await setupApp(stores); const clientRegistration = { appName: 'multipleRegistration', instanceId: 'test', @@ -39,13 +57,12 @@ test.serial('should allow client to register multiple times', async t => { .post('/api/client/register') .send(clientRegistration) .expect(202) - ) - .then(destroy); + ); }); test.serial('should accept client metrics', async t => { t.plan(0); - const { request, destroy } = await setupApp('metrics_serial'); + const request = await setupApp(stores); return request .post('/api/client/metrics') .send({ @@ -57,13 +74,12 @@ test.serial('should accept client metrics', async t => { toggles: {}, }, }) - .expect(202) - .then(destroy); + .expect(202); }); test.serial('should get application details', async t => { t.plan(3); - const { request, destroy } = await setupApp('metrics_serial'); + const request = await setupApp(stores); return request .get('/api/admin/metrics/applications/demo-app-1') .expect('Content-Type', /json/) @@ -71,19 +87,17 @@ test.serial('should get application details', async t => { t.true(res.status === 200); t.true(res.body.appName === 'demo-app-1'); t.true(res.body.instances.length === 1); - }) - .then(destroy); + }); }); test.serial('should get list of applications', async t => { t.plan(2); - const { request, destroy } = await setupApp('metrics_serial'); + const request = await setupApp(stores); return request .get('/api/admin/metrics/applications') .expect('Content-Type', /json/) .expect(res => { t.true(res.status === 200); - t.true(res.body.applications.length === 2); - }) - .then(destroy); + t.is(res.body.applications.length, 2); + }); }); diff --git a/test/e2e/api/admin/state.e2e.test.js b/test/e2e/api/admin/state.e2e.test.js index da7eaa1183..1d60310f16 100644 --- a/test/e2e/api/admin/state.e2e.test.js +++ b/test/e2e/api/admin/state.e2e.test.js @@ -1,12 +1,25 @@ 'use strict'; const test = require('ava'); -const { setupApp } = require('./../../helpers/test-helper'); const importData = require('../../../examples/import.json'); +const dbInit = require('../../helpers/database-init'); +const { setupApp } = require('../../helpers/test-helper'); +const getLogger = require('../../../fixtures/no-logger'); + +let stores; + +test.before(async () => { + const db = await dbInit('state_api_serial', getLogger); + stores = db.stores; +}); + +test.after(async () => { + await stores.db.destroy(); +}); test.serial('exports strategies and features as json by default', async t => { t.plan(2); - const { request, destroy } = await setupApp('state_api_serial'); + const request = await setupApp(stores); return request .get('/api/admin/state/export') .expect('Content-Type', /json/) @@ -14,67 +27,60 @@ test.serial('exports strategies and features as json by default', async t => { .expect(res => { t.true('features' in res.body); t.true('strategies' in res.body); - }) - .then(destroy); + }); }); test.serial('exports strategies and features as yaml', async t => { t.plan(0); - const { request, destroy } = await setupApp('state_api_serial'); + const request = await setupApp(stores); return request .get('/api/admin/state/export?format=yaml') .expect('Content-Type', /yaml/) - .expect(200) - .then(destroy); + .expect(200); }); test.serial('exports strategies and features as attachment', async t => { t.plan(0); - const { request, destroy } = await setupApp('state_api_serial'); + const request = await setupApp(stores); return request .get('/api/admin/state/export?download=1') .expect('Content-Type', /json/) .expect('Content-Disposition', /attachment/) - .expect(200) - .then(destroy); + .expect(200); }); test.serial('imports strategies and features', async t => { t.plan(0); - const { request, destroy } = await setupApp('state_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/state/import') .send(importData) - .expect(202) - .then(destroy); + .expect(202); }); test.serial('does not not accept gibberish', async t => { t.plan(0); - const { request, destroy } = await setupApp('state_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/state/import') .send({ features: 'nonsense' }) - .expect(400) - .then(destroy); + .expect(400); }); test.serial('imports strategies and features from json file', async t => { t.plan(0); - const { request, destroy } = await setupApp('state_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/state/import') .attach('file', 'test/examples/import.json') - .expect(202) - .then(destroy); + .expect(202); }); test.serial('imports strategies and features from yaml file', async t => { t.plan(0); - const { request, destroy } = await setupApp('state_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/state/import') .attach('file', 'test/examples/import.yml') - .expect(202) - .then(destroy); + .expect(202); }); diff --git a/test/e2e/api/admin/strategy.e2e.test.js b/test/e2e/api/admin/strategy.e2e.test.js index 8628ae1d2c..f166520367 100644 --- a/test/e2e/api/admin/strategy.e2e.test.js +++ b/test/e2e/api/admin/strategy.e2e.test.js @@ -1,11 +1,25 @@ 'use strict'; const test = require('ava'); -const { setupApp } = require('./../../helpers/test-helper'); + +const dbInit = require('../../helpers/database-init'); +const { setupApp } = require('../../helpers/test-helper'); +const getLogger = require('../../../fixtures/no-logger'); + +let stores; + +test.before(async () => { + const db = await dbInit('strategy_api_serial', getLogger); + stores = db.stores; +}); + +test.after(async () => { + await stores.db.destroy(); +}); test.serial('gets all strategies', async t => { t.plan(1); - const { request, destroy } = await setupApp('strategy_api_serial'); + const request = await setupApp(stores); return request .get('/api/admin/strategies') .expect('Content-Type', /json/) @@ -15,33 +29,30 @@ test.serial('gets all strategies', async t => { res.body.strategies.length === 2, 'expected to have two strategies' ); - }) - .then(destroy); + }); }); test.serial('gets a strategy by name', async t => { t.plan(0); - const { request, destroy } = await setupApp('strategy_api_serial'); + const request = await setupApp(stores); return request .get('/api/admin/strategies/default') .expect('Content-Type', /json/) - .expect(200) - .then(destroy); + .expect(200); }); test.serial('cant get a strategy by name that dose not exist', async t => { t.plan(0); - const { request, destroy } = await setupApp('strategy_api_serial'); + const request = await setupApp(stores); return request .get('/api/admin/strategies/mystrategy') .expect('Content-Type', /json/) - .expect(404) - .then(destroy); + .expect(404); }); test.serial('creates a new strategy', async t => { t.plan(0); - const { request, destroy } = await setupApp('strategy_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/strategies') .send({ @@ -50,53 +61,44 @@ test.serial('creates a new strategy', async t => { parameters: [], }) .set('Content-Type', 'application/json') - .expect(201) - .then(destroy); + .expect(201); }); test.serial('requires new strategies to have a name', async t => { t.plan(0); - const { request, destroy } = await setupApp('strategy_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/strategies') .send({ name: '' }) .set('Content-Type', 'application/json') - .expect(400) - .then(destroy); + .expect(400); }); test.serial('refuses to create a strategy with an existing name', async t => { t.plan(0); - const { request, destroy } = await setupApp('strategy_api_serial'); + const request = await setupApp(stores); return request .post('/api/admin/strategies') .send({ name: 'default', parameters: [] }) .set('Content-Type', 'application/json') - .expect(400) - .then(destroy); + .expect(400); }); test.serial('deletes a new strategy', async t => { t.plan(0); - const { request, destroy } = await setupApp('strategy_api_serial'); - return request - .delete('/api/admin/strategies/usersWithEmail') - .expect(200) - .then(destroy); + const request = await setupApp(stores); + return request.delete('/api/admin/strategies/usersWithEmail').expect(200); }); test.serial("can't delete a strategy that dose not exist", async t => { t.plan(0); - const { request, destroy } = await setupApp('strategy_api_serial', false); - return request - .delete('/api/admin/strategies/unknown') - .expect(404) - .then(destroy); + const request = await setupApp(stores); + return request.delete('/api/admin/strategies/unknown').expect(404); }); test.serial('updates a exiting strategy', async t => { t.plan(0); - const { request, destroy } = await setupApp('strategy_api_serial'); + const request = await setupApp(stores); return request .put('/api/admin/strategies/default') .send({ @@ -105,17 +107,15 @@ test.serial('updates a exiting strategy', async t => { parameters: [], }) .set('Content-Type', 'application/json') - .expect(200) - .then(destroy); + .expect(200); }); test.serial('cant update a unknown strategy', async t => { t.plan(0); - const { request, destroy } = await setupApp('strategy_api_serial'); + const request = await setupApp(stores); return request .put('/api/admin/strategies/unknown') .send({ name: 'unkown', parameters: [] }) .set('Content-Type', 'application/json') - .expect(404) - .then(destroy); + .expect(404); }); diff --git a/test/e2e/api/client/feature.e2e.test.js b/test/e2e/api/client/feature.e2e.test.js index e27ca5e640..639fbbb375 100644 --- a/test/e2e/api/client/feature.e2e.test.js +++ b/test/e2e/api/client/feature.e2e.test.js @@ -1,36 +1,46 @@ 'use strict'; const test = require('ava'); -const { setupApp } = require('./../../helpers/test-helper'); +const { setupApp } = require('../../helpers/test-helper'); +const dbInit = require('../../helpers/database-init'); +const getLogger = require('../../../fixtures/no-logger'); + +let stores; + +test.before(async () => { + const db = await dbInit('feature_api_client', getLogger); + stores = db.stores; +}); + +test.after(async () => { + await stores.db.destroy(); +}); test.serial('returns four feature toggles', async t => { - const { request, destroy } = await setupApp('feature_api_client'); + const request = await setupApp(stores); return request .get('/api/client/features') .expect('Content-Type', /json/) .expect(200) .expect(res => { t.true(res.body.features.length === 4); - }) - .then(destroy); + }); }); test.serial('gets a feature by name', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_client'); + const request = await setupApp(stores); return request .get('/api/client/features/featureX') .expect('Content-Type', /json/) - .expect(200) - .then(destroy); + .expect(200); }); test.serial('cant get feature that dose not exist', async t => { t.plan(0); - const { request, destroy } = await setupApp('feature_api_client'); + const request = await setupApp(stores); return request .get('/api/client/features/myfeature') .expect('Content-Type', /json/) - .expect(404) - .then(destroy); + .expect(404); }); diff --git a/test/e2e/api/client/metrics.e2e.test.js b/test/e2e/api/client/metrics.e2e.test.js index 46124de178..7496738303 100644 --- a/test/e2e/api/client/metrics.e2e.test.js +++ b/test/e2e/api/client/metrics.e2e.test.js @@ -4,25 +4,36 @@ const test = require('ava'); const { setupApp } = require('./../../helpers/test-helper'); const metricsExample = require('../../../examples/client-metrics.json'); +const dbInit = require('../../helpers/database-init'); +const getLogger = require('../../../fixtures/no-logger'); + +let stores; + +test.before(async () => { + const db = await dbInit('metrics_api_client', getLogger); + stores = db.stores; +}); + +test.after(async () => { + await stores.db.destroy(); +}); test.serial('should be possble to send metrics', async t => { t.plan(0); - const { request, destroy } = await setupApp('metrics_api_client'); + const request = await setupApp(stores); return request .post('/api/client/metrics') .send(metricsExample) - .expect(202) - .then(destroy); + .expect(202); }); test.serial('should require valid send metrics', async t => { t.plan(0); - const { request, destroy } = await setupApp('metrics_api_client'); + const request = await setupApp(stores); return request .post('/api/client/metrics') .send({ appName: 'test', }) - .expect(400) - .then(destroy); + .expect(400); }); diff --git a/test/e2e/health.e2e.test.js b/test/e2e/health.e2e.test.js index d913c9a81c..c9fea7d99e 100644 --- a/test/e2e/health.e2e.test.js +++ b/test/e2e/health.e2e.test.js @@ -2,14 +2,26 @@ const test = require('ava'); const { setupApp } = require('./helpers/test-helper'); +const dbInit = require('./helpers/database-init'); +const getLogger = require('../fixtures/no-logger'); + +let stores; + +test.before(async () => { + const db = await dbInit('health_api', getLogger); + stores = db.stores; +}); + +test.after(async () => { + await stores.db.destroy(); +}); test('returns health good', async t => { t.plan(0); - const { request, destroy } = await setupApp('health'); + const request = await setupApp(stores); return request .get('/health') .expect('Content-Type', /json/) .expect(200) - .expect('{"health":"GOOD"}') - .then(destroy); + .expect('{"health":"GOOD"}'); }); diff --git a/test/e2e/helpers/database-init.js b/test/e2e/helpers/database-init.js index cc356eb99f..668134d5a5 100644 --- a/test/e2e/helpers/database-init.js +++ b/test/e2e/helpers/database-init.js @@ -69,5 +69,11 @@ module.exports = async function init(databaseSchema = 'test', getLogger) { await resetDatabase(stores); await setupDatabase(stores); - return stores; + return { + stores, + reset: async () => { + await resetDatabase(stores); + await setupDatabase(stores); + }, + }; }; diff --git a/test/e2e/helpers/test-helper.js b/test/e2e/helpers/test-helper.js index 76ac6f109a..ab43d484d7 100644 --- a/test/e2e/helpers/test-helper.js +++ b/test/e2e/helpers/test-helper.js @@ -5,7 +5,6 @@ process.env.NODE_ENV = 'test'; const supertest = require('supertest'); const getApp = require('../../../lib/app'); -const dbInit = require('./database-init'); const getLogger = require('../../fixtures/no-logger'); const StateService = require('../../../lib/state-service'); @@ -26,50 +25,18 @@ function createApp(stores, adminAuthentication = 'none', preHook) { } module.exports = { - async setupApp(name) { - const stores = await dbInit(name, getLogger); + async setupApp(stores) { const app = createApp(stores); - - return { - request: supertest.agent(app), - destroy: async () => { - try { - await stores.db.destroy(); - } catch (error) { - console.error('Failed to destroy db', error); - } - }, - }; + return supertest.agent(app); }, - async setupAppWithAuth(name) { - const stores = await dbInit(name, getLogger); + + async setupAppWithAuth(stores) { const app = createApp(stores, 'unsecure'); - - return { - request: supertest.agent(app), - destroy: async () => { - try { - await stores.db.destroy(); - } catch (error) { - console.error('Failed to destroy db', error); - } - }, - }; + return supertest.agent(app); }, - async setupAppWithCustomAuth(name, preHook) { - const stores = await dbInit(name, getLogger); + async setupAppWithCustomAuth(stores, preHook) { const app = createApp(stores, 'custom', preHook); - - return { - request: supertest.agent(app), - destroy: async () => { - try { - await stores.db.destroy(); - } catch (error) { - console.error('Failed to destroy db', error); - } - }, - }; + return supertest.agent(app); }, };