-
Notifications
You must be signed in to change notification settings - Fork 807
Expand file tree
/
Copy pathtest_version.py
More file actions
1044 lines (890 loc) · 37.1 KB
/
test_version.py
File metadata and controls
1044 lines (890 loc) · 37.1 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
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 unittest
from unittest.mock import Mock
from tests import IntegrationTestCase
from tests.holodeck import Request
from twilio.base.version import Version
from twilio.base.exceptions import (
TwilioRestException,
TwilioServiceException,
TwilioException,
)
from twilio.base.page import Page
from twilio.http.response import Response
class TestPage(Page):
def get_instance(self, payload):
return payload
class StreamTestCase(IntegrationTestCase):
def setUp(self):
super(StreamTestCase, self).setUp()
self.holodeck.mock(
Response(
200,
"""
{
"next_page_uri": "/2010-04-01/Accounts/AC123/Messages.json?Page=1",
"messages": [{"body": "payload0"}, {"body": "payload1"}]
}
""",
),
Request(
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages.json"
),
)
self.holodeck.mock(
Response(
200,
"""
{
"next_page_uri": "/2010-04-01/Accounts/AC123/Messages.json?Page=2",
"messages": [{"body": "payload2"}, {"body": "payload3"}]
}
""",
),
Request(
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages.json?Page=1"
),
)
self.holodeck.mock(
Response(
200,
"""
{
"next_page_uri": null,
"messages": [{"body": "payload4"}]
}
""",
),
Request(
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages.json?Page=2"
),
)
self.version = self.client.api.v2010
self.response = self.version.page(
method="GET", uri="/Accounts/AC123/Messages.json"
)
self.page = TestPage(self.version, self.response, {})
def test_stream(self):
messages = list(self.version.stream(self.page))
self.assertEqual(len(messages), 5)
def test_stream_limit(self):
messages = list(self.version.stream(self.page, limit=3))
self.assertEqual(len(messages), 3)
def test_stream_page_limit(self):
messages = list(self.version.stream(self.page, page_limit=1))
self.assertEqual(len(messages), 2)
class VersionTestCase(IntegrationTestCase):
def test_fetch_redirect(self):
self.holodeck.mock(
Response(307, '{"redirect_to": "some_place"}'),
Request(url="https://messaging.twilio.com/v1/Deactivations"),
)
response = self.client.messaging.v1.fetch(method="GET", uri="/Deactivations")
self.assertIsNotNone(response)
def test_delete_success(self):
self.holodeck.mock(
Response(201, ""),
Request(
method="DELETE",
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages/MM123.json",
),
)
result = self.client.api.v2010.delete(
method="DELETE", uri="/Accounts/AC123/Messages/MM123.json"
)
self.assertTrue(result)
def test_delete_not_found(self):
self.holodeck.mock(
Response(404, '{"message": "Resource not found"}'),
Request(
method="DELETE",
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages/MM456.json",
),
)
with self.assertRaises(Exception) as context:
self.client.api.v2010.delete(
method="DELETE", uri="/Accounts/AC123/Messages/MM456.json"
)
self.assertIn("Unable to delete record", str(context.exception))
class VersionExceptionTestCase(unittest.TestCase):
"""Test cases for base Version.exception() method with RFC-9457 auto-detection"""
def test_exception_rfc9457_auto_detection(self):
"""Test that base Version auto-detects RFC-9457 errors and creates TwilioServiceException"""
response = Mock(spec=Response)
response.status_code = 400
response.text = """{
"type": "https://www.twilio.com/docs/api/errors/20001",
"title": "Invalid parameter",
"status": 400,
"code": 20001,
"detail": "The 'PhoneNumber' parameter is required.",
"instance": "/api/v1/accounts/AC123/calls/CA456"
}"""
exception = Version.exception(
method="POST", uri="/test", response=response, message="Test error"
)
self.assertIsInstance(exception, TwilioServiceException)
self.assertEqual(exception.type, "https://www.twilio.com/docs/api/errors/20001")
self.assertEqual(exception.title, "Invalid parameter")
self.assertEqual(exception.status, 400)
self.assertEqual(exception.code, 20001)
self.assertEqual(exception.detail, "The 'PhoneNumber' parameter is required.")
self.assertEqual(exception.instance, "/api/v1/accounts/AC123/calls/CA456")
self.assertEqual(exception.method, "POST")
self.assertEqual(exception.uri, "/test")
def test_exception_legacy_format_fallback(self):
"""Test that base Version falls back to TwilioRestException for legacy errors"""
response = Mock(spec=Response)
response.status_code = 400
response.text = """{
"message": "Invalid phone number",
"code": 21211
}"""
exception = Version.exception(
method="POST", uri="/test", response=response, message="Test error"
)
self.assertIsInstance(exception, TwilioRestException)
self.assertEqual(exception.status, 400)
self.assertEqual(exception.code, 21211)
self.assertIn("Invalid phone number", exception.msg)
def test_exception_rfc9457_with_validation_errors_base_version(self):
"""Test base Version handles RFC-9457 with validation errors array"""
response = Mock(spec=Response)
response.status_code = 422
response.text = """{
"type": "https://www.twilio.com/docs/api/errors/20001",
"title": "Validation failed",
"status": 422,
"code": 20001,
"detail": "Request validation failed",
"errors": [
{"detail": "must be a positive integer", "pointer": "#/age"},
{"detail": "must be 'green', 'red' or 'blue'", "pointer": "#/profile/color"}
]
}"""
exception = Version.exception(
method="POST", uri="/test", response=response, message="Validation error"
)
self.assertIsInstance(exception, TwilioServiceException)
self.assertEqual(exception.title, "Validation failed")
self.assertEqual(len(exception.errors), 2)
self.assertEqual(exception.errors[0]["detail"], "must be a positive integer")
self.assertEqual(exception.errors[0]["pointer"], "#/age")
def test_exception_malformed_json_fallback(self):
"""Test that base Version handles malformed JSON gracefully"""
response = Mock(spec=Response)
response.status_code = 500
response.text = "This is not JSON"
exception = Version.exception(
method="GET", uri="/test", response=response, message="Server error"
)
self.assertIsInstance(exception, TwilioRestException)
self.assertEqual(exception.status, 500)
class TwilioServiceExceptionTestCase(unittest.TestCase):
"""Comprehensive test cases for TwilioServiceException"""
def test_minimal_required_fields(self):
"""Test TwilioServiceException with only required fields"""
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20003",
title="Authentication Failed",
status=401,
code=20003,
)
self.assertEqual(exc.type, "https://www.twilio.com/docs/api/errors/20003")
self.assertEqual(exc.title, "Authentication Failed")
self.assertEqual(exc.status, 401)
self.assertEqual(exc.code, 20003)
self.assertIsNone(exc.detail)
self.assertIsNone(exc.instance)
self.assertEqual(exc.errors, [])
self.assertEqual(exc.method, "GET")
self.assertEqual(exc.uri, "")
def test_all_fields_populated(self):
"""Test TwilioServiceException with all fields populated"""
errors = [
{"detail": "Field is required", "pointer": "#/name"},
{"detail": "Must be a valid email", "pointer": "#/email"},
]
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20001",
title="Invalid Request",
status=400,
code=20001,
detail="The request could not be processed",
instance="/api/v1/accounts/AC123/messages/SM456",
errors=errors,
method="POST",
uri="/api/v1/messages",
)
self.assertEqual(exc.type, "https://www.twilio.com/docs/api/errors/20001")
self.assertEqual(exc.title, "Invalid Request")
self.assertEqual(exc.status, 400)
self.assertEqual(exc.code, 20001)
self.assertEqual(exc.detail, "The request could not be processed")
self.assertEqual(exc.instance, "/api/v1/accounts/AC123/messages/SM456")
self.assertEqual(len(exc.errors), 2)
self.assertEqual(exc.errors[0]["detail"], "Field is required")
self.assertEqual(exc.errors[0]["pointer"], "#/name")
self.assertEqual(exc.method, "POST")
self.assertEqual(exc.uri, "/api/v1/messages")
def test_partial_optional_fields(self):
"""Test TwilioServiceException with some optional fields"""
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20002",
title="Rate Limit Exceeded",
status=429,
code=20002,
detail="Too many requests",
method="POST",
uri="/api/v1/calls",
)
self.assertEqual(exc.detail, "Too many requests")
self.assertIsNone(exc.instance)
self.assertEqual(exc.errors, [])
self.assertEqual(exc.method, "POST")
self.assertEqual(exc.uri, "/api/v1/calls")
def test_empty_errors_array(self):
"""Test TwilioServiceException with explicitly empty errors array"""
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20001",
title="Bad Request",
status=400,
code=20001,
errors=[],
)
self.assertEqual(exc.errors, [])
def test_none_errors_becomes_empty_list(self):
"""Test that None errors parameter becomes empty list"""
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20001",
title="Bad Request",
status=400,
code=20001,
errors=None,
)
self.assertEqual(exc.errors, [])
self.assertIsInstance(exc.errors, list)
def test_different_http_methods(self):
"""Test TwilioServiceException with different HTTP methods"""
methods = ["GET", "POST", "PUT", "PATCH", "DELETE"]
for method in methods:
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20001",
title="Error",
status=400,
code=20001,
method=method,
)
self.assertEqual(exc.method, method)
def test_different_status_codes(self):
"""Test TwilioServiceException with various HTTP status codes"""
status_codes = [400, 401, 403, 404, 422, 429, 500, 502, 503]
for status_code in status_codes:
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20001",
title="Error",
status=status_code,
code=20001,
)
self.assertEqual(exc.status, status_code)
def test_string_representation_non_tty(self):
"""Test __str__ method for non-TTY output (plain text)"""
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20001",
title="Invalid Parameter",
status=400,
code=20001,
detail="The PhoneNumber parameter is required",
)
# Mock sys.stderr to simulate non-TTY
import sys
original_stderr = sys.stderr
try:
sys.stderr = Mock()
sys.stderr.isatty = Mock(return_value=False)
result = str(exc)
self.assertIn("HTTP 400 error", result)
self.assertIn("Invalid Parameter", result)
self.assertIn("The PhoneNumber parameter is required", result)
finally:
sys.stderr = original_stderr
def test_string_representation_non_tty_with_validation_errors(self):
"""Test __str__ method for non-TTY with validation errors"""
errors = [
{"detail": "must be positive", "pointer": "#/age"},
{"detail": "must be valid email", "pointer": "#/email"},
]
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20001",
title="Validation Failed",
status=422,
code=20001,
detail="Request validation failed",
errors=errors,
)
import sys
original_stderr = sys.stderr
try:
sys.stderr = Mock()
sys.stderr.isatty = Mock(return_value=False)
result = str(exc)
self.assertIn("HTTP 422 error", result)
self.assertIn("Validation Failed", result)
self.assertIn("Request validation failed", result)
self.assertIn("Validation errors", result)
self.assertIn("#/age", result)
self.assertIn("must be positive", result)
finally:
sys.stderr = original_stderr
def test_string_representation_tty(self):
"""Test __str__ method for TTY output (colored)"""
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20001",
title="Invalid Parameter",
status=400,
code=20001,
detail="The PhoneNumber parameter is required",
instance="/api/v1/accounts/AC123/calls/CA456",
method="POST",
uri="/api/v1/calls",
)
import sys
original_stderr = sys.stderr
try:
sys.stderr = Mock()
sys.stderr.isatty = Mock(return_value=True)
result = str(exc)
# Check for ANSI escape codes (color formatting)
self.assertIn("\033[", result)
# Check for content
self.assertIn("HTTP Error", result)
self.assertIn("POST /api/v1/calls", result)
self.assertIn("Invalid Parameter", result)
self.assertIn("The PhoneNumber parameter is required", result)
self.assertIn("400", result)
self.assertIn("20001", result)
self.assertIn("https://www.twilio.com/docs/api/errors/20001", result)
finally:
sys.stderr = original_stderr
def test_string_representation_tty_with_validation_errors(self):
"""Test __str__ method for TTY with validation errors (colored)"""
errors = [
{"detail": "must be positive", "pointer": "#/age"},
{"detail": "must be valid", "pointer": "#/email"},
]
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20001",
title="Validation Failed",
status=422,
code=20001,
errors=errors,
method="POST",
uri="/api/v1/users",
)
import sys
original_stderr = sys.stderr
try:
sys.stderr = Mock()
sys.stderr.isatty = Mock(return_value=True)
result = str(exc)
self.assertIn("Validation Errors", result)
self.assertIn("#/age", result)
self.assertIn("must be positive", result)
self.assertIn("#/email", result)
self.assertIn("must be valid", result)
finally:
sys.stderr = original_stderr
def test_string_representation_tty_no_uri(self):
"""Test __str__ method when uri is empty"""
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20001",
title="Error",
status=400,
code=20001,
method="GET",
uri="",
)
import sys
original_stderr = sys.stderr
try:
sys.stderr = Mock()
sys.stderr.isatty = Mock(return_value=True)
result = str(exc)
self.assertIn("(no URI)", result)
finally:
sys.stderr = original_stderr
def test_validation_errors_with_missing_fields(self):
"""Test validation errors with missing detail or pointer fields"""
errors = [
{"detail": "error detail"}, # missing pointer
{"pointer": "#/field"}, # missing detail
{}, # both missing
]
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20001",
title="Validation Failed",
status=422,
code=20001,
errors=errors,
)
import sys
original_stderr = sys.stderr
try:
# Test non-TTY output
sys.stderr = Mock()
sys.stderr.isatty = Mock(return_value=False)
result = str(exc)
# Should handle missing fields gracefully
self.assertIn("Validation errors", result)
finally:
sys.stderr = original_stderr
def test_multiple_validation_errors(self):
"""Test with multiple validation errors"""
errors = [
{"detail": "Field is required", "pointer": "#/firstName"},
{"detail": "Must be at least 18", "pointer": "#/age"},
{"detail": "Invalid format", "pointer": "#/phoneNumber"},
{"detail": "Must be unique", "pointer": "#/email"},
]
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20001",
title="Validation Failed",
status=422,
code=20001,
errors=errors,
)
self.assertEqual(len(exc.errors), 4)
self.assertEqual(exc.errors[2]["pointer"], "#/phoneNumber")
self.assertEqual(exc.errors[3]["detail"], "Must be unique")
def test_exception_as_parent_class(self):
"""Test that TwilioServiceException is a proper exception"""
exc = TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20001",
title="Error",
status=400,
code=20001,
)
self.assertIsInstance(exc, Exception)
self.assertIsInstance(exc, TwilioException)
def test_exception_can_be_raised_and_caught(self):
"""Test that TwilioServiceException can be raised and caught"""
with self.assertRaises(TwilioServiceException) as context:
raise TwilioServiceException(
type_uri="https://www.twilio.com/docs/api/errors/20001",
title="Test Error",
status=400,
code=20001,
)
self.assertEqual(context.exception.title, "Test Error")
self.assertEqual(context.exception.code, 20001)
class ResponseInfoIntegrationTestCase(IntegrationTestCase):
"""Integration tests for *_with_response_info methods"""
def test_fetch_with_response_info(self):
self.holodeck.mock(
Response(
200,
'{"sid": "AC123", "name": "Test Account"}',
{"X-Custom-Header": "test-value"},
),
Request(url="https://api.twilio.com/2010-04-01/Accounts/AC123.json"),
)
payload, status_code, headers = self.client.api.v2010.fetch_with_response_info(
method="GET", uri="/Accounts/AC123.json"
)
self.assertEqual(payload["sid"], "AC123")
self.assertEqual(payload["name"], "Test Account")
self.assertEqual(status_code, 200)
self.assertIn("X-Custom-Header", headers)
self.assertEqual(headers["X-Custom-Header"], "test-value")
def test_update_with_response_info(self):
self.holodeck.mock(
Response(
200,
'{"sid": "AC123", "name": "Updated Account"}',
{"X-Update-Header": "updated"},
),
Request(
method="POST",
url="https://api.twilio.com/2010-04-01/Accounts/AC123.json",
),
)
payload, status_code, headers = self.client.api.v2010.update_with_response_info(
method="POST", uri="/Accounts/AC123.json", data={"name": "Updated Account"}
)
self.assertEqual(payload["sid"], "AC123")
self.assertEqual(payload["name"], "Updated Account")
self.assertEqual(status_code, 200)
self.assertIn("X-Update-Header", headers)
def test_delete_with_response_info(self):
self.holodeck.mock(
Response(204, "", {"X-Delete-Header": "deleted"}),
Request(
method="DELETE",
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages/MM123.json",
),
)
success, status_code, headers = self.client.api.v2010.delete_with_response_info(
method="DELETE", uri="/Accounts/AC123/Messages/MM123.json"
)
self.assertTrue(success)
self.assertEqual(status_code, 204)
self.assertIn("X-Delete-Header", headers)
def test_create_with_response_info(self):
self.holodeck.mock(
Response(
201,
'{"sid": "MM123", "body": "Hello World"}',
{"X-Create-Header": "created"},
),
Request(
method="POST",
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages.json",
),
)
payload, status_code, headers = self.client.api.v2010.create_with_response_info(
method="POST",
uri="/Accounts/AC123/Messages.json",
data={"body": "Hello World"},
)
self.assertEqual(payload["sid"], "MM123")
self.assertEqual(payload["body"], "Hello World")
self.assertEqual(status_code, 201)
self.assertIn("X-Create-Header", headers)
def test_page_with_response_info(self):
self.holodeck.mock(
Response(
200,
'{"messages": [], "next_page_uri": null}',
{"X-Page-Header": "page"},
),
Request(
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages.json"
),
)
response, status_code, headers = self.client.api.v2010.page_with_response_info(
method="GET", uri="/Accounts/AC123/Messages.json"
)
self.assertIsNotNone(response)
self.assertEqual(status_code, 200)
self.assertIn("X-Page-Header", headers)
def test_fetch_with_response_info_error(self):
self.holodeck.mock(
Response(404, '{"message": "Resource not found"}'),
Request(url="https://api.twilio.com/2010-04-01/Accounts/AC456.json"),
)
with self.assertRaises(Exception) as context:
self.client.api.v2010.fetch_with_response_info(
method="GET", uri="/Accounts/AC456.json"
)
self.assertIn("Unable to fetch record", str(context.exception))
def test_update_with_response_info_error(self):
self.holodeck.mock(
Response(400, '{"message": "Invalid request"}'),
Request(
method="POST",
url="https://api.twilio.com/2010-04-01/Accounts/AC123.json",
),
)
with self.assertRaises(Exception) as context:
self.client.api.v2010.update_with_response_info(
method="POST", uri="/Accounts/AC123.json", data={"invalid": "data"}
)
self.assertIn("Unable to update record", str(context.exception))
def test_delete_with_response_info_error(self):
self.holodeck.mock(
Response(404, '{"message": "Resource not found"}'),
Request(
method="DELETE",
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages/MM456.json",
),
)
with self.assertRaises(Exception) as context:
self.client.api.v2010.delete_with_response_info(
method="DELETE", uri="/Accounts/AC123/Messages/MM456.json"
)
self.assertIn("Unable to delete record", str(context.exception))
def test_create_with_response_info_error(self):
self.holodeck.mock(
Response(400, '{"message": "Invalid request"}'),
Request(
method="POST",
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages.json",
),
)
with self.assertRaises(Exception) as context:
self.client.api.v2010.create_with_response_info(
method="POST",
uri="/Accounts/AC123/Messages.json",
data={"invalid": "data"},
)
self.assertIn("Unable to create record", str(context.exception))
def test_fetch_with_response_info_empty_headers(self):
self.holodeck.mock(
Response(200, '{"sid": "AC123", "name": "Test Account"}', None),
Request(url="https://api.twilio.com/2010-04-01/Accounts/AC123.json"),
)
payload, status_code, headers = self.client.api.v2010.fetch_with_response_info(
method="GET", uri="/Accounts/AC123.json"
)
self.assertEqual(payload["sid"], "AC123")
self.assertEqual(status_code, 200)
self.assertIsInstance(headers, dict)
self.assertEqual(len(headers), 0)
def test_fetch_with_response_info_multiple_headers(self):
self.holodeck.mock(
Response(
200,
'{"sid": "AC123"}',
{
"X-Custom-Header-1": "value1",
"X-Custom-Header-2": "value2",
"Content-Type": "application/json",
},
),
Request(url="https://api.twilio.com/2010-04-01/Accounts/AC123.json"),
)
payload, status_code, headers = self.client.api.v2010.fetch_with_response_info(
method="GET", uri="/Accounts/AC123.json"
)
self.assertEqual(status_code, 200)
self.assertIn("X-Custom-Header-1", headers)
self.assertIn("X-Custom-Header-2", headers)
self.assertIn("Content-Type", headers)
self.assertEqual(headers["X-Custom-Header-1"], "value1")
self.assertEqual(headers["X-Custom-Header-2"], "value2")
def test_fetch_with_response_info_returns_tuple(self):
self.holodeck.mock(
Response(200, '{"sid": "AC123"}', {"X-Test": "test"}),
Request(url="https://api.twilio.com/2010-04-01/Accounts/AC123.json"),
)
result = self.client.api.v2010.fetch_with_response_info(
method="GET", uri="/Accounts/AC123.json"
)
self.assertIsInstance(result, tuple)
self.assertEqual(len(result), 3)
payload, status_code, headers = result
self.assertIsInstance(payload, dict)
self.assertIsInstance(status_code, int)
self.assertIsInstance(headers, dict)
class AsyncVersionTestCase(unittest.IsolatedAsyncioTestCase):
def setUp(self):
from tests.holodeck import AsyncHolodeck
from twilio.rest import Client
self.holodeck = AsyncHolodeck()
self.client = Client(
"ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "AUTHTOKEN", http_client=self.holodeck
)
async def test_fetch_with_response_info_async(self):
"""Test fetch_with_response_info_async method"""
self.holodeck.mock(
Response(
200,
'{"sid": "AC123", "name": "Test Account"}',
{"X-Custom-Header": "test-value"},
),
Request(url="https://api.twilio.com/2010-04-01/Accounts/AC123.json"),
)
payload, status_code, headers = (
await self.client.api.v2010.fetch_with_response_info_async(
method="GET", uri="/Accounts/AC123.json"
)
)
self.assertEqual(payload["sid"], "AC123")
self.assertEqual(payload["name"], "Test Account")
self.assertEqual(status_code, 200)
self.assertIn("X-Custom-Header", headers)
self.assertEqual(headers["X-Custom-Header"], "test-value")
async def test_update_with_response_info_async(self):
"""Test update_with_response_info_async method"""
self.holodeck.mock(
Response(
200,
'{"sid": "AC123", "name": "Updated Account"}',
{"X-Update-Header": "updated"},
),
Request(
method="POST",
url="https://api.twilio.com/2010-04-01/Accounts/AC123.json",
),
)
payload, status_code, headers = (
await self.client.api.v2010.update_with_response_info_async(
method="POST",
uri="/Accounts/AC123.json",
data={"name": "Updated Account"},
)
)
self.assertEqual(payload["sid"], "AC123")
self.assertEqual(payload["name"], "Updated Account")
self.assertEqual(status_code, 200)
self.assertIn("X-Update-Header", headers)
async def test_delete_with_response_info_async(self):
"""Test delete_with_response_info_async method"""
self.holodeck.mock(
Response(204, "", {"X-Delete-Header": "deleted"}),
Request(
method="DELETE",
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages/MM123.json",
),
)
success, status_code, headers = (
await self.client.api.v2010.delete_with_response_info_async(
method="DELETE", uri="/Accounts/AC123/Messages/MM123.json"
)
)
self.assertTrue(success)
self.assertEqual(status_code, 204)
self.assertIn("X-Delete-Header", headers)
async def test_create_with_response_info_async(self):
"""Test create_with_response_info_async method"""
self.holodeck.mock(
Response(
201,
'{"sid": "MM123", "body": "Hello World"}',
{"X-Create-Header": "created"},
),
Request(
method="POST",
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages.json",
),
)
payload, status_code, headers = (
await self.client.api.v2010.create_with_response_info_async(
method="POST",
uri="/Accounts/AC123/Messages.json",
data={"body": "Hello World"},
)
)
self.assertEqual(payload["sid"], "MM123")
self.assertEqual(payload["body"], "Hello World")
self.assertEqual(status_code, 201)
self.assertIn("X-Create-Header", headers)
async def test_page_with_response_info_async(self):
"""Test page_with_response_info_async method"""
self.holodeck.mock(
Response(
200,
'{"messages": [], "next_page_uri": null}',
{"X-Page-Header": "page"},
),
Request(
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages.json"
),
)
response, status_code, headers = (
await self.client.api.v2010.page_with_response_info_async(
method="GET", uri="/Accounts/AC123/Messages.json"
)
)
self.assertIsNotNone(response)
self.assertEqual(status_code, 200)
self.assertIn("X-Page-Header", headers)
async def test_fetch_with_response_info_async_error(self):
"""Test fetch_with_response_info_async method with error"""
self.holodeck.mock(
Response(404, '{"message": "Resource not found"}'),
Request(url="https://api.twilio.com/2010-04-01/Accounts/AC456.json"),
)
with self.assertRaises(Exception) as context:
await self.client.api.v2010.fetch_with_response_info_async(
method="GET", uri="/Accounts/AC456.json"
)
self.assertIn("Unable to fetch record", str(context.exception))
async def test_update_with_response_info_async_error(self):
"""Test update_with_response_info_async method with error"""
self.holodeck.mock(
Response(400, '{"message": "Invalid request"}'),
Request(
method="POST",
url="https://api.twilio.com/2010-04-01/Accounts/AC123.json",
),
)
with self.assertRaises(Exception) as context:
await self.client.api.v2010.update_with_response_info_async(
method="POST", uri="/Accounts/AC123.json", data={"invalid": "data"}
)
self.assertIn("Unable to update record", str(context.exception))
async def test_delete_with_response_info_async_error(self):
"""Test delete_with_response_info_async method with error"""
self.holodeck.mock(
Response(404, '{"message": "Resource not found"}'),
Request(
method="DELETE",
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages/MM456.json",
),
)
with self.assertRaises(Exception) as context:
await self.client.api.v2010.delete_with_response_info_async(
method="DELETE", uri="/Accounts/AC123/Messages/MM456.json"
)
self.assertIn("Unable to delete record", str(context.exception))
async def test_create_with_response_info_async_error(self):
"""Test create_with_response_info_async method with error"""
self.holodeck.mock(
Response(400, '{"message": "Invalid request"}'),
Request(
method="POST",
url="https://api.twilio.com/2010-04-01/Accounts/AC123/Messages.json",
),
)
with self.assertRaises(Exception) as context:
await self.client.api.v2010.create_with_response_info_async(
method="POST",
uri="/Accounts/AC123/Messages.json",
data={"invalid": "data"},
)
self.assertIn("Unable to create record", str(context.exception))
async def test_fetch_with_response_info_async_empty_headers(self):
"""Test fetch_with_response_info_async with None headers"""
self.holodeck.mock(
Response(200, '{"sid": "AC123", "name": "Test Account"}', None),
Request(url="https://api.twilio.com/2010-04-01/Accounts/AC123.json"),
)
payload, status_code, headers = (
await self.client.api.v2010.fetch_with_response_info_async(
method="GET", uri="/Accounts/AC123.json"
)
)
self.assertEqual(payload["sid"], "AC123")
self.assertEqual(status_code, 200)
self.assertIsInstance(headers, dict)
self.assertEqual(len(headers), 0)