a
    Dg
F                     @   s   d dl 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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__ \/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/test/utils/page_tests.pyr      s    
zWagtailPageTestCase.setUpClassc                 C   s   ||  v 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|vr.d|v 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v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$   r8   clientpoststatus_coder!   r%   Zredirect_chaincontextr5   r2   sorteditemsr   ZobjectsZorder_bylast)r   parentr   datar'   publishZadd_urlresponser/   r5   Zexpected_urlr   r   r   assertCanCreateH   st    	
	







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``.
        rF   N)rG   rH   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u rb| |dt|j||f }| |dd |	dD }z|j
jj| j|\}}}W n< ty   | |d|t|j||d }| |Y n0 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.
        rK   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   )r3   	componentr   r   r   
<listcomp>       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".)rM   	page_typerL   	full_path)get_urlr   rstriplstripZget_siter!   type__name__r%   splitZ	root_pageZ	localizedZspecificZrouter   )	r   rL   rM   r'   pathsiteZpath_componentsr,   kwargsr   r   r   assertPageIsRoutable   s:    


z(WagtailPageTestCase.assertPageIsRoutableF)rL   rM   
query_data	post_datauser
accept_404accept_redirectr'   c	                 C   s  |r| j |t || j}	|dkr>|	dd |d }	i }
|durfd|i}
|rft|dd|
d< zz2|du r| j j|	|d}n| j j	|	fi |
}W nN t
y } z6| |d|t|j||d	 }| |W Y d}~n
d}~0 0 W |r| j   n|r| j   0 |jd
ksL|r,|jdksL|r>|jdv sLt|tjrPd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.
        rK   NrB   T)doseqQUERYSTRINGrB   zMFailed to render route "%(route_path)s" for %(page_type)s "%(page)s":
%(exc)s)rM   rQ   rL   excr.   i  )i-  r9   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".)rM   rQ   rL   coderR   )r:   force_loginAUTH_BACKENDrS   r   rT   rU   r
   getr;   	Exceptionr!   rV   rW   r%   logoutr<   
isinstancer   Z	MagicMock)r   rL   rM   r]   r^   r_   r`   ra   r'   rY   Zpost_kwargsresper   r   r   assertPageIsRenderable   sn    
"
z*WagtailPageTestCase.assertPageIsRenderable)rL   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 nV ty } z>| j	  | |dt|j||d	 }| |W Y d
}~n
d
}~0 0 |jdkr | j	  | |dt|j||jd }| ||d
ur0|}nt|j dd}d|d< zxz| j	|| W nN ty } z4| |dt|j||d	 }| |W Y d
}~n
d
}~0 0 W |  | j	  n|  | j	  0 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.)rQ   rL   r_   _pageiseditable_superuserZassertpageiseditabler7   page_idr[   zFFailed to load edit view via GET for %(page_type)s "%(page)s":
%(exc)s)rQ   rL   re   Nr.   zqFailed to load edit view via GET for %(page_type)s "%(page)s":
Received response with HTTP status code: %(code)s.)rQ   rL   rf   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!   rV   rW   r%   hasattrcreate_superuserrp   r:   rg   rh   r	   idri   rj   rk   r<   r   contentdecoder;   save)	r   rL   r^   r_   r'   rY   rD   rn   Zdata_to_postr   r   r   assertPageIsEditable)  s|    	

 
	


"z(WagtailPageTestCase.assertPageIsEditableru   )rL   moder^   r_   r'   c                 C   s  |s t | ds| d| _| j}| j|t |du 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 nZ ty }
 z@| j  | |dt|j|||
d }| |W Y d}
~
n
d}
~
0 0 zxz| jj|d|id W nP ty }
 z6| |dt|j|||
d }| |W Y d}
~
n
d}
~
0 0 W | j  n| j  0 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assertpageispreviewableNr7   rq   rr   rs   rt   z"wagtailadmin_pages:preview_on_editzmode=)rB   rc   r.   T)Zis_validZis_availablezQFailed to load preview for %(page_type)s "%(page)s" with mode="%(mode)s":
%(exc)s)rQ   rL   r}   re   r}   rd   )rv   rw   r~   r:   rg   rh   r	   rx   ri   ry   rz   r   r;   rG   r<   ZassertJSONEqualrj   rk   r!   rV   rW   r%   )r   rL   r}   r^   r_   r'   Z	edit_pathhtmlZpreview_pathrD   rn   r   r   r   assertPageIsPreviewablez  sf    



 
"z+WagtailPageTestCase.assertPageIsPreviewable)N)N)NT)N)N)rK   N)rK   NNNFFN)NNN)ru   NNN)rW   
__module____qualname____doc__classmethodr   r   r(   r)   rE   rI   rJ   r   r   strr\   dictr   r   boolro   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)rW   r   r   r   r   r   r   r   r   r     s   r   N)typingr   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_BACKENDSrh   r   r   r   r   r   r   <module>   s$   
   4