a
    Dg:                     @   s  d dl Zd dlZd dlZd dlmZ 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 eddG d	d
 d
e
ZeddG dd de
ZeddG dd de
Zedi ejdddiidG dd de
ZeddG dd de
ZeddG dd de
ZdS )    N)mock)settings)ContentFile)TestCaseoverride_settings)reverse)models)WAGTAILDOCS_SERVE_METHODc                   @   s   e Zd Zdd Zdd Zd*ddZdd	 Zd
d Zdd Zdd Z	dd Z
ededdd Zede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d&d' Zd(d) ZdS )+TestServeViewc                 C   sL   t jddd| _| jjdtd t jddd| _| jjdtd d S )NTest documentZ123456)titleZ	file_hashzserve_view.doc   A boring example documentzserve_view.pdf)r   Documentdocumentfilesaver   pdf_documentself r   a/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/documents/tests/test_views.pysetUp   s    zTestServeView.setUpc                 C   s4   t | drd| jj | jj  | jj  d S )Nresponse    )hasattrjoinr   streaming_contentr   r   deleter   r   r   r   r   tearDown   s    
zTestServeView.tearDownNc                 C   s.   |p| j }| jtd|j|jfd| _| jS Nwagtaildocs_serveargs)r   clientgetr   idfilenamer   )r   r   r   r   r   r$   '   s
    
zTestServeView.getc                 C   s   |  |  jd d S )N   )assertEqualr$   status_coder   r   r   r   test_response_code.   s    z TestServeView.test_response_codec                 C   s(   |  | | jd d| jj d d S )NContent-Dispositionzattachment; filename="")r(   r$   r   r&   r   r   r   r   test_content_disposition_header1   s    z-TestServeView.test_content_disposition_headerc                 C   s(   |  | | jd d| jj d d S )Nr+   zinline; filename="r,   )r(   r$   r   r&   r   r   r   r   &test_inline_content_disposition_header7   s    z4TestServeView.test_inline_content_disposition_headerc                 C   sV   |  |  d d | jdd" | d|  j W d    n1 sH0    Y  d S )NContent-Security-Policydefault-src 'none'F)Z"WAGTAILDOCS_BLOCK_EMBEDDED_CONTENT)r(   r$   r   ZassertNotInheadersr   r   r   r   test_content_security_policy=   s    z*TestServeView.test_content_security_policyc                 C   s   |  |  d d d S NX-Content-Type-Optionsnosniffr(   r$   r   r   r   r   test_no_sniff_content_typeC   s    z(TestServeView.test_no_sniff_content_type#wagtail.documents.views.serve.hooks/wagtail.documents.views.serve.get_object_or_404c                 C   s   t  }| jj|_| jj|_| jj|_td|_d |j_d |j_	||_
g |j_
|  }| |jd | |d dtj| jj | |d d | |d d d S )	N   file-like objectfile-like objectfile-like objectfile-like objectfile-like objectfile-like objectfile-like objectfile-like objectfile-like objectfile-like objectr'   r+   z.attachment; filename={0}; filename*=UTF-8''{0}r/   r0   r4   r5   )r   Mockr   r&   content_typecontent_dispositionr   r   pathurlreturn_value	get_hooksr$   r(   r)   formaturllibparsequoter   Zmock_get_object_or_404Z
mock_hooksZmock_docr   r   r   r   !test_non_local_filesystem_headersF   s&    



z/TestServeView.test_non_local_filesystem_headersc                 C   s   t  }| jj|_| jj|_| jj|_td|_d |j_d |j_	||_
g |j_
| | j}| |jd | |d d | |d d | |d d d S )	Nr:   r'   r+   inliner/   r0   r4   r5   )r   r;   r   r&   r<   r=   r   r   r>   r?   r@   rA   r$   r(   r)   rF   r   r   r   (test_non_local_filesystem_inline_headersc   s    



