1
+ import sys
2
+ import time
3
+ import unittest
4
+
5
+ from freeclimb .utils .request_verifier import RequestVerifier
6
+
7
+ class TestRequestVerifier (unittest .TestCase ):
8
+ """RequestVerifier unit test stubs"""
9
+
10
+ def setUp (self ):
11
+ self .request_verifier = RequestVerifier ()
12
+
13
+ def tearDown (self ):
14
+ pass
15
+
16
+ def test_check_request_body (self ):
17
+ request_body = ""
18
+ signing_secret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793"
19
+ tolerance = 5 * 60
20
+ request_header = "t=1679944186,v1=c3957749baf61df4b1506802579cc69a74c77a1ae21447b930e5a704f9ec4120,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8"
21
+ with self .assertRaises (Exception ) as exc :
22
+ RequestVerifier .verify_request_signature (request_body , request_header , signing_secret , tolerance )
23
+ self .assertEqual (str (exc .exception ), "Request Body cannot be empty or null" )
24
+
25
+ def test_check_request_header_no_signatures (self ):
26
+ request_body = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }"
27
+ signing_secret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793"
28
+ tolerance = 5 * 60
29
+ request_header = "t=1679944186,"
30
+ with self .assertRaises (Exception ) as exc :
31
+ RequestVerifier .verify_request_signature (request_body , request_header , signing_secret , tolerance )
32
+ self .assertEqual (str (exc .exception ), "Error with request header, signatures are not present" )
33
+
34
+ def test_check_request_header_no_timestamp (self ):
35
+ request_body = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }"
36
+ signing_secret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793"
37
+ tolerance = 5 * 60
38
+ request_header = "v1=c3957749baf61df4b1506802579cc69a74c77a1ae21447b930e5a704f9ec4120,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8"
39
+ with self .assertRaises (Exception ) as exc :
40
+ RequestVerifier .verify_request_signature (request_body , request_header , signing_secret , tolerance )
41
+ self .assertEqual (str (exc .exception ), "Error with request header, timestamp is not present" )
42
+
43
+ def test_check_request_header_empty_request_header (self ):
44
+ request_body = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }"
45
+ signing_secret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793"
46
+ tolerance = 5 * 60
47
+ request_header = ""
48
+ with self .assertRaises (Exception ) as exc :
49
+ RequestVerifier .verify_request_signature (request_body , request_header , signing_secret , tolerance )
50
+ self .assertEqual (str (exc .exception ), "Error with request header, Request header is empty" )
51
+
52
+ def test_check_signing_secret (self ):
53
+ request_body = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }"
54
+ signing_secret = ""
55
+ tolerance = 5 * 60
56
+ request_header = "t=1679944186,v1=c3957749baf61df4b1506802579cc69a74c77a1ae21447b930e5a704f9ec4120,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8"
57
+ with self .assertRaises (Exception ) as exc :
58
+ RequestVerifier .verify_request_signature (request_body , request_header , signing_secret , tolerance )
59
+ self .assertEqual (str (exc .exception ), "Signing secret cannot be empty or null" )
60
+
61
+ def test_check_tolerance_max_int (self ):
62
+ request_body = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }"
63
+ signing_secret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793"
64
+ tolerance = sys .maxsize
65
+ request_header = "t=1679944186,v1=c3957749baf61df4b1506802579cc69a74c77a1ae21447b930e5a704f9ec4120,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8"
66
+ with self .assertRaises (Exception ) as exc :
67
+ RequestVerifier .verify_request_signature (request_body , request_header , signing_secret , tolerance )
68
+ self .assertEqual (str (exc .exception ), "Tolerance value must be a positive integer" )
69
+
70
+ def test_check_tolerance_zero_value (self ):
71
+ request_body = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }"
72
+ signing_secret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793"
73
+ tolerance = 0
74
+ request_header = "t=1679944186,v1=c3957749baf61df4b1506802579cc69a74c77a1ae21447b930e5a704f9ec4120,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8"
75
+ with self .assertRaises (Exception ) as exc :
76
+ self .request_verifier .verify_request_signature (request_body , request_header , signing_secret , tolerance )
77
+ self .assertEqual (str (exc .exception ), "Tolerance value must be a positive integer" )
78
+
79
+ def test_check_tolerance_negative_value (self ):
80
+ request_body = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }"
81
+ signing_secret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793"
82
+ tolerance = - 5
83
+ request_header = "t=1679944186,v1=c3957749baf61df4b1506802579cc69a74c77a1ae21447b930e5a704f9ec4120,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8"
84
+ with self .assertRaises (Exception ) as exc :
85
+ RequestVerifier .verify_request_signature (request_body , request_header , signing_secret , tolerance )
86
+ self .assertEqual (str (exc .exception ), "Tolerance value must be a positive integer" )
87
+
88
+ def test_verify_tolerance (self ):
89
+ current_time = int (time .time ())
90
+ request_body = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }"
91
+ signing_secret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793"
92
+ tolerance = 5 * 60
93
+ request_header = "t=1900871395,v1=1d798c86e977ff734dec3a8b8d67fe8621dcc1df46ef4212e0bfe2e122b01bfd,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8"
94
+ with self .assertRaises (Exception ) as exc :
95
+ RequestVerifier .verify_request_signature (request_body , request_header , signing_secret , tolerance )
96
+ self .assertEqual (str (exc .exception ), "Request time exceeded tolerance threshold. Request: 1900871395"
97
+ + ", CurrentTime: " + str (current_time ) + ", tolerance: " + str (tolerance ))
98
+
99
+ def test_verify_signature (self ):
100
+ request_body = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }"
101
+ signing_secret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7794"
102
+ tolerance = 5 * 60
103
+ request_header = "t=1679944186,v1=c3957749baf61df4b1506802579cc69a74c77a1ae21447b930e5a704f9ec4120,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8"
104
+ with self .assertRaises (Exception ) as exc :
105
+ RequestVerifier .verify_request_signature (request_body , request_header , signing_secret , tolerance )
106
+ self .assertEqual (str (exc .exception ), "Unverified signature request, If this request was unexpected, it may be from a bad actor. Please proceed with caution. If the request was exepected, please check any typos or issues with the signingSecret" )
107
+
108
+ def test_verify_request_signature (self ):
109
+ request_body = "{\" accountId\" :\" AC1334ffb694cd8d969f51cddf5f7c9b478546d50c\" ,\" callId\" :\" CAccb0b00506553cda09b51c5477f672a49e0b2213\" ,\" callStatus\" :\" ringing\" ,\" conferenceId\" :null,\" direction\" :\" inbound\" ,\" from\" :\" +13121000109\" ,\" parentCallId\" :null,\" queueId\" :null,\" requestType\" :\" inboundCall\" ,\" to\" :\" +13121000096\" }"
110
+ signing_secret = "sigsec_ead6d3b6904196c60835d039e91b3341c77a7793"
111
+ tolerance = 5 * 60
112
+ request_header = "t=1679944186,v1=c3957749baf61df4b1506802579cc69a74c77a1ae21447b930e5a704f9ec4120,v1=1ba18712726898fbbe48cd862dd096a709f7ad761a5bab14bda9ac24d963a6a8"
113
+ raised = False
114
+ try :
115
+ RequestVerifier .verify_request_signature (request_body , request_header , signing_secret , tolerance )
116
+ except :
117
+ raised = True
118
+ self .assertFalse (raised , 'Exception has been raised' )
119
+
120
+
121
+
122
+ if __name__ == '__main__' :
123
+ unittest .main ()
0 commit comments