o
    he                     @   s  d dl Z d dl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mZmZmZmZmZ d dlmZ d dlm Z m!Z! d dl"m#Z# G dd de#eZ$G dd deZ%dd Z&G dd de#eZ'dS )    N)settings)get_user_model)ContentType)ValidationError)DjangoJSONEncoder)TestCase)timezone)freeze_time)LogActionRegistry)registry)PagePageLogEntryPageViewRestrictionTaskWorkflowWorkflowTask)ModelLogEntry)FullFeaturedSnippet
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 )TestAuditLogManagerc                 C   sr   | j dddd| _tjjdd| _| jjtddd	| jd
d| _t	jj
dd| _t	jj
dd| _tjt	| _d S )Nadministratorzadministrator@email.compassword)usernameemailr      )pkzSimple pagesimpleHellotitleslugcontentownerinstancez	snippet 1)textz	snippet 2)create_superuseruserr   objectsgetpage	add_childr   simple_pager   create	snippet_1	snippet_2r   get_for_modelsnippet_content_typeself r5   \/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/tests/test_audit_log.pysetUp   s    

zTestAuditLogManager.setUpc                 C   sz   t  }t| tjj| jd| jd}W d    n1 sw   Y  | |j	| jj	 | |j| j | |j
| d S Nwagtail.edit)r(   )r   nowr	   r   r)   
log_actionr+   r(   assertEqualcontent_type	timestampr4   r:   entryr5   r5   r6   test_log_action_for_page/   s   

z,TestAuditLogManager.test_log_action_for_pagec                 C   sx   t  }t| tjj| jd| jd}W d    n1 sw   Y  | |j	| j
 | |j| j | |j| d S r8   )r   r:   r	   r   r)   r;   r/   r(   r<   r=   r2   r>   r?   r5   r5   r6   test_log_action_for_snippet;   s   

z/TestAuditLogManager.test_log_action_for_snippetc                 C   s`   t j| jd t j| jd t jt}| | d | 	t
|t
t jj| jd d S )Nr9      r+   )r   r)   r;   r+   r-   r1   r   r<   countassertListEquallistfilterr4   entriesr5   r5   r6   test_get_for_page_modelG   s   z+TestAuditLogManager.test_get_for_page_modelc                 C   s`   t j| jd t j| jd t jt}| | d | 	t
|t
t jj| jd d S )Nr9   rC   )r=   )r   r)   r;   r/   r0   r1   r   r<   rE   rF   rG   rH   r2   rI   r5   r5   r6   test_get_for_snippet_modelQ   s   z.TestAuditLogManager.test_get_for_snippet_modelc                 C   s   |  tj| j d d S )Nr   )r<   r   r)   get_for_userr(   rE   r3   r5   r5   r6   test_get_for_user\   s   z%TestAuditLogManager.test_get_for_userc                 C   s   t j| jd t j| jd | jjtddd| jdd}t j|d t j| j}t	t jj
| jd}| | d | t	|| t| j}| | d | t	|| d S )	Nr9   zSimple page 2simple2r   r   r$   rD   rC   )r   r)   r;   r+   r-   r,   r   r(   for_instancerG   rH   r<   rE   rF   log_registryget_logs_for_instance)r4   other_simple_pagerJ   expected_entriesr5   r5   r6   test_get_for_page_instancea   s   
z.TestAuditLogManager.test_get_for_page_instancec                 C   s   t j| jd t j| jd t j| j}tt jj| j| jj	d}| 
| d | t|| t| j}| 
| d | t|| d S )Nr9   )r=   	object_idr   )r   r)   r;   r/   r0   rP   rG   rH   r2   r   r<   rE   rF   rQ   rR   )r4   rJ   rT   r5   r5   r6   test_get_for_snippet_instancev   s   
z1TestAuditLogManager.test_get_for_snippet_instanceN)__name__
__module____qualname__r7   rA   rB   rK   rL   rN   rU   rW   r5   r5   r5   r6   r      s    
r   c                   @   s   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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 )0TestAuditLogc                 C   s<   t jjdd| _| jjtddddd| _tj 	  d S )Nr   idHomepagehome2hellor    r!   r"   r$   )
r   r)   r*   	root_pager,   r   	home_pager   alldeleter3   r5   r5   r6   r7      s
   zTestAuditLog.setUpc                 C   s   |  tj d | jjtddddd}|  tj d tjd }|  |j	d	 |  |j
