/
Mihaham
/
Table-Time
Обзор
Документация
Войти
/
Mihaham
/
Table-Time
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
services/api/tests/test_admin_api.py
129 строк
4 KB
MihahamYT
fix: ruff lint for CI and GitVerse Pages deploy workflow
14 июн 2026, 10:47
14 июн 2026, 10:47
b35fc39
Код
Авторство
О чём код?
"""Admin HTTP API coverage.""" import os from unittest.mock import AsyncMock, patch import pytest os.environ["ADMIN_TELEGRAM_ID"] = "999888777" os.environ["ADMIN_USERNAME"] = "Mihaham" from app.config import get_settings from tests.conftest import auth_header from tests.test_admin_auth import FakeRedis from tests.test_game import _setup_two_player_session @pytest.fixture def admin_token(client): get_settings.cache_clear() fake_redis = FakeRedis() with patch("app.modules.admin.auth.get_redis", new_callable=AsyncMock) as mock_redis: mock_redis.return_value = fake_redis req = client.post("/api/v1/auth/admin/otp/request", json={"telegram_id": 999888777}) code = req.json()["code"] verify = client.post( "/api/v1/auth/admin/otp/verify", json={"username": "Mihaham", "code": code}, ) yield verify.json()["access_token"] get_settings.cache_clear() def _admin_headers(token: str) -> dict: return {"Authorization": f"Bearer {token}"} def test_admin_list_games_and_filters(client, admin_token): session_id, token1, _, _ = _setup_two_player_session(client) started = client.post( f"/api/v1/sessions/{session_id}/games", json={"plugin_id": "memo"}, headers=auth_header(token1), ) game_id = started.json()["game_id"] r = client.get("/api/v1/admin/games", headers=_admin_headers(admin_token)) assert r.status_code == 200 assert r.json()["total"] >= 1 r2 = client.get( "/api/v1/admin/games", params={"plugin_id": "memo", "status": "playing"}, headers=_admin_headers(admin_token), ) assert r2.status_code == 200 assert all(g["plugin_id"] == "memo" for g in r2.json()["games"]) r3 = client.get(f"/api/v1/admin/games/{game_id}", headers=_admin_headers(admin_token)) assert r3.status_code == 200 assert r3.json()["game"]["id"] == game_id assert len(r3.json()["participants"]) == 2 r4 = client.get(f"/api/v1/admin/games/{game_id}/log", headers=_admin_headers(admin_token)) assert r4.status_code == 200 assert "timeline" in r4.json() def test_admin_game_not_found(client, admin_token): fake = "00000000-0000-0000-0000-000000000099" r = client.get(f"/api/v1/admin/games/{fake}", headers=_admin_headers(admin_token)) assert r.status_code == 404 r2 = client.get(f"/api/v1/admin/games/{fake}/log", headers=_admin_headers(admin_token)) assert r2.status_code == 404 def test_admin_list_sessions(client, admin_token): _setup_two_player_session(client) r = client.get("/api/v1/admin/sessions", headers=_admin_headers(admin_token)) assert r.status_code == 200 assert r.json()["total"] >= 1 session_id = r.json()["sessions"][0]["id"] r2 = client.get( f"/api/v1/admin/sessions/{session_id}/games", headers=_admin_headers(admin_token), ) assert r2.status_code == 200 assert "games" in r2.json() def test_admin_session_not_found(client, admin_token): fake = "00000000-0000-0000-0000-000000000099" r = client.get( f"/api/v1/admin/sessions/{fake}/games", headers=_admin_headers(admin_token), ) assert r.status_code == 404 def test_admin_otp_rate_limit(client): get_settings.cache_clear() fake_redis = FakeRedis() fake_redis.store["admin:otp:rate:999888777"] = "4" with patch("app.modules.admin.auth.get_redis", new_callable=AsyncMock) as mock_redis: mock_redis.return_value = fake_redis r = client.post("/api/v1/auth/admin/otp/request", json={"telegram_id": 999888777}) assert r.status_code == 429 get_settings.cache_clear() def test_admin_otp_locked(client): get_settings.cache_clear() fake_redis = FakeRedis() with patch("app.modules.admin.auth.get_redis", new_callable=AsyncMock) as mock_redis: mock_redis.return_value = fake_redis req = client.post("/api/v1/auth/admin/otp/request", json={"telegram_id": 999888777}) code = req.json()["code"] fake_redis.store["admin:otp:attempts:999888777"] = "4" r = client.post( "/api/v1/auth/admin/otp/verify", json={"username": "Mihaham", "code": code}, ) assert r.status_code == 401 get_settings.cache_clear() def test_admin_invalid_bearer(client): r = client.get("/api/v1/admin/games", headers={"Authorization": "Bearer not-a-jwt"}) assert r.status_code == 403