class TaskQueueCapabilityTokenTest(unittest.TestCase): def setUp(self): self.account_sid = "AC123" self.auth_token = "foobar" self.workspace_sid = "WS456" self.taskqueue_sid = "WQ789" self.capability = TaskQueueCapabilityToken(self.account_sid, self.auth_token, self.workspace_sid, self.taskqueue_sid) def test_generate_token(self): token = self.capability.to_jwt() self.assertNotEqual(None, token) decoded = TaskQueueCapabilityToken.from_jwt(token, self.auth_token) self.assertNotEqual(None, decoded) self.assertEqual(decoded.payload["iss"], self.account_sid) self.assertEqual(decoded.payload["account_sid"], self.account_sid) self.assertEqual(decoded.payload["workspace_sid"], self.workspace_sid) self.assertEqual(decoded.payload["taskqueue_sid"], self.taskqueue_sid) self.assertEqual(decoded.payload["channel"], self.taskqueue_sid) self.assertEqual(decoded.payload["version"], "v1") self.assertEqual(decoded.payload["friendly_name"], self.taskqueue_sid) def test_generate_token_with_default_ttl(self): token = self.capability.to_jwt() self.assertNotEqual(None, token) decoded = TaskQueueCapabilityToken.from_jwt(token, self.auth_token) self.assertNotEqual(None, decoded) self.assertAlmostEqual(int(time.time()) + 3600, decoded.valid_until, delta=5) def test_generate_token_with_custom_ttl(self): ttl = 10000 token = self.capability.to_jwt(ttl=ttl) self.assertNotEqual(None, token) decoded = TaskQueueCapabilityToken.from_jwt(token, self.auth_token) self.assertNotEqual(None, decoded) self.assertAlmostEqual(int(time.time()) + 10000, decoded.valid_until, delta=5) def test_default(self): token = self.capability.to_jwt() self.assertNotEqual(None, token) decoded = TaskQueueCapabilityToken.from_jwt(token, self.auth_token) self.assertNotEqual(None, decoded) policies = decoded.payload['policies'] self.assertEqual(len(policies), 3) # websocket GET get_policy = policies[0] self.assertEqual( "https://event-bridge.twilio.com/v1/wschannels/AC123/WQ789", get_policy['url']) self.assertEqual("GET", get_policy['method']) self.assertTrue(get_policy['allow']) self.assertEqual({}, get_policy['query_filter']) self.assertEqual({}, get_policy['post_filter']) # websocket POST post_policy = policies[1] self.assertEqual( "https://event-bridge.twilio.com/v1/wschannels/AC123/WQ789", post_policy['url']) self.assertEqual("POST", post_policy['method']) self.assertTrue(post_policy['allow']) self.assertEqual({}, post_policy['query_filter']) self.assertEqual({}, post_policy['post_filter']) # fetch GET fetch_policy = policies[2] self.assertEqual( "https://taskrouter.twilio.com/v1/Workspaces/WS456/TaskQueues/WQ789", fetch_policy['url']) self.assertEqual("GET", fetch_policy['method']) self.assertTrue(fetch_policy['allow']) self.assertEqual({}, fetch_policy['query_filter']) self.assertEqual({}, fetch_policy['post_filter']) def test_allow_fetch_subresources(self): self.capability.allow_fetch_subresources() token = self.capability.to_jwt() self.assertNotEqual(None, token) decoded = TaskQueueCapabilityToken.from_jwt(token, self.auth_token) self.assertNotEqual(None, decoded) policies = decoded.payload['policies'] self.assertEqual(len(policies), 4) # confirm the additional policy generated with allow_fetch_subresources() policy = policies[3] self.assertEqual( policy['url'], "https://taskrouter.twilio.com/v1/Workspaces/WS456/TaskQueues/WQ789/**" ) self.assertEqual(policy['method'], "GET") self.assertTrue(policy['allow']) self.assertEqual({}, policy['query_filter']) self.assertEqual({}, policy['post_filter']) def test_allow_updates_subresources(self): self.capability.allow_update_subresources() token = self.capability.to_jwt() self.assertNotEqual(None, token) decoded = TaskQueueCapabilityToken.from_jwt(token, self.auth_token) self.assertNotEqual(None, decoded) policies = decoded.payload['policies'] self.assertEqual(len(policies), 4) # confirm the additional policy generated with allow_updates_subresources() policy = policies[3] self.assertEqual( policy['url'], "https://taskrouter.twilio.com/v1/Workspaces/WS456/TaskQueues/WQ789/**" ) self.assertEqual(policy['method'], "POST") self.assertTrue(policy['allow']) self.assertEqual({}, policy['query_filter']) self.assertEqual({}, policy['post_filter']) def test_pass_policy_in_constructor(self): self.capability = TaskQueueCapabilityToken( self.account_sid, self.auth_token, self.workspace_sid, self.taskqueue_sid, allow_update_subresources=True) token = self.capability.to_jwt() self.assertNotEqual(None, token) decoded = TaskQueueCapabilityToken.from_jwt(token, self.auth_token) self.assertNotEqual(None, decoded) policies = decoded.payload['policies'] self.assertEqual(len(policies), 4) # confirm the additional policy generated with allow_updates_subresources() policy = policies[3] self.assertEqual( policy['url'], "https://taskrouter.twilio.com/v1/Workspaces/WS456/TaskQueues/WQ789/**" ) self.assertEqual(policy['method'], "POST") self.assertTrue(policy['allow']) self.assertEqual({}, policy['query_filter']) self.assertEqual({}, policy['post_filter'])
# Download the Python helper library from twilio.com/docs/python/install from twilio.jwt.taskrouter.capabilities import TaskQueueCapabilityToken # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" workspace_sid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" task_queue_sid = "WQXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" capability = TaskQueueCapabilityToken(account_sid=account_sid, auth_token=auth_token, workspace_sid=workspace_sid, task_queue_sid=task_queue_sid) capability.allow_fetch_subresources() capability.allow_update_subresources() token = capability.to_jwt() # By default, tokens are good for one hour. # Override this default timeout by specifiying a new value (in seconds). # For example, to generate a token good for 8 hours: # 60 * 60 * 8 = 28800 token = capability.to_jwt(ttl=28800) print(token)
# Download the Python helper library from twilio.com/docs/python/install from twilio.jwt.taskrouter.capabilities import TaskQueueCapabilityToken # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" workspace_sid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" task_queue_sid = "WQXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" capability = TaskQueueCapabilityToken( account_sid=account_sid, auth_token=auth_token, workspace_sid=workspace_sid, task_queue_sid=task_queue_sid ) capability.allow_fetch_subresources() capability.allow_update_subresources() token = capability.to_jwt() # By default, tokens are good for one hour. # Override this default timeout by specifiying a new value (in seconds). # For example, to generate a token good for 8 hours: # 60 * 60 * 8 = 28800 token = capability.to_jwt(ttl=28800) print(token)