U
    ÓZj™B  ã                   @   sâ   d Z ddlZddlZddlZ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gZdd	d
gZdddgZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZedkrÞe ¡  dS )z!Test :mod:`certbot.display.util`.é    N)Úerrors)Ú
interfaces)Úutil)ZFirstZDescription1)ZSecondZDescription2Útag1Útag2Útag3)Ú1r   )Ú2r   )Ú3r   c                   @   sH   e Zd ZdZedd„ ƒZdd„ Zddd„Ze 	d	¡d
d„ ƒZ
dd„ ZdS )ÚInputWithTimeoutTestz2Tests for certbot.display.util.input_with_timeout.c                 O   s   ddl m} |||ŽS )Nr   )Úinput_with_timeout)Úcertbot.display.utilr   )ÚclsÚargsÚkwargsr   © r   úb/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/certbot/tests/display/util_test.pyÚ_call   s    zInputWithTimeoutTest._callc              
   C   sB   t  d¡�.}tjd|d�� |  t| j¡ W 5 Q R X W 5 Q R X d S )Nzr+úcertbot.display.util.sys.stdin)Únew)ÚtempfileÚTemporaryFileÚmockÚpatchÚassertRaisesÚEOFErrorr   )ÚselfÚfr   r   r   Útest_eof   s    zInputWithTimeoutTest.test_eofNc              	   C   sL   d}t  |d ¡}t d¡�&}|gg g f|_|  |  |¡|¡ W 5 Q R X d S )Nzfoo barÚ
z!certbot.compat.misc.select.select)ÚsixÚStringIOr   r   Úreturn_valueÚassertEqualr   )r   ÚpromptÚexpectedÚstdinZmock_selectr   r   r   Ú
test_input   s
    zInputWithTimeoutTest.test_inputzcertbot.display.util.sys.stdoutc                 C   s(   d}|   |¡ |j |¡ |j ¡  d S )Nztest prompt: )r'   ÚwriteZassert_called_once_withÚflush)r   Úmock_stdoutr$   r   r   r   Útest_input_with_prompt&   s    
z+InputWithTimeoutTest.test_input_with_promptc              	   C   s\   t   t jt j¡}| d¡ | d¡ t d|¡� | jtj	| j
dd� W 5 Q R X | ¡  d S )N)Ú r   é   r   gü©ñÒMbP?)Útimeout)ÚsocketÚAF_INETÚSOCK_STREAMÚbindÚlistenr   r   r   r   ÚErrorr   Úclose)r   r&   r   r   r   Útest_timeout-   s    

z!InputWithTimeoutTest.test_timeout)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr   r   r'   r   r   r+   r6   r   r   r   r   r      s   


r   c                       s<  e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Z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e	 
d¡d d!„ ƒZe	 
d¡d"d#„ ƒZe	 
d¡d$d%„ ƒZe	 
d¡d&d'„ ƒZd(d)„ Zd*d+„ Ze	 
d¡d,d-„ ƒZd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Z d:d;„ Z!d<d=„ Z"‡  Z#S )>ÚFileOutputDisplayTestz®Test stdout display.

    Most of this class has to deal with visual output.  In order to test how the
    functions look to a user, uncomment the test_visual function.

    c                    s,   t t| ƒ ¡  t ¡ | _t | jd¡| _d S )NF)	Úsuperr<   ÚsetUpr   Ú	MagicMockr*   Údisplay_utilZFileDisplayÚ	displayer©r   ©Ú	__class__r   r   r>   >   s    
