• 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
/TutorFilter/tests.py
1
from django.test import TestCase
2✔
2
from django.urls import reverse
2✔
3
from django.contrib.auth.models import User
2✔
4
from TutorRegister.models import (
2✔
5
    ProfileT,
6
    ProfileS,
7
    Expertise,
8
    UserType,
9
    Availability,
10
    TutoringSession,
11
    Favorite,
12
)
13
from django.test import Client
2✔
14
from datetime import datetime, timedelta
2✔
15
from .forms import TutoringSessionRequestForm
2✔
16
import json
2✔
17

18

19
class TutorFilterTest(TestCase):
2✔
20
    @classmethod
2✔
21
    def setUpTestData(cls):
2✔
22
        # Set up data for the whole TestCase
23
        # Create expertise, profile, and other necessary objects here
UNCOV
24
        cls.testuser = User.objects.create_user(
1✔
25
            username="testuser@example.com",
26
            password="testpassword",
27
        )
UNCOV
28
        cls.testuser2 = User.objects.create_user(
1✔
29
            username="testuser2@example.com",
30
            password="testpassword",
31
        )
UNCOV
32
        cls.testuser.usertype.user_type = "tutor"
1✔
UNCOV
33
        cls.testuser2.usertype.user_type = "student"
1✔
UNCOV
34
        cls.testuser.usertype.has_profile_complete = True
1✔
UNCOV
35
        cls.testuser2.usertype.has_profile_complete = True
1✔
UNCOV
36
        cls.testuser.usertype.save()
1✔
UNCOV
37
        cls.testuser2.usertype.save()
1✔
UNCOV
38
        Expertise.objects.create(user=cls.testuser, subject="math")
1✔
39

UNCOV
40
        cls.profile_t = ProfileT.objects.create(
1✔
41
            user=cls.testuser,
42
            preferred_mode="remote",
43
            grade="freshman",
44
            zip="12345",
45
            salary_min=50,
46
        )
UNCOV
47
        cls.profile_s = ProfileS.objects.create(
1✔
48
            user=cls.testuser2,
49
            school="CUNY",
50
            preferred_mode="remote",
51
            grade="undergrad",
52
            zip="12345",
53
        )
54

UNCOV
55
        User.objects.create_user(username="student", password="studentpassword")
1✔
56

57
    def test_filter_tutors(self):
2✔
UNCOV
58
        c = Client()
1✔
UNCOV
59
        c.login(username="student", password="studentpassword")
1✔
UNCOV
60
        form_data = {
1✔
61
            "preferred_mode": "remote",
62
            "grade": "freshman",
63
            "expertise": "math",
64
            "zipcode": "12345",
65
            "salary_max": 60,
66
            "rating": ">= 2 stars",
67
            "category": "..",
68
            "sortBy": "Lowest Price",
69
        }
UNCOV
70
        response = c.get(
1✔
71
            reverse("TutorFilter:filter_tutors"),
72
            form_data,
73
        )
UNCOV
74
        self.assertEqual(response.status_code, 200)
1✔
UNCOV
75
        users_in_context = response.context["users"]
1✔
UNCOV
76
        self.assertFalse(any(user.user == self.testuser for user in users_in_context))
1✔
77

78
    def test_filter_tutors2(self):
2✔
UNCOV
79
        c = Client()
1✔
UNCOV
80
        c.login(username="student", password="studentpassword")
1✔
UNCOV
81
        form_data2 = {
1✔
82
            "preferred_mode": "remote",
83
            "grade": "grad",
84
            "expertise": "math",
85
            "zipcode": "11111",
86
            "salary_max": 20,
87
            "rating": ">= 1 star",
88
            "category": "..",
89
            "sortBy": "Highest Rating",
90
        }
91
        # form = TutorFilterForm(data=form_data)
92

UNCOV
93
        response2 = c.post(reverse("TutorFilter:filter_tutors"), form_data2)
1✔
94

95
        # Check if the response is 200 OK
