update tests/conftest.py
Browse files- tests/conftest.py +115 -0
tests/conftest.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
import asyncio
|
| 3 |
+
import base64
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Ensure data directory exists for SQLite BEFORE any app imports
|
| 7 |
+
if not os.path.exists("./data"):
|
| 8 |
+
os.makedirs("./data", exist_ok=True)
|
| 9 |
+
|
| 10 |
+
from typing import List
|
| 11 |
+
from app.models.proxy import Proxy, ValidationResult
|
| 12 |
+
from app.models.source import SourceConfig, SourceType
|
| 13 |
+
from app.database import engine, Base
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@pytest.fixture(scope="session", autouse=True)
|
| 17 |
+
def setup_test_env():
|
| 18 |
+
"""Double check test environment."""
|
| 19 |
+
os.makedirs("./data", exist_ok=True)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@pytest.fixture(scope="session")
|
| 23 |
+
def event_loop():
|
| 24 |
+
loop = asyncio.get_event_loop_policy().new_event_loop()
|
| 25 |
+
yield loop
|
| 26 |
+
loop.close()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@pytest.fixture(scope="session", autouse=True)
|
| 30 |
+
async def init_test_db():
|
| 31 |
+
"""Initialize the test database schema."""
|
| 32 |
+
async with engine.begin() as conn:
|
| 33 |
+
await conn.run_sync(Base.metadata.drop_all)
|
| 34 |
+
await conn.run_sync(Base.metadata.create_all)
|
| 35 |
+
yield
|
| 36 |
+
async with engine.begin() as conn:
|
| 37 |
+
await conn.run_sync(Base.metadata.drop_all)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
@pytest.fixture
|
| 41 |
+
def sample_http_proxy() -> Proxy:
|
| 42 |
+
return Proxy(
|
| 43 |
+
ip="192.168.1.1", port=8080, protocol="http", source="test", anonymity="elite"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
@pytest.fixture
|
| 48 |
+
def sample_vmess_proxy() -> Proxy:
|
| 49 |
+
return Proxy(ip="10.0.0.1", port=443, protocol="vmess", source="test")
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
@pytest.fixture
|
| 53 |
+
def sample_proxy_list() -> List[Proxy]:
|
| 54 |
+
return [
|
| 55 |
+
Proxy(ip="192.168.1.1", port=8080, protocol="http", source="test"),
|
| 56 |
+
Proxy(ip="192.168.1.2", port=3128, protocol="http", source="test"),
|
| 57 |
+
Proxy(ip="10.0.0.1", port=443, protocol="vmess", source="test"),
|
| 58 |
+
Proxy(ip="10.0.0.2", port=443, protocol="vless", source="test"),
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@pytest.fixture
|
| 63 |
+
def github_raw_source() -> SourceConfig:
|
| 64 |
+
return SourceConfig(
|
| 65 |
+
url="https://raw.githubusercontent.com/user/repo/main/list.txt",
|
| 66 |
+
type=SourceType.GITHUB_RAW,
|
| 67 |
+
enabled=True,
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
@pytest.fixture
|
| 72 |
+
def subscription_source() -> SourceConfig:
|
| 73 |
+
return SourceConfig(
|
| 74 |
+
url="https://example.com/subscription",
|
| 75 |
+
type=SourceType.SUBSCRIPTION_BASE64,
|
| 76 |
+
enabled=True,
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
@pytest.fixture
|
| 81 |
+
def sample_validation_result(sample_http_proxy) -> ValidationResult:
|
| 82 |
+
return ValidationResult(
|
| 83 |
+
proxy_id=sample_http_proxy.id,
|
| 84 |
+
passed=True,
|
| 85 |
+
latency_ms=123.45,
|
| 86 |
+
is_elite=True,
|
| 87 |
+
headers={"User-Agent": "test"},
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
@pytest.fixture
|
| 92 |
+
def http_proxy_list_content() -> str:
|
| 93 |
+
return """
|
| 94 |
+
192.168.1.1:8080
|
| 95 |
+
192.168.1.2:3128
|
| 96 |
+
10.0.0.1:8888
|
| 97 |
+
10.0.0.2:9999
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
@pytest.fixture
|
| 102 |
+
def mixed_protocol_content() -> str:
|
| 103 |
+
return """
|
| 104 |
+
192.168.1.1:8080
|
| 105 |
+
vmess://eyJhZGQiOiIxMjcuMC4wLjEiLCJwb3J0Ijo0NDN9
|
| 106 |
+
vless://uuid-test@example.com:443?type=tcp
|
| 107 |
+
trojan://password@server.com:443
|
| 108 |
+
ss://YWVzLTI1Ni1nY206cGFzcw@10.0.0.1:8388
|
| 109 |
+
"""
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
@pytest.fixture
|
| 113 |
+
def base64_subscription_content() -> str:
|
| 114 |
+
content = "vmess://test1\nvless://test2"
|
| 115 |
+
return base64.b64encode(content.encode()).decode()
|