2021-09-14 20:17:13 +02:00
|
|
|
import faker from 'faker';
|
|
|
|
import { setupApp } from '../../helpers/test-helper';
|
|
|
|
import dbInit from '../../helpers/database-init';
|
|
|
|
import getLogger from '../../../fixtures/no-logger';
|
|
|
|
import version from '../../../../lib/util/version';
|
2021-03-04 12:54:13 +01:00
|
|
|
|
|
|
|
const asyncFilter = async (arr, predicate) => {
|
|
|
|
const results = await Promise.all(arr.map(predicate));
|
|
|
|
|
|
|
|
return arr.filter((_v, index) => results[index]);
|
|
|
|
};
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
let app;
|
2021-03-19 22:25:21 +01:00
|
|
|
let db;
|
2021-03-04 12:54:13 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
beforeAll(async () => {
|
2021-03-19 22:25:21 +01:00
|
|
|
db = await dbInit('register_client', getLogger);
|
2021-05-28 11:10:24 +02:00
|
|
|
app = await setupApp(db.stores);
|
2021-03-04 12:54:13 +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();
|
2021-03-04 12:54:13 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should register client', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
return app.request
|
2021-03-04 12:54:13 +01:00
|
|
|
.post('/api/client/register')
|
|
|
|
.send({
|
|
|
|
appName: 'demo',
|
|
|
|
instanceId: 'test',
|
|
|
|
strategies: ['default'],
|
|
|
|
started: Date.now(),
|
|
|
|
interval: 10,
|
|
|
|
})
|
|
|
|
.expect(202);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should allow client to register multiple times', async () => {
|
|
|
|
expect.assertions(2);
|
|
|
|
jest.useFakeTimers('modern');
|
|
|
|
const { clientInstanceStore, clientApplicationsStore } = db.stores;
|
|
|
|
|
2021-03-04 12:54:13 +01:00
|
|
|
const clientRegistration = {
|
|
|
|
appName: 'multipleRegistration',
|
|
|
|
instanceId: 'test',
|
|
|
|
strategies: ['default', 'another'],
|
|
|
|
started: Date.now(),
|
|
|
|
interval: 10,
|
|
|
|
};
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request
|
|
|
|
.post('/api/client/register')
|
|
|
|
.send(clientRegistration)
|
|
|
|
.expect(202);
|
|
|
|
|
|
|
|
await app.request
|
2021-03-04 12:54:13 +01:00
|
|
|
.post('/api/client/register')
|
|
|
|
.send(clientRegistration)
|
2021-05-28 11:10:24 +02:00
|
|
|
.expect(202);
|
|
|
|
|
|
|
|
jest.advanceTimersByTime(6 * 1000);
|
|
|
|
expect(clientApplicationsStore.exists(clientRegistration)).toBeTruthy();
|
|
|
|
expect(clientInstanceStore.exists(clientRegistration)).toBeTruthy();
|
|
|
|
jest.useRealTimers();
|
2021-03-04 12:54:13 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test.skip('Should handle a massive bulk registration', async () => {
|
|
|
|
const { clientInstanceStore, clientApplicationsStore } = db.stores;
|
|
|
|
|
2021-03-04 12:54:13 +01:00
|
|
|
const clients = [];
|
|
|
|
while (clients.length < 2000) {
|
|
|
|
const clientRegistration = {
|
|
|
|
appName: faker.internet.domainName(),
|
2021-05-21 19:30:57 +02:00
|
|
|
instanceId: faker.datatype.uuid(),
|
2021-03-04 12:54:13 +01:00
|
|
|
strategies: ['default'],
|
|
|
|
started: Date.now(),
|
2021-05-21 19:30:57 +02:00
|
|
|
interval: faker.datatype.number(),
|
2021-03-04 12:54:13 +01:00
|
|
|
sdkVersion: version,
|
|
|
|
icon: '',
|
|
|
|
description: faker.company.catchPhrase(),
|
|
|
|
color: faker.internet.color(),
|
|
|
|
};
|
|
|
|
clients.push(clientRegistration);
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request
|
2021-03-04 12:54:13 +01:00
|
|
|
.post('/api/client/register')
|
|
|
|
.send(clientRegistration)
|
|
|
|
.expect(202);
|
|
|
|
}
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(clients.length).toBe(2000);
|
2021-08-12 15:04:37 +02:00
|
|
|
await new Promise((res) => setTimeout(res, 5500));
|
2021-03-04 12:54:13 +01:00
|
|
|
|
|
|
|
// Verify clientInstance
|
2021-08-12 15:04:37 +02:00
|
|
|
const notSavedInstance = await asyncFilter(clients, async (c) => {
|
2021-03-04 12:54:13 +01:00
|
|
|
const exists = await clientInstanceStore.exists(c);
|
|
|
|
return !exists;
|
|
|
|
});
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(notSavedInstance.length).toBe(0);
|
2021-03-04 12:54:13 +01:00
|
|
|
|
|
|
|
// Verify application
|
2021-08-12 15:04:37 +02:00
|
|
|
const notSavedApp = await asyncFilter(clients, async (c) => {
|
2021-03-04 12:54:13 +01:00
|
|
|
const exists = await clientApplicationsStore.exists(c);
|
|
|
|
return !exists;
|
|
|
|
});
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(notSavedApp.length).toBe(0);
|
2021-03-04 12:54:13 +01:00
|
|
|
});
|