authentik.events.tests.test_enrich_geoip

Test GeoIP Wrapper

 1"""Test GeoIP Wrapper"""
 2
 3from django.test import TestCase
 4
 5from authentik.events.context_processors.base import get_context_processors
 6from authentik.events.context_processors.geoip import GeoIPContextProcessor
 7from authentik.events.models import Event, EventAction
 8
 9
10class TestGeoIP(TestCase):
11    """Test GeoIP Wrapper"""
12
13    def setUp(self) -> None:
14        self.reader = GeoIPContextProcessor()
15
16    def test_simple(self):
17        """Test simple city wrapper"""
18        # IPs from https://github.com/maxmind/MaxMind-DB/blob/main/source-data/GeoLite2-City-Test.json
19        self.assertEqual(
20            self.reader.city_dict("2.125.160.216"),
21            {
22                "city": "Boxford",
23                "continent": "EU",
24                "country": "GB",
25                "lat": 51.75,
26                "long": -1.25,
27            },
28        )
29
30    def test_special_chars(self):
31        """Test city name with special characters"""
32        # IPs from https://github.com/maxmind/MaxMind-DB/blob/main/source-data/GeoLite2-City-Test.json
33        event = Event.new(EventAction.LOGIN)
34        event.client_ip = "89.160.20.112"
35        for processor in get_context_processors():
36            processor.enrich_event(event)
37        event.save()
class TestGeoIP(django.test.testcases.TestCase):
11class TestGeoIP(TestCase):
12    """Test GeoIP Wrapper"""
13
14    def setUp(self) -> None:
15        self.reader = GeoIPContextProcessor()
16
17    def test_simple(self):
18        """Test simple city wrapper"""
19        # IPs from https://github.com/maxmind/MaxMind-DB/blob/main/source-data/GeoLite2-City-Test.json
20        self.assertEqual(
21            self.reader.city_dict("2.125.160.216"),
22            {
23                "city": "Boxford",
24                "continent": "EU",
25                "country": "GB",
26                "lat": 51.75,
27                "long": -1.25,
28            },
29        )
30
31    def test_special_chars(self):
32        """Test city name with special characters"""
33        # IPs from https://github.com/maxmind/MaxMind-DB/blob/main/source-data/GeoLite2-City-Test.json
34        event = Event.new(EventAction.LOGIN)
35        event.client_ip = "89.160.20.112"
36        for processor in get_context_processors():
37            processor.enrich_event(event)
38        event.save()

Test GeoIP Wrapper

def setUp(self) -> None:
14    def setUp(self) -> None:
15        self.reader = GeoIPContextProcessor()

Hook method for setting up the test fixture before exercising it.

def test_simple(self):
17    def test_simple(self):
18        """Test simple city wrapper"""
19        # IPs from https://github.com/maxmind/MaxMind-DB/blob/main/source-data/GeoLite2-City-Test.json
20        self.assertEqual(
21            self.reader.city_dict("2.125.160.216"),
22            {
23                "city": "Boxford",
24                "continent": "EU",
25                "country": "GB",
26                "lat": 51.75,
27                "long": -1.25,
28            },
29        )

Test simple city wrapper

def test_special_chars(self):
31    def test_special_chars(self):
32        """Test city name with special characters"""
33        # IPs from https://github.com/maxmind/MaxMind-DB/blob/main/source-data/GeoLite2-City-Test.json
34        event = Event.new(EventAction.LOGIN)
35        event.client_ip = "89.160.20.112"
36        for processor in get_context_processors():
37            processor.enrich_event(event)
38        event.save()

Test city name with special characters