Spaces:
Sleeping
Sleeping
fix: add tests for auth failure without API key and ensure rate limiting applies regardless of auth
Browse files- tests/test_commend.py +32 -0
tests/test_commend.py
CHANGED
|
@@ -80,3 +80,35 @@ def test_commend_post_error_envelope_format(client: TestClient) -> None:
|
|
| 80 |
if isinstance(detail, dict):
|
| 81 |
assert "code" in detail
|
| 82 |
assert "message" in detail
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
if isinstance(detail, dict):
|
| 81 |
assert "code" in detail
|
| 82 |
assert "message" in detail
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def test_commend_auth_fail_closed_without_key(client: TestClient) -> None:
|
| 86 |
+
"""Without COMMEND_API_KEY set, protected endpoints should reject (fail-closed)."""
|
| 87 |
+
response = client.post(
|
| 88 |
+
"/api/commend/generate",
|
| 89 |
+
json={
|
| 90 |
+
"videoUrl": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
| 91 |
+
"language": "English",
|
| 92 |
+
"commentStyle": "supportive",
|
| 93 |
+
},
|
| 94 |
+
)
|
| 95 |
+
# With COMMEND_REQUIRE_AUTH=true (default) and no key, expect 429 or 503
|
| 96 |
+
assert response.status_code in (429, 503)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def test_commend_rate_limit_independent_of_auth(client: TestClient) -> None:
|
| 100 |
+
"""Rate limiting should apply regardless of auth configuration."""
|
| 101 |
+
# Send multiple rapid requests — rate limit should apply even without auth key
|
| 102 |
+
statuses = []
|
| 103 |
+
for _ in range(15):
|
| 104 |
+
response = client.post(
|
| 105 |
+
"/api/commend/generate",
|
| 106 |
+
json={
|
| 107 |
+
"videoUrl": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
| 108 |
+
"language": "English",
|
| 109 |
+
"commentStyle": "supportive",
|
| 110 |
+
},
|
| 111 |
+
)
|
| 112 |
+
statuses.append(response.status_code)
|
| 113 |
+
# Should see at least one 429 in the batch (rate limit is 10/min)
|
| 114 |
+
assert 429 in statuses, f"Expected 429 in statuses but got: {set(statuses)}"
|