1
1
import time
2
2
import unittest
3
-
4
3
from datetime import datetime
5
- from nose .tools import assert_equal
6
4
7
5
from twilio .jwt .access_token import AccessToken
8
6
from twilio .jwt .access_token .grants import (
@@ -35,31 +33,31 @@ def assert_greater_equal(obj1, obj2):
35
33
36
34
class AccessTokenTest (unittest .TestCase ):
37
35
def _validate_claims (self , payload ):
38
- assert_equal ( SIGNING_KEY_SID , payload ['iss' ])
39
- assert_equal ( ACCOUNT_SID , payload ['sub' ])
36
+ assert SIGNING_KEY_SID == payload ['iss' ]
37
+ assert ACCOUNT_SID == payload ['sub' ]
40
38
41
- assert_is_not_none ( payload ['exp' ])
42
- assert_is_not_none ( payload ['jti' ])
43
- assert_is_not_none ( payload ['grants' ])
39
+ assert payload ['exp' ] is not None
40
+ assert payload ['jti' ] is not None
41
+ assert payload ['grants' ] is not None
44
42
45
- assert_greater_equal ( payload ['exp' ], int (time .time () ))
43
+ assert payload ['exp' ] >= int (time .time ())
46
44
47
- assert_in ( payload ['iss' ], payload ['jti' ])
45
+ assert payload ['iss' ] in payload ['jti' ]
48
46
49
47
def test_empty_grants (self ):
50
48
scat = AccessToken (ACCOUNT_SID , SIGNING_KEY_SID , 'secret' )
51
49
token = scat .to_jwt ()
52
50
53
- assert_is_not_none ( token )
51
+ assert token is not None
54
52
decoded_token = AccessToken .from_jwt (token , 'secret' )
55
53
self ._validate_claims (decoded_token .payload )
56
- assert_equal ({}, decoded_token .payload ['grants' ])
54
+ assert {} == decoded_token .payload ['grants' ]
57
55
58
56
def test_region (self ):
59
57
scat = AccessToken (ACCOUNT_SID , SIGNING_KEY_SID , 'secret' , region = 'foo' )
60
58
token = scat .to_jwt ()
61
59
decoded_token = AccessToken .from_jwt (token , 'secret' )
62
- assert_equal ( decoded_token .headers ['twr' ], 'foo' )
60
+ assert decoded_token .headers ['twr' ] == 'foo'
63
61
64
62
def test_empty_region (self ):
65
63
scat = AccessToken (ACCOUNT_SID , SIGNING_KEY_SID , 'secret' )
@@ -72,111 +70,111 @@ def test_nbf(self):
72
70
scat = AccessToken (ACCOUNT_SID , SIGNING_KEY_SID , 'secret' , nbf = now )
73
71
token = scat .to_jwt ()
74
72
75
- assert_is_not_none ( token )
73
+ assert token is not None
76
74
decoded_token = AccessToken .from_jwt (token , 'secret' )
77
75
self ._validate_claims (decoded_token .payload )
78
- assert_equal ( now , decoded_token .nbf )
76
+ assert now == decoded_token .nbf
79
77
80
78
def test_headers (self ):
81
79
scat = AccessToken (ACCOUNT_SID , SIGNING_KEY_SID , 'secret' )
82
80
token = scat .to_jwt ()
83
- assert_is_not_none ( token )
81
+ assert token is not None
84
82
decoded_token = AccessToken .from_jwt (token , 'secret' )
85
83
self .assertEqual (decoded_token .headers ['cty' ], 'twilio-fpa;v=1' )
86
84
87
85
def test_identity (self ):
88
86
scat = AccessToken (
ACCOUNT_SID ,
SIGNING_KEY_SID ,
'secret' ,
identity = '[email protected] ' )
89
87
token = scat .to_jwt ()
90
88
91
- assert_is_not_none ( token )
89
+ assert token is not None
92
90
decoded_token = AccessToken .from_jwt (token , 'secret' )
93
91
self ._validate_claims (decoded_token .payload )
94
- assert_equal ( {
95
-
96
- }, decoded_token .payload ['grants' ])
92
+ assert {
93
+
94
+ } == decoded_token .payload ['grants' ]
97
95
98
96
def test_conversations_grant (self ):
99
97
scat = AccessToken (ACCOUNT_SID , SIGNING_KEY_SID , 'secret' )
100
98
scat .add_grant (ConversationsGrant (configuration_profile_sid = 'CP123' ))
101
99
102
100
token = scat .to_jwt ()
103
- assert_is_not_none ( token )
101
+ assert token is not None
104
102
decoded_token = AccessToken .from_jwt (token , 'secret' )
105
103
self ._validate_claims (decoded_token .payload )
106
- assert_equal ( 1 , len (decoded_token .payload ['grants' ]) )
107
- assert_equal ( {
108
- 'configuration_profile_sid' : 'CP123'
109
- }, decoded_token .payload ['grants' ]['rtc' ])
104
+ assert 1 == len (decoded_token .payload ['grants' ])
105
+ assert {
106
+ 'configuration_profile_sid' : 'CP123'
107
+ } == decoded_token .payload ['grants' ]['rtc' ]
110
108
111
109
def test_video_grant (self ):
112
110
scat = AccessToken (ACCOUNT_SID , SIGNING_KEY_SID , 'secret' )
113
111
scat .add_grant (VideoGrant (room = 'RM123' ))
114
112
115
113
token = scat .to_jwt ()
116
- assert_is_not_none ( token )
114
+ assert token is not None
117
115
decoded_token = AccessToken .from_jwt (token , 'secret' )
118
116
self ._validate_claims (decoded_token .payload )
119
- assert_equal ( 1 , len (decoded_token .payload ['grants' ]) )
120
- assert_equal ( {
121
- 'room' : 'RM123'
122
- }, decoded_token .payload ['grants' ]['video' ])
117
+ assert 1 == len (decoded_token .payload ['grants' ])
118
+ assert {
119
+ 'room' : 'RM123'
120
+ } == decoded_token .payload ['grants' ]['video' ]
123
121
124
122
def test_ip_messaging_grant (self ):
125
123
scat = AccessToken (ACCOUNT_SID , SIGNING_KEY_SID , 'secret' )
126
124
scat .add_grant (IpMessagingGrant (service_sid = 'IS123' , push_credential_sid = 'CR123' ))
127
125
128
126
token = scat .to_jwt ()
129
- assert_is_not_none ( token )
127
+ assert token is not None
130
128
decoded_token = AccessToken .from_jwt (token , 'secret' )
131
129
self ._validate_claims (decoded_token .payload )
132
- assert_equal ( 1 , len (decoded_token .payload ['grants' ]) )
133
- assert_equal ( {
134
- 'service_sid' : 'IS123' ,
135
- 'push_credential_sid' : 'CR123'
136
- }, decoded_token .payload ['grants' ]['ip_messaging' ])
130
+ assert 1 == len (decoded_token .payload ['grants' ])
131
+ assert {
132
+ 'service_sid' : 'IS123' ,
133
+ 'push_credential_sid' : 'CR123'
134
+ } == decoded_token .payload ['grants' ]['ip_messaging' ]
137
135
138
136
def test_chat_grant (self ):
139
137
scat = AccessToken (ACCOUNT_SID , SIGNING_KEY_SID , 'secret' )
140
138
scat .add_grant (ChatGrant (service_sid = 'IS123' , push_credential_sid = 'CR123' ))
141
139
142
140
token = scat .to_jwt ()
143
- assert_is_not_none ( token )
141
+ assert token is not None
144
142
decoded_token = AccessToken .from_jwt (token , 'secret' )
145
143
self ._validate_claims (decoded_token .payload )
146
- assert_equal ( 1 , len (decoded_token .payload ['grants' ]) )
147
- assert_equal ( {
148
- 'service_sid' : 'IS123' ,
149
- 'push_credential_sid' : 'CR123'
150
- }, decoded_token .payload ['grants' ]['chat' ])
144
+ assert 1 == len (decoded_token .payload ['grants' ])
145
+ assert {
146
+ 'service_sid' : 'IS123' ,
147
+ 'push_credential_sid' : 'CR123'
148
+ } == decoded_token .payload ['grants' ]['chat' ]
151
149
152
150
def test_sync_grant (self ):
153
151
scat = AccessToken (ACCOUNT_SID , SIGNING_KEY_SID , 'secret' )
154
152
scat .identity = "bender"
155
153
scat .add_grant (SyncGrant (service_sid = 'IS123' , endpoint_id = 'blahblahendpoint' ))
156
154
157
155
token = scat .to_jwt ()
158
- assert_is_not_none ( token )
156
+ assert token is not None
159
157
decoded_token = AccessToken .from_jwt (token , 'secret' )
160
158
self ._validate_claims (decoded_token .payload )
161
- assert_equal ( 2 , len (decoded_token .payload ['grants' ]) )
162
- assert_equal ( "bender" , decoded_token .payload ['grants' ]['identity' ])
163
- assert_equal ( {
164
- 'service_sid' : 'IS123' ,
165
- 'endpoint_id' : 'blahblahendpoint'
166
- }, decoded_token .payload ['grants' ]['data_sync' ])
159
+ assert 2 == len (decoded_token .payload ['grants' ])
160
+ assert "bender" == decoded_token .payload ['grants' ]['identity' ]
161
+ assert {
162
+ 'service_sid' : 'IS123' ,
163
+ 'endpoint_id' : 'blahblahendpoint'
164
+ } == decoded_token .payload ['grants' ]['data_sync' ]
167
165
168
166
def test_grants (self ):
169
167
scat = AccessToken (ACCOUNT_SID , SIGNING_KEY_SID , 'secret' )
170
168
scat .add_grant (VideoGrant ())
171
169
scat .add_grant (IpMessagingGrant ())
172
170
173
171
token = scat .to_jwt ()
174
- assert_is_not_none ( token )
172
+ assert token is not None
175
173
decoded_token = AccessToken .from_jwt (token , 'secret' )
176
174
self ._validate_claims (decoded_token .payload )
177
- assert_equal ( 2 , len (decoded_token .payload ['grants' ]) )
178
- assert_equal ({}, decoded_token .payload ['grants' ]['video' ])
179
- assert_equal ({}, decoded_token .payload ['grants' ]['ip_messaging' ])
175
+ assert 2 == len (decoded_token .payload ['grants' ])
176
+ assert {} == decoded_token .payload ['grants' ]['video' ]
177
+ assert {} == decoded_token .payload ['grants' ]['ip_messaging' ]
180
178
181
179
def test_programmable_voice_grant (self ):
182
180
grant = VoiceGrant (
@@ -190,18 +188,18 @@ def test_programmable_voice_grant(self):
190
188
scat .add_grant (grant )
191
189
192
190
token = scat .to_jwt ()
193
- assert_is_not_none ( token )
191
+ assert token is not None
194
192
decoded_token = AccessToken .from_jwt (token , 'secret' )
195
193
self ._validate_claims (decoded_token .payload )
196
- assert_equal ( 1 , len (decoded_token .payload ['grants' ]) )
197
- assert_equal ( {
198
- 'outgoing' : {
199
- 'application_sid' : 'AP123' ,
200
- 'params' : {
201
- 'foo' : 'bar'
202
- }
203
- }
204
- }, decoded_token .payload ['grants' ]['voice' ])
194
+ assert 1 == len (decoded_token .payload ['grants' ])
195
+ assert {
196
+ 'outgoing' : {
197
+ 'application_sid' : 'AP123' ,
198
+ 'params' : {
199
+ 'foo' : 'bar'
200
+ }
201
+ }
202
+ } == decoded_token .payload ['grants' ]['voice' ]
205
203
206
204
def test_programmable_voice_grant_incoming (self ):
207
205
grant = VoiceGrant (
@@ -212,15 +210,15 @@ def test_programmable_voice_grant_incoming(self):
212
210
scat .add_grant (grant )
213
211
214
212
token = scat .to_jwt ()
215
- assert_is_not_none ( token )
213
+ assert token is not None
216
214
decoded_token = AccessToken .from_jwt (token , 'secret' )
217
215
self ._validate_claims (decoded_token .payload )
218
- assert_equal ( 1 , len (decoded_token .payload ['grants' ]) )
219
- assert_equal ( {
220
- 'incoming' : {
221
- 'allow' : True
222
- }
223
- }, decoded_token .payload ['grants' ]['voice' ])
216
+ assert 1 == len (decoded_token .payload ['grants' ])
217
+ assert {
218
+ 'incoming' : {
219
+ 'allow' : True
220
+ }
221
+ } == decoded_token .payload ['grants' ]['voice' ]
224
222
225
223
def test_task_router_grant (self ):
226
224
grant = TaskRouterGrant (
@@ -233,15 +231,15 @@ def test_task_router_grant(self):
233
231
scat .add_grant (grant )
234
232
235
233
token = scat .to_jwt ()
236
- assert_is_not_none ( token )
234
+ assert token is not None
237
235
decoded_token = AccessToken .from_jwt (token , 'secret' )
238
236
self ._validate_claims (decoded_token .payload )
239
- assert_equal ( 1 , len (decoded_token .payload ['grants' ]) )
240
- assert_equal ( {
241
- 'workspace_sid' : 'WS123' ,
242
- 'worker_sid' : 'WK123' ,
243
- 'role' : 'worker'
244
- }, decoded_token .payload ['grants' ]['task_router' ])
237
+ assert 1 == len (decoded_token .payload ['grants' ])
238
+ assert {
239
+ 'workspace_sid' : 'WS123' ,
240
+ 'worker_sid' : 'WK123' ,
241
+ 'role' : 'worker'
242
+ } == decoded_token .payload ['grants' ]['task_router' ]
245
243
246
244
def test_playback_grant (self ):
247
245
"""Test that PlaybackGrants are created and decoded correctly."""
@@ -253,11 +251,11 @@ def test_playback_grant(self):
253
251
scat = AccessToken (ACCOUNT_SID , SIGNING_KEY_SID , 'secret' )
254
252
scat .add_grant (PlaybackGrant (grant = grant ))
255
253
token = scat .to_jwt ()
256
- assert_is_not_none ( token )
254
+ assert token is not None
257
255
decoded_token = AccessToken .from_jwt (token , 'secret' )
258
256
self ._validate_claims (decoded_token .payload )
259
- assert_equal ( 1 , len (decoded_token .payload ['grants' ]) )
260
- assert_equal ( grant , decoded_token .payload ['grants' ]['player' ])
257
+ assert 1 == len (decoded_token .payload ['grants' ])
258
+ assert grant == decoded_token .payload ['grants' ]['player' ]
261
259
262
260
def test_pass_grants_in_constructor (self ):
263
261
grants = [
@@ -267,13 +265,13 @@ def test_pass_grants_in_constructor(self):
267
265
scat = AccessToken (ACCOUNT_SID , SIGNING_KEY_SID , 'secret' , grants = grants )
268
266
269
267
token = scat .to_jwt ()
270
- assert_is_not_none ( token )
268
+ assert token is not None
271
269
272
270
decoded_token = AccessToken .from_jwt (token , 'secret' )
273
271
self ._validate_claims (decoded_token .payload )
274
- assert_equal ( 2 , len (decoded_token .payload ['grants' ]) )
275
- assert_equal ({}, decoded_token .payload ['grants' ]['video' ])
276
- assert_equal ({}, decoded_token .payload ['grants' ]['ip_messaging' ])
272
+ assert 2 == len (decoded_token .payload ['grants' ])
273
+ assert {} == decoded_token .payload ['grants' ]['video' ]
274
+ assert {} == decoded_token .payload ['grants' ]['ip_messaging' ]
277
275
278
276
def test_constructor_validates_grants (self ):
279
277
grants = [VideoGrant , 'GrantMeAccessToEverything' ]
0 commit comments