zFileOutputDisplayTest.setUpc                 C   s2   | j  dd¡ | jjjd d }|  d|k¡ d S )NÚmessageFr   ©rA   Únotificationr*   r(   Ú	call_argsÚ
assertTrue©r   Ústringr   r   r   Útest_notification_no_pauseC   s    z0FileOutputDisplayTest.test_notification_no_pausec              	   C   sN   d}t j|dd�� | jjddd� W 5 Q R X |  d| jjjd d k¡ d S )Nú'certbot.display.util.input_with_timeoutZenter©r"   rE   T©Úforce_interactiver   )r   r   rA   rG   rI   r*   r(   rH   ©r   r   r   r   r   Útest_notification_pauseI   s    z-FileOutputDisplayTest.test_notification_pausec                 C   s4   |   | jjd¡ | jjjd d }|  d|k¡ d S )NrE   r   )Ú_force_noninteractiverA   rG   r*   r(   rH   rI   rJ   r   r   r   Ú test_notification_noninteractiveP   s    z6FileOutputDisplayTest.test_notification_noninteractivec                 C   sr   |   | jjd¡ | jjjd d }|  d|k¡ |  | jj¡ |   | jjd¡ | jjjd d }|  d|k¡ d S )NrE   r   Zmessage2)rS   rA   rG   r*   r(   rH   rI   Úskipped_interactionrJ   r   r   r   Ú!test_notification_noninteractive2U   s    z7FileOutputDisplayTest.test_notification_noninteractive2z3certbot.display.util.FileDisplay._get_valid_int_ansc                 C   s4   t jdf|_| jjdtdd�}|  |t jdf¡ d S )Nr-   rE   TrO   r   )r@   ÚOKr"   rA   ÚmenuÚCHOICESr#   )r   Zmock_ansÚretr   r   r   Ú	test_menub   s    zFileOutputDisplayTest.test_menuc                 C   s0   d}| j | jjdt|d�}|  |tj|f¡ d S )Nr   Úmsg©Údefault)rS   rA   rX   rY   r#   r@   rW   )r   r^   Úresultr   r   r   Útest_menu_noninteractivei   s       ÿz.FileOutputDisplayTest.test_menu_noninteractivec              	   C   sD   d}t j|dd�� | jjddd�\}}W 5 Q R X |  |tj¡ d S )NrM   ÚcrN   rE   TrO   )r   r   rA   ÚinputrI   r@   ÚCANCEL)r   r   ÚcodeÚ_r   r   r   Útest_input_cancelo   s    z'FileOutputDisplayTest.test_input_cancelc              	   C   sP   d}t j|dd�� | jjddd�\}}W 5 Q R X |  |tj¡ |  |d¡ d S )NrM   z
domain.comrN   rE   TrO   )r   r   rA   rb   r#   r@   rW   )r   r   rd   Úinput_r   r   r   Útest_input_normalv   s
    z'FileOutputDisplayTest.test_input_normalc                 C   s:   d}| j | jjd|d�\}}|  |tj¡ |  ||¡ d S )NZfoorE   r]   )rS   rA   rb   r#   r@   rW   ©r   r^   rd   rg   r   r   r   Útest_input_noninteractive~   s      ÿ
z/FileOutputDisplayTest.test_input_noninteractivec                 C   s   | j t| j| jjddd� d S )NrE   ú--flag©Zcli_flag)r   Ú	ExceptionrS   rA   rb   rB   r   r   r   Útest_input_assertion_fail†   s
    
  ÿz/FileOutputDisplayTest.test_input_assertion_failc              	   C   s6   t  d¡�" | jtj| j| jjddd� W 5 Q R X d S )Nz&certbot.display.util.assert_valid_callr\   rk   rl   )r   r   r   r   r4   rS   rA   rb   rB   r   r   r   Útest_input_assertion_fail2Œ   s      ÿz0FileOutputDisplayTest.test_input_assertion_fail2c              	   C   s2  d}t j|dd�� |  | jjddd�¡ W 5 Q R X t j|dd�� |  | jjddd�¡ W 5 Q R X t j|ddgd	�� |  | jjddd�¡ W 5 Q R X t j|d
