U
    |hF                     @   s   d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ ddlmZ ddlmZ ejd  ZG dd deeZG dd deZdS )    )AnyDictOptional)mock)settings)AbstractBaseUser)Http404)TestCase)reverse)	urlencode)slugify)get_dummy_request)Page   )querydict_from_html)WagtailTestUtilsc                
       s  e Zd ZdZe fddZdd Zd!ddZd"d	d
Zd#ddZ	d$ddZ
d%ddZd&eee ee dddZd'eee eeeef  eeeef  ee ee ee ee dddZd(eeeeef  ee ee dddZd)eee eeeef  ee ee ddd Z  ZS )*WagtailPageTestCasezO
    A set of assertions to help write tests for custom Wagtail page types
    c                    s   t    t | _d S N)super
setUpClassr   dummy_request)cls	__class__ e/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/test/utils/page_tests.pyr      s    
zWagtailPageTestCase.setUpClassc                 C   s   ||  kS r   )allowed_subpage_models)selfparent_modelchild_modelr   r   r   _testCanCreateAt   s    z$WagtailPageTestCase._testCanCreateAtNc                 C   sB   |  ||s>| |d|jj|jj|jj|jjf }| |dS )z
        Assert a particular child Page type can be created under a parent
        Page type. ``parent_model`` and ``child_model`` should be the Page
        classes being tested.
        z$Can not create a %s.%s under a %s.%sNr    _formatMessage_meta	app_label
model_namefailureExceptionr   r   r   msgr   r   r   assertCanCreateAt"   s    
z%WagtailPageTestCase.assertCanCreateAtc                 C   sB   |  ||r>| |d|jj|jj|jj|jjf }| |dS )z
        Assert a particular child Page type can not be created under a parent
        Page type. ``parent_model`` and ``child_model`` should be the Page
        classes being tested.
        z Can create a %s.%s under a %s.%sNr!   r'   r   r   r   assertCanNotCreateAt5   s    
z(WagtailPageTestCase.assertCanNotCreateAtTc                 C   s  |  |j| d|kr.d|kr.t|d |d< |r:d|d< td|jj|jj|jgd}| jj	||dd}|j
dkr| |d	|jj|jj|j
f }| ||jg kr4d
|jkr| |d}| ||jd
 }|js| |d}| |ddd t|j D }	| |d|jj|jj|	f }| ||rLtd|jgd}
ntdtjd jgd}
|j|
dfgkr| |d|jj|jj|
|jf }| |dS )aJ  
        Assert that a child of the given Page type can be created under the
        parent, using the supplied POST data.

        ``parent`` should be a Page instance, and ``child_model`` should be a
        Page subclass. ``data`` should be a dict that will be POSTed at the
        Wagtail admin Page creation method.
        Zslugtitleaction-publishzwagtailadmin_pages:add)argsT)follow   zCreating a %s.%s returned a %dformz Creating a page failed unusuallyz,Creating a page failed for an unknown reason
c                 s   s$   | ]\}}d  |d|V  qdS )z  {}:
    {}z
    N)formatjoin).0fielderrorsr   r   r   	<genexpr>u   s   z6WagtailPageTestCase.assertCanCreate.<locals>.<genexpr>z1Validation errors found when creating a %s.%s:
%sZwagtailadmin_explorewagtailadmin_pages:editpk.  zQCreating a page %s.%s didn't redirect the user to the expected page %s, but to %sN)r)   Zspecific_classr   r
   r#   r$   r%   r9   clientpoststatus_coder"   r&   Zredirect_chaincontextr6   r3   sorteditemsr   objectsZorder_bylast)r   parentr   datar(   publishZadd_urlresponser0   r6   Zexpected_urlr   r   r   assertCanCreateH   sx    	
	



 


 
z#WagtailPageTestCase.assertCanCreatec                 C   s    | j t| t||d dS )a   
        Test that the only page types that can be created under
        ``parent_model`` are ``child_models``.

        The list of allowed child models may differ from those set in
        ``Page.subpage_types``, if the child models have set
        ``Page.parent_page_types``.
        r(   N)assertEqualsetr   )r   r   Zchild_modelsr(   r   r   r   assertAllowedSubpageTypes   s
    	
  z-WagtailPageTestCase.assertAllowedSubpageTypesc                 C   s    | j t| t||d dS )a"  
        Test that the only page types that ``child_model`` can be created under
        are ``parent_models``.

        The list of allowed parent models may differ from those set in
        ``Page.parent_page_types``, if the parent models have set
        ``Page.subpage_types``.
        rH   N)rI   rJ   Zallowed_parent_page_models)r   r   Zparent_modelsr(   r   r   r   assertAllowedParentPageTypes   s
    	
  z0WagtailPageTestCase.assertAllowedParentPageTypes/)page