|j |  |j|j |  |j|  d S )
Nr   r   zmy-pageworldra   r$   r   r   wagtail.create)r<   r   r)   rE   rc   r,   r   order_bylastactionpage_idr]   r=   labelget_admin_display_title)r4   r+   	log_entryr5   r5   r6   test_page_create   s   zTestAuditLog.test_page_createc                 C   sJ   d| j _| j   | j jdd}| |j | tjjdd	 d d S )NT	the-aliasupdate_slugwagtail.publishrj   r   )
rc   livesavecreate_alias
assertTruer<   r   r)   rH   rE   )r4   aliasr5   r5   r6   ?test_alias_create_from_published_page_doesnt_log_publish_action   s   
zLTestAuditLog.test_alias_create_from_published_page_doesnt_log_publish_actionc                 C   s   | j   | tj d | j jdd | tj d | tjjdd d | j jdd | tjjdd d d S )Nr   Tr;   r   r9   rt   wagtail.revert)rc   save_revisionr<   r   r)   rE   rH   r3   r5   r5   r6   test_page_edit   s   
zTestAuditLog.test_page_editc                 C   sB   | j  }|  | tj d | tjjdd d d S )Nr   rs   rt   )rc   r}   publishr<   r   r)   rE   rH   r4   revisionr5   r5   r6   test_page_publish   s   
zTestAuditLog.test_page_publishc                 C   s>   | j jdd | j  }|  | tjjdd d d S )Nrp   rq   rs   rt   r   )	rc   rw   r}   r   r<   r   r)   rH   rE   r   r5   r5   r6   (test_page_publish_doesnt_log_for_aliases   s   
z5TestAuditLog.test_page_publish_doesnt_log_for_aliasesc                 C   s   | j  }d| j _| j   |  | tjjdd	 d | tjjdd	 d | j  }d| j _| j   |  | tj	 d | tjjdd	 d	 | tjjdd	 d d S )
Nz	Old titlers   rt   r   zwagtail.renamer   z	New title   rC   )
rc   r}   r    rv   r   r<   r   r)   rH   rE   r   r5   r5   r6   test_page_rename   s*   



zTestAuditLog.test_page_renamec                 C   s:   | j   | tj d | tjjdd d d S )Nr   wagtail.unpublishrt   )rc   	unpublishr<   r   r)   rE   rH   r3   r5   r5   r6   test_page_unpublish   s
   
z TestAuditLog.test_page_unpublishc                 C   s6   | j jdd | j   | tjjdd d d S )Nrp   rq   r   rt   r   )rc   rw   r   r<   r   r)   rH   rE   r3   r5   r5   r6   *test_page_unpublish_doesnt_log_for_aliases   s
   
z7TestAuditLog.test_page_unpublish_doesnt_log_for_aliasesc                 C   sB   | j  }| j   | j jd|d | tjjdd d d S )NTr;   previous_revisionr|   rt   r   )rc   r}   r<   r   r)   rH   rE   )r4   	revision1r5   r5   r6   test_revision_revert   s   

z!TestAuditLog.test_revision_revertc                 C   s   t j  t jdd }tjrt|}t|t jj}n|}|| j	_
| j	 }|  tjjdd}| | d | |d jd d |j | |d jd d tj|td	dd
  d S )Nr   dayswagtail.publish.schedulert   r   r   r]   
go_live_atcls)datetimer:   	timedeltar   USE_TZr   
make_aware	localtimeutcrc   r   r}   r   r   r)   rH   r<   rE   datar]   jsondumpsr   r4   r   expected_go_live_atr   log_entriesr5   r5   r6   test_revision_schedule_publish   s   

z+TestAuditLog.test_revision_schedule_publishc                 C   s   | j  }| j  }tjrttj tjdd | j _	ntj tjdd | j _	| j jd|d}|j
|d | ttjjddddd	g d S )
Nr   r   Tr   )r   rj   flatr   r|   )rc   r}   r   r   r   r   r   r:   r   r   r   rF   rG   r   r)   values_list)r4   r   	revision2schedule_revisionr5   r5   r6   test_revision_schedule_revert  s&   


