• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gcivil-nyu-org / INT2-Monday-Spring2024-Team-2 / 719

15 Apr 2024 04:27PM UTC coverage: 90.674%. Remained the same
719

push

travis-pro

web-flow
Merge pull request #237 from gcivil-nyu-org/feature-shihui

exempt contact from middleware

1 of 1 new or added line in 1 file covered. (100.0%)

713 existing lines in 14 files now uncovered.

1789 of 1973 relevant lines covered (90.67%)

1.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/TutorRegister/tests/test_forms.py
1
from django.test import TestCase
2✔
2
from django.contrib.auth.models import User
2✔
3
from TutorRegister.forms.register_login import RegisterUserForm
2✔
4
from TutorRegister.forms.student_info import StudentForm
2✔
5
from TutorRegister.models import ProfileS
2✔
6

7

8
class RegisterUserFormTest(TestCase):
2✔
9
    def setUp(self):
2✔
UNCOV
10
        User.objects.create_user(
1✔
11
            username="existinguser@example.com",
12
            email="existinguser@example.com",
13
            password="12345",
14
        )
15

16
    def test_form_valid(self):
2✔
UNCOV
17
        form_data = {
1✔
18
            "user_type": "student",
19
            "email": "newuser@example.com",
20
            "first_name": "John",
21
            "last_name": "Doe",
22
            "password1": "Password123...",
23
            "password2": "Password123...",
24
        }
UNCOV
25
        form = RegisterUserForm(data=form_data)
1✔
UNCOV
26
        self.assertTrue(form.is_valid())
1✔
27

28
    def test_form_invalid_email_exists(self):
2✔
UNCOV
29
        form_data = {
1✔
30
            "user_type": "student",
31
            "email": "existinguser@example.com",
32
            "first_name": "John",
33
            "last_name": "Doe",
34
            "password1": "password123",
35
            "password2": "password123",
36
        }
UNCOV
37
        form = RegisterUserForm(data=form_data)
1✔
UNCOV
38
        self.assertFalse(form.is_valid())
1✔
UNCOV
39
        self.assertIn("email", form.errors)
1✔
UNCOV
40
        self.assertEqual(
1✔
41
            form.errors["email"], ["A user with that email already exists."]
42
        )
43

44
    def test_form_invalid_tutor_email(self):
2✔
UNCOV
45
        form_data = {
1✔
46
            "user_type": "tutor",
47
            "email": "tutor@example.com",
48
            "first_name": "John",
49
            "last_name": "Doe",
50
            "password1": "password123",
51
            "password2": "password123",
52
        }
UNCOV
53
        form = RegisterUserForm(data=form_data)
1✔
UNCOV
54
        self.assertFalse(form.is_valid())
1✔
UNCOV
55
        self.assertIn("email", form.errors)
1✔
UNCOV
56
        self.assertEqual(
1✔
57
            form.errors["email"], ["Tutor email address must end with '@nyu.edu'."]
58
        )
59

60
    def test_form_empty_first_name(self):
2✔
UNCOV
61
        form_data = {
1✔
62
            "user_type": "student",
63
            "email": "newuser@example.com",
64
            "first_name": "",
65
            "last_name": "Doe",
66
            "password1": "password123",
67
            "password2": "password123",
68
        }
UNCOV
69
        form = RegisterUserForm(data=form_data)
1✔
UNCOV
70
        self.assertFalse(form.is_valid())
1✔
UNCOV
71
        self.assertIn("first_name", form.errors)
1✔
UNCOV
72
        self.assertEqual(
1✔
73
            form.errors["first_name"],
74
            ["This field is required.", "First name cannot be empty."],
75
        )
76

77
    def test_form_empty_last_name(self):
2✔
UNCOV
78
        form_data = {
1✔
79
            "user_type": "student",
80
            "email": "newuser@example.com",
81
            "first_name": "John",
82
            "last_name": "",
83
            "password1": "password123",
84
            "password2": "password123",
85
        }
UNCOV
86
        form = RegisterUserForm(data=form_data)
1✔
UNCOV
87
        self.assertFalse(form.is_valid())
1✔
UNCOV
88
        self.assertIn("last_name", form.errors)
1✔
UNCOV
89
        self.assertEqual(
1✔
90
            form.errors["last_name"],
91
            ["This field is required.", "Last name cannot be empty."],
92
        )
93

94
    def test_save_new_user(self):
