Beispiel #1
0
def test_unproxification():
    """test for unproxified error view publishing.
    """
    root = get_structure()
    publisher = DawnlightPublisher(
        view_lookup=ViewLookup(lookup=wrap_http_renderer))

    provideAdapter(
        AttributeErrorView, (AttributeError, IRequest), IView)

    provideAdapter(
        InnocentView, (IModel, IRequest), IView, name="innocent")

    req = TestRequest(path="/a/@@innocent")
    proxified = wrap_http_renderer(req, root.a, "innocent")

    with pytest.raises(AttributeError):
        proxified.context

    with pytest.raises(AttributeError):
        proxified()

    assert publisher.publish(req, root) == (
        u"AttributeError on <class 'cromlech.dawnlight.tests."
        u"test_publish.Container'>")

    # we test the error handling deactivation
    with pytest.raises(AttributeError):
        assert publisher.publish(req, root, handle_errors=False)
Beispiel #2
0
def test_item_traversing():
    """test that sub item traversing works.
    """
    root = get_structure()
    publisher = DawnlightPublisher()

    req = TestRequest(path="/b")
    assert publisher.publish(req, root) == root['b']
Beispiel #3
0
def test_end_with_slash():
    root = get_structure()
    publisher = DawnlightPublisher()

    req = TestRequest(path="/b/")
    assert publisher.publish(req, root) == root['b']

    req = TestRequest(path="/b///")
    assert publisher.publish(req, root) == root['b']
Beispiel #4
0
def test_no_view():
    """test for raising ResolveError.
    """
    root = get_structure()
    publisher = DawnlightPublisher()

    req = TestRequest(path="/b/@@unknown")
    with pytest.raises(ResolveError):
        publisher.publish(req, root)
Beispiel #5
0
def test_attribute_masquerade_item():
    """test that attributes takes precedence over sub item.
    """
    root = get_structure()
    root['a'] = Model()
    publisher = DawnlightPublisher()
    
    req = TestRequest(path="/a")
    assert publisher.publish(req, root) == root.a
    assert publisher.publish(req, root) != root['a']
Beispiel #6
0
    def __call__(self, request):
        publisher = DawnlightPublisher(
            request, self, view_lookup=view_lookup)
        notify(PublicationBeginsEvent(self.site, request))

        result = publisher.publish(self.site, handle_errors=True)

        notify(PublicationEndsEvent(request, result))

        return result
Beispiel #7
0
 def publisher(environ, start_response):
     request = Request(environ)
     site = Site(name)
     setSite(site)
     notify(PublicationBeginsEvent(site, request))
     publisher = DawnlightPublisher(view_lookup=view_lookup)
     response = publisher.publish(request, site, handle_errors=True)
     notify(PublicationEndsEvent(request, response))
     setSite()
     return response(environ, start_response)
Beispiel #8
0
def test_attribute_traversing():
    """test that attributes traversing works"""
    root = get_structure()
    publisher = DawnlightPublisher()

    req = TestRequest(path="/a")
    assert publisher.publish(req, root) == root.a

    req = TestRequest(path="/_b")
    assert publisher.publish(req, root) == root._b
Beispiel #9
0
def test_private_attribute_not_traversing():
    """test that traversing on private attributes does not works"""
    root = get_structure()
    publisher = DawnlightPublisher()

    req = TestRequest(path="/c")
    with pytest.raises(ResolveError):
        publisher.publish(req, root)

    req = TestRequest(path="/not_existing")
    with pytest.raises(ResolveError):
        publisher.publish(req, root)
Beispiel #10
0
def test_urlencoded_path():
    """test urlencoded path
    """
    root = Container()
    setattr(root, "à", Model())
    root[u"â ñ"] = Model()
    publisher = DawnlightPublisher()

    req = TestRequest(path="/%C3%A0")
    assert publisher.publish(req, root) == getattr(root, 'à')

    req = TestRequest(path="/%C3%A2%20%C3%B1")
    assert publisher.publish(req, root) == root[u"â ñ"]
Beispiel #11
0
def test_uncomplete_publication():
    """test for raising PublicationError.
    """
    # no more default publisher
    def no_lookup(request, result, crumbs):
        return None

    root = get_structure()
    publisher = DawnlightPublisher(view_lookup=no_lookup)

    req = TestRequest(path="/a")
    with pytest.raises(PublicationError):
        publisher.publish(req, root)
def test_traverser_traversing():
    root = get_structure()
    publisher = DawnlightPublisher()

    # register traverser for namespace spam
    crom.implicit.registry.register(
        (Container, IRequest), ITraverser, 'spam', SpamTraverser)

    req = Request(path="/++spam++foo")
    assert publisher.publish(req, root) == root._spam_foo

    req = Request(path="/++spam++bar")
    with pytest.raises(AttributeError):
        publisher.publish(req, root)
