diff --git a/config/init.sql b/config/init.sql index b53f249e..666c0550 100644 --- a/config/init.sql +++ b/config/init.sql @@ -10,17 +10,17 @@ CREATE TABLE IF NOT EXISTS adopted_probes ( userId VARCHAR(255) NOT NULL, ip VARCHAR(255) NOT NULL, uuid VARCHAR(255), - lastSyncDate DATE, - isCustomCity TINYINT, + lastSyncDate DATE NOT NULL, + isCustomCity TINYINT DEFAULT 0, tags LONGTEXT, - status VARCHAR(255), - version VARCHAR(255), - country VARCHAR(255), + status VARCHAR(255) NOT NULL, + version VARCHAR(255) NOT NULL, + country VARCHAR(255) NOT NULL, city VARCHAR(255), - latitude FLOAT, - longitude FLOAT, - asn INTEGER, - network VARCHAR(255) + latitude FLOAT(10, 5), + longitude FLOAT(10, 5), + asn INTEGER NOT NULL, + network VARCHAR(255) NOT NULL ); CREATE TABLE IF NOT EXISTS directus_users ( @@ -29,27 +29,35 @@ CREATE TABLE IF NOT EXISTS directus_users ( ); INSERT IGNORE INTO adopted_probes ( - id, userId, lastSyncDate, ip, uuid, isCustomCity, + tags, + status, + version, country, city, latitude, - longitude + longitude, + network, + asn ) VALUES ( - '1', '1834071', CURRENT_DATE, '51.158.22.211', - '', + 'c77f021d-23ff-440a-aa96-35e82c73e731', 1, + '["mytag1"]', + 'ready', + '0.26.0', 'FR', 'Marseille', '43.29695', - '5.38107' + '5.38107', + 'SCALEWAY S.A.S.', + 12876 ); INSERT IGNORE INTO directus_users ( diff --git a/src/lib/adopted-probes.ts b/src/lib/adopted-probes.ts index 6e529c6a..3ce35a34 100644 --- a/src/lib/adopted-probes.ts +++ b/src/lib/adopted-probes.ts @@ -9,7 +9,7 @@ import { normalizePublicName } from './geoip/utils.js'; const logger = scopedLogger('adopted-probes'); -const ADOPTED_PROBES_TABLE = 'adopted_probes'; +export const ADOPTED_PROBES_TABLE = 'adopted_probes'; const USERS_TABLE = 'directus_users'; export type AdoptedProbe = { diff --git a/test/tests/integration/middleware/compress.test.ts b/test/tests/integration/middleware/compress.test.ts index 3f8d1d45..1c1b21fe 100644 --- a/test/tests/integration/middleware/compress.test.ts +++ b/test/tests/integration/middleware/compress.test.ts @@ -3,7 +3,7 @@ import { expect } from 'chai'; import nock from 'nock'; import type { Socket } from 'socket.io-client'; import { getTestServer, addFakeProbe, deleteFakeProbe } from '../../../utils/server.js'; -import { geoIpMocks } from '../../../utils/nock-geo-ip.js'; +import geoIpMocks from '../../../mocks/nock-geoip.json' assert { type: 'json' }; describe('compression', () => { let requestAgent: any; diff --git a/test/tests/integration/probes/get-probes.test.ts b/test/tests/integration/probes/get-probes.test.ts index bd453151..138f9bdc 100644 --- a/test/tests/integration/probes/get-probes.test.ts +++ b/test/tests/integration/probes/get-probes.test.ts @@ -5,6 +5,8 @@ import request, { type SuperTest, type Test } from 'supertest'; import type { Socket } from 'socket.io-client'; import { getTestServer, addFakeProbe, deleteFakeProbe } from '../../../utils/server.js'; import nockGeoIpProviders from '../../../utils/nock-geo-ip.js'; +import { adoptedProbes, ADOPTED_PROBES_TABLE } from '../../../../src/lib/adopted-probes.js'; +import { client } from '../../../../src/lib/sql/client.js'; describe('Get Probes', () => { let requestAgent: SuperTest; @@ -54,87 +56,6 @@ describe('Get Probes', () => { }); }); - it('should detect 1 probe in "ready: true" status', async () => { - nockGeoIpProviders({ maxmind: 'argentina', ipinfo: 'argentina', fastly: 'argentina' }); - - const probe = await addProbe(); - probe.emit('probe:status:update', 'ready'); - - await requestAgent.get('/v1/probes') - .send() - .expect(200) - .expect((response) => { - expect(response.body).to.deep.equal([{ - version: '0.14.0', - location: { - continent: 'SA', - region: 'South America', - country: 'AR', - city: 'Buenos Aires', - asn: 61004, - latitude: -34.6131, - longitude: -58.3772, - network: 'InterBS S.R.L. (BAEHOST)', - }, - tags: [], - resolvers: [], - }]); - - expect(response).to.matchApiSchema(); - }); - }); - - it('should detect 2 probes in "ready: true" status', async () => { - nockGeoIpProviders({ ip2location: 'argentina', ipmap: 'argentina', maxmind: 'argentina', ipinfo: 'argentina', fastly: 'argentina' }); - nockGeoIpProviders(); - - const probe1 = await addProbe(); - const probe2 = await addProbe(); - probe1.emit('probe:status:update', 'ready'); - probe2.emit('probe:status:update', 'ready'); - - await requestAgent.get('/v1/probes') - .send() - .expect(200) - .expect((response) => { - expect(response.body).to.deep.equal([ - { - version: '0.14.0', - location: { - continent: 'SA', - region: 'South America', - country: 'AR', - city: 'Buenos Aires', - asn: 61004, - latitude: -34.6131, - longitude: -58.3772, - network: 'InterBS S.R.L. (BAEHOST)', - }, - tags: [], - resolvers: [], - }, - { - version: '0.14.0', - location: { - continent: 'NA', - region: 'Northern America', - country: 'US', - state: 'TX', - city: 'Dallas', - asn: 20004, - latitude: 32.7831, - longitude: -96.8067, - network: 'The Constant Company LLC', - }, - tags: [ 'datacenter-network' ], - resolvers: [], - }, - ]); - - expect(response).to.matchApiSchema(); - }); - }); - it('should detect 4 probes in "ready: true" status', async () => { nockGeoIpProviders({ ip2location: 'argentina', ipmap: 'argentina', maxmind: 'argentina', ipinfo: 'argentina', fastly: 'argentina' }); nockGeoIpProviders(); @@ -291,5 +212,61 @@ describe('Get Probes', () => { 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: '["dashboardtag1"]', + status: 'ready', + version: '0.26.0', + country: 'AR', + city: 'Cordoba', + latitude: '-31.4135', + longitude: '-64.18105', + network: 'InterBS S.R.L. (BAEHOST)', + asn: 61004, + }); + + await adoptedProbes.syncDashboardData(); + }); + + after(async () => { + await client(ADOPTED_PROBES_TABLE).where({ city: 'Cordoba' }).delete(); + }); + + it('should update probes data', async () => { + nockGeoIpProviders({ ip2location: 'argentina', ipmap: 'argentina', maxmind: 'argentina', ipinfo: 'argentina', fastly: 'argentina' }); + const probe = await addProbe(); + probe.emit('probe:status:update', 'ready'); + + await requestAgent.get('/v1/probes') + .send() + .expect(200) + .expect((response) => { + expect(response.body[0]).to.deep.equal({ + version: '0.14.0', + location: { + continent: 'SA', + region: 'South America', + country: 'AR', + city: 'Cordoba', + latitude: -31.4135, + longitude: -64.18105, + asn: 61004, + network: 'InterBS S.R.L. (BAEHOST)', + }, + tags: [ 'u-jimaek-dashboardtag1' ], + resolvers: [], + }); + + expect(response).to.matchApiSchema(); + }); + }); + }); }); }); diff --git a/test/tests/unit/geoip/client.test.ts b/test/tests/unit/geoip/client.test.ts index bfa6185e..f8dff294 100644 --- a/test/tests/unit/geoip/client.test.ts +++ b/test/tests/unit/geoip/client.test.ts @@ -3,8 +3,10 @@ import mockFs from 'mock-fs'; import { expect } from 'chai'; import GeoipClient, { type LocationInfo } from '../../../../src/lib/geoip/client.js'; import NullCache from '../../../../src/lib/cache/null-cache.js'; -import nockGeoIpProviders, { geoIpMocks } from '../../../utils/nock-geo-ip.js'; +import nockGeoIpProviders from '../../../utils/nock-geo-ip.js'; import type { CacheInterface } from '../../../../src/lib/cache/cache-interface.js'; +import geoIpMocks from '../../../mocks/nock-geoip.json' assert { type: 'json' }; + const MOCK_IP = '131.255.7.26'; diff --git a/test/utils/nock-geo-ip.ts b/test/utils/nock-geo-ip.ts index 9c1d96e0..b33b871d 100644 --- a/test/utils/nock-geo-ip.ts +++ b/test/utils/nock-geo-ip.ts @@ -1,7 +1,5 @@ -import * as fs from 'node:fs'; import nock from 'nock'; - -export const geoIpMocks = JSON.parse(fs.readFileSync('./test/mocks/nock-geoip.json').toString()) as Record; +import geoIpMocks from '../mocks/nock-geoip.json' assert { type: 'json' }; type ProviderToMockname = { ipmap?: string;