import pytest

from ratelimit.auths import EmptyInformation
from ratelimit.auths.ip import client_ip


@pytest.mark.parametrize(
    "scope, real_ip",
    [
        ({"client": ("1.1.1.1", 8000), "headers": tuple()}, "1.1.1.1"),
        (
            {
                "client": ("127.0.0.1", 8000),
                "headers": (
                    (
                        b"x-forwarded-for",
                        b"1.1.1.1, 172.16.17.32, 172.16.17.32",
                    ),
                    (b"x-real-ip", b"172.16.17.32"),
                ),
            },
            "172.16.17.32",
        ),
    ],
)
@pytest.mark.asyncio
async def test_client_ip(scope, real_ip):
    assert (await client_ip(scope))[0] == real_ip


@pytest.mark.parametrize(
    "scope",
    [
        {
            "client": ("127.0.0.1", 8000),
            "headers": ((b"host", b"example.com"),),
        },
        {"client": None, "headers": ((b"host", b"example.com"),)},
        {
            "client": ("172.18.81.1", 8000),
            "headers": tuple(),
        },
    ],
)
@pytest.mark.asyncio
async def test_error(scope):
    with pytest.raises(EmptyInformation):
        await client_ip(scope)

import pytest

from ratelimit.auths import EmptyInformation
from ratelimit.auths.ip import client_ip


@pytest.mark.parametrize(
    "scope, real_ip",
    [
        ({"client": ("1.1.1.1", 8000), "headers": tuple()}, "1.1.1.1"),
        (
            {
                "client": ("127.0.0.1", 8000),
                "headers": (
                    (
                        b"x-forwarded-for",
                        b"1.1.1.1, 172.16.17.32, 172.16.17.32",
                    ),
                    (b"x-real-ip", b"172.16.17.32"),
                ),
            },
            "172.16.17.32",
        ),
    ],
)
@pytest.mark.asyncio
async def test_client_ip(scope, real_ip):
    assert (await client_ip(scope))[0] == real_ip


@pytest.mark.parametrize(
    "scope",
    [
        {
            "client": ("127.0.0.1", 8000),
            "headers": ((b"host", b"example.com"),),
        },
        {"client": None, "headers": ((b"host", b"example.com"),)},
        {
            "client": ("172.18.81.1", 8000),
            "headers": tuple(),
        },
    ],
)
@pytest.mark.asyncio
async def test_error(scope):
    with pytest.raises(EmptyInformation):
        await client_ip(scope)
