o
    hJ<                     @   s  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mZmZmZ d d	lmZ d d
lmZmZmZ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eZ!G dd deeZ"G dd deeZ#G dd deeZ$dS )    )get_user_model)Group
Permission)ContentType)TestCase)reverse)timezone)freeze_time)LockedPagesPanelRecentEditsPanel$UserObjectsInWorkflowModerationPanelWorkflowObjectsToModeratePanel)get_dummy_request)GroupPagePermissionPageWorkflowWorkflowContentType)FullFeaturedSnippet
SimplePage)WagtailTestUtilsc                   @   sD   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S )TestRecentEditsPanelc                 C   st   t jjdd| _tdddd}| jj|d | | _| j  tjj|j	d| _
| jdd	d
| _| jdd	d
 d S )N   idzHello world!hello-worldzSome content here)titleslugcontent)instancealicepasswordusernamer    bob)r   objectsget	root_pager   	add_childsave_revisionrevisionpublishr   
child_pagecreate_superuser
user_alice)selfr+    r/   b/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/admin/tests/test_dashboard.pysetUp   s   

zTestRecentEditsPanel.setUpc                 C   sd   |ddd}| j td| jjfd|}| |td| jjfd tjj| jjd}| 	|j
 d S )NzSome contentr   )r   r   r   zwagtailadmin_pages:editargsr   )clientpostr   r+   r   assertRedirectsr   r$   r%   
assertTruehas_unpublished_changes)r.   r   	post_dataresponsechild_page_newr/   r/   r0   change_something(   s   z%TestRecentEditsPanel.change_somethingc                 C   s"   | j td}| |jd |S )Nwagtailadmin_home   )r4   r%   r   assertEqualstatus_coder.   r:   r/   r/   r0   go_to_dashboard_response7   s   z-TestRecentEditsPanel.go_to_dashboard_responsec                 C   s   | j ddd | jtd}| d|jd | j  | j ddd | d | 	 }| 
d|jd | j ddd | d	 | 	 }| 
d|jd | j  | j ddd | 	 }| 
d|jd d S )
Nr#   r    r!   r=   Your most recent editszutf-8r   Alice's edit
Bob's edit)loginr4   r%   r   assertNotInr   decodelogoutr<   rB   assertInrA   r/   r/   r0   test_your_recent_edits<   s    



z+TestRecentEditsPanel.test_your_recent_editsc                 C   sD   | j ddd | d | j  | jtd}| |jd d S )Nr   r    r!   rD   r=   r>   )	rF   r<   r+   deleter4   r%   r   r?   r@   rA   r/   r/   r0   test_missing_page_record_   s
   

z-TestRecentEditsPanel.test_missing_page_recordc                 C   s   | j ddd t }t| | d W d   n1 sw   Y  t jjdd| j_	t
 }|d| ji}tjj| jjd	j}| |d
 d d | | |d
 d d | dS )z1Test if the panel actually returns expected pagesr#   r    r!   rE   Nzbob@example.com)emailrequest)pk
last_editsr      )rF   r   nowr	   r<   r   r$   r%   r4   userr   get_context_datar   r+   r   specificr?   )r.   edit_timestamppanelctxpager/   r/   r0   
test_panelj   s   
zTestRecentEditsPanel.test_panelc                 C   s   | j ddd | d ddt| jjddd}| jtd	| jjfd
| | 	t
jjdd | jtd}| |jd | |d | |d | |d d S )Nr#   r    r!   zBob was erezGoodbye world!zgoodbye-worldF)	new_titlenew_slugnew_parent_pagecopy_subpagesaliaszwagtailadmin_pages:copyr2   )r   r=   r>   rC   )rF   r<   strr&   r   r4   r5   r   r+   r7   r   r$   r%   r?   r@   assertContainsassertNotContains)r.   r9   r:   r/   r/   r0   &test_copying_does_not_count_as_an_edit   s"   

z;TestRecentEditsPanel.test_copying_does_not_count_as_an_editN)
__name__
__module____qualname__r1   r<   rB   rK   rM   r[   rd   r/   r/   r/   r0   r      s    #r   c                   @   "   e Zd ZdgZdd Zdd ZdS )TestRecentEditsQueryCount	test.jsonc                 C   s   | j ddd| _t | _| j| j_tj }ddh}ddh}ddh}tt	jj
g dd	d
 }|D ]9}|j| jdd}|j|v rJ||| j |j|v r_d|_| j|_t |_|  |j|v rmt |_|  q4d S )Nr#   r    r!         	      )   rk   rl   rm   rn      id__inrP   T)rT   
log_action)r,   r#   r   dummy_requestrT   r   r$   firstlistr   filterorder_byrV   r(   rP   startlocked	locked_byr   rS   	locked_atsaveapproved_go_live_at)r.   workflowworkflow_pageslocked_pagesscheduled_pagespages_to_editrZ   r)   r/   r/   r0   r1      s0   






zTestRecentEditsQueryCount.setUpc                 C   s   t  }d| ji}||}| d ||}W d    n1 s"w   Y  | d| | |}| t|dd g d}dd |d	D }| || d S )
NrO   rk   Ameristralia Daysvg use[href="#icon-lock"]r   )zlive + draftlive + scheduledr   in moderationr   c                 S   s$   g | ]}d  |jddd qS ) TF)string	recursive)joinfind_allstrip.0er/   r/   r0   
<listcomp>   s    zDTestRecentEditsQueryCount.test_panel_query_count.<locals>.<listcomp>z	.w-status)	r   rt   render_htmlassertNumQueriesrJ   get_soupr?   lenselect)r.   rX   parent_contexthtmlsoupexpected_statusesstatusesr/   r/   r0   test_panel_query_count   s   


