from sanic_ipware import get_client_ip


def test_meta_none(create_request):
    request = create_request()
    ip, routable = get_client_ip(request)
    assert ip is None
    assert routable is False


def test_meta_single(create_request):
    request = create_request(
        {"X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"}
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_multi(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "Via": "fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b",
        }
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_multi_precedence_order(create_request):
    request = create_request(
        {
            "Forwarded-For": "fc00:db20:35b:7399::5, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "Via": "fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b",
        }
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_proxy_order_left_most(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"
        }
    )
    result = get_client_ip(request, proxy_order="left-most")
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_proxy_order_right_most(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"
        }
    )
    result = get_client_ip(request, proxy_order="right-most")
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_multi_precedence_private_first(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "2001:db8:, ::1",
            "Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "Via": "fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b",
        }
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_multi_precedence_invalid_first(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "unknown, 2001:db8:, ::1",
            "Forwarded-For": "3ffe:1900:4545:3:200:f8ff:fe21:67cf, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "Via": "fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b",
        }
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_error_only(create_request):
    request = create_request(
        {
            "Forwarded-For": "unknown, fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3bcf, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"
        }
    )
    result = get_client_ip(request)
    assert result == (None, False)


def test_meta_error_first(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "unknown, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
        }
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_singleton(create_request):
    request = create_request(
        {"X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"}
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_singleton_proxy_count(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "X-Real-IP": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
        }
    )
    result = get_client_ip(request, proxy_count=1)
    assert result == (None, False)


def test_meta_singleton_proxy_count_private(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "::1",
            "X-Real-IP": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
        }
    )
    result = get_client_ip(request, proxy_count=1)
    assert result == (None, False)


def test_meta_singleton_private_fallback(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "::1",
            "X-Real-IP": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
        }
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_proxy_trusted_ips(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"
        }
    )
    result = get_client_ip(request, proxy_trusted_ips=["fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"])
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_proxy_trusted_ips_proxy_count(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"
        }
    )
    result = get_client_ip(
        request, proxy_count=2, proxy_trusted_ips=["fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"]
    )
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_proxy_trusted_ips_proxy_count_less_error(create_request):
    request = create_request(
        {"X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"}
    )
    result = get_client_ip(
        request, proxy_count=2, proxy_trusted_ips=["fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"]
    )
    assert result == (None, False)


def test_meta_proxy_trusted_ips_proxy_count_more_error(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"
        }
    )
    result = get_client_ip(
        request, proxy_count=1, proxy_trusted_ips=["fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"]
    )
    assert result == (None, False)


def test_meta_proxy_trusted_ips_proxy_count_more_error_ignore_fallback(
    create_request
):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "X-Real-IP": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
        }
    )
    result = get_client_ip(
        request, proxy_count=1, proxy_trusted_ips=["fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"]
    )
    assert result == (None, False)

from sanic_ipware import get_client_ip


def test_meta_none(create_request):
    request = create_request()
    ip, routable = get_client_ip(request)
    assert ip is None
    assert routable is False


def test_meta_single(create_request):
    request = create_request(
        {"X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"}
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_multi(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "Via": "fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b",
        }
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_multi_precedence_order(create_request):
    request = create_request(
        {
            "Forwarded-For": "fc00:db20:35b:7399::5, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "Via": "fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b",
        }
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_proxy_order_left_most(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"
        }
    )
    result = get_client_ip(request, proxy_order="left-most")
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_proxy_order_right_most(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"
        }
    )
    result = get_client_ip(request, proxy_order="right-most")
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_multi_precedence_private_first(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "2001:db8:, ::1",
            "Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "Via": "fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b",
        }
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_multi_precedence_invalid_first(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "unknown, 2001:db8:, ::1",
            "Forwarded-For": "3ffe:1900:4545:3:200:f8ff:fe21:67cf, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "Via": "fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b",
        }
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_error_only(create_request):
    request = create_request(
        {
            "Forwarded-For": "unknown, fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3bcf, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"
        }
    )
    result = get_client_ip(request)
    assert result == (None, False)


def test_meta_error_first(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "unknown, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
        }
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_singleton(create_request):
    request = create_request(
        {"X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"}
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_singleton_proxy_count(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "X-Real-IP": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
        }
    )
    result = get_client_ip(request, proxy_count=1)
    assert result == (None, False)


def test_meta_singleton_proxy_count_private(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "::1",
            "X-Real-IP": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
        }
    )
    result = get_client_ip(request, proxy_count=1)
    assert result == (None, False)


def test_meta_singleton_private_fallback(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "::1",
            "X-Real-IP": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
        }
    )
    result = get_client_ip(request)
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_proxy_trusted_ips(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"
        }
    )
    result = get_client_ip(request, proxy_trusted_ips=["fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"])
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_proxy_trusted_ips_proxy_count(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"
        }
    )
    result = get_client_ip(
        request, proxy_count=2, proxy_trusted_ips=["fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"]
    )
    assert result == ("fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b", True)


def test_meta_proxy_trusted_ips_proxy_count_less_error(create_request):
    request = create_request(
        {"X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"}
    )
    result = get_client_ip(
        request, proxy_count=2, proxy_trusted_ips=["fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"]
    )
    assert result == (None, False)


def test_meta_proxy_trusted_ips_proxy_count_more_error(create_request):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"
        }
    )
    result = get_client_ip(
        request, proxy_count=1, proxy_trusted_ips=["fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"]
    )
    assert result == (None, False)


def test_meta_proxy_trusted_ips_proxy_count_more_error_ignore_fallback(
    create_request
):
    request = create_request(
        {
            "X-Forwarded-For": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b, fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
            "X-Real-IP": "fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b",
        }
    )
    result = get_client_ip(
        request, proxy_count=1, proxy_trusted_ips=["fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b"]
    )
    assert result == (None, False)
