U
    {h%                     @   s   d dl mZ d dlmZ 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 G dd deeZG dd deeZdS )    )mock)
Permission)HttpRequestHttpResponse)TestCase)reverse)gettext_lazy)Page)page_unpublished)
SimplePage)WagtailTestUtilsc                   @   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )TestPageUnpublishc                 C   s@   |   | _tjjdd| _tddddd| _| jj| jd d S )	N   idHello world!hello-worldhelloT)titleslugcontentliveinstance)	loginuserr	   objectsget	root_pager   page	add_childself r#   u/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/admin/tests/pages/test_unpublish_page.pysetUp   s    
zTestPageUnpublish.setUpc                 C   s8   | j td| jjfd}| |jd | |d dS )z\
        This tests that the unpublish view responds with an unpublish confirm page
        wagtailadmin_pages:unpublishargs   )wagtailadmin/pages/confirm_unpublish.htmlN)clientr   r   r   r   assertEqualstatus_codeassertTemplateUsedr"   responser#   r#   r$   test_unpublish_view   s
    z%TestPageUnpublish.test_unpublish_viewc                 C   s&   | j tddd}| |jd dS )z_
        This tests that the unpublish view returns an error if the page id is invalid
        r&   )i90  r'   i  N)r+   r   r   r,   r-   r/   r#   r#   r$   #test_unpublish_view_invalid_page_id*   s    
z5TestPageUnpublish.test_unpublish_view_invalid_page_idc                 C   sX   d| j _| j jtjjddd | j   | jt	d| j
jfd}| |jd dS )	zf
        This tests that the unpublish view doesn't allow users without unpublish permissions
        FZwagtailadminZaccess_admin)Zcontent_type__app_labelcodenamer&   r'   i.  N)r   Zis_superuserZuser_permissionsaddr   r   r   saver+   r   r   r   r,   r-   r/   r#   r#   r$   #test_unpublish_view_bad_permissions6   s     
z5TestPageUnpublish.test_unpublish_view_bad_permissionsc              	   C   s   t  }t| z| jtd| jj	fd}| 
|td| jj	fd | tjj| jj	dj | |jd |jd d }| |d | jj | |d	 | j | |d	 | jj W 5 t| X d
S )z[
        This posts to the unpublish view and checks that the page was unpublished
        r&   r'   wagtailadmin_explorer      r   r   Zsenderr   N)r   Z	MagicMockr
   connectZ
disconnectr+   postr   r   r   assertRedirectsr   assertFalser   r   r   r   r,   Z
call_countZ
mock_callsZspecific_classassertIsInstance)r"   Zmock_handlerr0   Z	mock_callr#   r#   r$   test_unpublish_view_postK   s"    
 z*TestPageUnpublish.test_unpublish_view_postc              	      s    fdd}  d|& i } jtd jjfd|}W 5 Q R X  |jd  |jd  j	    jj
td d S )	Nc                    s&     | t  |j jj tdS NzOverridden!r=   r   r,   r   r   r   requestr   r!   r#   r$   	hook_funcl   s    z>TestPageUnpublish.test_after_unpublish_page.<locals>.hook_funcZafter_unpublish_pager&   r'   r)      Overridden!Zdraftregister_hookr+   r:   r   r   r   r,   r-   r   Zrefresh_from_dbZstatus_string_r"   rC   Z	post_datar0   r#   r!   r$   test_after_unpublish_pagek   s     
z+TestPageUnpublish.test_after_unpublish_pagec              	      s    fdd}  d|& i } jtd jjfd|}W 5 Q R X  |jd  |jd  j	    jj
td d S )	Nc                    s&     | t  |j jj tdS r?   r@   rA   r!   r#   r$   rC      s    z?TestPageUnpublish.test_before_unpublish_page.<locals>.hook_funcZbefore_unpublish_pager&   r'   r)   rD   r   rE   rH   r#   r!   r$   test_before_unpublish_page~   s     
z,TestPageUnpublish.test_before_unpublish_pagec                 C   sD   | j td| jjfd}| |jd | |d | |d dS )z
        This tests that the unpublish view responds with an unpublish confirm page that does not contain the form field 'include_descendants'
        r&   r'   r)   r*   name="include_descendants"N)	r+   r   r   r   r   r,   r-   r.   ZassertNotContainsr/   r#   r#   r$   test_unpublish_descendants_view   s    z1TestPageUnpublish.test_unpublish_descendants_viewN)__name__
__module____qualname__r%   r1   r2   r6   r>   rI   rJ   rL   r#   r#   r#   r$   r      s    r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
%TestPageUnpublishIncludingDescendantsc                 C   sx   |   | _tjjdd| _| jjtddddddd	| _| jjtd
dddddd	| _	| jjtddddddd	| _
d S )Nr   r   r   r   r   TF)r   r   r   r   Zhas_unpublished_changesr   z
Child pagez
child-pagezAnother Child pagezanother-child-page)r   r   r	   r   r   r   r    r   	test_pagetest_child_pagetest_another_child_pager!   r#   r#   r$   r%      s:    

z+TestPageUnpublishIncludingDescendants.setUpc                 C   sD   | j td| jjfd}| |jd | |d | |d dS )z
        This tests that the unpublish view responds with an unpublish confirm page that contains the form field 'include_descendants'
        r&   r'   r)   r*   rK   N)	r+   r   r   rQ   r   r,   r-   r.   ZassertContainsr/   r#   r#   r$   rL      s    zETestPageUnpublishIncludingDescendants.test_unpublish_descendants_viewc                 C   s   | j td| jjfdddi}| |td| jjfd | tj	j
| jjdj | tj	j
| jjdj | tj	j
| jjdj dS )zp
        This posts to the unpublish view and checks that the page and its descendants were unpublished
        r&   r'   Zinclude_descendantsonr7   r   N)r+   r:   r   rQ   r   r;   r   r<   r   r   r   r   rR   rS   r/   r#   r#   r$   )test_unpublish_include_children_view_post   s     zOTestPageUnpublishIncludingDescendants.test_unpublish_include_children_view_postc                 C   s   | j td| jjfdi }| |td| jjfd | tj	j
| jjdj | tj	j
| jjdj | tj	j
| jjdj dS )zx
        This posts to the unpublish view and checks that the page was unpublished but its descendants were not
        r&   r'   r7   r   N)r+   r:   r   rQ   r   r;   r   r<   r   r   r   r   Z
assertTruerR   rS   r/   r#   r#   r$   -test_unpublish_not_include_children_view_post   s      zSTestPageUnpublishIncludingDescendants.test_unpublish_not_include_children_view_postN)rM   rN   rO   r%   rL   rU   rV   r#   r#   r#   r$   rP      s   %rP   N)Zunittestr   Zdjango.contrib.auth.modelsr   Zdjango.httpr   r   Zdjango.testr   Zdjango.urlsr   Zdjango.utils.translationr   rG   Zwagtail.modelsr	   Zwagtail.signalsr
   Zwagtail.test.testapp.modelsr   Zwagtail.test.utilsr   r   rP   r#   r#   r#   r$   <module>   s    