def treat(request_body):
    """
    Treat a notification and guarantee its authenticity.

    :param request_body: The request body in plain text.
    :type request_body: string

    :return: A safe APIResource
    :rtype: APIResource
    """
    # Python 3+ support
    if isinstance(request_body, six.binary_type):
        request_body = request_body.decode("utf-8")

    try:
        data = json.loads(request_body)
    except ValueError:
        raise exceptions.UnknownAPIResource("Request body is malformed JSON.")

    unsafe_api_resource = APIResource.factory(data)

    try:
        consistent_api_resource = unsafe_api_resource.get_consistent_resource()
    except AttributeError:
        raise exceptions.UnknownAPIResource("The API resource provided is invalid.")

    return consistent_api_resource
Beispiel #2
0
def treat(request_body):
    """
    Treat a notification and guarantee its authenticity.

    :param request_body: The request body in plain text.
    :type request_body: string

    :return: A safe APIResource
    :rtype: APIResource
    """
    # Python 3+ support
    if isinstance(request_body, six.binary_type):
        request_body = request_body.decode('utf-8')

    try:
        data = json.loads(request_body)
    except ValueError:
        raise exceptions.UnknownAPIResource('Request body is malformed JSON.')

    unsafe_api_resource = APIResource.factory(data)

    try:
        consistent_api_resource = unsafe_api_resource.get_consistent_resource()
    except AttributeError:
        raise exceptions.UnknownAPIResource('The API resource provided is invalid.')

    return consistent_api_resource
 def test_get_attribute(self):
     resource = APIResource(foo='bar', val=22)
     assert resource.foo == 'bar'
     assert resource.val == 22
     assert resource._attributes == {'foo': 'bar', 'val': 22}
 def test_with_unknown_object(self):
     with pytest.raises(exceptions.UnknownAPIResource) as excinfo:
         APIResource.factory({'id': 'boo_42', 'object': 'bouillabaisse'})
     assert str(excinfo.value) == 'Unknown object `bouillabaisse`.'
 def test_factory_with_refund(self):
     refund = APIResource.factory({'id': 'a_refund_id', 'object': 'refund'})
     assert isinstance(refund, resources.Refund)
     assert refund.id == 'a_refund_id'
     assert refund.object == 'refund'
 def test_factory_with_payment(self):
     payment = APIResource.factory({'id': 'a_payment_id', 'object': 'payment'})
     assert isinstance(payment, resources.Payment)
     assert payment.id == 'a_payment_id'
     assert payment.object == 'payment'
 def test_factory_without_object_key(self):
     with pytest.raises(exceptions.UnknownAPIResource) as excinfo:
         APIResource.factory({'id': 'no_object_key'})
     assert str(excinfo.value) == 'Missing `object` key in resource.'
 def test_set_attributes_manually(self):
     resource = APIResource(foo='bar')
     resource.bar = 'baz'
     assert resource.bar == 'baz'
     assert not hasattr(resource, 'baz')
 def test_get_undefined_attribute(self):
     resource = APIResource(foo='bar', val=22)
     with pytest.raises(AttributeError) as excinfo:
         resource.baz
     assert '`baz`' in str(excinfo.value)