126 lines
3.4 KiB
Python
126 lines
3.4 KiB
Python
from fastapi.testclient import TestClient
|
|
import sys
|
|
import os
|
|
|
|
sys.path.append(
|
|
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
)
|
|
|
|
from src.app.main import app
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
def test_health_endpoint():
|
|
response = client.get("/healthz")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
print("Health check response:", data)
|
|
assert "status" in data
|
|
assert data["status"] == "healthy"
|
|
|
|
|
|
def test_readiness_endpoint():
|
|
response = client.get("/readiness")
|
|
assert response.status_code == 200
|
|
|
|
|
|
def test_api_documentation():
|
|
response = client.get("/docs")
|
|
assert response.status_code == 200
|
|
|
|
|
|
def test_root_redirect():
|
|
response = client.get("/")
|
|
assert response.status_code == 200
|
|
|
|
|
|
def test_generate_email_missing_fields():
|
|
incomplete_payload = {"contact": ""}
|
|
|
|
response = client.post("/api/v1/generate_email", json=incomplete_payload)
|
|
assert response.status_code == 422
|
|
|
|
|
|
def test_generate_email_basic_validation():
|
|
valid_payload = {"contact": "Test Person"}
|
|
|
|
response = client.post("/api/v1/generate_email", json=valid_payload)
|
|
assert response.status_code == 422
|
|
|
|
|
|
def test_generate_email_success():
|
|
payload = {
|
|
"contact": "Помящий Никита",
|
|
"position": "Технический директор",
|
|
"company_name": "FIVE",
|
|
"segment": "маркетинговое агентство",
|
|
}
|
|
|
|
response = client.post("/api/v1/generate_email", json=payload)
|
|
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
assert "subject" in data
|
|
assert "body" in data
|
|
assert "meta" in data
|
|
else:
|
|
print(f"Response: {response.status_code}, {response.text}")
|
|
|
|
|
|
def test_generate_email_with_email():
|
|
payload = {
|
|
"contact": "Помящий Никита",
|
|
"position": "Технический директор",
|
|
"company_name": "FIVE",
|
|
"segment": "маркетинговое агентство",
|
|
"email": "nikita@five.agency",
|
|
"locale": "ru",
|
|
}
|
|
|
|
response = client.post("/api/v1/generate_email", json=payload)
|
|
assert response.status_code in [200, 500]
|
|
|
|
|
|
def test_api_status():
|
|
response = client.get("/api/v1/status")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["status"] == "operational"
|
|
|
|
|
|
def test_invalid_json():
|
|
response = client.post(
|
|
"/api/v1/generate_email",
|
|
content="invalid json".encode("utf-8"),
|
|
headers={"Content-Type": "application/json"},
|
|
)
|
|
assert response.status_code == 422
|
|
|
|
|
|
def test_admin_endpoints_without_auth():
|
|
response = client.get("/api/v1/admin/knowledge-base/stats")
|
|
assert response.status_code == 401
|
|
|
|
response = client.post("/api/v1/admin/ingest")
|
|
assert response.status_code == 401
|
|
|
|
|
|
def test_admin_endpoints_with_auth():
|
|
from src.app.config import settings
|
|
|
|
headers = {"Authorization": f"Bearer {settings.api_secret_key}"}
|
|
|
|
response = client.get("/api/v1/admin/knowledge-base/stats", headers=headers)
|
|
assert response.status_code in [200, 500]
|
|
|
|
response = client.post("/api/v1/admin/ingest", headers=headers)
|
|
assert response.status_code in [200, 500]
|
|
|
|
|
|
def test_admin_endpoints_with_wrong_auth():
|
|
headers = {"Authorization": "Bearer wrong_token"}
|
|
|
|
response = client.get("/api/v1/admin/knowledge-base/stats", headers=headers)
|
|
assert response.status_code == 403
|