Exemplo n.º 1
0
def test_predicates():
    config = setup()

    class app(App):
        testing_config = config

    config.commit()

    def view(self, request):
        return "all"

    def post_view(self, request):
        return "post"

    registry = app.registry
    register_view(registry, dict(model=Model), view)
    register_view(registry, dict(model=Model, request_method='POST'),
                  post_view)

    model = Model()
    assert resolve_response(
        app().request(get_environ(path='')), model).body == b'all'
    assert (resolve_response(app().
                             request(get_environ(path='', method='POST')),
                             model).body == b'post')
Exemplo n.º 2
0
def test_request_view_with_predicates():
    app = App()

    c = setup()
    c.configurable(app)
    c.commit()

    def view(request, model):
        return {'hey': 'hey'}

    register_view(app, Model, view, render=render_json,
                  predicates=dict(name='foo'))

    request = app.request(get_environ(path=''))
    request.mounts = [app]  # XXX should do this centrally

    model = Model()
    # since the name is set to foo, we get nothing here
    assert request.view(model) is None
    # we have to pass the name predicate ourselves
    assert request.view(model, name='foo') == {'hey': 'hey'}
    # the predicate information in the request is ignored when we do a
    # manual view lookup using request.view
    request = app.request(get_environ(path='foo'))
    request.mounts = [app]  # XXX should do this centrally
    assert request.view(model) is None
Exemplo n.º 3
0
def test_request_view_with_predicates():
    config = setup()

    class app(morepath.App):
        testing_config = config

    config.commit()

    def view(self, request):
        return {'hey': 'hey'}

    register_view(app.registry, dict(model=Model, name='foo'), view,
                  render=render_json)

    request = app().request(get_environ(path=''))

    model = Model()
    # since the name is set to foo, we get nothing here
    assert request.view(model) is None
    # we have to pass the name predicate ourselves
    assert request.view(model, name='foo') == {'hey': 'hey'}
    # the predicate information in the request is ignored when we do a
    # manual view lookup using request.view
    request = app().request(get_environ(path='foo'))
    assert request.view(model) is None
Exemplo n.º 4
0
def test_view_after_doesnt_apply_to_exception_view():
    config = setup()

    class App(morepath.App):
        testing_config = config

    class Root(object):
        pass

    class MyException(Exception):
        pass

    @App.path(model=Root, path='')
    def get_root():
        return Root()

    @App.view(model=Root)
    def view(self, request):
        @request.after
        def set_header(response):
            response.headers.add('Foo', 'FOO')
        raise MyException()

    @App.view(model=MyException)
    def exc_view(self, request):
        return "My exception"

    config.commit()

    c = Client(App())

    response = c.get('/')
    assert response.body == b'My exception'
    assert response.headers.get('Foo') is None
Exemplo n.º 5
0
def test_notfound():
    config = setup()
    app = App(testing_config=config)
    config.commit()

    response = publish(app.request(get_environ(path='')), app.mounted())
    assert response.status == '404 NOT FOUND'
Exemplo n.º 6
0
def test_view_after_doesnt_apply_to_exception():
    config = setup()

    class App(morepath.App):
        testing_config = config

    class Root(object):
        pass

    @App.path(model=Root, path='')
    def get_root():
        return Root()

    @App.view(model=Root)
    def view(self, request):
        @request.after
        def set_header(response):
            response.headers.add('Foo', 'FOO')
        raise HTTPNotFound()

    config.commit()

    c = Client(App())

    response = c.get('/', status=404)
    assert response.headers.get('Foo') is None
Exemplo n.º 7
0
def test_notfound():
    app = App()

    c = setup()
    c.configurable(app)
    c.commit()

    response = publish(app.request(get_environ(path='')), app.mounted())
    assert response.status == '404 NOT FOUND'
Exemplo n.º 8
0
def test_notfound():
    config = setup()
    app = App(testing_config=config)
    config.commit()

    request = app.request(get_environ(path=''))
    request.mounts.append(app.mounted())

    with pytest.raises(HTTPNotFound):
        publish(request)
Exemplo n.º 9
0
def test_response_returned():
    config = setup()
    app = App(testing_config=config)
    config.commit()

    def view(self, request):
        return Response('Hello world!')

    register_view(app, Model, view)
    model = Model()
    response = resolve_response(app.request(get_environ(path='')), model)
    assert response.data == 'Hello world!'
Exemplo n.º 10
0
def test_notfound():
    config = setup()

    class app(App):
        testing_config = config

    config.commit()

    request = app().request(get_environ(path=''))

    with pytest.raises(HTTPNotFound):
        publish(request)
Exemplo n.º 11
0
def test_view():
    config = setup()
    app = App(testing_config=config)
    config.commit()

    def view(self, request):
        return "View!"

    register_view(app, Model, view, predicates=dict(name=''))

    model = Model()
    result = resolve_response(app.request(get_environ(path='')), model)
    assert result.data == 'View!'
