8000 Support unicode characters when validating requests. Make sure twiml … · MC6/twilio-python@e6a941c · GitHub
[go: up one dir, main page]

Skip to content

Commit e6a941c

Browse files
committed
Support unicode characters when validating requests. Make sure twiml attributes aren't lost
1 parent 6a7dff3 commit e6a941c

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

tests/test_phone_numbers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ def setUp(self):
1313
self.uri = "/base"
1414
self.auth = ("AC123", "token")
1515

16+
def test_application_sid(self):
17+
resource = PhoneNumbers(self.uri, self.auth)
18+
resource.update_instance = Mock()
19+
resource.update("SID", application_sid="foo")
20+
resource.update_instance.assert_called_with(
21+
"SID", {"ApplicationSid": "foo"})
22+
23+
1624
def test_status_callback_url(self):
1725
resource = PhoneNumbers(self.uri, self.auth)
1826
resource.update_instance = Mock()

tests/test_twiml.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from twilio import twiml
66
from twilio.twiml import TwimlException
77
from twilio.twiml import Response
8+
import xml.etree.ElementTree as ET
89

910
class TwilioTest(unittest.TestCase):
1011
def strip(self, xml):
@@ -288,6 +289,36 @@ def testBadAppend(self):
288289
""" should raise exceptions for wrong appending"""
289290
self.improperAppend(twiml.Sms("Hello"))
290291

292+
293+
class TestConference(TwilioTest):
294+
295+
def setUp(self):
296+
r = Response()
297+
with r.dial() as dial:
298+
dial.conference("TestConferenceAttributes", beep=False, waitUrl="",
299+
startConferenceOnEnter=True, endConferenceOnExit=True)
300+
xml = r.toxml()
301+
302+
#parse twiml XML string with Element Tree and inspect structure
303+
tree = ET.fromstring(xml)
304+
self.conf = tree.find(".//Dial/Conference")
305+
306+
def test_conf_text(self):
307+
self.assertEqual(self.conf.text.strip(), "TestConferenceAttributes")
308+
309+
def test_conf_beep(self):
310+
self.assertEqual(self.conf.get('beep'), "false")
311+
312+
def test_conf_waiturl(self):
313+
self.assertEqual(self.conf.get('waitUrl'), "")
314+
315+
def test_conf_start_conference(self):
316+
self.assertEqual(self.conf.get('startConferenceOnEnter'), "true")
317+
318+
def test_conf_end_conference(self):
319+
self.assertEqual(self.conf.get('endConferenceOnExit'), "true")
320+
321+
291322
class TestDial(TwilioTest):
292323

293324
def testDial(self):

twilio/twiml.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, **kwargs):
2222
for k, v in kwargs.items():
2323
if k == "sender":
2424
k = "from"
25-
if v:
25+
if v is not None:
2626
self.attrs[k] = v
2727

2828
def __str__(self):
@@ -54,7 +54,12 @@ def xml(self):
5454
keys = self.attrs.keys()
5555
keys.sort()
5656
for a in keys:
57-
el.set(a, str(self.attrs[a]))
57+
value = self.attrs[a]
58+
59+
if isinstance(value, bool):
60+
el.set(a, str(value).lower())
61+
else:
62+
el.set(a, str(value))
5863

5964
if self.body:
6065
el.text = self.body

twilio/util.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ def compute_signature(self, uri, params):
2424
2525
:returns: The computed signature
2626
"""
27-
s = uri
27+
s = unicode(uri)
2828
if len(params) > 0:
2929
for k, v in sorted(params.items()):
3030
s += k + v
3131

3232
# compute signature and compare signatures
33-
computed = base64.encodestring(hmac.new(self.token, s, sha1).digest())
33+
34+
mac = hmac.new(self.token, s.encode("utf-8"), sha1)
35+
computed = base64.b64encode(mac.digest())
36+
37+
# print base64.b64decode(computed.strip())
38+
# print base64.b64decode(computed.strip()).decode("utf-8")
39+
3440
return computed.strip()
3541

3642
def validate(self, uri, params, signature):

0 commit comments

Comments
 (0)
0