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 { createServices } from '../../services';
|
|
|
|
import permissions from '../../../test/fixtures/permissions';
|
|
|
|
import getApp from '../../app';
|
2019-10-13 20:21:04 +02:00
|
|
|
|
|
|
|
function getSetup() {
|
|
|
|
const base = `/random${Math.round(Math.random() * 1000)}`;
|
2021-04-12 20:25:03 +02:00
|
|
|
const perms = permissions();
|
2021-04-22 10:07:10 +02:00
|
|
|
const config = createTestConfig({
|
2021-04-12 20:25:03 +02:00
|
|
|
preHook: perms.hook,
|
2021-04-22 10:07:10 +02:00
|
|
|
server: { baseUriPath: base },
|
|
|
|
});
|
2021-08-12 15:04:37 +02:00
|
|
|
const stores = createStores();
|
2021-02-12 10:23:43 +01:00
|
|
|
|
|
|
|
const services = createServices(stores, config);
|
2021-12-09 21:02:58 +01:00
|
|
|
const app = getApp(config, stores, services);
|
2019-10-13 20:21:04 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
base,
|
|
|
|
request: supertest(app),
|
2021-05-28 11:10:24 +02:00
|
|
|
destroy: () => {
|
|
|
|
services.versionService.destroy();
|
2021-12-09 21:25:06 +01:00
|
|
|
services.clientInstanceService.destroy();
|
2021-05-28 11:10:24 +02:00
|
|
|
services.apiTokenService.destroy();
|
|
|
|
},
|
2019-10-13 20:21:04 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
let base;
|
|
|
|
let request;
|
|
|
|
let destroy;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
const setup = getSetup();
|
|
|
|
base = setup.base;
|
|
|
|
request = setup.request;
|
|
|
|
destroy = setup.destroy;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should get all context definitions', () => {
|
|
|
|
expect.assertions(2);
|
2019-10-13 20:21:04 +02:00
|
|
|
return request
|
|
|
|
.get(`${base}/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 === 3).toBe(true);
|
2021-08-12 15:04:37 +02:00
|
|
|
const envField = res.body.find((c) => c.name === 'environment');
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(envField.name === 'environment').toBe(true);
|
2019-10-13 20:21:04 +02:00
|
|
|
});
|
|
|
|
});
|
2020-02-27 21:26:18 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should get context definition', () => {
|
|
|
|
expect.assertions(1);
|
2020-02-27 21:26:18 +01:00
|
|
|
return request
|
|
|
|
.get(`${base}/api/admin/context/userId`)
|
|
|
|
.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('userId');
|
2020-02-27 21:26:18 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should be allowed to use new context field name', () => {
|
|
|
|
expect.assertions(0);
|
2020-02-27 21:26:18 +01:00
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/context/validate`)
|
|
|
|
.send({ name: 'new.name' })
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(200);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not be allowed reuse context field name', () => {
|
|
|
|
expect.assertions(0);
|
2020-02-27 21:26:18 +01:00
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`${base}/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 create a context field', () => {
|
|
|
|
expect.assertions(0);
|
2020-02-27 21:26:18 +01:00
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/context`)
|
|
|
|
.send({ name: 'fancy', description: 'Bla bla' })
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(201);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should create a context field with legal values', () => {
|
|
|
|
expect.assertions(0);
|
2020-02-27 21:26:18 +01:00
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/context`)
|
|
|
|
.send({
|
|
|
|
name: 'page',
|
|
|
|
description: 'Bla bla',
|
|
|
|
legalValues: ['blue', 'red'],
|
|
|
|
})
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(201);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should require name when creating a context field', () => {
|
|
|
|
expect.assertions(0);
|
2020-02-27 21:26:18 +01:00
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/context`)
|
|
|
|
.send({ description: 'Bla bla' })
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not create a context field with existing name', () => {
|
|
|
|
expect.assertions(0);
|
2020-02-27 21:26:18 +01:00
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/context`)
|
|
|
|
.send({ name: 'userId', description: 'Bla bla' })
|
|
|
|
.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 not create a context field with duplicate legal values', () => {
|
|
|
|
expect.assertions(0);
|
2020-02-27 21:26:18 +01:00
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/context`)
|
|
|
|
.send({
|
|
|
|
name: 'page',
|
|
|
|
description: 'Bla bla',
|
|
|
|
legalValues: ['blue', 'blue'],
|
|
|
|
})
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should update a context field with new legal values', () => {
|
|
|
|
expect.assertions(0);
|
2020-02-27 21:26:18 +01:00
|
|
|
|
|
|
|
return request
|
|
|
|
.put(`${base}/api/admin/context/environment`)
|
|
|
|
.send({
|
|
|
|
name: 'environment',
|
|
|
|
description: 'Used target application envrionments',
|
|
|
|
legalValues: ['local', 'stage', 'production'],
|
|
|
|
})
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(200);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not delete a unknown context field', () => {
|
|
|
|
expect.assertions(0);
|
2020-02-27 21:26:18 +01:00
|
|
|
|
|
|
|
return request
|
|
|
|
.delete(`${base}/api/admin/context/unknown`)
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should delete a context field', () => {
|
|
|
|
expect.assertions(0);
|
2020-02-27 21:26:18 +01:00
|
|
|
|
|
|
|
return request
|
|
|
|
.delete(`${base}/api/admin/context/appName`)
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(200);
|
|
|
|
});
|