- two plans in sokhan/core/plans.py: free (10-min files, 30 min/month)
and pro (180-min files, 1200 min/month, only while unexpired);
expired subscribers fall back to free limits automatically
- both upload paths enforce the gates: POST /jobs probes duration before
creating the job, POST /v1/audio/transcriptions after the inline decode;
over-limit uploads answer HTTP 402 with detail.code file_too_long or
quota_exceeded and the stored audio is deleted on rejection
- operator API: GET/POST /admin/users/{email}/plan behind a constant-time
X-Admin-Key (503 unless ADMIN_API_KEY is set); pro grants take N days
and renewals stack on the remaining time
- GET /plans publishes the catalogue; GET /quota reports plan, expiry and
the per-file cap
- bot: /upgrade renders the catalogue, /quota is plan-aware, voices longer
than the cap are refused before download, 402 codes map to dedicated
Persian messages (FileTooLongError vs QuotaError)
- web: upload screen shows plan + per-file cap and renders targeted 402
messages (fa/en); ApiError now parses structured detail codes
- alembic 0004 adds users.plan / plan_expires_at; .env.example, README and
CHANGELOG 1.4.0 updated; stale __version__ 1.1.0 -> 1.4.0
- tests: 18 backend plan/subscription tests + 4 bot client tests
(httpx.MockTransport); suite 176 passed, ruff + mypy clean
91 lines
2.9 KiB
Python
91 lines
2.9 KiB
Python
"""402 code mapping and plan catalogue parsing in the backend client."""
|
|
|
|
import httpx
|
|
import pytest
|
|
from sokhan_bot.client import BackendError, FileTooLongError, QuotaError, SokhanClient
|
|
from sokhan_bot.config import BotSettings
|
|
|
|
|
|
def _client(handler) -> SokhanClient:
|
|
settings = BotSettings(
|
|
telegram_bot_token="t",
|
|
api_base_url="http://api.test",
|
|
web_base_url="http://web.test",
|
|
)
|
|
return SokhanClient(settings, transport=httpx.MockTransport(handler))
|
|
|
|
|
|
def test_upload_402_quota_exceeded():
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
return httpx.Response(
|
|
402,
|
|
json={"detail": {"code": "quota_exceeded", "used_minutes": 30.0, "limit_minutes": 30}},
|
|
)
|
|
|
|
with _client(handler) as client:
|
|
with pytest.raises(QuotaError):
|
|
client.upload_audio("tok", "voice.ogg", b"bytes")
|
|
|
|
|
|
def test_upload_402_file_too_long():
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
return httpx.Response(
|
|
402,
|
|
json={
|
|
"detail": {
|
|
"code": "file_too_long",
|
|
"plan": "free",
|
|
"file_minutes": 14.2,
|
|
"max_file_minutes": 10,
|
|
}
|
|
},
|
|
)
|
|
|
|
with _client(handler) as client:
|
|
with pytest.raises(FileTooLongError) as excinfo:
|
|
client.upload_audio("tok", "voice.ogg", b"bytes")
|
|
assert excinfo.value.max_file_minutes == 10
|
|
assert excinfo.value.file_minutes == pytest.approx(14.2)
|
|
|
|
|
|
def test_upload_other_failure_is_plain_backend_error():
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
return httpx.Response(500, json={"detail": "boom"})
|
|
|
|
with _client(handler) as client:
|
|
with pytest.raises(BackendError) as excinfo:
|
|
client.upload_audio("tok", "voice.ogg", b"bytes")
|
|
assert not isinstance(excinfo.value, QuotaError)
|
|
assert not isinstance(excinfo.value, FileTooLongError)
|
|
|
|
|
|
def test_get_plans_parses_catalogue():
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
assert request.url.path == "/plans"
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"plans": [
|
|
{
|
|
"code": "free",
|
|
"title_fa": "رایگان",
|
|
"title_en": "Free",
|
|
"max_file_minutes": 10,
|
|
"monthly_minutes": 30,
|
|
},
|
|
{
|
|
"code": "pro",
|
|
"title_fa": "حرفهای",
|
|
"title_en": "Pro",
|
|
"max_file_minutes": 180,
|
|
"monthly_minutes": 1200,
|
|
},
|
|
]
|
|
},
|
|
)
|
|
|
|
with _client(handler) as client:
|
|
plans = client.get_plans()
|
|
assert [plan["code"] for plan in plans] == ["free", "pro"]
|
|
assert plans[1]["max_file_minutes"] == 180
|