UNCOV
96
        self.assertEqual(response2.status_code, 200)
1✔
97

UNCOV
98
        response2 = c.get(
1✔
99
            reverse("TutorFilter:filter_tutors"),
100
            form_data2,
101
        )
102
        # Check if the response context contains the expected user
UNCOV
103
        self.assertEqual(len(response2.context["users"]), 0)
1✔
104

105
    def test_filter_tutors3(self):
2✔
UNCOV
106
        c = Client()
1✔
UNCOV
107
        c.login(username="student", password="studentpassword")
1✔
UNCOV
108
        form_data2 = {
1✔
109
            "preferred_mode": "remote",
110
            "grade": "grad",
111
            "expertise": "math",
112
            "zipcode": "11111",
113
            "salary_max": 20,
114
            "rating": ">= 3 stars",
115
            "category": "..",
116
            "sortBy": "Highest Price",
117
        }
118
        # form = TutorFilterForm(data=form_data)
119

UNCOV
120
        response2 = c.post(reverse("TutorFilter:filter_tutors"), form_data2)
1✔
121

122
        # Check if the response is 200 OK
UNCOV
123
        self.assertEqual(response2.status_code, 200)
1✔
124

UNCOV
125
        response2 = c.get(
1✔
126
            reverse("TutorFilter:filter_tutors"),
127
            form_data2,
128
        )
129
        # Check if the response context contains the expected user
UNCOV
130
        self.assertEqual(len(response2.context["users"]), 0)
1✔
131

132
    def test_filter_tutors4(self):
2✔
UNCOV
133
        c = Client()
1✔
UNCOV
134
        c.login(username="student", password="studentpassword")
1✔
UNCOV
135
        form_data2 = {
1✔
136
            "preferred_mode": "remote",
137
            "grade": "grad",
138
            "expertise": "math",
139
            "zipcode": "11111",
140
            "salary_max": 20,
141
            "rating": ">= 4 stars",
142
            "category": "..",
143
            "sortBy": "Highest Price",
144
        }
145
        # form = TutorFilterForm(data=form_data)
146

UNCOV
147
        response2 = c.post(reverse("TutorFilter:filter_tutors"), form_data2)
1✔
148

149
        # Check if the response is 200 OK
UNCOV
150
        self.assertEqual(response2.status_code, 200)
1✔
151

UNCOV
152
        response2 = c.get(
1✔
153
            reverse("TutorFilter:filter_tutors"),
154
            form_data2,
155
        )
156
        # Check if the response context contains the expected user
UNCOV
157
        self.assertEqual(len(response2.context["users"]), 0)
1✔
158

159
    def test_filter_tutors5(self):
2✔
UNCOV
160
        c = Client()
1✔
UNCOV
161
        c.login(username="student", password="studentpassword")
1✔
UNCOV
162
        form_data2 = {
1✔
163
            "preferred_mode": "remote",
164
            "grade": "grad",
165
            "expertise": "math",
166
            "zipcode": "11111",
167
            "salary_max": 20,
168
            "rating": "= 5 stars",
169
            "category": "..",
170
            "sortBy": "Highest Price",
171
        }
172
        # form = TutorFilterForm(data=form_data)
173

UNCOV
174
        response2 = c.post(reverse("TutorFilter:filter_tutors"), form_data2)
1✔
175

176
        # Check if the response is 200 OK
UNCOV
177
        self.assertEqual(response2.status_code, 200)
1✔
178

UNCOV
179
        response2 = c.get(
1✔
180
            reverse("TutorFilter:filter_tutors"),
181
            form_data2,
182
        )
183
        # Check if the response context contains the expected user
UNCOV
184
        self.assertEqual(len(response2.context["users"]), 0)
1✔
185

186
    def test_add_favorite(cls):
2✔
187
        # Assuming you have a URL named 'add_favorite' pointing to your add_favorite view
UNCOV
188
        c = Client()
1✔
UNCOV
189
        c.login(username="testuser2@example.com", password="testpassword")