z6TestServeView.test_non_local_filesystem_inline_headersc                 C   s   |  |  d d d S )NzContent-LengthZ25r6   r   r   r   r   test_content_length_header}   s    z(TestServeView.test_content_length_headerc                 C   s   |  |  d d d S )NzContent-Typezapplication/mswordr6   r   r   r   r   test_content_type_header   s    z&TestServeView.test_content_type_headerc                 C   s   |  |  j d S N)
assertTruer$   Z	streamingr   r   r   r   test_is_streaming_response   s    z(TestServeView.test_is_streaming_responsec                 C   s   |  d|  jd d S )Nr   r   )r(   r   r$   r   r   r   r   r   test_content   s    zTestServeView.test_contentc              	   C   s   t  }tj| z^|   | |jd | |jd d d tj	 | |jd d d | j
 W tj| ntj| 0 d S )N   r      Zsenderinstance)r   Z	MagicMockr   Zdocument_servedconnectr$   r(   Z
call_countZ
mock_callsr   r   Z
disconnect)r   Zmock_handlerr   r   r   test_document_served_fired   s    z(TestServeView.test_document_served_firedc                 C   s&   | j tddd}| |jd d S )Nr    )i  Zblahblahblahr!     )r#   r$   r   r(   r)   r   r   r   r   r   test_with_nonexistent_document   s    	z,TestServeView.test_with_nonexistent_documentc                 C   s.   | j td| jjdfd}| |jd d S )Nr    Zincorrectfilenamer!   rU   )r#   r$   r   r   r%   r(   r)   rV   r   r   r   test_with_incorrect_filename   s    z*TestServeView.test_with_incorrect_filenamec                 C   s   |  |  d d d S )NETagz"123456"r6   r   r   r   r   test_has_etag_header   s    z"TestServeView.test_has_etag_headerc                 C   s   ddl m} |  d S Nr   )_get_sendfileZwagtail.utils.sendfiler\   clearr   r\   r   r   r   clear_sendfile_cache   s    z"TestServeView.clear_sendfile_cache)N)__name__
__module____qualname__r   r   r$   r*   r-   r.   r2   r7   r   patchrG   rI   rJ   rK   rN   rO   rT   rW   rX   rZ   r`   r   r   r   r   r
      s.   

r
   redirectc                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestServeViewWithRedirectc                 C   s@   t jdd| _| jjdtd td| jj| jjfd| _	d S )Nr   r   zserve_view_with_redirect.docA boring example documentr    r!   
r   r   r   r   r   r   r   r%   r&   serve_view_urlr   r   r   r   r      s    zTestServeViewWithRedirect.setUpc                 C   s   | j   d S rL   r   r   r   r   r   r   r      s    z"TestServeViewWithRedirect.tearDownc                 C   s    | j td| jj| jjfdS r   r#   r$   r   r   r%   r&   r   r   r   r   r$      s
    zTestServeViewWithRedirect.getc                 C   s   |  | jj| j d S rL   r(   r   r?   rj   r   r   r   r   ,test_document_url_should_point_to_serve_view   s    zFTestServeViewWithRedirect.test_document_url_should_point_to_serve_viewc                 C   s"   |   }| j|| jjjdd d S NF)Zfetch_redirect_responser$   assertRedirectsr   r   r?   rV   r   r   r   test_redirect   s    z'TestServeViewWithRedirect.test_redirectN)ra   rb   rc   r   r   r$   rn   rr   r   r   r   r   rf      s
   
rf   directc                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestDirectDocumentUrlsc                 C   s&   t jdd| _| jjdtd d S )Nr   rg   zdirect_document_urls.docrh   )r   r   r   r   r   r   r   r   r   r   r      s
    zTestDirectDocumentUrls.setUpc                 C   s   | j   d S rL   rk   r   r   r   r   r      s    zTestDirectDocumentUrls.tearDownc                 C   s    | j td| jj| jjfdS r   rl   r   r   r   r   r$      s
    zTestDirectDocumentUrls.getc                 C   s   |  | jj| jjj d S rL   )r(   r   r?   r   r   r   r   r   2test_url_should_point_directly_to_file_storage_url   s    zITestDirectDocumentUrls.test_url_should_point_directly_to_file_storage_urlc                 C   s"   |   }| j|| jjjdd d S ro   rp   rV   r   r   r   rr      s    z$TestDirectDocumentUrls.test_redirectN)ra   rb   rc   r   r   r$   ru   rr   r   r   r   r   rt      s
   rt   defaultZBACKENDz8wagtail.test.dummy_external_storage.DummyExternalStorage)r	   STORAGESc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )TestServeWithExternalStoragez
    Test the behaviour of the default serve method when used with a remote storage backend
    (i.e. one that throws NotImplementedError for the path() method).
    c                 C   s@   t jdd| _| jjdtd td| jj| jjfd| _	d S )Nr   rg   zserve_with_external_storage.docrh   r    r!   ri   r   r   r   r   r     s    z"TestServeWithExternalStorage.setUpc                 C   s   | j   d S rL   rk   r   r   r   r   r     s    z%TestServeWithExternalStorage.tearDownc                 C   s   |  | jj| j d S rL   rm   r   r   r   r   rn     s    zITestServeWithExternalStorage.test_document_url_should_point_to_serve_viewc                 C   s(   | j | j}| j|| jjjdd d S ro   )r#   r$   rj   rq   r   r   r?   rV   r   r   r   rr     s    z*TestServeWithExternalStorage.test_redirectN)ra   rb   rc   __doc__r   r   rn   rr   r   r   r   r   rx      s
   

rx   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zed	d
dd Zede	j
e	jdd ddd Zede	j
e	jdd ddd Zdd Zdd ZdS )TestServeViewWithSendfilec                 C   sP   zdd l }W n ty(   tdY n0 tjdd| _| jjdt	d d S )Nr   zdjango-sendfile not installedr   rg   zserve_view_with_sendfile.docrh   )
sendfileImportErrorunittestSkipTestr   r   r   r   r   r   )r   r{   r   r   r   r     s    zTestServeViewWithSendfile.setUpc                 C   s   | j j  d S rL   r   r   r   r   r   r   r   r   )  s    z"TestServeViewWithSendfile.tearDownc                 C   s    | j td| jj| jjfdS r   rl   r   r   r   r   r$   .  s
    zTestServeViewWithSendfile.getc                 C   s   ddl m} |  d S r[   r]   r_   r   r   r   r`   5  s    z.TestServeViewWithSendfile.clear_sendfile_cachezsendfile.backends.xsendfile)SENDFILE_BACKENDc                 C   s8   |    |  }| |jd | |d | jjj d S )Nr'   z
