a
    Dųgī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ŚcontentZ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   ŚaddZprivate_groups_child_page)ŚselfŚrestriction© r-   ś_/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/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-   r.   Ś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
        r0   r1   r3   r4   r   r5   r   r   r   r   N)r7   r    r   r$   r   r8   r9   r:   r;   r<   r=   r>   r-   r-   r.   Ś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
        r0   r1   r3   śwagtailadmin_pages:editŚhtmlz=<span>Privacy is inherited from the ancestor page - <a href="z'">Private page (simple page)</a></span>N)r7   r    r   r&   r   r8   r9   r$   ŚpkŚjsonŚassertIn)r+   r?   Zparent_edit_urlrC   r-   r-   r.   Ś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   Z
helloworld©r   r   r   r0   r1   r3   ś"is_public": false©r   r   N)r7   Śpostr   r#   r   r8   r9   ŚassertContainsŚ
assertTruer   r   ŚfilterŚexistsr    r   r   r   Ścount©r+   Ś	post_datar?   r,   r-   r-   r.   Śtest_set_password_restriction   s"    żž’z0TestSetPrivacyView.test_set_password_restrictionc                 C   sN   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   Ś rH   r0   r1   r3   r5   zThis field is required.N©	r7   rK   r   r#   r   r8   r9   ZassertFormErrorr;   ©r+   rR   r?   r-   r-   r.   Ś,test_set_password_restriction_password_unset    s    żž’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
        r6   rT   rH   r0   r1   r3   ś"is_public": truerJ   zwagtailadmin_pages:historyZpage_id)ŚkwargszhRemoved the &#x27;Private, accessible with a shared password&#x27; view restriction. The page is public.N)r7   rK   r   r$   r   r8   r9   rL   ŚassertFalser   r   rN   rO   r    )r+   rR   r?   Zhistory_urlZhistory_responseZexpected_log_messager-   r-   r.   Ś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 )Nr0   r1   z[data-field-input]r3   r   r5   )r7   r    r   r#   r   Śget_soupr   Ś
select_oner8   r9   rF   r;   ŚfieldsZassertIsNotNone)r+   r?   Zinput_elr-   r-   r.   Śtest_set_shared_password_pageŁ   s    ’z0TestSetPrivacyView.test_set_shared_password_pageZSHARED_PASSWORDF)Z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 )Nr0   r1   r3   r   r5   r   z-<div class="w-field__input" data-field-input>)r7   r    r   r#   r   r8   r9   ZassertNotInr;   r^   rZ   Zvalid_valuer   ZPASSWORDZassertNotContainsr>   r-   r-   r.   Ś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
        r0   r1   r3   r4   r   r5   r   r   r   rT   N)r7   r    r   r'   r   r8   r9   r:   r;   r<   r=   r(   r)   r>   r-   r-   r.   Ś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   rT   rH   r0   r1   r3   rI   rJ   N)r(   r   r)   r7   rK   r   r#   r8   r9   rL   rM   r   r   rN   rO   r    r   r   Śsetr   ŚallrQ   r-   r-   r.   Śtest_set_group_restriction  s(    żž’
žz-TestSetPrivacyView.test_set_group_restrictionc                 C   sN   ddg d}| j  td| jjfd|”}|  |jd” |  |jd dd” d	S )
zV
        This tests that the group fields on the form are validated correctly
        r   rT   rH   r0   r1   r3   r5   z!Please select at least one group.NrU   rV   r-   r-   r.   Ś)test_set_group_restriction_password_unset6  s    żž’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
        r6   rT   rH   r0   r1   r3   rX   rJ   N)r7   rK   r   r$   r   r8   r9   rL   rZ   r   r   rN   rO   rV   r-   r-   r.   Śtest_unset_group_restrictionL  s    żž’z/TestSetPrivacyView.test_unset_group_restrictionN)Ś__name__Ś
