def setUp(self): self.student = CustomUser.objects.create_user( "*****@*****.**", "Student", self.state1, "12345678", email_verified=True) self.student_data = StudentData.objects.create( user=self.student, school_data=self.school_data1) self.tutor = CustomUser.objects.create_user( "*****@*****.**", "Tutor", self.state1, "12345678", email_verified=True) self.tutor_data = TutorData( user=self.tutor) self.tutor_data.save() self.tutor_data.schooldata.set([self.school_data1]) self.tutor_data.subjects.set([self.subject1])
def setUpTestData(cls): cls.school_type = SchoolType.objects.create(name="Testschooltype1") cls.school_data = SchoolData.objects.create( school_type=cls.school_type, grade=10) cls.subject = Subject.objects.create(name="TestSubject") cls.state = State.objects.create(name="Teststate1") cls.student_pw = "12345678" cls.student = CustomUser.objects.create_user( "*****@*****.**", "Student", cls.state, cls.student_pw) cls.student_data = StudentData.objects.create( user=cls.student, school_data=cls.school_data) cls.tutor_pw = "12345678" cls.tutor = CustomUser.objects.create_user( "*****@*****.**", "Tutor", cls.state, cls.tutor_pw, last_name="Lastname") cls.tutor_data = TutorData( user=cls.tutor) cls.tutor_data.save() cls.tutor_data.schooldata.set([cls.school_data]) cls.tutor_data.subjects.set([cls.subject])
class AccountChangeTest(APITestCase): """ Account change test-cases """ @classmethod def setUpTestData(cls): cls.school_type1 = SchoolType.objects.create(name="Testschooltype1") cls.school_type2 = SchoolType.objects.create(name="Testschooltype2") cls.school_data1 = SchoolData.objects.create( school_type=cls.school_type1, grade=10) cls.school_data2 = SchoolData.objects.create( school_type=cls.school_type2, grade=6) cls.subject1 = Subject.objects.create(name="TestSubject") cls.subject2 = Subject.objects.create(name="TestSubject") cls.state1 = State.objects.create(name="Teststate1") cls.state2 = State.objects.create(name="Teststate2") def setUp(self): self.student = CustomUser.objects.create_user("*****@*****.**", "Student", self.state1, "12345678", email_verified=True) self.student_data = StudentData.objects.create( user=self.student, school_data=self.school_data1) self.tutor = CustomUser.objects.create_user("*****@*****.**", "Tutor", self.state1, "12345678", email_verified=True) self.tutor_data = TutorData(user=self.tutor) self.tutor_data.save() self.tutor_data.schooldata.set([self.school_data1]) self.tutor_data.subjects.set([self.subject1]) def test_patch_student_account(self): self.client.force_authenticate(self.student) data = { 'firstName': 'Student2', 'email': '*****@*****.**', 'state': self.state2.id, 'studentdata': { 'school_data': self.school_data2.id } } response = self.client.patch(reverse("account:account_view"), data=data, format='json') self.assertEqual(response.status_code, 200) student = CustomUser.objects.get(id=self.student.id) self.assertEqual(student.first_name, 'Student2') self.assertEqual(student.email, '*****@*****.**') self.assertEqual(student.state, self.state2) self.assertEqual(student.studentdata.school_data, self.school_data2) # check email verified status (email changed) self.assertEqual(len(mail.outbox), 1) self.assertFalse(student.email_verified) self.assertTrue( VerificationToken.objects.filter(user=student).exists()) def test_patch_tutor_account(self): self.client.force_authenticate(self.tutor) data = { 'firstName': 'Tutor2', 'email': '*****@*****.**', 'tutordata': { 'schooldata': [self.school_data1.id, self.school_data2.id], 'subjects': [self.subject1.id, self.subject2.id] }, 'state': self.state2.id } response = self.client.patch(reverse("account:account_view"), data=data, format='json') self.assertEqual(response.status_code, 200) tutor = CustomUser.objects.get(id=self.tutor.id) self.assertEqual(tutor.first_name, 'Tutor2') self.assertEqual(tutor.email, '*****@*****.**') self.assertEqual(tutor.state, self.state2) self.assertSetEqual(set(tutor.tutordata.schooldata.all()), set([self.school_data1, self.school_data2])) self.assertSetEqual(set(tutor.tutordata.subjects.all()), set([self.subject1, self.subject2])) # check email verified status (email changed) self.assertEqual(len(mail.outbox), 1) self.assertFalse(tutor.email_verified) self.assertTrue(VerificationToken.objects.filter(user=tutor).exists())