Skip to content

Commit

Permalink
test: fix part of the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Nov 30, 2023
1 parent 20997b1 commit 4dc3b00
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 221 deletions.
2 changes: 1 addition & 1 deletion src/measurement/schema/global-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const schema = Joi.object({
type: Joi.string().valid('ping', 'traceroute', 'dns', 'mtr', 'http').insensitive().required(),
target: targetSchema,
measurementOptions: measurementSchema,
locations: Joi.alternatives().try(locationSchema, Joi.string()),
locations: locationSchema,
limit: Joi.number().min(1).max(measurementConfig.limits.global).default(GLOBAL_DEFAULTS.limit),
inProgressUpdates: Joi.bool().default(GLOBAL_DEFAULTS.inProgressUpdates),
});
41 changes: 22 additions & 19 deletions src/measurement/schema/location-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,25 @@ const measurementConfig = config.get<{limits: {global: number; location: number}

const normalizeValue = (value: string): string => anyAscii(value);

export const schema = Joi.array().items(Joi.object().keys({
continent: Joi.string().valid(...Object.keys(continents)).insensitive()
.messages({ 'any.only': 'The continent must be a valid two-letter continent code' }),
region: Joi.string().valid(...regionNames).insensitive(),
country: Joi.string().valid(...Object.keys(countries)).insensitive()
.messages({ 'any.only': 'The country must be a valid two-letter ISO code' }),
state: Joi.string().valid(...Object.values(states)).insensitive()
.messages({ 'any.only': 'The US state must be a valid two-letter code, e.g. CA' }),
city: Joi.string().min(1).max(128).lowercase().custom(normalizeValue),
network: Joi.string().min(1).max(128).lowercase().custom(normalizeValue),
asn: Joi.number().integer().positive(),
magic: Joi.string().min(1).lowercase().custom(normalizeValue),
tags: Joi.array().items(Joi.string().min(1).max(128).lowercase().custom(normalizeValue)),
limit: Joi.number().min(1).max(measurementConfig.limits.location).when(Joi.ref('/limit'), {
is: Joi.exist(),
then: Joi.forbidden().messages({ 'any.unknown': 'limit per location is not allowed when a global limit is set' }),
otherwise: Joi.number().default(1),
}),
}).or('continent', 'region', 'country', 'state', 'city', 'network', 'asn', 'magic', 'tags')).default(GLOBAL_DEFAULTS.locations);
export const schema = Joi.alternatives().try(
Joi.string(),
Joi.array().items(Joi.object().keys({
continent: Joi.string().valid(...Object.keys(continents)).insensitive()
.messages({ 'any.only': 'The continent must be a valid two-letter continent code' }),
region: Joi.string().valid(...regionNames).insensitive(),
country: Joi.string().valid(...Object.keys(countries)).insensitive()
.messages({ 'any.only': 'The country must be a valid two-letter ISO code' }),
state: Joi.string().valid(...Object.values(states)).insensitive()
.messages({ 'any.only': 'The US state must be a valid two-letter code, e.g. CA' }),
city: Joi.string().min(1).max(128).lowercase().custom(normalizeValue),
network: Joi.string().min(1).max(128).lowercase().custom(normalizeValue),
asn: Joi.number().integer().positive(),
magic: Joi.string().min(1).lowercase().custom(normalizeValue),
tags: Joi.array().items(Joi.string().min(1).max(128).lowercase().custom(normalizeValue)),
limit: Joi.number().min(1).max(measurementConfig.limits.location).when(Joi.ref('/limit'), {
is: Joi.exist(),
then: Joi.forbidden().messages({ 'any.unknown': 'limit per location is not allowed when a global limit is set' }),
otherwise: Joi.number().default(1),
}),
}).or('continent', 'region', 'country', 'state', 'city', 'network', 'asn', 'magic', 'tags')),
).default(GLOBAL_DEFAULTS.locations);
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { expect } from 'chai';
import * as sinon from 'sinon';
import type { Socket } from 'socket.io-client';
import request from 'supertest';
import { getTestServer, addFakeProbe, deleteFakeProbe } from '../../../utils/server.js';
import nockGeoIpProviders from '../../../utils/nock-geo-ip.js';
import { getTestServer, addFakeProbe, deleteFakeProbe } from '../../utils/server.js';
import nockGeoIpProviders from '../../utils/nock-geo-ip.js';

let probe: Socket;
const app = await getTestServer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import process from 'node:process';
import { expect } from 'chai';
import * as sinon from 'sinon';
import request, { type SuperTest, type Test } from 'supertest';
import { getTestServer } from '../../../utils/server.js';
import { getTestServer } from '../../utils/server.js';

after(() => {
process.removeAllListeners('SIGTERM');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import requestIp from 'request-ip';
import type { RateLimiterRedis } from 'rate-limiter-flexible';
import { expect } from 'chai';
import type { Socket } from 'socket.io-client';
import { getTestServer, addFakeProbe, deleteFakeProbe } from '../../../utils/server.js';
import nockGeoIpProviders from '../../../utils/nock-geo-ip.js';
import { getTestServer, addFakeProbe, deleteFakeProbe } from '../../utils/server.js';
import nockGeoIpProviders from '../../utils/nock-geo-ip.js';

describe('rate limiter', () => {
let app: Server;
Expand All @@ -24,7 +24,7 @@ describe('rate limiter', () => {
// Koa sees ipv6-ipv4 monster
clientIpv6 = `::ffff:${clientIp ?? '127.0.0.1'}`;

const rateLimiter = await import('../../../../src/lib/ratelimiter.js');
const rateLimiter = await import('../../../src/lib/ratelimiter.js');
rateLimiterInstance = rateLimiter.default;

nockGeoIpProviders();
Expand All @@ -43,15 +43,15 @@ describe('rate limiter', () => {

describe('headers', () => {
it('should NOT include headers (GET)', async () => {
const response = await requestAgent.get('/v1/').send() as Response;
const response = await requestAgent.get('/v1/').send().expect(200) as Response;

expect(response.headers['x-ratelimit-limit']).to.not.exist;
expect(response.headers['x-ratelimit-remaining']).to.not.exist;
expect(response.headers['x-ratelimit-reset']).to.not.exist;
});

it('should NOT include headers if body is not valid (POST)', async () => {
const response = await requestAgent.post('/v1/measurements').send() as Response;
const response = await requestAgent.post('/v1/measurements').send().expect(400) as Response;

expect(response.headers['x-ratelimit-limit']).to.not.exist;
expect(response.headers['x-ratelimit-remaining']).to.not.exist;
Expand All @@ -62,7 +62,7 @@ describe('rate limiter', () => {
const response = await requestAgent.post('/v1/measurements').send({
type: 'ping',
target: 'jsdelivr.com',
}) as Response;
}).expect(202) as Response;

expect(response.headers['x-ratelimit-limit']).to.exist;
expect(response.headers['x-ratelimit-remaining']).to.exist;
Expand All @@ -73,7 +73,7 @@ describe('rate limiter', () => {
const response = await requestAgent.post('/v1/measurements').send({
type: 'ping',
target: 'jsdelivr.com',
}) as Response;
}).expect(202) as Response;

expect(response.headers['x-ratelimit-limit']).to.equal('100000');
expect(response.headers['x-ratelimit-remaining']).to.equal('99999');
Expand All @@ -82,7 +82,7 @@ describe('rate limiter', () => {
const response2 = await requestAgent.post('/v1/measurements').send({
type: 'ping',
target: 'jsdelivr.com',
}) as Response;
}).expect(202) as Response;

expect(response2.headers['x-ratelimit-limit']).to.equal('100000');
expect(response2.headers['x-ratelimit-remaining']).to.equal('99998');
Expand All @@ -97,7 +97,7 @@ describe('rate limiter', () => {
const response = await requestAgent.post('/v1/measurements').send({
type: 'ping',
target: 'jsdelivr.com',
}) as Response;
}).expect(202) as Response;

expect(Number(response.headers['x-ratelimit-remaining'])).to.equal(99999);
});
Expand All @@ -108,10 +108,9 @@ describe('rate limiter', () => {
const response = await requestAgent.post('/v1/measurements').send({
type: 'ping',
target: 'jsdelivr.com',
}) as Response;
}).expect(429) as Response;

expect(Number(response.headers['x-ratelimit-remaining'])).to.equal(0);
expect(response.statusCode).to.equal(429);
});
});
});
44 changes: 22 additions & 22 deletions test/tests/unit/geoip/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('geoip service', () => {
expect(info).to.deep.equal({
continent: 'SA',
country: 'AR',
state: undefined,
state: null,
city: 'Buenos Aires',
region: 'South America',
normalizedCity: 'buenos aires',
Expand All @@ -89,7 +89,7 @@ describe('geoip service', () => {
expect(info).to.deep.equal({
continent: 'SA',
country: 'AR',
state: undefined,
state: null,
city: 'Buenos Aires',
region: 'South America',
normalizedCity: 'buenos aires',
Expand All @@ -98,7 +98,7 @@ describe('geoip service', () => {
longitude: -58.3772,
network: 'InterBS S.R.L. (BAEHOST)',
normalizedNetwork: 'interbs s.r.l. (baehost)',
isHosting: undefined,
isHosting: null,
});
});

Expand All @@ -119,7 +119,7 @@ describe('geoip service', () => {
longitude: -96.8067,
network: 'The Constant Company LLC',
normalizedNetwork: 'the constant company llc',
isHosting: undefined,
isHosting: null,
});
});

Expand All @@ -140,7 +140,7 @@ describe('geoip service', () => {
longitude: -96.8067,
network: 'The Constant Company LLC',
normalizedNetwork: 'the constant company llc',
isHosting: undefined,
isHosting: null,
});
});

Expand All @@ -152,7 +152,7 @@ describe('geoip service', () => {
expect(info).to.deep.equal({
continent: 'SA',
country: 'AR',
state: undefined,
state: null,
city: 'Buenos Aires',
region: 'South America',
normalizedCity: 'buenos aires',
Expand All @@ -161,7 +161,7 @@ describe('geoip service', () => {
longitude: -58.3772,
network: 'InterBS S.R.L. (BAEHOST)',
normalizedNetwork: 'interbs s.r.l. (baehost)',
isHosting: undefined,
isHosting: null,
});
});

Expand All @@ -173,7 +173,7 @@ describe('geoip service', () => {
expect(info).to.deep.equal({
continent: 'SA',
country: 'AR',
state: undefined,
state: null,
city: 'Buenos Aires',
region: 'South America',
normalizedCity: 'buenos aires',
Expand All @@ -182,7 +182,7 @@ describe('geoip service', () => {
longitude: -58.3772,
network: 'InterBS S.R.L. (BAEHOST)',
normalizedNetwork: 'interbs s.r.l. (baehost)',
isHosting: undefined,
isHosting: null,
});
});

Expand All @@ -207,7 +207,7 @@ describe('geoip service', () => {
expect(info).to.deep.equal({
continent: 'AF',
country: 'EG',
state: undefined,
state: null,
city: 'El-Rashda',
region: 'Northern Africa',
normalizedCity: 'el-rashda',
Expand All @@ -216,7 +216,7 @@ describe('geoip service', () => {
longitude: 26.487,
network: 'The Constant Company LLC',
normalizedNetwork: 'the constant company llc',
isHosting: undefined,
isHosting: null,
});
});

Expand All @@ -228,7 +228,7 @@ describe('geoip service', () => {
expect(info).to.deep.equal({
continent: 'EU',
country: 'DE',
state: undefined,
state: null,
city: 'Falkenstein',
region: 'Western Europe',
normalizedCity: 'falkenstein',
Expand All @@ -237,7 +237,7 @@ describe('geoip service', () => {
longitude: 12.371563,
network: 'Hetzner Online GmbH',
normalizedNetwork: 'hetzner online gmbh',
isHosting: undefined,
isHosting: null,
});
});

Expand All @@ -249,7 +249,7 @@ describe('geoip service', () => {
expect(info).to.deep.equal({
continent: 'EU',
country: 'DE',
state: undefined,
state: null,
city: 'Zwickau',
region: 'Western Europe',
normalizedCity: 'zwickau',
Expand All @@ -258,7 +258,7 @@ describe('geoip service', () => {
longitude: 12.371563,
network: 'Hetzner Online GmbH',
normalizedNetwork: 'hetzner online gmbh',
isHosting: undefined,
isHosting: null,
});
});

Expand Down Expand Up @@ -291,7 +291,7 @@ describe('geoip service', () => {
expect(info).to.deep.equal({
continent: 'SA',
country: 'AR',
state: undefined,
state: null,
city: 'Buenos Aires',
region: 'South America',
normalizedCity: 'buenos aires',
Expand All @@ -300,7 +300,7 @@ describe('geoip service', () => {
longitude: -58.3772,
network: 'InterBS S.R.L. (BAEHOST)',
normalizedNetwork: 'interbs s.r.l. (baehost)',
isHosting: undefined,
isHosting: null,
});
});

Expand Down Expand Up @@ -342,7 +342,7 @@ describe('geoip service', () => {
longitude: -96.8067,
network: 'The Constant Company LLC',
normalizedNetwork: 'the constant company llc',
isHosting: undefined,
isHosting: null,
});
});

Expand All @@ -364,7 +364,7 @@ describe('geoip service', () => {
longitude: -96.8067,
network: 'The Constant Company LLC',
normalizedNetwork: 'the constant company llc',
isHosting: undefined,
isHosting: null,
});
});

Expand All @@ -385,7 +385,7 @@ describe('geoip service', () => {
longitude: -96.8067,
network: 'The Constant Company LLC',
normalizedNetwork: 'the constant company llc',
isHosting: undefined,
isHosting: null,
});
});

Expand All @@ -406,7 +406,7 @@ describe('geoip service', () => {
longitude: -96.8067,
network: 'The Constant Company LLC',
normalizedNetwork: 'the constant company llc',
isHosting: undefined,
isHosting: null,
});
});

Expand All @@ -427,7 +427,7 @@ describe('geoip service', () => {
longitude: -77.039476,
network: 'Psychz Networks',
normalizedNetwork: 'psychz networks',
isHosting: undefined,
isHosting: null,
});

nockGeoIpProviders({ ip2location: 'empty', ipmap: 'empty', maxmind: 'empty', ipinfo: 'washington', fastly: 'empty' });
Expand Down
Loading

0 comments on commit 4dc3b00

Please sign in to comment.