2016-09-05 16:50:52 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-17 09:24:49 +01:00
|
|
|
const test = require('ava');
|
2020-04-14 22:29:11 +02:00
|
|
|
const supertest = require('supertest');
|
|
|
|
const { EventEmitter } = require('events');
|
|
|
|
const store = require('../../../test/fixtures/store');
|
2018-12-19 10:36:56 +01:00
|
|
|
const permissions = require('../../../test/fixtures/permissions');
|
2019-04-30 21:14:23 +02:00
|
|
|
const getLogger = require('../../../test/fixtures/no-logger');
|
2017-06-28 10:20:22 +02:00
|
|
|
const getApp = require('../../app');
|
2018-12-19 10:36:56 +01:00
|
|
|
const {
|
|
|
|
DELETE_STRATEGY,
|
|
|
|
CREATE_STRATEGY,
|
|
|
|
UPDATE_STRATEGY,
|
|
|
|
} = require('../../permissions');
|
2016-09-05 16:50:52 +02:00
|
|
|
|
2016-11-30 23:41:57 +01:00
|
|
|
const eventBus = new EventEmitter();
|
|
|
|
|
2017-06-28 10:20:22 +02:00
|
|
|
function getSetup() {
|
2016-12-13 13:59:52 +01:00
|
|
|
const base = `/random${Math.round(Math.random() * 1000)}`;
|
2018-12-19 10:36:56 +01:00
|
|
|
const perms = permissions();
|
2016-11-13 15:41:35 +01:00
|
|
|
const stores = store.createStores();
|
2016-11-13 21:07:14 +01:00
|
|
|
const app = getApp({
|
2016-12-13 13:59:52 +01:00
|
|
|
baseUriPath: base,
|
2016-11-13 15:41:35 +01:00
|
|
|
stores,
|
2016-11-30 23:41:57 +01:00
|
|
|
eventBus,
|
2019-04-30 21:14:23 +02:00
|
|
|
getLogger,
|
2018-12-19 10:36:56 +01:00
|
|
|
extendedPermissions: true,
|
|
|
|
preRouterHook: perms.hook,
|
2016-09-05 16:50:52 +02:00
|
|
|
});
|
|
|
|
|
2016-12-13 13:59:52 +01:00
|
|
|
return {
|
|
|
|
base,
|
|
|
|
strategyStore: stores.strategyStore,
|
|
|
|
request: supertest(app),
|
2018-12-19 10:36:56 +01:00
|
|
|
perms,
|
2016-12-13 13:59:52 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-29 08:35:05 +02:00
|
|
|
test('add version numbers for /stategies', t => {
|
2017-06-28 10:20:22 +02:00
|
|
|
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
|
2017-06-28 10:20:22 +02:00
|
|
|
.get(`${base}/api/admin/strategies`)
|
2016-11-13 15:41:35 +01:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2017-06-28 10:20:22 +02:00
|
|
|
.expect(res => {
|
2016-11-13 15:41:35 +01:00
|
|
|
t.true(res.body.version === 1);
|
|
|
|
});
|
2016-09-05 16:50:52 +02:00
|
|
|
});
|
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 => {
|
2017-06-28 10:20:22 +02:00
|
|
|
t.plan(1);
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(CREATE_STRATEGY);
|
2016-12-13 13:59:52 +01:00
|
|
|
|
|
|
|
return request
|
2017-06-28 10:20:22 +02:00
|
|
|
.post(`${base}/api/admin/strategies`)
|
2016-12-13 13:59:52 +01:00
|
|
|
.send({})
|
|
|
|
.expect(400)
|
2017-06-28 10:20:22 +02:00
|
|
|
.expect(res => {
|
2020-01-02 19:23:52 +01:00
|
|
|
t.true(res.body.details[0].message === '"name" is required');
|
2016-12-13 13:59:52 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-29 08:35:05 +02:00
|
|
|
test('require parameters array when creating a new stratey', t => {
|
2017-06-28 10:20:22 +02:00
|
|
|
t.plan(1);
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(CREATE_STRATEGY);
|
2016-12-13 13:59:52 +01:00
|
|
|
|
|
|
|
return request
|
2017-06-28 10:20:22 +02:00
|
|
|
.post(`${base}/api/admin/strategies`)
|
2016-12-13 13:59:52 +01:00
|
|
|
.send({ name: 'TestStrat' })
|
|
|
|
.expect(400)
|
2017-06-28 10:20:22 +02:00
|
|
|
.expect(res => {
|
2020-01-02 19:23:52 +01:00
|
|
|
t.deepEqual(
|
|
|
|
res.body.details[0].message,
|
2020-04-14 22:29:11 +02:00
|
|
|
'"parameters" is required',
|
2020-01-02 19:23:52 +01:00
|
|
|
);
|
2016-12-13 13:59:52 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-29 08:35:05 +02:00
|
|
|
test('create a new stratey with empty parameters', t => {
|
2017-06-28 10:20:22 +02:00
|
|
|
t.plan(0);
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(CREATE_STRATEGY);
|
2016-12-13 13:59:52 +01:00
|
|
|
|
|
|
|
return request
|
2017-06-28 10:20:22 +02:00
|
|
|
.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 => {
|
2017-06-28 10:20:22 +02:00
|
|
|
t.plan(0);
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, strategyStore, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(CREATE_STRATEGY);
|
2016-12-13 13:59:52 +01:00
|
|
|
strategyStore.addStrategy({ name: 'Testing', parameters: [] });
|
|
|
|
|
|
|
|
return request
|
2017-06-28 10:20:22 +02:00
|
|
|
.post(`${base}/api/admin/strategies`)
|
2016-12-13 13:59:52 +01:00
|
|
|
.send({ name: 'Testing', parameters: [] })
|
2019-01-02 12:38:58 +01:00
|
|
|
.expect(400);
|
2016-12-13 13:59:52 +01:00
|
|
|
});
|
2016-12-17 12:31:23 +01:00
|
|
|
|
2017-06-29 08:35:05 +02:00
|
|
|
test('update strategy', t => {
|
2017-06-28 10:20:22 +02:00
|
|
|
t.plan(0);
|
2016-12-17 12:31:23 +01:00
|
|
|
const name = 'AnotherStrat';
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, strategyStore, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(UPDATE_STRATEGY);
|
2016-12-17 12:31:23 +01:00
|
|
|
strategyStore.addStrategy({ name, parameters: [] });
|
|
|
|
|
|
|
|
return request
|
2017-06-28 10:20:22 +02:00
|
|
|
.put(`${base}/api/admin/strategies/${name}`)
|
2016-12-17 12:31:23 +01:00
|
|
|
.send({ name, parameters: [], description: 'added' })
|
|
|
|
.expect(200);
|
|
|
|
});
|
|
|
|
|
2018-12-19 10:36:56 +01:00
|
|
|
test('not update unknown strategy', t => {
|
2017-06-28 10:20:22 +02:00
|
|
|
t.plan(0);
|
2016-12-17 12:31:23 +01:00
|
|
|
const name = 'UnknownStrat';
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(UPDATE_STRATEGY);
|
2016-12-17 12:31:23 +01:00
|
|
|
|
|
|
|
return request
|
2017-06-28 10:20:22 +02:00
|
|
|
.put(`${base}/api/admin/strategies/${name}`)
|
2016-12-17 12:31:23 +01:00
|
|
|
.send({ name, parameters: [], description: 'added' })
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
|
2017-06-29 08:35:05 +02:00
|
|
|
test('validate format when updating strategy', t => {
|
2017-06-28 10:20:22 +02:00
|
|
|
t.plan(0);
|
2016-12-17 12:31:23 +01:00
|
|
|
const name = 'AnotherStrat';
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, strategyStore, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(UPDATE_STRATEGY);
|
2016-12-17 12:31:23 +01:00
|
|
|
strategyStore.addStrategy({ name, parameters: [] });
|
|
|
|
|
|
|
|
return request
|
2017-06-28 10:20:22 +02:00
|
|
|
.put(`${base}/api/admin/strategies/${name}`)
|
|
|
|
.send({})
|
2016-12-17 12:31:23 +01:00
|
|
|
.expect(400);
|
|
|
|
});
|
2017-06-29 08:42:03 +02:00
|
|
|
|
|
|
|
test('editable=false will stop delete request', t => {
|
|
|
|
t.plan(0);
|
|
|
|
const name = 'default';
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(DELETE_STRATEGY);
|
2017-06-29 08:42:03 +02:00
|
|
|
|
|
|
|
return request.delete(`${base}/api/admin/strategies/${name}`).expect(500);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('editable=false will stop edit request', t => {
|
|
|
|
t.plan(0);
|
|
|
|
const name = 'default';
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(UPDATE_STRATEGY);
|
2017-06-29 08:42:03 +02:00
|
|
|
|
|
|
|
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';
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, strategyStore, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(DELETE_STRATEGY);
|
2017-06-29 08:42:03 +02:00
|
|
|
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';
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, strategyStore, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(UPDATE_STRATEGY);
|
2017-06-29 08:42:03 +02:00
|
|
|
strategyStore.addStrategy({ name, parameters: [] });
|
|
|
|
|
|
|
|
return request
|
|
|
|
.put(`${base}/api/admin/strategies/${name}`)
|
|
|
|
.send({ name, parameters: [] })
|
|
|
|
.expect(200);
|
|
|
|
});
|