Skip to content

Instantly share code, notes, and snippets.

@bjmc
Last active December 15, 2015 01:19
Show Gist options
  • Save bjmc/5179297 to your computer and use it in GitHub Desktop.
Save bjmc/5179297 to your computer and use it in GitHub Desktop.
Demonstration of testing a click-to-call service using Google voice
import unittest
import urllib2
from urllib import urlencode
from time import sleep, time
import logging
logging.basicConfig(level=logging.DEBUG)
from googlevoice import Voice
# https://code.google.com/r/bwpayne-pygooglevoice-auth-fix/
# https://code.google.com/p/pygooglevoice/
class PhoneTestCase(unittest.TestCase):
def setUp(self):
self.gv = Voice()
self.gv.login()
def get_call(self, timeout=600, initial_wait=90, poll_interval=10):
endtime = time() + timeout
sleep(initial_wait)
while True:
try:
self.new_call = self.most_recent_call()
return self.new_call
except NoCallReceived:
logging.debug("No call yet, waiting.")
if time() > endtime:
raise
sleep(poll_interval)
def most_recent_call(self):
inbox = self.gv.inbox()
try:
return inbox.messages[-1]
except IndexError:
raise NoCallReceived()
def tearDown(self):
logging.info("Deleting call from Google Voice inbox.")
try:
self.new_call.delete()
except AttributeError:
pass
class NoCallReceived(Exception):
pass
class TestClicktoCall(PhoneTestCase):
def setUp(self):
self.google_voice_number = '<GOOGLE VOICE NUMBER HERE>'
self.base_url = 'http://secure.cloud2ivr.com/click_to_xyz.php'
self.params = {'phone_to_call': self.google_voice_number,
'click_id': 53001,
'key': '26db7bfb486d5d542f24cf00da849afc003df951',
'click_key': 'eYhT2n4X'}
super(TestClicktoCall, self).setUp()
def test_click_to_call_makes_call(self):
"""Does hitting the click-to-call URL trigger a phone call?"""
url = '{0}?{1}'.format(self.base_url, urlencode(self.params))
call = self.place_call(url)
self.assertEqual(call['displayNumber'], '(800) 438-1709')
def place_call(self, url):
logging.info("Making GET to click-to-call URL {0}".format(url))
resp = urllib2.urlopen(url)
logging.debug("HTTP Response: {0}".format(resp.read()))
logging.info("Checking google voice for call...")
call = self.get_call(initial_wait=30)
logging.info("Found call from {0} at {1}".format(call['displayNumber'],
call['displayStartDateTime']))
return call
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment