diff --git a/tests/unit/jwt/test_access_token.py b/tests/unit/jwt/test_access_token.py index 0808333104..f7695c0c28 100644 --- a/tests/unit/jwt/test_access_token.py +++ b/tests/unit/jwt/test_access_token.py @@ -10,6 +10,7 @@ SyncGrant, VoiceGrant, VideoGrant, + ConversationsGrant, TaskRouterGrant ) @@ -80,9 +81,22 @@ def test_identity(self): 'identity': 'test@twilio.com' }, decoded_token.payload['grants']) + def test_conversations_grant(self): + scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret') + scat.add_grant(ConversationsGrant(configuration_profile_sid='CP123')) + + token = scat.to_jwt() + assert_is_not_none(token) + decoded_token = AccessToken.from_jwt(token, 'secret') + self._validate_claims(decoded_token.payload) + assert_equal(1, len(decoded_token.payload['grants'])) + assert_equal({ + 'configuration_profile_sid': 'CP123' + }, decoded_token.payload['grants']['rtc']) + def test_video_grant(self): scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret') - scat.add_grant(VideoGrant(room='CP123')) + scat.add_grant(VideoGrant(room='RM123')) token = scat.to_jwt() assert_is_not_none(token) @@ -90,7 +104,7 @@ def test_video_grant(self): self._validate_claims(decoded_token.payload) assert_equal(1, len(decoded_token.payload['grants'])) assert_equal({ - 'room': 'CP123' + 'room': 'RM123' }, decoded_token.payload['grants']['video']) def test_ip_messaging_grant(self): diff --git a/twilio/jwt/access_token/grants.py b/twilio/jwt/access_token/grants.py index d334e22910..7d895e8f9e 100644 --- a/twilio/jwt/access_token/grants.py +++ b/twilio/jwt/access_token/grants.py @@ -1,4 +1,23 @@ from twilio.jwt.access_token import AccessTokenGrant +import warnings +import functools + + +def deprecated(func): + '''This is a decorator which can be used to mark functions + as deprecated. It will result in a warning being emitted + when the function is used.''' + + @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 + ) + return func(*args, **kwargs) + return new_func class IpMessagingGrant(AccessTokenGrant): @@ -86,6 +105,24 @@ def to_payload(self): return grant +class ConversationsGrant(AccessTokenGrant): + """Grant to access Twilio Conversations""" + @deprecated + def __init__(self, configuration_profile_sid=None): + self.configuration_profile_sid = configuration_profile_sid + + @property + def key(self): + return "rtc" + + def to_payload(self): + grant = {} + if self.configuration_profile_sid: + grant['configuration_profile_sid'] = self.configuration_profile_sid + + return grant + + class VideoGrant(AccessTokenGrant): """Grant to access Twilio Video""" def __init__(self, room=None):