Subir todos los cambios recientes a la rama principal
This commit is contained in:
54
tests/test_tenant_middleware.py
Normal file
54
tests/test_tenant_middleware.py
Normal file
@@ -0,0 +1,54 @@
|
||||
import sys
|
||||
import os
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from app.models.tenant import Tenant
|
||||
from app.core.database import get_db
|
||||
|
||||
# Add the project root to PYTHONPATH
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../')))
|
||||
|
||||
from app.main import app
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
async def setup_test_data():
|
||||
async with get_db() as db:
|
||||
# Insert a valid tenant
|
||||
valid_tenant = Tenant(id="valid-tenant-id", slug="valid-tenant", status="active")
|
||||
await db.add(valid_tenant)
|
||||
await db.commit()
|
||||
|
||||
# Insert an invalid tenant
|
||||
invalid_tenant = Tenant(id="invalid-tenant-id", slug="invalid-tenant", status="inactive")
|
||||
await db.add(invalid_tenant)
|
||||
await db.commit()
|
||||
|
||||
yield
|
||||
|
||||
# Cleanup
|
||||
await db.delete(valid_tenant)
|
||||
await db.delete(invalid_tenant)
|
||||
await db.commit()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_request_without_tenant(setup_test_data):
|
||||
async with AsyncClient(app=app, base_url="http://testserver") as client:
|
||||
response = await client.get("/v1/categories/")
|
||||
assert response.status_code == 400
|
||||
assert response.json()["detail"] == "Tenant information required (X-Tenant-ID or X-Tenant-Slug header)"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_request_with_invalid_tenant(setup_test_data):
|
||||
async with AsyncClient(app=app, base_url="http://testserver") as client:
|
||||
headers = {"X-Tenant-ID": "invalid-tenant-id"}
|
||||
response = await client.get("/v1/categories/", headers=headers)
|
||||
assert response.status_code == 404
|
||||
assert response.json()["detail"] == "Tenant not found or inactive"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_request_with_valid_tenant(setup_test_data):
|
||||
async with AsyncClient(app=app, base_url="http://testserver") as client:
|
||||
headers = {"X-Tenant-ID": "valid-tenant-id"}
|
||||
response = await client.get("/v1/categories/", headers=headers)
|
||||
assert response.status_code == 200
|
||||
Reference in New Issue
Block a user