U
    {h8                     @   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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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   j/var/www/dating/data/www/fatepal.com/env/lib/python3.8/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_header#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S )
        Tests the 'Content-Disposition' header in a response when using a
        storage backend that doesn't expose filesystem paths.
           file-like objectfile-like objectfile-like objectfile-like objectfile-like objectfile-like objectfile-like objectfile-like objectfile-like objectfile-like objectNr'   r+   z.attachment; filename={0}; filename*=UTF-8''{0})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   4test_non_local_filesystem_content_disposition_header=   s"    




zBTestServeView.test_non_local_filesystem_content_disposition_headerc                 C   s|   t  }| jj|_| jj|_| jj|_td|_d|j_d|j_	||_
g |j_
| | j}| |jd | |d d dS )r1   r2   Nr'   r+   inline)r   r3   r   r&   r4   r5   r   r   r6   r7   r8   r9   r$   r(   r)   r>   r   r   r   ;test_non_local_filesystem_inline_content_disposition_header^   s    




zITestServeView.test_non_local_filesystem_inline_content_disposition_headerc                 C   s   |  |  d d d S )NzContent-LengthZ25r(   r$   r   r   r   r   test_content_length_headerz   s    z(TestServeView.test_content_length_headerc                 C   s   |  |  d d d S )NzContent-Typezapplication/mswordrB   r   r   r   r   test_content_type_header}   s    z&TestServeView.test_content_type_headerc                 C   s   |  |  j d S N)Z
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   sz   t  }tj| zR|   | |jd | |j	d d d tj
 | |j	d d d | j W 5 tj| X d S )N   r      Zsenderinstance)r   Z	MagicMockr   Zdocument_servedconnectZ
disconnectr$   r(   Z
call_countZ
mock_callsr   r   )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!   rM   )r#   r$   r   r   r%   r(   r)   rN   r   r   r   test_with_incorrect_filename   s    z*TestServeView.test_with_incorrect_filenamec                 C   s   |  |  d d d S )NETagz"123456"rB   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.sendfilerT   clearr   rT   r   r   r   clear_sendfile_cache   s    z"TestServeView.clear_sendfile_cache)N)__name__
__module____qualname__r   r   r$   r*   r-   r.   r   patchr?   rA   rC   rD   rF   rG   rL   rO   rP   rR   rX   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 rE   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 rE   r(   r   r7   rb   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   r7   rN   r   r   r   test_redirect   s      z'TestServeViewWithRedirect.test_redirectN)rY   rZ   r[   r   r   r$   rf   rj   r   r   r   r   r^      s
   
r^   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   r_   zdirect_document_urls.docr`   )r   r   r   r   r   r   r   r   r   r   r      s
    zTestDirectDocumentUrls.setUpc                 C   s   | j   d S rE   rc   r   r   r   r   r      s    zTestDirectDocumentUrls.tearDownc                 C   s    | j td| jj| jjfdS r   rd   r   r   r   r   r$      s     zTestDirectDocumentUrls.getc                 C   s   |  | jj| jjj d S rE   )r(   r   r7   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 rg   rh   rN   r   r   r   rj      s      z$TestDirectDocumentUrls.test_redirectN)rY   rZ   r[   r   r   r$   rm   rj   r   r   r   r   rl      s
   rl   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   r_   zserve_with_external_storage.docr`   r    r!   ra   r   r   r   r   r      s     z"TestServeWithExternalStorage.setUpc                 C   s   | j   d S rE   rc   r   r   r   r   r     s    z%TestServeWithExternalStorage.tearDownc                 C   s   |  | jj| j d S rE   re   r   r   r   r   rf     s    zITestServeWithExternalStorage.test_document_url_should_point_to_serve_viewc                 C   s(   | j | j}| j|| jjjdd d S rg   )r#   r$   rb   ri   r   r   r7   rN   r   r   r   rj     s      z*TestServeWithExternalStorage.test_redirectN)rY   rZ   r[   __doc__r   r   rf   rj   r   r   r   r   rp      s
   

rp   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S )TestServeViewWithSendfilec                 C   sR   zdd l }W n tk
r*   tdY nX tjdd| _| jjdt	d d S )Nr   zdjango-sendfile not installedr   r_   zserve_view_with_sendfile.docr`   )
sendfileImportErrorunittestSkipTestr   r   r   r   r   r   )r   rs   r   r   r   r     s    zTestServeViewWithSendfile.setUpc                 C   s   | j j  d S rE   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   rd   r   r   r   r   r$   +  s     zTestServeViewWithSendfile.getc                 C   s   ddl m} |  d S rS   rU   rW   r   r   r   rX   2  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)rX   r$   r(   r)   r   r   r6   rN   r   r   r   test_sendfile_xsendfile_backend7  s    z9TestServeViewWithSendfile.test_sendfile_xsendfile_backendzsendfile.backends.mod_wsgiN)rx   ZSENDFILE_ROOTZSENDFILE_URLc                 C   sD   |    |  }| |jd | |d tjtj| j	j
j d S )Nr'   ZLocationrX   r$   r(   r)   osr6   r   r   	MEDIA_URLr   r   namerN   r   r   r   test_sendfile_mod_wsgi_backend?  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{   rN   r   r   r   test_sendfile_nginx_backendN  s    z5TestServeViewWithSendfile.test_sendfile_nginx_backend)rY   rZ   r[   r   r   r$   rX   r   ry   r   Z
MEDIA_ROOTr}   r   r   r   r   r   r   rr     s$   


rr   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   sR   t jdd| _d| _z| jj| jtd W n tk
rL   t	dY nX d S )Nr   r_   u   docsالكاتدراr`   z,Filesystem doesn't support unicode filenames)
r   r   r   r&   r   r   r   UnicodeEncodeErrorru   rv   r   r   r   r   r   `  s     z"TestServeWithUnicodeFilename.setUpc                 C   s   | j j  d S rE   rw   r   r   r   r   r   l  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)   rN   r   r   r   r*   q  s    z/TestServeWithUnicodeFilename.test_response_coder/   r0   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k
r   | d	 Y nX z|d d
 W n tk
r   | d Y nX 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.docr2   Nr    r!   r'   r+   asciizBContent-Disposition with unicode characters failed ascii encoding.zlatin-1zDContent-Disposition with unicode characters failed latin-1 encoding.)r   r3   r&   r   r   r6   r7   r8   r9   r#   r$   r   r   r%   r(   r)   encodeUnicodeDecodeErrorZfailr>   r   r   r   <test_non_local_filesystem_unicode_content_disposition_headerw  s.    

zYTestServeWithUnicodeFilename.test_non_local_filesystem_unicode_content_disposition_headerN)	rY   rZ   r[   r   r   r*   r   r\   r   r   r   r   r   r   ^  s   r   )os.pathr|   ru   r;   r   Zdjango.confr   Zdjango.core.files.baser   Zdjango.testr   r   Zdjango.urlsr   Zwagtail.documentsr   r
   r^   rl   ro   rp   rr   r   r   r   r   r   <module>   s:    " 	G