def test_my_received_message_list_success(self, target_profile_param, sender_profile_param, receiver_type, target_users_role, from_user_subjects, from_user_displayed_subjects): target_profile = getattr(self, target_profile_param) self.client.login(username=target_profile.username, password='******') sender_profile = getattr(self, sender_profile_param) # Create a few notifications for _ in range(5): notification = NotificationFactory( from_user=sender_profile, receiver_type=receiver_type, from_user_subjects=from_user_subjects, target_users_role=target_users_role, target_study_class=self.study_class) TargetUserThroughFactory(notification=notification, is_read=True, user_profile=target_profile) response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data['results']), 5) expected_fields = [ 'id', 'title', 'created', 'from_user', 'from_user_full_name', 'from_user_role', 'from_user_subjects', 'is_read' ] received_notifications = response.data['results'] for notification_data in received_notifications: self.assertCountEqual(notification_data.keys(), expected_fields) self.assertTrue(notification_data['is_read']) self.assertEqual(notification_data['from_user_subjects'], from_user_displayed_subjects)
def test_my_sent_message_detail_class_parents(self, profile_param): profile = getattr(self, profile_param) self.client.login(username=profile.username, password='******') notification = NotificationFactory( from_user=profile, receiver_type=Notification.ReceiverTypes.CLASS_PARENTS, target_users_role=UserProfile.UserRoles.PARENT, target_study_class=self.study_class, targets_count=15) for _ in range(7): TargetUserThroughFactory( notification=notification, is_read=True, user_profile=UserProfileFactory( user_role=UserProfile.UserRoles.PARENT, school_unit=self.school_unit)) response = self.client.get(self.build_url(notification.id)) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertCountEqual(response.data.keys(), self.expected_fields) self.assertEqual(response.data['status']['sent_to_count'], 15) self.assertEqual(response.data['status']['read_by_count'], 7) self.assertCountEqual(list(response.data['target_study_class']), self.expected_study_class_fields)
def test_my_sent_message_detail_one_student(self, profile_param): profile = getattr(self, profile_param) self.client.login(username=profile.username, password='******') student = UserProfileFactory(user_role=UserProfile.UserRoles.STUDENT, school_unit=self.school_unit, student_in_class=self.study_class) notification = NotificationFactory( from_user=profile, receiver_type=Notification.ReceiverTypes.ONE_STUDENT, target_users_role=UserProfile.UserRoles.STUDENT, target_study_class=self.study_class, targets_count=1) TargetUserThroughFactory(notification=notification, user_profile=student) response = self.client.get(self.build_url(notification.id)) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertCountEqual(response.data.keys(), self.expected_fields) self.assertEqual(response.data['status']['sent_to_count'], 1) self.assertEqual(response.data['status']['read_by_count'], 0) self.assertCountEqual(list(response.data['target_study_class']), self.expected_study_class_fields) self.assertCountEqual(list(response.data['target_user_through']), ['user_profile', 'user_profile_full_name']) self.assertEqual(response.data['target_user_through']['user_profile'], student.id)
def test_my_received_notification_detail_success( self, target_profile_param, sender_profile_param, receiver_type, target_users_role, from_user_subjects, from_user_displayed_subjects): target_profile = getattr(self, target_profile_param) self.client.login(username=target_profile.username, password='******') sender_profile = getattr(self, sender_profile_param) expected_fields = [ 'id', 'title', 'created', 'is_read', 'from_user', 'from_user_full_name', 'from_user_role', 'from_user_subjects', 'body' ] notification = NotificationFactory( from_user=sender_profile, receiver_type=receiver_type, from_user_subjects=from_user_subjects, target_users_role=target_users_role, target_study_class=self.study_class) target_through = TargetUserThroughFactory(notification=notification, is_read=True, user_profile=target_profile) response = self.client.get(self.build_url(target_through.id)) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertCountEqual(response.data.keys(), expected_fields) self.assertEqual(response.data['from_user_subjects'], from_user_displayed_subjects)
def test_student_detail_include_risk_alerts(self, profile_param): self.client.login(username=getattr(self, profile_param).username, password='******') self.student.labels.add(LabelFactory(user_role=UserProfile.UserRoles.STUDENT)) self.student.parents.add(self.parent) notification1 = NotificationFactory() target_user_through1 = TargetUserThroughFactory(notification=notification1, user_profile=self.student, sent_at_email='*****@*****.**') notification2 = NotificationFactory(receiver_type=Notification.ReceiverTypes.ONE_PARENT, target_users_role=UserProfile.UserRoles.PARENT, send_sms=True) target_user_through2 = TargetUserThroughFactory(notification=notification2, user_profile=self.parent, sent_at_phone_number='0712345678') target_user_through2.children.add(self.student) response = self.client.get(self.build_url(self.student.id), {'include_risk_alerts': 'true'}) self.assertEqual(response.status_code, status.HTTP_200_OK) expected_fields = ['id', 'full_name', 'user_role', 'email', 'phone_number', 'use_phone_as_username', 'is_active', 'last_online', 'labels', 'risk_description', 'student_in_class', 'address', 'personal_id_number', 'birth_date', 'parents', 'educator_full_name', 'educator_email', 'educator_phone_number', 'risk_alerts'] self.assertCountEqual(response.data.keys(), expected_fields) self.assertCountEqual(response.data['labels'][0].keys(), ['id', 'text']) self.assertCountEqual(response.data['parents'][0].keys(), ['id', 'full_name', 'phone_number', 'email', 'address']) self.assertCountEqual(response.data['student_in_class'].keys(), ['id', 'class_grade', 'class_letter']) risk_alerts = response.data['risk_alerts'] self.assertEqual(risk_alerts['dates'], {timezone.now().date()}) self.assertEqual(risk_alerts['alerted_users'][0], { 'id': self.student.id, 'user_role': UserProfile.UserRoles.STUDENT, 'full_name': self.student.full_name, 'email': target_user_through1.sent_at_email, 'phone_number': None }) self.assertEqual(risk_alerts['alerted_users'][1], { 'id': self.parent.id, 'user_role': UserProfile.UserRoles.PARENT, 'full_name': self.parent.full_name, 'email': None, 'phone_number': target_user_through2.sent_at_phone_number })
def test_my_received_message_list_filters(self): self.client.login(username=self.parent.username, password='******') notification1 = NotificationFactory( title='title', from_user_subjects='English__Mathematics', from_user=self.teacher, receiver_type=Notification.ReceiverTypes.CLASS_PARENTS, target_users_role=UserProfile.UserRoles.PARENT, target_study_class=self.study_class) TargetUserThroughFactory(notification=notification1, is_read=True, user_profile=self.parent) notification2 = NotificationFactory( title='other', from_user=self.principal, receiver_type=Notification.ReceiverTypes.ONE_PARENT, target_users_role=UserProfile.UserRoles.PARENT, target_study_class=None) TargetUserThroughFactory(notification=notification2, is_read=True, user_profile=self.parent) notification2.created = datetime.datetime(2020, 4, 1, tzinfo=utc) notification2.save() response = self.client.get(self.url, { 'created': datetime.datetime.now().strftime(settings.DATE_FORMAT) }) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data['results']), 1) self.assertEqual(response.data['results'][0]['title'], notification1.title)
def test_my_received_message_list_search(self): self.client.login(username=self.parent.username, password='******') notification1 = NotificationFactory( title='title', from_user_subjects='English__Mathematics', from_user=self.teacher, receiver_type=Notification.ReceiverTypes.CLASS_PARENTS, target_users_role=UserProfile.UserRoles.PARENT, target_study_class=self.study_class) TargetUserThroughFactory(notification=notification1, is_read=True, user_profile=self.parent) notification2 = NotificationFactory( title='other', from_user=self.principal, receiver_type=Notification.ReceiverTypes.ONE_PARENT, target_users_role=UserProfile.UserRoles.PARENT, target_study_class=None) TargetUserThroughFactory(notification=notification2, is_read=True, user_profile=self.parent) response = self.client.get(self.url, {'search': 'other'}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data['results']), 1) self.assertEqual(response.data['results'][0]['title'], notification2.title) response = self.client.get(self.url, {'search': self.teacher.full_name}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data['results']), 1) self.assertEqual(response.data['results'][0]['from_user_full_name'], self.teacher.full_name)
def setUpTestData(cls): cls.principal = UserProfileFactory( user_role=UserProfile.UserRoles.PRINCIPAL) cls.school_unit = RegisteredSchoolUnitFactory( school_principal=cls.principal) cls.teacher = UserProfileFactory( user_role=UserProfile.UserRoles.TEACHER, school_unit=cls.school_unit) cls.study_class = StudyClassFactory(school_unit=cls.school_unit) cls.parent = UserProfileFactory(user_role=UserProfile.UserRoles.PARENT, school_unit=cls.school_unit) cls.student = UserProfileFactory( user_role=UserProfile.UserRoles.STUDENT, school_unit=cls.school_unit) cls.notification = NotificationFactory() cls.target_user_through = TargetUserThroughFactory( notification=cls.notification, user_profile=cls.parent)
def test_my_received_message_mark_as_read_not_own_notification( self, profile_param, receiver_type): profile = getattr(self, profile_param) self.client.login(username=profile.username, password='******') other_profile = UserProfileFactory(user_role=profile.user_role) notification = NotificationFactory( from_user=self.principal, receiver_type=receiver_type, target_users_role=other_profile.user_role, target_study_class=self.study_class) target_through = TargetUserThroughFactory(notification=notification, is_read=False, user_profile=other_profile) response = self.client.post(self.build_url(target_through.id)) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def setUpTestData(cls): cls.principal = UserProfileFactory( user_role=UserProfile.UserRoles.PRINCIPAL) cls.school_unit = RegisteredSchoolUnitFactory( school_principal=cls.principal) cls.teacher = UserProfileFactory( user_role=UserProfile.UserRoles.TEACHER, school_unit=cls.school_unit) cls.study_class = StudyClassFactory(school_unit=cls.school_unit) TeacherClassThroughFactory(teacher=cls.teacher, study_class=cls.study_class) cls.notification = NotificationFactory() cls.expected_fields = [ 'id', 'title', 'created', 'send_sms', 'status', 'receiver_type', 'target_users_role', 'target_study_class', 'target_user_through', 'body' ] cls.expected_study_class_fields = ['id', 'class_grade', 'class_letter']
def test_my_sent_message_detail_one_parent(self, profile_param): profile = getattr(self, profile_param) self.client.login(username=profile.username, password='******') notification = NotificationFactory( from_user=profile, receiver_type=Notification.ReceiverTypes.ONE_PARENT, target_users_role=UserProfile.UserRoles.PARENT, targets_count=1, target_study_class=None) parent = UserProfileFactory(user_role=UserProfile.UserRoles.PARENT, school_unit=self.school_unit) student = UserProfileFactory(user_role=UserProfile.UserRoles.STUDENT, school_unit=self.school_unit, student_in_class=self.study_class) another_student = UserProfileFactory( user_role=UserProfile.UserRoles.STUDENT, school_unit=self.school_unit, student_in_class=self.study_class) target_user_through = TargetUserThroughFactory( notification=notification, user_profile=parent, is_read=True) target_user_through.children.add(student, another_student) response = self.client.get(self.build_url(notification.id)) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertCountEqual(response.data.keys(), self.expected_fields) self.assertEqual(response.data['status']['sent_to_count'], 1) self.assertEqual(response.data['status']['read_by_count'], 1) self.assertIsNone(response.data['target_study_class']) target_user_through_data = response.data['target_user_through'] self.assertCountEqual( list(target_user_through_data), ['user_profile', 'user_profile_full_name', 'children']) self.assertEqual(target_user_through_data['user_profile'], parent.id) self.assertEqual(len(target_user_through_data['children']), 2) self.assertCountEqual(list(target_user_through_data['children'][0]), ['id', 'full_name', 'study_class']) self.assertCountEqual( list(target_user_through_data['children'][0]['study_class']), self.expected_study_class_fields)
def test_my_sent_message_list_success(self, profile_param): profile = getattr(self, profile_param) self.client.login(username=profile.username, password='******') study_class = StudyClassFactory(school_unit=self.school_unit) # Target = Class students notification1 = NotificationFactory( title='title 1', from_user=profile, receiver_type=Notification.ReceiverTypes.CLASS_STUDENTS, target_users_role=UserProfile.UserRoles.STUDENT, target_study_class=study_class, targets_count=25) for _ in range(5): TargetUserThroughFactory( notification=notification1, is_read=True, user_profile=UserProfileFactory( user_role=UserProfile.UserRoles.STUDENT, school_unit=self.school_unit, student_in_class=study_class)) # Target = Class parents notification2 = NotificationFactory( title='title 2', from_user=profile, receiver_type=Notification.ReceiverTypes.CLASS_PARENTS, target_users_role=UserProfile.UserRoles.PARENT, target_study_class=study_class, targets_count=15) for _ in range(7): TargetUserThroughFactory( notification=notification2, is_read=True, user_profile=UserProfileFactory( user_role=UserProfile.UserRoles.PARENT, school_unit=self.school_unit)) # Target = One student student = UserProfileFactory(user_role=UserProfile.UserRoles.STUDENT, school_unit=self.school_unit, student_in_class=study_class, full_name='Student full name') notification3 = NotificationFactory( title='title 3', from_user=profile, receiver_type=Notification.ReceiverTypes.ONE_STUDENT, target_users_role=UserProfile.UserRoles.STUDENT, target_study_class=study_class, targets_count=1) TargetUserThroughFactory(notification=notification3, user_profile=student) # Target = One parent notification4 = NotificationFactory( title='title 4', from_user=profile, receiver_type=Notification.ReceiverTypes.ONE_PARENT, target_users_role=UserProfile.UserRoles.PARENT, targets_count=1, target_study_class=None) parent = UserProfileFactory(user_role=UserProfile.UserRoles.PARENT, school_unit=self.school_unit, full_name='Parent full name') another_student = UserProfileFactory( user_role=UserProfile.UserRoles.STUDENT, school_unit=self.school_unit, student_in_class=study_class) target_user_through = TargetUserThroughFactory( notification=notification4, user_profile=parent, is_read=True) target_user_through.children.add(student, another_student) if profile.user_role == UserProfile.UserRoles.TEACHER: TeacherClassThroughFactory(teacher=profile, study_class=study_class) # A notification from another sender NotificationFactory( title='notification from another user', from_user=UserProfileFactory( user_role=UserProfile.UserRoles.TEACHER), receiver_type=Notification.ReceiverTypes.CLASS_STUDENTS, target_users_role=UserProfile.UserRoles.STUDENT, target_study_class=study_class, targets_count=25) response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 4) results = response.data['results'] expected_fields = [ 'id', 'title', 'created', 'send_sms', 'status', 'receiver_type', 'target_users_role', 'target_study_class', 'target_user_through' ] expected_study_class_fields = ['id', 'class_grade', 'class_letter'] # Check 1st notification data self.assertCountEqual(list(results[0]), expected_fields) self.assertEqual(results[0]['id'], notification4.id) self.assertEqual(results[0]['status']['sent_to_count'], 1) self.assertEqual(results[0]['status']['read_by_count'], 1) self.assertIsNone(results[0]['target_study_class']) target_user_through_data = results[0]['target_user_through'] self.assertCountEqual( list(target_user_through_data), ['user_profile', 'user_profile_full_name', 'children']) self.assertEqual(target_user_through_data['user_profile'], parent.id) self.assertEqual(len(target_user_through_data['children']), 2) self.assertCountEqual(list(target_user_through_data['children'][0]), ['id', 'full_name', 'study_class']) self.assertCountEqual( list(target_user_through_data['children'][0]['study_class']), expected_study_class_fields) # Check 2nd notification data self.assertCountEqual(list(results[1]), expected_fields) self.assertEqual(results[1]['id'], notification3.id) self.assertEqual(results[1]['status']['sent_to_count'], 1) self.assertEqual(results[1]['status']['read_by_count'], 0) self.assertCountEqual(list(results[1]['target_study_class']), expected_study_class_fields) self.assertCountEqual(list(results[1]['target_user_through']), ['user_profile', 'user_profile_full_name']) self.assertEqual(results[1]['target_user_through']['user_profile'], student.id) # Check 3rd notification data self.assertCountEqual(list(results[2]), expected_fields) self.assertEqual(results[2]['id'], notification2.id) self.assertEqual(results[2]['status']['sent_to_count'], 15) self.assertEqual(results[2]['status']['read_by_count'], 7) self.assertCountEqual(list(results[2]['target_study_class']), expected_study_class_fields) # Check 4th notification data self.assertCountEqual(list(results[3]), expected_fields) self.assertEqual(results[3]['id'], notification1.id) self.assertEqual(results[3]['status']['sent_to_count'], 25) self.assertEqual(results[3]['status']['read_by_count'], 5) self.assertCountEqual(list(results[3]['target_study_class']), expected_study_class_fields) # Search by title response = self.client.get(self.url, {'search': 'title 4'}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 1) self.assertEqual(response.data['results'][0]['id'], notification4.id) # Search by receiver name response = self.client.get(self.url, {'search': 'Student full'}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 1) self.assertEqual(response.data['results'][0]['id'], notification3.id) # Filter by created notification1.created = datetime.datetime(2020, 1, 1, tzinfo=utc) notification1.save() response = self.client.get(self.url, {'created': '01-01-2020'}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 1) self.assertEqual(response.data['results'][0]['id'], notification1.id) response = self.client.get(self.url, {'created': '2020-01-01'}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 4)
def test_my_sent_notification_detail_from_another_sender(self): self.client.login(username=self.teacher.username, password='******') notification = NotificationFactory(from_user=self.principal) response = self.client.get(self.build_url(notification.id)) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)