U
    {hJ                     @   s   d dl mZ 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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! G dd deeeZ"dS )    )	timedelta)StringIO)settings)Group
Permission)call_command)TestCase)reverse)timezone)freeze_time)log)GroupPagePermissionPagePageLogEntryPageViewRestriction)
SimplePage)WagtailTestUtils)AdminTemplateTestUtils)render_timestampc                   @   s   e Zd Zg 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d Zdd Zdd Zdd Zdd Zdd Zdd ZdS ) TestAuditLogAdminc                 C   s   t jjdd| _tddddd| _| jj| jd td	d
dd| _| jj| jd | jdddd| _	| j
dddd| _tjjdd}|jtjjddd | jj| dD ]}tjj|| j|d qd S )N   )idzHello world!hello-worldhelloF)titleslugcontentliveinstanceAboutaboutr   r   r   administratorzadministrator@email.compassword)usernameemailr$   
the_editorzthe_editor@example.comzSub editorsnameZwagtailadminZaccess_admin)Zcontent_type__app_labelcodename)addZchangepublish)grouppagepermission_type)r   objectsget	root_pager   
hello_page	add_child
about_pageZcreate_superuserr#   Zcreate_usereditorr   createZpermissionsr+   r   groupsr   )selfZsub_editorsr/    r:   j/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/admin/tests/test_audit_log.pysetUp   sD         zTestAuditLogAdmin.setUpc              	   C   s   |j | jdd t tdd |_|j | jdd}|j| jd tt tdd n |j| jd |j| jdd |j| jdd t	|t	j
d	}|j| jd t	j|_|j| jd |  W 5 Q R X d S )
NTuser
log_action   )secondsr>   r   zwagtail.lockzwagtail.unlock)r.   restriction_type)save_revisionr6   r
   nowr   
go_live_atr,   r   saver   ZLOGINZPASSWORDrC   r#   delete)r9   r.   revisionZrestrictionr:   r:   r;   _update_page:   s      zTestAuditLogAdmin._update_pagec                 C   s   t dd| jjid}| j| jd | j|}| |jd | 	|d | 	|d t dd	d
t d| j
jfddd
t d| jjfddd
ddddg}| ||j d S )Nwagtailadmin_pages:historypage_idkwargsrB      zwagtailadmin/pages/history.htmlz!wagtailadmin/generic/listing.htmlZwagtailadmin_explore_rootZRoot)urllabelZwagtailadmin_exploreargsz!Welcome to your new Wagtail site!zHello world! (simple page) ZHistory)rP   rQ   Zsublabel)r	   r3   r   loginr#   clientr1   assertEqualstatus_codeZassertTemplateUsedr2   ZassertBreadcrumbsItemsRenderedr   )r9   history_urlresponseitemsr:   r:   r;   test_simpleS   s0     
zTestAuditLogAdmin.test_simplec                 C   s   |  | j tdd| jjid}| j| jd | j|}| |j	d | 
|dd | 
|dd	 | 
|d
d | 
|dd | 
|dd | 
|dd | 
|d | 
|d | 
|d | 
|dd | 
|dd | 
|dd	 d S )NrK   rL   rM   rB   rO   Createdr@   Draft savedr   LockedUnlockedPage scheduled for publishing	PublishedzQAdded the &#x27;Private, accessible to any logged-in users&#x27; view restrictionzVUpdated the view restriction to &#x27;Private, accessible with a shared password&#x27;zSRemoved the &#x27;Private, accessible with a shared password&#x27; view restrictionsystem   r'   	   r#   )rJ   r3   r	   r   rU   r6   rV   r1   rW   rX   assertContainsr9   rY   rZ   r:   r:   r;   test_page_historyt   sR     
      z#TestAuditLogAdmin.test_page_historyc                 C   s   | j | jd | | j tdd| jjid}| j| d}| |j	d | j