Exemplo n.º 12
0
def test_response_returned():
    app = App()

    c = setup()
    c.configurable(app)
    c.commit()

    def view(request, model):
        return Response('Hello world!')

    register_view(app, Model, view)
    model = Model()
    response = resolve_response(app.request(get_environ(path='')), model)
    assert response.data == 'Hello world!'
Exemplo n.º 13
0
def test_notfound_with_predicates():
    config = setup()
    app = App(testing_config=config)
    config.commit()

    def view(self, request):
        return "view"

    register_view(app, Model, view, predicates=dict(name=''))
    model = Model()
    request = app.request(get_environ())
    request.unconsumed = ['foo']
    with pytest.raises(NotFound):
        resolve_response(request, model)
Exemplo n.º 14
0
def test_response_returned():
    config = setup()

    class app(morepath.App):
        testing_config = config

    config.commit()

    def view(self, request):
        return Response('Hello world!')

    register_view(app.registry, dict(model=Model), view)
    model = Model()
    response = resolve_response(app().request(get_environ(path='')), model)
    assert response.body == b'Hello world!'
Exemplo n.º 15
0
def test_render_html():
    config = setup()
    app = App(testing_config=config)
    config.commit()

    def view(self, request):
        return '<p>Hello world!</p>'

    register_view(app, Model, view, render=render_html)

    request = app.request(get_environ(path=''))
    model = Model()
    response = resolve_response(request, model)
    assert response.data == '<p>Hello world!</p>'
    assert response.content_type == 'text/html'
Exemplo n.º 16
0
def test_view():
    app = App()

    c = setup()
    c.configurable(app)
    c.commit()

    def view(request, model):
        return "View!"

    register_view(app, Model, view, predicates=dict(name=''))

    model = Model()
    result = resolve_response(app.request(get_environ(path='')), model)
    assert result.data == 'View!'
Exemplo n.º 17
0
def test_view_raises_http_error():
    config = setup()
    app = App(testing_config=config)
    config.commit()

    from werkzeug.exceptions import BadRequest
    def view(self, request):
        raise BadRequest()

    register_path(app, Model, 'foo', None, None, None, Model)
    register_view(app, Model, view)

    response = publish(app.request(get_environ(path='foo')), app.mounted())

    assert response.status == '400 BAD REQUEST'
Exemplo n.º 18
0
def test_view_raises_http_error():
    config = setup()
    app = App(testing_config=config)
    config.commit()

    def view(self, request):
        raise HTTPBadRequest()

    register_path(app, Model, 'foo', None, None, None, None, Model)
    register_view(app, Model, view)

    request = app.request(get_environ(path='foo'))
    request.mounts.append(app.mounted())

    with pytest.raises(HTTPBadRequest):
        publish(request)
Exemplo n.º 19
0
def test_view():
    config = setup()

    class app(App):
        testing_config = config

    config.commit()

    def view(self, request):
        return "View!"

    register_view(app.registry, dict(model=Model), view)

    model = Model()
    result = resolve_response(app().request(get_environ(path='')), model)
    assert result.body == b'View!'
Exemplo n.º 20
0
def test_view_after():
    config = setup()
    app = App(testing_config=config)
    config.commit()

    def view(self, request):
        @request.after
        def set_header(response):
            response.headers.add('Foo', 'FOO')
        return "View!"

    register_view(app, Model, view, predicates=dict(name=''))

    model = Model()
    result = resolve_response(app.request(get_environ(path='')), model)
    assert result.data == 'View!'
    assert result.headers.get('Foo') == 'FOO'
Exemplo n.º 21
0
def test_view_raises_http_error():
    app = App()

    c = setup()
    c.configurable(app)
    c.commit()

    from werkzeug.exceptions import BadRequest
    def view(request, model):
        raise BadRequest()

    register_model(app, Model, 'foo', None, Model)
    register_view(app, Model, view)

    response = publish(app.request(get_environ(path='foo')), app.mounted())

    assert response.status == '400 BAD REQUEST'
Exemplo n.º 22
0
def test_notfound_with_predicates():
    config = setup()

    class app(morepath.App):
        testing_config = config

    config.commit()

    def view(self, request):
        return "view"

    register_view(app.registry, dict(model=Model), view)
    model = Model()
    request = app().request(get_environ(''))
    request.unconsumed = ['foo']
    with pytest.raises(HTTPNotFound):
        resolve_response(request, model)
