Skip to content

Commit

Permalink
test: add adoption POST test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Nov 6, 2023
1 parent 5fc7067 commit 8be798e
Showing 1 changed file with 77 additions and 46 deletions.
123 changes: 77 additions & 46 deletions test/tests/integration/measurement/create-measurement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ import * as td from 'testdouble';
import nock from 'nock';
import type { Socket } from 'socket.io-client';
import nockGeoIpProviders from '../../../utils/nock-geo-ip.js';
import { client } from '../../../../src/lib/sql/client.js';
import type { AdoptedProbes } from '../../../../src/lib/adopted-probes.js';

describe('Create measurement', () => {
let addFakeProbe: () => Promise<Socket>;
let deleteFakeProbe: (socket: Socket) => Promise<void>;
let getTestServer;
let requestAgent: SuperTest<Test>;
let adoptedProbes: AdoptedProbes;
let ADOPTED_PROBES_TABLE: string;

before(async () => {
await td.replaceEsm('../../../../src/lib/ip-ranges.ts', { getRegion: () => 'gcp-us-west4', populateMemList: () => Promise.resolve() });
({ getTestServer, addFakeProbe, deleteFakeProbe } = await import('../../../utils/server.js'));
({ adoptedProbes, ADOPTED_PROBES_TABLE } = await import('../../../../src/lib/adopted-probes.js'));
const app = await getTestServer();
requestAgent = request(app);
});
Expand Down Expand Up @@ -58,6 +63,11 @@ describe('Create measurement', () => {
before(async () => {
nockGeoIpProviders();
probe = await addFakeProbe();
probe.emit('probe:status:update', 'ready');
});

afterEach(() => {
probe.emit('probe:status:update', 'ready');
});

after(async () => {
Expand All @@ -66,34 +76,7 @@ describe('Create measurement', () => {
});

it('should respond with error if there are no ready probes', async () => {
await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
target: 'example.com',
locations: [{ country: 'US' }],
measurementOptions: {
packets: 4,
},
limit: 2,
})
.expect(422)
.expect((response) => {
expect(response.body).to.deep.equal({
error: {
message: 'No suitable probes found.',
type: 'no_probes_found',
},
links: {
documentation: 'https://www.jsdelivr.com/docs/api.globalping.io#post-/v1/measurements',
},
});

expect(response).to.matchApiSchema();
});
});

it('should respond with error if probe emitted non-"ready" "probe:status:update"', async () => {
probe.emit('probe:status:update', 'sigterm');
probe.emit('probe:status:update', 'initializing');

await requestAgent.post('/v1/measurements')
.send({
Expand Down Expand Up @@ -121,8 +104,7 @@ describe('Create measurement', () => {
});
});

it('should respond with error if probe emitted non-"ready" "probe:status:update" after being "ready"', async () => {
probe.emit('probe:status:update', 'ready');
it('should respond with error if probe emitted non-"ready" "probe:status:update"', async () => {
probe.emit('probe:status:update', 'sigterm');

await requestAgent.post('/v1/measurements')
Expand Down Expand Up @@ -152,8 +134,6 @@ describe('Create measurement', () => {
});

it('should create measurement with global limit', async () => {
probe.emit('probe:status:update', 'ready');

await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
Expand All @@ -174,8 +154,6 @@ describe('Create measurement', () => {
});

it('should create measurement with location limit', async () => {
probe.emit('probe:status:update', 'ready');

await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
Expand All @@ -195,8 +173,6 @@ describe('Create measurement', () => {
});

it('should create measurement for globally distributed probes', async () => {
probe.emit('probe:status:update', 'ready');

await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
Expand All @@ -216,8 +192,6 @@ describe('Create measurement', () => {
});

it('should create measurement with "magic: world" location', async () => {
probe.emit('probe:status:update', 'ready');

await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
Expand All @@ -237,8 +211,6 @@ describe('Create measurement', () => {
});

it('should create measurement with "magic" value in any case', async () => {
probe.emit('probe:status:update', 'ready');

await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
Expand All @@ -258,8 +230,6 @@ describe('Create measurement', () => {
});

it('should create measurement with partial tag value "magic: GCP-us-West4" location', async () => {
probe.emit('probe:status:update', 'ready');

await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
Expand All @@ -279,8 +249,6 @@ describe('Create measurement', () => {
});

it('should not create measurement with "magic: non-existing-tag" location', async () => {
probe.emit('probe:status:update', 'ready');

await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
Expand All @@ -307,8 +275,6 @@ describe('Create measurement', () => {
});

it('should create measurement with "tags: ["tag-value"]" location', async () => {
probe.emit('probe:status:update', 'ready');

await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
Expand All @@ -326,5 +292,70 @@ describe('Create measurement', () => {
expect(response).to.matchApiSchema();
});
});

describe('adopted probes', () => {
before(async () => {
await client(ADOPTED_PROBES_TABLE).insert({
userId: '1834071',
lastSyncDate: new Date(),
ip: '1.2.3.4',
uuid: '1-1-1-1-1',
isCustomCity: 1,
tags: '["dashboard_tag"]',
status: 'ready',
version: '0.26.0',
country: 'US',
city: 'Oklahoma City',
latitude: '35.46756',
longitude: '-97.51643',
network: 'InterBS S.R.L. (BAEHOST)',
asn: 61004,
});

await adoptedProbes.syncDashboardData();
});

after(async () => {
await client(ADOPTED_PROBES_TABLE).where({ city: 'Oklahoma City' }).delete();
});

it('should create measurement with adopted "city: Oklahoma City" location', async () => {
await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
target: 'example.com',
locations: [{ city: 'Oklahoma City', limit: 2 }],
measurementOptions: {
packets: 4,
},
})
.expect(202)
.expect((response) => {
expect(response.body.id).to.exist;
expect(response.header.location).to.exist;
expect(response.body.probesCount).to.equal(1);
expect(response).to.matchApiSchema();
});
});

it('should create measurement with adopted "tags: ["u-jimaek-dashboard_tag"]" location', async () => {
await requestAgent.post('/v1/measurements')
.send({
type: 'ping',
target: 'example.com',
locations: [{ tags: [ 'u-jimaek-dashboard_tag' ], limit: 2 }],
measurementOptions: {
packets: 4,
},
})
.expect(202)
.expect((response) => {
expect(response.body.id).to.exist;
expect(response.header.location).to.exist;
expect(response.body.probesCount).to.equal(1);
expect(response).to.matchApiSchema();
});
});
});
});
});

0 comments on commit 8be798e

Please sign in to comment.