diff --git a/tests/requirements.txt b/tests/requirements.txt index 9262910394..f1df3797ed 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -3,6 +3,6 @@ mock==0.8.0 nose coverage nosexcover -flake8 +flake8==3.0.3 mccabe wheel>=0.22.0 diff --git a/tests/test_access_token.py b/tests/test_access_token.py index 589db4509a..6f1f029ef5 100644 --- a/tests/test_access_token.py +++ b/tests/test_access_token.py @@ -4,7 +4,7 @@ from datetime import datetime from nose.tools import assert_equal from twilio.jwt import decode -from twilio.access_token import AccessToken, ConversationsGrant, IpMessagingGrant +from twilio.access_token import AccessToken, ConversationsGrant, IpMessagingGrant, VoiceGrant ACCOUNT_SID = 'AC123' SIGNING_KEY_SID = 'SK123' @@ -105,3 +105,28 @@ def test_grants(self): assert_equal(2, len(payload['grants'])) assert_equal({}, payload['grants']['rtc']) assert_equal({}, payload['grants']['ip_messaging']) + + def test_programmable_voice_grant(self): + grant = VoiceGrant( + outgoing_application_sid='AP123', + outgoing_application_params={ + 'foo': 'bar' + } + ) + + scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret') + scat.add_grant(grant) + + token = str(scat) + assert_is_not_none(token) + payload = decode(token, 'secret') + self._validate_claims(payload) + assert_equal(1, len(payload['grants'])) + assert_equal({ + 'outgoing': { + 'application_sid': 'AP123', + 'params': { + 'foo': 'bar' + } + } + }, payload['grants']['voice']) diff --git a/twilio/access_token.py b/twilio/access_token.py index f7104f1742..3fa1ddd403 100644 --- a/twilio/access_token.py +++ b/twilio/access_token.py @@ -46,6 +46,45 @@ def to_payload(self): return grant +class VoiceGrant(object): + """ Grant to access Twilio Programmable Voice""" + def __init__(self, + outgoing_application_sid=None, + outgoing_application_params=None, + push_credential_sid=None, + endpoint_id=None): + self.outgoing_application_sid = outgoing_application_sid + """ :type : str """ + self.outgoing_application_params = outgoing_application_params + """ :type : dict """ + self.push_credential_sid = push_credential_sid + """ :type : str """ + self.endpoint_id = endpoint_id + """ :type : str """ + + @property + def key(self): + return "voice" + + def to_payload(self): + grant = {} + if self.outgoing_application_sid: + grant['outgoing'] = {} + grant['outgoing']['application_sid'] = \ + self.outgoing_application_sid + + if self.outgoing_application_params: + grant['outgoing']['params'] = self.outgoing_application_params + + if self.push_credential_sid: + grant['push_credential_sid'] = self.push_credential_sid + + if self.endpoint_id: + grant['endpoint_id'] = self.endpoint_id + + return grant + + class AccessToken(object): """ Access Token used to access Twilio Resources """ def __init__(self, account_sid, signing_key_sid, secret,