def test_user_profile_update_student_parent_validations(self): self.client.login(username=self.principal.username, password='******') url = self.build_url(self.student_profile.id) # Parents don't exist self.student_data['parents'] = [0] response = self.client.put(url, self.student_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data, {'parents': ['Invalid pk "0" - object does not exist.']}) # Parents don't belong to the same school other_school = RegisteredSchoolUnitFactory() parent = UserProfileFactory(user_role=UserProfile.UserRoles.PARENT, school_unit=other_school) self.student_data['parents'] = [parent.id] response = self.client.put(url, self.student_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data, {'parents': ["Parents must belong to the user's school unit."]}) # Parents don't have a school parent.school_unit = None parent.save() response = self.client.put(url, self.student_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data, {'parents': ["Parents must belong to the user's school unit."]})
def test_study_class_receiver_counts_parents_list(self, profile_param): profile = getattr(self, profile_param) self.client.login(username=profile.username, password='******') # Create a parent without email and one without a phone_number student = UserProfileFactory(user_role=UserProfile.UserRoles.STUDENT, student_in_class=self.study_class) parent = UserProfileFactory(user_role=UserProfile.UserRoles.PARENT, phone_number=None, use_phone_as_username=False) student.parents.add(parent) student1 = UserProfileFactory(user_role=UserProfile.UserRoles.STUDENT, student_in_class=self.study_class) parent1 = UserProfileFactory(user_role=UserProfile.UserRoles.PARENT, username='******') parent1.email = None parent1.save() student1.parents.add(parent1) response = self.client.get(self.build_url(self.study_class.id), {'receiver_type': 'CLASS_PARENTS'}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['total_count'], 2) self.assertEqual(response.data['emails_count'], 1) self.assertEqual(response.data['phone_numbers_count'], 1)
def test_user_profile_update_forbidden_user_role(self, user_role, forbidden_roles): self.client.login(username=UserProfile.objects.filter(user_role=user_role).first().username, password='******') for forbidden_role in forbidden_roles: profile = UserProfileFactory(user_role=forbidden_role) if forbidden_role != UserProfile.UserRoles.ADMINISTRATOR: profile.school_unit = self.school_unit profile.save() self.data['user_role'] = forbidden_role response = self.client.put(self.build_url(profile.id), self.data) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_difference_subject_list_secondary_school(self, mocked_method): self.client.login(username=self.principal.username, password='******') student = UserProfileFactory(user_role=UserProfile.UserRoles.STUDENT, school_unit=self.school_unit) student.student_in_class = StudyClassFactory( school_unit=self.school_unit, class_grade_arabic=7, class_grade='VII') student.save() study_class = StudyClassFactory(school_unit=self.school_unit, class_grade_arabic=7, class_grade='VII') subject1 = SubjectFactory() response = self.client.get(self.build_url(student.id, study_class.id)) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, {})
def test_study_class_receiver_counts_students_list(self, profile_param): profile = getattr(self, profile_param) self.client.login(username=profile.username, password='******') # Create a student without email and one without a phone_number UserProfileFactory(user_role=UserProfile.UserRoles.STUDENT, student_in_class=self.study_class, phone_number=None, use_phone_as_username=False) profile = UserProfileFactory(user_role=UserProfile.UserRoles.STUDENT, student_in_class=self.study_class, username='******') profile.email = None profile.save() response = self.client.get(self.build_url(self.study_class.id)) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['total_count'], 2) self.assertEqual(response.data['emails_count'], 1) self.assertEqual(response.data['phone_numbers_count'], 1) # Create a student from another school school_unit = RegisteredSchoolUnitFactory() UserProfileFactory(user_role=UserProfile.UserRoles.STUDENT, school_unit=school_unit) StudyClassFactory(school_unit=school_unit) response = self.client.get(self.build_url(self.study_class.id)) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['total_count'], 2) # Create a student from another class other_class = StudyClassFactory(school_unit=self.school_unit) UserProfileFactory(user_role=UserProfile.UserRoles.STUDENT, school_unit=self.school_unit, student_in_class=other_class) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['total_count'], 2)
def test_study_class_update_validate_teachers(self, timezone_mock): timezone_mock.return_value = datetime.datetime(self.calendar.academic_year, 9, 14, tzinfo=tz.UTC) self.client.login(username=self.principal.username, password='******') url = self.build_url(self.study_class1.id) # Not a teacher profile1 = UserProfileFactory(user_role=UserProfile.UserRoles.STUDENT, school_unit=self.school_unit) # Another school unit profile2 = UserProfileFactory(user_role=UserProfile.UserRoles.TEACHER, school_unit=RegisteredSchoolUnitFactory()) # Inactive teacher profile3 = UserProfileFactory(user_role=UserProfile.UserRoles.TEACHER, school_unit=self.school_unit, is_active=False) for profile in [profile1, profile2, profile3]: self.highschool_request_data['teachers_class_through'] = [ { "teacher": profile.id, "subject": self.subject1.id }, { "teacher": self.teacher2.id, "subject": self.subject2.id } ] response = self.client.put(url, self.highschool_request_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data['teachers_class_through'], ['At least one teacher is invalid.']) # Subject not in teacher's taught subjects profile2.school_unit = self.school_unit profile2.save() self.highschool_request_data['teachers_class_through'] = [ { "teacher": self.teacher1.id, "subject": self.subject1.id }, { "teacher": profile2.id, "subject": self.subject2.id } ] response = self.client.put(url, self.highschool_request_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data['teachers_class_through'], ['Teacher {} does not teach {}.' .format(profile2.full_name, self.subject2.name)]) # User not found self.highschool_request_data['teachers_class_through'] = [ { "teacher": 0, "subject": self.subject1.id }, { "teacher": self.teacher2.id, "subject": self.subject2.id } ] response = self.client.put(url, self.highschool_request_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data['teachers_class_through'][0]['teacher'], ['Invalid pk "0" - object does not exist.'])
class RegisteredSchoolUnitCreateTestCase(CommonAPITestCase): @classmethod def setUpTestData(cls): cls.admin_user = UserProfileFactory( user_role=UserProfile.UserRoles.ADMINISTRATOR) cls.calendar = AcademicYearCalendarFactory() cls.academic_profile = SchoolUnitProfileFactory() cls.url = reverse('schools:school-unit-list') def setUp(self): self.principal = UserProfileFactory( user_role=UserProfile.UserRoles.PRINCIPAL) self.request_data = { 'categories': [self.academic_profile.category.id], 'academic_profile': self.academic_profile.id, 'school_principal': self.principal.id, 'address': 'address', 'phone_number': '+0799111222', 'email': '*****@*****.**', 'district': 'district', 'city': 'city', 'name': 'name' } def test_registered_school_unit_create_unauthenticated(self): response = self.client.post(self.url, self.request_data) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) @data(UserProfile.UserRoles.TEACHER, UserProfile.UserRoles.PRINCIPAL, UserProfile.UserRoles.STUDENT, UserProfile.UserRoles.PARENT) def test_registered_school_unit_create_wrong_user_type(self, user_role): school_unit = RegisteredSchoolUnitFactory() user = UserProfileFactory(user_role=user_role, school_unit=school_unit) self.client.login(username=user.username, password='******') response = self.client.post(self.url, self.request_data) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) def test_registered_school_unit_create_missing_fields(self): self.client.login(username=self.admin_user.username, password='******') required_fields = [ 'categories', 'address', 'phone_number', 'email', 'school_principal', 'district', 'city', 'name' ] request_data = copy(self.request_data) for field in required_fields: del request_data[field] response = self.client.post(self.url, request_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data, {field: ['This field is required.']}) request_data = copy(self.request_data) def test_registered_school_unit_create_validate_phone_number(self): self.client.login(username=self.admin_user.username, password='******') invalid_phone_numbers = [ 'abc', '+3232', '32324', '123456789123456789122' '++2392321234' ] for phone_number in invalid_phone_numbers: self.request_data['phone_number'] = phone_number response = self.client.post(self.url, self.request_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data, { 'phone_number': [ 'Invalid format. Must be minimum 10, maximum 20 digits or +.' ] }) def test_registered_school_unit_create_validate_principal(self): self.client.login(username=self.admin_user.username, password='******') # Inexistent school principal self.request_data['school_principal'] = 0 response = self.client.post(self.url, self.request_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data, {'school_principal': ['Invalid pk "0" - object does not exist.']}) # Inactive principal self.principal.is_active = False self.principal.save() self.request_data['school_principal'] = self.principal.id response = self.client.post(self.url, self.request_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data, {'school_principal': ['Invalid user.']}) self.principal.is_active = True self.principal.save() # The school principal is already assigned to another school RegisteredSchoolUnitFactory(school_principal=self.principal) response = self.client.post(self.url, self.request_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data, {'school_principal': ['This field must be unique.']}) def test_registered_school_unit_create_validate_categories(self): self.client.login(username=self.admin_user.username, password='******') # Invalid categories self.request_data['categories'] = [0] response = self.client.post(self.url, self.request_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data, {'categories': ['Invalid pk "0" - object does not exist.']}) # Multiple categories from the same level self.request_data['categories'] = [ self.academic_profile.category.id, SchoolUnitCategoryFactory().id ] response = self.client.post(self.url, self.request_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data, { 'categories': ['Cannot have multiple categories for the same school level.'] }) def test_registered_school_unit_create_validate_academic_profile(self): self.client.login(username=self.admin_user.username, password='******') academic_profile2 = SchoolUnitProfileFactory() self.request_data['academic_profile'] = academic_profile2.id response = self.client.post(self.url, self.request_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data, { 'academic_profile': [ 'The academic profile does not correspond with the school category.' ] }) def test_registered_school_unit_create_duplicate_school(self): self.client.login(username=self.admin_user.username, password='******') RegisteredSchoolUnitFactory(name=self.request_data['name'], district=self.request_data['district'], city=self.request_data['city']) response = self.client.post(self.url, self.request_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data['general_errors'], ['This school is already registered.']) def test_registered_school_unit_create_success(self): self.client.login(username=self.admin_user.username, password='******') expected_fields = [ 'id', 'is_active', 'categories', 'address', 'phone_number', 'email', 'school_principal', 'district', 'city', 'name', 'academic_profile' ] response = self.client.post(self.url, self.request_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertCountEqual(response.data.keys(), expected_fields) self.assertEqual(list(response.data['categories'][0]), ['id', 'name', 'category_level']) self.assertEqual(list(response.data['academic_profile']), ['id', 'name']) self.assertEqual(list(response.data['school_principal']), ['id', 'full_name']) school_unit = RegisteredSchoolUnit.objects.filter( id=response.data['id']).first() self.assertIsNotNone(school_unit) self.principal.refresh_from_db() self.assertEqual(self.principal.school_unit, school_unit) self.assertTrue( SchoolUnitStats.objects.filter( school_unit=school_unit, academic_year=self.calendar.academic_year).exists()) self.assertTrue( StudentAtRiskCounts.objects.filter( school_unit=school_unit).exists()) def test_registered_school_unit_create_no_academic_profile(self): self.client.login(username=self.admin_user.username, password='******') del self.request_data['academic_profile'] response = self.client.post(self.url, self.request_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertIsNone(response.data['academic_profile']) def test_registered_school_unit_create_updates_enrollment_stats(self): self.client.login(username=self.admin_user.username, password='******') today = timezone.now().date() stats = SchoolUnitEnrollmentStatsFactory( year=today.year, month=today.month, daily_statistics=[{ 'day': today.day, 'weekday': WEEKDAYS_MAP[today.weekday()], 'count': 0 }]) response = self.client.post(self.url, self.request_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) stats.refresh_from_db() self.assertEqual(len(stats.daily_statistics), 1) self.assertEqual(stats.daily_statistics[0]['count'], 1) self.assertEqual(stats.daily_statistics[0]['day'], today.day) self.assertEqual(stats.daily_statistics[0]['weekday'], WEEKDAYS_MAP[today.weekday()]) self.request_data['name'] = 'Other' self.request_data['school_principal'] = UserProfileFactory( user_role=UserProfile.UserRoles.PRINCIPAL).id response = self.client.post(self.url, self.request_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) stats.refresh_from_db() self.assertEqual(len(stats.daily_statistics), 1) self.assertEqual(stats.daily_statistics[0]['count'], 2)