generated from ieeeuoft/hackathon-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api.py
1169 lines (992 loc) · 42.9 KB
/
test_api.py
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from django.conf import settings
from django.contrib.auth.models import Group
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from hackathon_site.tests import SetupUserMixin
from django.contrib.auth.models import Permission
from django.db.models import Q
from event.models import Profile, User, Team
from event.serializers import (
UserSerializer,
TeamSerializer,
)
from hardware.serializers import OrderListSerializer
from hardware.models import Hardware, Order, OrderItem
class CurrentUserTestCase(SetupUserMixin, APITestCase):
def setUp(self):
super().setUp()
self.group = Group.objects.create(name="Test Users")
self.user.groups.add(self.group)
self.profile = Profile.objects.create(user=self.user)
self.view = reverse("api:event:current-user")
def test_user_not_logged_in(self):
response = self.client.get(self.view)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_user_has_no_profile(self):
self.profile.delete()
self._login()
expected_response = {
**{
attr: getattr(self.user, attr)
for attr in ("id", "first_name", "last_name", "email")
},
"profile": None,
"groups": [{"id": self.group.id, "name": self.group.name}],
}
response = self.client.get(self.view)
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.json()
self.assertEqual(expected_response, data)
def test_user_has_profile(self):
self._login()
response = self.client.get(self.view)
user_expected = User.objects.get(pk=self.user.pk)
serializer = UserSerializer(user_expected)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json(), serializer.data)
class CurrentTeamTestCase(SetupUserMixin, APITestCase):
def setUp(self):
super().setUp()
self.group = Group.objects.create(name="Test Users")
self.user.groups.add(self.group)
self.team = Team.objects.create()
self.profile = Profile.objects.create(user=self.user, team=self.team)
self.view = reverse("api:event:current-team")
def test_user_not_logged_in(self):
response = self.client.get(self.view)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_user_has_no_profile(self):
"""
When the user attempts to access the team, while it has no profile.
The user must be accepted or waitlisted to have formed a team.
"""
self.profile.delete()
self._login()
response = self.client.get(self.view)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_user_has_profile(self):
"""
When user has a profile and attempts to access the team, then the user
should get the correct response.
"""
self._login()
response = self.client.get(self.view)
team_expected = Team.objects.get(pk=self.team.pk)
serializer = TeamSerializer(team_expected)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json(), serializer.data)
class JoinTeamTestCase(SetupUserMixin, APITestCase):
def setUp(self):
super().setUp()
self.team = Team.objects.create()
self.profile = Profile.objects.create(user=self.user, team=self.team)
self.team_code = self.team.team_code
self.view_name = "api:event:join-team"
def _build_view(self, team_code):
return reverse(self.view_name, kwargs={"team_code": team_code})
def test_join_and_delete(self):
"""
When a member wants to join a team, if their current team only includes
them, their current team is deleted..
"""
self._login()
# A single-user team is created as part of the setup.
team = self._make_event_team(self_users=False, num_users=2)
self.client.post(self._build_view(team.team_code))
self.assertFalse(self.team.profiles.exists())
def test_invalid_key(self):
self._login()
response = self.client.post(self._build_view("56ABD"))
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_join_full_team(self):
self._login()
team = self._make_event_team(self_users=False)
response = self.client.post(self._build_view(team.team_code))
self.assertEqual(response.json(), {"detail": "Team is full"})
def test_user_not_logged_in(self):
response = self.client.post(self._build_view("56ABD"))
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_user_has_no_profile(self):
self.profile.delete()
self._login()
response = self.client.post(self._build_view("56ABD"))
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def check_cannot_leave_active(self):
old_team = self.profile.team
sample_team = self._make_event_team(self_users=False, num_users=2)
response = self.client.post(self._build_view(sample_team.team_code))
self.user.refresh_from_db()
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(old_team.pk, self.user.profile.team.pk)
def check_can_leave_cancelled_or_returned(self):
old_team = self.profile.team
sample_team = self._make_event_team(self_users=False, num_users=2)
response = self.client.post(self._build_view(sample_team.team_code))
self.user.refresh_from_db()
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["id"], self.user.profile.team.pk)
self.assertNotEqual(old_team.pk, self.user.profile.team.pk)
self.assertFalse(Team.objects.filter(team_code=old_team.team_code).exists())
def test_cannot_leave_with_order(self):
"""
check user cannot join another team when there
are submitted orders, unless those orders are
cancelled.
"""
self._login()
hardware = Hardware.objects.create(
name="name",
model_number="model",
manufacturer="manufacturer",
datasheet="/datasheet/location/",
quantity_available=4,
max_per_team=1,
picture="/picture/location",
)
order = Order.objects.create(
status="Cart",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}]},
)
OrderItem.objects.create(order=order, hardware=hardware)
for _, status_choice in Order.STATUS_CHOICES:
order.status = status_choice
order.save()
if status_choice not in ("Cancelled", "Returned"):
self.check_cannot_leave_active()
else:
self.check_can_leave_cancelled_or_returned()
# Since there are 2 cases where teams can change, reset foreign key
order.team = self.user.profile.team
order.save()
class LeaveTeamTestCase(SetupUserMixin, APITestCase):
def setUp(self):
super().setUp()
self.team = Team.objects.create()
self.profile = Profile.objects.create(user=self.user, team=self.team)
self.view = reverse("api:event:leave-team")
def test_user_not_logged_in(self):
response = self.client.post(self.view)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_user_has_no_profile(self):
self.profile.delete()
self._login()
response = self.client.post(self.view)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def check_leave_and_delete(self):
_email = self._get_random_email()
other_user = User.objects.create_user(
username=_email,
password=self.password,
first_name="other_user",
last_name="Bar",
email=_email,
)
Profile.objects.create(team=self.user.profile.team, user=other_user)
old_team = self.profile.team
response = self.client.post(self.view)
self.user.refresh_from_db()
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data["id"], self.user.profile.team.pk)
self.assertNotEqual(old_team.pk, self.user.profile.team.pk)
self.assertEqual(
Team.objects.filter(team_code=old_team.team_code).exists(), True
)
def check_cannot_leave(self):
old_team = self.profile.team
response = self.client.post(self.view)
self.user.refresh_from_db()
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(old_team.pk, self.user.profile.team.pk)
self.assertEqual(Team.objects.filter(team_code=old_team.team_code).count(), 1)
def test_leave_and_delete(self):
"""
when there is no orders or other members on the team,
user should be able to leave and be put on a new team
"""
self._login()
self.check_leave_and_delete()
def test_leave_with_order(self):
"""
when there are orders but no other members on the team,
user can only leave when there are only cancelled orders
"""
self._login()
hardware = Hardware.objects.create(
name="name",
model_number="model",
manufacturer="manufacturer",
datasheet="/datasheet/location/",
quantity_available=4,
max_per_team=1,
picture="/picture/location",
)
order = Order.objects.create(
status="Cart",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}]},
)
OrderItem.objects.create(order=order, hardware=hardware)
for _, status_choice in Order.STATUS_CHOICES:
order.status = status_choice
order.save()
if status_choice not in ("Cancelled", "Returned"):
self.check_cannot_leave()
else:
self.check_leave_and_delete()
def test_leave_with_not_empty_team(self):
"""
when there are other members but no orders on the team,
user can only leave but the old team stays
"""
old_team = self.profile.team
other_user = User.objects.create_user(
username="other_user@bar.com",
password=self.password,
first_name="other_user",
last_name="Bar",
email="other_user@bar.com",
)
Profile.objects.create(user=other_user, team=self.profile.team)
self._login()
response = self.client.post(self.view)
self.user.refresh_from_db()
other_user.refresh_from_db()
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data["id"], self.user.profile.team.pk)
self.assertNotEqual(old_team.pk, self.user.profile.team.pk)
self.assertEqual(old_team.pk, other_user.profile.team.pk)
self.assertEqual(
Team.objects.filter(team_code=old_team.team_code).exists(), True
)
class EventTeamListsViewTestCase(SetupUserMixin, APITestCase):
def setUp(self):
self.team = Team.objects.create()
self.team2 = Team.objects.create()
self.team3 = Team.objects.create()
self.permissions = Permission.objects.filter(
content_type__app_label="event", codename="view_team"
)
super().setUp()
self.view = reverse("api:event:team-list")
def _build_filter_url(self, **kwargs):
return (
self.view + "?" + "&".join([f"{key}={val}" for key, val in kwargs.items()])
)
def test_team_id_filter(self):
self._login(self.permissions)
url = self._build_filter_url(team_ids="1,3")
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.json()
results = data["results"]
returned_ids = [res["id"] for res in results]
self.assertCountEqual(returned_ids, [1, 3])
def test_team_get_no_permissions(self):
self._login()
response = self.client.get(self.view)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_team_get_has_permissions(self):
self._login(self.permissions)
response = self.client.get(self.view)
self.assertEqual(response.status_code, status.HTTP_200_OK)
queryset = Team.objects.all()
# need to provide a request in the serializer context to produce absolute url for image field
expected_response = TeamSerializer(
queryset, many=True, context={"request": response.wsgi_request}
).data
data = response.json()
self.assertEqual(expected_response, data["results"])
def test_team_get_not_login(self):
response = self.client.get(self.view)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_team_code_filter(self):
self._login(self.permissions)
url = self._build_filter_url(team_code=self.team.team_code)
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.json()
results = data["results"]
returned_ids = [res["team_code"] for res in results]
self.assertCountEqual(returned_ids, [self.team.team_code])
def test_name_search_filter(self):
self._login(self.permissions)
url = self._build_filter_url(search=self.team2.team_code)
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.json()
results = data["results"]
returned_ids = [res["team_code"] for res in results]
self.assertCountEqual(returned_ids, [self.team2.team_code])
class ProfileDetailViewTestCase(SetupUserMixin, APITestCase):
def setUp(self):
super().setUp()
self.user_with_profile = User.objects.create_user(
username="foo2@bar.com",
password=self.password,
first_name="Test2",
last_name="Bar2",
email="foo2@bar.com",
)
self.profile = self._make_event_profile(user=self.user_with_profile)
self.request_body = {
"id_provided": True,
"attended": True,
}
self.change_permissions = Permission.objects.filter(
content_type__app_label="event", codename="change_profile"
)
self.expected_response = {
"id": self.profile.id,
"id_provided": True,
"attended": True,
"acknowledge_rules": False,
"e_signature": None,
"team": self.profile.team_id,
"phone_number": "1234567890",
}
self.view = reverse("api:event:profile-detail", kwargs={"pk": self.profile.id})
def test_user_not_logged_in(self):
response = self.client.patch(self.view, self.request_body)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_user_has_no_change_permissions(self):
self._login()
response = self.client.patch(self.view, self.request_body)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_user_has_change_permissions(self):
self._login(self.change_permissions)
response = self.client.patch(self.view, self.request_body)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(self.expected_response, data)
def test_partially_modifying_profile(self):
self._login(self.change_permissions)
# only modifying id_provided
request_body = {"id_provided": True}
expected_response = {
"id": self.profile.id,
"id_provided": True,
"attended": False,
"acknowledge_rules": False,
"e_signature": None,
"team": self.profile.team_id,
"phone_number": "1234567890",
}
response = self.client.patch(self.view, request_body)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(expected_response, data)
class CurrentProfileViewTestCase(SetupUserMixin, APITestCase):
def setUp(self):
super().setUp()
self.profile = self._make_event_profile(user=self.user)
self.request_body = {
"acknowledge_rules": True,
"e_signature": "user signature",
}
self.expected_response = {
"id": self.profile.id,
"id_provided": False,
"attended": False,
"acknowledge_rules": True,
"e_signature": "user signature",
"team": self.profile.team_id,
"phone_number": "1234567890",
}
self.view = reverse("api:event:current-profile")
def test_user_not_logged_in(self):
response = self.client.patch(self.view, self.request_body)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_user_has_no_profile(self):
self.profile.delete()
self._login()
response = self.client.patch(self.view, self.request_body)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_user_has_profile(self):
self._login()
# Testing to get user's own profile
response = self.client.patch(self.view, self.request_body)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(self.expected_response, data)
def test_modifying_acknowledge_rules_and_e_signature_twice(self):
self._login()
# First, normally update profile
response = self.client.patch(self.view, self.request_body)
data = response.json()
expected_response = self.expected_response
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(expected_response, data)
# Then update profile with a different request, changing acknowledge & e signature
new_request_body = {
"acknowledge_rules": False,
"e_signature": "new signature",
}
# acknowledge_rules and e_signature do not change
response2 = self.client.patch(self.view, new_request_body)
data2 = response2.json()
self.assertEqual(response2.status_code, status.HTTP_200_OK)
self.assertEqual(expected_response, data2)
def test_partially_modifying_profile(self):
self._login()
# only modifying acknowledge_rules
request_body = {"acknowledge_rules": True}
expected_response = {
"id": self.profile.id,
"id_provided": False,
"attended": False,
"acknowledge_rules": True,
"e_signature": None,
"team": self.profile.team_id,
"phone_number": "1234567890",
}
response = self.client.patch(self.view, request_body)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(expected_response, data)
class CreateProfileViewTestCase(SetupUserMixin, APITestCase):
def setUp(self):
super().setUp()
self.request_body = {
"acknowledge_rules": True,
"e_signature": "user signature",
}
self.expected_response = {
"attended": True,
"id_provided": False,
"acknowledge_rules": True,
"e_signature": "user signature",
"phone_number": "1234567890",
}
self.view = reverse("api:event:current-profile")
def test_user_not_logged_in(self):
response = self.client.post(self.view, self.request_body)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_user_can_have_profile(self):
self._review(application=self._apply_as_user(self.user, rsvp=True))
self._login()
response = self.client.post(self.view, self.request_body)
data = response.json()
profile_created = Profile.objects.get(user=self.user)
self.assertIsNotNone(profile_created)
self.expected_response = {
**self.expected_response,
"team": profile_created.team.team_code,
}
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(self.expected_response, data)
def test_not_including_required_fields(self):
self._review(application=self._apply_as_user(self.user, rsvp=True))
self._login()
response = self.client.post(self.view, {"e_signature": "user signature",})
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
response = self.client.post(self.view, {"acknowledge_rules": True,})
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_acknowledge_rules_is_false(self):
self._review(application=self._apply_as_user(self.user, rsvp=True))
self._login()
response = self.client.post(
self.view, {"e_signature": "user signature", "acknowledge_rules": False,},
)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
data[0], "User must acknowledge rules and provide an e_signature"
)
def test_e_signature_is_empty(self):
self._review(application=self._apply_as_user(self.user, rsvp=True))
self._login()
response = self.client.post(
self.view, {"e_signature": "", "acknowledge_rules": True,},
)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
data[0], "User must acknowledge rules and provide an e_signature"
)
def test_user_already_has_profile(self):
self._make_event_profile(user=self.user)
self._login()
response = self.client.post(self.view, self.request_body)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(data[0], "User already has profile")
def test_user_has_not_completed_application(self):
self._login()
response = self.client.post(self.view, self.request_body)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
data[0],
"User has not completed their application to the hackathon. Please do so to access the Hardware Signout Site",
)
def test_user_has_not_rsvp(self):
self._apply_as_user(self.user)
self._login()
response = self.client.post(self.view, self.request_body)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
data[0],
"User has not RSVP'd to the hackathon. Please RSVP to access the Hardware Signout Site",
)
def test_user_has_not_been_reviewed(self):
self._apply_as_user(self.user, rsvp=True)
self._login()
response = self.client.post(self.view, self.request_body)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
data[0],
"User has not been reviewed yet, Hardware Signout Site cannot be accessed until reviewed",
)
def test_user_has_been_reviewed_but_not_sent(self):
self._review(
application=self._apply_as_user(self.user, rsvp=True),
decision_sent_date=None,
)
self._login()
response = self.client.post(self.view, self.request_body)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
data[0],
"User has not been reviewed yet, Hardware Signout Site cannot be accessed until reviewed",
)
def test_user_review_rejected(self):
self._review(
application=self._apply_as_user(self.user, rsvp=True), status="Rejected"
)
self._login()
response = self.client.post(self.view, self.request_body)
data = response.json()
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
data[0],
f"User has not been accepted to participate in {settings.HACKATHON_NAME}",
)
class CurrentTeamOrderListViewTestCase(SetupUserMixin, APITestCase):
def setUp(self):
super().setUp()
self.team = Team.objects.create()
self.order = Order.objects.create(
status="Cart", team=self.team, request={"hardware": []}
)
self.hardware = Hardware.objects.create(
name="name",
model_number="model",
manufacturer="manufacturer",
datasheet="/datasheet/location/",
notes="notes",
quantity_available=4,
max_per_team=1,
picture="/picture/location",
)
self.other_hardware = Hardware.objects.create(
name="other",
model_number="otherModel",
manufacturer="otherManufacturer",
datasheet="/datasheet/location/other",
quantity_available=10,
max_per_team=10,
picture="/picture/location/other",
)
OrderItem.objects.create(
order=self.order, hardware=self.hardware,
)
OrderItem.objects.create(
order=self.order, hardware=self.other_hardware,
)
# making extra data to test if team data is being filtered
self.team2 = Team.objects.create(team_code="ABCDE")
self.order_2 = Order.objects.create(
status="Submitted", team=self.team2, request={"hardware": []}
)
OrderItem.objects.create(
order=self.order_2, hardware=self.hardware,
)
OrderItem.objects.create(
order=self.order_2, hardware=self.other_hardware,
)
self.view = reverse("api:event:team-orders")
def test_user_not_logged_in(self):
response = self.client.get(self.view)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_user_has_no_profile(self):
self._login()
response = self.client.get(self.view)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_user_has_profile(self):
Profile.objects.create(user=self.user, team=self.team)
self._login()
response = self.client.get(self.view)
self.assertEqual(response.status_code, status.HTTP_200_OK)
queryset = Order.objects.filter(team_id=self.team.id)
expected_response = OrderListSerializer(
queryset, many=True, context={"request": response.wsgi_request}
).data
data = response.json()
self.assertEqual(expected_response, data["results"])
class TeamIncidentListViewPostTestCase(SetupUserMixin, APITestCase):
def setUp(self):
super().setUp()
self.team = Team.objects.create()
self.order = Order.objects.create(
status="Cart",
team=self.team,
request={"hardware": [{"id": 1, "quantity": 2}]},
)
self.hardware = Hardware.objects.create(
name="name",
model_number="model",
manufacturer="manufacturer",
datasheet="/datasheet/location/",
notes="notes",
quantity_available=4,
max_per_team=1,
picture="/picture/location",
)
self.order_item = OrderItem.objects.create(
order=self.order, hardware=self.hardware,
)
self.request_data = {
"state": "Broken",
"time_occurred": "2022-08-08T01:18:00-04:00",
"description": "Description",
"order_item": self.order_item.id,
}
self.view = reverse("api:event:incident-list")
def test_user_not_logged_in(self):
response = self.client.post(self.view, self.request_data)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_user_no_profile(self):
self._login()
response = self.client.post(self.view, self.request_data)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_post_another_team_incident(self):
self.team2 = Team.objects.create()
self.other_hardware = Hardware.objects.create(
name="other",
model_number="otherModel",
manufacturer="otherManufacturer",
datasheet="/datasheet/location/other",
quantity_available=10,
max_per_team=10,
picture="/picture/location/other",
)
self.order2 = Order.objects.create(
status="Cart",
team=self.team2,
request={"hardware": [{"id": 1, "quantity": 2}]},
)
self.order_item2 = OrderItem.objects.create(
order=self.order2, hardware=self.other_hardware,
)
request_data = {
"state": "Broken",
"time_occurred": "2022-08-08T01:18:00-04:00",
"description": "Description",
"order_item": self.order_item2.id,
}
self._login()
Profile.objects.create(user=self.user, team=self.team)
response = self.client.post(self.view, request_data)
self.assertEqual(
response.json(), {"detail": "Can only create incidents for your own team."}
)
def test_successful_post(self):
self._login()
Profile.objects.create(user=self.user, team=self.team)
response = self.client.post(self.view, self.request_data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
similar_attributes = [
"state",
"time_occurred",
"description",
"order_item",
]
final_response = response.json()
del final_response["id"]
for attribute in similar_attributes:
self.assertEqual(final_response[attribute], self.request_data[attribute])
class EventTeamDetailViewTestCase(SetupUserMixin, APITestCase):
def setUp(self, **kwargs):
self.team = Team.objects.create()
self.team2 = Team.objects.create()
self.team3 = Team.objects.create()
self.hardware = Hardware.objects.create(
name="name",
model_number="model",
manufacturer="manufacturer",
datasheet="/datasheet/location/",
notes="notes",
quantity_available=4,
max_per_team=1,
picture="/picture/location",
)
self.order = Order.objects.create(
status="Submitted",
team=self.team2,
request={"hardware": [{"id": 1, "quantity": 2}, {"id": 2, "quantity": 3}]},
)
self.order_item_1 = OrderItem.objects.create(
order=self.order, hardware=self.hardware,
)
self.order_2 = Order.objects.create(
status="Returned",
team=self.team3,
request={"hardware": [{"id": 1, "quantity": 2}, {"id": 2, "quantity": 3}]},
)
self.order_item_2 = OrderItem.objects.create(
order=self.order_2, hardware=self.hardware, part_returned_health="Healthy"
)
self.permissions = Permission.objects.filter(
Q(content_type__app_label="event", codename="view_team")
| Q(content_type__app_label="event", codename="change_team")
| Q(content_type__app_label="event", codename="delete_team"),
)
super().setUp()
def _build_view(self, team_code):
return reverse("api:event:team-detail", kwargs={"team_code": team_code})
def test_team_get_not_login(self):
response = self.client.get(self._build_view("56ABD"))
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_team_get_no_permissions(self):
self._login()
response = self.client.get(self._build_view("56ABD"))
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_team_get_has_permissions(self):
self._login(self.permissions)
response = self.client.get(self._build_view(self.team.team_code))
self.assertEqual(response.status_code, status.HTTP_200_OK)
queryset = Team.objects.filter(team_code=self.team)
expected_response = TeamSerializer(
queryset, many=True, context={"request": response.wsgi_request}
).data
data = response.json()
self.assertEqual(expected_response[0], data)
def test_team_delete_not_login(self):
response = self.client.delete(self._build_view("56ABD"))
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_team_delete_no_permissions(self):
self._login()
response = self.client.delete(self._build_view("56ABD"))
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_team_delete_has_permissions(self):
self._login(self.permissions)
response = self.client.delete(self._build_view(self.team.team_code))
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def test_team_delete_with_unreturned_orders(self):
self._login(self.permissions)
response = self.client.delete(self._build_view(self.team2.team_code))
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_team_delete_with_returned_orders(self):
self._login(self.permissions)
response = self.client.delete(self._build_view(self.team3.team_code))
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def test_team_patch_not_login(self):
response = self.client.patch(
self._build_view("56ABD"), data={"project_description": "New description"}
)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_team_patch_no_permissions(self):
self._login()
response = self.client.patch(
self._build_view("56ABD"), data={"project_description": "New description"}
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_team_patch_has_permissions(self):
self._login(self.permissions)
response = self.client.patch(
self._build_view(self.team.team_code),
data={"project_description": "New description"},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
updated_team = Team.objects.get(team_code=self.team.team_code)
self.assertEqual(updated_team.project_description, "New description")
def test_team_patch_invalid_request_data_format(self):
self._login(self.permissions)
response = self.client.patch(
self._build_view(self.team.team_code), data={"Invalid data"}, format="json"
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data, "Invalid request data format")
def test_team_patch_invalid_field_for_update(self):
self._login(self.permissions)
response = self.client.patch(
self._build_view(self.team.team_code),
data={"invalid_field": "Invalid data"},
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
response.data, '"invalid_field" is not a valid field for update'
)
def test_team_patch_invalid_project_description(self):
self._login(self.permissions)
response = self.client.patch(
self._build_view(self.team.team_code),
data={"project_description": 12345},
format="json",
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data, "project_description must be a string")
class TeamOrderDetailViewTestCase(SetupUserMixin, APITestCase):
def setUp(self):
super().setUp()
self.team = Team.objects.create()
self.profile = Profile.objects.create(user=self.user, team=self.team)
self.view_name = "api:event:team-order-detail"
hardware = Hardware.objects.create(
name="name",
model_number="model",
manufacturer="manufacturer",
datasheet="/datasheet/location/",