You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

103 lines
4.1 KiB

import datetime
from django.utils import timezone
from django.test import TestCase
from unittest.mock import patch
from django.core.files.uploadedfile import SimpleUploadedFile
from dj_language.models import Language
from apps.account.models import StudentUser, Notification
from apps.course.models.course import Course, CourseCategory
from apps.transaction.models import TransactionParticipant
class TransactionNotificationsFlowTests(TestCase):
def setUp(self):
# Create languages
self.lang_fa, _ = Language.objects.update_or_create(
code='fa',
defaults={'name': 'Persian', 'status': True, 'countries': []}
)
self.lang_en, _ = Language.objects.update_or_create(
code='en',
defaults={'name': 'English', 'status': True, 'countries': []}
)
# Create category
self.category = CourseCategory.objects.create(
name=[{"title": "Category", "language_code": "en"}],
slug=[{"title": "category", "language_code": "en"}]
)
# Create student user
self.student = StudentUser.objects.create(
email='student_tx@example.com',
username='student_tx',
fullname='Tx Student',
language=self.lang_fa,
fcm='token_tx_student'
)
# Create Course
thumbnail = SimpleUploadedFile('thumb.jpg', b'filecontent', content_type='image/jpeg')
self.course = Course.objects.create(
title=[
{"title": "Course 1", "language_code": "en"},
{"title": "دوره ۱", "language_code": "fa"}
],
slug=[{"title": "course-1", "language_code": "en"}],
category=self.category,
thumbnail=thumbnail,
video_type=Course.VedioTypeChoices.YOUTUBE_LINK,
video_link='https://example.com/video',
is_online=False,
level=Course.LevelChoices.BEGINNER,
duration=10,
lessons_count=0,
description='Description',
short_description='Short description',
status=[{"title": "ongoing", "language_code": "en"}],
is_free=True,
)
# Create transaction with PENDING status
self.tx = TransactionParticipant.objects.create(
user=self.student,
course=self.course,
price=150.00,
status=TransactionParticipant.TransactionStatus.PENDING
)
@patch('apps.account.notification_service.send_notification')
def test_payment_successful_notification(self, mock_send):
# Update status to SUCCESS
self.tx.status = TransactionParticipant.TransactionStatus.SUCCESS
self.tx.save()
# Check DB notification (in Persian)
notif = Notification.objects.filter(user=self.student, title="پرداخت موفقیت‌آمیز").first()
self.assertIsNotNone(notif)
self.assertIn("دوره ۱", notif.message)
self.assertTrue(mock_send.called)
@patch('apps.account.notification_service.send_notification')
def test_payment_failed_notification(self, mock_send):
# Update status to FAILED
self.tx.status = TransactionParticipant.TransactionStatus.FAILED
self.tx.save()
# Check DB notification (in Persian)
notif = Notification.objects.filter(user=self.student, title="خطا در پرداخت").first()
self.assertIsNotNone(notif)
self.assertIn("دوره ۱", notif.message)
self.assertTrue(mock_send.called)
@patch('apps.account.notification_service.send_notification')
def test_refund_completed_notification(self, mock_send):
# Update status to REFUNDED
self.tx.status = TransactionParticipant.TransactionStatus.REFUNDED
self.tx.save()
# Check DB notification (in Persian)
notif = Notification.objects.filter(user=self.student, title="بازگشت وجه انجام شد").first()
self.assertIsNotNone(notif)
self.assertIn("دوره ۱", notif.message)
self.assertTrue(mock_send.called)