accounts.0008 drops the columns and refuses to run while guest rows exist. Docs describe the current state; tests that only proved guest behaviour are gone. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
57 lines
3.1 KiB
Python
57 lines
3.1 KiB
Python
"""
|
|
حفاظهای بازبینیِ امنیتیِ پیش از تحویل (بستهها و پلن). هر تست رفتاری را نگه میدارد که پیشتر غلط بود.
|
|
"""
|
|
from django.contrib.auth import get_user_model
|
|
from django.core.cache import cache
|
|
from django.test import TestCase
|
|
from rest_framework.test import APIClient
|
|
|
|
from accounts.authentication import issue_access_token
|
|
from accounts.models import User
|
|
from payment.models import Order
|
|
from payment.services import apply_order_payment_result
|
|
|
|
from .models import Plan
|
|
from .services import credit_balance
|
|
|
|
|
|
class PaidPackWithDeletedPlanTests(TestCase):
|
|
"""پرداختِ سفارشی که پلنش بعد از ساخت پاک شده بود «paid» میشد بیهیچ اعتبار و بیهیچ لاگی."""
|
|
|
|
def test_order_snapshot_is_granted_even_if_plan_is_gone(self):
|
|
cache.clear()
|
|
user = User.objects.create(phone='09120009902')
|
|
app = APIClient()
|
|
app.credentials(HTTP_AUTHORIZATION=f'Bearer {issue_access_token(user.id)}')
|
|
pack = Plan.objects.get(kind='pack', question_credits=20)
|
|
order = Order.objects.get(pk=app.post('/api/subscription/purchase', {'planId': str(pack.id)},
|
|
format='json').json()['orderId'])
|
|
pack.delete()
|
|
apply_order_payment_result(order=order, gateway='zarinpal', gateway_ref_id='GONE-1', new_status='paid', event='t')
|
|
self.assertEqual(credit_balance(user.id), 20)
|
|
|
|
def test_nothing_to_grant_is_logged_as_error(self):
|
|
order = Order.objects.create(user_id=1, amount=1000, plan_id=999999, idempotency_key='sec-noplan')
|
|
with self.assertLogs('subscription.services', level='ERROR'):
|
|
apply_order_payment_result(order=order, gateway='zarinpal', gateway_ref_id='GONE-2', new_status='paid',
|
|
event='t')
|
|
|
|
|
|
class PlanCurrencyIsTomanOnlyTests(TestCase):
|
|
"""اپ هر قیمتی را «تومان» نشان میدهد و currencyِ پلن بیتغییر به زرینپال میرود؛ IRR یعنی یکدهمِ قیمت."""
|
|
|
|
def test_admin_refuses_irr(self):
|
|
admin = get_user_model().objects.create_superuser('root', 'r@example.com', 'pw-for-tests-only')
|
|
self.client.force_login(admin)
|
|
pack = Plan.objects.get(kind='pack', question_credits=20)
|
|
data = {f: getattr(pack, f) for f in ('title', 'price', 'discount_percent', 'duration_days', 'badge',
|
|
'ai_daily_quota', 'kind', 'question_credits', 'hearts_days',
|
|
'bazaar_sku')}
|
|
data.update(currency='IRR', features='[]', on_sale='on')
|
|
page = self.client.post(f'/admin/subscription/plan/{pack.pk}/change/', data)
|
|
self.assertEqual(page.status_code, 200) # فرم با خطا برگشت
|
|
pack.refresh_from_db()
|
|
self.assertEqual(pack.currency, 'IRT')
|
|
data['currency'] = 'IRT'
|
|
self.assertEqual(self.client.post(f'/admin/subscription/plan/{pack.pk}/change/', data).status_code, 302)
|