def setUp(self):
     self.request = Request(
         'GET',
         'https://api.twilio.com/2010-04-01/Accounts/AC123/Messages',
         auth=('Username', 'Password'),
     )
     self.request = self.request.prepare()
     self.client = ValidationClient('AC123', 'SK123', 'CR123', 'private_key')
def example():
    """
    Example of using the ValidationClient for signed requests to Twilio.
    This is only available to enterprise customers.

    This will walkthrough creating an API Key, generating an RSA keypair, setting up a
    ValidationClient with these values and making requests with the client.
    """
    client = Client(ACCOUNT_SID, AUTH_TOKEN)

    # Using Client Validation requires using API Keys for auth
    # First create an API key using the standard account sid, auth token client
    print('Creating new api key...')
    api_key = client.new_keys.create(friendly_name='ClientValidationApiKey')

    # Generate a new RSA Keypair
    print('Generating RSA key pair...')
    key_pair = rsa.generate_private_key(public_exponent=65537,
                                        key_size=2048,
                                        backend=default_backend())
    public_key = key_pair.public_key().public_bytes(
        Encoding.PEM, PublicFormat.SubjectPublicKeyInfo)
    private_key = key_pair.private_bytes(Encoding.PEM, PrivateFormat.PKCS8,
                                         NoEncryption())

    # Register the public key with Twilio
    print('Registering public key with Twilio...')
    credential = client.accounts.credentials.public_key.create(
        public_key, friendly_name='ClientValidationPublicKey')

    # Create a new ValidationClient with the keys we created
    validation_client = ValidationClient(ACCOUNT_SID, api_key.sid,
                                         credential.sid, private_key)

    # Create a REST Client using the validation_client
    client = Client(api_key.sid,
                    api_key.secret,
                    ACCOUNT_SID,
                    http_client=validation_client)

    # Use the library as usual
    print('Trying out client validation...')
    messages = client.messages.list(limit=10)
    for m in messages:
        print('Message {}'.format(m.sid))

    print('Client validation works!')
    def setUp(self):
        self.session_patcher = patch('twilio.http.validation_client.Session')
        self.jwt_patcher = patch('twilio.http.validation_client.ClientValidationJwt')

        self.session_mock = Mock(wraps=Session())
        self.validation_token = self.jwt_patcher.start()
        self.request_mock = Mock()

        self.session_mock.prepare_request.return_value = self.request_mock
        self.session_mock.send.return_value = Mock(status_code=200, content='test')
        self.validation_token.return_value.to_jwt.return_value = 'test-token'
        self.request_mock.headers = {}

        session_constructor_mock = self.session_patcher.start()
        session_constructor_mock.return_value = self.session_mock

        self.client = ValidationClient('AC123', 'SK123', 'CR123', 'private_key')