1✔
UNCOV
190
        url = reverse("TutorFilter:add_favorite")
1✔
UNCOV
191
        data = {
1✔
192
            "category_name": "Math",
193
            "tutor_id": cls.profile_t.user.id,
194
        }
UNCOV
195
        response = c.post(url, data)
1✔
196

UNCOV
197
        cls.assertEqual(response.status_code, 200)
1✔
UNCOV
198
        cls.assertEqual(
1✔
199
            response.json(),
200
            {"status": "success", "message": "Add the tutor successfully!"},
201
        )
202
        # Check if Favorite object was indeed created
UNCOV
203
        cls.assertTrue(
1✔
204
            Favorite.objects.filter(student=cls.testuser2, tutor=cls.testuser).exists()
205
        )
206

207
    def test_remove_favorite(self):
2✔
208
        # First, create a Favorite object to be removed
UNCOV
209
        c = Client()
1✔
UNCOV
210
        c.login(username="testuser2@example.com", password="testpassword")
1✔
211

UNCOV
212
        Favorite.objects.create(
1✔
213
            category="Math", student=self.testuser2, tutor=self.testuser
214
        )
215

UNCOV
216
        url = reverse("TutorFilter:remove_favorite")
1✔
UNCOV
217
        data = {
1✔
218
            "tutor_id": self.profile_t.user.id,
219
        }
UNCOV
220
        response = c.post(url, data)
1✔
221

UNCOV
222
        self.assertEqual(response.status_code, 200)
1✔
UNCOV
223
        self.assertEqual(
1✔
224
            response.json(),
225
            {"status": "success", "message": "Remove the tutor successfully!"},
226
        )
227
        # Check if Favorite object was indeed removed
UNCOV
228
        self.assertFalse(
1✔
229
            Favorite.objects.filter(student=self.testuser, tutor=self.testuser).exists()
230
        )
231

232
    def test_view_profile_tutor(self):
2✔
233
        # Test view_profile for a tutor user
UNCOV
234
        self.client.login(username="test@example.com", password="testpassword")
1✔
UNCOV
235
        response = self.client.get(
1✔
236
            reverse("TutorFilter:view_profile", args=[self.testuser.id])
237
        )
UNCOV
238
        self.assertEqual(response.status_code, 200)
1✔
UNCOV
239
        self.assertTemplateUsed(response, "TutorFilter/view_tutor_profile.html")
1✔
240

241
    def test_view_profile_student(self):
2✔
242
        # Test view_profile for a student user
UNCOV
243
        self.client.login(username="test@nyu.edu", password="testpassword")
1✔
UNCOV
244
        response = self.client.get(
1✔
245
            reverse("TutorFilter:view_profile", args=[self.testuser2.id])
246
        )
UNCOV
247
        self.assertEqual(response.status_code, 200)
1✔
UNCOV
248
        self.assertTemplateUsed(response, "TutorFilter/view_student_profile.html")
1✔
249

250
    def test_view_tutor_profile(self):
2✔
251
        # Test view_tutor_profile
UNCOV
252
        self.client.login(username="testuser2@example.com", password="testpassword")
1✔
UNCOV
253
        response = self.client.get(
1✔
254
            reverse("TutorFilter:view_tutor_profile", args=[self.testuser.id])
255
        )
UNCOV
256
        self.assertEqual(response.status_code, 200)
1✔
UNCOV
257
        self.assertTemplateUsed(response, "TutorFilter/view_tutor_profile.html")
1✔
258

259
    def test_view_student_profile(self):
2✔
260
        # Test view_student_profile
UNCOV
261
        self.client.login(username="testuser@example.com", password="testpassword")
1✔
UNCOV
262
        response = self.client.get(
1✔
263
            reverse("TutorFilter:view_student_profile", args=[self.testuser2.id])
264
        )
UNCOV
265
        self.assertEqual(response.status_code, 200)
1✔
UNCOV
266
        self.assertTemplateUsed(response, "TutorFilter/view_student_profile.html")
1✔
267

