8000 Voice response devx5021 and Python testing, compatibility by ilanbiala · Pull Request #346 · twilio/twilio-python · GitHub
[go: up one dir, main page]

Skip to content

Voice response devx5021 and Python testing, compatibility #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
language: python
python:
- "2.6"
- "2.7"
- "3.3"
- "3.4"
- "3.5"
- "3.6"
install:
- pip install virtualenv --upgrade
- make install
- make test-install
script:
script:
- make test
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: clean install analysis test test-install develop docs docs-install

venv:
virtualenv venv
virtualenv --python=python venv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the benefit of doing --python=python? Isnt that the default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding --python=python uses the standard alias Travis-CI sets up in each environment, which seemed to be the simplest way to use the intended test version. I can look around for another way if you think there is one.


install: venv
. venv/bin/activate; pip install .
Expand Down
3 changes: 0 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
# documentation: http://pypi.python.org/pypi/setuptools
REQUIRES = ["requests >= 2.0.0", "six", "pytz", "PyJWT >= 1.4.2"]

if sys.version_info < (2, 6):
REQUIRES.append('simplejson')
if sys.version_info < (3, 0):
REQUIRES.extend(["cryptography >= 1.3.4", "idna >= 2.0.0", "pyOpenSSL >= 0.14"])
if sys.version_info >= (3, 0):
Expand Down Expand Up @@ -46,7 +44,6 @@
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
Expand Down
51 changes: 51 additions & 0 deletions tests/unit/http/test_http_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-

import six

import unittest

import mock
from mock import patch, Mock
from requests import Request
from requests import Session

from twilio.http.http_client import TwilioHttpClient


class TestHttpClientRequest(unittest.TestCase):

def setUp(self):
self.session_patcher = patch('twilio.http.http_client.Session')

self.session_mock = Mock(wraps=Session())
self.request_mock = Mock()

self.session_mock.prepare_request.return_value = self.request_mock
self.session_mock.send.return_value = Mock(status_code=200, content=six.u('testing-unicodeΩ≈ç√'))
self.request_mock.headers = {}

session_constructor_mock = self.session_patcher.start()
session_constructor_mock.return_value = self.session_mock

self.client = TwilioHttpClient()

def tearDown(self):
self.session_patcher.stop()

def test_request_sets_host_header_if_missing(self):
self.request_mock.url = 'https://api.twilio.com/'
self.request_mock.headers = {'Host': 'other.twilio.com'}

self.client.request('doesnt matter', 'doesnt matter')

self.assertEqual('other.twilio.com', self.request_mock.headers['Host'])

def test_request_with_unicode_response(self):
self.request_mock.url = 'https://api.twilio.com/'
self.request_mock.headers = {'Host': 'other.twilio.com'}

response = self.client.request('doesnt matter', 'doesnt matter')

self.assertEqual('other.twilio.com', self.request_mock.headers['Host'])
self.assertEqual(200, response.status_code)
self.assertEqual(six.u('testing-unicodeΩ≈ç√'), response.content)
17 changes: 16 additions & 1 deletion tests/unit/http/test_validation_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# -*- coding: utf-8 -*-

import six

import unittest

import mock
Expand Down Expand Up @@ -76,7 +80,7 @@ def setUp(self):
self.request_mock = Mock()

self.session_mock.prepare_request.return_value = self.request_mock
self.session_mock.send.return_value = Mock(status_code=200, content='test')
self.session_mock.send.return_value = Mock(status_code=200, content=six.u('testΩ'))
self.validation_token.return_value.to_jwt.return_value = 'test-token'
self.request_mock.headers = {}

Expand Down Expand Up @@ -105,3 +109,14 @@ def test_request_sets_host_header_if_missing(self):

self.assertEqual('other.twilio.com', self.request_mock.headers['Host'])
self.assertEqual('test-token', self.request_mock.headers['Twilio-Client-Validation'])

def test_request_with_unicode_response(self):
self.request_mock.url = 'https://api.twilio.com/'
self.request_mock.headers = {'Host': 'other.twilio.com'}

response = self.client.request('doesnt matter', 'doesnt matter')

self.assertEqual('other.twilio.com', self.request_mock.headers['Host'])
self.assertEqual('test-token', self.request_mock.headers['Twilio-Client-Validation'])
self.assertEqual(200, response.status_code)
self.assertEqual(six.u('testΩ'), response.content)
19 changes: 19 additions & 0 deletions tests/unit/twiml/test_voice_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,25 @@ def test_conference(self):
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Conference beep="false" endConferenceOnExit="true" startConferenceOnEnter="true" waitUrl="">TestConferenceAttributes</Conference></Dial></Response>'
)

def test_muted_conference(self):
d = Dial()
d.conference(
'TestConferenceMutedAttribute',
beep=False,
muted=True,
wait_url='',
start_conference_on_enter=True,
end_conference_on_exit=True
)

r = VoiceResponse()
r.append(d)

assert_equal(
self.strip(r),
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Conference beep="false" endConferenceOnExit="true" muted="true" startConferenceOnEnter="true" waitUrl="">TestConferenceMutedAttribute</Conference></Dial></Response>'
)


class TestQueue(TwilioTest):

Expand Down
3 changes: 1 addition & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
[tox]
envlist = py26, py27, py33, py34, py35, py36, pypy
envlist = py27, py33, py34, py35, py36, pypy

[testenv]
deps= -r{toxinidir}/tests/requirements.txt
commands=
nosetests \
[]

2 changes: 1 addition & 1 deletion twilio/http/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None,
timeout=timeout,
)

return Response(int(response.status_code), response.content.decode('utf-8'))
return Response(int(response.status_code), response.content)
5 changes: 2 additions & 3 deletions twilio/http/validation_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from urlparse import urlparse

from collections import namedtuple

from requests import Request, Session

from twilio.compat import urlparse
from twilio.http import HttpClient, get_cert_file
from twilio.http.response import Response
from twilio.jwt.validation import ClientValidationJwt
Expand Down Expand Up @@ -70,7 +69,7 @@ def request(self, method, url, params=None, data=None, headers=None, auth=None,
timeout=timeout,
)

return Response(int(response.status_code), response.content.decode('utf-8'))
return Response(int(response.status_code), response.content)

def _build_validation_payload(self, request):
"""
Expand Down
10 changes: 4 additions & 6 deletions twilio/jwt/access_token/grants.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ def deprecated(func):

@functools.wraps(func)
def new_func(*args, **kwargs):
warnings.warn_explicit(
"Call to deprecated function {}.".format(func.__name__),
category=DeprecationWarning,
filename=func.func_code.co_filename,
lineno=func.func_code.co_firstlineno + 1
)
warnings.simplefilter('always', DeprecationWarning)
warnings.warn("Call to deprecated function {}.".format(func.__name__), category=DeprecationWarning, stacklevel=2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did this need to change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func.func_code is no longer an attribute in Python 3, and the decorator is only used on grants.py:108.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

warnings.simplefilter('default', DeprecationWarning)
return func(*args, **kwargs)

return new_func


Expand Down
1 change: 1 addition & 0 deletions twilio/twiml/voice_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ def conference(self,
"""
return self.append(Conference(
name,
muted=muted,
start_conference_on_enter=start_conference_on_enter,
end_conference_on_exit=end_conference_on_exit,
max_participants=max_participants,
Expand Down
0