B
    3¢\†  ã               @   s”   d Z ddlZddlZddlmZ ejjZddlZddl	Z	ddl
mZ ddlmZmZmZmZ ddlmZ ddlmZ dd	lmZ G d
d„ deƒZdS )zTest the /files/ handler.é    N)Ú	normalize)Úwrite)Únew_notebookÚnew_markdown_cellÚnew_code_cellÚ
new_output)Úurl_path_joiné   )ÚNotebookTestBase)Ú	py3compatc               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )Ú	FilesTestc       
   
   C   s\  ddg}ddg}|| }| j }x‚|D ]z}t|| dtj¡ƒ}tj |¡sRt |¡ tt|dƒdƒ}| 	d¡ W d Q R X tt|dƒdƒ}| 	d¡ W d Q R X q$W xl|D ]d}t|| dtj¡ƒ}|  
d	td
|dƒ¡}| ¡  |  |jd¡ |  
d	td
|dƒ¡}|  |jd¡ q¨W xT|D ]L}t|| dtj¡ƒ}x0dD ](}	|  
d	td
||	ƒ¡}|  |jd¡ q4W qW d| jj_zÚxv|D ]n}t|| dtj¡ƒ}|  
d	td
|dƒ¡}| ¡  |  |jd¡ |  
d	td
|dƒ¡}| ¡  |  |jd¡ qxW x\|D ]T}t|| dtj¡ƒ}x8dD ]0}	|  
d	td
||	ƒ¡}| ¡  |  |j|	¡ qW qðW W d d| jj_X d S )Nu   Ã¥ bu
   Ã¥ b/Ã§. du   .Ã¥ bu
   Ã¥ b/.Ã§ dú/ÚfooÚwz.fooÚGETÚfilesi”  )r   z.fooTF)Únotebook_dirÚpjoinÚreplaceÚosÚsepÚpathÚexistsÚmkdirÚopenr   Úrequestr   Zraise_for_statusÚassertEqualÚtextÚstatus_codeZnotebookZcontents_managerZallow_hidden)
ÚselfZ
not_hiddenZhiddenÚdirsÚnbdirÚdr   ÚfÚrr   © r%   ú8lib/python3.7/site-packages/notebook/tests/test_files.pyÚtest_hidden_files   sV    








zFilesTest.test_hidden_filesc          	   C   sŠ  | j }ttdƒtdtdddgdgd}tjt|dƒd	d
d}t||dd W dQ R X t t|dƒd¡"}| dt	 
d¡ ¡ | ¡  W dQ R X t t|dƒd	¡}| d¡ | ¡  W dQ R X |  dd¡}|  |jd¡ |  d|j¡ t |j¡ |  dd¡}|  |jd¡ |  |jd d¡ |  |jdd… d¡ |  t|jƒd¡ |  dd¡}|  |jd¡ |  |jd d¡ |  |jd¡ dS )z@make sure ContentsManager returns right files (ipynb, bin, txt).u   Created by test Â³z
print(2*6)ÚstreamZ12)r   )Zoutputs)Zcellsztestnb.ipynbr   zutf-8)Úencodingé   )ÚversionNztest.binÚwbó   ÿé   ztest.txtZfoobarr   zfiles/testnb.ipynbéÈ   zfiles/test.binzcontent-typezapplication/octet-streamr	   é   zfiles/test.txtztext/plain; charset=UTF-8)r   r   r   r   r   Úior   r   r   r   ÚurandomÚcloser   r   r   ÚassertInr   ÚjsonÚloadsÚheadersZcontentÚlen)r   r!   Znbr#   r$   r%   r%   r&   Útest_contents_managerO   s6    

zFilesTest.test_contents_managerc          	   C   sŒ   | j }d}tt|dƒdƒ}| |¡ W d Q R X |  dd¡}|j dd¡}|  d|¡ |  dd	¡}|j dd¡}|  d|¡ |  d
|¡ d S )NZhelloztest.txtr   r   zfiles/test.txtzContent-DispositionÚ Z
attachmentzfiles/test.txt?download=1zfilename*=utf-8''test.txt)	r   r   r   r   r   r7   ÚgetZassertNotInr4   )r   r!   r   r#   r$   Zdispositionr%   r%   r&   Útest_downloady   s    zFilesTest.test_downloadc          	   C   sN   | j }d}tt|dƒdƒ}| |¡ W d Q R X |  dd¡}|  |jd¡ d S )Nz<div>Test test</div>z	test.htmlr   r   zview/test.htmlr/   )r   r   r   r   r   r   r   )r   r!   Zhtmlr#   r$   r%   r%   r&   Útest_view_html‰   s    zFilesTest.test_view_htmlc          
   C   s   | j }t t|dƒ¡ t t|ddƒ¡ xpdD ]f}tt||ddƒdƒ}| |d ¡ W dQ R X tt||ddƒdƒ}| |d	 ¡ W dQ R X tt||dƒdƒ}| |d
 ¡ W dQ R X tt||dƒdƒ}| |d ¡ W dQ R X td|ddƒ}|  d|¡}|  	|j
d¡ |  	|j|d ¡ td|ddƒ}|  d|¡}|  	|j
d¡ |  	|j|d	 ¡ td|ddƒ}|  d|¡}|  	|j
d¡ |  	|j|d ¡ q0W dS )z7pre-2.0 'files/' prefixed links are properly redirectedr   Úsub)r:   r>   zf1.txtr   z	/files/f1Nzf2.txtz	/files/f2z/f2zf3.txtz/f3Z	notebooksr   r/   )r   r   r   r   Úmakedirsr   r   r   r   r   r   r   )r   r!   Úprefixr#   Zurlr$   r%   r%   r&   Útest_old_files_redirect“   s0    z!FilesTest.test_old_files_redirectN)Ú__name__Ú
__module__Ú__qualname__r'   r9   r<   r=   rA   r%   r%   r%   r&   r      s
   7*
r   )Ú__doc__r1   r   Zunicodedatar   r   Újoinr   Zrequestsr5   Znbformatr   Znbformat.v4r   r   r   r   Znotebook.utilsr   Zlaunchnotebookr
   Zipython_genutilsr   r   r%   r%   r%   r&   Ú<module>   s   