-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_serializers.py
More file actions
726 lines (653 loc) · 27.7 KB
/
test_serializers.py
File metadata and controls
726 lines (653 loc) · 27.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
import json
from datetime import datetime
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test import RequestFactory, TestCase
from django.urls import reverse
from django.utils import timezone
from rest_framework import serializers
from rest_framework.test import APIClient
from schedule.models import Event
from ohq.models import Announcement, Course, Membership, Question, Queue, Semester, Tag
from ohq.serializers import (
CourseCreateSerializer,
MembershipSerializer,
SemesterSerializer,
UserPrivateSerializer,
)
User = get_user_model()
class UserPrivateSerializerTestCase(TestCase):
def setUp(self):
self.user = User.objects.create(username="user")
self.serializer = UserPrivateSerializer(instance=self.user)
def test_set_sms_notifications_enabled(self):
self.assertFalse(self.user.profile.sms_notifications_enabled)
self.serializer.update(self.user, {"profile": {"sms_notifications_enabled": True}})
self.assertTrue(self.user.profile.sms_notifications_enabled)
def test_set_new_phone_number(self):
self.assertIsNone(self.user.profile.sms_verification_timestamp)
phone_number = "+15555555555"
self.serializer.update(self.user, {"profile": {"phone_number": phone_number}})
self.assertFalse(self.user.profile.sms_verified)
self.assertEqual(self.user.profile.phone_number, phone_number)
self.assertIsNotNone(self.user.profile.sms_verification_timestamp)
self.assertRegex(self.user.profile.sms_verification_code, "[0-9]{6}")
def test_verify_phone_number(self):
self.serializer.update(self.user, {"profile": {"phone_number": "+15555555555"}})
self.serializer.update(
self.user,
{"profile": {"sms_verification_code": self.user.profile.sms_verification_code}},
)
self.assertTrue(self.user.profile.sms_verified)
def test_verify_phone_number_invalid(self):
self.serializer.update(self.user, {"profile": {"phone_number": "+15555555555"}})
with self.assertRaises(serializers.ValidationError):
self.serializer.update(
self.user,
{"profile": {"sms_verification_code": "ABC123"}},
)
def test_verify_phone_number_time_expired(self):
self.serializer.update(self.user, {"profile": {"phone_number": "+15555555555"}})
date = timezone.make_aware(datetime(2020, 1, 1))
self.user.profile.sms_verification_timestamp = date
with self.assertRaises(serializers.ValidationError):
self.serializer.update(
self.user,
{"profile": {"sms_verification_code": self.user.profile.sms_verification_code}},
)
class CourseCreateSerializerTestCase(TestCase):
def test_create_membership(self):
semester = Semester.objects.create(year=2020, term=Semester.TERM_SUMMER)
user = User.objects.create(username="user")
data = {
"course_code": "000",
"department": "Penn Labs",
"course_title": "Course",
"semester": semester.id,
"created_role": Membership.KIND_HEAD_TA,
}
request = RequestFactory().get("/")
request.user = user
serializer = CourseCreateSerializer(data=data, context={"request": request})
serializer.is_valid()
serializer.save()
self.assertEqual(1, len(Membership.objects.all()))
membership = Membership.objects.get(user=user)
self.assertEqual(membership.kind, Membership.KIND_HEAD_TA)
class MembershipSerializerTestCase(TestCase):
def test_create_membership(self):
semester = Semester.objects.create(year=2020, term=Semester.TERM_SUMMER)
course = Course.objects.create(course_code="000", department="Penn Labs", semester=semester)
user = User.objects.create(username="professor")
class View(object):
"""
Mock view object to provide a course pk
"""
def __init__(self):
self.kwargs = {"course_pk": course.id}
request = RequestFactory().get("/")
request.user = user
serializer = MembershipSerializer(data={}, context={"request": request, "view": View()})
serializer.is_valid()
serializer.save()
self.assertEqual(1, len(Membership.objects.all()))
class SemesterSerializerTestCase(TestCase):
def test_pretty(self):
semester = Semester.objects.create(year=2020, term=Semester.TERM_SUMMER)
serializer = SemesterSerializer(instance=semester)
self.assertEqual(serializer.data["pretty"], str(semester))
class QueueSerializerTestCase(TestCase):
def setUp(self):
self.client = APIClient()
self.semester = Semester.objects.create(year=2020, term=Semester.TERM_SUMMER)
self.course = Course.objects.create(
course_code="000", department="Penn Labs", semester=self.semester
)
self.name = "Queue"
self.queue = Queue.objects.create(name=self.name, course=self.course)
self.pin = "AAAAA"
self.pin_queue_name = "Pin Queue"
self.pin_queue = Queue.objects.create(
name=self.pin_queue_name, course=self.course, pin_enabled=True, pin=self.pin
)
self.head_ta = User.objects.create(username="head_ta")
self.ta = User.objects.create(username="ta")
self.student = User.objects.create(username="student")
Membership.objects.create(
course=self.course, user=self.head_ta, kind=Membership.KIND_HEAD_TA
)
Membership.objects.create(course=self.course, user=self.ta, kind=Membership.KIND_TA)
Membership.objects.create(
course=self.course, user=self.student, kind=Membership.KIND_STUDENT
)
def test_update_active_ta(self):
"""
Ensure TAs can open and close a queue.
"""
# TAs+ can activate queue but not students
self.assertFalse(self.queue.active)
self.client.force_authenticate(user=self.ta)
self.client.patch(
reverse("ohq:queue-detail", args=[self.course.id, self.queue.id]), {"active": True}
)
self.queue.refresh_from_db()
self.assertTrue(self.queue.active)
self.queue.active = False
self.queue.save()
self.client.force_authenticate(user=self.student)
self.client.patch(
reverse("ohq:queue-detail", args=[self.course.id, self.queue.id]), {"active": True}
)
self.assertFalse(self.queue.active)
def test_update_other_ta(self):
"""
TAs can't modify other attributes of a queue
"""
self.assertEqual(self.name, self.queue.name)
self.client.force_authenticate(user=self.ta)
self.client.patch(
reverse("ohq:queue-detail", args=[self.course.id, self.queue.id]), {"name": "New"}
)
self.queue.refresh_from_db()
self.assertEqual(self.name, self.queue.name)
def test_update_leadership(self):
"""
Course leadership can modify anything
"""
self.assertEqual(self.name, self.queue.name)
self.client.force_authenticate(user=self.head_ta)
new_name = "New Queue Name"
self.client.patch(
reverse("ohq:queue-detail", args=[self.course.id, self.queue.id]), {"name": new_name}
)
self.queue.refresh_from_db()
self.assertEqual(new_name, self.queue.name)
def test_generate_pin(self):
"""
Ensure pin is generated when queue is opened
"""
# queue without pin enabled shouldn't generate pin
self.client.force_authenticate(user=self.head_ta)
old_pin = self.queue.pin
self.client.patch(
reverse("ohq:queue-detail", args=[self.course.id, self.queue.id]), {"active": True}
)
self.queue.refresh_from_db()
self.assertEqual(old_pin, self.queue.pin)
# queue with pin enabled generates new pin
self.queue.pin_enabled = True
self.queue.active = False
self.queue.save()
self.client.patch(
reverse("ohq:queue-detail", args=[self.course.id, self.queue.id]), {"active": True}
)
self.queue.refresh_from_db()
self.assertNotEqual(old_pin, self.queue.pin)
self.queue.active = False
self.queue.save()
old_pin = self.queue.pin
self.client.force_authenticate(user=self.ta)
self.client.patch(
reverse("ohq:queue-detail", args=[self.course.id, self.queue.id]), {"active": True}
)
self.queue.refresh_from_db()
self.assertNotEqual(old_pin, self.queue.pin)
# TAs+ (but not student) can change pin
self.queue.pin_enabled = True
self.queue.save()
manual_update_pin = "BBBBB"
self.client.force_authenticate(user=self.ta)
self.client.patch(
reverse("ohq:queue-detail", args=[self.course.id, self.queue.id]),
{"pin": manual_update_pin},
)
self.queue.refresh_from_db()
self.assertEqual(manual_update_pin, self.queue.pin)
self.queue.pin = self.pin
self.queue.save()
self.client.force_authenticate(user=self.student)
self.client.patch(
reverse("ohq:queue-detail", args=[self.course.id, self.queue.id]),
{"pin": manual_update_pin},
)
self.queue.refresh_from_db()
self.assertNotEqual(manual_update_pin, self.queue.pin)
self.assertEqual(self.pin, self.queue.pin)
def test_get_pin(self):
"""
Ensure only TAs can get pin in queue detail
"""
# student should not get pin
self.client.force_authenticate(user=self.student)
response = self.client.get(
reverse("ohq:queue-detail", args=[self.course.id, self.pin_queue.id])
)
content = json.loads(response.content)
self.assertTrue("pin" not in content)
# TAs should get pin
self.client.force_authenticate(user=self.ta)
response = self.client.get(
reverse("ohq:queue-detail", args=[self.course.id, self.pin_queue.id])
)
content = json.loads(response.content)
self.assertEqual(content["pin"], self.pin)
@patch("ohq.serializers.sendUpNextNotificationTask.delay")
class QuestionSerializerTestCase(TestCase):
def setUp(self):
self.client = APIClient()
self.semester = Semester.objects.create(year=2020, term=Semester.TERM_SUMMER)
self.course = Course.objects.create(
course_code="000", department="Penn Labs", semester=self.semester
)
self.course2 = Course.objects.create(
course_code="001", department="Penn Labs", semester=self.semester
)
self.queue = Queue.objects.create(name="Queue", course=self.course)
self.queue2 = Queue.objects.create(name="Queue", course=self.course2)
self.ta = User.objects.create(username="ta")
self.student = User.objects.create(username="student")
self.student2 = User.objects.create(username="student2")
Tag.objects.create(course=self.course, name="Tag")
Tag.objects.create(course=self.course2, name="Tag")
Membership.objects.create(course=self.course, user=self.ta, kind=Membership.KIND_TA)
Membership.objects.create(
course=self.course, user=self.student, kind=Membership.KIND_STUDENT
)
Membership.objects.create(
course=self.course, user=self.student2, kind=Membership.KIND_STUDENT
)
self.question_text = "This is a question"
self.question = Question.objects.create(
queue=self.queue, asked_by=self.student, text=self.question_text
)
def test_create(self, mock_delay):
self.client.force_authenticate(user=self.student2)
self.client.post(
reverse("ohq:question-list", args=[self.course.id, self.queue.id]),
{"text": "Help me", "tags": [{"name": "Tag"}]},
)
self.assertEqual(2, Question.objects.all().count())
question = Question.objects.all().order_by("time_asked")[1]
self.assertEqual(self.student2, question.asked_by)
self.assertEqual(Question.STATUS_ASKED, question.status)
mock_delay.assert_not_called()
def test_student_update(self, mock_delay):
"""
Ensure a student can update their question's text and videochat link.
"""
text = "Different"
url = "https://example.com"
student_descriptor = "In the back"
self.client.force_authenticate(user=self.student)
self.client.patch(
reverse("ohq:question-detail", args=[self.course.id, self.queue.id, self.question.id]),
{
"text": text,
"video_chat_url": url,
"tags": [{"name": "Tag"}],
"student_descriptor": student_descriptor,
},
)
self.question.refresh_from_db()
self.assertEqual(text, self.question.text)
self.assertEqual(url, self.question.video_chat_url)
self.assertEqual(student_descriptor, self.question.student_descriptor)
mock_delay.assert_not_called()
def test_student_update_note(self, mock_delay):
"""
Ensure a note is removed when a student modifies their question.
"""
self.question.note = "Make changes"
self.question.resolved_note = False
self.question.save()
self.client.force_authenticate(user=self.student)
self.client.patch(
reverse("ohq:question-detail", args=[self.course.id, self.queue.id, self.question.id]),
{"text": "different"},
)
self.question.refresh_from_db()
self.assertTrue(self.question.resolved_note)
self.assertEqual(self.question.note, "")
mock_delay.assert_not_called()
def test_student_withdraw(self, mock_delay):
"""
Ensure a student can withdraw their question.
"""
self.client.force_authenticate(user=self.student)
self.client.patch(
reverse("ohq:question-detail", args=[self.course.id, self.queue.id, self.question.id]),
{"status": Question.STATUS_WITHDRAWN},
)
self.question.refresh_from_db()
self.assertEqual(Question.STATUS_WITHDRAWN, self.question.status)
mock_delay.assert_called()
def test_student_active(self, mock_delay):
"""
Ensure students can't mark a question as active
"""
self.client.force_authenticate(user=self.student)
response = self.client.patch(
reverse("ohq:question-detail", args=[self.course.id, self.queue.id, self.question.id]),
{"status": Question.STATUS_ACTIVE},
)
self.assertEqual(400, response.status_code)
self.question.refresh_from_db()
self.assertEqual(Question.STATUS_ASKED, self.question.status)
mock_delay.assert_not_called()
def test_student_answered(self, mock_delay):
"""
Ensure a student can mark their question as answered
"""
self.client.force_authenticate(user=self.student)
self.client.patch(
reverse("ohq:question-detail", args=[self.course.id, self.queue.id, self.question.id]),
{"status": Question.STATUS_ANSWERED},
)
self.question.refresh_from_db()
self.assertEqual(Question.STATUS_ANSWERED, self.question.status)
mock_delay.assert_not_called()
def test_ta_update(self, mock_delay):
"""
Ensure TAs+ can start answering, undo answering, and reject a question
"""
self.client.force_authenticate(user=self.ta)
# Start answering the question
self.client.patch(
reverse("ohq:question-detail", args=[self.course.id, self.queue.id, self.question.id]),
{"status": Question.STATUS_ACTIVE},
)
self.question.refresh_from_db()
self.assertEqual(Question.STATUS_ACTIVE, self.question.status)
self.assertEqual(self.ta, self.question.responded_to_by)
# Add a note to the question
note = "Invalid zoom link"
self.client.patch(
reverse("ohq:question-detail", args=[self.course.id, self.queue.id, self.question.id]),
{"note": note},
)
self.question.refresh_from_db()
self.assertFalse(self.question.resolved_note)
self.assertEqual(self.question.note, note)
# Undo and put user back in queue
self.client.patch(
reverse("ohq:question-detail", args=[self.course.id, self.queue.id, self.question.id]),
{"status": Question.STATUS_ASKED},
)
self.question.refresh_from_db()
self.assertEqual(Question.STATUS_ASKED, self.question.status)
self.assertIsNone(self.question.responded_to_by)
# Reject the question
rejected_reason = "This is a rejection"
self.client.patch(
reverse("ohq:question-detail", args=[self.course.id, self.queue.id, self.question.id]),
{"status": Question.STATUS_REJECTED, "rejected_reason": rejected_reason},
)
self.question.refresh_from_db()
self.assertEqual(Question.STATUS_REJECTED, self.question.status)
self.assertEqual(self.ta, self.question.responded_to_by)
self.assertEqual(rejected_reason, self.question.rejected_reason)
mock_delay.assert_called()
def test_ta_answer(self, mock_delay):
"""
Ensure TAs+ can answer a question
"""
self.client.force_authenticate(user=self.ta)
# Start answering the question
self.client.patch(
reverse("ohq:question-detail", args=[self.course.id, self.queue.id, self.question.id]),
{"status": Question.STATUS_ACTIVE},
)
self.question.refresh_from_db()
self.assertEqual(Question.STATUS_ACTIVE, self.question.status)
self.assertEqual(self.ta, self.question.responded_to_by)
# Finish answering the question
self.client.patch(
reverse("ohq:question-detail", args=[self.course.id, self.queue.id, self.question.id]),
{"status": Question.STATUS_ANSWERED},
)
self.question.refresh_from_db()
self.assertEqual(Question.STATUS_ANSWERED, self.question.status)
self.assertEqual(self.ta, self.question.responded_to_by)
mock_delay.assert_called()
def test_ta_update_text(self, mock_delay):
"""
Ensure TAs+ can't modify a question's contents
"""
self.client.force_authenticate(user=self.ta)
response = self.client.patch(
reverse("ohq:question-detail", args=[self.course.id, self.queue.id, self.question.id]),
{"text": "Different"},
)
self.assertEqual(200, response.status_code)
self.question.refresh_from_db()
self.assertEqual(self.question_text, self.question.text)
mock_delay.assert_not_called()
def test_ta_withdraw(self, mock_delay):
"""
Ensure TAs+ can't mark a question as withdrawn
"""
self.client.force_authenticate(user=self.ta)
response = self.client.patch(
reverse("ohq:question-detail", args=[self.course.id, self.queue.id, self.question.id]),
{"status": Question.STATUS_WITHDRAWN},
)
self.assertEqual(400, response.status_code)
self.question.refresh_from_db()
self.assertEqual(Question.STATUS_ASKED, self.question.status)
mock_delay.assert_not_called()
class AnnouncementSerializerTestCase(TestCase):
def setUp(self):
self.client = APIClient()
self.semester = Semester.objects.create(year=2020, term=Semester.TERM_SUMMER)
self.course = Course.objects.create(
course_code="000", department="Penn Labs", semester=self.semester
)
self.name = "Queue"
self.queue = Queue.objects.create(name=self.name, course=self.course)
self.head_ta = User.objects.create(username="head_ta")
self.ta = User.objects.create(username="ta")
Membership.objects.create(
course=self.course, user=self.head_ta, kind=Membership.KIND_HEAD_TA
)
Membership.objects.create(course=self.course, user=self.ta, kind=Membership.KIND_TA)
self.announcement = Announcement.objects.create(
course=self.course, author=self.head_ta, content="Original announcement"
)
def test_update_ta(self):
"""
Ensure TAs can update an Announcement and the author is updated
"""
content = "New content"
self.client.force_authenticate(user=self.ta)
self.client.patch(
reverse("ohq:announcement-detail", args=[self.course.id, self.announcement.id]),
{"content": content},
)
self.announcement.refresh_from_db()
self.assertTrue(self.announcement.author, self.ta)
def test_create(self):
"""
Ensure TAs can create an Announcement and the author matches
"""
self.client.force_authenticate(user=self.ta)
self.client.post(
reverse("ohq:announcement-list", args=[self.course.id]), {"content": "New announcement"}
)
self.assertEqual(2, Announcement.objects.all().count())
announcement = Announcement.objects.all().order_by("time_updated")[1]
self.assertEqual(self.ta, announcement.author)
class EventSerializerTestCase(TestCase):
def setUp(self):
self.client = APIClient()
self.semester = Semester.objects.create(year=2020, term=Semester.TERM_SUMMER)
self.course = Course.objects.create(
course_code="000", department="Penn Labs", semester=self.semester
)
self.head_ta = User.objects.create(username="head_ta")
self.ta = User.objects.create(username="ta")
self.student = User.objects.create(username="student")
Membership.objects.create(
course=self.course, user=self.head_ta, kind=Membership.KIND_HEAD_TA
)
Membership.objects.create(course=self.course, user=self.ta, kind=Membership.KIND_TA)
Membership.objects.create(
course=self.course, user=self.student, kind=Membership.KIND_STUDENT
)
self.old_title = "TA session"
self.new_title = "New TA session"
self.start_time = "2019-08-24T14:15:22Z"
self.end_time = "2019-09-24T14:15:22Z"
def test_create(self):
"""
Ensure TAs can create Event
"""
self.client.force_authenticate(user=self.ta)
self.client.post(
reverse("ohq:event-list"),
{
"start": self.start_time,
"end": self.end_time,
"title": self.old_title,
"rule": {"frequency": "WEEKLY"},
"end_recurring_period": self.end_time,
"course_id": self.course.id,
},
)
self.assertEqual(1, Event.objects.all().count())
# creating event without rule works
self.client.post(
reverse("ohq:event-list"),
{
"start": self.start_time,
"end": self.end_time,
"title": self.old_title,
"end_recurring_period": self.end_time,
"course_id": self.course.id,
},
)
self.assertEqual(2, Event.objects.all().count())
# creating without course_id does not
self.client.post(
reverse("ohq:event-list"),
{
"start": self.start_time,
"end": self.end_time,
"title": self.old_title,
"rule": {"frequency": "WEEKLY"},
"end_recurring_period": self.end_time,
},
)
self.assertEqual(2, Event.objects.all().count())
# student cannot create new event
self.client.force_authenticate(user=self.student)
self.client.post(
reverse("ohq:event-list"),
{
"start": self.start_time,
"end": self.end_time,
"title": self.old_title,
"rule": {"frequency": "WEEKLY"},
"end_recurring_period": "2019-10-24T14:15:22Z",
"course_id": self.course.id,
},
)
self.assertEqual(2, Event.objects.all().count())
def test_update(self):
"""
Ensure TAs can update event
"""
self.client.force_authenticate(user=self.ta)
self.client.post(
reverse("ohq:event-list"),
{
"start": self.start_time,
"end": self.end_time,
"title": self.old_title,
"rule": {"frequency": "WEEKLY"},
"end_recurring_period": self.end_time,
"course_id": self.course.id,
},
)
self.assertEqual(1, Event.objects.all().count())
event = Event.objects.all().first()
self.client.patch(
reverse("ohq:event-detail", args=[event.id]),
{
"title": self.new_title,
"course_id": self.course.id,
"rule": {"frequency": "MONTHLY"},
},
)
event = Event.objects.all().first()
self.assertEqual(event.title, self.new_title)
self.assertEqual(event.rule.frequency, "MONTHLY")
# student cannot make changes
self.client.force_authenticate(user=self.student)
self.client.patch(
reverse("ohq:event-detail", args=[event.id]),
{"title": self.old_title, "course_id": self.course.id},
)
event = Event.objects.all().first()
# title has not changed
self.assertEqual(event.title, self.new_title)
self.assertEqual(event.rule.frequency, "MONTHLY")
def test_update_no_rule(self):
"""
Ensure TAs can update event without Rule
"""
self.client.force_authenticate(user=self.ta)
self.client.post(
reverse("ohq:event-list"),
{
"start": self.start_time,
"end": self.end_time,
"title": self.old_title,
"rule": {"frequency": "WEEKLY"},
"end_recurring_period": self.end_time,
"course_id": self.course.id,
},
)
self.assertEqual(1, Event.objects.all().count())
event = Event.objects.all().first()
self.client.patch(
reverse("ohq:event-detail", args=[event.id]),
{"title": self.new_title, "courseId": self.course.id},
)
event = Event.objects.all().first()
self.assertEqual(event.title, self.new_title)
def test_list(self):
"""
Ensure list of events work
"""
self.client.force_authenticate(user=self.ta)
self.client.post(
reverse("ohq:event-list"),
{
"start": self.start_time,
"end": self.end_time,
"title": self.old_title,
"rule": {"frequency": "WEEKLY"},
"end_recurring_period": self.end_time,
"course_id": self.course.id,
},
)
self.client.post(
reverse("ohq:event-list"),
{
"start": self.start_time,
"end": self.end_time,
"title": self.new_title,
"rule": {"frequency": "WEEKLY"},
"end_recurring_period": self.end_time,
"course_id": self.course.id,
},
)
response = self.client.get(
# i don't know how to reverse this, so it is a bit clunky
"/api/events/?course="
+ str(self.course.id)
)
data = json.loads(response.content)
self.assertEqual(2, len(data))
self.assertEqual(self.course.id, data[0]["course_id"])
self.assertEqual(self.course.id, data[1]["course_id"])