ab59476d10
- 优化桌面标签、上下文标题、前进后退、导航栏隐藏和原生菜单体验 - 补充登录会话 IP 采集、地理位置回退、管理端展示及数据库迁移 - 更新桌面发布检查、运维文档和前后端测试覆盖
59 lines
2.1 KiB
Python
59 lines
2.1 KiB
Python
import pytest
|
|
|
|
from app.core.config import settings
|
|
from app.services import ip_geolocation_fallback
|
|
from app.services.ip_geolocation_fallback import ExternalIpLocation
|
|
|
|
|
|
def test_ip2location_response_parser_requires_matching_public_ip_and_coordinates():
|
|
result = ip_geolocation_fallback._parse_response(
|
|
"8.8.8.8",
|
|
{
|
|
"ip": "8.8.8.8",
|
|
"country_code": "US",
|
|
"country_name": "United States of America",
|
|
"region_name": "California",
|
|
"city_name": "Mountain View",
|
|
"latitude": 37.38605,
|
|
"longitude": -122.08385,
|
|
"isp": "Google LLC",
|
|
},
|
|
)
|
|
|
|
assert result is not None
|
|
assert result.country_code == "US"
|
|
assert result.longitude == -122.08385
|
|
assert ip_geolocation_fallback._parse_response("8.8.8.8", {"ip": "1.1.1.1"}) is None
|
|
assert ip_geolocation_fallback._global_ip("192.168.1.8") is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_external_fallback_caches_public_ip_results(monkeypatch):
|
|
calls = []
|
|
|
|
async def fake_fetch(_client, _semaphore, ip_address):
|
|
calls.append(ip_address)
|
|
return ExternalIpLocation(
|
|
ip_address=ip_address,
|
|
country="United States of America",
|
|
country_code="US",
|
|
region="California",
|
|
city="Mountain View",
|
|
isp="Google LLC",
|
|
longitude=-122.08385,
|
|
latitude=37.38605,
|
|
)
|
|
|
|
monkeypatch.setattr(settings, "ENV", "development")
|
|
monkeypatch.setattr(settings, "MONITORING_IP_GEO_FALLBACK_ENABLED", True)
|
|
monkeypatch.setattr(ip_geolocation_fallback, "_fetch_one", fake_fetch)
|
|
ip_geolocation_fallback.reset_ip_geolocation_fallback_cache()
|
|
|
|
first = await ip_geolocation_fallback.resolve_external_ip_locations(["8.8.8.8", "192.168.1.8"])
|
|
second = await ip_geolocation_fallback.resolve_external_ip_locations(["8.8.8.8"])
|
|
|
|
assert first["8.8.8.8"].city == "Mountain View"
|
|
assert second["8.8.8.8"].latitude == 37.38605
|
|
assert calls == ["8.8.8.8"]
|
|
ip_geolocation_fallback.reset_ip_geolocation_fallback_cache()
|