1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/routes/admin-api/strategy.test.ts

219 lines
6.9 KiB
TypeScript
Raw Normal View History

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