import httpx

from abuseipdb import AbuseIPDB
from abuseipdb.reporters import report_suspicious_operation


def test_suspicious_reporter_formats_request_and_uses_web_app_category() -> None:
    requests: list[httpx.Request] = []

    def handler(request: httpx.Request) -> httpx.Response:
        requests.append(request)
        return httpx.Response(
            200, json={"data": {"ipAddress": "203.0.113.9", "abuseConfidenceScore": 10}}
        )

    with AbuseIPDB(api_key="key", transport=httpx.MockTransport(handler)) as client:
        result = report_suspicious_operation(
            client,
            "203.0.113.9",
            {"path": "/admin", "attempt": 3},
        )
    assert result is not None
    values = dict(httpx.QueryParams(requests[0].content.decode()))
    assert values == {
        "ip": "203.0.113.9",
        "categories": "21",
        "comment": "Suspicious Operation. Request content:\npath: /admin\nattempt: 3\n",
    }


def test_suspicious_reporter_returns_none_when_sdk_request_fails() -> None:
    def handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(429, json={"errors": [{"detail": "limit"}]})

    with AbuseIPDB(api_key="key", transport=httpx.MockTransport(handler)) as client:
        assert report_suspicious_operation(client, "203.0.113.9", {}) is None
