@@ -0,0 +1,81 @@
|
||||
"""Tests for the app-level proxy setting."""
|
||||
|
||||
import pytest
|
||||
|
||||
from settings import get_proxy_enabled, set_proxy_enabled
|
||||
|
||||
|
||||
def _clear_proxy_env(monkeypatch):
|
||||
monkeypatch.delenv("HTTPS_PROXY", raising=False)
|
||||
monkeypatch.delenv("https_proxy", raising=False)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_proxy_enabled_defaults_true_when_https_proxy_is_usable(
|
||||
mock_pool, monkeypatch
|
||||
):
|
||||
"""First-run default should enable only with a usable proxy."""
|
||||
_clear_proxy_env(monkeypatch)
|
||||
monkeypatch.setenv("HTTPS_PROXY", "proxy.example:8080:user:pass")
|
||||
mock_pool.fetchval.return_value = None
|
||||
|
||||
assert await get_proxy_enabled(mock_pool) is True
|
||||
|
||||
mock_pool.fetchval.assert_awaited_once()
|
||||
assert "proxy_enabled" in mock_pool.fetchval.await_args.args
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("proxy_value", [None, "", "proxy.example:8080:user"])
|
||||
async def test_get_proxy_enabled_defaults_false_without_usable_https_proxy(
|
||||
mock_pool, monkeypatch, proxy_value
|
||||
):
|
||||
"""Missing, blank, and malformed proxy env values default disabled."""
|
||||
_clear_proxy_env(monkeypatch)
|
||||
if proxy_value is not None:
|
||||
monkeypatch.setenv("HTTPS_PROXY", proxy_value)
|
||||
mock_pool.fetchval.return_value = None
|
||||
|
||||
assert await get_proxy_enabled(mock_pool) is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("stored_value", "expected"),
|
||||
[
|
||||
("true", True),
|
||||
("1", True),
|
||||
("yes", True),
|
||||
("on", True),
|
||||
("false", False),
|
||||
("0", False),
|
||||
("no", False),
|
||||
("off", False),
|
||||
],
|
||||
)
|
||||
async def test_get_proxy_enabled_uses_persisted_setting(
|
||||
mock_pool, monkeypatch, stored_value, expected
|
||||
):
|
||||
"""Once saved, the DB setting should control the UI/runtime toggle."""
|
||||
_clear_proxy_env(monkeypatch)
|
||||
monkeypatch.setenv("HTTPS_PROXY", "proxy.example:8080:user:pass")
|
||||
mock_pool.fetchval.return_value = stored_value
|
||||
|
||||
assert await get_proxy_enabled(mock_pool) is expected
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("enabled", "stored_value"),
|
||||
[(True, "true"), (False, "false")],
|
||||
)
|
||||
async def test_set_proxy_enabled_persists_proxy_toggle(
|
||||
mock_pool, enabled, stored_value
|
||||
):
|
||||
"""The proxy toggle should be stored under the app setting key."""
|
||||
await set_proxy_enabled(mock_pool, enabled)
|
||||
|
||||
mock_pool.execute.assert_awaited()
|
||||
sql, *args = mock_pool.execute.await_args.args
|
||||
assert "proxy_enabled" in sql or "proxy_enabled" in args
|
||||
assert stored_value in args
|
||||
Reference in New Issue
Block a user