Files

89 lines
2.9 KiB
Python

"""
Tests for admin endpoints.
"""
import pytest
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.models import User, Ban
from app.core.security import hash_password
class TestAdminEndpoints:
"""Test admin endpoints."""
@pytest.mark.asyncio
async def test_list_users_admin(self, client: AsyncClient, admin_headers: dict):
"""Test listing users as admin."""
response = await client.get("/admin/users", headers=admin_headers)
print(f"\nDEBUG: status={response.status_code}, body={response.text}")
if response.status_code != 200:
print(f"DEBUG: response.json()={response.json()}")
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
@pytest.mark.asyncio
async def test_list_users_non_admin(self, client: AsyncClient, auth_headers: dict):
"""Test listing users as non-admin."""
response = await client.get("/admin/users", headers=auth_headers)
assert response.status_code == 403
@pytest.mark.asyncio
async def test_create_ban(self, client: AsyncClient, admin_headers: dict, test_user: User):
"""Test creating a ban."""
response = await client.post(
"/admin/bans",
json={
"user_id": test_user.id,
"reason": "Test ban reason",
},
headers=admin_headers,
)
assert response.status_code == 200
data = response.json()
assert data["reason"] == "Test ban reason"
assert data["active"] == True
@pytest.mark.asyncio
async def test_list_bans(self, client: AsyncClient, admin_headers: dict, test_user: User):
"""Test listing bans."""
# Create a ban first
await client.post(
"/admin/bans",
json={
"user_id": test_user.id,
"reason": "Test ban",
},
headers=admin_headers,
)
# List bans
response = await client.get("/admin/bans", headers=admin_headers)
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
assert len(data) > 0
@pytest.mark.asyncio
async def test_unban_user(self, client: AsyncClient, admin_headers: dict, test_user: User):
"""Test unbanning a user."""
# Create a ban first
ban_response = await client.post(
"/admin/bans",
json={
"user_id": test_user.id,
"reason": "Test ban",
},
headers=admin_headers,
)
ban_id = ban_response.json()["id"]
# Unban user
response = await client.post(
f"/admin/bans/{ban_id}/unban",
headers=admin_headers,
)
assert response.status_code == 200
assert response.json()["message"] == "User unbanned successfully"