From 8be798e31f800a738efe9c48e1ebe2aabc1c90c6 Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Mon, 6 Nov 2023 17:29:57 +0300 Subject: [PATCH] test: add adoption POST test --- .../measurement/create-measurement.test.ts | 123 +++++++++++------- 1 file changed, 77 insertions(+), 46 deletions(-) diff --git a/test/tests/integration/measurement/create-measurement.test.ts b/test/tests/integration/measurement/create-measurement.test.ts index 58667e2d..f35587a4 100644 --- a/test/tests/integration/measurement/create-measurement.test.ts +++ b/test/tests/integration/measurement/create-measurement.test.ts @@ -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; let deleteFakeProbe: (socket: Socket) => Promise; let getTestServer; let requestAgent: SuperTest; + 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); }); @@ -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 () => { @@ -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({ @@ -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') @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', @@ -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(); + }); + }); + }); }); });