|
10 | 10 | class ValidationTest(unittest.TestCase):
|
11 | 11 |
|
12 | 12 | def setUp(self):
|
13 |
| - token = "1c892n40nd03kdnc0112slzkl3091j20" |
| 13 | + token = "12345" |
14 | 14 | self.validator = RequestValidator(token)
|
15 | 15 |
|
16 |
| - self.uri = "http://www.postbin.org/1ed898x" |
| 16 | + self.uri = "https://mycompany.com/myapp.php?foo=1&bar=2" |
17 | 17 | self.params = {
|
18 |
| - "AccountSid": "AC9a9f9392lad99kla0sklakjs90j092j3", |
19 |
| - "ApiVersion": "2010-04-01", |
20 |
| - "CallSid": "CAd800bb12c0426a7ea4230e492fef2a4f", |
21 |
| - "CallStatus": "ringing", |
22 |
| - "Called": "+15306384866", |
23 |
| - "CalledCity": "OAKLAND", |
24 |
| - "CalledCountry": "US", |
25 |
| - "CalledState": "CA", |
26 |
| - "CalledZip": "94612", |
27 |
| - "Caller": "+15306666666", |
28 |
| - "CallerCity": "SOUTH LAKE TAHOE", |
29 |
| - "CallerCountry": "US", |
30 |
| - "CallerName": "CA Wireless Call", |
31 |
| - "CallerState": "CA", |
32 |
| - "CallerZip": "89449", |
33 |
| - "Direction": "inbound", |
34 |
| - "From": "+15306666666", |
35 |
| - "FromCity": "SOUTH LAKE TAHOE", |
36 |
| - "FromCountry": "US", |
37 |
| - "FromState": "CA", |
38 |
| - "FromZip": "89449", |
39 |
| - "To": "+15306384866", |
40 |
| - "ToCity": "OAKLAND", |
41 |
| - "ToCountry": "US", |
42 |
| - "ToState": "CA", |
43 |
| - "ToZip": "94612", |
| 18 | + "CallSid": "CA1234567890ABCDE", |
| 19 | + "Digits": "1234", |
| 20 | + "From": "+14158675309", |
| 21 | + "To": "+18005551212", |
| 22 | + "Caller": "+14158675309", |
44 | 23 | }
|
| 24 | + self.expected = "RSOYDt4T1cUTdK1PDd93/VVr8B8=" |
| 25 | + self.body = "{\"property\": \"value\", \"boolean\": true}" |
| 26 | + self.bodyHash = "Ch/3Y02as7ldtcmi3+lBbkFQKyg6gMfPGWMmMvluZiA=" |
| 27 | + self.encodedBodyHash = self.bodyHash.replace("+", "%2B").replace("=", "%3D") |
| 28 | + self.uriWithBody = self.uri + "&bodySHA256=" + self.encodedBodyHash |
45 | 29 |
|
46 | 30 | def test_compute_signature_bytecode(self):
|
47 |
| - expected = b("fF+xx6dTinOaCdZ0aIeNkHr/ZAA=") |
| 31 | + expected = b(self.expected) |
48 | 32 | signature = self.validator.compute_signature(self.uri,
|
49 | 33 | self.params,
|
50 | 34 | utf=False)
|
51 | 35 | assert_equal(signature, expected)
|
52 | 36 |
|
53 | 37 | def test_compute_signature_unicode(self):
|
54 |
| - expected = u("fF+xx6dTinOaCdZ0aIeNkHr/ZAA=") |
| 38 | + expected = u(self.expected) |
55 | 39 | signature = self.validator.compute_signature(self.uri,
|
56 | 40 | self.params,
|
57 | 41 | utf=True)
|
58 | 42 | assert_equal(signature, expected)
|
59 | 43 |
|
| 44 | + def test_compute_hash_bytecode(self): |
| 45 | + expected = b(self.bodyHash) |
| 46 | + body_hash = self.validator.compute_hash(self.body, utf=False) |
| 47 | + |
| 48 | + assert_equal(expected, body_hash) |
| 49 | + |
| 50 | + def test_compute_hash_unicode(self): |
| 51 | + expected = u(self.bodyHash) |
| 52 | + body_hash = self.validator.compute_hash(self.body, utf=True) |
| 53 | + |
| 54 | + assert_equal(expected, body_hash) |
| 55 | + |
60 | 56 | def test_validation(self):
|
61 |
| - expected = "fF+xx6dTinOaCdZ0aIeNkHr/ZAA=" |
62 |
| - assert_true(self.validator.validate(self.uri, self.params, expected)) |
| 57 | + assert_true(self.validator.validate(self.uri, self.params, self.expected)) |
63 | 58 |
|
64 | 59 | def test_validation_removes_port_on_https(self):
|
65 |
| - self.uri = "https://www.postbin.org:1234/1ed898x" |
66 |
| - expected = "Y7MeICc5ECftd1G11Fc8qoxAn0A=" |
67 |
| - assert_true(self.validator.validate(self.uri, self.params, expected)) |
| 60 | + uri = self.uri.replace(".com", ".com:1234") |
| 61 | + assert_true(self.validator.validate(uri, self.params, self.expected)) |
| 62 | + |
| 63 | + def test_validation_of_body_succeeds(self): |
| 64 | + uri = self.uriWithBody |
| 65 | + is_valid = self.validator.validate(uri, self.body, "afcFvPLPYT8mg/JyIVkdnqQKa2s=") |
| 66 | + assert_true(is_valid) |
0 commit comments