-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_test.py
56 lines (46 loc) · 1.46 KB
/
api_test.py
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
"""
Tests for the MaxMind IP lookup stuff.
"""
import ipaddress
import faker
import pytest
from maxmind.api import ip_to_country_code
from maxmind.factories import NetBlockIPv4Factory, NetBlockIPv6Factory
fake = faker.Factory.create()
@pytest.mark.django_db()
@pytest.mark.parametrize(
"v4,in_block", # noqa: PT006
[
[True, True], # noqa: PT007
[True, False], # noqa: PT007
[False, True], # noqa: PT007
[False, False], # noqa: PT007
],
)
def test_ipv4_lookup(v4, in_block):
"""
Test to see if a given IPv4 address can be looked up. If the IP is in a
netblock we've seen, then you should get a country code back. If not, you
should get None.
"""
factory_block = NetBlockIPv4Factory.create() if v4 else NetBlockIPv6Factory.create()
netblock = (
ipaddress.IPv4Network(factory_block.network)
if v4
else ipaddress.IPv6Network(factory_block.network)
)
while True:
test_address = ipaddress.ip_address(fake.ipv4() if v4 else fake.ipv6())
if (
in_block
and (
int(test_address) > int(netblock[0])
and int(test_address) < int(netblock[-1])
)
) or (
int(test_address) < int(netblock[0])
or int(test_address) > int(netblock[-1])
):
break
result = ip_to_country_code(str(test_address))
assert (result is not None and in_block) or result is None