__module__Ś__qualname__r/   r@   rA   rG   rS   rW   r[   r_   r   r`   ra   rd   re   rf   r-   r-   r-   r.   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-   r.   r/   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_explorer1   r3   ś)[data-w-zone-switch-key-value="isPublic"]ś*[data-w-zone-switch-key-value="!isPublic"]Śclassśpage-status-tagśw-hiddenN©
r7   r    r   r#   r   r8   r9   r\   r   r]   ©r+   r?   ZsoupZpublic_linkZprivate_linkr-   r-   r.   Śtest_explorer_public  s    ’

z*TestPrivacyIndicators.test_explorer_publicc                 C   sr   | 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"
        rk   r1   r3   rl   rm   rn   ro   rp   N©
r7   r    r   r$   r   r8   r9   r\   r   r]   rr   r-   r-   r.   Śtest_explorer_private  s    ’

z+TestPrivacyIndicators.test_explorer_privatec                 C   sr   | j  td| jjfd”}|  |jd” |  |j”}| 	d”}| 	d”}|  |d dg” |  |d ddg” d	S )
zs
        This tests that the privacy indicator on the private child pages explore view is set to "PRIVATE"
        rk   r1   r3   rl   rm   rn   ro   rp   N©
r7   r    r   r&   r   r8   r9   r\   r   r]   rr   r-   r-   r.   Śtest_explorer_private_child²  s    ’

z1TestPrivacyIndicators.test_explorer_private_childc                 C   s<   | 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
        rk   r1   r3   ś#class="indicator privacy-indicator"é   ©rP   N)r7   r    r   r!   r   r8   r9   rL   r>   r-   r-   r.   Śtest_explorer_list_homepageČ  s    ’żz1TestPrivacyIndicators.test_explorer_list_homepagec                 C   s<   | j  td| jjfd”}|  |jd” | j|ddd dS )z
        This tests that there is a padlock displayed
        next to the private child page in the private pages explorer listing
        rk   r1   r3   rx   ry   rz   N)r7   r    r   r$   r   r8   r9   rL   r>   r-   r-   r.   Ś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"
        rB   r1   r3   rl   rm   rn   ro   rp   Nrq   rr   r-   r-   r.   Śtest_edit_publicķ  s    ’

z&TestPrivacyIndicators.test_edit_publicc                 C   sr   | j  td| jjfd”}|  |jd” |  |j”}| 	d”}| 	d”}|  |d dg” |  |d ddg” d	S )
zj
        This tests that the privacy indicator on the private pages edit view is set to "PRIVATE"
        rB   r1   r3   rl   rm   rn   ro   rp   Nrt   rr   r-   r-   r.   Śtest_edit_private  s    ’

z'TestPrivacyIndicators.test_edit_privatec                 C   sr   | j  td| jjfd”}|  |jd” |  |j”}| 	d”}| 	d”}|  |d dg” |  |d ddg” d	S )
zp
        This tests that the privacy indicator on the private child pages edit view is set to "PRIVATE"
        rB   r1   r3   rl   rm   rn   ro   rp   Nrv   rr   r-   r-   r.   Śtest_edit_private_child  s    ’

z-TestPrivacyIndicators.test_edit_private_childc                 C   sn   | 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   r0   r1   c                 S   s   g | ]}|d  qS ©r   r-   ©Ś.0Śchoicer-   r-   r.   Ś
<listcomp>9  s   ’zXTestPrivacyIndicators.test_private_page_options_only_password_groups.<locals>.<listcomp>r5   r   )r6   r   r   ©r#   Śprivate_page_optionsr<   Ś	__class__r7   r    r   r   r;   r^   ŚchoicesZassertListEqual©r+   Śoriginal_private_page_optionsr?   Zrestriction_typesr-   r-   r.   Ś.test_private_page_options_only_password_groups-  s    ž’ž’zDTestPrivacyIndicators.test_private_page_options_only_password_groupsc                 C   sn   | 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   r0   r1   c                 S   s   g | ]}|d  qS r   r-   r   r-   r-   r.   r   O  s   ’zWTestPrivacyIndicators.test_private_page_options_only_password_login.<locals>.<listcomp>r5   r   )r6   r   r   r   r   r-   r-   r.   Ś-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 )Nr0   r1   r3   z)wagtailadmin/page_privacy/no_privacy.html)r#   r   r<   r   r7   r    r   r   r8   r9   r:   )r+   r   r?   r-   r-   r.   Śtest_private_page_no_options\  s    ’’z2TestPrivacyIndicators.test_private_page_no_optionsN)rg   rh   ri   r/   rs   ru   rw   r{   r|   r}   r~   r   r   r   r   r-   r-   r-   r.   rj   d  s   !rj   N)Zdjango.contrib.auth.modelsr   Zdjango.testr   r   Zdjango.urlsr   Zwagtail.modelsr   r   Zwagtail.test.testapp.modelsr   Zwagtail.test.utilsr	   r
   rj   r-   r-   r-   r.   Ś<module>   s     \