def test_faulty_resolution():
    root = get_structure()
    publisher = DawnlightPublisher()

    # Fail on the renderer init
    crom.implicit.registry.register(
        (IModel, IRequest), IView, 'faulty_init', FaultyInit)
    
    req = Request(path="/a/faulty_init")
    with pytest.raises(NotImplementedError) as e:
        publisher.publish(req, root)
        assert e.value == 'init failed'

    # Fail in the renderer call
    crom.implicit.registry.register(
        (IModel, IRequest), IView, 'faulty_caller', FaultyCaller)

    req = Request(path="/a/faulty_caller")
    with pytest.raises(NotImplementedError) as e:
        publisher.publish(req, root)

    # We can render errors
    crom.implicit.registry.register(
        (NotImplementedError, IRequest), IView, '', NotImplementedView)

    req = Request(path="/a/faulty_caller")
    assert publisher.publish(req, root) == 'Not implemented: call failed'

    # Simulation of a component lookup error
    crom.implicit.registry.register(
        (IModel, IRequest), IView, 'fail_lookup', LookupFailure)

    req = Request(path="/a/fail_lookup")
    with pytest.raises(ComponentLookupError) as e:
        publisher.publish(req, root)
Beispiel #14
0
def test_faulty_resolution():
    root = get_structure()
    publisher = DawnlightPublisher()

    # Fail on the renderer init
    provideAdapter(FaultyInit, (IModel, IRequest),
                   IView, name=u'faulty_init')
    
    req = TestRequest(path="/a/faulty_init")
    with pytest.raises(NotImplementedError) as e:
        publisher.publish(req, root)
        assert e.value == 'init failed'

    # Fail in the renderer call
    provideAdapter(FaultyCaller, (IModel, IRequest),
                   IView, name=u'faulty_caller')

    req = TestRequest(path="/a/faulty_caller")
    with pytest.raises(NotImplementedError) as e:
        publisher.publish(req, root)

    # We can render errors
    provideAdapter(
        NotImplementedView, (NotImplementedError, IRequest), IView)

    req = TestRequest(path="/a/faulty_caller")
    assert publisher.publish(req, root) == u'Not implemented: call failed'

    # Simulation of a component lookup error
    provideAdapter(LookupFailure, (IModel, IRequest),
                   IView, name=u'fail_lookup')

    req = TestRequest(path="/a/fail_lookup")
    with pytest.raises(ComponentLookupError) as e:
        publisher.publish(req, root)
Beispiel #15
0
def test_script_name():
    """test that request.script_name is taken into account"""
    root = get_structure()
    publisher = DawnlightPublisher()

    req = TestRequest(path="/foo/a", script_name="/foo")
    assert publisher.publish(req, root) == root.a

    req = TestRequest(path="/foo/a", script_name="/bar")
    with pytest.raises(ResolveError):
        publisher.publish(req, root)
                  
    req = TestRequest(path="/a", script_name="/foo")
    assert publisher.publish(req, root) == root.a
Beispiel #16
0
def test_traverser_traversing():
    root = get_structure()
    publisher = DawnlightPublisher()

    # register traverser for namespace spam
    provideAdapter(
        SpamTraverser, (Container, IRequest), ITraverser, name=u'spam')

    req = TestRequest(path="/++spam++foo")
    assert publisher.publish(req, root) == root._spam_foo

    req = TestRequest(path="/++spam++bar")
    with pytest.raises(AttributeError):
        publisher.publish(req, root)
Beispiel #17
0
class TrajectApplication(object):

    def __init__(self, name, engine):
        self.name = name
        self.engine = engine
        self.site = Site(name)
        self.publisher = DawnlightPublisher(view_lookup=view_lookup)

    def add_object(self, obj, session=None):
        if session is None:
            session = get_session(self.name)
        session.add(obj)

    @wsgify(RequestClass=request.Request)
    def __call__(self, request):
        with SQLAlchemySession(self.engine):
            with transaction.manager:
                with Interaction():
                    setSite(self.site)

                    # Publication is about to begin
                    notify(PublicationBeginsEvent(self.site, request))

                    # publish
                    result = self.publisher.publish(
                        request, self.site, handle_errors=True)

                    # Publication ends
                    notify(PublicationEndsEvent(request, result))

                    setSite()
                    return removeSecurityProxy(result)
def test_consumer_returning_view():

    class ViewReturningConsumer:

        def __init__(self, context):
            self.context = context
        
        def __call__(self, request, obj, stack):
            view = RawView(obj, request)
            return True, view, deque()

    dawnlight_components.subscribe(
        (Interface,), IConsumer, ViewReturningConsumer)

    root = get_structure()
    publisher = DawnlightPublisher()

    req = Request(path="/it_will_return_a_view")
    assert publisher.publish(req, root) == root
