B
    `                 @   sV   d dl mZ d dlmZ d dlmZmZmZ d dlm	Z	 d dl
mZ G dd dZdS )	    )	Interface)action_method)IRendererFactoryISecurityPolicy
ITraverser)RendererHelper)split_path_infoc               @   s>   e Zd ZdddZdd ZeZedddZdd	d
ZeZ	dS )TestingConfiguratorMixinNTc             C   s.   ddl m} ||||||}| j|t |S )a0  Unit/integration testing helper.  Registers a faux :term:`security
        policy`.

        This function is most useful when testing code that uses the security
        APIs, such as :meth:`pyramid.request.Request.identity`,
        :attr:`pyramid.request.Request.authenticated_userid`, or
        :meth:`pyramid.request.Request.has_permission`,

        The behavior of the registered :term:`security policy` depends on the
        arguments passed to this method.

        :param userid:  If provided, the policy's ``authenticated_userid``
            method will return this value.  As a result,
            :attr:`pyramid.request.Request.authenticated_userid` will have this
            value as well.
        :type userid:  str
        :param identity:  If provided, the policy's ``identity`` method will
            return this value.  As a result,
            :attr:`pyramid.request.Request.identity`` will have this value.
        :type identity:  object
        :param permissive:  If true, the policy will allow access to any user
            for any permission.  If false, the policy will deny all access.
        :type permissive:  bool
        :param remember_result:  If provided, the policy's ``remember`` method
            will return this value.  Otherwise, ``remember`` will return an
            empty list.
        :type remember_result:  list
        :param forget_result:  If provided, the policy's ``forget`` method will
            return this value.  Otherwise, ``forget`` will return an empty
            list.
        :type forget_result:  list

        .. versionadded:: 1.4
            The ``remember_result`` argument.

        .. versionadded:: 1.4
            The ``forget_result`` argument.

        .. versionchanged:: 2.0
            Removed ``groupids`` argument and add `identity` argument.

        r   )DummySecurityPolicy)pyramid.testingr
   registryregisterUtilityr   )selfZuserididentityZ
permissiveZremember_resultZforget_resultr
   policy r   ^/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/config/testing.pytesting_securitypolicy   s
    2z/TestingConfiguratorMixin.testing_securitypolicyc                s(   G  fddd}| j |tft  S )a  Unit/integration testing helper: registers a dictionary of
        :term:`resource` objects that can be resolved via the
        :func:`pyramid.traversal.find_resource` API.

        The :func:`pyramid.traversal.find_resource` API is called with
        a path as one of its arguments.  If the dictionary you
        register when calling this method contains that path as a
        string key (e.g. ``/foo/bar`` or ``foo/bar``), the
        corresponding value will be returned to ``find_resource`` (and
        thus to your code) when
        :func:`pyramid.traversal.find_resource` is called with an
        equivalent path string or tuple.
        c                   s    e Zd Zdd Z fddZdS )zITestingConfiguratorMixin.testing_resources.<locals>.DummyTraverserFactoryc             S   s
   || _ d S )N)context)r   r   r   r   r   __init__U   s    zRTestingConfiguratorMixin.testing_resources.<locals>.DummyTraverserFactory.__init__c                s*   |j } | }t|}|dd||d|dS )N r   )r   Z	view_namesubpath	traversedZvirtual_rootZvirtual_root_pathroot)Z	path_infor   )r   requestpathobr   )	resourcesr   r   __call__X   s    zRTestingConfiguratorMixin.testing_resources.<locals>.DummyTraverserFactory.__call__N)__name__
__module____qualname__r   r   r   )r   r   r   DummyTraverserFactoryT   s   r"   )r   ZregisterAdapterr   r   )r   r   r"   r   )r   r   testing_resourcesE   s    z*TestingConfiguratorMixin.testing_resourcesc                s*   |  |}g   fdd}| ||  S )a  Unit/integration testing helper: Registers a
        :term:`subscriber` which listens for events of the type
        ``event_iface``.  This method returns a list object which is
        appended to by the subscriber whenever an event is captured.

        When an event is dispatched that matches the value implied by
        the ``event_iface`` argument, that event will be appended to
        the list.  You can then compare the values in the list to
        expected event notifications.  This method is useful when
        testing code that wants to call
        :meth:`pyramid.registry.Registry.notify`,
        or :func:`zope.component.event.dispatch`.

        The default value of ``event_iface`` (``None``) implies a
        subscriber registered for *any* kind of event.
        c                 s     |  d S )N)extend)event)Lr   r   
subscriber   s    zCTestingConfiguratorMixin.testing_add_subscriber.<locals>.subscriber)Zmaybe_dottedZadd_subscriber)r   Zevent_ifacer'   r   )r&   r   testing_add_subscriberm   s
    
z/TestingConfiguratorMixin.testing_add_subscriberc             C   s   ddl m} t|| jd}| jjt|jd}t||sV||j|}| jj|t|jd ddl m	} |dkrp| }|
|| |S )a  Unit/integration testing helper: register a renderer at
        ``path`` (usually a relative filename ala ``templates/foo.pt``
        or an asset specification) and return the renderer object.
        If the ``renderer`` argument is None, a 'dummy' renderer will
        be used.  This function is useful when testing code that calls
        the :func:`pyramid.renderers.render` function or
        :func:`pyramid.renderers.render_to_response` function or
        any other ``render_*`` or ``get_*`` API of the
        :mod:`pyramid.renderers` module.

        Note that calling this method for with a ``path`` argument
        representing a renderer factory type (e.g. for ``foo.pt``
        usually implies the ``chameleon_zpt`` renderer factory)
        clobbers any existing renderer factory registered for that
        type.

        .. note:: This method is also available under the alias
           ``testing_add_template`` (an older name for it).

        r   )DummyRendererFactory)namer   )r*   )DummyTemplateRendererN)r   r)   r   r   ZqueryUtilityr   type
isinstancer   r+   add)r   r   Zrendererr)   helperfactoryr+   r   r   r   testing_add_renderer   s    
z-TestingConfiguratorMixin.testing_add_renderer)NNTNN)N)N)
r   r    r!   r   r#   Ztesting_modelsr   r(   r1   Ztesting_add_templater   r   r   r   r	   	   s       
4&
(r	   N)Zzope.interfacer   Zpyramid.config.actionsr   Zpyramid.interfacesr   r   r   Zpyramid.renderersr   Zpyramid.traversalr   r	   r   r   r   r   <module>   s
   