1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/lib/routes/admin-api/strategy.test.js

164 lines
4.3 KiB
JavaScript
Raw Normal View History

'use strict';
const { test } = require('ava');
const store = require('./../../../test/fixtures/store');
const supertest = require('supertest');
const getApp = require('../../app');
const { EventEmitter } = require('events');
const eventBus = new EventEmitter();
function getSetup() {
2016-12-13 13:59:52 +01:00
const base = `/random${Math.round(Math.random() * 1000)}`;
2016-11-13 15:41:35 +01:00
const stores = store.createStores();
const app = getApp({
2016-12-13 13:59:52 +01:00
baseUriPath: base,
2016-11-13 15:41:35 +01:00
stores,
eventBus,
});
2016-12-13 13:59:52 +01:00
return {
base,
strategyStore: stores.strategyStore,
request: supertest(app),
};
}
2017-06-29 08:35:05 +02:00
test('add version numbers for /stategies', t => {
t.plan(1);
2016-12-13 13:59:52 +01:00
const { request, base } = getSetup();
2016-11-13 15:41:35 +01:00
return request
.get(`${base}/api/admin/strategies`)
2016-11-13 15:41:35 +01:00
.expect('Content-Type', /json/)
.expect(200)
.expect(res => {
2016-11-13 15:41:35 +01:00
t.true(res.body.version === 1);
});
});
2016-12-13 13:59:52 +01:00
2017-06-29 08:35:05 +02:00
test('require a name when creating a new stratey', t => {
t.plan(1);
2016-12-13 13:59:52 +01:00
const { request, base } = getSetup();
return request
.post(`${base}/api/admin/strategies`)
2016-12-13 13:59:52 +01:00
.send({})
.expect(400)
.expect(res => {
2016-12-13 13:59:52 +01:00
t.true(res.body.name === 'ValidationError');
});
});
2017-06-29 08:35:05 +02:00
test('require parameters array when creating a new stratey', t => {
t.plan(1);
2016-12-13 13:59:52 +01:00
const { request, base } = getSetup();
return request
.post(`${base}/api/admin/strategies`)
2016-12-13 13:59:52 +01:00
.send({ name: 'TestStrat' })
.expect(400)
.expect(res => {
2016-12-13 13:59:52 +01:00
t.true(res.body.name === 'ValidationError');
});
});
2017-06-29 08:35:05 +02:00
test('create a new stratey with empty parameters', t => {
t.plan(0);
2016-12-13 13:59:52 +01:00
const { request, base } = getSetup();
return request
.post(`${base}/api/admin/strategies`)
2016-12-13 13:59:52 +01:00
.send({ name: 'TestStrat', parameters: [] })
.expect(201);
});
2017-06-29 08:35:05 +02:00
test('not be possible to override name', t => {
t.plan(0);
2016-12-13 13:59:52 +01:00
const { request, base, strategyStore } = getSetup();
strategyStore.addStrategy({ name: 'Testing', parameters: [] });
return request
.post(`${base}/api/admin/strategies`)
2016-12-13 13:59:52 +01:00
.send({ name: 'Testing', parameters: [] })
.expect(403);
});
2017-06-29 08:35:05 +02:00
test('update strategy', t => {
t.plan(0);
const name = 'AnotherStrat';
const { request, base, strategyStore } = getSetup();
strategyStore.addStrategy({ name, parameters: [] });
return request
.put(`${base}/api/admin/strategies/${name}`)
.send({ name, parameters: [], description: 'added' })
.expect(200);
});
2017-06-29 08:35:05 +02:00
test('not update uknown strategy', t => {
t.plan(0);
const name = 'UnknownStrat';
const { request, base } = getSetup();
return request
.put(`${base}/api/admin/strategies/${name}`)
.send({ name, parameters: [], description: 'added' })
.expect(404);
});
2017-06-29 08:35:05 +02:00
test('validate format when updating strategy', t => {
t.plan(0);
const name = 'AnotherStrat';
const { request, base, strategyStore } = getSetup();
strategyStore.addStrategy({ name, parameters: [] });
return request
.put(`${base}/api/admin/strategies/${name}`)
.send({})
.expect(400);
});
2017-06-29 08:42:03 +02:00
test('editable=false will stop delete request', t => {
t.plan(0);
const name = 'default';
const { request, base } = getSetup();
return request.delete(`${base}/api/admin/strategies/${name}`).expect(500);
});
test('editable=false will stop edit request', t => {
t.plan(0);
const name = 'default';
const { request, base } = getSetup();
return request
.put(`${base}/api/admin/strategies/${name}`)
.send({ name, parameters: [] })
.expect(500);
});
test('editable=true will allow delete request', t => {
t.plan(0);
const name = 'deleteStrat';
const { request, base, strategyStore } = getSetup();
strategyStore.addStrategy({ name, parameters: [] });
return request
.delete(`${base}/api/admin/strategies/${name}`)
.send({})
.expect(200);
});
test('editable=true will allow edit request', t => {
t.plan(0);
const name = 'editStrat';
const { request, base, strategyStore } = getSetup();
strategyStore.addStrategy({ name, parameters: [] });
return request
.put(`${base}/api/admin/strategies/${name}`)
.send({ name, parameters: [] })
.expect(200);
});