Skip to content

Instantly share code, notes, and snippets.

@iL3D
Created June 1, 2015 23:10
Show Gist options
  • Save iL3D/e776b4f7f09744e50b99 to your computer and use it in GitHub Desktop.
Save iL3D/e776b4f7f09744e50b99 to your computer and use it in GitHub Desktop.
Unit tests for ported twimlets
import re
import unittest
from .context import app
app.config['TWILIO_ACCOUNT_SID'] = 'ACxxxxxx'
app.config['TWILIO_AUTH_TOKEN'] = 'yyyyyyyyy'
#app.config['TWILIO_CALLER_ID'] = '+15558675309'
class TwiMLTest(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
def assertTwiML(self, response, clause=None):
app.logger.info(response.data)
print("{}".format(response.data))
self.assertTrue(b"</Response>" in response.data,
"Did not find </Response>: {}".format(response.data))
if clause:
self.assertTrue(clause in response.data,
"Did not find {}: {}".format(clause,response.data))
self.assertEqual("200 OK", response.status)
def merge_params(self, params, extra_params):
all_params = params.copy()
all_params.update(extra_params)
return all_params
def sms(self, body, url='/sms', to=app.config['TWILIO_CALLER_ID'],
from_='+15558675309', extra_params=None):
params = {
'SmsSid': 'SMtesting',
'AccountSid': app.config['TWILIO_ACCOUNT_SID'],
'To': to,
'From': from_,
'Body': body}
if extra_params:
params = self.merge_params(params,extra_params)
return self.app.post(url, data=params)
def voice(self, url='/voice', to=app.config['TWILIO_CALLER_ID'],
from_='+15558675309', extra_params=None):
params = {
'CallSid': 'CAtesting',
'AccountSid': app.config['TWILIO_ACCOUNT_SID'],
'To': to,
'From': from_,
'CallStatus': 'ringing',
'Direction': 'inbound'}
if extra_params:
params = self.merge_params(params,extra_params)
return self.app.post(url, data=params)
class ExampleTests(TwiMLTest):
# Callme ==================================
def test_callme(self):
response = self.voice(url='/callme',extra_params={'PhoneNumber':'+15557779311','Message':'Hello world','FailUrl':'/vmail'})
# Is it dialing?
self.assertTrue( b"</Dial>" in response.data,
"Did not find </Dial>: {}".format(response.data))
# Is post-call behavior specified by setting action=""?
self.assertTrue( re.search(b'action="[^" ]+"', response.data),
"No action parameter set: {}".format(response.data))
# Is Dial set to indicate a second pass after the call
self.assertTrue( b"Dial=true" in response.data,
"Missing Dial flag: {}".format(response.data))
# Is "&" properly encoded with amp; suffixes
# Regexp does not but should verify all occurences of &
self.assertTrue( re.search(b'&(?=amp;)', response.data),
"Missing &amp; encodings: {}".format(response.data))
# Is FailUrl set
self.assertTrue( re.search(b'FailUrl=', response.data),
"Missing parameter for FailUrl: {}".format(response.data))
# Is whisper present... for defeating unintended voice mail pickups
self.assertTrue( b"/whisper?" in response.data,
"Did not find whisper: {}".format(response.data))
# Do endpoint and dial noun match
self.assertTrue( re.search(b"\+1\d{10}</Number>",response.data) or
re.search(b"sip:\S+@\S+</Sip>", response.data) or
re.search(b">\w+</Client>", response.data),
"Missing properly formated number or endpoint: {}".format(response.data))
self.assertTwiML(response)
def test_callme_answered(self):
response = self.voice(url='/callme', extra_params={'Dial':'true','DialStatus':'answered','DialCallStatus':''})
self.assertTwiML(response, b"<Hangup")
def test_callme_unanswered(self):
response = self.voice(url='/callme', extra_params={'FailUrl':'/vmail','Dial':'true','DialStatus':'no_answer','DialCallStatus':''})
self.assertTwiML(response,b"</Redirect>")
# ================================== Callme
# Menu ====================================
def test_menu(self):
response = self.voice(url='/menu',extra_params={'Options[2]':'/callme','Options[5]':'/vmail','Message':'Hello world'})
self.assertTrue(b"</Gather>" in response.data and
b"<Redirect" in response.data,
"Missing combination of gather+redirect: {}".format(response.data))
self.assertTwiML(response)
def test_menu_selection(self):
selection="/callme"
response = self.voice(url='/menu',extra_params={'Options[2]':selection,'Options[5]':'/vmail','Message':'Hello world','Digits':'2'})
self.assertTrue(b"</Redirect>" in response.data and
selection in str(response.data),
"Missing menu selection: {}".format(response.data))
self.assertTwiML(response)
selection="http://twimlets.com/vmail"
response = self.voice(url='/menu',extra_params={'Options[2]':selection,'Options[5]':selection,'Message':'Hello world','Digits':'5'})
self.assertTrue(b"</Redirect>" in response.data and
selection in str(response.data),
"Missing menu selection: {}".format(response.data))
self.assertTwiML(response)
response = self.voice(url='/menu',extra_params={'Options[2]':selection,'Options[5]':selection,'Message':'Hello world','Digits':'7'})
self.assertTrue(b"</Say>" in response.data and
b"<Hangup" in response.data and
re.search(b"not.+valid", response.data),
"Missing notification for invalid key press: {}".format(response.data))
self.assertTwiML(response)
# ==================================== Menu
'''Copyright (c) 2012 Twilio, Inc.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment