コード例 #1
0
 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')
コード例 #2
0
class TestValidationClientRequest(unittest.TestCase):
    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 = Response(
            200, 'test, omega: Ω, pile of poop: 💩')
        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')

    def tearDown(self):
        self.session_patcher.stop()
        self.jwt_patcher.stop()

    def test_request_does_not_overwrite_host_header(self):
        self.request_mock.url = 'https://api.twilio.com/'

        self.client.request('doesnt matter', 'doesnt matter')

        self.assertEqual('api.twilio.com', self.request_mock.headers['Host'])
        self.assertEqual('test-token',
                         self.request_mock.headers['Twilio-Client-Validation'])

    def test_request_sets_host_header_if_missing(self):
        self.request_mock.url = 'https://api.twilio.com/'
        self.request_mock.headers = {'Host': 'other.twilio.com'}

        self.client.request('doesnt matter', 'doesnt matter')

        self.assertEqual('other.twilio.com', self.request_mock.headers['Host'])
        self.assertEqual('test-token',
                         self.request_mock.headers['Twilio-Client-Validation'])

    def test_request_with_unicode_response(self):
        self.request_mock.url = 'https://api.twilio.com/'
        self.request_mock.headers = {'Host': 'other.twilio.com'}

        response = self.client.request('doesnt matter', 'doesnt matter')

        self.assertEqual('other.twilio.com', self.request_mock.headers['Host'])
        self.assertEqual('test-token',
                         self.request_mock.headers['Twilio-Client-Validation'])
        self.assertEqual(200, response.status_code)
        self.assertEqual('test, omega: Ω, pile of poop: 💩',
                         response.content)
コード例 #3
0
class TestValidationClientHelpers(unittest.TestCase):
    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 test_build_validation_payload_basic(self):
        validation_payload = self.client._build_validation_payload(self.request)
        self.assertEqual('GET', validation_payload.method)
        self.assertEqual('/2010-04-01/Accounts/AC123/Messages', validation_payload.path)
        self.assertEqual('', validation_payload.query_string)
        self.assertEqual(['authorization', 'host'], validation_payload.signed_headers)
        self.assertEqual('', validation_payload.body)

    def test_build_validation_payload_query_string_parsed(self):
        self.request.url = self.request.url + '?QueryParam=1&Other=true'

        validation_payload = self.client._build_validation_payload(self.request)

        self.assertEqual('GET', validation_payload.method)
        self.assertEqual('/2010-04-01/Accounts/AC123/Messages', validation_payload.path)
        self.assertEqual('QueryParam=1&Other=true', validation_payload.query_string)
        self.assertEqual(['authorization', 'host'], validation_payload.signed_headers)
        self.assertEqual('', validation_payload.body)

    def test_build_validation_payload_body_parsed(self):
        self.request.body = 'foobar'

        validation_payload = self.client._build_validation_payload(self.request)

        self.assertEqual('GET', validation_payload.method)
        self.assertEqual('/2010-04-01/Accounts/AC123/Messages', validation_payload.path)
        self.assertEqual('', validation_payload.query_string)
        self.assertEqual(['authorization', 'host'], validation_payload.signed_headers)
        self.assertEqual('foobar', validation_payload.body)

    def test_build_validation_payload_complex(self):
        self.request.body = 'foobar'
        self.request.url = self.request.url + '?QueryParam=Value&OtherQueryParam=OtherValue'

        validation_payload = self.client._build_validation_payload(self.request)

        self.assertEqual('GET', validation_payload.method)
        self.assertEqual('/2010-04-01/Accounts/AC123/Messages', validation_payload.path)
        self.assertEqual(['authorization', 'host'], validation_payload.signed_headers)
        self.assertEqual('foobar', validation_payload.body)
        self.assertEqual('QueryParam=Value&OtherQueryParam=OtherValue',
                         validation_payload.query_string)

    def test_get_host(self):
        self.assertEqual('api.twilio.com', self.client._get_host(self.request))
