o
    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 qWd 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namewagtailadminaccess_admin)content_type__app_labelcodename)addchangepublish)grouppagepermission_type)r   objectsget	root_pager   
hello_page	add_child
about_pagecreate_superuserr#   create_usereditorr   createpermissionsr.   r   groupsr   )selfsub_editorsr3    rB   b/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/admin/tests/test_audit_log.pysetUp   s<   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 = |j| jd |j| jdd |j| jdd t	|t	j
d	}|j| jd t	j|_|j| jd |  W d    d S 1 slw   Y  d S )
NTuser
log_action   )secondsrF   r   zwagtail.lockzwagtail.unlock)r2   restriction_type)save_revisionr<   r
   nowr   
go_live_atr0   r   saver   LOGINPASSWORDrK   r#   delete)r@   r2   revisionrestrictionrB   rB   rC   _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kwargsrJ      zwagtailadmin/pages/history.htmlz!wagtailadmin/generic/listing.htmlwagtailadmin_explore_rootRoot)urllabelwagtailadmin_exploreargsz!Welcome to your new Wagtail site!zHello world! (simple page) History)r]   r^   sublabel)r	   r7   r   loginr#   clientr5   assertEqualstatus_codeassertTemplateUsedr6   assertBreadcrumbsItemsRenderedr   )r@   history_urlresponseitemsrB   rB   rC   test_simpleS   s.   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 )NrV   rW   rX   rJ   rZ   CreatedrH   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#   )rU   r7   r	   r   re   r<   rf   r5   rg   rh   assertContainsr@   rk   rl   rB   rB   rC   test_page_historyt   sD   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 )NrJ   rV   rW   rX   z(?action=wagtail.edit&action=wagtail.lockrZ   rp   r   countrq   rr   rs   rt   z%[data-w-active-filter-id="id_action"]c                 S   s   g | ]	}|j d ddqS ) T)	separatorstrip)get_text).0filterrB   rB   rC   
<listcomp>   s    z?TestAuditLogAdmin.test_page_history_filters.<locals>.<listcomp>zAction: EditzAction: Lock)re   r<   rU   r7   r	   r   rf   r5   rg   rh   rx   assertNotContainsget_soupr   selectassertCountEqual)r@   rk   rl   soupactive_filtersrB   rB   rC   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 )NrJ   rV   rW   rX   wagtail.comments.createcomment{   r   A comment that was addedr   contentpathtextr   actionrF   rS   datawagtail.comments.editA comment that was editedrZ   rp   r   r{   rq   rr   rs   rt   z?is_commenting_action=truez?is_commenting_action=false)re   r<   rU   r7   r	   r   r   latest_revisionrf   r5   rg   rh   rx   r   ry   rB   rB   rC   !test_is_commenting_action_filters   sl   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 )NTrE   rJ   !wagtailadmin_reports:site_historyrZ   r    rp   r   DeletedzNo log entries foundEditorsr(      ro   )rU   r7   r9   rL   r#   rR   r	   re   r<   rf   r5   rg   rh   r   rx   r?   r.   r   r4   )r@   site_history_urlrl   rB   rB   rC   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)rJ   rV   rW   rX   r   )rU   r7   r<   pkrR   re   r#   rf   r5   r	   r   rx   )r@   expected_deleted_stringrl   rB   rB   rC   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 )Npurge_revisionsr   )daysstdoutrV   rW   rX   rJ   rZ   ro   rH   rp   r   rq   rr   rs   rt   )rU   r7   r   r   r	   r   re   r<   rf   r5   rg   rh   rx   ry   rB   rB   rC   &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 )NrJ   zwagtailadmin_pages:editr`   rZ   rV   )r7   rL   re   r<   rf   r5   r	   r   rg   rh   rx   )r@   rl   rk   rB   rB   rC   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g d d S )NrJ   z	New page!zSome contentzhello-world-reduxaction-publishr   r   r   r   zwagtailadmin_pages:addtests
simplepager`   TfollowrZ   )path__startswithr   r2   r   flat)wagtail.createzwagtail.editwagtail.publish)re   r#   rf   postr	   r6   r   rg   rh   r   r4   r5   pathassertListEquallistr   r   values_listorder_by)r@   	post_datarl   rW   rB   rB   rC   *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|jfddddddd	d
}| 	|j
d tjj| j djdd	dd}| t|g d d S )NrJ   z#wagtailadmin_pages:revisions_revertr`   zHello World!zanother hellor   r   r   Tr   rZ   r   r   r   )r   r   zwagtail.renamezwagtail.revert)r7   rL   r<   re   r#   rf   r   r	   r   rg   rh   r   r4   r   r   r   r   r   )r@   rS   rl   entriesrB   rB   rC   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rG   rJ   'wagtailadmin_pages:revisions_unscheduler`   rV   rW   rX   rZ   z#Page unscheduled for publishing at )r
   rM   r   r   USE_TZ	localtimer7   rN   rL   r0   re   r<   rf   r   r	   r   assertRedirectsr5   rg   rh   rx   r   )r@   rN   rS   rl   rk   rB   rB   rC   /test_page_history_after_unscheduled_publication  s4   
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
r-t|}||_|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   rJ   r   r`   rV   rW   rX   rZ   z	Revision z from z  unscheduled from publishing at .)r   r7   r8   rL   r0   refresh_from_dbr
   rM   r   r   r   r   rN   re   r<   rf   r   r	   r   r   r5   rg   rh   rx   r   
created_at)r@   	test_pagerS   rN   rl   rk   rB   rB   rC   ,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 d    n1 s-w   Y  | | j | d | j| W d    n1 sNw   Y  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 d    d S 1 sw   Y  d S )NrJ   rV   rW   rX         r   r   r   r   r   r   r   r   r   )re   r<   r	   r7   r   rf   r5   assertNumQueriesrU   r   r   )r@   rk   rB   rB   rC   test_num_queries  sP   "z"TestAuditLogAdmin.test_num_queriesN)__name__
__module____qualname__base_breadcrumb_itemsrD   rU   rn   rz   r   r   r   r   r   r   r   r   r   r   r   rB   rB   rB   rC   r      s"    #!*C&
 'r   N)#datetimer   ior   django.confr   django.contrib.auth.modelsr   r   django.core.managementr   django.testr   django.urlsr	   django.utilsr
   	freezegunr   wagtail.log_actionsr   wagtail.modelsr   r   r   r   wagtail.test.testapp.modelsr   wagtail.test.utilsr   !wagtail.test.utils.template_testsr   wagtail.utils.timestampsr   r   rB   rB   rB   rC   <module>   s     