d�� |  | jjddd�¡ W 5 Q R X t j|ddgd	�� |  | jjddd�¡ W 5 Q R X t j|dd�� |  | jjdddd�¡ W 5 Q R X d S )NrM   ZYesrN   rE   TrO   ÚyÚmaybe©Úside_effectZNoÚcancelÚnÚar\   ZAgree)Z	yes_labelrP   )r   r   rI   rA   ÚyesnoÚassertFalserQ   r   r   r   Ú
test_yesno‘   s@    
 ÿ
 ÿ
 ÿ
 ÿ
 ÿ
  ÿz FileOutputDisplayTest.test_yesnoc                 C   s   |   | j| jjddd�¡ d S )NrE   Tr]   )rI   rS   rA   rw   rB   r   r   r   Útest_yesno_noninteractive§   s
      ÿz/FileOutputDisplayTest.test_yesno_noninteractiverM   c                 C   sB   d|_ | jjdtdd�\}}|  |t|ƒftjtddgƒf¡ d S )Nz2 1r\   TrO   r   r   ©r"   rA   Ú	checklistÚTAGSr#   Úsetr@   rW   ©r   Ú
mock_inputrd   Ztag_listr   r   r   Útest_checklist_valid«   s      ÿ

 ÿz*FileOutputDisplayTest.test_checklist_validc                 C   sD   d|_ | jjdtdd�\}}|  |t|ƒftjtdddgƒf¡ d S )Nr,   r\   TrO   r   r   r   r{   r   r   r   r   Útest_checklist_empty³   s    
 ÿz*FileOutputDisplayTest.test_checklist_emptyc                 C   s6   dddg|_ | jjdtdd�}|  |tjdgf¡ d S )NÚ10ztag1 pleaser   r\   TrO   r   )rs   rA   r|   r}   r#   r@   rW   ©r   r€   rZ   r   r   r   Útest_checklist_miss_validº   s    z/FileOutputDisplayTest.test_checklist_miss_validc                 C   s2   ddg|_ | jjdtdd�}|  |tjg f¡ d S )Nrƒ   ra   r\   TrO   )rs   rA   r|   r}   r#   r@   rc   r„   r   r   r   Útest_checklist_miss_quitÁ   s    
z.FileOutputDisplayTest.test_checklist_miss_quitc                 C   s<   t }| j| jjdt |d�\}}|  |tj¡ |  ||¡ d S )Nr\   r]   )r}   rS   rA   r|   r#   r@   rW   ri   r   r   r   Útest_checklist_noninteractiveÈ   s       ÿ
z3FileOutputDisplayTest.test_checklist_noninteractivec                 C   sn   dgdddgddgg}t dgƒt ddgƒt ddgƒg}t|ƒD ]*\}}t | j |t¡ƒ}|  ||| ¡ q>d S )Nr   r	   r
   r   r   r   )r~   Ú	enumeraterA   Ú_scrub_checklist_inputr}   r#   )r   ÚindicesÚexpÚiÚlist_Zset_tagsr   r   r   Ú test_scrub_checklist_input_validÐ   s    ý

ýÿz6FileOutputDisplayTest.test_scrub_checklist_input_validc                 C   s8   ddddg}d}||_ | jj|Ž }|  |tj|f¡ d S )Nr\   ú/var/www/htmlrk   T)r"   rA   Údirectory_selectr#   r@   rW   )r   r€   r   Z
user_inputZreturnedr   r   r   Útest_directory_selectá   s
    z+FileOutputDisplayTest.test_directory_selectc                 C   s:   d}| j | jjd|d�\}}|  |tj¡ |  ||¡ d S )Nr�   r\   r]   )rS   rA   r�   r#   r@   rW   ri   r   r   r   Ú$test_directory_select_noninteractiveê   s      ÿ
z:FileOutputDisplayTest.test_directory_select_noninteractivec              
   O   sn   | j j}t d¡�.}d|j_t d¡�}|||Ž}W 5 Q R X W 5 Q R X |rZ|  |jj¡ n|  	|jj