268

269
class TutoringSessionTests(TestCase):
2✔
270
    @classmethod
2✔
271
    def setUpTestData(cls):
2✔
UNCOV
272
        cls.user = User.objects.create_user(
1✔
273
            username="tutor@test.com", password="password123"
274
        )
UNCOV
275
        cls.user.usertype.user_type = "tutor"
1✔
UNCOV
276
        cls.user.usertype.save()
1✔
UNCOV
277
        cls.url = reverse("Dashboard:tutor_profile")
1✔
UNCOV
278
        ProfileT.objects.create(
1✔
279
            user=cls.user,
280
            fname="Test",
281
            lname="User",
282
            gender="prefernottosay",
283
            major="Computer Science",
284
            zip="00000",
285
            grade="grad",
286
            preferred_mode="remote",
287
            intro="ello \nyello \njelo",
288
        )
UNCOV
289
        Availability.objects.create(
1✔
290
            user=cls.user,
291
            day_of_week="monday",
292
            start_time=datetime.strptime("09:00", "%H:%M").time(),
293
            end_time=datetime.strptime("17:00", "%H:%M").time(),
294
        )
UNCOV
295
        Expertise.objects.create(user=cls.user, subject="math")
1✔
UNCOV
296
        User.objects.create_user(username="student", password="studentpassword")
1✔
297

UNCOV
298
        cls.num_sessions = TutoringSession.objects.count()
1✔
299

300
    def setUp(self):
2✔
UNCOV
301
        self.client = Client()
1✔
UNCOV
302
        self.client.login(username="student", password="studentpassword")
1✔
303

304
    def test_request_tutoring_session_get(self):
2✔
UNCOV
305
        response = self.client.get(reverse("TutorFilter:request", args=[self.user.id]))
1✔
UNCOV
306
        self.assertEqual(response.status_code, 200)
1✔
UNCOV
307
        self.assertIsInstance(response.context["form"], TutoringSessionRequestForm)
1✔
308

309
    def test_request_tutoring_session_post_valid(self):
2✔
UNCOV
310
        data = {
1✔
311
            "tutoring_mode": "remote",
312
            "subject": "math",
313
            "date": "2024-04-01",
314
            "offering_rate": 50,
315
            "message": "Looking forward to the session!",
316
            "selected_timeslots": json.dumps([{"start": "09:00", "end": "09:30"}]),
317
        }
UNCOV
318
        response = self.client.post(
1✔
319
            reverse("TutorFilter:request", args=[self.user.id]), data
320
        )
UNCOV
321
        self.assertEqual(response.status_code, 302)
1✔
UNCOV
322
        self.assertEqual(TutoringSession.objects.count(), self.num_sessions + 1)
1✔
323

324
    def test_request_tutoring_session_post_invalid(self):
2✔
UNCOV
325
        data = {
1✔
326
            "tutoring_mode": "",
327
            "subject": "Math",
328
            "date": "2024-03-25",
329
            "offering_rate": 50,
330
            "message": "Looking forward to the session!",
331
            "selected_timeslots": json.dumps([{"start": "09:00", "end": "09:30"}]),
332
        }
UNCOV
333
        response = self.client.post(
1✔
334
            reverse("TutorFilter:request", args=[self.user.id]), data
335
        )
UNCOV
336
        self.assertEqual(response.status_code, 200)
1✔
UNCOV
337
        self.assertEqual(TutoringSession.objects.count(), self.num_sessions)
1✔
338

339
    def test_get_available_times(self):
2✔
UNCOV
340
        response = self.client.post(
1✔
341
            reverse(
342
                "TutorFilter:get-available-times", args=[self.user.id, "2024-04-01"]
343
            )
344
        )
UNCOV
345
        self.assertEqual(response.status_code, 200)
1✔
UNCOV
346
        response_data = json.loads(response.content)
1✔
UNCOV
347
        self.assertIn("available_slots", response_data)
1✔
UNCOV
348
        self.assertIn("day", response_data)
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