Beispiel #19
0
def test_consumer_returning_view():

    class ViewReturningConsumer(grok.Subscription):
        grok.implements(IConsumer)
        grok.context(Interface)

        def __call__(self, request, obj, stack):
            view = RawView(obj, request)
            return True, view, []

    grok_component('consumer', ViewReturningConsumer)

    root = get_structure()
    publisher = DawnlightPublisher()

    req = TestRequest(path="/it_will_return_a_view")
    assert publisher.publish(req, root) == root

    # The same test fail if we don't use the provided ViewLookup
    publisher = DawnlightPublisher(
        view_lookup=ViewLookup(lookup=wrap_http_renderer))
    with pytest.raises(ResolveError) as e:
        publisher.publish(req, root) == root
Beispiel #20
0
class WSGIApplication(object):

    def __init__(self, name):
        self.name = name
        self.publisher = DawnlightPublisher(view_lookup=view_lookup)

    @wsgify(RequestClass=Request)
    def __call__(self, request):
        conn = request.environment[KEY]
        site = get_site(conn, self.name)
        with Site(site) as site:
            response = self.publisher.publish(
                request,
                site,
                handle_errors=True
            )
        return response
Beispiel #21
0
class Application(object):

    def __init__(self, engine):
        self.engine = engine
        self.publisher = DawnlightPublisher(view_lookup=view_lookup)

    def __call__(self, environ, start_response):
        request = Request(environ)
        with transaction.manager as tm:
            with Interaction():
                with Site(ROOT) as root:
                    with SQLAlchemySession(self.engine, transaction_manager=tm):
                        notify(PublicationBeginsEvent(root, request))
                        response = self.publisher.publish(
                            request, root, handle_errors=True)
                        result = response(environ, start_response)
        return result
Beispiel #22
0
 def __init__(self, name):
     self.name = name
     self.publisher = DawnlightPublisher(view_lookup=view_lookup)
Beispiel #23
0
# used by the form.
auth = Auth({
    'demo': 'demo',
    'admin': 'admin',
    'grok': 'grok',
})


# The publisher is a 2 times component that is in charge of looking up
# the model and the view, given an URL.
# It relies on traversers to handle the publishing.
# Here, we provide a custom way to retrieve views, using a security-aware
# function.
# See `dawnlight` and `cromlech.dawnlight` for more information.
publisher = DawnlightPublisher(
    view_lookup=ViewLookup(view_locator(secure_query_view)),
).publish


class Session(object):

    def __init__(self, session):
        self.session = session

    def __enter__(self):
        setSession(self.session)
        return self.session

    def __exit__(self, type, value, traceback):
        # Traceback or not, we reset the session thread-local.
        # Exiting the block, we don't want the session set.
Beispiel #24
0
def test_unicode_script_name():
    root = get_structure()
    publisher = DawnlightPublisher()

    req = TestRequest(path=u"/éléonore", script_name=u'/')
    assert publisher.publish(req, root) == root[u"éléonore"]
Beispiel #25
0
def test_path_parsing():
    root = get_structure()
    publisher = DawnlightPublisher()

    req = TestRequest(path=u"/éléonore")
    assert publisher.publish(req, root) == root[u"éléonore"]
Beispiel #26
0
 def __init__(self, name, engine):
     self.name = name
     self.engine = engine
     self.site = Site(name)
     self.publisher = DawnlightPublisher(view_lookup=view_lookup)
Beispiel #27
0
 def __init__(self, engine):
     self.engine = engine
     self.publisher = DawnlightPublisher(view_lookup=view_lookup)
Beispiel #28
0
 def get_publisher(self,
                   view_lookup=secured_view,
                   model_lookup=base_model_lookup):
     publisher = DawnlightPublisher(model_lookup, view_lookup)
     return publisher.publish
Beispiel #29
0
    # We init it here, just to have the view lookup working.
    from crom import monkey, implicit
    monkey.incompat()  # incompat means it changes the behavior of the iface
    implicit.initialize()  # we create a new registry and make it the default

    # we define our publisher
    from cromlech.dawnlight import DawnlightPublisher
    from cromlech.browser.interfaces import IView
    from cromlech.dawnlight import ViewLookup, view_locator
    from cromlech.webob.request import Request

    def query_view(request, context, name=""):
        return IView.component(context, request, name=name)

    view_lookup = ViewLookup(view_locator(query_view))
    Publisher = DawnlightPublisher(view_lookup=view_lookup)

    # We read the zodb conf and initialize it
    from cromlech.zodb import init_db_from_file
    with open(config['zodb']['config'], 'r') as fd:
        db = init_db_from_file(fd)

    # We create our ZODB connection manager
    import transaction
    from cromlech.zodb.controlled import Connection

    class ZODBApplication(object):
        def __init__(self, db):
            self.db = db

        async def handle(self, request):
def test_empty():
    root = get_structure()
    publisher = DawnlightPublisher()

    req = Request(path="/")
    assert publisher.publish(req, root) == root