2✔
UNCOV
95
        form_data = {
1✔
96
            "user_type": "student",
97
            "email": "newuser@example.com",
98
            "first_name": "John",
99
            "last_name": "Doe",
100
            "password1": "Testpassword123",
101
            "password2": "Testpassword123",
102
        }
UNCOV
103
        form = RegisterUserForm(data=form_data)
1✔
UNCOV
104
        self.assertTrue(form.is_valid())
1✔
105

UNCOV
106
        user = form.save()
1✔
UNCOV
107
        self.assertIsInstance(user, User)
1✔
UNCOV
108
        self.assertEqual(user.username, form_data["email"])
1✔
UNCOV
109
        self.assertEqual(user.first_name, form_data["first_name"])
1✔
UNCOV
110
        self.assertEqual(user.last_name, form_data["last_name"])
1✔
UNCOV
111
        self.assertTrue(user.check_password(form_data["password1"]))
1✔
112

113
    def test_save_existing_user(self):
2✔
UNCOV
114
        User.objects.create_user(
1✔
115
            username="existing@example.com", password="Testpassword123"
116
        )
UNCOV
117
        form_data = {
1✔
118
            "user_type": "student",
119
            "email": "existing@example.com",
120
            "first_name": "John",
121
            "last_name": "Doe",
122
            "password1": "Testpassword123",
123
            "password2": "Testpassword123",
124
        }
UNCOV
125
        form = RegisterUserForm(data=form_data)
1✔
UNCOV
126
        self.assertFalse(form.is_valid())
1✔
UNCOV
127
        self.assertIn("email", form.errors)
1✔
128

129

130
class StudentFormTestCase(TestCase):
2✔
131
    def test_student_form_valid_data(self):
2✔
UNCOV
132
        form = StudentForm(
1✔
133
            data={
134
                "fname": "John",
135
                "lname": "Doe",
136
                "gender": "male",
137
                "zip": "12345",
138
                "school": "ABC High School",
139
                "grade": "10",
140
                "preferred_mode": "inperson",
141
                "intro": "I am a 10th-grade student.",
142
            }
143
        )
UNCOV
144
        self.assertTrue(form.is_valid())
1✔
145

146
    def test_student_form_invalid_data(self):
2✔
UNCOV
147
        form = StudentForm(data={})
1✔
UNCOV
148
        self.assertFalse(form.is_valid())
1✔
UNCOV
149
        self.assertEqual(len(form.errors), 8)  # Assuming all fields are required
1✔
150

151
    def test_student_form_gender_choices(self):
2✔
UNCOV
152
        form = StudentForm()
1✔
UNCOV
153
        self.assertEqual(
1✔
154
            form.fields["gender"].choices,
155
            [
156
                ("female", "Female"),
157
                ("male", "Male"),
158
                ("prefernottosay", "Prefer not to say"),
159
            ],
160
        )
161

162
    def test_student_form_grade_choices(self):
2✔
UNCOV
163
        form = StudentForm()
1✔
UNCOV
164
        self.assertEqual(
1✔
165
            form.fields["grade"].choices,
166
            [
167
                ("1", "1st Grade"),
168
                ("2", "2nd Grade"),
169
                ("3", "3rd Grade"),
170
                ("4", "4th Grade"),
171
                ("5", "5th Grade"),
172
                ("6", "6th Grade"),
173
                ("7", "7th Grade"),
174
                ("8", "8th Grade"),
175
                ("9", "9th Grade"),
176
                ("10", "10th Grade"),
177
                ("11", "11th Grade"),
178
                ("12", "12th Grade"),
179
                ("undergrad", "Undergraduate"),
180
                ("grad", "Graduate"),
181
                ("postgrad", "Post-graduate"),
182
            ],
183
        )
184

185
    def test_student_form_preferred_mode_choices(self):
2✔
UNCOV
186
        form = StudentForm()
1✔
UNCOV
187
        self.assertEqual(
1✔
188
            form.fields["preferred_mode"].choices,
189
            [
190
                ("inperson", "In-person"),
191
                ("remote", "Remote"),
192
                ("both", "Both"),
193
            ],
194
        )
195

196
    def test_student_form_empty_fields(self):
2✔
UNCOV
197
        form = StudentForm(
1✔
198
            data={
199
                "fname": "",
200
                "lname": "",
201
                "gender": "",
202
                "zip": "",
203
                "school": "",
204
                "grade": "",
205
                "preferred_mode": "",
206
                "intro": "",
207
            }
208
        )
UNCOV
209
        self.assertFalse(form.is_valid())
1✔
UNCOV
210
        self.assertEqual(len(form.errors), 8)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc