def test_process_after_request_hooks_processed_in_order(app):

    call_sequence = []

    def test_hook(type_, sequence):
        def hook(endpoint, resp):
            sequence.append(type_)

        return hook

    api_hook = test_hook('api', call_sequence)
    resource_hook = test_hook('resource', call_sequence)
    endpoint_hook = test_hook('endpoint', call_sequence)
    endpoint_get_hook = test_hook('endpoint_get', call_sequence)

    my_api = ArrestedAPI(after_all_hooks=[api_hook], url_prefix='/')
    my_api.init_app(app)
    my_resource = Resource('test', __name__, after_all_hooks=[resource_hook])

    class MyEndpoint(Endpoint):

        after_all_hooks = [endpoint_hook]
        after_get_hooks = [endpoint_get_hook]
        url = ''

    my_resource.add_endpoint(Endpoint)
    my_api.register_resource(my_resource)
    endpoint = MyEndpoint()
    endpoint.resource = my_resource
    endpoint.meth = 'get'

    resp = MagicMock(spec=Response())
    endpoint.process_after_request_hooks(resp)

    assert call_sequence == ['endpoint_get', 'endpoint', 'resource', 'api']
def test_process_before_request_hooks_processed_in_order(app):

    call_sequence = []

    def test_hook(type_, sequence):
        def hook(endpoint):
            sequence.append(type_)

        return hook

    api_hook = test_hook('api', call_sequence)
    resource_hook = test_hook('resource', call_sequence)
    endpoint_hook = test_hook('endpoint', call_sequence)
    endpoint_get_hook = test_hook('endpoint_get', call_sequence)

    my_api = ArrestedAPI(before_all_hooks=[api_hook], url_prefix='/')
    my_api.init_app(app)
    my_resource = Resource('test', __name__, before_all_hooks=[resource_hook])

    class MyEndpoint(Endpoint):

        before_all_hooks = [endpoint_hook]
        before_get_hooks = [endpoint_get_hook]
        url = ''

    my_resource.add_endpoint(Endpoint)
    my_api.register_resource(my_resource)
    endpoint = MyEndpoint()
    endpoint.resource = my_resource
    endpoint.meth = 'get'

    endpoint.process_before_request_hooks()

    assert call_sequence == ['api', 'resource', 'endpoint', 'endpoint_get']
示例#3
0
def test_defer_resource_registration(app):
    """Test that Resources are properly reigstered as a blueprint when
    ArrestedAPI.register_resource is called.
    """
    api_v1 = ArrestedAPI()
    example_resource = Resource('example', __name__, url_prefix='/example')
    example_resource_2 = Resource('example_2',
                                  __name__,
                                  url_prefix='/example-2')
    api_v1.register_resource(example_resource, defer=True)
    api_v1.register_resource(example_resource_2, defer=True)

    assert app.blueprints == {}

    api_v1.init_app(app)
    assert app.blueprints == {
        'example': example_resource,
        'example_2': example_resource_2
    }
示例#4
0
def defer_app_initialisation(app):
    """Test deferring initialising the flask app object using init_app method.
    """
    api_v1 = ArrestedAPI()
    api_v1.init_app(app)
    assert api_v1.app == app