some improve for sms delivery

This commit is contained in:
javad committed 2026-09-16 10:40:21 +03:30
1 parent 1d3cff0b63
commit ffc1ec0dcf
10 files changed
+134 -11

No files matched your search

+31 -1
View File
@@ -8,6 +8,7 @@ from django.utils import timezone
from django.core.files.uploadedfile import SimpleUploadedFile
from apps.attachments.models import Attachment, AttachmentPolicy
from apps.accounts.models import User, normalize_mobile
from apps.messaging.models import MessageReadReceipt
from apps.notifications.models import NotificationJob
from apps.staff.models import OrganizationalUnit, StaffProfile
from apps.tickets.models import Ticket, TicketCategory
@@ -499,7 +500,11 @@ def test_due_unread_staff_reply_is_sent_once(category,customer,monkeypatch):
from apps.integrations.sms.base import SMSResult
from apps.notifications.management.commands import process_notifications
a=make_staff("a"); ticket=create_ticket(customer=customer,category=category,subject="پاسخ خوانده نشده",selected_expert=a,body="متن")
reply_ticket(ticket_id=ticket.pk,user=a,body="پاسخ کارشناس")
message=reply_ticket(ticket_id=ticket.pk,user=a,body="پاسخ کارشناس")
observer=make_staff("observer")
other_customer=User.objects.create_user(username="other-sms-customer",mobile="09123334444",account_type="CUSTOMER")
MessageReadReceipt.objects.create(message=message,user=observer)
MessageReadReceipt.objects.create(message=message,user=other_customer)
job=NotificationJob.objects.get(ticket=ticket,event_type="STAFF_REPLY")
NotificationJob.objects.filter(pk=job.pk).update(next_attempt_at=timezone.now()-timedelta(seconds=1))
ticket.last_activity_at=timezone.now()-timedelta(hours=1); ticket.save(update_fields=["last_activity_at"])
@@ -514,6 +519,31 @@ def test_due_unread_staff_reply_is_sent_once(category,customer,monkeypatch):
assert len(sent)==1
@pytest.mark.django_db
def test_due_staff_reply_is_cancelled_only_when_ticket_customer_has_seen_it(category,customer,monkeypatch):
from django.core.management import call_command
from apps.notifications.management.commands import process_notifications
staff=make_staff("a")
ticket=create_ticket(customer=customer,category=category,subject="پاسخ دیده شده",selected_expert=staff,body="متن")
message=reply_ticket(ticket_id=ticket.pk,user=staff,body="پاسخ کارشناس")
MessageReadReceipt.objects.create(message=message,user=customer)
job=NotificationJob.objects.get(ticket=ticket,event_type="STAFF_REPLY")
NotificationJob.objects.filter(pk=job.pk).update(next_attempt_at=timezone.now()-timedelta(seconds=1))
ticket.last_activity_at=timezone.now()-timedelta(hours=1)
ticket.save(update_fields=["last_activity_at"])
class Provider:
def send_message(self,mobile,message):
raise AssertionError("پیام دیده‌شده توسط خود مشتری نباید به درگاه ارسال شود")
monkeypatch.setattr(process_notifications,"get_sms_provider",lambda:Provider())
call_command("process_notifications",once=True,job_id=job.pk)
job.refresh_from_db()
assert job.status==NotificationJob.Status.CANCELLED
assert job.last_error=="customer_read"
@pytest.mark.django_db
def test_every_failed_sms_gets_exactly_three_retries_three_minutes_apart(category,customer,monkeypatch,settings):
from django.core.management import call_command