2021-08-12 15:04:37 +02:00
|
|
|
import dbInit from '../../helpers/database-init';
|
|
|
|
import { setupApp } from '../../helpers/test-helper';
|
|
|
|
import getLogger from '../../../fixtures/no-logger';
|
2019-10-03 15:01:33 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
let app;
|
2021-03-19 22:25:21 +01:00
|
|
|
let db;
|
2019-10-03 15:01:33 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
beforeAll(async () => {
|
2021-03-19 22:25:21 +01:00
|
|
|
db = await dbInit('strategy_api_serial', getLogger);
|
2021-05-28 11:10:24 +02:00
|
|
|
app = await setupApp(db.stores);
|
2019-10-03 15:01:33 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
afterAll(async () => {
|
|
|
|
await app.destroy();
|
2021-03-19 22:25:21 +01:00
|
|
|
await db.destroy();
|
2019-10-03 15:01:33 +02:00
|
|
|
});
|
2014-11-25 15:28:08 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('gets all strategies', async () => {
|
|
|
|
expect.assertions(1);
|
|
|
|
|
|
|
|
return app.request
|
2017-06-28 10:20:22 +02:00
|
|
|
.get('/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) => {
|
|
|
|
expect(res.body.strategies).toHaveLength(2);
|
2019-10-03 15:01:33 +02:00
|
|
|
});
|
2016-11-13 15:41:35 +01:00
|
|
|
});
|
2014-11-25 15:28:08 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('gets a strategy by name', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
2017-06-28 10:20:22 +02:00
|
|
|
.get('/api/admin/strategies/default')
|
2016-11-13 15:41:35 +01:00
|
|
|
.expect('Content-Type', /json/)
|
2019-10-03 15:01:33 +02:00
|
|
|
.expect(200);
|
2016-11-13 15:41:35 +01:00
|
|
|
});
|
2014-11-25 15:28:08 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('cant get a strategy by name that does not exist', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
2017-06-28 10:20:22 +02:00
|
|
|
.get('/api/admin/strategies/mystrategy')
|
2016-11-13 15:41:35 +01:00
|
|
|
.expect('Content-Type', /json/)
|
2019-10-03 15:01:33 +02:00
|
|
|
.expect(404);
|
2016-11-13 15:41:35 +01:00
|
|
|
});
|
2015-02-10 18:28:17 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('creates a new strategy', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
2017-06-28 10:20:22 +02:00
|
|
|
.post('/api/admin/strategies')
|
|
|
|
.send({
|
|
|
|
name: 'myCustomStrategy',
|
|
|
|
description: 'Best strategy ever.',
|
|
|
|
parameters: [],
|
|
|
|
})
|
2016-11-13 15:41:35 +01:00
|
|
|
.set('Content-Type', 'application/json')
|
2019-10-03 15:01:33 +02:00
|
|
|
.expect(201);
|
2016-11-13 15:41:35 +01:00
|
|
|
});
|
2014-11-25 15:28:08 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('requires new strategies to have a name', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
2017-06-28 10:20:22 +02:00
|
|
|
.post('/api/admin/strategies')
|
2016-11-13 15:41:35 +01:00
|
|
|
.send({ name: '' })
|
|
|
|
.set('Content-Type', 'application/json')
|
2019-10-03 15:01:33 +02:00
|
|
|
.expect(400);
|
2016-11-13 15:41:35 +01:00
|
|
|
});
|
2014-11-25 15:28:08 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('refuses to create a strategy with an existing name', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
2017-06-28 10:20:22 +02:00
|
|
|
.post('/api/admin/strategies')
|
2016-12-12 21:44:21 +01:00
|
|
|
.send({ name: 'default', parameters: [] })
|
2016-11-13 15:41:35 +01:00
|
|
|
.set('Content-Type', 'application/json')
|
2020-09-25 22:31:35 +02:00
|
|
|
.expect(409);
|
2016-11-13 15:41:35 +01:00
|
|
|
});
|
2014-11-25 16:34:24 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('deletes a new strategy', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
|
|
|
.delete('/api/admin/strategies/usersWithEmail')
|
|
|
|
.expect(200);
|
2016-11-13 15:41:35 +01:00
|
|
|
});
|
2015-02-12 11:41:30 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test("can't delete a strategy that dose not exist", async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request.delete('/api/admin/strategies/unknown').expect(404);
|
2016-04-24 22:41:37 +02:00
|
|
|
});
|
2016-12-17 12:31:23 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('updates a exiting strategy', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
2017-06-28 10:20:22 +02:00
|
|
|
.put('/api/admin/strategies/default')
|
|
|
|
.send({
|
|
|
|
name: 'default',
|
|
|
|
description: 'Default is the best!',
|
|
|
|
parameters: [],
|
|
|
|
})
|
2016-12-17 12:31:23 +01:00
|
|
|
.set('Content-Type', 'application/json')
|
2019-10-03 15:01:33 +02:00
|
|
|
.expect(200);
|
2016-12-17 12:31:23 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('cant update a unknown strategy', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
2017-06-28 10:20:22 +02:00
|
|
|
.put('/api/admin/strategies/unknown')
|
2016-12-17 12:31:23 +01:00
|
|
|
.send({ name: 'unkown', parameters: [] })
|
|
|
|
.set('Content-Type', 'application/json')
|
2019-10-03 15:01:33 +02:00
|
|
|
.expect(404);
|
2016-12-17 12:31:23 +01:00
|
|
|
});
|
2021-01-21 13:47:08 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('deprecating a strategy works', async () => {
|
2021-01-21 13:47:08 +01:00
|
|
|
const name = 'deprecate';
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request
|
2021-01-21 13:47:08 +01:00
|
|
|
.post('/api/admin/strategies')
|
|
|
|
.send({ name, description: 'Should deprecate this', parameters: [] })
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(201);
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request
|
2021-01-21 13:47:08 +01:00
|
|
|
.post(`/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);
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request
|
2021-01-21 13:47:08 +01:00
|
|
|
.get(`/api/admin/strategies/${name}`)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.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
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('can reactivate a deprecated strategy', async () => {
|
2021-01-21 13:47:08 +01:00
|
|
|
const name = 'reactivate';
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request
|
2021-01-21 13:47:08 +01:00
|
|
|
.post('/api/admin/strategies')
|
|
|
|
.send({ name, description: 'Should deprecate this', parameters: [] })
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(201);
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request
|
2021-01-21 13:47:08 +01:00
|
|
|
.post(`/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);
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request
|
2021-01-21 13:47:08 +01:00
|
|
|
.get(`/api/admin/strategies/${name}`)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2021-08-12 15:04:37 +02:00
|
|
|
.expect((res) => expect(res.body.deprecated).toBe(true));
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request
|
2021-01-21 13:47:08 +01:00
|
|
|
.post(`/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);
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request
|
2021-01-21 13:47:08 +01:00
|
|
|
.get(`/api/admin/strategies/${name}`)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.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
|
|
|
});
|
2021-01-22 10:03:01 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('cannot deprecate default strategy', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
await app.request
|
2021-02-10 14:15:28 +01:00
|
|
|
.post('/api/admin/strategies/default/deprecate')
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(403);
|
2021-01-22 10:03:01 +01:00
|
|
|
});
|
2021-02-01 09:41:50 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('can update a exiting strategy with deprecated', async () => {
|
|
|
|
await app.request
|
2021-02-01 09:41:50 +01:00
|
|
|
.post('/api/admin/strategies')
|
|
|
|
.send({
|
2021-08-12 15:04:37 +02:00
|
|
|
name: 'myCustomStrategyDeprecated',
|
2021-02-01 09:41:50 +01:00
|
|
|
description: 'Best strategy ever.',
|
|
|
|
parameters: [],
|
|
|
|
deprecated: true,
|
|
|
|
})
|
2021-08-12 15:04:37 +02:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(201);
|
2021-02-01 09:41:50 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const { body: strategy } = await app.request.get(
|
2021-08-12 15:04:37 +02:00
|
|
|
'/api/admin/strategies/myCustomStrategyDeprecated',
|
2021-02-01 09:41:50 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
strategy.description = 'A new desc';
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
return app.request
|
2021-08-12 15:04:37 +02:00
|
|
|
.put('/api/admin/strategies/myCustomStrategyDeprecated')
|
2021-02-01 09:41:50 +01:00
|
|
|
.send(strategy)
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(200);
|
|
|
|
});
|