U
    {®Žh2T  ć                   @   s   d dl Z 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 )é    N)Ś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   ŚaddZprivate_groups_child_page)ŚselfŚrestriction© r.   śh/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/admin/tests/test_privacy.pyŚsetUp   sd    ż’ż’  ’ż’ż’ ’ż’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_publicE   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
        r1   r2   r4   r5   r   r6   r   r   r   r   N)r8   r!   r   r%   r   r9   r:   r;   r<   r=   r>   r?   r.   r.   r/   Śtest_get_privateU   s    ’ ’z#TestSetPrivacyView.test_get_privatec                 C   s¾   | j  td| jjfd”}|  |jd” |  |d” |  |d” |  |j	d j
| j” |  t |j”d ”}|  | d”” |  | d	”” | d
”}td| jjf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
        r1   r2   r4   z/wagtailadmin/page_privacy/ancestor_privacy.htmlz1This page has been made private by a parent page.Zpage_with_restrictionŚhtmlŚheaderZh1Śaśwagtailadmin_pages:editŚhrefN)r8   r!   r   r'   r   r9   r:   r;   ŚassertContainsr<   r=   r%   Śget_soupŚjsonŚloadsr   ZassertIsNoneŚ
select_oneŚpk)r,   r@   ŚsoupŚlinkZparent_edit_urlr.   r.   r/   Śtest_get_private_childj   s8     ’’ ’ ’
 ’
