/
Mihaham
/
Table-Time
Обзор
Документация
Войти
/
Mihaham
/
Table-Time
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
services/api/tests/test_admin_auth.py
104 строки
3 KB
MihahamYT
feat: admin panel with Telegram OTP and detailed game logs
14 июн 2026, 09:40
14 июн 2026, 09:40
88f1eac
Код
Авторство
О чём код?
import os from unittest.mock import AsyncMock, patch import pytest from starlette.testclient import TestClient os.environ["ADMIN_TELEGRAM_ID"] = "999888777" os.environ["ADMIN_USERNAME"] = "Mihaham" from app.config import get_settings class FakeRedis: def __init__(self): self.store: dict[str, str] = {} self.ttl: dict[str, int] = {} async def incr(self, key: str) -> int: val = int(self.store.get(key, "0")) + 1 self.store[key] = str(val) return val async def expire(self, key: str, seconds: int) -> None: self.ttl[key] = seconds async def setex(self, key: str, ttl: int, value: str) -> None: self.store[key] = value self.ttl[key] = ttl async def get(self, key: str) -> str | None: return self.store.get(key) async def delete(self, *keys: str) -> None: for key in keys: self.store.pop(key, None) self.ttl.pop(key, None) @pytest.fixture def fake_redis(): return FakeRedis() @pytest.fixture def admin_client(client: TestClient, fake_redis): get_settings.cache_clear() with patch("app.modules.admin.auth.get_redis", new_callable=AsyncMock) as mock_redis: mock_redis.return_value = fake_redis yield client, fake_redis get_settings.cache_clear() def test_admin_otp_request_not_admin(admin_client): client, _ = admin_client r = client.post("/api/v1/auth/admin/otp/request", json={"telegram_id": 111}) assert r.status_code == 403 def test_admin_otp_request_success(admin_client): client, redis = admin_client r = client.post("/api/v1/auth/admin/otp/request", json={"telegram_id": 999888777}) assert r.status_code == 200 data = r.json() assert len(data["code"]) == 6 assert data["expires_in"] == 300 assert redis.store["admin:otp:999888777"] == data["code"] def test_admin_otp_verify_flow(admin_client): client, redis = admin_client req = client.post("/api/v1/auth/admin/otp/request", json={"telegram_id": 999888777}) code = req.json()["code"] bad = client.post( "/api/v1/auth/admin/otp/verify", json={"username": "wrong", "code": code}, ) assert bad.status_code == 401 ok = client.post( "/api/v1/auth/admin/otp/verify", json={"username": "Mihaham", "code": code}, ) assert ok.status_code == 200 token = ok.json()["access_token"] assert ok.json()["token_type"] == "admin" r = client.get("/api/v1/admin/games", headers={"Authorization": f"Bearer {token}"}) assert r.status_code == 200 def test_admin_otp_invalid_code(admin_client): client, redis = admin_client client.post("/api/v1/auth/admin/otp/request", json={"telegram_id": 999888777}) r = client.post( "/api/v1/auth/admin/otp/verify", json={"username": "Mihaham", "code": "000000"}, ) assert r.status_code == 401 def test_admin_endpoints_require_auth(client): r = client.get("/api/v1/admin/games") assert r.status_code == 403