import pytest

from starlette_proxy_router.authenfication import check_auth_midlleware
from starlette_proxy_router.exceptions import ProxyAuthenficationError
from starlette_proxy_router.parser import EndpointArgs, Parser
from tests.constants import PATH_FILE_CONFIG
from tests.utils.auth import create_auth_middleware


@pytest.fixture
def endpoints_args() -> list[EndpointArgs]:
    parser = Parser(PATH_FILE_CONFIG)
    return parser.parse_configs()


def test_check_auth_midlleware(endpoints_args: list[EndpointArgs]) -> None:
    auth_midlleware = create_auth_middleware()

    assert check_auth_midlleware(auth_midlleware, endpoints_args) is None  # type: ignore

    with pytest.raises(ProxyAuthenficationError):
        check_auth_midlleware([], endpoints_args)

    endpoints_args = [
        EndpointArgs(
            path=args.path,
            target=args.target,
            method=args.method,
            host=args.host,
            service_name=args.service_name,
            is_auth=False,
        )
        for args in endpoints_args
    ]
    with pytest.deprecated_call():
        check_auth_midlleware(auth_midlleware, endpoints_args)
