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_ru, _ = Language.objects.update_or_create( code='ru', defaults={'name': 'Russian', '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_ru, 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": "Курс 1", "language_code": "ru"} ], 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 Russian) notif = Notification.objects.filter(user=self.student, title="Оплата успешна").first() self.assertIsNotNone(notif) self.assertIn("Курс 1", 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 Russian) notif = Notification.objects.filter(user=self.student, title="Ошибка оплаты").first() self.assertIsNotNone(notif) self.assertIn("Курс 1", 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 Russian) notif = Notification.objects.filter(user=self.student, title="Возврат средств выполнен").first() self.assertIsNotNone(notif) self.assertIn("Курс 1", notif.message) self.assertTrue(mock_send.called)