o
    h:                     @   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 document123456)title	file_hashzserve_view.doc   A boring example documentzserve_view.pdf)r   Documentdocumentfilesaver   pdf_documentself r   b/var/www/html/ndineBlogger/venv/lib/python3.10/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.      z TestServeView.test_response_codec                 C   (   |  | | 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      z-TestServeView.test_content_disposition_headerc                 C   r.   )Nr/   zinline; filename="r0   )r*   r&   r   r(   r   r   r   r   &test_inline_content_disposition_header7   r2   z4TestServeView.test_inline_content_disposition_headerc                 C   sX   |  |  d d | jdd | d|  j W d    d S 1 s%w   Y  d S )NContent-Security-Policydefault-src 'none'F)"WAGTAILDOCS_BLOCK_EMBEDDED_CONTENT)r*   r&   r   assertNotInheadersr   r   r   r   test_content_security_policy=   s   "z*TestServeView.test_content_security_policyc                 C      |  |  d d d S NX-Content-Type-Optionsnosniffr*   r&   r   r   r   r   test_no_sniff_content_typeC      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}r4   r5   r<   r=   )r   Mockr   r(   content_typecontent_dispositionr   r   pathurlreturn_value	get_hooksr&   r*   r+   formaturllibparsequoter   mock_get_object_or_404
mock_hooks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 )	NrC   r)   r/   inliner4   r5   r<   r=   )r   rD   r   r(   rE   rF   r   r   rG   rH   rI   rJ   r&   r*   r+   rO   r   r   r   (test_non_local_filesystem_inline_headersc   s   



z6TestServeView.test_non_local_filesystem_inline_headersc                 C   r:   )NzContent-Length25r>   r   r   r   r   test_content_length_header}   r@   z(TestServeView.test_content_length_headerc                 C   r:   )NzContent-Typezapplication/mswordr>   r   r   r   r   test_content_type_header   r@   z&TestServeView.test_content_type_headerc                 C   s   |  |  j d S N)
assertTruer&   	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| z0|   | |jd | |jd d d tj	 | |jd d d | j
 W tj| d S tj| w )N   r      senderinstance)r   	MagicMockr   document_servedconnectr&   r*   
call_count
mock_callsr   r   
disconnect)r   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  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"   incorrectfilenamer#   rk   )r%   r&   r   r   r'   r*   r+   rl   r   r   r   test_with_incorrect_filename   s   z*TestServeView.test_with_incorrect_filenamec                 C   r:   )NETagz"123456"r>   r   r   r   r   test_has_etag_header   r@   z"TestServeView.test_has_etag_headerc                 C      ddl m} |  d S Nr   )_get_sendfilewagtail.utils.sendfilert   clearr   rt   r   r   r   clear_sendfile_cache      z"TestServeView.clear_sendfile_cacherY   )__name__
__module____qualname__r   r    r&   r,   r1   r3   r9   r?   r   patchrS   rU   rW   rX   r\   r]   ri   rm   ro   rq   ry   r   r   r   r   r
      s0    

r
   redirectc                   @   4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestServeViewWithRedirectc                 C   @   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         zTestServeViewWithRedirect.setUpc                 C      | j   d S rY   r   r   r   r   r   r   r          z"TestServeViewWithRedirect.tearDownc                 C       | j td| jj| jjfdS r!   r%   r&   r   r   r'   r(   r   r   r   r   r&      
   zTestServeViewWithRedirect.getc                 C      |  | jj| j d S rY   r*   r   rH   r   r   r   r   r   ,test_document_url_should_point_to_serve_view   r-   zFTestServeViewWithRedirect.test_document_url_should_point_to_serve_viewc                 C   "   |   }| j|| jjjdd d S NF)fetch_redirect_responser&   assertRedirectsr   r   rH   rl   r   r   r   test_redirect   s   
