Files
post-moderation/ai-moderation/tests/test_text_api.py
2026-10-23 00:12:34 +03:00

46 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from app.config.settings import settings
URL = "/api/v1/moderation/text"
def test_clean_text_contract(client):
response = client.post(URL, json={"text": "Потерял кошелёк в парке Горького, нашедшему вознаграждение"})
assert response.status_code == 200
# Поля, которые читает ModerationResponse в ReFound
assert response.json() == {"approved": True, "score": 0.01, "reason": "OK"}
def test_profanity_is_rejected(client):
response = client.post(URL, json={"text": "Нашёл кошелёк, бля"})
body = response.json()
assert response.status_code == 200
assert body["approved"] is False
assert body["reason"] == "PROFANITY"
def test_toxic_text_is_rejected(client, text_classifier):
text_classifier.score = 0.99
body = client.post(URL, json={"text": "какой-то текст"}).json()
assert body == {"approved": False, "score": 0.99, "reason": "TOXIC"}
def test_whitespace_text_is_400(client):
response = client.post(URL, json={"text": " "})
assert response.status_code == 400
assert response.json()["code"] == "INVALID_TEXT"
def test_empty_and_too_long_text_are_422(client):
assert client.post(URL, json={"text": ""}).status_code == 422
too_long = "а" * (settings.TEXT_MAX_LENGTH + 1)
assert client.post(URL, json={"text": too_long}).status_code == 422
def test_large_json_body_is_413(client):
response = client.post(
URL,
content=b'{"text": "' + b"a" * (settings.MAX_REQUEST_BYTES + 10) + b'"}',
headers={"content-type": "application/json"}
)
assert response.status_code == 413