コード例 #4
0
class TestValidationClientHelpers(unittest.TestCase):
    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 test_build_validation_payload_basic(self):
        validation_payload = self.client._build_validation_payload(self.request)
        self.assertEqual('GET', validation_payload.method)
        self.assertEqual('/2010-04-01/Accounts/AC123/Messages', validation_payload.path)
        self.assertEqual('', validation_payload.query_string)
        self.assertEqual(['authorization', 'host'], validation_payload.signed_headers)
        self.assertEqual('', validation_payload.body)

    def test_build_validation_payload_query_string_parsed(self):
        self.request.url = self.request.url + '?QueryParam=1&Other=true'

        validation_payload = self.client._build_validation_payload(self.request)

        self.assertEqual('GET', validation_payload.method)
        self.assertEqual('/2010-04-01/Accounts/AC123/Messages', validation_payload.path)
        self.assertEqual('QueryParam=1&Other=true', validation_payload.query_string)
        self.assertEqual(['authorization', 'host'], validation_payload.signed_headers)
        self.assertEqual('', validation_payload.body)

    def test_build_validation_payload_body_parsed(self):
        self.request.body = 'foobar'

        validation_payload = self.client._build_validation_payload(self.request)

        self.assertEqual('GET', validation_payload.method)
        self.assertEqual('/2010-04-01/Accounts/AC123/Messages', validation_payload.path)
        self.assertEqual('', validation_payload.query_string)
        self.assertEqual(['authorization', 'host'], validation_payload.signed_headers)
        self.assertEqual('foobar', validation_payload.body)

    def test_build_validation_payload_complex(self):
        self.request.body = 'foobar'
        self.request.url = self.request.url + '?QueryParam=Value&OtherQueryParam=OtherValue'

        validation_payload = self.client._build_validation_payload(self.request)

        self.assertEqual('GET', validation_payload.method)
        self.assertEqual('/2010-04-01/Accounts/AC123/Messages', validation_payload.path)
        self.assertEqual(['authorization', 'host'], validation_payload.signed_headers)
        self.assertEqual('foobar', validation_payload.body)
        self.assertEqual('QueryParam=Value&OtherQueryParam=OtherValue',
                         validation_payload.query_string)

    def test_get_host(self):
        self.assertEqual('api.twilio.com', self.client._get_host(self.request))
コード例 #5
0
class TestValidationClientRequest(unittest.TestCase):

    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 = Response(200, 'test, omega: Ω, pile of poop: 💩')
        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')

    def tearDown(self):
        self.session_patcher.stop()
        self.jwt_patcher.stop()

    def test_request_does_not_overwrite_host_header(self):
        self.request_mock.url = 'https://api.twilio.com/'

        self.client.request('doesnt matter', 'doesnt matter')

        self.assertEqual('api.twilio.com', self.request_mock.headers['Host'])
        self.assertEqual('test-token', self.request_mock.headers['Twilio-Client-Validation'])

    def test_request_sets_host_header_if_missing(self):
        self.request_mock.url = 'https://api.twilio.com/'
        self.request_mock.headers = {'Host': 'other.twilio.com'}

        self.client.request('doesnt matter', 'doesnt matter')

        self.assertEqual('other.twilio.com', self.request_mock.headers['Host'])
        self.assertEqual('test-token', self.request_mock.headers['Twilio-Client-Validation'])

    def test_request_with_unicode_response(self):
        self.request_mock.url = 'https://api.twilio.com/'
        self.request_mock.headers = {'Host': 'other.twilio.com'}

        response = self.client.request('doesnt matter', 'doesnt matter')

        self.assertEqual('other.twilio.com', self.request_mock.headers['Host'])
        self.assertEqual('test-token', self.request_mock.headers['Twilio-Client-Validation'])
        self.assertEqual(200, response.status_code)
        self.assertEqual('test, omega: Ω, pile of poop: 💩', response.content)
コード例 #6
0
 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')
コード例 #7
0
    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')
コード例 #8
0
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!')
コード例 #9
0
    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 = Response(200, 'test, omega: Ω, pile of poop: 💩')
        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')