z'TestServeViewWithRedirect.test_redirectN)r{   r|   r}   r   r    r&   r   r   r   r   r   r   r      s    
r   directc                   @   r   )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   r   rY   r   r   r   r   r   r       r   zTestDirectDocumentUrls.tearDownc                 C   r   r!   r   r   r   r   r   r&      r   zTestDirectDocumentUrls.getc                 C   s   |  | jj| jjj d S rY   )r*   r   rH   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   r   r   r   rl   r   r   r   r      s   
z$TestDirectDocumentUrls.test_redirectN)r{   r|   r}   r   r    r&   r   r   r   r   r   r   r      s    r   default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   r   )Nr   r   zserve_with_external_storage.docr   r"   r#   r   r   r   r   r   r     r   z"TestServeWithExternalStorage.setUpc                 C   r   rY   r   r   r   r   r   r      r   z%TestServeWithExternalStorage.tearDownc                 C   r   rY   r   r   r   r   r   r     r-   zITestServeWithExternalStorage.test_document_url_should_point_to_serve_viewc                 C   s(   | j | j}| j|| jjjdd d S r   )r%   r&   r   r   r   r   rH   rl   r   r   r   r     s   
z*TestServeWithExternalStorage.test_redirectN)r{   r|   r}   __doc__r   r    r   r   r   r   r   r   r      s    

r   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   sL   zdd l }W n ty   tdw 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   r   r   r   r   r     s   
zTestServeViewWithSendfile.setUpc                 C      | j j  d S rY   r   r   r   r   r   r   r   r    )     z"TestServeViewWithSendfile.tearDownc                 C   r   r!   r   r   r   r   r   r&   .  r   zTestServeViewWithSendfile.getc                 C   rr   rs   ru   rx   r   r   r   ry   5  rz   z.TestServeViewWithSendfile.clear_sendfile_cachezsendfile.backends.xsendfile)SENDFILE_BACKENDc                 C   s8   |    |  }| |jd | |d | jjj d S )Nr)   z
X-Sendfile)ry   r&   r*   r+   r   r   rG   rl   r   r   r   test_sendfile_xsendfile_backend:  s   z9TestServeViewWithSendfile.test_sendfile_xsendfile_backendzsendfile.backends.mod_wsgiN)r   SENDFILE_ROOTSENDFILE_URLc                 C   D   |    |  }| |jd | |d tjtj| j	j
j d S )Nr)   Locationry   r&   r*   r+   osrG   r   r   	MEDIA_URLr   r   namerl   r   r   r   test_sendfile_mod_wsgi_backendB     z8TestServeViewWithSendfile.test_sendfile_mod_wsgi_backendzsendfile.backends.nginxc                 C   r   )Nr)   zX-Accel-Redirectr   rl   r   r   r   test_sendfile_nginx_backendQ  r   z5TestServeViewWithSendfile.test_sendfile_nginx_backendc                 C   r:   )Nr4   r5   r>   r   r   r   r   r9   `  r@   z6TestServeViewWithSendfile.test_content_security_policyc                 C   r:   r;   r>   r   r   r   r   r?   c  r@   z4TestServeViewWithSendfile.test_no_sniff_content_type)r{   r|   r}   r   r    r&   ry   r   r   r   
MEDIA_ROOTr   r   r   r9   r?   r   r   r   r   r     s*    




r   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   sL   t jdd| _d| _	 z| jj| jtd W d S  ty%   t	dw )Nr   r   u   docsالكاتدراr   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   r   rY   r   r   r   r   r   r    u  r   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+   rl   r   r   r   r,   z  s   z/TestServeWithUnicodeFilename.test_response_coderA   rB   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yH   | d	 Y nw z
|d d
 W dS  tyb   | d Y dS w )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.docrC   Nr"   r#   r)   r/   asciizBContent-Disposition with unicode characters failed ascii encoding.zlatin-1zDContent-Disposition with unicode characters failed latin-1 encoding.)r   rD   r(   r   r   rG   rH   rI   rJ   r%   r&   r   r   r'   r*   r+   encodeUnicodeDecodeErrorfailrO   r   r   r   <test_non_local_filesystem_unicode_content_disposition_header  s2   

zYTestServeWithUnicodeFilename.test_non_local_filesystem_unicode_content_disposition_headerN)	r{   r|   r}   r   r    r,   r   r~   r   r   r   r   r   r   g  s    r   )os.pathr   r   rL   r   django.confr   django.core.files.baser   django.testr   r   django.urlsr   wagtail.documentsr   r
   r   r   r   r   r   r   r   r   r   r   <module>   s>     %	M