Skip to content

Commit

Permalink
UPDATE: test and logic for checking and applying geodata
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremiahUy committed Nov 4, 2022
1 parent c102e0e commit bff9e8a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/filters/add-geo-data.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const geoLookup = require('geoip-lite').lookup;
module.exports = function(inputEvents, ip) {
const outputEvents = [];
const geoData = geoLookup(ip);
if(ip){
const geoData = geoLookup(ip);
inputEvents.forEach(event => {
const cloneEvent = Object.assign({}, event);
cloneEvent.user_agent = null;
Expand All @@ -19,11 +19,11 @@ module.exports = function(inputEvents, ip) {
const cloneEvent = Object.assign({}, event);
cloneEvent.user_agent = null;
cloneEvent.os_version = null;
if (geoData && geoData.country) cloneEvent.country = null;
if (geoData && geoData.region) cloneEvent.region = null;
if (geoData && geoData.city) cloneEvent.city = null;
if (geoData && geoData.ll) cloneEvent.location_lat = null;
if (geoData && geoData.ll) cloneEvent.location_lng = null;
cloneEvent.country = null;
cloneEvent.region = null;
cloneEvent.city = null;
cloneEvent.location_lat = null;
cloneEvent.location_lng = null;
outputEvents.push(cloneEvent);
});
}
Expand Down
15 changes: 15 additions & 0 deletions src/filters/add-geo-data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,19 @@ describe('add-tracking-options', function() {
assert.notStrictEqual(event.os_version, events[0].os_version);
});
});

it('should not add tracking options', function() {
let events = [generateTestEvent(), generateTestEvent()];
const result = addGeoData(events, undefined);
assert.strictEqual(result.length, 2);
result.forEach(event => {
assert.strictEqual(event.city, null);
assert.strictEqual(event.country, null);
assert.strictEqual(event.location_lat, null);
assert.strictEqual(event.location_lng, null);
assert.strictEqual(event.region, null);
assert.notStrictEqual(event.user_agent, events[0].user_agent);
assert.notStrictEqual(event.os_version, events[0].user_agent);
});
});
});
2 changes: 1 addition & 1 deletion src/routes/collect-auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const customHandler = function (request, reply, ingresses) {
const eventsWithProxyData = addProxyData(eventsWithClusterData, process.env.NAIS_APP_IMAGE);
let eventsWithGeoData;
if(request.api_properties && request.api_properties.ip_address) {
eventsWithGeoData = addGeoData(eventsWithProxyData, undefined);
eventsWithGeoData = addGeoData(eventsWithProxyData, null);
} else {
eventsWithGeoData = addGeoData(eventsWithProxyData, request.ip);
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const handler = function(request, reply) {
const eventsWithProxyData = addProxyData(inputEvents, process.env.NAIS_APP_IMAGE);
let eventsWithGeoData;
if(request.api_properties && !request.api_properties.ip_address) {
eventsWithGeoData = addGeoData(eventsWithProxyData, undefined);
eventsWithGeoData = addGeoData(eventsWithProxyData, null);
} else {
eventsWithGeoData = addGeoData(eventsWithProxyData, request.ip);
}
Expand Down

0 comments on commit bff9e8a

Please sign in to comment.