-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathhandler_plus_test.py
More file actions
212 lines (170 loc) · 5.99 KB
/
handler_plus_test.py
File metadata and controls
212 lines (170 loc) · 5.99 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import os
import pytest
from ipinfo import handler_utils
from ipinfo.cache.default import DefaultCache
from ipinfo.details import Details
from ipinfo.handler_plus import HandlerPlus
def test_init():
token = "mytesttoken"
handler = HandlerPlus(token)
assert handler.access_token == token
assert isinstance(handler.cache, DefaultCache)
assert "US" in handler.countries
def test_headers():
token = "mytesttoken"
handler = HandlerPlus(token, headers={"custom_field": "yes"})
headers = handler_utils.get_headers(token, handler.headers)
assert "user-agent" in headers
assert "accept" in headers
assert "authorization" in headers
assert "custom_field" in headers
@pytest.mark.skipif(
"IPINFO_TOKEN" not in os.environ,
reason="Can't call Plus API without token",
)
def test_get_details():
"""Test basic Plus API lookup"""
token = os.environ.get("IPINFO_TOKEN", "")
handler = HandlerPlus(token)
details = handler.getDetails("8.8.8.8")
# Should return Details object
assert isinstance(details, Details)
assert details.ip == "8.8.8.8"
assert hasattr(details, "hostname")
# Check nested geo object with all fields
assert hasattr(details, "geo")
assert isinstance(details.geo, dict)
assert "city" in details.geo
assert "region" in details.geo
assert "region_code" in details.geo
assert "country" in details.geo
assert "country_code" in details.geo
assert "continent" in details.geo
assert "continent_code" in details.geo
assert "latitude" in details.geo
assert "longitude" in details.geo
assert "timezone" in details.geo
assert "postal_code" in details.geo
assert "dma_code" in details.geo
assert "geoname_id" in details.geo
assert "radius" in details.geo
# Check nested as object with all fields
assert "as" in details.all
as_obj = details.all["as"]
assert isinstance(as_obj, dict)
assert "asn" in as_obj
assert "name" in as_obj
assert "domain" in as_obj
assert "type" in as_obj
assert "last_changed" in as_obj
# Check mobile and anonymous objects
assert hasattr(details, "mobile")
assert isinstance(details.mobile, dict)
assert hasattr(details, "anonymous")
assert isinstance(details.anonymous, dict)
assert "is_proxy" in details.anonymous
assert "is_relay" in details.anonymous
assert "is_tor" in details.anonymous
assert "is_vpn" in details.anonymous
# Check all network/type flags
assert hasattr(details, "is_anonymous")
assert hasattr(details, "is_anycast")
assert hasattr(details, "is_hosting")
assert hasattr(details, "is_mobile")
assert hasattr(details, "is_satellite")
# Check geo formatting was applied
assert "country_name" in details.geo
assert "isEU" in details.geo
assert "country_flag_url" in details.geo
#############
# BOGON TESTS
#############
@pytest.mark.skipif(
"IPINFO_TOKEN" not in os.environ,
reason="Can't call Plus API without token",
)
def test_bogon_details():
token = os.environ.get("IPINFO_TOKEN", "")
handler = HandlerPlus(token)
details = handler.getDetails("127.0.0.1")
assert isinstance(details, Details)
assert details.all == {"bogon": True, "ip": "127.0.0.1"}
#####################
# BATCH TESTS
#####################
@pytest.mark.skipif(
"IPINFO_TOKEN" not in os.environ,
reason="Can't call Plus API without token",
)
def test_batch_ips():
"""Test batch request with IPs"""
token = os.environ.get("IPINFO_TOKEN", "")
handler = HandlerPlus(token)
results = handler.getBatchDetails(["8.8.8.8", "1.1.1.1"])
assert len(results) == 2
assert "8.8.8.8" in results
assert "1.1.1.1" in results
# Both should be Details objects
assert isinstance(results["8.8.8.8"], Details)
assert isinstance(results["1.1.1.1"], Details)
# Check structure - Plus API returns nested geo and as objects
assert hasattr(results["8.8.8.8"], "geo")
assert "as" in results["8.8.8.8"].all
@pytest.mark.skipif(
"IPINFO_TOKEN" not in os.environ,
reason="Can't call Plus API without token",
)
def test_batch_with_bogon():
"""Test batch including bogon IPs"""
token = os.environ.get("IPINFO_TOKEN", "")
handler = HandlerPlus(token)
results = handler.getBatchDetails(
[
"8.8.8.8",
"127.0.0.1", # Bogon
"1.1.1.1",
]
)
assert len(results) == 3
# Normal IPs should be Details
assert isinstance(results["8.8.8.8"], Details)
assert isinstance(results["1.1.1.1"], Details)
# Bogon should also be Details with bogon flag
assert isinstance(results["127.0.0.1"], Details)
assert results["127.0.0.1"].bogon == True
#####################
# CACHING TESTS
#####################
@pytest.mark.skipif(
"IPINFO_TOKEN" not in os.environ,
reason="Can't call Plus API without token",
)
def test_caching():
"""Test that results are properly cached"""
token = os.environ.get("IPINFO_TOKEN", "")
handler = HandlerPlus(token)
# First request - should hit API
details1 = handler.getDetails("8.8.8.8")
assert isinstance(details1, Details)
# Second request - should come from cache
details2 = handler.getDetails("8.8.8.8")
assert isinstance(details2, Details)
assert details2.ip == details1.ip
# Verify cache key exists
cache_key_val = handler_utils.cache_key("8.8.8.8")
assert cache_key_val in handler.cache
@pytest.mark.skipif(
"IPINFO_TOKEN" not in os.environ,
reason="Can't call Plus API without token",
)
def test_batch_caching():
"""Test that batch results are properly cached"""
token = os.environ.get("IPINFO_TOKEN", "")
handler = HandlerPlus(token)
# First batch request
results1 = handler.getBatchDetails(["8.8.8.8", "1.1.1.1"])
assert len(results1) == 2
# Second batch with same IPs (should come from cache)
results2 = handler.getBatchDetails(["8.8.8.8", "1.1.1.1"])
assert len(results2) == 2
assert results2["8.8.8.8"].ip == results1["8.8.8.8"].ip