Harden auth, AI tutor, payments, and subscriptions
This commit is contained in:
@@ -1,3 +1,54 @@
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
# Create your tests here.
|
||||
from accounts.authentication import issue_access_token
|
||||
from accounts.models import User
|
||||
from .models import DiscountCode
|
||||
|
||||
|
||||
class ValidateDiscountPermissionTests(TestCase):
|
||||
"""
|
||||
🔒 اثبات فیکسِ امنیتی: ValidateDiscountView قبلاً permission_classes
|
||||
نداشت (یعنی دیفالتِ پروژه AllowAny اعمال میشد) و یک کلاینتِ بدونِ
|
||||
احرازهویت میتونست بینهایت کد رو probe کنه و valid/type/value رو
|
||||
ببینه — درحالیکه RedeemDiscountView (اندپوینتِ واقعیِ مصرف) از قبل
|
||||
IsAuthenticated داره. این تست هم رفتارِ ۴۰۱ برای anonymous، هم رفتارِ
|
||||
صحیحِ اعتبارسنجی برای کاربرِ لاگینکرده رو تضمین میکنه.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
self.user = User.objects.create(device_id='device-validate-1', is_guest=True, name='')
|
||||
self.token = issue_access_token(self.user.id)
|
||||
self.discount = DiscountCode.objects.create(
|
||||
code='SUMMER10', type='percent', value=10, max_uses=5, expires_at=None,
|
||||
)
|
||||
|
||||
def test_unauthenticated_request_is_rejected(self):
|
||||
response = self.client.get('/api/discount/validate', {'code': self.discount.code})
|
||||
self.assertEqual(response.status_code, 401)
|
||||
|
||||
def test_authenticated_request_validates_usable_code(self):
|
||||
response = self.client.get(
|
||||
'/api/discount/validate',
|
||||
{'code': self.discount.code},
|
||||
HTTP_AUTHORIZATION=f'Bearer {self.token}',
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTrue(response.data['valid'])
|
||||
self.assertEqual(response.data['type'], 'percent')
|
||||
self.assertEqual(response.data['value'], 10)
|
||||
|
||||
def test_authenticated_request_reports_unusable_code_as_invalid(self):
|
||||
expired = DiscountCode.objects.create(
|
||||
code='EXPIRED5', type='fixed', value=5000, max_uses=5,
|
||||
expires_at=timezone.now() - timezone.timedelta(days=1),
|
||||
)
|
||||
response = self.client.get(
|
||||
'/api/discount/validate',
|
||||
{'code': expired.code},
|
||||
HTTP_AUTHORIZATION=f'Bearer {self.token}',
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertFalse(response.data['valid'])
|
||||
|
||||
Reference in New Issue
Block a user