U
    {hC                     @   s   d dl mZ 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mZ d dlmZ zd dlmZ W n  ek
r   d dlmZ Y nX G d	d
 d
eeZdS )    )Group)ContentFile)TestCaseoverride_settings)reverse)Document)
CollectionCollectionViewRestriction)WagtailTestUtils)quotec                   @   s   e Zd ZdgZdd Zdd Zdd Zedd	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S ) TestCollectionPrivacyDocumentz	test.jsonc                 C   sx   t d| _d| j_tjjdd| _tjjdd| _tjjdd| _tjjdd| _	t
jj| jd	| _tjjd
d| _d S )Ns   A boring example documentztest.txt   )idzPassword protected)namezLogin protectedzGroup protected)
collectionzEvent editors)r   	fake_filer   r   objectsgetr   password_collectionlogin_collectiongroup_collectionr	   view_restrictionr   Zevent_editors_group)self r   w/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/documents/tests/test_collection_privacy.pysetUp   s    
z#TestCollectionPrivacyDocument.setUpc                 C   s@   t jjd| j|d}td|j|jfd}| j|}|t	|fS )NTest documenttitlefiler   wagtaildocs_serveargs)
r   r   creater   r   r   filenameclientr   r   )r   r   secret_documenturlresponser   r   r   get_document   s     
z*TestCollectionPrivacyDocument.get_documentc              	   C   sH  t jjd| j| jd}td|j|jfd}| j	|}| 
|jd jd td| jjgd}| 
|jd | |d	|  | j|d
|dd | j|d|d}| 
|jd | 
|jd jd | |d	|  | j|d|d}| || | j	| | j  | jdd& | j|ddd}| |d W 5 Q R X d S )Nr   r   r    r!   r   "wagtaildocs/password_required.htmlZ&wagtaildocs_authenticate_with_password   z<form action="%s"zG<input id="id_return_url" name="return_url" type="hidden" value="{}" />T)htmlZwrongpassword)passwordZ
return_urlZ	swordfish/)ZLOGIN_REDIRECT_URLzhttps://invaliddomain.com)r   r   r#   r   r   r   r   r$   r%   r   assertEqual	templatesr   r   status_codeZassertContainsformatpostassertRedirectsZlogoutsettings)r   r&   doc_urlr(   Z
submit_urlr   r   r   %test_anonymous_user_must_authenticate+   sp     

  	
 
zCTestCollectionPrivacyDocument.test_anonymous_user_must_authenticate(tests/custom_docs_password_required.html)Z&WAGTAILDOCS_PASSWORD_REQUIRED_TEMPLATEc                 C   sb   t jjd| j| jd}td|j|jfd}| j	|}| 
|jd jd | |jd jd d S )Nr   r   r    r!   r   r*   r8   )r   r   r#   r   r   r   r   r$   r%   r   ZassertNotEqualr0   r   r/   )r   r&   r6   r(   r   r   r   Ltest_anonymous_user_must_authenticate_with_custom_password_required_templaten   s$     

 
 zjTestCollectionPrivacyDocument.test_anonymous_user_must_authenticate_with_custom_password_required_templatec                 C   s&   |  | j\}}| |d|  d S N/_util/login/?next=)r)   r   r4   r   r(   r'   r   r   r   *test_group_restriction_with_anonymous_user   s    zHTestCollectionPrivacyDocument.test_group_restriction_with_anonymous_userc                 C   s4   | j ddd | | j\}}| |d|  d S )Neventmoderatorr-   usernamer-   r;   )loginr)   r   r4   r<   r   r   r   ,test_group_restriction_with_unpermitted_user   s    zJTestCollectionPrivacyDocument.test_group_restriction_with_unpermitted_userc                 C   s0   | j ddd | | j\}}| |jd d S )NZeventeditorr-   r?   r+   rA   r)   r   r/   r1   r<   r   r   r   *test_group_restriction_with_permitted_user   s    zHTestCollectionPrivacyDocument.test_group_restriction_with_permitted_userc                 C   s0   | j ddd | | j\}}| |jd d S )NZ	superuserr-   r?   r+   rC   r<   r   r   r   %test_group_restriction_with_superuser   s    zCTestCollectionPrivacyDocument.test_group_restriction_with_superuserc                 C   s&   |  | j\}}| |d|  d S r:   )r)   r   r4   r<   r   r   r   *test_login_restriction_with_anonymous_user   s    zHTestCollectionPrivacyDocument.test_login_restriction_with_anonymous_userc                 C   s0   | j ddd | | j\}}| |jd d S )Nr>   r-   r?   r+   )rA   r)   r   r/   r1   r<   r   r   r   *test_login_restriction_with_logged_in_user   s    zHTestCollectionPrivacyDocument.test_login_restriction_with_logged_in_userc                 C   sd   |    | jtd| jjfd}| |jd}| 	|j
d | d|jd j | | d S )N$wagtailadmin_collections:set_privacyr!   z[data-field-input]r+   r-   form)rA   r%   r   r   r   r   Zget_soupcontentZ
select_oner/   r1   ZassertIncontextfieldsZassertIsNotNone)r   r(   Zinput_elr   r   r   ,test_set_shared_password_with_logged_in_user   s    zJTestCollectionPrivacyDocument.test_set_shared_password_with_logged_in_userZSHARED_PASSWORDF)Z&WAGTAILDOCS_PRIVATE_COLLECTION_OPTIONSc                 C   sf   |    | jtd| jjfd}| |jd | d|j	d j
 | |j	d j
d tj d S )NrH   r!   r+   r-   rI   Zrestriction_type)rA   r%   r   r   r   r   r/   r1   ZassertNotInrK   rL   ZassertFalseZvalid_valuer	   ZPASSWORD)r   r(   r   r   r   .test_unset_shared_password_with_logged_in_user   s    
zLTestCollectionPrivacyDocument.test_unset_shared_password_with_logged_in_userN)__name__
__module____qualname__Zfixturesr   r)   r7   r   r9   r=   rB   rD   rE   rF   rG   rM   rN   r   r   r   r   r      s&   C
r   N)Zdjango.contrib.auth.modelsr   Zdjango.core.files.baser   Zdjango.testr   r   Zdjango.urlsr   Zwagtail.documents.modelsr   Zwagtail.modelsr   r	   Zwagtail.test.utilsr
   urllib.parser   ImportErrorurllibr   r   r   r   r   <module>   s   