2020-02-27 13:29:36 +03:00
|
|
|
const nock = require('nock');
|
2021-01-18 16:55:40 +03:00
|
|
|
const GeolocationService = require('../../../lib/services/geolocation');
|
2020-02-27 13:29:36 +03:00
|
|
|
|
|
|
|
const RESPONSE = {
|
|
|
|
longitude: '-2.2417',
|
|
|
|
city: 'Kidderminster',
|
|
|
|
timezone: 'Europe/London',
|
|
|
|
accuracy: 200,
|
|
|
|
asn: 8468,
|
|
|
|
region: 'England',
|
|
|
|
organization_name: 'Entanet',
|
|
|
|
organization: 'AS8468 Entanet',
|
|
|
|
country_code: 'GB',
|
|
|
|
ip: '188.39.113.90',
|
|
|
|
latitude: '52.375',
|
|
|
|
area_code: '0',
|
|
|
|
continent_code: 'EU',
|
|
|
|
country: 'United Kingdom',
|
|
|
|
country_code3: 'GBR'
|
|
|
|
};
|
|
|
|
|
2021-01-18 16:55:40 +03:00
|
|
|
const service = new GeolocationService();
|
|
|
|
|
2020-02-27 13:29:36 +03:00
|
|
|
describe('lib/geolocation', function () {
|
|
|
|
describe('getGeolocationFromIP', function () {
|
|
|
|
afterEach(function () {
|
|
|
|
nock.abortPendingRequests();
|
|
|
|
nock.cleanAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fetches from geojs.io with IPv4 address', async function () {
|
|
|
|
const scope = nock('https://get.geojs.io')
|
|
|
|
.get('/v1/ip/geo/188.39.113.90.json')
|
|
|
|
.reply(200, RESPONSE);
|
|
|
|
|
2021-01-18 16:55:40 +03:00
|
|
|
const result = await service.getGeolocationFromIP('188.39.113.90');
|
2020-02-27 13:29:36 +03:00
|
|
|
|
|
|
|
scope.isDone().should.eql(true, 'request was not made');
|
|
|
|
should.exist(result, 'nothing was returned');
|
|
|
|
result.should.deepEqual(RESPONSE, 'result didn\'t match expected response');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fetches from geojs.io with IPv6 address', async function () {
|
|
|
|
const scope = nock('https://get.geojs.io')
|
|
|
|
.get('/v1/ip/geo/2a01%3A4c8%3A43a%3A13c9%3A8d6%3A128e%3A1fd5%3A6aad.json')
|
|
|
|
.reply(200, RESPONSE);
|
|
|
|
|
2021-01-18 16:55:40 +03:00
|
|
|
const result = await service.getGeolocationFromIP('2a01:4c8:43a:13c9:8d6:128e:1fd5:6aad');
|
2020-02-27 13:29:36 +03:00
|
|
|
|
|
|
|
scope.isDone().should.eql(true, 'request was not made');
|
|
|
|
should.exist(result, 'nothing was returned');
|
|
|
|
result.should.deepEqual(RESPONSE, 'result didn\'t match expected response');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles non-IP addresses', async function () {
|
|
|
|
let scope = nock('https://get.geojs.io').get('/v1/ip/geo/.json').reply(200, {test: true});
|
2021-01-18 16:55:40 +03:00
|
|
|
let result = await service.getGeolocationFromIP('');
|
2020-02-27 13:29:36 +03:00
|
|
|
scope.isDone().should.eql(false);
|
|
|
|
should.equal(undefined, result);
|
|
|
|
|
|
|
|
scope = nock('https://get.geojs.io').get('/v1/ip/geo/null.json').reply(200, {test: true});
|
2021-01-18 16:55:40 +03:00
|
|
|
result = await service.getGeolocationFromIP(null);
|
2020-02-27 13:29:36 +03:00
|
|
|
scope.isDone().should.eql(false);
|
|
|
|
should.equal(undefined, result);
|
|
|
|
|
|
|
|
scope = nock('https://get.geojs.io').get('/v1/ip/geo/undefined.json').reply(200, {test: true});
|
2021-01-18 16:55:40 +03:00
|
|
|
result = await service.getGeolocationFromIP(undefined);
|
2020-02-27 13:29:36 +03:00
|
|
|
scope.isDone().should.eql(false);
|
|
|
|
should.equal(undefined, result);
|
|
|
|
|
|
|
|
scope = nock('https://get.geojs.io').get('/v1/ip/geo/test.json').reply(200, {test: true});
|
2021-01-18 16:55:40 +03:00
|
|
|
result = await service.getGeolocationFromIP('test');
|
2020-02-27 13:29:36 +03:00
|
|
|
scope.isDone().should.eql(false);
|
|
|
|
should.equal(undefined, result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|