Skip to content

Commit

Permalink
test: add GET int test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Nov 6, 2023
1 parent 6591ab5 commit 6e4b564
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 101 deletions.
36 changes: 22 additions & 14 deletions config/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion src/lib/adopted-probes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion test/tests/integration/middleware/compress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
139 changes: 58 additions & 81 deletions test/tests/integration/probes/get-probes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Test>;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});
});
});
});
});
4 changes: 3 additions & 1 deletion test/tests/unit/geoip/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
4 changes: 1 addition & 3 deletions test/utils/nock-geo-ip.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>;
import geoIpMocks from '../mocks/nock-geoip.json' assert { type: 'json' };

type ProviderToMockname = {
ipmap?: string;
Expand Down

0 comments on commit 6e4b564

Please sign in to comment.