X-Sendfile)r`   r$   r(   r)   r   r   r>   rV   r   r   r   test_sendfile_xsendfile_backend:  s    z9TestServeViewWithSendfile.test_sendfile_xsendfile_backendzsendfile.backends.mod_wsgiN)r   ZSENDFILE_ROOTZSENDFILE_URLc                 C   sD   |    |  }| |jd | |d tjtj| j	j
j d S )Nr'   ZLocationr`   r$   r(   r)   osr>   r   r   	MEDIA_URLr   r   namerV   r   r   r   test_sendfile_mod_wsgi_backendB  s    z8TestServeViewWithSendfile.test_sendfile_mod_wsgi_backendzsendfile.backends.nginxc                 C   sD   |    |  }| |jd | |d tjtj| j	j
j d S )Nr'   zX-Accel-Redirectr   rV   r   r   r   test_sendfile_nginx_backendQ  s    z5TestServeViewWithSendfile.test_sendfile_nginx_backendc                 C   s   |  |  d d d S )Nr/   r0   r6   r   r   r   r   r2   `  s    z6TestServeViewWithSendfile.test_content_security_policyc                 C   s   |  |  d d d S r3   r6   r   r   r   r   r7   c  s    z4TestServeViewWithSendfile.test_no_sniff_content_type)ra   rb   rc   r   r   r$   r`   r   r   r   Z
MEDIA_ROOTr   r   r   r2   r7   r   r   r   r   rz     s(   




rz   c                   @   s@   e Zd Zdd Zdd Zdd Zededd	d
 ZdS )TestServeWithUnicodeFilenamec                 C   sP   t jdd| _d| _z| jj| jtd W n tyJ   t	dY n0 d S )Nr   rg   u   docsالكاتدراrh   z,Filesystem doesn't support unicode filenames)
r   r   r   r&   r   r   r   UnicodeEncodeErrorr}   r~   r   r   r   r   r   i  s    
z"TestServeWithUnicodeFilename.setUpc                 C   s   | j j  d S rL   r   r   r   r   r   r   u  s    z%TestServeWithUnicodeFilename.tearDownc                 C   s0   | j td| jj| jfd}| |jd d S )Nr    r!   r'   )r#   r$   r   r   r%   r&   r(   r)   rV   r   r   r   r*   z  s    z/TestServeWithUnicodeFilename.test_response_coder8   r9   c                 C   s   t  }d|_td|_d|j_d|j_||_g |j_| j	
td| jj|jfd}| |jd z|d d W n ty   | d	 Y n0 z|d d
 W n ty   | d Y n0 dS )z
        Tests that a unicode 'Content-Disposition' header (for a response using
        a storage backend that doesn't expose filesystem paths) doesn't cause an
        error if encoded differently.
        u	   TÈST.docr:   Nr    r!   r'   r+   asciizBContent-Disposition with unicode characters failed ascii encoding.zlatin-1zDContent-Disposition with unicode characters failed latin-1 encoding.)r   r;   r&   r   r   r>   r?   r@   rA   r#   r$   r   r   r%   r(   r)   encodeUnicodeDecodeErrorZfailrF   r   r   r   <test_non_local_filesystem_unicode_content_disposition_header  s.    

zYTestServeWithUnicodeFilename.test_non_local_filesystem_unicode_content_disposition_headerN)	ra   rb   rc   r   r   r*   r   rd   r   r   r   r   r   r   g  s   r   )os.pathr   r}   rC   r   Zdjango.confr   Zdjango.core.files.baser   Zdjango.testr   r   Zdjango.urlsr   Zwagtail.documentsr   r
   rf   rt   rw   rx   rz   r   r   r   r   r   <module>   s<    %	M