-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gsr_views.py
More file actions
486 lines (411 loc) · 18.7 KB
/
test_gsr_views.py
File metadata and controls
486 lines (411 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import json
from unittest import mock
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.test import TestCase
from django.urls import reverse
from rest_framework.test import APIClient
from gsr_booking.models import GSR, Group, GroupMembership, GSRBooking
User = get_user_model()
def create_test_gsrs(cls):
"""Helper to create test GSRs and store references in the class"""
cls.huntsman_gsr, _ = GSR.objects.get_or_create(
lid="JMHH",
gid=1,
defaults={
"name": "Huntsman",
"kind": GSR.KIND_WHARTON,
"image_url": "https://s3.us-east-2.amazonaws.com/labs.api/gsr/lid-JMHH-gid-1.jpg",
"in_use": True,
"bookable_days": 7,
},
)
cls.agh_gsr, _ = GSR.objects.get_or_create(
lid="20157",
gid=42437,
defaults={
"name": "Amy Gutmann Hall",
"kind": GSR.KIND_PENNGROUPS,
"image_url": "https://s3.us-east-2.amazonaws.com/labs.api/gsr/lid-20157-gid-42437.jpg",
"in_use": True,
"bookable_days": 4,
},
)
cls.weigle_gsr, _ = GSR.objects.get_or_create(
lid="1086",
gid=1889,
defaults={
"name": "Weigle",
"kind": GSR.KIND_LIBCAL,
"image_url": "https://s3.us-east-2.amazonaws.com/labs.api/gsr/lid-1086-gid-1889.jpg",
"in_use": True,
"bookable_days": 7,
},
)
def is_wharton_false(*args):
return False
def is_wharton_true(*args):
return True
def libcal_availability(*args):
with open("tests/gsr_booking/views_libcal_availability.json") as data:
return json.load(data)
def wharton_availability(*args):
with open("tests/gsr_booking/views_wharton_availability.json") as data:
return json.load(data)
def book_cancel_room(*args):
pass
def reservations(*args):
with open("tests/gsr_booking/views_reservations.json") as data:
return json.load(data)
class TestGSRs(TestCase):
"""
Tests for GSR locations endpoint with different permission levels.
setUpTestData: Creates shared test data (GSRs, Penn Labs group) - runs once per class
setUp: Creates fresh user/client for each test - runs before each test method
"""
@classmethod
def setUpTestData(cls):
create_test_gsrs(cls)
test_user = User.objects.create_user("user1", "user")
with mock.patch(
"gsr_booking.models.PennGroupsGSRBooker.is_seas", return_value=False
), mock.patch("gsr_booking.models.WhartonGSRBooker.is_wharton", return_value=False):
Group.objects.create(owner=test_user, name="Penn Labs", color="blue")
def setUp(self):
# Clear cache to avoid stale data from previous tests
cache.clear()
# Create a fresh user and client for this test
self.user = User.objects.create_user("user", "user@seas.upenn.edu", "user")
self.client = APIClient()
self.client.force_authenticate(user=self.user)
def test_get_location(self):
"""Test that the locations endpoint returns all GSRs without auth checks"""
response = self.client.get(reverse("locations"))
res_json = json.loads(response.content)
# TODO: add AGH back to availability route
for entry in res_json:
if entry["id"] == 1:
self.assertEquals(entry["name"], "Huntsman")
self.assertEquals(entry["kind"], GSR.KIND_WHARTON)
self.assertEquals(entry["lid"], "JMHH")
self.assertEquals(entry["gid"], 1)
self.assertEquals(entry["bookable_days"], 7)
if entry["id"] == 2:
self.assertEquals(entry["name"], "Amy Gutmann Hall")
self.assertEquals(entry["kind"], GSR.KIND_PENNGROUPS)
self.assertEquals(entry["lid"], "20157")
self.assertEquals(entry["gid"], 42437)
self.assertEquals(entry["bookable_days"], 4)
if entry["id"] == 3:
self.assertEquals(entry["name"], "Weigle")
self.assertEquals(entry["kind"], GSR.KIND_LIBCAL)
self.assertEquals(entry["lid"], "1086")
self.assertEquals(entry["gid"], 1889)
self.assertEquals(entry["bookable_days"], 7)
@mock.patch("gsr_booking.views.WhartonGSRBooker.is_wharton", return_value=False)
@mock.patch("gsr_booking.views.PennGroupsGSRBooker.is_seas", return_value=False)
def test_get_user_location(self, mock_is_seas, mock_is_wharton):
"""Test that regular users only see LibCal GSRs"""
response = self.client.get(reverse("user-locations"))
res_json = json.loads(response.content)
gsr_ids = [entry["id"] for entry in res_json]
self.assertNotIn(self.huntsman_gsr.id, gsr_ids)
self.assertNotIn(self.agh_gsr.id, gsr_ids)
self.assertIn(self.weigle_gsr.id, gsr_ids)
self.assertEqual(len(res_json), 1)
for entry in res_json:
self.assertEqual(entry["kind"], GSR.KIND_LIBCAL)
@mock.patch("gsr_booking.views.WhartonGSRBooker.is_wharton", return_value=True)
@mock.patch("gsr_booking.views.PennGroupsGSRBooker.is_seas", return_value=False)
def test_get_user_location_wharton_user(self, mock_is_seas, mock_is_wharton):
"""Test that Wharton users see LibCal and Wharton GSRs"""
response = self.client.get(reverse("user-locations"))
res_json = json.loads(response.content)
# Wharton users should see LibCal and Wharton GSRs
kinds_seen = set()
for entry in res_json:
self.assertIn("kind", entry)
kinds_seen.add(entry["kind"])
self.assertIn(GSR.KIND_LIBCAL, kinds_seen)
self.assertIn(GSR.KIND_WHARTON, kinds_seen)
self.assertNotIn(GSR.KIND_PENNGROUPS, kinds_seen)
@mock.patch("gsr_booking.views.WhartonGSRBooker.is_wharton", return_value=False)
@mock.patch("gsr_booking.views.PennGroupsGSRBooker.is_seas", return_value=True)
def test_get_user_location_seas_user(self, mock_is_seas, mock_is_wharton):
"""Test that SEAS users see LibCal and PennGroups GSRs"""
response = self.client.get(reverse("user-locations"))
res_json = json.loads(response.content)
# SEAS users should see LibCal and PennGroups GSRs
kinds_seen = set()
for entry in res_json:
self.assertIn("kind", entry)
kinds_seen.add(entry["kind"])
self.assertIn(GSR.KIND_LIBCAL, kinds_seen)
self.assertIn(GSR.KIND_PENNGROUPS, kinds_seen)
self.assertNotIn(GSR.KIND_WHARTON, kinds_seen)
@mock.patch("gsr_booking.views.WhartonGSRBooker.is_wharton", return_value=True)
@mock.patch("gsr_booking.views.PennGroupsGSRBooker.is_seas", return_value=True)
def test_get_user_location_wharton_seas_user(self, mock_is_seas, mock_is_wharton):
"""Test that users with both Wharton and SEAS access see all non-Penn Labs GSRs"""
response = self.client.get(reverse("user-locations"))
res_json = json.loads(response.content)
# Users with both should see all kinds
kinds_seen = set()
for entry in res_json:
self.assertIn("kind", entry)
kinds_seen.add(entry["kind"])
self.assertIn(GSR.KIND_LIBCAL, kinds_seen)
self.assertIn(GSR.KIND_WHARTON, kinds_seen)
self.assertIn(GSR.KIND_PENNGROUPS, kinds_seen)
@mock.patch("gsr_booking.models.PennGroupsGSRBooker.is_seas", return_value=False)
@mock.patch("gsr_booking.models.WhartonGSRBooker.is_wharton", return_value=False)
def test_get_user_location_penn_labs_member(self, mock_is_wharton, mock_is_seas):
"""Test that Penn Labs members see all GSRs regardless of their individual status"""
# Add user to Penn Labs group
penn_labs_group = Group.objects.get(name="Penn Labs")
GroupMembership.objects.create(
user=self.user, group=penn_labs_group, accepted=True, type=GroupMembership.MEMBER
)
response = self.client.get(reverse("user-locations"))
res_json = json.loads(response.content)
# Penn Labs members should see all GSRs
# Verify all kinds are present in the response
kinds_seen = set()
for entry in res_json:
self.assertIn("kind", entry)
kinds_seen.add(entry["kind"])
# Should see all available kinds (check that all kinds in database are in response)
expected_kinds = set(GSR.objects.values_list("kind", flat=True).distinct())
# Verify that all expected kinds are present (use subset check for robustness)
self.assertTrue(
expected_kinds.issubset(kinds_seen),
f"Expected kinds {expected_kinds} not all found in {kinds_seen}",
)
# Verify that test GSRs are present
gsr_ids = [entry["id"] for entry in res_json]
self.assertIn(self.huntsman_gsr.id, gsr_ids, "Huntsman GSR should be visible")
self.assertIn(self.agh_gsr.id, gsr_ids, "AGH GSR should be visible")
self.assertIn(self.weigle_gsr.id, gsr_ids, "Weigle GSR should be visible")
class TestGSRFunctions(TestCase):
@classmethod
def setUpTestData(cls):
create_test_gsrs(cls)
def setUp(self):
self.user = User.objects.create_user("user", "user@seas.upenn.edu", "user")
self.client = APIClient()
self.client.force_authenticate(user=self.user)
def test_recent(self):
gsrs = list(GSR.objects.all())
GSRBooking.objects.create(user=self.user, room_id=1, room_name="Room 1", gsr=gsrs[0])
GSRBooking.objects.create(user=self.user, room_id=3, room_name="Room 3", gsr=gsrs[0])
GSRBooking.objects.create(user=self.user, room_id=2, room_name="Room 2", gsr=gsrs[1])
GSRBooking.objects.create(user=self.user, room_id=3, room_name="Room 3", gsr=gsrs[2])
response = self.client.get(reverse("recent-gsrs"))
res_json = json.loads(response.content)
self.assertEqual(2, len(res_json))
self.assertEqual(7, len(res_json[0]))
self.assertEqual(7, len(res_json[1]))
self.assertIn("id", res_json[0])
self.assertIn("kind", res_json[0])
self.assertIn("lid", res_json[0])
self.assertIn("gid", res_json[0])
self.assertIn("name", res_json[0])
self.assertIn("image_url", res_json[0])
self.assertIn("bookable_days", res_json[0])
self.assertNotEqual(res_json[0]["id"], res_json[1]["id"])
@mock.patch("gsr_booking.api_wrapper.WhartonBookingWrapper.is_wharton", is_wharton_false)
def test_get_wharton_false(self):
response = self.client.get(reverse("is-wharton"))
res_json = json.loads(response.content)
self.assertEqual(1, len(res_json))
self.assertFalse(res_json["is_wharton"])
@mock.patch("gsr_booking.api_wrapper.WhartonBookingWrapper.is_wharton", is_wharton_true)
def test_get_wharton_true(self):
response = self.client.get(reverse("is-wharton"))
res_json = json.loads(response.content)
self.assertEqual(1, len(res_json))
self.assertTrue(res_json["is_wharton"])
@mock.patch("gsr_booking.api_wrapper.BookingHandler.get_availability", libcal_availability)
def test_availability_libcal(self):
response = self.client.get(reverse("availability", args=["1086", "1889"]))
res_json = json.loads(response.content)
self.assertEqual(3, len(res_json))
self.assertIn("name", res_json)
self.assertIn("gid", res_json)
self.assertIn("rooms", res_json)
if len(res_json["rooms"]) > 0:
room = res_json["rooms"][0]
self.assertIn("room_name", room)
self.assertIn("id", room)
self.assertIn("availability", room)
@mock.patch("gsr_booking.api_wrapper.BookingHandler.get_availability", wharton_availability)
def test_availability_wharton(self):
response = self.client.get(reverse("availability", args=["JMHH", "1"]))
res_json = json.loads(response.content)
self.assertEqual(3, len(res_json))
self.assertIn("name", res_json)
self.assertIn("gid", res_json)
self.assertIn("rooms", res_json)
if len(res_json["rooms"]) > 0:
room = res_json["rooms"][0]
self.assertIn("room_name", room)
self.assertIn("id", room)
self.assertIn("availability", room)
@mock.patch("gsr_booking.api_wrapper.BookingHandler.book_room", book_cancel_room)
def test_book_libcal(self):
payload = {
"start_time": "2021-11-21T18:30:00-05:00",
"end_time": "2021-11-21T19:00:00-05:00",
"room_name": "VP WIC Booth 01",
"id": 7192,
"gid": 1889,
}
response = self.client.post(
reverse("book"), json.dumps(payload), content_type="application/json"
)
res_json = json.loads(response.content)
self.assertEqual(1, len(res_json))
self.assertEqual("success", res_json["detail"])
@mock.patch("gsr_booking.api_wrapper.BookingHandler.book_room", book_cancel_room)
def test_book_wharton(self):
payload = {
"start_time": "2021-11-21T18:30:00-05:00",
"end_time": "2021-11-21T19:00:00-05:00",
"room_name": "241",
"id": 94,
"gid": 1,
}
response = self.client.post(
reverse("book"), json.dumps(payload), content_type="application/json"
)
res_json = json.loads(response.content)
self.assertEqual(1, len(res_json))
self.assertEqual("success", res_json["detail"])
@mock.patch("gsr_booking.api_wrapper.BookingHandler.cancel_room", book_cancel_room)
def test_cancel_room(self):
payload = {"booking_id": "booking id"}
response = self.client.post(
reverse("cancel"), json.dumps(payload), content_type="application/json"
)
res_json = json.loads(response.content)
self.assertEqual(1, len(res_json))
self.assertEqual("success", res_json["detail"])
@mock.patch("gsr_booking.api_wrapper.BookingHandler.get_reservations", reservations)
def test_reservations(self):
response = self.client.get(reverse("reservations"))
res_json = json.loads(response.content)
self.assertEqual(6, len(res_json))
entry = res_json[0]
self.assertIn("booking_id", entry)
self.assertIn("gsr", entry)
self.assertIn("room_id", entry)
self.assertIn("room_name", entry)
self.assertIn("start", entry)
self.assertIn("end", entry)
gsr = entry["gsr"]
self.assertIn("id", gsr)
self.assertIn("kind", gsr)
self.assertIn("lid", gsr)
self.assertIn("gid", gsr)
self.assertIn("name", gsr)
self.assertIn("image_url", gsr)
class TestSEASViews(TestCase):
@classmethod
def setUpTestData(cls):
create_test_gsrs(cls)
def setUp(self):
self.user = User.objects.create_user("user", "user@seas.upenn.edu", "user")
self.client = APIClient()
self.client.force_authenticate(user=self.user)
@mock.patch("gsr_booking.api_wrapper.PennGroupsBookingWrapper.is_seas", return_value=True)
def test_check_seas_true(self, mock_is_seas):
response = self.client.get(reverse("is-seas"))
res_json = json.loads(response.content)
self.assertEqual(1, len(res_json))
self.assertTrue(res_json["is_seas"])
@mock.patch("gsr_booking.api_wrapper.PennGroupsBookingWrapper.is_seas", return_value=False)
def test_check_seas_false(self, mock_is_seas):
response = self.client.get(reverse("is-seas"))
res_json = json.loads(response.content)
self.assertEqual(1, len(res_json))
self.assertFalse(res_json["is_seas"])
def penngroups_availability(*args):
"""Mock AGH availability response"""
return {
"name": "Amy Gutmann Hall",
"gid": 42437,
"rooms": [
{
"room_name": "AGH 206",
"id": 172129,
"availability": [
{
"start_time": "2025-10-30T23:30:00-04:00",
"end_time": "2025-10-30T23:59:00-04:00",
}
],
},
{
"room_name": "AGH 334",
"id": 191384,
"availability": [
{
"start_time": "2025-10-30T23:30:00-04:00",
"end_time": "2025-10-30T23:59:00-04:00",
}
],
},
],
}
def penngroups_book_cancel(*args):
"""Mock AGH book/cancel response"""
pass
class TestPennGroupsViews(TestCase):
@classmethod
def setUpTestData(cls):
create_test_gsrs(cls)
def setUp(self):
self.user = User.objects.create_user("user", "user@seas.upenn.edu", "user")
self.client = APIClient()
self.client.force_authenticate(user=self.user)
@mock.patch("gsr_booking.api_wrapper.BookingHandler.get_availability", penngroups_availability)
def test_availability_penngroups(self):
"""Test AGH availability endpoint"""
response = self.client.get(reverse("availability", args=["20157", "42437"]))
res_json = json.loads(response.content)
self.assertEqual(3, len(res_json))
self.assertIn("name", res_json)
self.assertIn("gid", res_json)
self.assertIn("rooms", res_json)
self.assertEqual("Amy Gutmann Hall", res_json["name"])
if len(res_json["rooms"]) > 0:
room = res_json["rooms"][0]
self.assertIn("room_name", room)
self.assertIn("id", room)
self.assertIn("availability", room)
@mock.patch("gsr_booking.api_wrapper.BookingHandler.book_room", penngroups_book_cancel)
def test_book_penngroups(self):
"""Test AGH booking endpoint"""
payload = {
"start_time": "2025-10-30T23:30:00-04:00",
"end_time": "2025-10-31T00:00:00-04:00",
"room_name": "AGH 206",
"id": 172129,
"gid": 42437,
}
response = self.client.post(
reverse("book"), json.dumps(payload), content_type="application/json"
)
res_json = json.loads(response.content)
self.assertEqual(1, len(res_json))
self.assertEqual("success", res_json["detail"])
@mock.patch("gsr_booking.api_wrapper.BookingHandler.cancel_room", penngroups_book_cancel)
def test_cancel_penngroups(self):
"""Test AGH cancel endpoint"""
payload = {"booking_id": "476df5dc92c1"}
response = self.client.post(
reverse("cancel"), json.dumps(payload), content_type="application/json"
)
res_json = json.loads(response.content)
self.assertEqual(1, len(res_json))
self.assertEqual("success", res_json["detail"])