|
| 1 | +import json |
1 | 2 | import os
|
2 | 3 |
|
3 | 4 | from ipinfo.cache.default import DefaultCache
|
4 | 5 | from ipinfo.details import Details
|
5 | 6 | from ipinfo.handler_async import AsyncHandler
|
6 | 7 | from ipinfo import handler_utils
|
| 8 | +from ipinfo.error import APIError |
| 9 | +from ipinfo.exceptions import RequestQuotaExceededError |
7 | 10 | import ipinfo
|
8 | 11 | 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 |
9 | 35 |
|
10 | 36 |
|
11 | 37 | @pytest.mark.asyncio
|
@@ -103,6 +129,39 @@ async def test_get_details():
|
103 | 129 |
|
104 | 130 | await handler.deinit()
|
105 | 131 |
|
| 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") |
106 | 165 |
|
107 | 166 | #############
|
108 | 167 | # BATCH TESTS
|
|
0 commit comments