ž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   r1   r2   r4   ś"is_public": false©r   r   N)r8   Śpostr   r$   r   r9   r:   rH   Ś
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   Ś rQ   r1   r2   r4   r6   zThis field is required.N©	r8   rT   r   r$   r   r9   r:   ZassertFormErrorr<   ©r,   rZ   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
        r7   r\   rQ   r1   r2   r4   ś"is_public": truerS   zwagtailadmin_pages:historyZpage_id)ŚkwargszhRemoved the &#x27;Private, accessible with a shared password&#x27; view restriction. The page is public.N)r8   rT   r   r%   r   r9   r:   rH   ŚassertFalser   r    rV   rW   r!   )r,   rZ   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 )Nr1   r2   z[data-field-input]r4   r   r6   )r8   r!   r   r$   r   rI   r   rL   r9   r:   ZassertInr<   ŚfieldsŚ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 )Nr1   r2   r4   r   r6   r   z-<div class="w-field__input" data-field-input>)r8   r!   r   r$   r   r9   r:   ZassertNotInr<   rd   rb   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
        r1   r2   r4   r5   r   r6   r   r   r   r\   N)r8   r!   r   r(   r   r9   r:   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   r\   rQ   r1   r2   r4   rR   rS   N)r)   r   r*   r8   rT   r   r$   r9   r:   rH   rU   r   r    rV   rW   r!   r   r   Śsetr   ŚallrY   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   r\   rQ   r1   r2   r4   r6   z!Please select at least one group.Nr]   r^   r.   r.   r/   Ś)test_set_group_restriction_password_unsetD  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
        r7   r\   rQ   r1   r2   r4   r`   rS   N)r8   rT   r   r%   r   r9   r:   rH   rb   r   r    rV   rW   r^   r.   r.   r/   Śtest_unset_group_restrictionZ  s    żž’z/TestSetPrivacyView.test_unset_group_restrictionN)Ś__name__Ś
__module__Ś__qualname__r0   rA   rB   rP   r[   r_   rc   rf   r   rg   rh   rk   rl   rm   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/   r0   s  s8    ż’ż’  ’ż’zTestPrivacyIndicators.setUpc                 C   sD   | j  td| jjfd”}|  |jd” |  |d” |  |d” dS )zk
        This tests that the privacy indicator on the public pages explore view is set to "PUBLIC"
        Śwagtailadmin_explorer2   r4   ś3<div class="w-hidden" data-privacy-sidebar-private>ś*<div class="" data-privacy-sidebar-public>N©r8   r!   r   r$   r   r9   r:   rH   r?   r.   r.   r/   Śtest_explorer_public  s    ’ ’z*TestPrivacyIndicators.test_explorer_publicc                 C   sf   | j  td| jjfd”}|  |jd” |  |j”}| 	d”}|  |d g ” | 	d”}|  
|” dS )zm
        This tests that the privacy indicator on the private pages explore view is set to "PRIVATE"
        rr   r2   r4   z[data-privacy-sidebar-private]Śclassz&[data-privacy-sidebar-public].w-hiddenN)r8   r!   r   r%   r   r9   r:   rI   r   rL   re   )r,   r@   rN   Zprivate_indicatorZpublic_indicatorr.   r.   r/   Śtest_explorer_private„  s    ’

z+TestPrivacyIndicators.test_explorer_privatec                 C   sD   | j  td| jjfd”}|  |jd” |  |d” |  |d” dS )zs
        This tests that the privacy indicator on the private child pages explore view is set to "PRIVATE"
        rr   r2   r4   ś+<div class="" data-privacy-sidebar-private>ś2<div class="w-hidden" data-privacy-sidebar-public>N©r8   r!   r   r'   r   r9   r:   rH   r?   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
        rr   r2   r4   ś#class="indicator privacy-indicator"é   ©rX   N)r8   r!   r   r"   r   r9   r:   rH   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
        rr   r2   r4   r}   r~   r   N©r8   r!   r   r%   r   r9   r:   rH   r?   r.   r.   r/   Śtest_explorer_list_privateŽ  s    ’żz0TestPrivacyIndicators.test_explorer_list_privatec                 C   sD   | j  td| jjfd”}|  |jd” |  |d” |  |d” dS )zh
        This tests that the privacy indicator on the public pages edit view is set to "PUBLIC"
        rF   r2   r4   rs   rt   Nru   r?   r.   r.   r/   Śtest_edit_publicń  s    ’ ’z&TestPrivacyIndicators.test_edit_publicc                 C   sD   | j  td| jjfd”}|  |jd” |  |d” |  |d” dS )zj
        This tests that the privacy indicator on the private pages edit view is set to "PRIVATE"
        rF   r2   r4   ry   rz   Nr   r?   r.   r.   r/   Śtest_edit_private  s    ’ ’z'TestPrivacyIndicators.test_edit_privatec                 C   sD   | j  td| jjfd”}|  |jd” |  |d” |  |d” dS )zp
        This tests that the privacy indicator on the private child pages edit view is set to "PRIVATE"
        rF   r2   r4   ry   rz   Nr{   r?   r.   r.   r/   Śtest_edit_private_child  s    ’ ’z-TestPrivacyIndicators.test_edit_private_childc                 C   sp   | j j}ddg| j jj_| j td| j jfd”}dd |jd j	d j
D }|  |d	ddg” || j jj_d S )
Nr   r   r1   r2   c                 S   s   g | ]}|d  qS ©r   r.   ©Ś.0Śchoicer.   r.   r/   Ś
<listcomp>0  s   ’zXTestPrivacyIndicators.test_private_page_options_only_password_groups.<locals>.<listcomp>r6   r   r7   ©r$   Śprivate_page_optionsr=   Ś	__class__r8   r!   r   r   r<   rd   Ś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   sp   | j j}ddg| j jj_| j td| j jfd”}dd |jd j	d j
D }|  |d	ddg” || j jj_d S )
Nr   r   r1   r2   c                 S   s   g | ]}|d  qS r   r.   r   r.   r.   r/   r   F  s   ’zWTestPrivacyIndicators.test_private_page_options_only_password_login.<locals>.<listcomp>r6   r   r7   r   r   r.   r.   r/   Ś-test_private_page_options_only_password_login=  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 )Nr1   r2   r4   z)wagtailadmin/page_privacy/no_privacy.html)r$   r   r=   r   r8   r!   r   r   r9   r:   r;   )r,   r   r@   r.   r.   r/   Śtest_private_page_no_optionsS  s    ’’z2TestPrivacyIndicators.test_private_page_no_optionsN)rn   ro   rp   r0   rv   rx   r|   r   r   r   r   r   r   r   r   r.   r.   r.   r/   rq   r  s   !rq   )rJ   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
   rq   r.   r.   r.   r/   Ś<module>   s     h