"""
Tests auth API.
"""

from dataclasses import dataclass
import pytest
from fastapi import HTTPException

from pricewatch.app.api.main import require_token


@dataclass
class FakeRedisConfig:
    url: str


@dataclass
class FakeDbConfig:
    url: str


@dataclass
class FakeAppConfig:
    db: FakeDbConfig
    redis: FakeRedisConfig
    api_token: str


def test_missing_token_returns_401(monkeypatch):
    """Sans token, retourne 401."""
    config = FakeAppConfig(
        db=FakeDbConfig(url="sqlite:///:memory:"),
        redis=FakeRedisConfig(url="redis://localhost:6379/0"),
        api_token="secret",
    )
    monkeypatch.setattr("pricewatch.app.api.main.get_config", lambda: config)

    with pytest.raises(HTTPException) as excinfo:
        require_token(None)

    assert excinfo.value.status_code == 401


def test_bad_token_returns_403(monkeypatch):
    """Token invalide retourne 403."""
    config = FakeAppConfig(
        db=FakeDbConfig(url="sqlite:///:memory:"),
        redis=FakeRedisConfig(url="redis://localhost:6379/0"),
        api_token="secret",
    )
    monkeypatch.setattr("pricewatch.app.api.main.get_config", lambda: config)

    with pytest.raises(HTTPException) as excinfo:
        require_token("Bearer nope")

    assert excinfo.value.status_code == 403
