o
    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yE   d dlmZ Y nw 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   event_editors_group)self r   o/var/www/html/ndineBlogger/venv/lib/python3.10/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   s^  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 d    d S 1 sw   Y  d S )Nr   r   r!   r"   r   "wagtaildocs/password_required.html&wagtaildocs_authenticate_with_password   z<form action="%s"zG<input id="id_return_url" name="return_url" type="hidden" value="{}" />T)htmlwrongpassword)password
return_url	swordfish/)LOGIN_REDIRECT_URLzhttps://invaliddomain.com)r   r   r$   r   r   r   r   r%   r&   r   assertEqual	templatesr   r   status_codeassertContainsformatpostassertRedirectslogoutsettings)r   r'   doc_urlr)   
submit_urlr   r   r   %test_anonymous_user_must_authenticate+   sj   
	
"zCTestCollectionPrivacyDocument.test_anonymous_user_must_authenticate(tests/custom_docs_password_required.html)&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+   rA   )r   r   r$   r   r   r   r   r%   r&   r   assertNotEqualr6   r   r5   )r   r'   r>   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   &   |  | j\}}| |d|  d S N/_util/login/?next=)r*   r   r;   r   r)   r(   r   r   r   *test_group_restriction_with_anonymous_user      zHTestCollectionPrivacyDocument.test_group_restriction_with_anonymous_userc                 C   s4   | j ddd | | j\}}| |d|  d S )Neventmoderatorr0   usernamer0   rG   )loginr*   r   r;   rH   r   r   r   ,test_group_restriction_with_unpermitted_user   s   zJTestCollectionPrivacyDocument.test_group_restriction_with_unpermitted_userc                 C   0   | j ddd | | j\}}| |jd d S )Neventeditorr0   rL   r-   rN   r*   r   r5   r7   rH   r   r   r   *test_group_restriction_with_permitted_user      zHTestCollectionPrivacyDocument.test_group_restriction_with_permitted_userc                 C   rP   )N	superuserr0   rL   r-   rR   rH   r   r   r   %test_group_restriction_with_superuser   rT   zCTestCollectionPrivacyDocument.test_group_restriction_with_superuserc                 C   rE   rF   )r*   r   r;   rH   r   r   r   *test_login_restriction_with_anonymous_user   rJ   zHTestCollectionPrivacyDocument.test_login_restriction_with_anonymous_userc                 C   rP   )NrK   r0   rL   r-   )rN   r*   r   r5   r7   rH   r   r   r   *test_login_restriction_with_logged_in_user   rT   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-   r0   form)rN   r&   r   r   r   r   get_soupcontent
select_oner5   r7   assertIncontextfieldsassertIsNotNone)r   r)   input_elr   r   r   ,test_set_shared_password_with_logged_in_user   s   zJTestCollectionPrivacyDocument.test_set_shared_password_with_logged_in_userSHARED_PASSWORDF)&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 )NrY   r"   r-   r0   rZ   restriction_type)rN   r&   r   r   r   r   r5   r7   assertNotInr_   r`   assertFalsevalid_valuer	   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__fixturesr   r*   r@   r   rD   rI   rO   rS   rV   rW   rX   rc   rk   r   r   r   r   r      s(    C
r   N)django.contrib.auth.modelsr   django.core.files.baser   django.testr   r   django.urlsr   wagtail.documents.modelsr   wagtail.modelsr   r	   wagtail.test.utilsr
   urllib.parser   ImportErrorurllibr   r   r   r   r   <module>   s    