2021-08-12 15:04:37 +02:00
|
|
|
import supertest from 'supertest';
|
|
|
|
import { createTestConfig } from '../../../test/config/test-config';
|
|
|
|
import createStores from '../../../test/fixtures/store';
|
|
|
|
import permissions from '../../../test/fixtures/permissions';
|
|
|
|
import getApp from '../../app';
|
|
|
|
import { createServices } from '../../services';
|
2016-09-05 16:50:52 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
let destroy;
|
2016-11-30 23:41:57 +01:00
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
async function getSetup() {
|
2021-05-28 11:10:24 +02:00
|
|
|
const randomBase = `/random${Math.round(Math.random() * 1000)}`;
|
2018-12-19 10:36:56 +01:00
|
|
|
const perms = permissions();
|
2021-08-12 15:04:37 +02:00
|
|
|
const stores = createStores();
|
2021-04-22 10:07:10 +02:00
|
|
|
const config = createTestConfig({
|
2021-05-28 11:10:24 +02:00
|
|
|
server: { baseUriPath: randomBase },
|
2018-12-19 10:36:56 +01:00
|
|
|
preRouterHook: perms.hook,
|
2021-04-22 10:07:10 +02:00
|
|
|
});
|
2021-01-18 12:32:19 +01:00
|
|
|
const services = createServices(stores, config);
|
2022-01-06 10:31:00 +01:00
|
|
|
const app = await getApp(config, stores, services);
|
2016-09-05 16:50:52 +02:00
|
|
|
|
2021-10-29 11:22:31 +02:00
|
|
|
destroy = () => {
|
|
|
|
services.versionService.destroy();
|
2021-12-09 21:25:06 +01:00
|
|
|
services.clientInstanceService.destroy();
|
2021-10-29 11:22:31 +02:00
|
|
|
services.apiTokenService.destroy();
|
|
|
|
};
|
|
|
|
|
2016-12-13 13:59:52 +01:00
|
|
|
return {
|
2021-05-28 11:10:24 +02:00
|
|
|
base: randomBase,
|
2016-12-13 13:59:52 +01:00
|
|
|
strategyStore: stores.strategyStore,
|
|
|
|
request: supertest(app),
|
2018-12-19 10:36:56 +01:00
|
|
|
perms,
|
2016-12-13 13:59:52 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
afterEach(() => {
|
|
|
|
destroy();
|
2021-01-06 13:25:25 +01:00
|
|
|
});
|
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('add version numbers for /strategies', async () => {
|
|
|
|
const { request, base } = await 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)
|
2021-08-12 15:04:37 +02:00
|
|
|
.expect((res) => {
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(res.body.version).toBe(1);
|
2016-11-13 15:41:35 +01:00
|
|
|
});
|
2016-09-05 16:50:52 +02:00
|
|
|
});
|
2016-12-13 13:59:52 +01:00
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('require a name when creating a new strategy', async () => {
|
|
|
|
const { request, base } = await getSetup();
|
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)
|
2021-08-12 15:04:37 +02:00
|
|
|
.expect((res) => {
|
2022-06-23 08:10:20 +02:00
|
|
|
expect(res.body.validation[0].message).toEqual(
|
|
|
|
"should have required property 'name'",
|
2021-05-28 11:10:24 +02:00
|
|
|
);
|
2016-12-13 13:59:52 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('require parameters array when creating a new strategy', async () => {
|
|
|
|
const { request, base } = await getSetup();
|
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)
|
2021-08-12 15:04:37 +02:00
|
|
|
.expect((res) => {
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(res.body.details[0].message).toEqual(
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('create a new strategy with empty parameters', async () => {
|
2022-01-06 10:31:00 +01:00
|
|
|
const { request, base } = await getSetup();
|
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);
|
|
|
|
});
|
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('not be possible to override name', async () => {
|
|
|
|
const { request, base, strategyStore } = await getSetup();
|
2021-01-18 12:32:19 +01:00
|
|
|
strategyStore.createStrategy({ name: 'Testing', parameters: [] });
|
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: 'Testing', parameters: [] })
|
2020-09-25 22:31:35 +02:00
|
|
|
.expect(409);
|
2016-12-13 13:59:52 +01:00
|
|
|
});
|
2016-12-17 12:31:23 +01:00
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('update strategy', async () => {
|
|
|
|
const { request, base, strategyStore } = await getSetup();
|
2016-12-17 12:31:23 +01:00
|
|
|
const name = 'AnotherStrat';
|
2021-01-18 12:32:19 +01:00
|
|
|
strategyStore.createStrategy({ name, parameters: [] });
|
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(200);
|
|
|
|
});
|
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('not update unknown strategy', async () => {
|
|
|
|
const { request, base } = await getSetup();
|
2016-12-17 12:31:23 +01:00
|
|
|
const name = 'UnknownStrat';
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('validate format when updating strategy', async () => {
|
|
|
|
const { request, base, strategyStore } = await getSetup();
|
2016-12-17 12:31:23 +01:00
|
|
|
const name = 'AnotherStrat';
|
2021-01-18 12:32:19 +01:00
|
|
|
strategyStore.createStrategy({ name, parameters: [] });
|
2016-12-17 12:31:23 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('editable=false will stop delete request', async () => {
|
2021-10-29 11:22:31 +02:00
|
|
|
jest.spyOn(global.console, 'error').mockImplementation(() => jest.fn());
|
2022-01-06 10:31:00 +01:00
|
|
|
const { request, base } = await getSetup();
|
2017-06-29 08:42:03 +02:00
|
|
|
const name = 'default';
|
|
|
|
return request.delete(`${base}/api/admin/strategies/${name}`).expect(500);
|
|
|
|
});
|
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('editable=false will stop edit request', async () => {
|
2021-10-29 11:22:31 +02:00
|
|
|
jest.spyOn(global.console, 'error').mockImplementation(() => jest.fn());
|
2022-01-06 10:31:00 +01:00
|
|
|
const { request, base } = await getSetup();
|
2017-06-29 08:42:03 +02:00
|
|
|
const name = 'default';
|
|
|
|
return request
|
|
|
|
.put(`${base}/api/admin/strategies/${name}`)
|
|
|
|
.send({ name, parameters: [] })
|
|
|
|
.expect(500);
|
|
|
|
});
|
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('editable=true will allow delete request', async () => {
|
|
|
|
const { request, base, strategyStore } = await getSetup();
|
2017-06-29 08:42:03 +02:00
|
|
|
const name = 'deleteStrat';
|
2021-01-18 12:32:19 +01:00
|
|
|
strategyStore.createStrategy({ name, parameters: [] });
|
2017-06-29 08:42:03 +02:00
|
|
|
|
|
|
|
return request
|
|
|
|
.delete(`${base}/api/admin/strategies/${name}`)
|
|
|
|
.send({})
|
|
|
|
.expect(200);
|
|
|
|
});
|
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('editable=true will allow edit request', async () => {
|
|
|
|
const { request, base, strategyStore } = await getSetup();
|
2017-06-29 08:42:03 +02:00
|
|
|
const name = 'editStrat';
|
2021-01-18 12:32:19 +01:00
|
|
|
strategyStore.createStrategy({ name, parameters: [] });
|
2017-06-29 08:42:03 +02:00
|
|
|
|
|
|
|
return request
|
|
|
|
.put(`${base}/api/admin/strategies/${name}`)
|
|
|
|
.send({ name, parameters: [] })
|
|
|
|
.expect(200);
|
|
|
|
});
|
2021-01-21 13:47:08 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('deprecating a strategy works', async () => {
|
2022-01-06 10:31:00 +01:00
|
|
|
const { request, base, strategyStore } = await getSetup();
|
2021-01-21 13:47:08 +01:00
|
|
|
const name = 'editStrat';
|
|
|
|
strategyStore.createStrategy({ name, parameters: [] });
|
|
|
|
|
|
|
|
await request
|
|
|
|
.post(`${base}/api/admin/strategies/${name}/deprecate`)
|
2021-02-10 14:15:28 +01:00
|
|
|
.set('Content-Type', 'application/json')
|
2021-01-21 13:47:08 +01:00
|
|
|
.send()
|
|
|
|
.expect(200);
|
|
|
|
return request
|
|
|
|
.get(`${base}/api/admin/strategies/${name}`)
|
|
|
|
.expect(200)
|
2021-08-12 15:04:37 +02:00
|
|
|
.expect((res) => expect(res.body.deprecated).toBe(true));
|
2021-01-21 13:47:08 +01:00
|
|
|
});
|
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('deprecating a non-existent strategy yields 404', async () => {
|
|
|
|
const { request, base } = await getSetup();
|
2021-01-21 13:47:08 +01:00
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/strategies/non-existent-strategy/deprecate`)
|
2021-02-10 14:15:28 +01:00
|
|
|
.set('Content-Type', 'application/json')
|
2021-01-21 13:47:08 +01:00
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('reactivating a strategy works', async () => {
|
2022-01-06 10:31:00 +01:00
|
|
|
const { request, base, strategyStore } = await getSetup();
|
2021-01-21 13:47:08 +01:00
|
|
|
const name = 'editStrat';
|
|
|
|
strategyStore.createStrategy({ name, parameters: [] });
|
|
|
|
|
|
|
|
await request
|
|
|
|
.post(`${base}/api/admin/strategies/${name}/reactivate`)
|
2021-02-10 14:15:28 +01:00
|
|
|
.set('Content-Type', 'application/json')
|
2021-01-21 13:47:08 +01:00
|
|
|
.send()
|
|
|
|
.expect(200);
|
|
|
|
return request
|
|
|
|
.get(`${base}/api/admin/strategies/${name}`)
|
|
|
|
.expect(200)
|
2021-08-12 15:04:37 +02:00
|
|
|
.expect((res) => expect(res.body.deprecated).toBe(false));
|
2021-01-21 13:47:08 +01:00
|
|
|
});
|
|
|
|
|
2022-01-06 10:31:00 +01:00
|
|
|
test('reactivating a non-existent strategy yields 404', async () => {
|
|
|
|
const { request, base } = await getSetup();
|
2021-01-21 13:47:08 +01:00
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/strategies/non-existent-strategy/reactivate`)
|
2021-02-10 14:15:28 +01:00
|
|
|
.set('Content-Type', 'application/json')
|
2021-01-21 13:47:08 +01:00
|
|
|
.expect(404);
|
|
|
|
});
|
2022-01-06 10:31:00 +01:00
|
|
|
test("deprecating 'default' strategy will yield 403", async () => {
|
2021-10-29 11:22:31 +02:00
|
|
|
jest.spyOn(global.console, 'error').mockImplementation(() => jest.fn());
|
2022-01-06 10:31:00 +01:00
|
|
|
const { request, base } = await getSetup();
|
2021-01-22 10:03:01 +01:00
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/strategies/default/deprecate`)
|
2021-02-10 14:15:28 +01:00
|
|
|
.set('Content-Type', 'application/json')
|
2021-01-22 10:03:01 +01:00
|
|
|
.expect(403);
|
|
|
|
});
|