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.course.models.participant import Participant from apps.course.models.live_session import CourseLiveSession, LiveSessionRecording class LiveNotificationsFlowTests(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_live@example.com', username='student_live', fullname='Live Student', language=self.lang_ru, fcm='token_live_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, ) # Enroll student in the course self.participant = Participant.objects.create( student=self.student, course=self.course, is_active=True ) # Create Live Session self.session = CourseLiveSession.objects.create( course=self.course, subject='Lesson One Live', started_at=timezone.now() + datetime.timedelta(days=1), is_cancelled=False, reminder_sent=False ) @patch('apps.account.notification_service.send_notification') def test_live_class_cancelled_notification(self, mock_send): # Event 1: LIVE Class Cancelled self.session.is_cancelled = True self.session.save() # Check DB notification for Russian student notif = Notification.objects.filter(user=self.student, title="Живой урок отменен").first() self.assertIsNotNone(notif) self.assertIn("Lesson One Live", notif.message) self.assertIn("Курс 1", notif.message) self.assertTrue(mock_send.called) @patch('apps.account.notification_service.send_notification') def test_live_class_rescheduled_notification(self, mock_send): # Event 2: LIVE Class Rescheduled old_time = self.session.started_at new_time = old_time + datetime.timedelta(hours=2) self.session.started_at = new_time self.session.save() # Check DB notification notif = Notification.objects.filter(user=self.student, title="Время живого урока изменено").first() self.assertIsNotNone(notif) self.assertIn("Lesson One Live", notif.message) self.assertIn("Курс 1", notif.message) self.assertTrue(mock_send.called) @patch('apps.account.notification_service.send_notification') def test_live_recording_available_notification(self, mock_send): # Event 3: LIVE Recording Available # Create a LiveSessionRecording with a dummy file recording_file = SimpleUploadedFile('rec.mp4', b'video_data', content_type='video/mp4') recording = LiveSessionRecording.objects.create( session=self.session, file=recording_file ) # Check DB notification notif = Notification.objects.filter(user=self.student, title="Доступна запись урока").first() self.assertIsNotNone(notif) self.assertIn("Lesson One Live", notif.message) self.assertIn("Курс 1", notif.message) self.assertTrue(mock_send.called)