Skip to content

Commit e56eb4d

Browse files
author
Jon Wayne Parrott
committed
Adding tests for the twilio sample. Fixes #183 (#354)
1 parent c5c6c95 commit e56eb4d

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

managed_vms/twilio/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def send_sms():
4848
to = request.args.get('to')
4949
if not to:
5050
return ('Please provide the number to message in the "to" query string'
51-
' parameter.')
51+
' parameter.'), 400
5252

5353
client = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
5454
rv = client.messages.create(

managed_vms/twilio/main_test.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import json
16+
17+
from googleapiclient.http import HttpMockSequence
18+
import httplib2
19+
import pytest
20+
21+
22+
class HttpMockSequenceWithCredentials(HttpMockSequence):
23+
def add_credentials(self, *args):
24+
pass
25+
26+
27+
@pytest.fixture
28+
def app(monkeypatch):
29+
monkeypatch.setenv('TWILIO_ACCOUNT_SID', 'sid123')
30+
monkeypatch.setenv('TWILIO_AUTH_TOKEN', 'auth123')
31+
monkeypatch.setenv('TWILIO_NUMBER', '0123456789')
32+
33+
import main
34+
35+
main.app.testing = True
36+
return main.app.test_client()
37+
38+
39+
def test_receive_call(app):
40+
r = app.post('/call/receive')
41+
assert 'Hello from Twilio!' in r.data.decode('utf-8')
42+
43+
44+
def test_send_sms(app, monkeypatch):
45+
httpmock = HttpMockSequenceWithCredentials([
46+
({'status': '200'}, json.dumps({
47+
'sid': 'sid123'
48+
}))
49+
])
50+
51+
def mock_http_ctor(*args, **kwargs):
52+
return httpmock
53+
54+
monkeypatch.setattr(httplib2, 'Http', mock_http_ctor)
55+
56+
r = app.get('/sms/send')
57+
assert r.status_code == 400
58+
59+
r = app.get('/sms/send?to=5558675309')
60+
assert r.status_code == 200
61+
62+
63+
def test_receive_sms(app):
64+
r = app.post('/sms/receive', data={
65+
'From': '5558675309', 'Body': 'Jenny, I got your number.'})
66+
assert r.status_code == 200
67+
assert 'Jenny, I got your number' in r.data.decode('utf-8')

0 commit comments

Comments
 (0)