authentik.events.tests.test_mmdb
Test MMDB path resolution
1"""Test MMDB path resolution""" 2 3from pathlib import Path 4from shutil import copyfile 5from tempfile import TemporaryDirectory 6 7from django.test import TestCase 8 9from authentik.events.context_processors.geoip import GeoIPContextProcessor 10from authentik.lib.config import CONFIG 11 12TEST_DB = Path(__file__).parents[3] / "tests" / "geoip" / "GeoLite2-City-Test.mmdb" 13 14 15class TestMMDBPath(TestCase): 16 """Test MMDB path resolution""" 17 18 def setUp(self) -> None: 19 self.dir = TemporaryDirectory() 20 self.default = str(Path(self.dir.name) / "dbip-city-lite.mmdb") 21 self.legacy = str(Path(self.dir.name) / "GeoLite2-City.mmdb") 22 23 def tearDown(self) -> None: 24 self.dir.cleanup() 25 26 def processor(self) -> GeoIPContextProcessor: 27 processor = GeoIPContextProcessor() 28 processor.default_path = self.default 29 processor.legacy_path = self.legacy 30 processor.load() 31 return processor 32 33 def test_default(self): 34 """Default file is loaded when present""" 35 copyfile(TEST_DB, self.default) 36 copyfile(TEST_DB, self.legacy) 37 with CONFIG.patch("events.context_processors.geoip", self.default): 38 processor = self.processor() 39 self.assertEqual(processor.resolved_path(), self.default) 40 self.assertTrue(processor.configured()) 41 42 def test_legacy_fallback(self): 43 """Legacy file is loaded when the default file is missing""" 44 copyfile(TEST_DB, self.legacy) 45 with CONFIG.patch("events.context_processors.geoip", self.default): 46 processor = self.processor() 47 self.assertEqual(processor.resolved_path(), self.legacy) 48 self.assertTrue(processor.configured()) 49 self.assertEqual(processor.city_dict("2.125.160.216")["city"], "Boxford") 50 51 def test_no_fallback_custom_path(self): 52 """Legacy file is not used when a non-default path is configured""" 53 copyfile(TEST_DB, self.legacy) 54 custom = str(Path(self.dir.name) / "custom.mmdb") 55 with CONFIG.patch("events.context_processors.geoip", custom): 56 processor = self.processor() 57 self.assertEqual(processor.resolved_path(), custom) 58 self.assertFalse(processor.configured())
TEST_DB =
PosixPath('/home/runner/work/authentik/authentik/tests/geoip/GeoLite2-City-Test.mmdb')
class
TestMMDBPath(django.test.testcases.TestCase):
16class TestMMDBPath(TestCase): 17 """Test MMDB path resolution""" 18 19 def setUp(self) -> None: 20 self.dir = TemporaryDirectory() 21 self.default = str(Path(self.dir.name) / "dbip-city-lite.mmdb") 22 self.legacy = str(Path(self.dir.name) / "GeoLite2-City.mmdb") 23 24 def tearDown(self) -> None: 25 self.dir.cleanup() 26 27 def processor(self) -> GeoIPContextProcessor: 28 processor = GeoIPContextProcessor() 29 processor.default_path = self.default 30 processor.legacy_path = self.legacy 31 processor.load() 32 return processor 33 34 def test_default(self): 35 """Default file is loaded when present""" 36 copyfile(TEST_DB, self.default) 37 copyfile(TEST_DB, self.legacy) 38 with CONFIG.patch("events.context_processors.geoip", self.default): 39 processor = self.processor() 40 self.assertEqual(processor.resolved_path(), self.default) 41 self.assertTrue(processor.configured()) 42 43 def test_legacy_fallback(self): 44 """Legacy file is loaded when the default file is missing""" 45 copyfile(TEST_DB, self.legacy) 46 with CONFIG.patch("events.context_processors.geoip", self.default): 47 processor = self.processor() 48 self.assertEqual(processor.resolved_path(), self.legacy) 49 self.assertTrue(processor.configured()) 50 self.assertEqual(processor.city_dict("2.125.160.216")["city"], "Boxford") 51 52 def test_no_fallback_custom_path(self): 53 """Legacy file is not used when a non-default path is configured""" 54 copyfile(TEST_DB, self.legacy) 55 custom = str(Path(self.dir.name) / "custom.mmdb") 56 with CONFIG.patch("events.context_processors.geoip", custom): 57 processor = self.processor() 58 self.assertEqual(processor.resolved_path(), custom) 59 self.assertFalse(processor.configured())
Test MMDB path resolution
def
setUp(self) -> None:
19 def setUp(self) -> None: 20 self.dir = TemporaryDirectory() 21 self.default = str(Path(self.dir.name) / "dbip-city-lite.mmdb") 22 self.legacy = str(Path(self.dir.name) / "GeoLite2-City.mmdb")
Hook method for setting up the test fixture before exercising it.
def
test_default(self):
34 def test_default(self): 35 """Default file is loaded when present""" 36 copyfile(TEST_DB, self.default) 37 copyfile(TEST_DB, self.legacy) 38 with CONFIG.patch("events.context_processors.geoip", self.default): 39 processor = self.processor() 40 self.assertEqual(processor.resolved_path(), self.default) 41 self.assertTrue(processor.configured())
Default file is loaded when present
def
test_legacy_fallback(self):
43 def test_legacy_fallback(self): 44 """Legacy file is loaded when the default file is missing""" 45 copyfile(TEST_DB, self.legacy) 46 with CONFIG.patch("events.context_processors.geoip", self.default): 47 processor = self.processor() 48 self.assertEqual(processor.resolved_path(), self.legacy) 49 self.assertTrue(processor.configured()) 50 self.assertEqual(processor.city_dict("2.125.160.216")["city"], "Boxford")
Legacy file is loaded when the default file is missing
def
test_no_fallback_custom_path(self):
52 def test_no_fallback_custom_path(self): 53 """Legacy file is not used when a non-default path is configured""" 54 copyfile(TEST_DB, self.legacy) 55 custom = str(Path(self.dir.name) / "custom.mmdb") 56 with CONFIG.patch("events.context_processors.geoip", custom): 57 processor = self.processor() 58 self.assertEqual(processor.resolved_path(), custom) 59 self.assertFalse(processor.configured())
Legacy file is not used when a non-default path is configured