def test_generate_token_with_default_ttl(self):
        token = self.capability.to_jwt()
        self.assertNotEqual(None, token)

        decoded = WorkspaceCapabilityToken.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(self):
        token = self.capability.to_jwt()
        self.assertNotEqual(None, token)

        decoded = WorkspaceCapabilityToken.from_jwt(token, self.auth_token)
        self.assertNotEqual(None, decoded)

        self.check_decoded(decoded.payload, self.account_sid, self.workspace_sid, self.workspace_sid)
    def test_allow_updates_subresources(self):
        self.capability.allow_update_subresources()

        token = self.capability.to_jwt()
        self.assertNotEqual(None, token)

        decoded = WorkspaceCapabilityToken.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_update_subresources()
        policy = policies[3]
        self.check_policy('POST', "https://taskrouter.twilio_code.com/v1/Workspaces/WS456/**", policy)
    def test_default(self):
        token = self.capability.to_jwt()
        self.assertNotEqual(None, token)

        decoded = WorkspaceCapabilityToken.from_jwt(token, self.auth_token)
        self.assertNotEqual(None, decoded)

        policies = decoded.payload['policies']
        self.assertEqual(len(policies), 3)

        for method, url, policy in [
            ('GET', "https://event-bridge.twilio_code.com/v1/wschannels/AC123/WS456", policies[0]),
            ('POST', "https://event-bridge.twilio_code.com/v1/wschannels/AC123/WS456", policies[1]),
            ('GET', "https://taskrouter.twilio_code.com/v1/Workspaces/WS456", policies[2])
        ]:
            yield self.check_policy, method, url, policy