|
1 | 1 | from django.contrib.auth.models import User |
2 | 2 | from django.test import TestCase |
3 | | -from django.urls import reverse |
4 | | -from rest_framework.test import APIClient |
| 3 | +from rest_framework.test import APIClient, APITestCase |
5 | 4 | from rest_framework import status |
6 | 5 | from .models import Profile, StudentProfile, TutorProfile, ParentProfile |
7 | 6 | from rest_framework_simplejwt.tokens import RefreshToken |
8 | 7 |
|
| 8 | +# https://chatgpt.com/share/6804498e-2dfc-8005-8b02-d78afdc354ae |
| 9 | +class UserRegistrationTests(APITestCase): |
| 10 | + def test_register_student_profile(self): |
| 11 | + parent_profile = User.objects.create_user(username="parentuser", password="pass123") |
| 12 | + parent_profile_instance = ParentProfile.objects.create(profile=Profile.objects.create(user=parent_profile, role=Profile.PARENT)) |
| 13 | + |
| 14 | + url = "/users/register/" # make sure you have this in urls.py |
| 15 | + data = { |
| 16 | + "country": "US", |
| 17 | + "username": "studentuser", |
| 18 | + "email": "student@example.com", |
| 19 | + "firstName": "Student", |
| 20 | + "lastName": "User", |
| 21 | + "password": "testpassword", |
| 22 | + "role": 3, |
| 23 | + "parent_profile": parent_profile_instance.id, |
| 24 | + "grade_level": 5, |
| 25 | + "preferred_subjects": [], |
| 26 | + "emergency_contact_name": "Parent User", |
| 27 | + "emergency_contact_phone_number": "5555555555" |
| 28 | + } |
| 29 | + response = self.client.post(url, data, format='json') |
| 30 | + self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
| 31 | + self.assertTrue(User.objects.filter(username="studentuser").exists()) |
| 32 | + self.assertTrue(Profile.objects.filter(user__username="studentuser").exists()) |
| 33 | + self.assertTrue(StudentProfile.objects.filter(profile__user__username="studentuser").exists()) |
| 34 | + |
| 35 | + def test_register_tutor_profile(self): |
| 36 | + url = "/users/register/" |
| 37 | + data = { |
| 38 | + "country": "US", |
| 39 | + "username": "tutoruser", |
| 40 | + "email": "tutor@example.com", |
| 41 | + "firstName": "Tutor", |
| 42 | + "lastName": "User", |
| 43 | + "password": "testpassword", |
| 44 | + "role": 1, |
| 45 | + "city": "New York", |
| 46 | + "state": "NY", |
| 47 | + "bio": "I love teaching.", |
| 48 | + "hourly_rate": "40.00" |
| 49 | + } |
| 50 | + response = self.client.post(url, data, format='json') |
| 51 | + self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
| 52 | + self.assertTrue(User.objects.filter(username="tutoruser").exists()) |
| 53 | + self.assertTrue(TutorProfile.objects.filter(profile__user__username="tutoruser").exists()) |
| 54 | + |
| 55 | + def test_register_parent_profile(self): |
| 56 | + url = "/users/register/" |
| 57 | + data = { |
| 58 | + "country": "US", |
| 59 | + "username": "parentuser2", |
| 60 | + "email": "parent2@example.com", |
| 61 | + "firstName": "Parent", |
| 62 | + "lastName": "User", |
| 63 | + "password": "testpassword", |
| 64 | + "role": 2, |
| 65 | + } |
| 66 | + response = self.client.post(url, data, format='json') |
| 67 | + self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
| 68 | + self.assertTrue(User.objects.filter(username="parentuser2").exists()) |
| 69 | + self.assertTrue(ParentProfile.objects.filter(profile__user__username="parentuser2").exists()) |
| 70 | + |
| 71 | + |
| 72 | +class UserProfileViewTests(APITestCase): |
| 73 | + def setUp(self): |
| 74 | + # Student user |
| 75 | + student_user = User.objects.create_user(username="student", password="password") |
| 76 | + student_profile = Profile.objects.create(user=student_user, role=Profile.STUDENT) |
| 77 | + parent_user = User.objects.create_user(username="parent", password="password") |
| 78 | + parent_profile = Profile.objects.create(user=parent_user, role=Profile.PARENT) |
| 79 | + parent_profile_instance = ParentProfile.objects.create(profile=parent_profile) |
| 80 | + StudentProfile.objects.create( |
| 81 | + profile=student_profile, |
| 82 | + parent_profile=parent_profile_instance, |
| 83 | + grade_level=6, |
| 84 | + emergency_contact_name="Test Parent", |
| 85 | + emergency_contact_phone_number="1112223333" |
| 86 | + ) |
| 87 | + self.student_user = student_user |
| 88 | + # api |
| 89 | + self.client = APIClient() |
| 90 | + self.login_url = "/users/login/" |
| 91 | + response = self.client.post(self.login_url, {"username": "student", "password": "password"}) |
| 92 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 93 | + self.refresh_token = response.json().get("refreshToken") |
| 94 | + self.access_token = response.json().get("accessToken") |
| 95 | + self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.access_token}") |
| 96 | + |
| 97 | + def test_get_student_profile(self): |
| 98 | + url = "/users/user-profile/" # replace with your actual path name |
| 99 | + response = self.client.get(url) |
| 100 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 101 | + self.assertIn('grade_level', response.data) |
| 102 | + |
| 103 | + def test_patch_student_profile(self): |
| 104 | + url = "/users/user-profile/" # replace with your actual path name |
| 105 | + response = self.client.patch(url, {'grade_level': 8}, format='json') |
| 106 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 107 | + self.assertEqual(response.data['grade_level'], 8) |
| 108 | + |
| 109 | + def test_profile_not_found(self): |
| 110 | + Profile.objects.filter(user=self.student_user).delete() # force no profile |
| 111 | + url = "/users/user-profile/" |
| 112 | + response = self.client.get(url) |
| 113 | + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) |
| 114 | + |
9 | 115 | class UserAPITestCase(TestCase): |
10 | 116 | def setUp(self): |
11 | 117 | """Set up test data and API client.""" |
|
0 commit comments