z*TestAuditLog.test_revision_schedule_revertc                 C   s   t j  t jdd }tjrt|}t|t jj}n|}|| j	_
| j	 }|  d |_|jdgd tjjdd}| | d | |d jd d	 |j | |d jd d
 tj|tddd  | |d jd d  d S )Nr   r   approved_go_live_at)update_fieldszwagtail.schedule.cancelrt   r   r   r]   r   r   r   has_live_version)r   r:   r   r   r   r   r   r   r   rc   r   r}   r   r   rv   r   r)   rH   r<   rE   r   r]   r   r   r   rx   r   r5   r5   r6   test_revision_cancel_schedule-  s$   

z*TestAuditLog.test_revision_cancel_schedulec                 C   s>   | j jdd | j jdd | tjjddgd d d S )Nzwagtail.lockr{   zwagtail.unlock)
action__inrC   )rc   rv   r<   r   r)   rH   rE   r3   r5   r5   r6   test_page_lock_unlockF  s   z"TestAuditLog.test_page_lock_unlockc                 C   s8   | j jdddd | ttjjdddg d d S )	NAbout uszabout-us)r    r!   )update_attrsrj   Tr   )rs   zwagtail.copyrg   )rc   copyrF   rG   r   r)   r   r3   r5   r5   r6   test_page_copyQ  s
   zTestAuditLog.test_page_copyc                 C   s   | j jtddddd}| j jtddddd t j }|j| j |dd	 | tjj	d
