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_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_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.'])