-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_json_asserter.py
1208 lines (998 loc) · 50.9 KB
/
test_json_asserter.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
import pytest
import uuid
from rest_framework import status
from shipchain_common.test_utils import AssertionHelper
from unittest.mock import Mock
EXAMPLE_PLAIN = {
'id': '07b374c3-ed9b-4811-901a-d0c5d746f16a',
'name': 'example 1',
'field_1': 1,
'owner': {
'username': 'user1'
}
}
EXAMPLE_PLAIN_2 = {
'id': 'bf0d0b89-482f-40dd-b29b-9e5e05b83ed6',
'name': 'example 2',
'field_1': 2,
'owner': {
'username': 'user2'
}
}
EXAMPLE_PLAIN_3 = {
'id': '2aa1db84-6618-4e35-9b2a-f450c20699fe',
'name': 'example 3',
'field_1': 3,
'owner': {
'username': 'user3'
}
}
EXAMPLE_USER = {
'type': 'User',
'id': '07b374c3-ed9b-4811-901a-d0c5d746f16a',
'attributes': {
'username': 'user1'
}
}
EXAMPLE_RESOURCE = {
'type': 'ExampleResource',
'id': 'a6f554e9-3bd3-4972-90e1-b8a19aab7091',
'attributes': {
'name': 'example 1',
'field_1': 1
}
}
EXAMPLE_RESOURCE_2 = {
'type': 'ExampleResource',
'id': 'b717eff3-b021-4f3f-a2be-7cdc08a1bfb5',
'attributes': {
'name': 'example 2',
'field_1': 2
}
}
EXAMPLE_RESOURCE_3 = {
'type': 'ExampleResource',
'id': 'd72d5d56-c359-455e-876b-52835228c852',
'attributes': {
'name': 'example 3',
'field_1': 3
}
}
EXAMPLE_RESOURCE_4 = {
'type': 'ExampleResource',
'id': 'e8ba3cd9-9b5e-41fa-9b08-116284e968fd',
'attributes': {
'name': 'example 4',
'field_1': 4
}
}
@pytest.fixture
def vnd_single():
return {
'data': {
'type': EXAMPLE_RESOURCE['type'],
'id': EXAMPLE_RESOURCE['id'],
'attributes': EXAMPLE_RESOURCE['attributes'],
'relationships': {
'owner': {
'data': {
'type': EXAMPLE_USER['type'],
'id': EXAMPLE_USER['id']
}
},
'children': {
'meta': {
'count': 2
},
'data': [
{
'type': EXAMPLE_RESOURCE_2['type'],
'id': EXAMPLE_RESOURCE_2['id']
},
{
'type': EXAMPLE_RESOURCE_4['type'],
'id': EXAMPLE_RESOURCE_4['id']
}
]
}
},
'meta': {
'key': 'value',
'other_key': 'other_value',
}
},
'included': [
EXAMPLE_USER,
EXAMPLE_RESOURCE_2,
EXAMPLE_RESOURCE_4
]
}
@pytest.fixture
def vnd_list():
return {
'data': [
{
'type': EXAMPLE_RESOURCE['type'],
'id': EXAMPLE_RESOURCE['id'],
'attributes': EXAMPLE_RESOURCE['attributes'],
'relationships': {
'owner': {
'data': {
'type': EXAMPLE_USER['type'],
'id': EXAMPLE_USER['id']
}
},
'children': {
'meta': {
'count': 1
},
'data': [
{
'type': EXAMPLE_RESOURCE_2['type'],
'id': EXAMPLE_RESOURCE_2['id']
}
]
}
},
'meta': {
'key': 'value',
'other_key': 'other_value',
}
},
{
'type': EXAMPLE_RESOURCE_3['type'],
'id': EXAMPLE_RESOURCE_3['id'],
'attributes': EXAMPLE_RESOURCE_3['attributes'],
'relationships': {
'owner': {
'data': {
'type': EXAMPLE_USER['type'],
'id': EXAMPLE_USER['id']
}
},
'children': {
'meta': {
'count': 1
},
'data': [
{
'type': EXAMPLE_RESOURCE_2['type'],
'id': EXAMPLE_RESOURCE_2['id']
}
]
}
}
},
],
'included': [
EXAMPLE_USER,
EXAMPLE_RESOURCE_2
]
}
@pytest.fixture
def vnd_error():
return {
'errors': [
{
'detail': ''
}
]
}
@pytest.fixture
def vnd_error_400(vnd_error):
vnd_error['errors'][0]['detail'] = 'generic 400 error'
vnd_error['errors'][0]['source'] = {
'pointer': ''
}
return vnd_error
@pytest.fixture
def json_error():
return {
'detail': 'Error detail'
}
@pytest.fixture
def entity_ref_1():
return AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_RESOURCE['id'],
attributes=EXAMPLE_RESOURCE['attributes'],
relationships={'owner': AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_USER['id'],
)})
@pytest.fixture
def entity_ref_3():
return AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE_3['type'],
pk=EXAMPLE_RESOURCE_3['id'],
attributes=EXAMPLE_RESOURCE_3['attributes'],
relationships={'owner': AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_USER['id'],
)})
class TestAssertionHelper:
@pytest.fixture(autouse=True)
def make_build_response(self):
def _build_response(data, status_code=status.HTTP_200_OK):
return Mock(status_code=status_code, json=lambda: data)
self.build_response = _build_response
@pytest.fixture
def vnd_error_401(self, vnd_error):
vnd_error['errors'][0]['detail'] = 'Authentication credentials were not provided'
return vnd_error
@pytest.fixture
def vnd_error_403(self, vnd_error):
vnd_error['errors'][0]['detail'] = 'You do not have permission to perform this action'
return vnd_error
@pytest.fixture
def vnd_error_404(self, vnd_error):
vnd_error['errors'][0]['detail'] = 'Not found'
return vnd_error
@pytest.fixture
def vnd_error_405(self, vnd_error):
vnd_error['errors'][0]['detail'] = 'Method not allowed'
return vnd_error
def test_status_200(self, vnd_single, vnd_error_400):
response = self.build_response(vnd_single)
AssertionHelper.HTTP_200(response)
with pytest.raises(AssertionError) as err:
response = self.build_response(vnd_error_400, status_code=status.HTTP_400_BAD_REQUEST)
AssertionHelper.HTTP_200(response)
assert 'status_code 400 != 200' in str(err.value)
def test_status_201(self, vnd_single, vnd_error_400):
response = self.build_response(vnd_single, status_code=status.HTTP_201_CREATED)
AssertionHelper.HTTP_201(response)
with pytest.raises(AssertionError) as err:
response = self.build_response(vnd_error_400, status_code=status.HTTP_400_BAD_REQUEST)
AssertionHelper.HTTP_201(response)
assert 'status_code 400 != 201' in str(err.value)
def test_status_202(self, vnd_single, vnd_error_400):
response = self.build_response(vnd_single, status_code=status.HTTP_202_ACCEPTED)
AssertionHelper.HTTP_202(response)
with pytest.raises(AssertionError) as err:
response = self.build_response(vnd_error_400, status_code=status.HTTP_400_BAD_REQUEST)
AssertionHelper.HTTP_202(response)
assert 'status_code 400 != 202' in str(err.value)
def test_status_204(self, vnd_single, vnd_error_400):
response = self.build_response(vnd_single, status_code=status.HTTP_204_NO_CONTENT)
AssertionHelper.HTTP_204(response)
with pytest.raises(AssertionError) as err:
response = self.build_response(vnd_error_400, status_code=status.HTTP_400_BAD_REQUEST)
AssertionHelper.HTTP_204(response)
assert 'status_code 400 != 204' in str(err.value)
def test_status_400(self, vnd_single, vnd_error_400):
response = self.build_response(vnd_error_400, status_code=status.HTTP_400_BAD_REQUEST)
AssertionHelper.HTTP_400(response)
with pytest.raises(AssertionError) as err:
response = self.build_response(vnd_single)
AssertionHelper.HTTP_400(response)
assert 'status_code 200 != 400' in str(err.value)
def test_status_400_custom_message(self, vnd_error_400):
vnd_error_400['errors'][0]['detail'] = 'custom error message'
response = self.build_response(vnd_error_400, status_code=status.HTTP_400_BAD_REQUEST)
AssertionHelper.HTTP_400(response, error='custom error message')
def test_status_400_custom_pointer(self, vnd_error_400):
vnd_error_400['errors'][0]['detail'] = 'custom error message'
vnd_error_400['errors'][0]['source']['pointer'] = 'pointer'
response = self.build_response(vnd_error_400, status_code=status.HTTP_400_BAD_REQUEST)
AssertionHelper.HTTP_400(response, error='custom error message', pointer='pointer')
def test_status_400_json(self, vnd_single, json_error):
response = self.build_response(json_error, status_code=status.HTTP_400_BAD_REQUEST)
AssertionHelper.HTTP_400(response, error=json_error['detail'], vnd=False)
with pytest.raises(AssertionError) as err:
response = self.build_response(json_error, status_code=status.HTTP_200_OK)
AssertionHelper.HTTP_400(response, error='Different error', vnd=False)
assert 'status_code 200 != 400' in str(err.value)
with pytest.raises(AssertionError) as err:
response = self.build_response(json_error, status_code=status.HTTP_400_BAD_REQUEST)
AssertionHelper.HTTP_400(response, error='Different error', vnd=False)
assert f'Error Different error not found in {json_error["detail"]}' in str(err.value)
def test_status_401(self, vnd_single, vnd_error_401):
response = self.build_response(vnd_error_401, status_code=status.HTTP_401_UNAUTHORIZED)
AssertionHelper.HTTP_401(response)
with pytest.raises(AssertionError) as err:
response = self.build_response(vnd_single)
AssertionHelper.HTTP_401(response)
assert 'status_code 200 != 401' in str(err.value)
def test_status_401_json(self, vnd_single, json_error):
response = self.build_response(json_error, status_code=status.HTTP_401_UNAUTHORIZED)
AssertionHelper.HTTP_401(response, error=json_error['detail'], vnd=False)
with pytest.raises(AssertionError) as err:
response = self.build_response(json_error, status_code=status.HTTP_200_OK)
AssertionHelper.HTTP_401(response, error='Different error', vnd=False)
assert 'status_code 200 != 401' in str(err.value)
with pytest.raises(AssertionError) as err:
response = self.build_response(json_error, status_code=status.HTTP_401_UNAUTHORIZED)
AssertionHelper.HTTP_401(response, error='Different error', vnd=False)
assert f'Error Different error not found in {json_error["detail"]}' in str(err.value)
def test_status_403(self, vnd_single, vnd_error_403):
response = self.build_response(vnd_error_403, status_code=status.HTTP_403_FORBIDDEN)
AssertionHelper.HTTP_403(response)
with pytest.raises(AssertionError) as err:
response = self.build_response(vnd_single)
AssertionHelper.HTTP_403(response)
assert 'status_code 200 != 403' in str(err.value)
def test_status_403_json(self, vnd_single, json_error):
response = self.build_response(json_error, status_code=status.HTTP_403_FORBIDDEN)
AssertionHelper.HTTP_403(response, error=json_error['detail'], vnd=False)
with pytest.raises(AssertionError) as err:
response = self.build_response(json_error, status_code=status.HTTP_200_OK)
AssertionHelper.HTTP_403(response, error='Different error', vnd=False)
assert 'status_code 200 != 403' in str(err.value)
with pytest.raises(AssertionError) as err:
response = self.build_response(json_error, status_code=status.HTTP_403_FORBIDDEN)
AssertionHelper.HTTP_403(response, error='Different error', vnd=False)
assert f'Error Different error not found in {json_error["detail"]}' in str(err.value)
def test_status_404(self, vnd_single, vnd_error_404):
response = self.build_response(vnd_error_404, status_code=status.HTTP_404_NOT_FOUND)
AssertionHelper.HTTP_404(response)
with pytest.raises(AssertionError) as err:
response = self.build_response(vnd_single)
AssertionHelper.HTTP_404(response)
assert 'status_code 200 != 404' in str(err.value)
def test_status_404_json(self, vnd_single, json_error):
response = self.build_response(json_error, status_code=status.HTTP_404_NOT_FOUND)
AssertionHelper.HTTP_404(response, error=json_error['detail'], vnd=False)
with pytest.raises(AssertionError) as err:
response = self.build_response(json_error, status_code=status.HTTP_200_OK)
AssertionHelper.HTTP_404(response, error='Different error', vnd=False)
assert 'status_code 200 != 404' in str(err.value)
with pytest.raises(AssertionError) as err:
response = self.build_response(json_error, status_code=status.HTTP_404_NOT_FOUND)
AssertionHelper.HTTP_404(response, error='Different error', vnd=False)
assert f'Error Different error not found in {json_error["detail"]}' in str(err.value)
def test_status_405(self, vnd_single, vnd_error_405):
response = self.build_response(vnd_error_405, status_code=status.HTTP_405_METHOD_NOT_ALLOWED)
AssertionHelper.HTTP_405(response, error='Method not allowed')
with pytest.raises(AssertionError) as err:
response = self.build_response(vnd_single)
AssertionHelper.HTTP_405(response, error='Method not allowed')
assert 'status_code 200 != 405' in str(err.value)
def test_status_405_json(self, vnd_single, json_error):
response = self.build_response(json_error, status_code=status.HTTP_405_METHOD_NOT_ALLOWED)
AssertionHelper.HTTP_405(response, error=json_error['detail'], vnd=False)
with pytest.raises(AssertionError) as err:
response = self.build_response(json_error, status_code=status.HTTP_200_OK)
AssertionHelper.HTTP_405(response, error='Different error', vnd=False)
assert 'status_code 200 != 405' in str(err.value)
with pytest.raises(AssertionError) as err:
response = self.build_response(json_error, status_code=status.HTTP_405_METHOD_NOT_ALLOWED)
AssertionHelper.HTTP_405(response, error='Different error', vnd=False)
assert f'Error Different error not found in {json_error["detail"]}' in str(err.value)
def test_status_500(self, vnd_single, vnd_error):
vnd_error['errors'][0]['detail'] = 'A server error occurred.'
response = self.build_response(vnd_error, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
AssertionHelper.HTTP_500(response)
with pytest.raises(AssertionError) as err:
response = self.build_response(vnd_single)
AssertionHelper.HTTP_500(response)
assert 'status_code 200 != 500' in str(err.value)
def test_status_500_custom_message(self, vnd_error):
vnd_error['errors'][0]['detail'] = 'custom error message'
response = self.build_response(vnd_error, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
AssertionHelper.HTTP_500(response, error='custom error message')
def test_status_503(self, vnd_single, vnd_error):
vnd_error['errors'][0]['detail'] = 'Service temporarily unavailable, try again later'
response = self.build_response(vnd_error, status_code=status.HTTP_503_SERVICE_UNAVAILABLE)
AssertionHelper.HTTP_503(response)
with pytest.raises(AssertionError) as err:
response = self.build_response(vnd_single)
AssertionHelper.HTTP_503(response)
assert 'status_code 200 != 503' in str(err.value)
def test_status_503_custom_message(self, vnd_error):
vnd_error['errors'][0]['detail'] = 'custom error message'
response = self.build_response(vnd_error, status_code=status.HTTP_503_SERVICE_UNAVAILABLE)
AssertionHelper.HTTP_503(response, error='custom error message')
def test_status_wrong_message(self, vnd_error_404):
response = self.build_response(vnd_error_404, status_code=status.HTTP_404_NOT_FOUND)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_404(response, error='Not the correct error')
assert f'Error `Not the correct error` not found in' in str(err.value)
def test_status_400_wrong_pointer(self, vnd_error_400):
vnd_error_400['errors'][0]['detail'] = 'custom error message'
vnd_error_400['errors'][0]['source']['pointer'] = 'pointer'
response = self.build_response(vnd_error_400, status_code=status.HTTP_400_BAD_REQUEST)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_400(response, error='custom error message', pointer='Not the correct pointer')
assert f'Error `Not the correct pointer` not found in' in str(err.value)
def test_status_404_wrong_pointer(self, vnd_error):
vnd_error['errors'][0]['detail'] = 'Not found'
vnd_error['errors'][0]['source'] = {
'pointer': 'correct pointer'
}
response = self.build_response(vnd_error, status_code=status.HTTP_404_NOT_FOUND)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_404(response, pointer='Not the correct pointer')
assert f'Error `Not the correct pointer` not found in' in str(err.value)
def test_status_pointer_requires_correct_error(self, vnd_error):
vnd_error['errors'][0]['detail'] = 'Not found'
vnd_error['errors'][0]['source'] = {
'pointer': 'correct pointer'
}
response = self.build_response(vnd_error, status_code=status.HTTP_404_NOT_FOUND)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_404(response, error='Not the correct error', pointer='Not the correct pointer')
assert f'Error `Not the correct error` not found in' in str(err.value)
def test_status_in_second_error(self, vnd_error_404):
vnd_error_404['errors'].append({'detail': 'another error'})
response = self.build_response(vnd_error_404, status_code=status.HTTP_404_NOT_FOUND)
AssertionHelper.HTTP_404(response, error='another error')
def test_status_missing_in_multiple_errors(self, vnd_error_404):
vnd_error_404['errors'].append({'detail': 'another error'})
response = self.build_response(vnd_error_404, status_code=status.HTTP_404_NOT_FOUND)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_404(response, error='Not the correct error')
assert f'Error `Not the correct error` not found in' in str(err.value)
def test_exclusive_entity_refs_or_fields(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(), attributes={'test': 1})
assert 'Use Only `entity_refs` or explicit `attributes`, `relationships`, `resource`, and `pk` but not both' \
in str(err.value)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(), relationships={'test': 1})
assert 'Use Only `entity_refs` or explicit `attributes`, `relationships`, `resource`, and `pk` but not both' \
in str(err.value)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(), resource='test')
assert 'Use Only `entity_refs` or explicit `attributes`, `relationships`, `resource`, and `pk` but not both' \
in str(err.value)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(), pk='test')
assert 'Use Only `entity_refs` or explicit `attributes`, `relationships`, `resource`, and `pk` but not both' \
in str(err.value)
def test_vnd_with_non_jsonapi_data(self):
response = self.build_response(EXAMPLE_PLAIN)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, attributes=EXAMPLE_PLAIN)
assert f'response does not contain `data` property' in str(err.value)
def test_vnd_is_list(self, vnd_single, vnd_list):
single_response = self.build_response(vnd_single)
list_response = self.build_response(vnd_list)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(single_response, is_list=True)
assert 'Response should be a list' in str(err.value)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(list_response)
assert 'Response should not be a list' in str(err.value)
def test_vnd_attributes_match(self, vnd_single):
response = self.build_response(vnd_single)
AssertionHelper.HTTP_200(response, attributes=EXAMPLE_RESOURCE['attributes'])
def test_vnd_attributes_not_match(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, attributes=EXAMPLE_RESOURCE_2['attributes'])
assert f'Attribute Value incorrect `{EXAMPLE_RESOURCE_2["attributes"]["name"]}` in ' in str(err.value)
def test_vnd_relationships_should_be_entity_ref(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, relationships={'owner': EXAMPLE_RESOURCE_2})
assert f'asserted relationship is not an EntityRef' in str(err.value)
def test_vnd_relationships_match(self, vnd_single):
response = self.build_response(vnd_single)
AssertionHelper.HTTP_200(response, relationships={'owner': AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_USER['id'],
)})
def test_vnd_relationships_match_list(self, vnd_single):
response = self.build_response(vnd_single)
AssertionHelper.HTTP_200(response, relationships={
'owner': AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_USER['id'],
),
'children': [
AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE_2['type'],
pk=EXAMPLE_RESOURCE_2['id'],
),
AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE_4['type'],
pk=EXAMPLE_RESOURCE_4['id'],
),
]})
def test_vnd_relationships_not_match(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, relationships={'owner': AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_RESOURCE['id'],
)})
assert f'EntityRef resource type `{EXAMPLE_RESOURCE["type"]}` does not match' in str(err.value)
def test_vnd_relationships_not_match_in_list(self, vnd_single):
response = self.build_response(vnd_single)
relationship = AssertionHelper.EntityRef(resource=EXAMPLE_RESOURCE_3["type"],
pk=EXAMPLE_RESOURCE_3["id"],
attributes={})
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, relationships={'children': [
AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE_2['type'],
pk=EXAMPLE_RESOURCE_2['id'],
),
relationship,
]})
assert f'{relationship} NOT IN ' in str(err.value)
def test_vnd_included_should_be_entity_ref(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, included=EXAMPLE_RESOURCE_2)
assert f'asserted includes is not an EntityRef' in str(err.value)
def test_vnd_included_full_match(self, vnd_single):
response = self.build_response(vnd_single)
AssertionHelper.HTTP_200(response, included=AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE_2['type'],
pk=EXAMPLE_RESOURCE_2['id'],
attributes=EXAMPLE_RESOURCE_2['attributes'],
))
def test_vnd_included_full_not_match(self, vnd_single):
response = self.build_response(vnd_single)
include = AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_RESOURCE['id'],
attributes=EXAMPLE_RESOURCE['attributes'],
)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, included=include)
assert f'{include} NOT IN' in str(err.value)
def test_vnd_included_type_pk_match(self, vnd_single):
response = self.build_response(vnd_single)
AssertionHelper.HTTP_200(response, included=AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE_2['type'],
pk=EXAMPLE_RESOURCE_2['id'],
))
def test_vnd_included_type_pk_not_match(self, vnd_single):
response = self.build_response(vnd_single)
include = AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_RESOURCE['id'],
)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, included=include)
assert f'{include} NOT IN' in str(err.value)
def test_vnd_included_attributes_match(self, vnd_single):
response = self.build_response(vnd_single)
AssertionHelper.HTTP_200(response, included=AssertionHelper.EntityRef(
attributes=EXAMPLE_RESOURCE_2['attributes'],
))
def test_vnd_included_attributes_not_match(self, vnd_single):
response = self.build_response(vnd_single)
include = AssertionHelper.EntityRef(
attributes=EXAMPLE_RESOURCE['attributes'],
)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, included=include)
assert f'{include} NOT IN' in str(err.value)
def test_vnd_included_list_all_match(self, vnd_single):
response = self.build_response(vnd_single)
AssertionHelper.HTTP_200(response, included=[
AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE_2['type'],
pk=EXAMPLE_RESOURCE_2['id'],
attributes=EXAMPLE_RESOURCE_2['attributes']),
AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_USER['id'],
attributes=EXAMPLE_USER['attributes']),
])
def test_vnd_included_list_one_match(self, vnd_single):
response = self.build_response(vnd_single)
include_1 = AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_RESOURCE['id'],
attributes=EXAMPLE_RESOURCE['attributes'])
include_2 = AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_USER['id'],
attributes=EXAMPLE_USER['attributes'])
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, included=[include_1, include_2])
assert f'{include_1} NOT IN' in str(err.value)
def test_vnd_included_list_none_match(self, vnd_single):
response = self.build_response(vnd_single)
include_1 = AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_RESOURCE['id'],
attributes=EXAMPLE_RESOURCE['attributes'])
include_2 = AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE_3['type'],
pk=EXAMPLE_RESOURCE_3['id'],
attributes=EXAMPLE_RESOURCE_3['attributes'])
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, included=[include_1, include_2])
assert f'{include_1} NOT IN' in str(err.value)
def test_entity_list_non_list_response(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, entity_refs=[AssertionHelper.EntityRef()])
assert 'entity_refs should not be a list for a non-list response' in str(err.value)
def test_vnd_entity_uuid_pk(self, vnd_single):
response = self.build_response(vnd_single)
AssertionHelper.HTTP_200(
response,
entity_refs=AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=uuid.UUID(EXAMPLE_RESOURCE['id']),
attributes=EXAMPLE_RESOURCE['attributes'],
relationships={'owner': AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=uuid.UUID(EXAMPLE_USER['id']),
)}
),
included=AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE_2['type'],
pk=uuid.UUID(EXAMPLE_RESOURCE_2['id']),
))
def test_vnd_entity_full_match(self, vnd_single):
response = self.build_response(vnd_single)
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_RESOURCE['id'],
attributes=EXAMPLE_RESOURCE['attributes'],
relationships={'owner': AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_USER['id'],
)}
))
def test_vnd_entity_full_type_not_match(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_RESOURCE['id'],
attributes=EXAMPLE_RESOURCE['attributes'],
relationships={'owner': AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_USER['id'],
)}
))
assert f'Invalid Resource Type in' in str(err.value)
def test_vnd_entity_full_id_not_match(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_USER['id'],
attributes=EXAMPLE_RESOURCE['attributes'],
relationships={'owner': AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_USER['id'],
)}
))
assert f'Invalid ID in' in str(err.value)
def test_vnd_entity_full_attributes_missing(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_RESOURCE['id'],
attributes=EXAMPLE_USER['attributes'],
relationships={'owner': AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_USER['id'],
)}
))
assert f'Missing Attribute `username` in' in str(err.value)
def test_vnd_entity_full_attributes_not_match(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_RESOURCE['id'],
attributes=EXAMPLE_RESOURCE_2['attributes'],
relationships={'owner': AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_USER['id'],
)}
))
assert f'Attribute Value incorrect `example 2` in' in str(err.value)
def test_vnd_entity_full_relationships_type_not_match(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_RESOURCE['id'],
attributes=EXAMPLE_RESOURCE['attributes'],
relationships={'owner': AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_USER['id'],
)}
))
assert f'EntityRef resource type `{EXAMPLE_RESOURCE["type"]}` does not match' in str(err.value)
def test_vnd_entity_full_relationships_pk_not_match(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_RESOURCE['id'],
attributes=EXAMPLE_RESOURCE['attributes'],
relationships={'owner': AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_RESOURCE['id'],
)}
))
assert f'EntityRef ID `{EXAMPLE_RESOURCE["id"]}` does not match' in str(err.value)
def test_vnd_entity_type_pk_match(self, vnd_single):
response = self.build_response(vnd_single)
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_RESOURCE['id'],
))
def test_vnd_entity_type_pk_not_match(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_RESOURCE['id'],
))
assert f'Invalid Resource Type in' in str(err.value)
def test_vnd_entity_attribute_only_match(self, vnd_single):
response = self.build_response(vnd_single)
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
attributes=EXAMPLE_RESOURCE['attributes']
))
def test_vnd_entity_attribute_only_not_match(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, entity_refs=AssertionHelper.EntityRef(
attributes=EXAMPLE_RESOURCE_2['attributes'],
))
assert f'Attribute Value incorrect `example 2` in' in str(err.value)
def test_vnd_list_entity_full_match(self, vnd_list):
response = self.build_response(vnd_list)
AssertionHelper.HTTP_200(response, is_list=True, entity_refs=AssertionHelper.EntityRef(
resource=EXAMPLE_RESOURCE['type'],
pk=EXAMPLE_RESOURCE['id'],
attributes=EXAMPLE_RESOURCE['attributes'],
relationships={'owner': AssertionHelper.EntityRef(
resource=EXAMPLE_USER['type'],
pk=EXAMPLE_USER['id'],
)}
))
def test_vnd_list_entity_list_all_match(self, vnd_list, entity_ref_1, entity_ref_3):
response = self.build_response(vnd_list)
AssertionHelper.HTTP_200(response, is_list=True, entity_refs=[entity_ref_1, entity_ref_3])
def test_vnd_list_count(self, vnd_list):
response = self.build_response(vnd_list)
AssertionHelper.HTTP_200(response, is_list=True, count=len(vnd_list['data']))
def test_vnd_list_wrong_count(self, vnd_list):
list_length = len(vnd_list['data'])
response = self.build_response(vnd_list)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, is_list=True, count=list_length - 1)
assert f'Difference in count of response_data, got {list_length} expected {list_length - 1}' in str(err.value)
def test_vnd_single_count(self, vnd_single):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, count=1)
assert f'Count is only checked when response is list' in str(err.value)
def test_vnd_list_ordering(self, vnd_list, entity_ref_1, entity_ref_3):
response = self.build_response(vnd_list)
AssertionHelper.HTTP_200(response, is_list=True, entity_refs=[entity_ref_1, entity_ref_3], check_ordering=True)
def test_vnd_list_wrong_ordering(self, vnd_list, entity_ref_1, entity_ref_3):
response = self.build_response(vnd_list)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, is_list=True, entity_refs=[entity_ref_3, entity_ref_1],
check_ordering=True)
assert 'Invalid ID in ' in str(err.value)
def test_vnd_list_wrong_ordering_amount(self, vnd_list, entity_ref_1, entity_ref_3):
response = self.build_response(vnd_list)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, is_list=True, entity_refs=[entity_ref_1, entity_ref_3, entity_ref_1],
check_ordering=True)
assert 'Error: more entity refs supplied than available in response data. ' in str(err.value)
def test_vnd_single_ordering(self, vnd_single, entity_ref_1):
response = self.build_response(vnd_single)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, entity_refs=entity_ref_1, check_ordering=True)
assert f'Ordering is only checked when response is list' in str(err.value)
def test_vnd_list_entity_list_one_not_match(self, vnd_list, entity_ref_1, entity_ref_3):
response = self.build_response(vnd_list)
entity_ref_3.pk = EXAMPLE_RESOURCE_2['id']
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, is_list=True, entity_refs=[entity_ref_1, entity_ref_3])
assert f'{entity_ref_3} NOT IN' in str(err.value)
def test_plain_json_valid_parameters(self):
response = self.build_response(EXAMPLE_PLAIN)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, vnd=False, entity_refs={AssertionHelper.EntityRef()})
assert f'entity_refs not valid when vnd=False' in str(err.value)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, vnd=False, relationships=AssertionHelper.EntityRef())
assert f'relationships not valid when vnd=False' in str(err.value)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, vnd=False, included=AssertionHelper.EntityRef())
assert f'included not valid when vnd=False' in str(err.value)
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, vnd=False)
assert f'attributes must be provided when vnd=False' in str(err.value)
def test_plain_json_attributes(self):
response = self.build_response(EXAMPLE_PLAIN)
AssertionHelper.HTTP_200(response, vnd=False, attributes=EXAMPLE_PLAIN)
def test_plain_json_attributes_top_level_missing(self):
response = self.build_response(EXAMPLE_PLAIN)
invalid_attributes = EXAMPLE_PLAIN.copy()
invalid_attributes['new_field'] = 1
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, vnd=False, attributes=invalid_attributes)
assert f'Missing Attribute `new_field` in ' in str(err.value)
def test_plain_json_attributes_top_level_mismatch(self):
response = self.build_response(EXAMPLE_PLAIN)
invalid_attributes = EXAMPLE_PLAIN.copy()
invalid_attributes['id'] = 1
with pytest.raises(AssertionError) as err:
AssertionHelper.HTTP_200(response, vnd=False, attributes=invalid_attributes)
assert f'Attribute Value incorrect `1` in ' in str(err.value)
def test_plain_json_attributes_nested_missing(self):
response = self.build_response(EXAMPLE_PLAIN)
invalid_attributes = EXAMPLE_PLAIN.copy()