52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
|
|
import json
|
||
|
|
import ssl
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
from election_data import fetch
|
||
|
|
|
||
|
|
pytestmark = pytest.mark.unit
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture(autouse=True)
|
||
|
|
def no_sleep(monkeypatch: pytest.MonkeyPatch) -> None:
|
||
|
|
monkeypatch.setattr(fetch.time, "sleep", lambda _s: None)
|
||
|
|
|
||
|
|
|
||
|
|
def test_download_writes_meta_and_uses_cache(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||
|
|
calls: list[bool] = []
|
||
|
|
|
||
|
|
def fake_stream(url: str, tmp: Path, verify: bool, timeout: float) -> None:
|
||
|
|
calls.append(verify)
|
||
|
|
tmp.write_bytes(b'{"ok": true}')
|
||
|
|
|
||
|
|
monkeypatch.setattr(fetch, "_stream_to", fake_stream)
|
||
|
|
dest = tmp_path / "a" / "x.json"
|
||
|
|
first = fetch.download("https://example.org/x.json", dest)
|
||
|
|
assert not first.from_cache and first.tls_verified
|
||
|
|
meta = json.loads((tmp_path / "a" / "x.json.meta.json").read_text(encoding="utf-8"))
|
||
|
|
assert meta["sha256"] == first.sha256 and meta["bytes"] == 12
|
||
|
|
data, second = fetch.fetch_json("https://example.org/x.json", dest)
|
||
|
|
assert data == {"ok": True} and second.from_cache
|
||
|
|
assert calls == [True]
|
||
|
|
|
||
|
|
|
||
|
|
def test_tls_fallback_only_for_allowed_hosts(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||
|
|
def fake_stream(url: str, tmp: Path, verify: bool, timeout: float) -> None:
|
||
|
|
if verify:
|
||
|
|
raise ssl.SSLError("CERTIFICATE_VERIFY_FAILED")
|
||
|
|
tmp.write_bytes(b"data")
|
||
|
|
|
||
|
|
monkeypatch.setattr(fetch, "_stream_to", fake_stream)
|
||
|
|
ok = fetch.download("https://ws.dgbas.gov.tw/a.xlsx", tmp_path / "a.xlsx")
|
||
|
|
assert ok.tls_verified is False
|
||
|
|
with pytest.raises(RuntimeError):
|
||
|
|
fetch.download("https://example.org/b.xlsx", tmp_path / "b.xlsx", retries=2)
|
||
|
|
|
||
|
|
|
||
|
|
def test_ssl_context_keeps_verification() -> None:
|
||
|
|
ctx = fetch._ssl_context()
|
||
|
|
assert ctx.verify_mode == ssl.CERT_REQUIRED and ctx.check_hostname
|
||
|
|
assert not ctx.verify_flags & ssl.VERIFY_X509_STRICT
|