Files
shelfmark/tests/metadata/test_openlibrary_proxy.py
T
c43da4537d fix(network): apply HTTP proxy to Open Library and Prowlarr requests (#1526)
Fixes #1524

Open Library metadata searches and Prowlarr API calls used
`get_ssl_verify` but never `get_proxies`, so the HTTP proxy set in
Settings → Network was not applied to their HTTPS requests. Both now
pass the configured proxies. Relying on `requests`' `trust_env` with
only `HTTP_PROXY` set leaves the same gap for `https://` destinations.

Verification (Linux, `uv run --frozen pytest`):
- New tests `tests/metadata/test_openlibrary_proxy.py` and
`tests/prowlarr/test_api_proxy.py`: 4 passed. With the source change
reverted they fail (2 failed, 2 errors).
- `tests/metadata` + `tests/prowlarr`: 949 passed, 42 skipped.
- The full suite in my environment showed 61 failures and 26 errors
outside these areas. I did not compare them against `main`, so I'm not
claiming the full suite is green here; CI will show it.
- `ruff check` and `ruff format --check` on the changed files pass.

Prepared with AI assistance; reviewed and tested by me before
submitting.

Co-authored-by: dbdefault <246079841+dbdefault@users.noreply.github.com>
Co-authored-by: CaliBrain <calibrain@l4n.xyz>
2026-10-22 02:47:43 -04:00

85 lines
2.8 KiB
Python

"""Open Library requests must honor Network HTTP proxy settings (#1524)."""
import shelfmark.metadata_providers.openlibrary as openlibrary_module
from shelfmark.metadata_providers import MetadataSearchOptions, SearchType
from shelfmark.metadata_providers.openlibrary import OpenLibraryProvider
class _FakeResponse:
def __init__(self, payload=None, status_code=200):
self._payload = payload if payload is not None else {"docs": []}
self.status_code = status_code
self.ok = status_code < 400
def raise_for_status(self):
if not self.ok:
raise openlibrary_module.requests.HTTPError(f"{self.status_code}", response=self)
def json(self):
return self._payload
def test_openlibrary_search_passes_configured_http_proxy(monkeypatch):
"""Universal Open Library search must send traffic through get_proxies()."""
expected = {
"http": "http://10.0.80.3:8118",
"https": "http://10.0.80.3:8118",
}
monkeypatch.setattr(
openlibrary_module,
"get_proxies",
lambda url: expected if "openlibrary.org" in url else {},
)
monkeypatch.setattr(openlibrary_module, "get_ssl_verify", lambda _url: True)
monkeypatch.setattr(openlibrary_module._rate_limiter, "wait_if_needed", lambda: None)
from shelfmark.core.config import config as app_config
monkeypatch.setattr(
app_config,
"get",
lambda key, default=None: False if key == "METADATA_CACHE_ENABLED" else default,
)
provider = OpenLibraryProvider()
captured: dict = {}
def fake_get(url, **kwargs):
captured["url"] = url
captured.update(kwargs)
return _FakeResponse({"docs": []})
monkeypatch.setattr(provider.session, "get", fake_get)
provider.search(MetadataSearchOptions(query="Mistborn", search_type=SearchType.GENERAL))
assert captured.get("proxies") == expected
def test_openlibrary_connection_test_passes_configured_http_proxy(monkeypatch):
"""Settings connection test for Open Library must also use the proxy."""
expected = {
"http": "http://proxy.local:8118",
"https": "http://proxy.local:8118",
}
monkeypatch.setattr(openlibrary_module, "get_proxies", lambda _url: expected)
monkeypatch.setattr(openlibrary_module, "get_ssl_verify", lambda _url: True)
captured: dict = {}
def fake_get(url, **kwargs):
captured["url"] = url
captured.update(kwargs)
return _FakeResponse({"docs": [{"title": "ok"}]})
monkeypatch.setattr(
openlibrary_module.requests.Session,
"get",
lambda self, url, **kwargs: fake_get(url, **kwargs),
)
result = openlibrary_module._test_openlibrary_connection()
assert result["success"] is True
assert captured.get("proxies") == expected