59 lines
2.6 KiB
Python
59 lines
2.6 KiB
Python
|
|
import uuid
|
||
|
|
|
||
|
|
from django.test import TestCase
|
||
|
|
from django.urls import reverse
|
||
|
|
from rest_framework.test import APIClient
|
||
|
|
|
||
|
|
from apps.account.models import User
|
||
|
|
from apps.banker.models import Contact
|
||
|
|
from apps.messaging.models import BulkMessage, MessageRecipient
|
||
|
|
|
||
|
|
|
||
|
|
class ContactMessageHistoryTests(TestCase):
|
||
|
|
"""تاریخچهی پیامهای یک شماره — چیزی که پروفایلِ مشتری/مخاطب نشان میدهد."""
|
||
|
|
|
||
|
|
def setUp(self):
|
||
|
|
self.user = User.objects.create(phone="09120000010")
|
||
|
|
self.other = User.objects.create(phone="09120000011")
|
||
|
|
self.client = APIClient()
|
||
|
|
self.client.force_authenticate(self.user)
|
||
|
|
self.url = reverse("messaging:contact-history")
|
||
|
|
|
||
|
|
def _recipient(self, user, phone, body="سلام", status=MessageRecipient.Status.SENT):
|
||
|
|
contact = Contact.objects.create(
|
||
|
|
id=str(uuid.uuid4()), user=user, name="رضایی", phone=phone
|
||
|
|
)
|
||
|
|
bulk = BulkMessage.objects.create(user=user, channels=["sms"])
|
||
|
|
return MessageRecipient.objects.create(
|
||
|
|
bulk_message=bulk, contact=contact, channel="sms",
|
||
|
|
status=status, rendered_body=body,
|
||
|
|
)
|
||
|
|
|
||
|
|
def test_returns_messages_sent_to_that_number(self):
|
||
|
|
self._recipient(self.user, "09121112233", body="سفارشت آماده است")
|
||
|
|
response = self.client.get(self.url, {"phone": "09121112233"})
|
||
|
|
self.assertEqual(response.status_code, 200)
|
||
|
|
self.assertEqual(len(response.data), 1)
|
||
|
|
self.assertEqual(response.data[0]["rendered_body"], "سفارشت آماده است")
|
||
|
|
self.assertEqual(response.data[0]["channel_display"], "پیامک")
|
||
|
|
|
||
|
|
def test_matches_regardless_of_how_the_number_was_stored(self):
|
||
|
|
"""شماره ممکن است +۹۸… ذخیره شده باشد ولی کاربر ۰۹… جستجو کند."""
|
||
|
|
self._recipient(self.user, "+989121112233")
|
||
|
|
response = self.client.get(self.url, {"phone": "09121112233"})
|
||
|
|
self.assertEqual(len(response.data), 1)
|
||
|
|
|
||
|
|
def test_never_leaks_another_users_messages(self):
|
||
|
|
self._recipient(self.other, "09121112233", body="مالِ کاربر دیگر")
|
||
|
|
response = self.client.get(self.url, {"phone": "09121112233"})
|
||
|
|
self.assertEqual(response.data, [])
|
||
|
|
|
||
|
|
def test_missing_phone_is_a_400_not_a_dump_of_everything(self):
|
||
|
|
response = self.client.get(self.url)
|
||
|
|
self.assertEqual(response.status_code, 400)
|
||
|
|
|
||
|
|
def test_requires_authentication(self):
|
||
|
|
self.client.force_authenticate(None)
|
||
|
|
response = self.client.get(self.url, {"phone": "09121112233"})
|
||
|
|
self.assertIn(response.status_code, (401, 403))
|