|ddd	 | 
|d
 | |d | |d | |d | |j}|d}| dd |D ddg d S )NrB   rK   rL   rM   z(?action=wagtail.edit&action=wagtail.lockrO   r^   r   countr_   r`   ra   rb   z%[data-w-active-filter-id="id_action"]c                 S   s   g | ]}|j d ddqS ) T)	separatorstrip)Zget_text).0filterr:   r:   r;   
<listcomp>   s     z?TestAuditLogAdmin.test_page_history_filters.<locals>.<listcomp>zAction: EditzAction: Lock)rU   r6   rJ   r3   r	   r   rV   r1   rW   rX   rf   assertNotContainsZget_soupr   selectassertCountEqual)r9   rY   rZ   ZsoupZactive_filtersr:   r:   r;   test_page_history_filters   s*     

z+TestAuditLogAdmin.test_page_history_filtersc              
   C   s  | j | jd | | j tdd| jjid}t| jd| j| jjdddd	d
id t| jd| j| jjddddd
id | j	|}| 
|jd | j|ddd | |d | |d | |d | |d | j	|d }| 
|jd | |d	 | |d | |d | |d | |d | |d | |d | j	|d }| 
|jd | |d	 | |d | |d | |d | |d | |d | |d d S )NrB   rK   rL   rM   wagtail.comments.createcomment{   r   A comment that was addedr   Zcontentpathtextr   actionr>   rI   datawagtail.comments.editA comment that was editedrO   r^   r   ri   r_   r`   ra   rb   z?is_commenting_action=truez?is_commenting_action=false)rU   r6   rJ   r3   r	   r   r   latest_revisionrV   r1   rW   rX   rf   rq   rg   r:   r:   r;   !test_is_commenting_action_filters   sn     
z3TestAuditLogAdmin.test_is_commenting_action_filtersc                 C   s4  |  | j | jj| jdd | jj| jd td}| j| jd | j	
|}| |jd | |d | |dd | |d	 | jj| jd | j	
|}| |d
 | jjtjj
dd | j	
|}| |dd | |dd | |d	d | j| jd | j	
|}| |dd | |d	d d S )NTr=   rB   !wagtailadmin_reports:site_historyrO   r    r^   r   DeletedzNo log entries foundZEditorsr(   rd   r]   )rJ   r3   r5   rD   r#   rH   r	   rU   r6   rV   r1   rW   rX   rq   rf   r8   r+   r   r0   )r9   Zsite_history_urlrZ   r:   r:   r;   test_site_history   s,    z#TestAuditLogAdmin.test_site_historyc                 C   s|   |  | j d| jj d}| j  | j| jd | jt	dd| jj
id}| || | jt	d}| || d S )Nzuser z
 (deleted)rB   rK   rL   rM   r   )rJ   r3   r6   pkrH   rU   r#   rV   r1   r	   r   rf   )r9   Zexpected_deleted_stringrZ   r:   r:   r;   test_history_with_deleted_user"  s    
 
