import ipaddress from unittest import mock import pytest from pytest_mock import MockerFixture from location.connectors import ip2location from location.connectors.ip2location import IP2LocationRecord from location.test.faker import FakerTyped DEMO_VALUE = ( "This is DB5 demo BIN database. " "Please evaluate IP address from 0.0.0.0 to 99.255.255.255." ) IPV6_VALUE = "IPV6 ADDRESS MISSING IN IPV4 BIN" @pytest.fixture def database_mock(mocker: MockerFixture) -> mock.MagicMock: return mocker.patch("location.connectors.ip2location.database", autospec=True) @pytest.mark.anyio async def test_lookup(database_mock: mock.MagicMock, fake: FakerTyped) -> None: record = fake.ip2location_record() database_mock.get_all.return_value = record result = await ip2location.lookup(record.ip) assert result.resolved assert result == record @pytest.mark.anyio async def test_lookup_demo(database_mock: mock.MagicMock, fake: FakerTyped) -> None: ip = fake.ipv4() record = IP2LocationRecord() record.ip = IPV6_VALUE record.country_short = IPV6_VALUE record.country_long = IPV6_VALUE record.region = IPV6_VALUE record.city = IPV6_VALUE record.latitude = IPV6_VALUE record.longitude = IPV6_VALUE database_mock.get_all.return_value = record result = await ip2location.lookup(ipaddress.ip_address(ip)) assert result.ip == ip assert not result.resolved assert result.country_long == "-" assert result.country_short == "-" assert result.region == "-" assert result.city == "-" assert result.latitude == 0.0 assert result.longitude == 0.0 @pytest.mark.anyio async def test_lookup_ipv6(database_mock: mock.MagicMock, fake: FakerTyped) -> None: ip = fake.ipv6() record = IP2LocationRecord() record.ip = ip record.country_short = "-" record.country_long = "-" record.region = DEMO_VALUE record.city = DEMO_VALUE database_mock.get_all.return_value = record result = await ip2location.lookup(ipaddress.ip_address(record.ip)) assert result.ip == ip assert not result.resolved assert result.country_long == "-" assert result.country_short == "-" assert result.region == "-" assert result.city == "-" assert result.latitude == 0.0 assert result.longitude == 0.0 @pytest.mark.anyio async def test_lookup_private(database_mock: mock.MagicMock, fake: FakerTyped) -> None: ip = fake.ipv4_private() record = IP2LocationRecord() record.ip = ip record.country_short = "-" database_mock.get_all.return_value = record result = await ip2location.lookup(ipaddress.ip_address(record.ip)) assert result.ip == ip assert not result.resolved assert result.country_long == "-" assert result.country_short == "-" assert result.region == "-" assert result.city == "-" assert result.latitude == 0.0 assert result.longitude == 0.0 @pytest.mark.anyio async def test_lookup_batch(database_mock: mock.MagicMock, fake: FakerTyped) -> None: record_1 = fake.ip2location_record() record_2 = fake.ip2location_record() database_mock.get_all.side_effect = [record_1, record_2] result = await ip2location.lookup_batch([record_1.ip, record_2.ip]) assert result == { record_1.ip: record_1, record_2.ip: record_2, } @pytest.mark.anyio async def test_lookup_batch_only_resolved( database_mock: mock.MagicMock, fake: FakerTyped ) -> None: record_1 = fake.ip2location_record() record_2 = fake.ip2location_empty_record() database_mock.get_all.side_effect = [record_1, record_2] result = await ip2location.lookup_batch( [record_1.ip, record_2.ip], only_resolved=True ) assert result == { record_1.ip: record_1, }