AI-Sprint-bot / tests /unit /test_validators.py
FreshPixels's picture
Create tests/unit/test_validators.py
5dcad65 verified
Raw
History Blame Contribute Delete
2.76 kB
from __future__ import annotations
import pytest
from core.security.validators import validate_admin_id, validate_bot_token
# ---------------------------------------------------------------------------
# validate_bot_token
# ---------------------------------------------------------------------------
class TestValidateBotToken:
def test_valid_token(self) -> None:
token = "123456789:AAHdqTcvCH1vGWJxfSeofSAsQK6PALsAWo"
assert validate_bot_token(token) == token
def test_none_raises(self) -> None:
with pytest.raises(ValueError, match="BOT_TOKEN is None"):
validate_bot_token(None)
def test_empty_raises(self) -> None:
with pytest.raises(ValueError, match="BOT_TOKEN is empty"):
validate_bot_token("")
def test_whitespace_only_raises(self) -> None:
with pytest.raises(ValueError, match="BOT_TOKEN is empty"):
validate_bot_token(" ")
def test_invalid_format_no_colon(self) -> None:
with pytest.raises(ValueError, match="format is invalid"):
validate_bot_token("invalidtoken")
def test_invalid_format_short_hash(self) -> None:
with pytest.raises(ValueError, match="format is invalid"):
validate_bot_token("123:short")
def test_whitespace_stripped(self) -> None:
token = " 123456789:AAHdqTcvCH1vGWJxfSeofSAsQK6PALsAWo "
result = validate_bot_token(token)
assert result == token.strip()
# ---------------------------------------------------------------------------
# validate_admin_id
# ---------------------------------------------------------------------------
class TestValidateAdminId:
def test_valid_id(self) -> None:
assert validate_admin_id("42") == "42"
def test_none_raises(self) -> None:
with pytest.raises(ValueError, match="ADMIN_ID is None"):
validate_admin_id(None)
def test_empty_raises(self) -> None:
with pytest.raises(ValueError, match="ADMIN_ID is empty"):
validate_admin_id("")
def test_negative_raises(self) -> None:
with pytest.raises(ValueError, match="must be a positive integer"):
validate_admin_id("-1")
def test_zero_raises(self) -> None:
with pytest.raises(ValueError, match="must be a positive integer"):
validate_admin_id("0")
def test_float_raises(self) -> None:
with pytest.raises(ValueError, match="float-like value"):
validate_admin_id("3.14")
def test_non_numeric_raises(self) -> None:
with pytest.raises(ValueError, match="must be a numeric value"):
validate_admin_id("abc")
def test_whitespace_stripped(self) -> None:
assert validate_admin_id(" 42 ") == "42"