Audit fixes 2026-10-03: seed keeps admin edits, plan cache, refresh limit, payment and tutor guards
- seed_content no longer undoes admin work: a SeedRecord ledger (content/seed_sync.py) stops deleted/renamed/re-worded content from being recreated and keeps admin-edited lesson text; new rows in the seed files still arrive (migration content.0010) - plan list cache is invalidated on Plan save (receiver used a stale 'v1' key) - token refresh per-IP cap 30/hour -> 600/hour (TOKEN_REFRESH_IP_RATE) - a failed gateway result only changes a pending order (forged NOK no longer overwrites refunded/canceled) - tutor gets the correct answer only after the student has answered - support reply notification moved to a TicketMessage post_save signal - global daily cap on paid AI questions (AI_GLOBAL_DAILY_MAX, default 3000) - Bazaar verify uses only the order's SKU; bazaar orders require a SKU - /api/health warns on the committed signing key; deploy.sh saves local commits; 400 for bad ?unit= / ?period=; account delete keeps the referrer's Conversion - earlier today: no stub answers outside DEBUG, ?grade= removed, logged fallbacks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -133,6 +133,12 @@ def apply_order_payment_result(*, order: Order, gateway: str, gateway_ref_id: st
|
||||
# از قبل paid شده را به عقب برگرداند؛ چنین callback ای idempotent/no-op است.
|
||||
if already_paid and new_status != 'paid':
|
||||
is_duplicate = True
|
||||
# نتیجهی ناموفق فقط سفارشِ «در انتظار» را عوض میکند. کالبکِ زرینپال بیاحراز است و
|
||||
# authority از هدرِ Location ِ /pay خوانده میشود؛ تا ۲۰۲۶-۱۰-۰۳ یک Status=NOKِ جعلی سفارشِ
|
||||
# «بازپرداختشده» یا «لغوشده» را «ناموفق» میکرد و ردِ بازپرداخت از وضعیتِ سفارش میرفت
|
||||
# (بازتولید شد). «paid» عمداً از این قاعده بیرون است: کسی که پول داده باید چیزش را بگیرد.
|
||||
if new_status != 'paid' and old_status != 'pending':
|
||||
is_duplicate = True
|
||||
|
||||
# reuse رفرنسِ پرداخت روی سفارش دیگر هم duplicate/no-op در نظر گرفته میشود.
|
||||
if gateway_ref_id and Transaction.objects.filter(
|
||||
|
||||
@@ -66,3 +66,31 @@ class BazaarPackTests(TestCase):
|
||||
'userId': str(self.user.id), 'amount': 1, 'provider': 'bazaar', 'planId': '999999',
|
||||
}, format='json', HTTP_X_INTERNAL_KEY='internal-test-key')
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
|
||||
@override_settings(INTERNAL_SERVICE_KEY='internal-test-key')
|
||||
class BazaarSkuComesFromTheOrderTests(TestCase):
|
||||
"""تأییدِ بازار SKU را فقط از سفارش میگیرد، نه از کلاینت (۲۰۲۶-۱۰-۰۳)."""
|
||||
|
||||
def setUp(self):
|
||||
cache.clear()
|
||||
self.user = User.objects.create(phone='09120007102', is_guest=False)
|
||||
self.client = APIClient()
|
||||
self.client.credentials(HTTP_AUTHORIZATION=f'Bearer {issue_access_token(self.user.id)}')
|
||||
|
||||
def test_a_bazaar_order_needs_a_sku(self):
|
||||
response = APIClient().post('/api/payment/orders', {
|
||||
'userId': str(self.user.id), 'amount': 1000, 'provider': 'bazaar',
|
||||
}, format='json', HTTP_X_INTERNAL_KEY='internal-test-key')
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
def test_verify_never_uses_the_clients_sku(self):
|
||||
order = Order.objects.create(user_id=self.user.id, amount=259000, provider='bazaar',
|
||||
idempotency_key='no-sku-1')
|
||||
with patch('payment.bazaar.verify_purchase') as verify:
|
||||
response = self.client.post('/api/payment/bazaar/verify', {
|
||||
'orderId': str(order.id), 'productSku': 'cheapest', 'purchaseToken': 't'}, format='json')
|
||||
self.assertEqual(response.status_code, 400)
|
||||
verify.assert_not_called()
|
||||
order.refresh_from_db()
|
||||
self.assertEqual(order.status, 'pending')
|
||||
|
||||
@@ -175,3 +175,24 @@ class OrderAdminTests(_Base):
|
||||
page = self.admin.get(f'/admin/payment/order/{order.pk}/change/')
|
||||
self.assertEqual(page.status_code, 200)
|
||||
self.assertNotContains(page, 'name="status"')
|
||||
|
||||
|
||||
class ForgedFailureCallbackTests(TestCase):
|
||||
"""
|
||||
کالبکِ زرینپال بیاحراز است. Status=NOKِ جعلی نباید سفارشِ بازپرداختشده یا لغوشده را
|
||||
«ناموفق» کند (تا ۲۰۲۶-۱۰-۰۳ میکرد و ردِ بازپرداخت از وضعیتِ سفارش میرفت).
|
||||
"""
|
||||
|
||||
def test_nok_does_not_overwrite_refunded_or_canceled(self):
|
||||
for status in ('refunded', 'canceled'):
|
||||
order = Order.objects.create(user_id=1, amount=1000, status=status,
|
||||
gateway_authority=f'A-{status}', idempotency_key=f'k-{status}')
|
||||
APIClient().get('/api/payment/zarinpal/callback', {'Authority': f'A-{status}', 'Status': 'NOK'})
|
||||
order.refresh_from_db()
|
||||
self.assertEqual(order.status, status)
|
||||
|
||||
def test_nok_still_fails_a_pending_order(self):
|
||||
order = Order.objects.create(user_id=1, amount=1000, gateway_authority='A-p', idempotency_key='k-p')
|
||||
APIClient().get('/api/payment/zarinpal/callback', {'Authority': 'A-p', 'Status': 'NOK'})
|
||||
order.refresh_from_db()
|
||||
self.assertEqual(order.status, 'failed')
|
||||
|
||||
@@ -100,6 +100,9 @@ class CreateOrderView(APIView):
|
||||
serializer.is_valid(raise_exception=True)
|
||||
v = serializer.validated_data
|
||||
|
||||
if v.get('provider') == 'bazaar' and not v.get('bazaarProductSku'):
|
||||
raise ValidationError({'bazaarProductSku': 'سفارشِ بازار بدونِ SKU ساخته نمیشود.'})
|
||||
|
||||
plan = None
|
||||
if v.get('planId'):
|
||||
from subscription.models import Plan
|
||||
@@ -325,8 +328,12 @@ class BazaarVerifyPurchaseView(APIView):
|
||||
Order, pk=v['orderId'], user_id=request.user.id, provider='bazaar',
|
||||
)
|
||||
|
||||
# SKU فقط از خودِ سفارش. تا ۲۰۲۶-۱۰-۰۳ اگر سفارش SKU نداشت، SKUِ فرستادهی کلاینت سنجیده
|
||||
# میشد؛ یعنی توکنِ خریدِ ارزانترین محصول سفارشِ گرانترین پلن را «paid» میکرد.
|
||||
if not order_lookup.bazaar_product_sku:
|
||||
raise ValidationError({'orderId': 'این سفارش محصولِ بازار ندارد.'})
|
||||
result = bazaar.verify_purchase(
|
||||
product_sku=order_lookup.bazaar_product_sku or v['productSku'],
|
||||
product_sku=order_lookup.bazaar_product_sku,
|
||||
purchase_token=v['purchaseToken'],
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user