|
1 | 1 | import re |
| 2 | +from unittest import mock |
2 | 3 | from ipspot import get_private_ipv4 |
3 | 4 | from ipspot import get_public_ipv4, IPv4API |
4 | 5 |
|
|
7 | 8 | DATA_ITEMS = {'country_code', 'latitude', 'longitude', 'api', 'country', 'timezone', 'organization', 'region', 'ip', 'city'} |
8 | 9 |
|
9 | 10 |
|
10 | | -def test_private_ipv4(): |
| 11 | +def test_private_ipv4_success(): |
11 | 12 | result = get_private_ipv4() |
12 | 13 | assert result["status"] |
13 | 14 | assert IPV4_REGEX.match(result["data"]["ip"]) |
14 | 15 |
|
15 | 16 |
|
16 | | -def test_public_ipv4_auto(): |
| 17 | +def test_private_ipv4_error(): |
| 18 | + with mock.patch("socket.gethostbyname", side_effect=Exception("Test error")): |
| 19 | + result = get_private_ipv4() |
| 20 | + assert not result["status"] |
| 21 | + assert result["error"] == "Test error" |
| 22 | + |
| 23 | + |
| 24 | +def test_public_ipv4_auto_success(): |
17 | 25 | result = get_public_ipv4(api=IPv4API.AUTO, geo=True) |
18 | 26 | assert result["status"] |
19 | 27 | assert IPV4_REGEX.match(result["data"]["ip"]) |
20 | 28 | assert set(result["data"].keys()) == DATA_ITEMS |
21 | 29 |
|
22 | 30 |
|
23 | | -def test_public_ipv4_ipapi(): |
| 31 | +def test_public_ipv4_auto_timeout_error(): |
| 32 | + result = get_public_ipv4(api=IPv4API.AUTO, geo=True, timeout="5") |
| 33 | + assert not result["status"] |
| 34 | + |
| 35 | + |
| 36 | +def test_public_ipv4_auto_net_error(): |
| 37 | + with mock.patch("requests.get", side_effect=Exception("No Internet")): |
| 38 | + result = get_public_ipv4(api=IPv4API.AUTO) |
| 39 | + assert not result["status"] |
| 40 | + assert result["error"] == "All attempts failed." |
| 41 | + |
| 42 | + |
| 43 | +def test_public_ipv4_ipapi_success(): |
24 | 44 | result = get_public_ipv4(api=IPv4API.IPAPI, geo=True) |
25 | 45 | assert result["status"] |
26 | 46 | assert IPV4_REGEX.match(result["data"]["ip"]) |
27 | 47 | assert set(result["data"].keys()) == DATA_ITEMS |
28 | 48 | assert result["data"]["api"] == "ip-api.com" |
29 | 49 |
|
30 | 50 |
|
31 | | -def test_public_ipv4_ipinfo(): |
| 51 | +def test_public_ipv4_ipapi_timeout_error(): |
| 52 | + result = get_public_ipv4(api=IPv4API.IPAPI, geo=True, timeout="5") |
| 53 | + assert not result["status"] |
| 54 | + |
| 55 | + |
| 56 | +def test_public_ipv4_ipapi_net_error(): |
| 57 | + with mock.patch("requests.get", side_effect=Exception("No Internet")): |
| 58 | + result = get_public_ipv4(api=IPv4API.IPAPI) |
| 59 | + assert not result["status"] |
| 60 | + assert result["error"] == "No Internet" |
| 61 | + |
| 62 | + |
| 63 | +def test_public_ipv4_ipinfo_success(): |
32 | 64 | result = get_public_ipv4(api=IPv4API.IPINFO, geo=True) |
33 | 65 | assert result["status"] |
34 | 66 | assert IPV4_REGEX.match(result["data"]["ip"]) |
35 | 67 | assert set(result["data"].keys()) == DATA_ITEMS |
36 | 68 | assert result["data"]["api"] == "ipinfo.io" |
37 | 69 |
|
38 | 70 |
|
39 | | -def test_public_ipv4_ipsb(): |
| 71 | +def test_public_ipv4_ipinfo_timeout_error(): |
| 72 | + result = get_public_ipv4(api=IPv4API.IPINFO, geo=True, timeout="5") |
| 73 | + assert not result["status"] |
| 74 | + |
| 75 | + |
| 76 | +def test_public_ipv4_ipinfo_net_error(): |
| 77 | + with mock.patch("requests.get", side_effect=Exception("No Internet")): |
| 78 | + result = get_public_ipv4(api=IPv4API.IPINFO) |
| 79 | + assert not result["status"] |
| 80 | + assert result["error"] == "No Internet" |
| 81 | + |
| 82 | + |
| 83 | +def test_public_ipv4_ipsb_success(): |
40 | 84 | result = get_public_ipv4(api=IPv4API.IPSB, geo=True) |
41 | 85 | assert result["status"] |
42 | 86 | assert IPV4_REGEX.match(result["data"]["ip"]) |
43 | 87 | assert set(result["data"].keys()) == DATA_ITEMS |
44 | 88 | assert result["data"]["api"] == "ip.sb" |
45 | 89 |
|
| 90 | + |
| 91 | +def test_public_ipv4_ipsb_timeout_error(): |
| 92 | + result = get_public_ipv4(api=IPv4API.IPSB, geo=True, timeout="5") |
| 93 | + assert not result["status"] |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +def test_public_ipv4_ipsb_net_error(): |
| 98 | + with mock.patch("requests.get", side_effect=Exception("No Internet")): |
| 99 | + result = get_public_ipv4(api=IPv4API.IPSB) |
| 100 | + assert not result["status"] |
| 101 | + assert result["error"] == "No Internet" |
| 102 | + |
| 103 | + |
| 104 | +def test_public_ipv4_api_error(): |
| 105 | + result = get_public_ipv4(api="api1", geo=True) |
| 106 | + assert not result["status"] |
| 107 | + assert result["error"] == "Unsupported API: api1" |
| 108 | + |
0 commit comments