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';
|
2020-02-27 21:26:18 +01:00
|
|
|
|
2021-03-19 22:25:21 +01:00
|
|
|
let db;
|
2021-05-28 11:10:24 +02:00
|
|
|
let app;
|
2020-02-27 21:26:18 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
beforeAll(async () => {
|
2021-03-19 22:25:21 +01:00
|
|
|
db = await dbInit('context_api_serial', getLogger);
|
2021-05-28 11:10:24 +02:00
|
|
|
app = await setupApp(db.stores);
|
2020-02-27 21:26:18 +01: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();
|
2020-02-27 21:26:18 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('gets all context fields', async () => {
|
|
|
|
expect.assertions(1);
|
|
|
|
return app.request
|
2020-02-27 21:26:18 +01:00
|
|
|
.get('/api/admin/context')
|
|
|
|
.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.length).toBe(3);
|
2020-02-27 21:26:18 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('get the context field', async () => {
|
|
|
|
expect.assertions(1);
|
|
|
|
return app.request
|
2020-02-27 21:26:18 +01:00
|
|
|
.get('/api/admin/context/environment')
|
|
|
|
.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.name).toBe('environment');
|
2020-02-27 21:26:18 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should create context field', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
return app.request
|
2020-02-27 21:26:18 +01:00
|
|
|
.post('/api/admin/context')
|
|
|
|
.send({
|
|
|
|
name: 'country',
|
|
|
|
description: 'A Country',
|
|
|
|
})
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(201);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should create context field with legalValues', async () => {
|
2022-04-19 08:40:07 +02:00
|
|
|
expect.assertions(3);
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
name: 'region',
|
|
|
|
description: 'A region',
|
|
|
|
legalValues: [
|
|
|
|
{ value: 'north' },
|
|
|
|
{ value: 'south', description: 'south-desc' },
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
await app.request
|
2020-02-27 21:26:18 +01:00
|
|
|
.post('/api/admin/context')
|
2022-04-19 08:40:07 +02:00
|
|
|
.send(data)
|
2020-02-27 21:26:18 +01:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(201);
|
2022-04-19 08:40:07 +02:00
|
|
|
|
|
|
|
const res = await app.request.get(`/api/admin/context/${data.name}`);
|
|
|
|
expect(res.body.name).toEqual(data.name);
|
|
|
|
expect(res.body.description).toEqual(data.description);
|
|
|
|
expect(res.body.legalValues).toEqual(data.legalValues);
|
2020-02-27 21:26:18 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should update context field with legalValues', async () => {
|
2022-04-19 08:40:07 +02:00
|
|
|
expect.assertions(3);
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
name: 'environment',
|
|
|
|
description: 'Updated description',
|
|
|
|
legalValues: [
|
|
|
|
{ value: 'dev', description: 'dev-desc' },
|
|
|
|
{ value: 'prod' },
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
await app.request
|
2020-02-27 21:26:18 +01:00
|
|
|
.put('/api/admin/context/environment')
|
2022-04-19 08:40:07 +02:00
|
|
|
.send(data)
|
2020-02-27 21:26:18 +01:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(200);
|
2022-04-19 08:40:07 +02:00
|
|
|
|
|
|
|
const res = await app.request.get(`/api/admin/context/${data.name}`);
|
|
|
|
expect(res.body.name).toEqual(data.name);
|
|
|
|
expect(res.body.description).toEqual(data.description);
|
|
|
|
expect(res.body.legalValues).toEqual(data.legalValues);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should reject string legalValues', async () => {
|
|
|
|
await app.request
|
|
|
|
.put('/api/admin/context/environment')
|
|
|
|
.send({ name: 'environment', legalValues: ['a'] })
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should reject empty legalValues', async () => {
|
|
|
|
await app.request
|
|
|
|
.put('/api/admin/context/environment')
|
|
|
|
.send({ name: 'environment', legalValues: [{ description: 'b' }] })
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should reject legalValues without value', async () => {
|
|
|
|
await app.request
|
|
|
|
.put('/api/admin/context/environment')
|
|
|
|
.send({ name: 'environment', legalValues: [{ description: 'b' }] })
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(400);
|
2020-02-27 21:26:18 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should create context field with stickiness', async () => {
|
|
|
|
expect.assertions(1);
|
2021-02-11 17:59:16 +01:00
|
|
|
const name = 'with-sticky';
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request
|
2021-02-11 17:59:16 +01:00
|
|
|
.post('/api/admin/context')
|
|
|
|
.send({
|
|
|
|
name,
|
|
|
|
description: 'A context field supporting stickiness',
|
|
|
|
stickiness: true,
|
|
|
|
})
|
|
|
|
.set('Content-Type', 'application/json');
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const res = await app.request.get(`/api/admin/context/${name}`);
|
2021-02-11 17:59:16 +01:00
|
|
|
const contextField = res.body;
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(contextField.stickiness).toBe(true);
|
2021-02-11 17:59:16 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not create context field when name is missing', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
return app.request
|
2020-02-27 21:26:18 +01:00
|
|
|
.post('/api/admin/context')
|
|
|
|
.send({
|
|
|
|
description: 'A Country',
|
|
|
|
})
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('refuses to create a context field with an existing name', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
return app.request
|
|
|
|
.post('/api/admin/context')
|
|
|
|
.send({ name: 'userId' })
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(409);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should delete context field', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
return app.request.delete('/api/admin/context/userId').expect(200);
|
2020-02-27 21:26:18 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('refuses to create a context not url-friendly name', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
return app.request
|
2020-02-27 21:26:18 +01:00
|
|
|
.post('/api/admin/context')
|
|
|
|
.send({ name: 'not very nice' })
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should validate name to ok', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
return app.request
|
2020-02-27 21:26:18 +01:00
|
|
|
.post('/api/admin/context/validate')
|
|
|
|
.send({ name: 'newField' })
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(200);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should validate name to not ok', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
return app.request
|
2020-02-27 21:26:18 +01:00
|
|
|
.post('/api/admin/context/validate')
|
|
|
|
.send({ name: 'environment' })
|
|
|
|
.set('Content-Type', 'application/json')
|
2020-09-25 22:31:35 +02:00
|
|
|
.expect(409);
|
2020-02-27 21:26:18 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should validate name to not ok for non url-friendly', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
return app.request
|
2020-02-27 21:26:18 +01:00
|
|
|
.post('/api/admin/context/validate')
|
|
|
|
.send({ name: 'not url friendly' })
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(400);
|
|
|
|
});
|
2021-10-14 08:30:36 +02:00
|
|
|
|
|
|
|
test('should update context field with stickiness', async () => {
|
|
|
|
const name = 'with-sticky-update';
|
|
|
|
await app.request
|
|
|
|
.post('/api/admin/context')
|
|
|
|
.send({
|
|
|
|
name,
|
|
|
|
description: 'A context field supporting stickiness',
|
|
|
|
})
|
|
|
|
.set('Content-Type', 'application/json');
|
|
|
|
await app.request
|
|
|
|
.put(`/api/admin/context/${name}`)
|
|
|
|
.send({
|
|
|
|
description: 'asd',
|
|
|
|
legalValues: [],
|
|
|
|
name,
|
|
|
|
stickiness: true,
|
|
|
|
})
|
|
|
|
.set('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
const res = await app.request.get(`/api/admin/context/${name}`);
|
|
|
|
const contextField = res.body;
|
|
|
|
|
|
|
|
expect(contextField.description).toBe('asd');
|
|
|
|
expect(contextField.stickiness).toBe(true);
|
|
|
|
});
|