Spaces:
Sleeping
Sleeping
File size: 4,042 Bytes
fb6be88 d12895a fb6be88 8bcdcab fb6be88 be8f2ab fb6be88 f89ed85 8bcdcab be8f2ab 8bcdcab f89ed85 8bcdcab be8f2ab f89ed85 be8f2ab 8bcdcab f89ed85 be8f2ab de799a3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | """Tests for Crown Commend endpoints."""
from __future__ import annotations
from fastapi.testclient import TestClient
def test_commend_health(client: TestClient) -> None:
"""Commend health endpoint should return status information."""
response = client.get("/api/commend/health")
assert response.status_code == 200
data = response.json()
assert "status" in data
assert "geminiConfigured" in data
assert "youtubeConfigured" in data
def test_commend_styles(client: TestClient) -> None:
"""Styles endpoint should return available comment styles."""
response = client.get("/api/commend/styles")
assert response.status_code == 200
data = response.json()
assert "styles" in data
assert len(data["styles"]) > 0
def test_commend_generate_rejects_invalid_url(client: TestClient) -> None:
"""Generate endpoint should reject a non-YouTube URL."""
response = client.post(
"/api/commend/generate",
json={
"videoUrl": "https://example.com",
"language": "English",
"commentStyle": "supportive",
},
)
# 400 (invalid url), 401 (auth), 422 (validation), 503 (auth not configured)
assert response.status_code in (400, 401, 422, 429, 503)
def test_commend_generate_rejects_empty_url(client: TestClient) -> None:
"""Generate endpoint should reject empty URL."""
response = client.post(
"/api/commend/generate",
json={
"videoUrl": "",
"language": "English",
"commentStyle": "supportive",
},
)
# 422 (Pydantic min_length), 401 (auth), 503 (auth not configured)
assert response.status_code in (401, 422, 429, 503)
def test_commend_post_disabled_by_default(client: TestClient) -> None:
"""Post endpoint should be disabled by default (feature flag)."""
response = client.post(
"/api/commend/post",
json={
"videoUrl": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"commentText": "Test comment",
},
)
# 401 (auth), 403 (feature flag disabled), 503 (auth not configured)
assert response.status_code in (401, 403, 429, 503)
def test_commend_post_error_envelope_format(client: TestClient) -> None:
"""Error responses should use {code, message} envelope format."""
response = client.post(
"/api/commend/post",
json={
"videoUrl": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"commentText": "Test comment",
},
)
if response.status_code in (401, 403, 503):
data = response.json()
detail = data.get("detail", {})
if isinstance(detail, dict):
assert "code" in detail
assert "message" in detail
def test_commend_auth_fail_closed_without_key(client: TestClient) -> None:
"""Without COMMEND_API_KEY set, protected endpoints should reject (fail-closed)."""
response = client.post(
"/api/commend/generate",
json={
"videoUrl": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"language": "English",
"commentStyle": "supportive",
},
)
# With COMMEND_REQUIRE_AUTH=true (default) and no key, expect 429 or 503
assert response.status_code in (429, 503)
def test_commend_rate_limit_independent_of_auth(client: TestClient) -> None:
"""Rate limiting should apply regardless of auth configuration."""
# Send multiple rapid requests — rate limit should apply even without auth key
statuses = []
for _ in range(15):
response = client.post(
"/api/commend/generate",
json={
"videoUrl": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"language": "English",
"commentStyle": "supportive",
},
)
statuses.append(response.status_code)
# Should see at least one 429 in the batch (rate limit is 10/min)
assert 429 in statuses, f"Expected 429 in statuses but got: {set(statuses)}"
|