1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-14 00:19:16 +01:00

fix: simplify e2e tests by leverarging unelash.create method

This commit is contained in:
Ivar Conradi Østhus 2021-04-11 22:40:29 +02:00
parent ba09ca317c
commit 9354c5a15e
No known key found for this signature in database
GPG Key ID: 31AC596886B0BD09
2 changed files with 67 additions and 18 deletions

View File

@ -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);
});

View File

@ -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,
};
},
};