d¡ |S )Nr   Fzcertbot.display.util.loggerr-   )rA   rU   r   r   Úisattyr"   rx   ÚwarningZcalledr#   Z
call_count)r   Úfuncr   r   rU   Z
mock_stdinZmock_loggerr_   r   r   r   rS   ò   s    z+FileOutputDisplayTest._force_noninteractivec                 C   s@   dgdgdgddgddgg}|D ]}|   | j |t¡g ¡ q d S )NÚ0Ú4r   r   r	   Úo)r#   rA   r‰   r}   )r   rŠ   r�   r   r   r   Ú"test_scrub_checklist_input_invalid  s    û ÿz8FileOutputDisplayTest.test_scrub_checklist_input_invalidc                 C   s    | j  dt¡ | j  dt¡ d S )Nr\   )rA   Z_print_menurY   r}   rB   r   r   r   Útest_print_menu  s    z%FileOutputDisplayTest.test_print_menuc                 C   s*   d  d¡}t |¡}|  | d¡d¡ d S )Nz·This is just a weak test{0}This function is only meant to be for easy viewing{0}Test a really really really really really really really really really really really really long line...r   é   )Úformatr@   Z_wrap_linesr#   Úcount)r   r\   Útextr   r   r   Útest_wrap_lines  s
    ý
z%FileOutputDisplayTest.test_wrap_linesc              	   C   sx   d}t j|dd��  |  | j d¡tjdf¡ W 5 Q R X d}t j||d��$ |  | j d¡tjt|ƒf¡ W 5 Q R X d S )NrM   r   rN   r-   r	   r›   )r   r   r#   rA   Ú_get_valid_int_ansr@   rW   Úint)r   r   Úansr   r   r   Útest_get_valid_int_ans_valid  s    
 ÿ
þz2FileOutputDisplayTest.test_get_valid_int_ans_validc              
   C   s\   ddgdddgdgg}d}|D ]8}t j||d��  |  | j d¡tjd	f¡ W 5 Q R X qd S )
Nr–   ra   r—   ÚoneÚCrM   rr   r›   éÿÿÿÿ)r   r   r#   rA   r    r@   rc   )r   Zanswersr   r¢   r   r   r   Útest_get_valid_int_ans_invalid*  s    ý
þz4FileOutputDisplayTest.test_get_valid_int_ans_invalidc                 C   sH   t j ¡ D ]8}tjrtj}ntj}|t| j	|ƒƒ}|  
d|jk¡ q
d S )NrP   )r   ÚIDisplayÚnamesr    ÚPY2ÚinspectÚ
getargspecÚgetfullargspecÚgetattrrA   rI   r   )r   Únamer¬   Zarg_specr   r   r   Ú#test_methods_take_force_interactive8  s    z9FileOutputDisplayTest.test_methods_take_force_interactive)$r7   r8   r9   r:   r>   rL   rR   rT   rV   r   r   r[   r`   rf   rh   rj   rn   ro   ry   rz   r�   r‚   r…   r†   r‡   rŽ   r‘   r’   rS   r™   rš   rŸ   r£   r§   r°   Ú__classcell__r   r   rC   r   r<   6   sH   






r<   c                       sX   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Z‡  ZS )ÚNoninteractiveDisplayTestz:Test non-interactive display. These tests are pretty easy!c                    s*   t t| ƒ ¡  t ¡ | _t | j¡| _d S ©N)	r=   r²   r>   r   r?   r*   r@   ZNoninteractiveDisplayrA   rB   rC   r   r   r>   F  s    
zNoninteractiveDisplayTest.setUpc                 C   s2   | j  dd¡ | jjjd d }|  d|k¡ d S )NrE   é
   r   rF   rJ   r   r   r   rL   K  s    z4NoninteractiveDisplayTest.test_notification_no_pausec                 C   s>   d}| j jd|d�}|  |tj|f¡ |  tj| j jd¡ d S )Nzan incomputable valuerE   r]   )rA   rb   r#   r@   rW   r   r   ÚMissingCommandlineFlag©r   ÚdrZ   r   r   r   r'   Q  s    z$NoninteractiveDisplayTest.test_inputc                 C   s>   | j jdtdd�}|  |tjdf¡ |  tj| j jdt¡ d S )NrE   r-   r]   )	rA   rX   rY   r#   r@   rW   r   r   rµ   )r   rZ   r   r   r   r[   W  s    z#NoninteractiveDisplayTest.test_menuc                 C   s8   d}| j jd|d�}|  ||¡ |  tj| j jd¡ d S )NFrE   r]   )rA   rw   r#   r   r   rµ   r¶   r   r   r   ry   \  s    z$NoninteractiveDisplayTest.test_yesnoc                 C   sF   ddg}| j jdt|d�}|  |tj|f¡ |  tj| j jdt¡ d S )Nr-   r›   rE   r]   )	rA   r|   r}   r#   r@   rW   r   r   rµ   r¶   r   r   r   Útest_checklistb  s    z(NoninteractiveDisplayTest.test_checklistc                 C   s@   d}t j|f}| j d|¡}|  ||¡ |  tj| jjd¡ d S )Nr�   r\   )r@   rW   rA   r�   r#   r   r   rµ   )r   r^   r%   Úactualr   r   r   r‘   h  s    
  ÿz/NoninteractiveDisplayTest.test_directory_selectc                 C   s\   t j ¡ D ]L}t| j|ƒ}tjr<t |¡j	}|  
