U
    ]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re  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   A/usr/lib/python3/dist-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)tempfileZ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   gMbP?)Ztimeout)socketZAF_INETZSOCK_STREAMZbindZ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*   r0   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 )>FileOutputDisplayTestzTest 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)	superr6   setUpr   	MagicMockr)   display_utilZFileDisplay	displayerr   	__class__r   r   r8   >   s    
zFileOutputDisplayTest.setUpc                 C   s2   | j dd | jjjd d }| d|k d S )NmessageFr   r;   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!   r?   Tforce_interactiver   )r   r   r;   rA   rC   r)   r'   rB   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 )Nr?   r   )_force_noninteractiver;   rA   r)   r'   rB   rC   rD   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 )Nr?   r   Zmessage2)rM   r;   rA   r)   r'   rB   rC   skipped_interactionrD   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,   r?   TrI   r   )r:   OKr!   r;   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)rM   r;   rR   rS   r"   r:   rQ   )r   rX   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 )NrG   crH   r?   TrI   )r   r   r;   inputrC   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 )NrG   z
domain.comrH   r?   TrI   )r   r   r;   r\   r"   r:   rQ   )r   r   r^   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foor?   rW   )rM   r;   r\   r"   r:   rQ   r   rX   r^   ra   r   r   r   test_input_noninteractive~   s      
z/FileOutputDisplayTest.test_input_noninteractivec                 C   s   | j t| j| jjddd d S )Nr?   --flagZcli_flag)r   	ExceptionrM   r;   r\   r<   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_callrV   re   rf   )r   r   r   r   r.   rM   r;   r\   r<   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 )NrG   ZYesrH   r?   TrI   ymaybeside_effectZNoZcancelnarV   ZAgree)Z	yes_labelrJ   )r   r   rC   r;   yesnoassertFalserK   r   r   r   
test_yesno   s@    
 
 
 
 
 
  z FileOutputDisplayTest.test_yesnoc                 C   s   |  | j| jjddd d S )Nr?   TrW   )rC   rM   r;   rp   r<   r   r   r   test_yesno_noninteractive   s
      z/FileOutputDisplayTest.test_yesno_noninteractiverG   c                 C   sB   d|_ | jjdtdd\}}| |t|ftjtddgf d S )Nz2 1rV   TrI   r   r   r!   r;   	checklistTAGSr"   setr:   rQ   r   
mock_inputr^   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+   rV   TrI   r   r   r   rt   rx   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   rV   TrI   r   )rm   r;   ru   rv   r"   r:   rQ   r   ry   rT   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|   r[   rV   TrI   )rm   r;   ru   rv   r"   r:   r]   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 )NrV   rW   )rv   rM   r;   ru   r"   r:   rQ   rc   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   )rw   	enumerater;   _scrub_checklist_inputrv   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 )NrV   /var/www/htmlre   T)r!   r;   directory_selectr"   r:   rQ   )r   ry   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   rV   rW   )rM   r;   r   r"   r:   rQ   rc   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,   )r;   rO   r   r   isattyr!   rq   ZwarningZcalledr"   Z
call_count)r   funcr   r   rO   Z
mock_stdinZmock_loggerrY   r   r   r   rM      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"   r;   r   rv   )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 )NrV   )r;   Z_print_menurS   rv   r<   r   r   r   test_print_menu  s    z%FileOutputDisplayTest.test_print_menuc                 C   s*   d d}t|}| |dd d S )NzThis 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   rV   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 )NrG   r   rH   r,   r	   r   )r   r   r"   r;   _get_valid_int_ansr:   rQ   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   r[   r   ZoneCrG   rl   r   )r   r   r"   r;   r   r:   r]   )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 )NrJ   )r   IDisplaynamesr   PY2inspect
getargspecgetfullargspecgetattrr;   rC   r   )r   namer   Zarg_specr   r   r   #test_methods_take_force_interactive8  s    z9FileOutputDisplayTest.test_methods_take_force_interactive)$r1   r2   r3   r4   r8   rF   rL   rN   rP   r   r   rU   rZ   r`   rb   rd   rh   ri   rr   rs   rz   r{   r~   r   r   r   r   r   rM   r   r   r   r   r   r   __classcell__r   r   r=   r   r6   6   sH   






r6   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)	r7   r   r8   r   r9   r)   r:   ZNoninteractiveDisplayr;   r<   r=   r   r   r8   F  s    
zNoninteractiveDisplayTest.setUpc                 C   s2   | j dd | jjjd d }| d|k d S )Nr?   
   r   r@   rD   r   r   r   rF   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 valuer?   rW   )r;   r\   r"   r:   rQ   r   r   MissingCommandlineFlagr   drT   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 )Nr?   r,   rW   )	r;   rR   rS   r"   r:   rQ   r   r   r   )r   rT   r   r   r   rU   W  s    z#NoninteractiveDisplayTest.test_menuc                 C   s8   d}| j jd|d}| || | tj| j jd d S )NFr?   rW   )r;   rp   r"   r   r   r   r   r   r   r   rr   \  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   r?   rW   )	r;   ru   rv   r"   r:   rQ   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   rV   )r:   rQ   r;   r   r"   r   r   r   )r   rX   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   r;   r   r   r   r   keywordsrq   r   Zvarkw)r   r   methodrY   r   r   r   test_methods_take_kwargsq  s    z2NoninteractiveDisplayTest.test_methods_take_kwargs)r1   r2   r3   r4   r8   rF   r&   rU   rr   r   r   r   r   r   r   r=   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 )Nro   br[   Ztest)r   r<   r   r   r   r8     s    zSeparateListInputTest.setUpc                 C   s   ddl m} ||S )Nr   )separate_list_input)r   r   )r   ra   r   r   r   r   r     s    zSeparateListInputTest._callc                 C   s   |  | d| j d S )Nz
a,b,c,testr"   r   r   r<   r   r   r   test_commas  s    z!SeparateListInputTest.test_commasc                 C   s   |  | d| j d S )Nz
a b c testr   r<   r   r   r   test_spaces  s    z!SeparateListInputTest.test_spacesc                 C   s   |  | d| j d S )Nza, b, c, testr   r<   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)r1   r2   r3   r4   r8   r5   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   Zlabelr   r   r   r   r     s    zPlaceParensTest._callc                 C   s   |  d| d d S )Nz(a)ro   r"   r   r<   r   r   r   test_single_letter  s    z"PlaceParensTest.test_single_letterc                 C   s(   |  d| d |  d| d d S )Nz(L)abelZLabelz(y)es pleasez
yes pleaser   r<   r   r   r   test_multiple  s    zPlaceParensTest.test_multipleN)r1   r2   r3   r5   r   r   r   r   r   r   r   r     s   
r   __main__)r4   r   r-   r   Zunittestr   r   Zcertbotr   r   Zcertbot.displayr   r:   rS   rv   ZTAGS_CHOICESZTestCaser   r6   r   r   r   r1   mainr   r   r   r   <module>   s*   

#  ?