diff --git a/appengine/flexible/twilio/main.py b/appengine/flexible/twilio/main.py index e3fe8edd51a..4f1049fb558 100644 --- a/appengine/flexible/twilio/main.py +++ b/appengine/flexible/twilio/main.py @@ -17,8 +17,8 @@ import os from flask import Flask, request -from twilio import twiml -from twilio.rest import TwilioRestClient +from twilio import rest +from twilio.twiml import messaging_response, voice_response # [START configuration] @@ -35,7 +35,7 @@ @app.route('/call/receive', methods=['POST']) def receive_call(): """Answers a call and replies with a simple greeting.""" - response = twiml.Response() + response = voice_response.VoiceResponse() response.say('Hello from Twilio!') return str(response), 200, {'Content-Type': 'application/xml'} # [END receive_call] @@ -50,11 +50,12 @@ def send_sms(): return ('Please provide the number to message in the "to" query string' ' parameter.'), 400 - client = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN) + client = rest.Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN) rv = client.messages.create( to=to, from_=TWILIO_NUMBER, - body='Hello from Twilio!') + body='Hello from Twilio!' + ) return str(rv) # [END send_sms] @@ -68,7 +69,7 @@ def receive_sms(): message = 'Hello, {}, you said: {}'.format(sender, body) - response = twiml.Response() + response = messaging_response.MessagingResponse() response.message(message) return str(response), 200, {'Content-Type': 'application/xml'} # [END receive_sms] diff --git a/appengine/flexible/twilio/main_test.py b/appengine/flexible/twilio/main_test.py index b57c52d0d48..bfa037e2094 100644 --- a/appengine/flexible/twilio/main_test.py +++ b/appengine/flexible/twilio/main_test.py @@ -11,17 +11,10 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import re -import json - -from googleapiclient.http import HttpMockSequence -import httplib2 import pytest - - -class HttpMockSequenceWithCredentials(HttpMockSequence): - def add_credentials(self, *args): - pass +import responses @pytest.fixture @@ -41,17 +34,34 @@ def test_receive_call(app): assert 'Hello from Twilio!' in r.data.decode('utf-8') +@responses.activate def test_send_sms(app, monkeypatch): - httpmock = HttpMockSequenceWithCredentials([ - ({'status': '200'}, json.dumps({ - 'sid': 'sid123' - })) - ]) - - def mock_http_ctor(*args, **kwargs): - return httpmock - - monkeypatch.setattr(httplib2, 'Http', mock_http_ctor) + sample_response = { + "sid": "sid", + "date_created": "Wed, 20 Dec 2017 19:32:14 +0000", + "date_updated": "Wed, 20 Dec 2017 19:32:14 +0000", + "date_sent": None, + "account_sid": "account_sid", + "to": "+1234567890", + "from": "+9876543210", + "messaging_service_sid": None, + "body": "Hello from Twilio!", + "status": "queued", + "num_segments": "1", + "num_media": "0", + "direction": "outbound-api", + "api_version": "2010-04-01", + "price": None, + "price_unit": "USD", + "error_code": None, + "error_message": None, + "uri": "/2010-04-01/Accounts/sample.json", + "subresource_uris": { + "media": "/2010-04-01/Accounts/sample/Media.json" + } + } + responses.add(responses.POST, re.compile('.*'), + json=sample_response, status=200) r = app.get('/sms/send') assert r.status_code == 400 diff --git a/appengine/flexible/twilio/requirements.txt b/appengine/flexible/twilio/requirements.txt index dda690ab2e6..31c9947593a 100644 --- a/appengine/flexible/twilio/requirements.txt +++ b/appengine/flexible/twilio/requirements.txt @@ -1,3 +1,3 @@ Flask==0.12.2 gunicorn==19.7.1 -twilio<=6.0.0dev,==5.4.0 +twilio==6.10.0