Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,9 @@ class MembersImporter extends TableImporter {
name: name,
expertise: luck(30) ? faker.name.jobTitle() : undefined,
geolocation: JSON.stringify({
organization_name: faker.company.name(),
region: faker.address.state(),
accuracy: 50,
asn: parseInt(faker.random.numeric(4)),
organization: `${faker.random.alpha({count: 2, casing: 'upper'})}${faker.random.numeric(4)} ${faker.company.name()}`,
timezone: faker.address.timeZone(),
longitude: faker.address.longitude(),
country_code3: faker.address.countryCode('alpha-3'),
area_code: '0',
ip: faker.internet.ipv4(),
city: faker.address.cityName(),
country: faker.address.country(),
continent_code: 'EU',
country_code: faker.address.countryCode('alpha-2'),
latitude: faker.address.latitude()
region: faker.address.state()
}),
email_count: 0, // Depends on number of emails sent since created_at, the newsletter they're a part of and subscription status
email_opened_count: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ module.exports = class GeolocationService {

const geojsUrl = `https://get.geojs.io/v1/ip/geo/${encodeURIComponent(ipAddress)}.json`;
const response = await got(geojsUrl, gotOpts).json();
return response;
return {
country: response.country,
country_code: response.country_code,
region: response.region
};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const {assertExists} = require('../../../../../../utils/assertions');
const nock = require('nock');
const GeolocationService = require('../../../../../../../core/server/services/members/members-api/services/geolocation-service');

const RESPONSE = {
const GEOJS_RESPONSE = {
longitude: '-2.2417',
city: 'Kidderminster',
timezone: 'Europe/London',
Expand All @@ -21,6 +21,12 @@ const RESPONSE = {
country_code3: 'GBR'
};

const EXPECTED_RESULT = {
country: 'United Kingdom',
country_code: 'GB',
region: 'England'
};

const service = new GeolocationService();

describe('lib/geolocation', function () {
Expand All @@ -33,25 +39,25 @@ describe('lib/geolocation', function () {
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);
.reply(200, GEOJS_RESPONSE);

const result = await service.getGeolocationFromIP('188.39.113.90');

assert.equal(scope.isDone(), true, 'request was not made');
assertExists(result, 'nothing was returned');
assert.deepEqual(result, RESPONSE, 'result didn\'t match expected response');
assert.deepEqual(result, EXPECTED_RESULT, '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);
.reply(200, GEOJS_RESPONSE);

const result = await service.getGeolocationFromIP('2a01:4c8:43a:13c9:8d6:128e:1fd5:6aad');

assert.equal(scope.isDone(), true, 'request was not made');
assertExists(result, 'nothing was returned');
assert.deepEqual(result, RESPONSE, 'result didn\'t match expected response');
assert.deepEqual(result, EXPECTED_RESULT, 'result didn\'t match expected response');
});

it('handles non-IP addresses', async function () {
Expand Down
Loading