/
DaniilSk
/
DesignLab
Обзор
Документация
Войти
/
DaniilSk
/
DesignLab
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
backend/app/tests/test_api.py
79 строк
3 KB
Даниил
feat(admin-auth): add protected admin api
13 июл 2026, 23:41
13 июл 2026, 23:41
406f05e
Код
Авторство
О чём код?
import pytest from httpx import ASGITransport, AsyncClient from app.main import app @pytest.mark.asyncio async def test_health_check(): async with AsyncClient(app=app, base_url="http://test") as client: response = await client.get("/api/v1/health") assert response.status_code == 200 assert response.json()["status"] == "ok" @pytest.mark.asyncio async def test_home_contract(): transport = ASGITransport(app=app, raise_app_exceptions=False) async with AsyncClient(transport=transport, base_url="http://test") as client: response = await client.get("/api/v1/home") assert response.status_code in {200, 500} def test_referral_rules_documented(): from app.services.referrals import ReferralService assert hasattr(ReferralService, "claim") @pytest.mark.asyncio async def test_profile_requires_auth(): async with AsyncClient(app=app, base_url="http://test") as client: response = await client.get("/api/v1/profile") assert response.status_code == 401 @pytest.mark.asyncio async def test_loyalty_requires_auth(): async with AsyncClient(app=app, base_url="http://test") as client: response = await client.get("/api/v1/loyalty") assert response.status_code == 401 @pytest.mark.asyncio async def test_reward_claim_requires_auth(): async with AsyncClient(app=app, base_url="http://test") as client: response = await client.post("/api/v1/loyalty/rewards/00000000-0000-4000-8000-000000000001/claim") assert response.status_code == 401 def test_admin_credentials_success(monkeypatch): from app.api.v1.admin import pwd_context, verify_admin_credentials from app.core.config import get_settings monkeypatch.setenv("ADMIN_USERNAME", "admin") monkeypatch.setenv("ADMIN_PASSWORD_HASH", pwd_context.hash("secret")) get_settings.cache_clear() verify_admin_credentials("admin", "secret") get_settings.cache_clear() def test_admin_credentials_fail(monkeypatch): from fastapi import HTTPException from app.api.v1.admin import pwd_context, verify_admin_credentials from app.core.config import get_settings monkeypatch.setenv("ADMIN_USERNAME", "admin") monkeypatch.setenv("ADMIN_PASSWORD_HASH", pwd_context.hash("secret")) get_settings.cache_clear() with pytest.raises(HTTPException): verify_admin_credentials("admin", "wrong") get_settings.cache_clear() @pytest.mark.asyncio async def test_admin_dashboard_requires_auth(): async with AsyncClient(app=app, base_url="http://test") as client: response = await client.get("/api/v1/admin/dashboard") assert response.status_code == 401