z0TestAuditLogAdmin.test_history_with_deleted_userc                 C   s   |  | j tddt d tdd| jjid}| j| jd | j	|}| 
|jd | |d	d
 | |dd | |dd
 | |dd
 | |dd
 | |dd
 d S )NZpurge_revisionsr   )daysstdoutrK   rL   rM   rB   rO   r]   r@   r^   r   r_   r`   ra   rb   )rJ   r3   r   r   r	   r   rU   r6   rV   r1   rW   rX   rf   rg   r:   r:   r;   &test_page_history_after_revision_purge6  s     
z8TestAuditLogAdmin.test_page_history_after_revision_purgec                 C   sb   | j   | j| jd | jtd| j jgd}| |j	d td| j jgd}| 
|| d S )NrB   wagtailadmin_pages:editrR   rO   rK   )r3   rD   rU   r6   rV   r1   r	   r   rW   rX   rf   )r9   rZ   rY   r:   r:   r;   test_edit_form_has_history_linkJ  s    
z1TestAuditLogAdmin.test_edit_form_has_history_linkc                 C   s   | j | jd ddddd}| jjtddd	| jjfd
|dd}| |jd t	j
j| jjddj}| ttj
j|djdddddddg d S )NrB   z	New page!zSome contentzhello-world-reduxaction-publish)r   r   r   r   zwagtailadmin_pages:addZtestsZ
simplepagerR   TfollowrO   )Zpath__startswithr   r.   r|   Zflatwagtail.createzwagtail.editwagtail.publish)rU   r#   rV   postr	   r2   r   rW   rX   r   r0   r1   pathassertListEquallistr   ro   values_listorder_by)r9   Z	post_datarZ   rL   r:   r:   r;   *test_create_and_publish_logs_revision_saveT  s<      z<TestAuditLogAdmin.test_create_and_publish_logs_revision_savec                 C   s   | j j| jd}| j j| jd | j| jd | jjtd| j jfdddd|jddd	d
}| 	|j
d tjj| j djdd	dd}| t|ddddg d S )NrB   r   rR   zHello World!zanother hellor   r   )r   r   r   rI   r   Tr   rO   r   r|   r   r   r   zwagtail.renamezwagtail.revert)r3   rD   r6   rU   r#   rV   r   r	   r   rW   rX   r   r0   ro   r   r   r   r   )r9   rI   rZ   entriesr:   r:   r;   2test_revert_and_publish_logs_reversion_and_publishs  s2     
zDTestAuditLogAdmin.test_revert_and_publish_logs_reversion_and_publishc                 C   s   t  tdd }tjr"t |}|| j_| jjdd}|	  | j
| jd | jtd| jj|jfd}tdd	| jjid
}| || | j|}| |jd | |dt|  d S )N   minutesTr?   rB   'wagtailadmin_pages:revisions_unschedulerR   rK   rL   rM   rO   z#Page unscheduled for publishing at )r
   rE   r   r   USE_TZ	localtimer3   rF   rD   r,   rU   r6   rV   r   r	   r   assertRedirectsr1   rW   rX   rf   r   )r9   rF   rI   rZ   rY   r:   r:   r;   /test_page_history_after_unscheduled_publication  s6    
 
zATestAuditLogAdmin.test_page_history_after_unscheduled_publicationc              
   C   s  t dddd}| jj|d |jdd}|  |  t tdd	 }t	j
rZt|}||_|jdd}|  | j| jd
 | jtd|j|jfd}tdd|jid}| || | j|}| |jd | |d|j dt|j dt| d d S )Nr    r!   r   r"   r   Tr   r   r   rB   r   rR   rK   rL   rM   rO   z	Revision z from z  unscheduled from publishing at .)r   r3   r4   rD   r,   Zrefresh_from_dbr
   rE   r   r   r   r   rF   rU   r6   rV   r   r	   r   r   r1   rW   rX   rf   r   Z
created_at)r9   Z	test_pagerI   rF   rZ   rY   r:   r:   r;   ,test_page_history_after_unscheduled_revision  s@    

 "z>TestAuditLogAdmin.test_page_history_after_unscheduled_revisionc              
   C   s   | j | jd tdd| jjid}| j| | d | j| W 5 Q R X | | j | d | j| W 5 Q R X t	| jd| j| jj
dd	d
ddid t	| jd| j| jj
dd	d
ddid | | j | d | j| W 5 Q R X d S )NrB   rK   rL   rM         ru   rv   rw   r   rx   ry   r{   r~   r   )rU   r6   r	   r3   r   rV   r1   ZassertNumQueriesrJ   r   r   )r9   rY   r:   r:   r;   test_num_queries  sL     
z"TestAuditLogAdmin.test_num_queriesN)__name__
__module____qualname__Zbase_breadcrumb_itemsr<   rJ   r\   rh   rt   r   r   r   r   r   r   r   r   r   r   r:   r:   r:   r;   r      s    #!*C&
 'r   N)#datetimer   ior   Zdjango.confr   Zdjango.contrib.auth.modelsr   r   Zdjango.core.managementr   Zdjango.testr   Zdjango.urlsr	   Zdjango.utilsr
   Z	freezegunr   Zwagtail.log_actionsr   Zwagtail.modelsr   r   r   r   Zwagtail.test.testapp.modelsr   Zwagtail.test.utilsr   Z!wagtail.test.utils.template_testsr   Zwagtail.utils.timestampsr   r   r:   r:   r:   r;   <module>   s   