route_pathr(   c           	      C   s   | | j}|dkr,|dd |d }| }|dkrb| |dt|j||f }| |dd |	dD }z|j
jj| j|\}}}W n> tk
r   | |d|t|j||d }| |Y nX dS )z
        Asserts that ``page`` can be routed to without raising a ``Http404`` error.

        For page types with multiple routes, you can use ``route_path`` to specify an alternate route to test.
        rM   NzKFailed to route to "%s" for %s "%s". The page does not belong to any sites.c                 S   s   g | ]}|r|qS r   r   )r4   	componentr   r   r   
<listcomp>   s      z<WagtailPageTestCase.assertPageIsRoutable.<locals>.<listcomp>zqFailed to route to "%(route_path)s" for %(page_type)s "%(page)s". A Http404 was raised for path: "%(full_path)s".)rO   	page_typerN   	full_path)get_urlr   rstriplstripZget_siter"   type__name__r&   splitZ	root_pageZ	localizedZspecificZrouter   )	r   rN   rO   r(   pathsiteZpath_componentsr-   kwargsr   r   r   assertPageIsRoutable   s<    

 
z(WagtailPageTestCase.assertPageIsRoutableF)rN   rO   
query_data	post_datauser
accept_404accept_redirectr(   c	                 C   sl  |r| j |t || j}	|dkr>|	dd |d }	i }
|dk	rfd|i}
|rft|dd|
d< zz.|dkr| j j	|	|d}n| j j
|	f|
}W nL tk
r } z.| |d|t|j||d	 }| |W 5 d}~X Y nX W 5 |r| j   X |jd
ks6|r|jdks6|r(|jdks6t|tjr:dS | |d|t|j||j|	d }| |dS )aE  
        Asserts that ``page`` can be rendered without raising a fatal error.

        For page types with multiple routes, you can use ``route_path`` to specify an alternate route to test.

        When ``post_data`` is provided, the test makes a ``POST`` request with ``post_data`` in the request body. Otherwise, a ``GET`` request is made.

        When supplied, ``query_data`` is converted to a querystring and added to the request URL (regardless of whether ``post_data`` is provided).

        When ``user`` is provided, the test is conducted with them as the active user.

        By default, the assertion will fail if the request to the page URL results in a 301, 302 or 404 HTTP response. If you are testing a page/route
        where a 404 response is expected, you can use ``accept_404=True`` to indicate this, and the assertion will pass when encountering a 404. Likewise,
        if you are testing a page/route where a redirect response is expected, you can use `accept_redirect=True` to indicate this, and the assertion will
        pass when encountering 301 or 302.
        rM   NrD   T)doseqQUERYSTRINGrD   zMFailed to render route "%(route_path)s" for %(page_type)s "%(page)s":
%(exc)s)rO   rR   rN   excr/   i  )i-  r:   zFailed to render route "%(route_path)s" for %(page_type)s "%(page)s":
A HTTP %(code)s response was received for path: "%(full_path)s".)rO   rR   rN   coderS   )r;   force_loginAUTH_BACKENDrT   r   rU   rV   r   logoutgetr<   	Exceptionr"   rW   rX   r&   r=   
isinstancer   Z	MagicMock)r   rN   rO   r^   r_   r`   ra   rb   r(   rZ   Zpost_kwargsresper   r   r   assertPageIsRenderable   sj    
 
z*WagtailPageTestCase.assertPageIsRenderable)rN   r_   r`   r(   c           	      C   s  |r<| | sX| |dt|j||d  | |nt| dsR| d| _| j}| j	
|t tdd|jid}z| j	|}W nT tk
r } z6| j	  | |dt|j||d	 }| |W 5 d
}~X Y nX |jdkr| j	  | |dt|j||jd }| ||d
k	r.|}nt|j dd}d|d< zdz| j	|| W nL tk
r } z,| |dt|j||d	 }| |W 5 d
}~X Y nX W 5 |  | j	  X d
S )a  
        Asserts that the page edit view works for ``page`` without raising a fatal error.

        When ``user`` is provided, the test is conducted with them as the active user. Otherwise, a superuser is created and used for the test.

        After a successful ``GET`` request, a ``POST`` request is made with field data in the request body. If ``post_data`` is provided, that will be used for this purpose. If not, this data will be extracted from the ``GET`` response HTML.
        zeFailed to load edit view for %(page_type)s "%(page)s":
