2023-02-14 11:25:13 +01:00
|
|
|
import {
|
|
|
|
IUnleashTest,
|
|
|
|
setupAppWithCustomConfig,
|
|
|
|
} from '../../helpers/test-helper';
|
2021-09-20 12:13:38 +02:00
|
|
|
import dbInit, { ITestDb } from '../../helpers/database-init';
|
2021-08-12 15:04:37 +02:00
|
|
|
import getLogger from '../../../fixtures/no-logger';
|
2022-08-09 16:14:50 +02:00
|
|
|
import { FEATURE_CREATED, IBaseEvent } from '../../../../lib/types/events';
|
|
|
|
import { randomId } from '../../../../lib/util/random-id';
|
2023-09-27 15:23:05 +02:00
|
|
|
import { EventService } from '../../../../lib/services';
|
2019-10-03 15:01:33 +02:00
|
|
|
|
2021-09-20 12:13:38 +02:00
|
|
|
let app: IUnleashTest;
|
|
|
|
let db: ITestDb;
|
2023-09-27 15:23:05 +02:00
|
|
|
let eventService: EventService;
|
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('event_api_serial', getLogger);
|
2023-02-14 11:25:13 +01:00
|
|
|
app = await setupAppWithCustomConfig(db.stores, {
|
|
|
|
experimental: {
|
|
|
|
flags: {
|
|
|
|
strictSchemaValidation: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2023-09-27 15:23:05 +02:00
|
|
|
eventService = new EventService(db.stores, { getLogger });
|
2019-10-03 15:01:33 +02:00
|
|
|
});
|
|
|
|
|
2022-08-09 16:14:50 +02:00
|
|
|
beforeEach(async () => {
|
2023-09-27 15:23:05 +02:00
|
|
|
await db.stores.eventStore.deleteAll();
|
2022-08-09 16:14:50 +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
|
|
|
});
|
2016-11-10 21:15:16 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('returns events', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
return app.request
|
2017-06-28 10:20:22 +02:00
|
|
|
.get('/api/admin/events')
|
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 14:41:11 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('returns events given a name', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
return app.request
|
2017-06-28 10:20:22 +02:00
|
|
|
.get('/api/admin/events/myname')
|
2016-11-13 15:41:35 +01:00
|
|
|
.expect('Content-Type', /json/)
|
2019-10-03 15:01:33 +02:00
|
|
|
.expect(200);
|
2016-04-24 22:41:37 +02:00
|
|
|
});
|
2021-09-20 12:13:38 +02:00
|
|
|
|
|
|
|
test('Can filter by project', async () => {
|
2023-09-27 15:23:05 +02:00
|
|
|
await eventService.storeEvent({
|
2021-09-20 12:13:38 +02:00
|
|
|
type: FEATURE_CREATED,
|
|
|
|
project: 'something-else',
|
|
|
|
data: { id: 'some-other-feature' },
|
|
|
|
tags: [],
|
|
|
|
createdBy: 'test-user',
|
|
|
|
environment: 'test',
|
|
|
|
});
|
2023-09-27 15:23:05 +02:00
|
|
|
await eventService.storeEvent({
|
2021-09-20 12:13:38 +02:00
|
|
|
type: FEATURE_CREATED,
|
|
|
|
project: 'default',
|
|
|
|
data: { id: 'feature' },
|
|
|
|
tags: [],
|
|
|
|
createdBy: 'test-user',
|
|
|
|
environment: 'test',
|
|
|
|
});
|
|
|
|
await app.request
|
|
|
|
.get('/api/admin/events?project=default')
|
|
|
|
.expect(200)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.events).toHaveLength(1);
|
|
|
|
expect(res.body.events[0].data.id).toEqual('feature');
|
|
|
|
});
|
|
|
|
});
|
2022-08-09 16:14:50 +02:00
|
|
|
|
|
|
|
test('can search for events', async () => {
|
|
|
|
const events: IBaseEvent[] = [
|
|
|
|
{
|
|
|
|
type: FEATURE_CREATED,
|
|
|
|
project: randomId(),
|
|
|
|
data: { id: randomId() },
|
|
|
|
tags: [],
|
|
|
|
createdBy: randomId(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: FEATURE_CREATED,
|
|
|
|
project: randomId(),
|
|
|
|
data: { id: randomId() },
|
|
|
|
preData: { id: randomId() },
|
2023-09-04 17:37:23 +02:00
|
|
|
tags: [{ type: 'simple', value: randomId() }],
|
2022-08-09 16:14:50 +02:00
|
|
|
createdBy: randomId(),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
events.map((event) => {
|
2023-09-27 15:23:05 +02:00
|
|
|
return eventService.storeEvent(event);
|
2022-08-09 16:14:50 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
await app.request
|
|
|
|
.post('/api/admin/events/search')
|
|
|
|
.send({})
|
|
|
|
.expect(200)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.events).toHaveLength(2);
|
|
|
|
});
|
|
|
|
await app.request
|
|
|
|
.post('/api/admin/events/search')
|
|
|
|
.send({ limit: 1, offset: 1 })
|
|
|
|
.expect(200)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.events).toHaveLength(1);
|
|
|
|
});
|
|
|
|
await app.request
|
|
|
|
.post('/api/admin/events/search')
|
|
|
|
.send({ query: events[1].data.id })
|
|
|
|
.expect(200)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.events).toHaveLength(1);
|
|
|
|
expect(res.body.events[0].data.id).toEqual(events[1].data.id);
|
|
|
|
});
|
|
|
|
await app.request
|
|
|
|
.post('/api/admin/events/search')
|
|
|
|
.send({ query: events[1].preData.id })
|
|
|
|
.expect(200)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.events).toHaveLength(1);
|
|
|
|
expect(res.body.events[0].preData.id).toEqual(events[1].preData.id);
|
|
|
|
});
|
2023-09-04 17:37:23 +02:00
|
|
|
await app.request
|
|
|
|
.post('/api/admin/events/search')
|
|
|
|
.send({ query: events[1].tags![0].value })
|
|
|
|
.expect(200)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.events).toHaveLength(1);
|
|
|
|
expect(res.body.events[0].data.id).toEqual(events[1].data.id);
|
|
|
|
});
|
2022-08-09 16:14:50 +02:00
|
|
|
});
|