import httpx
import pytest
import respx

from app.config import get_settings, tls_verify
from app.pulp_client import PulpClient
from tests.conftest import PULP_TEST_URL


def test_tls_verify_prefers_ca_file(monkeypatch):
    monkeypatch.setenv("PULP_CA_FILE", "/etc/ssl/boknet-ca.pem")
    monkeypatch.setenv("PULP_VERIFY_TLS", "false")
    get_settings.cache_clear()
    assert tls_verify(get_settings()) == "/etc/ssl/boknet-ca.pem"


def test_tls_verify_falls_back_to_bool():
    # conftest: CA 비어있음, VERIFY_TLS=false
    assert tls_verify(get_settings()) is False


@respx.mock
def test_status_returns_json():
    route = respx.get(f"{PULP_TEST_URL}/pulp/api/v3/status/").mock(
        return_value=httpx.Response(200, json={"online": True, "versions": []})
    )
    with PulpClient() as pulp:
        data = pulp.status()
    assert route.called
    assert data == {"online": True, "versions": []}


@respx.mock
def test_get_raises_on_http_error():
    respx.get(f"{PULP_TEST_URL}/pulp/api/v3/status/").mock(
        return_value=httpx.Response(503)
    )
    with PulpClient() as pulp, pytest.raises(httpx.HTTPStatusError):
        pulp.status()
