o
    ĖÉhīU  ć                   @   sx   d dl mZ d dlmZmZ d dlmZ d dlmZ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 )é    )ŚGroup)ŚTestCaseŚoverride_settings)Śreverse)ŚPageŚPageViewRestriction)Ś
SimplePage)ŚWagtailTestUtilsc                   @   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
eddiddd Zdd Zdd Zdd Zdd ZdS )ŚTestSetPrivacyViewc                 C   s   |   ”  tjjdd| _| jjtddddd| _| jjtddddd| _t	jj
| jd	d
d | 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| _tjj
dd| _|j | j” |j | j” | jjtddddd| _d S )Né   ©ŚidśPublic pageŚhelloT©ŚtitleŚcontentŚlive©ŚinstanceśPrivate pageŚpasswordŚpassword123©ŚpageŚrestriction_typer   śPrivate child pagezPrivate groups pageŚgroups)r   r   zPrivate page group)ŚnamezPrivate page group2zPrivate groups child page)Śloginr   ŚobjectsŚgetŚhomepageŚ	add_childr   Śpublic_pageŚprivate_pager   ŚcreateŚprivate_child_pageŚprivate_groups_pager   ŚgroupŚgroup2r   ŚaddŚprivate_groups_child_page)ŚselfŚrestriction© r/   ś`/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/admin/tests/test_privacy.pyŚsetUp   s^   ż’ż’’ż’ż’’ż’zTestSetPrivacyView.setUpc                 C   sh   | j  td| jjfd”}|  |jd” |  |d” |  |jd j	| j” |  |jd d  
” d” d	S )
zr
        This tests that a blank form is returned when a user opens the set_privacy view on a public page
        śwagtailadmin_pages:set_privacy©ŚargséČ   ś*wagtailadmin/page_privacy/set_privacy.htmlr   Śformr   ŚnoneN)Śclientr!   r   r$   r   ŚassertEqualŚstatus_codeŚassertTemplateUsedŚcontextŚspecificŚvalue©r-   Śresponser/   r/   r0   Śtest_get_publicC   s   ’z"TestSetPrivacyView.test_get_publicc                 C   s   | j  td| jjfd”}|  |jd” |  |d” |  |jd j	| j” |  |jd d  
” d” |  |jd d  
” d	” |  |jd d
  
” g ” dS )z
        This tests that the restriction type and password fields as set correctly
        when a user opens the set_privacy view on a public page
        r2   r3   r5   r6   r   r7   r   r   r   r   N)r9   r!   r   r%   r   r:   r;   r<   r=   r>   r?   r@   r/   r/   r0   Śtest_get_privateS   s   ’’z#TestSetPrivacyView.test_get_privatec                 C   s^   | j  td| jjfd”}|  |jd” td| jjfd}| 	” d }|  
d| d|” dS )	z
        This tests that the set_privacy view tells the user
        that the password restriction has been applied to an ancestor
        r2   r3   r5   śwagtailadmin_pages:editŚhtmlz=<span>Privacy is inherited from the ancestor page - <a href="z'">Private page (simple page)</a></span>N)r9   r!   r   r'   r   r:   r;   r%   ŚpkŚjsonŚassertIn)r-   rA   Śparent_edit_urlrE   r/   r/   r0   Śtest_get_private_childh   s   
’’ž
žz)TestSetPrivacyView.test_get_private_childc                 C   s   ddg d}| j  td| jjfd|”}|  |jd” |  |d” |  t	j
j| jd ” ” t	j
j| jd}|  |jd” |  |jd” |  |j ” d	” d
S )za
        This tests that setting a password restriction using the set_privacy view works
        r   Ś
helloworld©r   r   r   r2   r3   r5   ś"is_public": false©r   r   N)r9   Śpostr   r$   r   r:   r;   ŚassertContainsŚ
assertTruer   r    ŚfilterŚexistsr!   r   r   r   Ścount©r-   Ś	post_datarA   r.   r/   r/   r0   Śtest_set_password_restriction   s"   żž’z0TestSetPrivacyView.test_set_password_restrictionc                 C   óN   ddg d}| j  td| jjfd|”}|  |jd” |  |jd dd” d	S )
zW
        This tests that the password field on the form is validated correctly
        r   Ś rL   r2   r3   r5   r7   zThis field is required.N©	r9   rO   r   r$   r   r:   r;   ŚassertFormErrorr=   ©r-   rV   rA   r/   r/   r0   Ś,test_set_password_restriction_password_unset    ó   żž’z?TestSetPrivacyView.test_set_password_restriction_password_unsetc                 C   s   ddg d}| j  td| jjfd|”}|  |jd” |  |d” |  t	j
j| jd ” ” td	d
| jjid}| j  |”}d}|  ||” dS )zb
        This tests that removing a password restriction using the set_privacy view works
        r8   rY   rL   r2   r3   r5   ś"is_public": truerN   zwagtailadmin_pages:historyŚpage_id)ŚkwargszhRemoved the &#x27;Private, accessible with a shared password&#x27; view restriction. The page is public.N)r9   rO   r   r%   r   r:   r;   rP   ŚassertFalser   r    rR   rS   r!   )r-   rV   rA   Śhistory_urlŚhistory_responseŚexpected_log_messager/   r/   r0   Śtest_unset_password_restriction¶   s,   żž’’žz2TestSetPrivacyView.test_unset_password_restrictionc                 C   s\   | j  td| jjfd”}|  |j” d”}|  |j	d” |  
d|jd j” |  |” d S )Nr2   r3   z[data-field-input]r5   r   r7   )r9   r!   r   r$   r   Śget_soupr   Ś
select_oner:   r;   rH   r=   ŚfieldsŚassertIsNotNone)r-   rA   Śinput_elr/   r/   r0   Śtest_set_shared_password_pageŁ   s   ’z0TestSetPrivacyView.test_set_shared_password_pageŚSHARED_PASSWORDF)ŚWAGTAIL_PRIVATE_PAGE_OPTIONSc                 C   sj   | j  td| jjfd”}|  |jd” |  d|jd j	” |  
|jd j	d  tj”” |  |d” d S )Nr2   r3   r5   r   r7   r   z-<div class="w-field__input" data-field-input>)r9   r!   r   r$   r   r:   r;   ŚassertNotInr=   ri   rb   Śvalid_valuer   ŚPASSWORDŚassertNotContainsr@   r/   r/   r0   Śtest_unset_shared_password_pageē   s   ’’ż’z2TestSetPrivacyView.test_unset_shared_password_pagec                 C   sØ   | j  td| jjfd”}|  |jd” |  |d” |  |jd j	| j” |  |jd d  
” d” |  |jd d	  
” d
” |  |jd d  
” | jj| jjg” dS )z
        This tests that the restriction type and group fields as set correctly when a user opens the set_privacy view on a public page
        r2   r3   r5   r6   r   r7   r   r   r   rY   N)r9   r!   r   r(   r   r:   r;   r<   r=   r>   r?   r)   r*   r@   r/   r/   r0   Śtest_get_private_groupsū   s   
’’’z*TestSetPrivacyView.test_get_private_groupsc                 C   sĀ   dd| j j| jjgd}| j td| jjfd|”}|  |jd” |  	|d” |  
tjj| jd ” ” tjj| jd}|  |jd” |  |jd” |  ttjj| jdj ” | j | jh” d	S )
z^
        This tests that setting a group restriction using the set_privacy view works
        r   rY   rL   r2   r3   r5   rM   rN   N)r)   r   r*   r9   rO   r   r$   r:   r;   rP   rQ   r   r    rR   rS   r!   r   r   Śsetr   ŚallrU   r/   r/   r0   Śtest_set_group_restriction  s(   żž’
žz-TestSetPrivacyView.test_set_group_restrictionc                 C   rX   )
zV
        This tests that the group fields on the form are validated correctly
        r   rY   rL   r2   r3   r5   r7   z!Please select at least one group.NrZ   r\   r/   r/   r0   Ś)test_set_group_restriction_password_unset6  r^   z<TestSetPrivacyView.test_set_group_restriction_password_unsetc                 C   s`   ddg d}| j  td| jjfd|”}|  |jd” |  |d” |  t	j
