|
| 1 | +import os |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from ipinfo import handler_utils |
| 6 | +from ipinfo.cache.default import DefaultCache |
| 7 | +from ipinfo.details import Details |
| 8 | +from ipinfo.handler_resproxy_async import AsyncHandlerResProxy |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.asyncio |
| 12 | +async def test_init(): |
| 13 | + token = "mytesttoken" |
| 14 | + handler = AsyncHandlerResProxy(token) |
| 15 | + assert handler.access_token == token |
| 16 | + assert isinstance(handler.cache, DefaultCache) |
| 17 | + await handler.deinit() |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.asyncio |
| 21 | +async def test_headers(): |
| 22 | + token = "mytesttoken" |
| 23 | + handler = AsyncHandlerResProxy(token, headers={"custom_field": "yes"}) |
| 24 | + headers = handler_utils.get_headers(token, handler.headers) |
| 25 | + await handler.deinit() |
| 26 | + |
| 27 | + assert "user-agent" in headers |
| 28 | + assert "accept" in headers |
| 29 | + assert "authorization" in headers |
| 30 | + assert "custom_field" in headers |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.skipif( |
| 34 | + "IPINFO_TOKEN" not in os.environ, |
| 35 | + reason="Can't call ResProxy API without token", |
| 36 | +) |
| 37 | +@pytest.mark.asyncio |
| 38 | +async def test_get_details(): |
| 39 | + """Test basic ResProxy API lookup""" |
| 40 | + token = os.environ.get("IPINFO_TOKEN", "") |
| 41 | + handler = AsyncHandlerResProxy(token) |
| 42 | + details = await handler.getDetails("139.5.0.122") |
| 43 | + |
| 44 | + # Should return Details object |
| 45 | + assert isinstance(details, Details) |
| 46 | + assert details.ip == "139.5.0.122" |
| 47 | + |
| 48 | + # Check ResProxy-specific fields |
| 49 | + assert hasattr(details, "last_seen") |
| 50 | + assert hasattr(details, "percent_days_seen") |
| 51 | + assert hasattr(details, "service") |
| 52 | + |
| 53 | + await handler.deinit() |
| 54 | + |
| 55 | + |
| 56 | +############# |
| 57 | +# BOGON TESTS |
| 58 | +############# |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.skipif( |
| 62 | + "IPINFO_TOKEN" not in os.environ, |
| 63 | + reason="Can't call ResProxy API without token", |
| 64 | +) |
| 65 | +@pytest.mark.asyncio |
| 66 | +async def test_bogon_details(): |
| 67 | + token = os.environ.get("IPINFO_TOKEN", "") |
| 68 | + handler = AsyncHandlerResProxy(token) |
| 69 | + details = await handler.getDetails("127.0.0.1") |
| 70 | + assert isinstance(details, Details) |
| 71 | + assert details.all == {"bogon": True, "ip": "127.0.0.1"} |
| 72 | + await handler.deinit() |
| 73 | + |
| 74 | + |
| 75 | +##################### |
| 76 | +# BATCH TESTS |
| 77 | +##################### |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.skipif( |
| 81 | + "IPINFO_TOKEN" not in os.environ, |
| 82 | + reason="Can't call ResProxy API without token", |
| 83 | +) |
| 84 | +@pytest.mark.asyncio |
| 85 | +async def test_batch_ips(): |
| 86 | + """Test batch request with IPs""" |
| 87 | + token = os.environ.get("IPINFO_TOKEN", "") |
| 88 | + handler = AsyncHandlerResProxy(token) |
| 89 | + results = await handler.getBatchDetails(["139.5.0.122", "45.95.168.1"]) |
| 90 | + |
| 91 | + assert len(results) == 2 |
| 92 | + assert "139.5.0.122" in results |
| 93 | + assert "45.95.168.1" in results |
| 94 | + |
| 95 | + # Both should be Details objects |
| 96 | + assert isinstance(results["139.5.0.122"], Details) |
| 97 | + assert isinstance(results["45.95.168.1"], Details) |
| 98 | + |
| 99 | + # Check ResProxy-specific fields |
| 100 | + assert hasattr(results["139.5.0.122"], "last_seen") |
| 101 | + assert hasattr(results["139.5.0.122"], "percent_days_seen") |
| 102 | + assert hasattr(results["139.5.0.122"], "service") |
| 103 | + |
| 104 | + await handler.deinit() |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.skipif( |
| 108 | + "IPINFO_TOKEN" not in os.environ, |
| 109 | + reason="Can't call ResProxy API without token", |
| 110 | +) |
| 111 | +@pytest.mark.asyncio |
| 112 | +async def test_batch_with_bogon(): |
| 113 | + """Test batch including bogon IPs""" |
| 114 | + token = os.environ.get("IPINFO_TOKEN", "") |
| 115 | + handler = AsyncHandlerResProxy(token) |
| 116 | + results = await handler.getBatchDetails( |
| 117 | + [ |
| 118 | + "139.5.0.122", |
| 119 | + "127.0.0.1", # Bogon |
| 120 | + "45.95.168.1", |
| 121 | + ] |
| 122 | + ) |
| 123 | + |
| 124 | + assert len(results) == 3 |
| 125 | + |
| 126 | + # Normal IPs should be Details |
| 127 | + assert isinstance(results["139.5.0.122"], Details) |
| 128 | + assert isinstance(results["45.95.168.1"], Details) |
| 129 | + |
| 130 | + # Bogon should also be Details with bogon flag |
| 131 | + assert isinstance(results["127.0.0.1"], Details) |
| 132 | + assert results["127.0.0.1"].bogon == True |
| 133 | + |
| 134 | + await handler.deinit() |
| 135 | + |
| 136 | + |
| 137 | +##################### |
| 138 | +# CACHING TESTS |
| 139 | +##################### |
| 140 | + |
| 141 | + |
| 142 | +@pytest.mark.skipif( |
| 143 | + "IPINFO_TOKEN" not in os.environ, |
| 144 | + reason="Can't call ResProxy API without token", |
| 145 | +) |
| 146 | +@pytest.mark.asyncio |
| 147 | +async def test_caching(): |
| 148 | + """Test that results are properly cached""" |
| 149 | + token = os.environ.get("IPINFO_TOKEN", "") |
| 150 | + handler = AsyncHandlerResProxy(token) |
| 151 | + |
| 152 | + # First request - should hit API |
| 153 | + details1 = await handler.getDetails("139.5.0.122") |
| 154 | + assert isinstance(details1, Details) |
| 155 | + |
| 156 | + # Second request - should come from cache |
| 157 | + details2 = await handler.getDetails("139.5.0.122") |
| 158 | + assert isinstance(details2, Details) |
| 159 | + assert details2.ip == details1.ip |
| 160 | + |
| 161 | + # Verify cache key exists |
| 162 | + cache_key_val = handler_utils.cache_key("139.5.0.122") |
| 163 | + assert cache_key_val in handler.cache |
| 164 | + |
| 165 | + await handler.deinit() |
| 166 | + |
| 167 | + |
| 168 | +@pytest.mark.skipif( |
| 169 | + "IPINFO_TOKEN" not in os.environ, |
| 170 | + reason="Can't call ResProxy API without token", |
| 171 | +) |
| 172 | +@pytest.mark.asyncio |
| 173 | +async def test_batch_caching(): |
| 174 | + """Test that batch results are properly cached""" |
| 175 | + token = os.environ.get("IPINFO_TOKEN", "") |
| 176 | + handler = AsyncHandlerResProxy(token) |
| 177 | + |
| 178 | + # First batch request |
| 179 | + results1 = await handler.getBatchDetails(["139.5.0.122", "45.95.168.1"]) |
| 180 | + assert len(results1) == 2 |
| 181 | + |
| 182 | + # Second batch with same IPs (should come from cache) |
| 183 | + results2 = await handler.getBatchDetails(["139.5.0.122", "45.95.168.1"]) |
| 184 | + assert len(results2) == 2 |
| 185 | + assert results2["139.5.0.122"].ip == results1["139.5.0.122"].ip |
| 186 | + |
| 187 | + await handler.deinit() |
0 commit comments