Spaces:
Runtime error
Runtime error
Create tests/unit/test_validators.py
Browse files
tests/unit/test_validators.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
from core.security.validators import validate_admin_id, validate_bot_token
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# ---------------------------------------------------------------------------
|
| 9 |
+
# validate_bot_token
|
| 10 |
+
# ---------------------------------------------------------------------------
|
| 11 |
+
|
| 12 |
+
class TestValidateBotToken:
|
| 13 |
+
def test_valid_token(self) -> None:
|
| 14 |
+
token = "123456789:AAHdqTcvCH1vGWJxfSeofSAsQK6PALsAWo"
|
| 15 |
+
assert validate_bot_token(token) == token
|
| 16 |
+
|
| 17 |
+
def test_none_raises(self) -> None:
|
| 18 |
+
with pytest.raises(ValueError, match="BOT_TOKEN is None"):
|
| 19 |
+
validate_bot_token(None)
|
| 20 |
+
|
| 21 |
+
def test_empty_raises(self) -> None:
|
| 22 |
+
with pytest.raises(ValueError, match="BOT_TOKEN is empty"):
|
| 23 |
+
validate_bot_token("")
|
| 24 |
+
|
| 25 |
+
def test_whitespace_only_raises(self) -> None:
|
| 26 |
+
with pytest.raises(ValueError, match="BOT_TOKEN is empty"):
|
| 27 |
+
validate_bot_token(" ")
|
| 28 |
+
|
| 29 |
+
def test_invalid_format_no_colon(self) -> None:
|
| 30 |
+
with pytest.raises(ValueError, match="format is invalid"):
|
| 31 |
+
validate_bot_token("invalidtoken")
|
| 32 |
+
|
| 33 |
+
def test_invalid_format_short_hash(self) -> None:
|
| 34 |
+
with pytest.raises(ValueError, match="format is invalid"):
|
| 35 |
+
validate_bot_token("123:short")
|
| 36 |
+
|
| 37 |
+
def test_whitespace_stripped(self) -> None:
|
| 38 |
+
token = " 123456789:AAHdqTcvCH1vGWJxfSeofSAsQK6PALsAWo "
|
| 39 |
+
result = validate_bot_token(token)
|
| 40 |
+
assert result == token.strip()
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# ---------------------------------------------------------------------------
|
| 44 |
+
# validate_admin_id
|
| 45 |
+
# ---------------------------------------------------------------------------
|
| 46 |
+
|
| 47 |
+
class TestValidateAdminId:
|
| 48 |
+
def test_valid_id(self) -> None:
|
| 49 |
+
assert validate_admin_id("42") == "42"
|
| 50 |
+
|
| 51 |
+
def test_none_raises(self) -> None:
|
| 52 |
+
with pytest.raises(ValueError, match="ADMIN_ID is None"):
|
| 53 |
+
validate_admin_id(None)
|
| 54 |
+
|
| 55 |
+
def test_empty_raises(self) -> None:
|
| 56 |
+
with pytest.raises(ValueError, match="ADMIN_ID is empty"):
|
| 57 |
+
validate_admin_id("")
|
| 58 |
+
|
| 59 |
+
def test_negative_raises(self) -> None:
|
| 60 |
+
with pytest.raises(ValueError, match="must be a positive integer"):
|
| 61 |
+
validate_admin_id("-1")
|
| 62 |
+
|
| 63 |
+
def test_zero_raises(self) -> None:
|
| 64 |
+
with pytest.raises(ValueError, match="must be a positive integer"):
|
| 65 |
+
validate_admin_id("0")
|
| 66 |
+
|
| 67 |
+
def test_float_raises(self) -> None:
|
| 68 |
+
with pytest.raises(ValueError, match="float-like value"):
|
| 69 |
+
validate_admin_id("3.14")
|
| 70 |
+
|
| 71 |
+
def test_non_numeric_raises(self) -> None:
|
| 72 |
+
with pytest.raises(ValueError, match="must be a numeric value"):
|
| 73 |
+
validate_admin_id("abc")
|
| 74 |
+
|
| 75 |
+
def test_whitespace_stripped(self) -> None:
|
| 76 |
+
assert validate_admin_id(" 42 ") == "42"
|
| 77 |
+
|