-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.test.ts
141 lines (132 loc) · 5.45 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { City, Reader } from '@maxmind/geoip2-node'
import { Plugin, PluginMeta } from '@posthog/plugin-scaffold'
// @ts-ignore
import { createPageview, resetMeta } from '@posthog/plugin-scaffold/test/utils'
import { join } from 'path'
import * as index from '.'
const { processEvent } = index as Required<Plugin>
const DEFAULT_MMDB_FILE_NAME = 'GeoLite2-City-Test.mmdb'
async function resetMetaWithMmdb(
transformResult = (res: City) => res as Record<string, any>,
file = DEFAULT_MMDB_FILE_NAME
): Promise<PluginMeta> {
const mmdb = await Reader.open(join(__dirname, file))
return resetMeta({
geoip: {
locate: (ipAddress: string) => {
const res = mmdb.city(ipAddress)
return transformResult(res)
},
},
}) as PluginMeta
}
test('event is enriched with IP location', async () => {
const event = await processEvent({ ...createPageview(), ip: '89.160.20.129' }, await resetMetaWithMmdb())
expect(event!.properties).toEqual(
expect.objectContaining({
$geoip_city_name: 'Linköping',
$geoip_country_name: 'Sweden',
$geoip_country_code: 'SE',
$geoip_continent_name: 'Europe',
$geoip_continent_code: 'EU',
$geoip_latitude: 58.4167,
$geoip_longitude: 15.6167,
$geoip_accuracy_radius: 76,
$geoip_time_zone: 'Europe/Stockholm',
$geoip_subdivision_1_code: 'E',
$geoip_subdivision_1_name: 'Östergötland County',
})
)
})
test('person is enriched with IP location', async () => {
const event = await processEvent({ ...createPageview(), ip: '89.160.20.129' }, await resetMetaWithMmdb())
expect(event!.properties!.$set).toEqual(
expect.objectContaining({
$geoip_city_name: 'Linköping',
$geoip_country_name: 'Sweden',
$geoip_country_code: 'SE',
$geoip_continent_name: 'Europe',
$geoip_continent_code: 'EU',
$geoip_latitude: 58.4167,
$geoip_longitude: 15.6167,
$geoip_time_zone: 'Europe/Stockholm',
$geoip_subdivision_1_code: 'E',
$geoip_subdivision_1_name: 'Östergötland County',
})
)
expect(event!.properties!.$set_once).toEqual(
expect.objectContaining({
$initial_geoip_city_name: 'Linköping',
$initial_geoip_country_name: 'Sweden',
$initial_geoip_country_code: 'SE',
$initial_geoip_continent_name: 'Europe',
$initial_geoip_continent_code: 'EU',
$initial_geoip_latitude: 58.4167,
$initial_geoip_longitude: 15.6167,
$initial_geoip_time_zone: 'Europe/Stockholm',
$initial_geoip_subdivision_1_code: 'E',
$initial_geoip_subdivision_1_name: 'Östergötland County',
})
)
})
test('person props default to null if no values present', async () => {
const removeCityNameFromLookupResult = (res: City) => {
const { city, ...remainingResult } = res
return remainingResult
}
const event = await processEvent(
{ ...createPageview(), ip: '89.160.20.129' },
await resetMetaWithMmdb(removeCityNameFromLookupResult)
)
expect(event!.properties!.$set).toMatchInlineSnapshot(`
Object {
"$geoip_accuracy_radius": 76,
"$geoip_city_confidence": null,
"$geoip_city_name": null,
"$geoip_continent_code": "EU",
"$geoip_continent_name": "Europe",
"$geoip_country_code": "SE",
"$geoip_country_name": "Sweden",
"$geoip_latitude": 58.4167,
"$geoip_longitude": 15.6167,
"$geoip_postal_code": null,
"$geoip_subdivision_1_code": "E",
"$geoip_subdivision_1_name": "Östergötland County",
"$geoip_subdivision_2_code": null,
"$geoip_subdivision_2_name": null,
"$geoip_time_zone": "Europe/Stockholm",
}
`)
expect(event!.properties!.$set_once).toMatchInlineSnapshot(`
Object {
"$initial_geoip_accuracy_radius": 76,
"$initial_geoip_city_confidence": null,
"$initial_geoip_city_name": null,
"$initial_geoip_continent_code": "EU",
"$initial_geoip_continent_name": "Europe",
"$initial_geoip_country_code": "SE",
"$initial_geoip_country_name": "Sweden",
"$initial_geoip_latitude": 58.4167,
"$initial_geoip_longitude": 15.6167,
"$initial_geoip_postal_code": null,
"$initial_geoip_subdivision_1_code": "E",
"$initial_geoip_subdivision_1_name": "Östergötland County",
"$initial_geoip_subdivision_2_code": null,
"$initial_geoip_subdivision_2_name": null,
"$initial_geoip_time_zone": "Europe/Stockholm",
}
`)
})
test('error is thrown if meta.geoip is not provided', async () => {
expect.assertions(1)
await expect(
async () => await processEvent({ ...createPageview(), ip: '89.160.20.129' }, resetMeta())
).rejects.toEqual(
new Error('This PostHog version does not have GeoIP capabilities! Upgrade to PostHog 1.24.0 or later')
)
})
test('event is skipped using $geoip_disable', async () => {
const testEvent = { ...createPageview(), ip: '89.160.20.129', properties: { $geoip_disable: true } }
const processedEvent = await processEvent(JSON.parse(JSON.stringify(testEvent)), await resetMetaWithMmdb())
expect(testEvent).toEqual(processedEvent)
})