j| jd ” ” d	S )
z`
        This tests that removing a groups restriction using the set_privacy view works
        r8   rY   rL   r2   r3   r5   r_   rN   N)r9   rO   r   r%   r   r:   r;   rP   rb   r   r    rR   rS   r\   r/   r/   r0   Śtest_unset_group_restrictionL  s   żž’z/TestSetPrivacyView.test_unset_group_restrictionN)Ś__name__Ś
__module__Ś__qualname__r1   rB   rC   rJ   rW   r]   rf   rl   r   rs   rt   rw   rx   ry   r/   r/   r/   r0   r
   
   s    8!#
%r
   c                   @   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d Zdd Zdd Zdd ZdS )ŚTestPrivacyIndicatorsc                 C   s~   |   ”  tjjdd| _| jjtddddd| _| jjtddddd| _t	jj
| jd	d
d | jjtddddd| _d S )Nr   r   r   r   Tr   r   r   r   r   r   r   )r   r   r    r!   r"   r#   r   r$   r%   r   r&   r'   )r-   r/   r/   r0   r1   e  s4   ż’ż’’ż’zTestPrivacyIndicators.setUpc                 C   sr   | j  td| jjfd”}|  |jd” |  |j”}| 	d”}| 	d”}|  |d ddg” |  |d dg” d	S )
zk
        This tests that the privacy indicator on the public pages explore view is set to "PUBLIC"
        Śwagtailadmin_explorer3   r5   ś)[data-w-zone-switch-key-value="isPublic"]ś*[data-w-zone-switch-key-value="!isPublic"]Śclassśpage-status-tagśw-hiddenN©
r9   r!   r   r$   r   r:   r;   rg   r   rh   ©r-   rA   ŚsoupŚpublic_linkŚprivate_linkr/   r/   r0   Śtest_explorer_public  s   ’

z*TestPrivacyIndicators.test_explorer_publicc                 C   ór   | j  td| jjfd”}|  |jd” |  |j”}| 	d”}| 	d”}|  |d dg” |  |d ddg” d	S )
zm
        This tests that the privacy indicator on the private pages explore view is set to "PRIVATE"
        r~   r3   r5   r   r   r   r   r   N©
r9   r!   r   r%   r   r:   r;   rg   r   rh   r   r/   r/   r0   Śtest_explorer_private  ó   ’

z+TestPrivacyIndicators.test_explorer_privatec                 C   r   )
zs
        This tests that the privacy indicator on the private child pages explore view is set to "PRIVATE"
        r~   r3   r5   r   r   r   r   r   N©
r9   r!   r   r'   r   r:   r;   rg   r   rh   r   r/   r/   r0   Śtest_explorer_private_child²  r   z1TestPrivacyIndicators.test_explorer_private_childc                 C   ó<   | j  td| jjfd”}|  |jd” | j|ddd dS )zy
        This tests that there is a padlock displayed next to the private page in the homepages explorer listing
        r~   r3   r5   ś#class="indicator privacy-indicator"é   ©rT   N)r9   r!   r   r"   r   r:   r;   rP   r@   r/   r/   r0   Śtest_explorer_list_homepageČ  s   ’
żz1TestPrivacyIndicators.test_explorer_list_homepagec                 C   r   )z
        This tests that there is a padlock displayed
        next to the private child page in the private pages explorer listing
        r~   r3   r5   r   r   r   N)r9   r!   r   r%   r   r:   r;   rP   r@   r/   r/   r0   Śtest_explorer_list_privateŚ  s   ’
żz0TestPrivacyIndicators.test_explorer_list_privatec                 C   sr   | j  td| jjfd”}|  |jd” |  |j”}| 	d”}| 	d”}|  |d dg” |  |d ddg” d	S )
zh
        This tests that the privacy indicator on the public pages edit view is set to "PUBLIC"
        rD   r3   r5   r   r   r   r   r   Nr   r   r/   r/   r0   Śtest_edit_publicķ  ó   ’

z&TestPrivacyIndicators.test_edit_publicc                 C   r   )
zj
        This tests that the privacy indicator on the private pages edit view is set to "PRIVATE"
        rD   r3   r5   r   r   r   r   r   Nr   r   r/   r/   r0   Śtest_edit_private  r   z'TestPrivacyIndicators.test_edit_privatec                 C   r   )
zp
        This tests that the privacy indicator on the private child pages edit view is set to "PRIVATE"
        rD   r3   r5   r   r   r   r   r   Nr   r   r/   r/   r0   Śtest_edit_private_child  s   ’

z-TestPrivacyIndicators.test_edit_private_childc                 C   ón   | j j}ddg| j jj_| j td| j jfd”}dd |jd j	d j
D }|  |g d	¢” || j jj_d S )
Nr   r   r2   r3   c                 S   ó   g | ]}|d  qS ©r   r/   ©Ś.0Śchoicer/   r/   r0   Ś
<listcomp>9  ó    ’’zXTestPrivacyIndicators.test_private_page_options_only_password_groups.<locals>.<listcomp>r7   r   )r8   r   r   ©r$   Śprivate_page_optionsr>   Ś	__class__r9   r!   r   r   r=   ri   ŚchoicesŚassertListEqual©r-   Śoriginal_private_page_optionsrA   Śrestriction_typesr/   r/   r0   Ś.test_private_page_options_only_password_groups-  s   ž’ž’zDTestPrivacyIndicators.test_private_page_options_only_password_groupsc                 C   r   )
Nr   r   r2   r3   c                 S   r   r   r/   r   r/   r/   r0   r    O  r”   zWTestPrivacyIndicators.test_private_page_options_only_password_login.<locals>.<listcomp>r7   r   )r8   r   r   r¢   r§   r/   r/   r0   Ś-test_private_page_options_only_password_loginF  s   ’ž’zCTestPrivacyIndicators.test_private_page_options_only_password_loginc                 C   sX   | j j}g | j jj_| j td| j jfd”}|  |j	d” |  
|d” || j jj_d S )Nr2   r3   r5   z)wagtailadmin/page_privacy/no_privacy.html)r$   r£   r>   r¤   r9   r!   r   r   r:   r;   r<   )r-   rØ   rA   r/   r/   r0   Śtest_private_page_no_options\  s   ’’z2TestPrivacyIndicators.test_private_page_no_optionsN)rz   r{   r|   r1   r   r   r   r   r   r   r   r   rŖ   r«   r¬   r/   r/   r/   r0   r}   d  s    !r}   N)Śdjango.contrib.auth.modelsr   Śdjango.testr   r   Śdjango.urlsr   Śwagtail.modelsr   r   Śwagtail.test.testapp.modelsr   Śwagtail.test.utilsr	   r
   r}   r/   r/   r/   r0   Ś<module>   s      \