- Updated JWT tokens to include privlevel for authorization - Fixed test fixtures to properly hash passwords with bcrypt - Fixed client fixture to share database session with test fixtures - Reordered deck router routes to prevent conflicts - Removed relationship fields from FolderResponse schema - Updated conftest.py with proper async session management
89 lines
3.0 KiB
Python
89 lines
3.0 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("/api/v1/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("/api/v1/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(
|
|
"/api/v1/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(
|
|
"/api/v1/admin/bans",
|
|
json={
|
|
"user_id": test_user.id,
|
|
"reason": "Test ban",
|
|
},
|
|
headers=admin_headers,
|
|
)
|
|
|
|
# List bans
|
|
response = await client.get("/api/v1/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(
|
|
"/api/v1/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"/api/v1/admin/bans/{ban_id}/unban",
|
|
headers=admin_headers,
|
|
)
|
|
assert response.status_code == 200
|
|
assert response.json()["message"] == "User unbanned successfully"
|