def test_student_optional_full_update_positive(self): """Test successfully fully updating a student with optional params""" grade = sample_grade() classroom1 = sample_classroom() classroom2 = sample_classroom() classroom3 = sample_classroom() payload = sample_student_payload( fullname="Testonildo", active=False, departure_date='2010-10-10', guardian2='Mr. Guard', grade=grade.id, classes=[classroom1.id, classroom3.id]) res = self.client.put(student_detail_url(self.student.id), payload) self.student.refresh_from_db() payload.pop('classes') self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(Student.objects.get(**payload), self.student) self.assertEqual(self.student.guardian2, payload['guardian2']) self.assertEqual(self.student.grade, grade) self.assertIn(classroom1, self.student.classes.all()) self.assertIn(classroom3, self.student.classes.all()) self.assertNotIn(classroom2, self.student.classes.all()) serializer = StudentCreateSerializer(self.student) self.assertEqual(res.data, serializer.data)
def test_unauthenticated_student_full_update_negative(self): """Test an unauthenticated request cannot fully update a student""" student = sample_student(fullname="A Full Name") payload = sample_student_payload(fullname="Other name") res = APIClient().put(student_detail_url(student.id), payload) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(Student.objects.get(id=student.id), student)
def test_student_create_illegal_operation_negative(self): """Test requests with illegal operations cannot create student""" student = sample_student(fullname="Joseph Alleph") payload = sample_student_payload(fullname="Joseph Doppelganger", id_doc=student.id_doc) res = self.client.post(STUDENT_LIST_URL, payload) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) self.assertFalse(Student.objects.filter(**payload).exists())
def test_student_full_update_illegal_operation_negative(self): """Test requests with illegal operations cannot fully update student""" student = sample_student(fullname="Joseph Alleph") payload = sample_student_payload(fullname="Joseph Doppelganger", id_doc=student.id_doc) res = self.client.put(student_detail_url(self.student.id), payload) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(Student.objects.get(id=self.student.id), self.student)
def test_student_basic_full_update_positive(self): """Test successfully fully updating a student object""" payload = sample_student_payload(fullname="The correct Name", sex='O', birthdate='2000-01-01', email='*****@*****.**') res = self.client.put(student_detail_url(self.student.id), payload) self.student.refresh_from_db() serializer = StudentCreateSerializer(self.student) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(Student.objects.get(**payload), self.student) self.assertEqual(res.data, serializer.data)
def test_student_create_cannot_set_id(self): """Test the student id cannot be set by the create request""" payload = sample_student_payload(id=99999) res = self.client.post(STUDENT_LIST_URL, payload) self.assertEqual(res.status_code, status.HTTP_201_CREATED) request_id = payload.pop('id') student = Student.objects.filter(**payload) self.assertTrue(student.exists()) student = student[0] self.assertNotEqual(student.id, request_id) self.assertNotEqual(res.data['id'], request_id)
def test_student_create_invalid_payload_negative(self): """Test requests with invalid payload cannot create student object""" payloads = [ sample_student_payload(sex='L'), sample_student_payload(birthdate=dt.date.today() + dt.timedelta(days=1)), sample_student_payload(email='not_an_email.com'), sample_student_payload(grade=1111), sample_student_payload(classes=[1241]), sample_student_payload(monthly_payment=10000000000), ] for payload in payloads: res = self.client.post(STUDENT_LIST_URL, payload) payload.pop('classes', None) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) self.assertFalse(Student.objects.filter(**payload).exists())
def test_student_full_update_invalid_payload_negative(self): """Test an invalid payload cannot fully update a student object""" payloads = [ sample_student_payload(sex='G'), sample_student_payload(birthdate=dt.date.today() + dt.timedelta(days=1)), sample_student_payload(email='not_an_email.com'), sample_student_payload(grade=1111), sample_student_payload(classes=[1241]), sample_student_payload(monthly_payment=10000000000), ] for payload in payloads: res = self.client.put(student_detail_url(self.student.id), payload) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(Student.objects.get(id=self.student.id), self.student)