Skip to content

Commit 8f8ec7b

Browse files
PriOliveiramax-ipinfo
authored andcommitted
add tests for getDetails error status code
1 parent 0a7222e commit 8f8ec7b

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

tests/handler_async_test.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1+
import json
12
import os
23

34
from ipinfo.cache.default import DefaultCache
45
from ipinfo.details import Details
56
from ipinfo.handler_async import AsyncHandler
67
from ipinfo import handler_utils
8+
from ipinfo.error import APIError
9+
from ipinfo.exceptions import RequestQuotaExceededError
710
import ipinfo
811
import pytest
12+
import aiohttp
13+
14+
15+
class MockResponse:
16+
def __init__(self, text, status, headers):
17+
self._text = text
18+
self.status = status
19+
self.headers = headers
20+
21+
def text(self):
22+
return self._text
23+
24+
async def json(self):
25+
return json.loads(self._text)
26+
27+
async def __aexit__(self, exc_type, exc_val, exc_tb):
28+
pass
29+
30+
async def __aenter__(self):
31+
return self
32+
33+
async def release(self):
34+
pass
935

1036

1137
@pytest.mark.asyncio
@@ -103,6 +129,39 @@ async def test_get_details():
103129

104130
await handler.deinit()
105131

132+
@pytest.mark.parametrize(
133+
("mock_resp_status_code", "mock_resp_headers", "mock_resp_error_msg", "expected_error_json"),
134+
[
135+
pytest.param(503, {"Content-Type": "text/plain"}, "Service Unavailable", {"error": "Service Unavailable"}, id="5xx_not_json"),
136+
pytest.param(403, {"Content-Type": "application/json"}, '{"message": "missing token"}', {"message": "missing token"}, id="4xx_json"),
137+
pytest.param(400, {"Content-Type": "application/json"}, '{"message": "missing field"}', {"message": "missing field"}, id="400"),
138+
]
139+
)
140+
@pytest.mark.asyncio
141+
async def test_get_details_error(monkeypatch, mock_resp_status_code, mock_resp_headers, mock_resp_error_msg, expected_error_json):
142+
async def mock_get(*args, **kwargs):
143+
response = MockResponse(status=mock_resp_status_code, text=mock_resp_error_msg, headers=mock_resp_headers)
144+
return response
145+
146+
monkeypatch.setattr(aiohttp.ClientSession, 'get', lambda *args, **kwargs: aiohttp.client._RequestContextManager(mock_get()))
147+
token = os.environ.get("IPINFO_TOKEN", "")
148+
handler = AsyncHandler(token)
149+
with pytest.raises(APIError) as exc_info:
150+
await handler.getDetails("8.8.8.8")
151+
assert exc_info.value.error_code == mock_resp_status_code
152+
assert exc_info.value.error_json == expected_error_json
153+
154+
@pytest.mark.asyncio
155+
async def test_get_details_quota_error(monkeypatch):
156+
async def mock_get(*args, **kwargs):
157+
response = MockResponse(status=429, text="Quota exceeded", headers={})
158+
return response
159+
160+
monkeypatch.setattr(aiohttp.ClientSession, 'get', lambda *args, **kwargs: aiohttp.client._RequestContextManager(mock_get()))
161+
token = os.environ.get("IPINFO_TOKEN", "")
162+
handler = AsyncHandler(token)
163+
with pytest.raises(RequestQuotaExceededError):
164+
await handler.getDetails("8.8.8.8")
106165

107166
#############
108167
# BATCH TESTS

tests/handler_test.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
from ipaddress import IPv4Address
2-
import json
31
import os
42

53
from ipinfo.cache.default import DefaultCache
64
from ipinfo.details import Details
75
from ipinfo.handler import Handler
86
from ipinfo import handler_utils
7+
from ipinfo.error import APIError
8+
from ipinfo.exceptions import RequestQuotaExceededError
99
import ipinfo
1010
import pytest
11+
import requests
1112

1213

1314
def test_init():
@@ -98,6 +99,43 @@ def test_get_details():
9899
assert "total" in domains
99100
assert len(domains["domains"]) == 5
100101

102+
@pytest.mark.parametrize(
103+
("mock_resp_status_code", "mock_resp_headers", "mock_resp_error_msg", "expected_error_json"),
104+
[
105+
pytest.param(503, {"Content-Type": "text/plain"}, b"Service Unavailable", {"error": "Service Unavailable"}, id="5xx_not_json"),
106+
pytest.param(403, {"Content-Type": "application/json"}, b'{"message": "missing token"}', {"message": "missing token"}, id="4xx_json"),
107+
pytest.param(400, {"Content-Type": "application/json"}, b'{"message": "missing field"}', {"message": "missing field"}, id="400"),
108+
]
109+
)
110+
def test_get_details_error(monkeypatch, mock_resp_status_code, mock_resp_headers, mock_resp_error_msg, expected_error_json):
111+
def mock_get(*args, **kwargs):
112+
response = requests.Response()
113+
response.status_code = mock_resp_status_code
114+
response.headers = mock_resp_headers
115+
response._content = mock_resp_error_msg
116+
return response
117+
118+
monkeypatch.setattr(requests, 'get', mock_get)
119+
token = os.environ.get("IPINFO_TOKEN", "")
120+
handler = Handler(token)
121+
122+
with pytest.raises(APIError) as exc_info:
123+
handler.getDetails("8.8.8.8")
124+
assert exc_info.value.error_code == mock_resp_status_code
125+
assert exc_info.value.error_json == expected_error_json
126+
127+
def test_get_details_quota_error(monkeypatch):
128+
def mock_get(*args, **kwargs):
129+
response = requests.Response()
130+
response.status_code = 429
131+
return response
132+
133+
monkeypatch.setattr(requests, 'get', mock_get)
134+
token = os.environ.get("IPINFO_TOKEN", "")
135+
handler = Handler(token)
136+
137+
with pytest.raises(RequestQuotaExceededError):
138+
handler.getDetails("8.8.8.8")
101139

102140
#############
103141
# BATCH TESTS

0 commit comments

Comments
 (0)