Exemplo n.º 23
0
def test_render_html():
    config = setup()

    class app(App):
        testing_config = config

    config.commit()

    def view(self, request):
        return '<p>Hello world!</p>'

    register_view(app.registry, dict(model=Model), view, render=render_html)

    request = app().request(get_environ(path=''))
    model = Model()
    response = resolve_response(request, model)
    assert response.body == b'<p>Hello world!</p>'
    assert response.content_type == 'text/html'
Exemplo n.º 24
0
def test_view_raises_http_error():
    config = setup()

    class app(morepath.App):
        testing_config = config

    config.commit()

    def view(self, request):
        raise HTTPBadRequest()

    registry = app.registry
    register_path(registry, Model, 'foo', None, None, None, None, False, Model)
    register_view(registry, dict(model=Model), view)

    request = app().request(get_environ(path='foo'))

    with pytest.raises(HTTPBadRequest):
        publish(request)
Exemplo n.º 25
0
def test_predicates():
    config = setup()
    app = App(testing_config=config)
    config.commit()

    def view(self, request):
        return "all"

    def post_view(self, request):
        return "post"

    register_view(app, Model, view, predicates=dict(name=''))
    register_view(app, Model, post_view,
                  predicates=dict(name='', request_method='POST'))

    model = Model()
    assert resolve_response(
        app.request(get_environ(path='')), model).data == 'all'
    assert (resolve_response(app.request(get_environ(path='', method='POST')),
                             model).data == 'post')
Exemplo n.º 26
0
def test_request_view():
    config = setup()
    app = App(testing_config=config)
    config.commit()

    def view(self, request):
        return {'hey': 'hey'}

    register_view(app, Model, view, render=render_json)

    request = app.request(get_environ(path=''))
    request.mounts = [app]  # XXX should do this centrally

    model = Model()
    response = resolve_response(request, model)
    # when we get the response, the json will be rendered
    assert response.data == '{"hey": "hey"}'
    assert response.content_type == 'application/json'
    # but we get the original json out when we access the view
    assert request.view(model) == {'hey': 'hey'}
Exemplo n.º 27
0
def test_view_after_non_decorator():
    app = App()

    c = setup()
    c.configurable(app)
    c.commit()

    def set_header(response):
        response.headers.add('Foo', 'FOO')

    def view(request, model):
        request.after(set_header)
        return "View!"

    register_view(app, Model, view, predicates=dict(name=''))

    model = Model()
    result = resolve_response(app.request(get_environ(path='')), model)
    assert result.data == 'View!'
    assert result.headers.get('Foo') == 'FOO'
Exemplo n.º 28
0
def test_request_view():
    app = App()

    c = setup()
    c.configurable(app)
    c.commit()

    def view(request, model):
        return {'hey': 'hey'}

    register_view(app, Model, view, render=render_json)

    request = app.request(get_environ(path=''))
    model = Model()
    response = resolve_response(request, model)
    # when we get the response, the json will be rendered
    assert response.data == '{"hey": "hey"}'
    assert response.content_type == 'application/json'
    # but we get the original json out when we access the view
    assert request.view(model) == {'hey': 'hey'}
Exemplo n.º 29
0
def autoconfig(ignore=None):
    """Automatically load Morepath configuration from packages.

    Morepath configuration consists of decorator calls on :class:`App`
    instances, i.e. ``@app.view()`` and ``@app.path()``.

    This function loads all needed Morepath configuration from all
    packages automatically. These packages do need to be made
    available using a ``setup.py`` file including currect
    ``install_requires`` information so that they can be found using
    setuptools_.

    .. _setuptools: http://pythonhosted.org/setuptools/

    Creates a :class:`Config` object as with :func:`setup`, but before
    returning it scans all packages, looking for those that depend on
    Morepath directly or indirectly. This includes the package that
    calls this function. Those packages are then scanned for
    configuration as with :meth:`Config.scan`.

    You can add manual :meth:`Config.scan` calls yourself on the
    returned :class:`Config` object. Finally you need to call
    :meth:`Config.commit` on the returned :class:`Config` object so
    the configuration is committed.

    Typically called immediately after startup just before the
    application starts serving using WSGI.

    See also :func:`autosetup`.

    :param ignore: Venusian_ style ignore to ignore some modules
      during scanning. Optional.
    :returns: :class:`Config` object.

    .. _Venusian: http://venusian.readthedocs.org

    """
    c = setup()
    for package in morepath_packages():
        c.scan(package, ignore)
    return c
Exemplo n.º 30
0
def test_view_after_non_decorator():
    config = setup()

    class app(morepath.App):
        testing_config = config

    config.commit()

    def set_header(response):
        response.headers.add('Foo', 'FOO')

    def view(self, request):
        request.after(set_header)
        return "View!"

    register_view(app.registry, dict(model=Model), view)

    model = Model()
    result = resolve_response(app().request(get_environ(path='')), model)
    assert result.body == b'View!'
    assert result.headers.get('Foo') == 'FOO'