|d k¡ q
t |¡j}|  
|d k¡ q
d S r³   )r   r¨   r©   r®   rA   r    rª   r«   r¬   Úkeywordsrx   r­   Úvarkw)r   r¯   Úmethodr_   r   r   r   Útest_methods_take_kwargsq  s    z2NoninteractiveDisplayTest.test_methods_take_kwargs)r7   r8   r9   r:   r>   rL   r'   r[   ry   r¸   r‘   r½   r±   r   r   rC   r   r²   D  s   	r²   c                   @   sD   e Zd ZdZdd„ Zedd„ ƒZdd„ Zdd	„ Zd
d„ Z	dd„ Z
dS )ÚSeparateListInputTestzTest Module functions.c                 C   s   ddddg| _ d S )Nrv   Úbra   Útest)r‹   rB   r   r   r   r>   …  s    zSeparateListInputTest.setUpc                 C   s   ddl m} ||ƒS )Nr   )Úseparate_list_input)r   rÁ   )r   rg   rÁ   r   r   r   r   ˆ  s    zSeparateListInputTest._callc                 C   s   |   |  d¡| j¡ d S )Nz
a,b,c,test©r#   r   r‹   rB   r   r   r   Útest_commas�  s    z!SeparateListInputTest.test_commasc                 C   s   |   |  d¡| j¡ d S )Nz
a b c testrÂ   rB   r   r   r   Útest_spaces�  s    z!SeparateListInputTest.test_spacesc                 C   s   |   |  d¡| j¡ d S )Nza, b, c, testrÂ   rB   r   r   r   Ú	test_both“  s    zSeparateListInputTest.test_bothc                 C   s8   |   d¡|   d¡|   d¡g}|D ]}|  || j¡ q d S )Nz  a , b    c 	 testz,a, ,, , b c  test  z,,,,, , a b,,, , c,test)r   r#   r‹   )r   r¹   Zactr   r   r   Ú	test_mess–  s    ýzSeparateListInputTest.test_messN)r7   r8   r9   r:   r>   r;   r   rÃ   rÄ   rÅ   rÆ   r   r   r   r   r¾   ƒ  s   
r¾   c                   @   s(   e Zd Zedd„ ƒZdd„ Zdd„ ZdS )ÚPlaceParensTestc                 C   s   ddl m} ||ƒS )Nr   )Ú_parens_around_char)r   rÈ   )r   ÚlabelrÈ   r   r   r   r   ¢  s    zPlaceParensTest._callc                 C   s   |   d|  d¡¡ d S )Nz(a)rv   ©r#   r   rB   r   r   r   Útest_single_letter§  s    z"PlaceParensTest.test_single_letterc                 C   s(   |   d|  d¡¡ |   d|  d¡¡ d S )Nz(L)abelÚLabelz(y)es pleasez
yes pleaserÊ   rB   r   r   r   Útest_multipleª  s    zPlaceParensTest.test_multipleN)r7   r8   r9   r;   r   rË   rÍ   r   r   r   r   rÇ   ¡  s   
rÇ   Ú__main__)r:   r«   r/   r   Zunittestr   r    Zcertbotr   r   Zcertbot.displayr   r@   rY   r}   ZTAGS_CHOICESZTestCaser   r<   r²   r¾   rÇ   r7   Úmainr   r   r   r   Ú<module>   s*   

#  ?