z0TestRecentEditsQueryCount.test_panel_query_countNre   rf   rg   fixturesr1   r   r/   r/   r/   r0   ri      s    ri   c                   @   rh   )TestLockedPagesQueryCountrj   c                 C   s|   | j ddd| _t | _| j| j_tjjg ddd}t	|D ]\}}d|_
| j|_t tj|d |_|  q!d S )	Nr#   r    r!   )rm   rn   rp   )pk__inrP   T)hours)r,   r#   r   rt   rT   r   r$   rw   rx   	enumeraterz   r{   r   rS   	timedeltar|   r}   )r.   pagesirZ   r/   r/   r0   r1      s   

zTestLockedPagesQueryCount.setUpc                 C   s   t  }| jdd}||}| d ||}W d    n1 s#w   Y  | |}g d}dd |dD }| || d S )NdummyrO   
csrf_token   )Saint Patrick (single event)Steal underpantsr   c                 S      g | ]}|j d dqS T)r   get_textr   r/   r/   r0   r          zDTestLockedPagesQueryCount.test_panel_query_count.<locals>.<listcomp>.title-wrapper a)r
   rt   r   r   r   r   r?   r.   rX   r   r   r   expected_titlestitlesr/   r/   r0   r      s   

z0TestLockedPagesQueryCount.test_panel_query_countNr   r/   r/   r/   r0   r      s    r   c                   @   rh   ))UserObjectsInWorkflowModerationQueryCountrj   c                 C   s  | j ddd| _| jddd| _| jddd| _tjjdd}|j	| j| j t
j }tjj|tjtd tjj|t d	d
 |j	tjjdd tjjddgdj| jd tjjddgd D ]}|  ||| j d|_| j|_t |_|   qhtjjddgd D ]}|  ||| j qt!ddD ]}tjjd| d}|  ||| j qt" | _#| j| j#_$d S )Nadminr    r!   r#   someoneelseEditorsnamer   content_typechangegrouprZ   permission_typechange_fullfeaturedsnippetcodenamerm   rn   rq   )ownerTro   rp   rR      	Some obj text)%r,   	superusercreate_userr#   someone_elser   r$   r%   user_setaddr   ru   r   creater   get_for_modelr   r   r   get_first_root_nodepermissionsr   rw   updaterV   r(   ry   rz   r{   r   rS   r|   r}   ranger   rt   rT   )r.   editorsr   rZ   r   objr/   r/   r0   r1      sF   




z/UserObjectsInWorkflowModerationQueryCount.setUpc                 C   s   t  }d| ji}||}| d ||}W d    n1 s"w   Y  | |}| t|dd g d}dd |dD }| || d S )	NrO   ro   r   r   z
Some obj 2z
Some obj 1r   	Christmasr   r   c                 S   r   r   r   r   r/   r/   r0   r   9  r   zTUserObjectsInWorkflowModerationQueryCount.test_panel_query_count.<locals>.<listcomp>r   )r   rt   r   r   r   r?   r   r   r   r/   r/   r0   r   &  s   


z@UserObjectsInWorkflowModerationQueryCount.test_panel_query_countNr   r/   r/   r/   r0   r      s    .r   c                   @   rh   )#WorkflowObjectsToModerateQueryCountrj   c                 C   s  | j ddd| _| jddd| _| jddd| _tjjdd}tjjdd}|j	| j |j	| j t
 }tjj||d	d
 tjj||d	d
 tjj||dd
 |j	tjjdd |jj	tjjddgd  tj }tjj|tjtd t
jjddgd D ]}|  ||| j d|_| j|_t |_|  qt
jjddgd D ]}|  ||| j qt ddD ]}tjjd| d}|  ||| j qt! | _"| j| j"_#d S )Nr   r    r!   r#   	moderatorr   r   
Moderatorsr   r   r*   r   r   publish_fullfeaturedsnippet)codename__inr   rm   rn   rq   Tro   rp   rR   r   r   r   )$r,   r   r   r#   r   r   r$   r%   r   r   r   r   r   r   r   r   rw   r   ru   r   r   r   r   rV   r(   ry   rz   r{   r   rS   r|   r}   r   r   rt   rT   )r.   r   
moderatorsrootr   rZ   r   r   r/   r/   r0   r1   @  s`   
	


z)WorkflowObjectsToModerateQueryCount.setUpc                 C   s   t  }| jdd}||}| d ||}W d    n1 s#w   Y  | |}| t|dd g d}dd |d	D }| || d S )
Nr   r   rp   r   r   r   c                 S   r   r   r   r   r/   r/   r0   r     r   zNWorkflowObjectsToModerateQueryCount.test_panel_query_count.<locals>.<listcomp>r   )r   rt   r   r   r   r?   r   r   r   r/   r/   r0   r     s   

z:WorkflowObjectsToModerateQueryCount.test_panel_query_countNr   r/   r/   r/   r0   r   =  s    @r   N)%django.contrib.authr   django.contrib.auth.modelsr   r   "django.contrib.contenttypes.modelsr   django.testr   django.urlsr   django.utilsr   	freezegunr	   wagtail.admin.views.homer
   r   r   r   wagtail.coreutilsr   wagtail.modelsr   r   r   r   wagtail.test.testapp.modelsr   r   wagtail.test.utilsr   r   ri   r   r   r   r/   r/   r/   r0   <module>   s$     9"H