|d
 d | tjj	d|d
 d d S )NzChild 1zchild-1r`   ra   r$   zChild 2zchild-2
last-child)r(   poswagtail.reorderrj   r(   r   wagtail.mover   )rb   r,   r   r   r)   firstmover<   r   rH   rE   )r4   	section_1r(   r5   r5   r6   test_page_reorderY  s   zTestAuditLog.test_page_reorderc                 C   sr   | j jtddddd}t j }|j| jd|d | t	jj
d|d	 d
 | t	jj
d|d	 d d S )Nr   aboutr`   ra   r$   r   )r   r(   r   r   r   r   r   )rb   r,   r   r   r)   r   r   rc   r<   r   rH   rE   )r4   sectionr(   r5   r5   r6   test_page_moven  s   zTestAuditLog.test_page_movec                 C   s   | j jtddddd | j jtddddd}|jtdd	ddd | j   | tjjd
d d | t	tjjd
dj
dddh d d S )NChildz
child-pager`   ra   r$   zAnother childzchild-page-2
Grandchildzgrandchild-pagezwagtail.deletert      rl   Tr   >   Child (simple page)Homepage (simple page)Grandchild (simple page)Another child (simple page))rc   r,   r   re   r<   r   r)   rH   rE   setr   )r4   childr5   r5   r6   test_page_delete  s2   
zTestAuditLog.test_page_deletec           
      C   s  t jjdd}tjjdd}tjjdd}tjj||dd tjj||dd | j  t j }|	| j|}t
jjdd	}| | d | |d
 jd|j|j|j|j|jjj|jjjddi dD ]j}| |[ |j}|jj|d |dd |  t
jjd| d	}	| |	 d | |	d
 j|j|j|j|j|jj|jjd|jjj|jjjdddd | |	d
 jd W d    n1 sw   Y  qhd S )Ntest_workflownametest_task_1test_task_2r   workflowtask
sort_orderrC   wagtail.workflow.startrt   r   r   r]   r    r]   r    statustask_state_idnextapproverejectThis is my commentr(   action_namecommentwagtail.workflow.r]   r    r   r   r   r   r   r   )r   r)   r.   r   r   rc   r}   r   r   startr   rH   r<   rE   r   r]   r   r   current_task_state_idcurrent_task_stater   subTest	on_actionrefresh_from_dbr   )
r4   r   task_1task_2r(   workflow_stateworkflow_entryrj   
task_stater@   r5   r5   r6   test_workflow_actions  sn   
z"TestAuditLog.test_workflow_actionsc                 C   s  t jjdd}tjjdd}tjjdd}tjj||dd tjj||dd tjjdd	d
}|  t j }|	||}t
jjdd}| | d | |d jd|j|j|j|j|jjj|jjjddi dD ]j}| |[ |j}	|	jj|	d |dd |  t
jjd| d}
| |
 d | |
d j|j|j|	j|	j|	jj|	jjd|jjj|jjjdddd | |
d jd W d    n1 sw   Y  qnd S )Nr   r   r   r   r   r   rC   InitialFr&   ru   r   rt   r   r   r   r   r   r   r   r   r   r   )r   r)   r.   r   r   r   r}   r   r   r   r   rH   r<   rE   r   r]   r   r   r   r   r   r   r   r   r   )r4   r   r   r   snippetr(   r   r   rj   r   r@   r5   r5   r6   test_snippet_workflow_actions  st   z*TestAuditLog.test_snippet_workflow_actionsc                 C   s   t jjdd}tjjdd}tjj||dd | tjjdd  | j	
  t j }|| j	|}t j }|j}|jj|d dd	 | tjjddj| d S )
Nr   r   r   r   r   rs   rt   r   r(   r   )r   r)   r.   r   r   assertFalser   rH   existsrc   r}   r   r   r   ri   r   r   r   r<   r*   r(   )r4   r   r   r(   r   	publisherr   r5   r5   r6   .test_workflow_completions_logs_publishing_user'  s   
z;TestAuditLog.test_workflow_completions_logs_publishing_userc                 C   s   t jjdd}tjjdd}tjj||dd | tjjdd  t	jjdd	d
}|
  t j }|||}t j }|j}|jj|d dd | tjjddj| d S )Nr   r   r   r   r   rs   rt   r   Fr   r   r   )r   r)   r.   r   r   r   r   rH   r   r   r}   r   r   r   ri   r   r   r   r<   r*   r(   )r4   r   r   r   r(   r   r   r   r5   r5   r6   6test_snippet_workflow_completions_logs_publishing_user:  s    zCTestAuditLog.test_snippet_workflow_completions_logs_publishing_userc                 C   sX   t jj| jd}| tjjdd d t j|_	|
  | tjjdd d d S )NrD   zwagtail.view_restriction.creatert   r   zwagtail.view_restriction.edit)r   r)   r.   rc   r<   r   rH   rE   PASSWORDrestriction_typerv   )r4   restrictionr5   r5   r6   test_page_privacyP  s   zTestAuditLog.test_page_privacyN)rX   rY   rZ   r7   ro   rz   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r5   r5   r5   r6   r[      s0    			#ADr[   c                 C   s   |  dddS )Ntest.custom_actionCustom actionTested!)register_action)actionsr5   r5   r6   	test_hook`  s   r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestAuditLogHooksc                 C   s   t jjdd| _d S )NrC   r\   )r   r)   r*   rb   r3   r5   r5   r6   r7   e  s   zTestAuditLogHooks.setUpc                 C   s   t  }| |d d S )Nrg   )r
   rx   action_exists)r4   log_actionsr5   r5   r6   test_register_log_actions_hookh  s   z0TestAuditLogHooks.test_register_log_actions_hookc                 C   sV   |  t}tjj| jdd W d    n1 sw   Y  | |jjddgi d S )Nr   rt   rj   z<The log action 'test.custom_action' has not been registered.)	assertRaisesr   r   r)   r;   rb   r<   	exceptionmessage_dict)r4   er5   r5   r6   test_action_must_be_registeredl  s   z0TestAuditLogHooks.test_action_must_be_registeredc                 C   s   t jj| jdd}t jjdd |  t }| |jd | 	|
d | dt) t }| |
d | |||d | |dd W d    d S 1 sXw   Y  d S )Nrg   rt   r   zUnknown test.custom_actionregister_log_actionsr   r   )r   r)   r;   rb   updater   r
   r<   messager   r  register_hookr   rx   get_formatterformat_messageget_action_label)r4   rn   r  r5   r5   r6   test_action_format_message{  s&   
"z,TestAuditLogHooks.test_action_format_messageN)rX   rY   rZ   r7   r  r	  r  r5   r5   r5   r6   r  d  s
    r  )(r   r   django.confr   django.contrib.authr   "django.contrib.contenttypes.modelsr   django.core.exceptionsr   django.core.serializers.jsonr   django.testr   django.utilsr   	freezegunr	   wagtail.log_actionsr
   r   rQ   wagtail.modelsr   r   r   r   r   r   wagtail.models.audit_logr   wagtail.test.testapp.modelsr   r   wagtail.test.utilsr   r   r[   r   r  r5   r5   r5   r6   <module>   s.     n   Y