User "%(user)s" have insufficient permissions.)rR   rN   r`   _pageiseditable_superuserZassertpageiseditabler8   page_idr\   zFFailed to load edit view via GET for %(page_type)s "%(page)s":
%(exc)s)rR   rN   rf   Nr/   zqFailed to load edit view via GET for %(page_type)s "%(page)s":
Received response with HTTP status code: %(code)s.)rR   rN   rg   page-edit-formZform_id r,   zGFailed to load edit view via POST for %(page_type)s "%(page)s":
%(exc)s)Zpermissions_for_userZcan_editr"   rW   rX   r&   hasattrcreate_superuserrq   r;   rh   ri   r
   idrk   rl   rj   r=   r   contentdecodesaver<   )	r   rN   r_   r`   r(   rZ   rF   ro   Zdata_to_postr   r   r   assertPageIsEditable)  sz    	


	

  z(WagtailPageTestCase.assertPageIsEditablerv   )rN   moder_   r`   r(   c                 C   s  |s t | ds| d| _| j}| j|t |dkrftdd|jid}| j|j	
 }t|dd}td	d|jid}zB| jj||d
| d}	| |	jd | |	j	
 ddd W nX tk
r }
 z8| j  | |dt|j|||
d }| |W 5 d}
~
X Y nX zlz| jj|d|id W nN tk
r| }
 z.| |dt|j|||
d }| |W 5 d}
~
X Y nX W 5 | j  X dS )a  
        Asserts that the page preview view can be loaded for ``page`` without raising a fatal error.

        For page types that support multiple preview modes, ``mode`` can be used to specify the preview mode to be tested.

        When ``user`` is provided, the test is conducted with them as the active user. Otherwise, a superuser is created and used for the test.

        To load the preview, the test client needs to make a ``POST`` request including all required field data in the request body.
        If ``post_data`` is provided, that will be used for this purpose. If not, the method will attempt to extract this data from the page edit view.
        _pageispreviewable_superuserZassertpageispreviewableNr8   rr   rs   rt   ru   z"wagtailadmin_pages:preview_on_editzmode=)rD   rd   r/   T)Zis_validZis_availablezQFailed to load preview for %(page_type)s "%(page)s" with mode="%(mode)s":
%(exc)s)rR   rN   r~   rf   r~   re   )rw   rx   r   r;   rh   ri   r
   ry   rk   rz   r{   r   r<   rI   r=   ZassertJSONEqualrl   rj   r"   rW   rX   r&   )r   rN   r~   r_   r`   r(   Z	edit_pathhtmlZpreview_pathrF   ro   r   r   r   assertPageIsPreviewablez  sl    
   


 z+WagtailPageTestCase.assertPageIsPreviewable)N)N)NT)N)N)rM   N)rM   NNNFFN)NNN)rv   NNN)rX   
__module____qualname____doc__classmethodr   r    r)   r*   rG   rK   rL   r   r   strr]   r   r   r   boolrp   r}   r   __classcell__r   r   r   r   r      sj   


L

  -       T   T    r   c                       s   e Zd Z fddZ  ZS )WagtailPageTestsc                    s   t    |   d S r   )r   setUplogin)r   r   r   r   r     s    
zWagtailPageTests.setUp)rX   r   r   r   r   r   r   r   r   r     s   r   N) typingr   r   r   Zunittestr   Zdjango.confr   Zdjango.contrib.auth.base_userr   Zdjango.httpr   Zdjango.testr	   Zdjango.urlsr
   Zdjango.utils.httpr   Zdjango.utils.textr   Zwagtail.coreutilsr   Zwagtail.modelsr   Z	form_datar   Zwagtail_testsr   ZAUTHENTICATION_BACKENDSri   r   r   r   r   r   r   <module>   s$   
   4