U
    ]T                     @   sn  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m	Z	 ddl
m  mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ ejed
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!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rje(  dS ) zTest certbot.display.ops.    N)messages)account)errors)os)
filesystem)ops)utilzrsa512_key.pemc                   @   s   e Zd ZdZedd Zeddd Zeddd Z	edd	d
 Z
eddd Zeddd Zeddd ZdS )GetEmailTestz(Tests for certbot.display.ops.get_email.c                 K   s   ddl m} |f |S )Nr   )	get_email)certbot.display.opsr
   )clskwargsr
    r   @/usr/lib/python3/dist-packages/certbot/tests/display/ops_test.py_call   s    zGetEmailTest._callcertbot.display.ops.z_utilc                 C   s<   | j }tjdf|_| tj| j | jtj| jdd d S )Nfoo@bar.bazFZoptional)inputdisplay_utilCANCELreturn_valueassertRaisesr   Errorr   )selfmock_get_utility
mock_inputr   r   r   test_cancel_none   s    zGetEmailTest.test_cancel_nonec              	   C   sF   | j }tjdf|_td}d|_| |  dk W 5 Q R X d S )Nr   #certbot.display.ops.util.safe_emailT)r   r   OKr   mockpatch
assertTruer   r   r   r   mock_safe_emailr   r   r   test_ok_safe&   s
    zGetEmailTest.test_ok_safec              	   C   sJ   | j }tjdf|_td"}ddg|_| |  dk W 5 Q R X d S )Nr   r   FT)	r   r   r   r   r    r!   side_effectr"   r   r#   r   r   r   test_ok_not_safe.   s
    
zGetEmailTest.test_ok_not_safec              	   C   s|   d}| j }tjdf|_tdP}d|_|   | ||jd d k | jdd | ||jd d k W 5 Q R X d S )NThere seem to be problemsr   r   Tr   Zinvalid)	r   r   r   r   r    r!   r   r"   	call_argsr   r   Zinvalid_txtr   r$   r   r   r   test_invalid_flag6   s    zGetEmailTest.test_invalid_flagc              	   C   sf   | j }tjdf|_td>}ddg|_| jdd |jD ]}| 	d|d d k q<W 5 Q R X d S )Nr   r   FTr   z!--register-unsafely-without-emailr   )
r   r   r   r   r    r!   r&   r   call_args_listr"   )r   r   r   r$   callr   r   r   test_optional_flagB   s    

zGetEmailTest.test_optional_flagc              	   C   s`   d}| j }tjdf|_td4}ddg|_| jdd | ||j	d d k W 5 Q R X d S )Nr(   r   r   FTr)   r   )
r   r   r   r   r    r!   r&   r   r"   r*   r+   r   r   r   test_optional_invalid_unsafeM   s    
z)GetEmailTest.test_optional_invalid_unsafeN)__name__
__module____qualname____doc__classmethodr   	test_utilpatch_get_utilityr   r%   r'   r,   r/   r0   r   r   r   r   r	      s   






r	   c                       sb   e Zd ZdZ fddZedd Zeddd Z	edd	d
 Z
eddd Z  ZS )ChooseAccountTestz-Tests for certbot.display.ops.choose_account.c                    s   t t|   tjttj	d t
j| jd| _t| jd tj| j| jdd| _t| _ttjd tjjddd| j| _ttjd tjjd	d
dd| j| _d S )NFkeysi  zcertbot-demo.org)Zaccounts_diraccount_keys_dirZserverzemail1@g.com)email)ZuriZbodyzemail2@g.comphone)r;   r<   )superr8   setUpzope	componentprovideUtilityr   FileDisplaysysstdoutr   pathjoinZtempdirr:   r   makedirsr    	MagicMockZconfigKEYkeyr   ZAccountr   ZRegistrationResourceZRegistrationZ	from_dataacc1acc2r   	__class__r   r   r>   Z   s:       zChooseAccountTest.setUpc                 C   s
   t |S N)r   Zchoose_account)r   Zaccountsr   r   r   r   p   s    zChooseAccountTest._callr   c                 C   s,   t jdf| j_| | | jg| j d S )Nr   )r   r   menur   assertEqualr   rK   r   	mock_utilr   r   r   test_onet   s    zChooseAccountTest.test_onec                 C   s0   t jdf| j_| | | j| jg| j d S N   )r   r   rQ   r   rR   r   rK   rL   rS   r   r   r   test_twoy   s    zChooseAccountTest.test_twoc                 C   s0   t jdf| j_| | | j| jgd k d S rV   )r   r   rQ   r   r"   r   rK   rL   rS   r   r   r   test_cancel~   s    zChooseAccountTest.test_cancel)r1   r2   r3   r4   r>   r5   r   r6   r7   rU   rX   rY   __classcell__r   r   rN   r   r8   X   s   


r8   c                   @   s4   e Zd ZdZdd Zedd Zdd Zdd	 Zd
S )GenSSLLabURLszELoose test of _gen_ssl_lab_urls. URL can change easily in the future.c                 C   s   t jttjd d S NFr?   r@   rA   r   rB   rC   rD   rM   r   r   r   r>      s    zGenSSLLabURLs.setUpc                 C   s   ddl m} ||S )Nr   )_gen_ssl_lab_urls)r   r^   )r   domainsr^   r   r   r   r      s    zGenSSLLabURLs._callc                 C   s   |  | g g  d S rP   rR   r   rM   r   r   r   	test_zero   s    zGenSSLLabURLs.test_zeroc                 C   s6   |  ddg}| d|d k | d|d k d S )Nzeff.orgz	umich.edur   rW   )r   r"   )r   Zurlsr   r   r   rX      s    zGenSSLLabURLs.test_twoN)	r1   r2   r3   r4   r>   r5   r   ra   rX   r   r   r   r   r[      s   
r[   c                   @   sL   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d ZdS )GenHttpsNamesTestzTest _gen_https_names.c                 C   s   t jttjd d S r\   r]   rM   r   r   r   r>      s    zGenHttpsNamesTest.setUpc                 C   s   ddl m} ||S )Nr   )_gen_https_names)r   rc   )r   r_   rc   r   r   r   r      s    zGenHttpsNamesTest._callc                 C   s   |  | g d d S N r`   rM   r   r   r   ra      s    zGenHttpsNamesTest.test_zeroc                 C   s.   ddg}|D ]}|  | |gd|  qd S )Nexample.comzasllkjsadfljasdf.cz
https://%sr`   )r   domsdomr   r   r   rU      s
    zGenHttpsNamesTest.test_onec                 C   s8   ddgddgg}|D ]}|  | |dj|d qd S )Nzfoo.bar.orgzbar.orgzpaypal.google.facebook.live.comz*.zombo.example.comz%https://{dom[0]} and https://{dom[1]}rh   rR   r   format)r   Zdomains_listrg   r   r   r   rX      s    
zGenHttpsNamesTest.test_twoc                 C   s(   dddg}|  | |dj|d d S )Na.orgb.orgc.orgz8https://{dom[0]}, https://{dom[1]}, and https://{dom[2]}ri   rj   )r   rg   r   r   r   
test_three   s    
zGenHttpsNamesTest.test_threec                 C   s.   ddddg}dj |d}| | || d S )Nrl   rm   rn   zd.orgzJhttps://{dom[0]}, https://{dom[1]}, https://{dom[2]}, and https://{dom[3]}ri   )rk   rR   r   )r   rg   Zexpr   r   r   	test_four   s
    zGenHttpsNamesTest.test_fourN)r1   r2   r3   r4   r>   r5   r   ra   rU   rX   ro   rp   r   r   r   r   rb      s   

rb   c                   @   s   e Zd Z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e
d
dd Ze
d
dd Ze
d
dd Zdd Ze
d
dd Ze
d
dd  ZdS )"ChooseNamesTestzTest choose names.c                 C   s$   t jttjd t | _	d S r\   )
r?   r@   rA   r   rB   rC   rD   r    rH   mock_installrM   r   r   r   r>      s    zChooseNamesTest.setUpNc                 C   s   ddl m} |||S )Nr   )choose_names)r   rs   )r   Z	installerquestionrs   r   r   r   r      s    zChooseNamesTest._callz*certbot.display.ops._choose_names_manuallyc                 C   s   |  d  | |jd d S rV   )r   rR   
call_count)r   Zmock_manualr   r   r   test_no_installer   s    
z!ChooseNamesTest.test_no_installerr   c                 C   s&   t jg f| j_| | d g  d S rP   )r   r   r   r   rR   r   rS   r   r   r   test_no_installer_cancel   s    z(ChooseNamesTest.test_no_installer_cancelc                 C   sn   t  |  j_d}tj|f| j_| | j}| | jj	d | ||g | 
d| jjd d k d S )Nrf   rW   zconfiguration filesr   )setrr   get_all_namesr   r   r   r   r   rR   ru   r"   r*   )r   rT   domainZactual_domsr   r   r   test_no_names_choose   s    z$ChooseNamesTest.test_no_names_choosec                    s   ddl m} | |g g  dddg}| ||dddg d ddd	d
g} fdd|D }t|} fdd|D }| ||| d S )Nr   _sort_nameszex.comzzx.comzax.comz.ex.comeazyc                    s   g | ]}|  qS r   r   .0usrz   r   r   
<listcomp>   s     z;ChooseNamesTest.test_sort_names_trivial.<locals>.<listcomp>c                    s   g | ]}|  qS r   r   r   r   r   r   r      s     )r   r}   rR   sorted)r   r}   Zsome_domainsunsorted_shortZunsorted_longsorted_shortZsorted_longr   r   r   test_sort_names_trivial   s    
z'ChooseNamesTest.test_sort_names_trivialc           	      C   s   ddl m} ddddg}ddd	d
ddg}d	dd
dddg}g }|D ]}|D ]}|||  qHq@g }t|D ]}|D ]}|||  qrqj| ||| d S )Nr   r|   z.cx.comz.bx.comz.ax.comz.dx.comZwwwzbnother.long.subdomainr   za.long.subdomainr   b)r   r}   appendr   rR   )	r   r}   Zunsorted_domainsr   r   Zto_sortZshortrz   Zsortdr   r   r   test_sort_names_many   s    z$ChooseNamesTest.test_sort_names_manyc                 C   sR   t dg| jj_tjdgf| j_| | j}| |dg | | jj	d d S )Nrf   rW   )
rx   rr   ry   r   r   r   	checklistr   rR   ru   r   rT   namesr   r   r   test_filter_names_valid_return  s
    z.ChooseNamesTest.test_filter_names_valid_returnc                 C   sn   t dg| jj_tjdgf| j_| | jd}| |dg | | jj	d | | jj
d d d d S )Nrf   ZCustomrW   r   )rx   rr   ry   r   r   r   r   r   rR   ru   r*   r   r   r   r   $test_filter_namees_override_question  s    z4ChooseNamesTest.test_filter_namees_override_questionc                 C   s8   t dg| jj_tjg f| j_| | | jg  d S Nrf   )	rx   rr   ry   r   r   r   r   rR   r   rS   r   r   r   "test_filter_names_nothing_selected#  s    z2ChooseNamesTest.test_filter_names_nothing_selectedc                 C   s:   t dg| jj_tjdgf| j_| | | jg  d S r   )	rx   rr   ry   r   r   r   r   rR   r   rS   r   r   r   test_filter_names_cancel*  s
     
z(ChooseNamesTest.test_filter_names_cancelc                 C   sf   ddl m} ddddddg}d	d
g}dddg}| ||| | ||g  | t||d d S )Nr   )get_valid_domainsrf   zsecond.example.comzalso.example.comunder_score.example.comjusttldz*.wildcard.comu   öóòps.net   uniçodé.comu   úniçøde.com   )r   r   rR   len)r   r   Z	all_validZall_invalidZ	two_validr   r   r   test_get_valid_domains2  s      
z&ChooseNamesTest.test_get_valid_domainsc              	   C   s   ddl m} | }d|j_tjdf|j_| | g  t	d*}t
ddddd}||_| | g  W 5 Q R X tjd	f|j_| | d
dddg d S )Nr   _choose_names_manuallyFr   z4certbot.display.ops.display_util.separate_list_inputr    re   rW   z=example.com,under_score.example.com,justtld,valid.example.comrf   r   r   zvalid.example.com)r   r   yesnor   r   r   r   rR   r    r!   UnicodeEncodeErrorr&   )r   rT   r   utility_mockZmock_sliZunicode_errorr   r   r   test_choose_manually=  s.     z$ChooseNamesTest.test_choose_manuallyc                 C   sH   ddl m} | }tjdf|j_dddg|j_|  | |jj	d d S )Nr   r   r   TF   )
r   r   r   r   r   r   r   r&   rR   ru   )r   rT   r   r   r   r   r   test_choose_manually_retryX  s    z*ChooseNamesTest.test_choose_manually_retry)N)r1   r2   r3   r4   r>   r5   r   r    r!   rv   r6   r7   rw   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   rq      s2   







rq   c                   @   s.   e Zd ZdZedd Zeddd ZdS )SuccessInstallationTestz&Test the success installation message.c                 C   s   ddl m} || d S )Nr   )success_installation)r   r   )r   r   r   r   r   r   r   g  s    zSuccessInstallationTest._callr   c                 C   s`   d | j _ddg}| | | | j jd | j jd d d }|D ]}| ||k qHd S Nrf   zabc.comrW   r   notificationr   r   rR   ru   r-   r"   r   rT   r   argnamer   r   r   test_success_installationl  s    

z1SuccessInstallationTest.test_success_installationN)	r1   r2   r3   r4   r5   r   r6   r7   r   r   r   r   r   r   d  s
   
r   c                   @   s.   e Zd ZdZedd Zeddd ZdS )SuccessRenewalTestz!Test the success renewal message.c                 C   s   ddl m} || d S )Nr   )success_renewal)r   r   )r   r   r   r   r   r   r   }  s    zSuccessRenewalTest._callr   c                 C   s`   d | j _ddg}| | | | j jd | j jd d d }|D ]}| ||k qHd S r   r   r   r   r   r   test_success_renewal  s    

z'SuccessRenewalTest.test_success_renewalN)	r1   r2   r3   r4   r5   r   r6   r7   r   r   r   r   r   r   z  s
   
r   c                   @   s.   e Zd ZdZedd Zeddd ZdS )SuccessRevocationTestz$Test the success revocation message.c                 C   s   ddl m} || d S )Nr   )success_revocation)r   r   )r   rE   r   r   r   r   r     s    zSuccessRevocationTest._callr   c                 C   sT   d | j _d}| | | j jd|tjdd | || j jd d k d S )Nz/path/to/cert.pemz\Congratulations! You have successfully revoked the certificate that was located at {0}{1}{1}F)pauser   )	r   r   r   Zassert_called_once_withrk   r   linesepr"   r*   )r   rT   rE   r   r   r   test_success_revocation  s    

z-SuccessRevocationTest.test_success_revocationN)	r1   r2   r3   r4   r5   r   r6   r7   r   r   r   r   r   r     s
   
r   c                   @   s   e Zd ZdZdZdZdZedd Ze	
 dd Ze	
 d	d
 Ze	
 dd Ze	
 dd Ze	
 dd Ze	
 dd Ze	
 dd ZdS )ValidatorTestsz6Tests for `validated_input` and `validated_directory`.zMust be non-emptyZasdfz/var/www/htmlc                 C   s   | dkrt tjd S rd   )r   ZPluginErrorr   _ValidatorTests__ERROR)mr   r   r   Z__validator  s    zValidatorTests.__validatorc                 C   sr   t jdft jdft jdft j| jfg| j_tj| jddd}| t	j
| jjd d  | |t j| jf d S )Nre   messageTZforce_interactiver   )r   r   valid_inputr   r&   r   validated_input_ValidatorTests__validatorrR   r   r   r   r*   r   rT   Zreturnedr   r   r   test_input_blank_with_validator  s    

z.ValidatorTests.test_input_blank_with_validatorc                 C   s>   t j| jfg| j_tj| jddd}| |t j| jf d S Nmsgotherdefault)	r   r   r   r   r&   r   r   r   rR   r   r   r   r   "test_input_validation_with_default  s    z1ValidatorTests.test_input_validation_with_defaultc                 C   s0   t j| jfg| j_| jttj| j	ddd d S Nr   re   r   )
r   r   r   r   r&   r   AssertionErrorr   r   r   rS   r   r   r   &test_input_validation_with_bad_default  s      z5ValidatorTests.test_input_validation_with_bad_defaultc                 C   s:   t jdfg| j_tj| jddd\}}| |t j d S )Nre   r   Tr   )r   r   r   r&   r   r   r   rR   )r   rT   codeZ
unused_rawr   r   r    test_input_cancel_with_validator  s    z/ValidatorTests.test_input_cancel_with_validatorc                 C   sb   t jdft j| jfg| j_tj| jddd}| t	j
| jjd d  | |t j| jf d S )Nre   r   Tr   r   )r   r   valid_directorydirectory_selectr&   r   validated_directoryr   rR   r   r   r   r*   r   r   r   r    test_directory_select_validation  s    

z/ValidatorTests.test_directory_select_validationc                 C   s>   t j| jfg| j_tj| jddd}| |t j| jf d S r   )	r   r   r   r   r&   r   r   r   rR   r   r   r   r   -test_directory_select_validation_with_default  s    z<ValidatorTests.test_directory_select_validation_with_defaultc                 C   s0   t j| jfg| j_| jttj| j	ddd d S r   )
r   r   r   r   r&   r   r   r   r   r   rS   r   r   r   1test_directory_select_validation_with_bad_default  s      z@ValidatorTests.test_directory_select_validation_with_bad_defaultN)r1   r2   r3   r4   r   r   r   staticmethodr   r6   r7   r   r   r   r   r   r   r   r   r   r   r   r     s(   







r   c                   @   sR   e Zd ZdZedd Zeddd Zeddd Z	edd	d
 Z
dS )ChooseValuesTestzTest choose_values.c                 C   s   ddl m} |||S )Nr   )choose_values)r   r   )r   valuesrt   r   r   r   r   r     s    zChooseValuesTest._callr   c                 C   sl   dddg}t j|d gf| j_| |d }| ||d g | | jj | | jjd d d  d S )Nfirstsecondthirdr   r   	r   r   r   r   r   rR   r"   calledr*   )r   rT   itemsresultr   r   r   test_choose_names_success  s    
z*ChooseValuesTest.test_choose_names_successc                 C   sp   dddg}d}t j|d gf| j_| ||}| ||d g | | jj | | jjd d | d S )Nr   r   r   z
Which one?rW   r   r   r   rT   r   rt   r   r   r   r   "test_choose_names_success_question  s    
z3ChooseValuesTest.test_choose_names_success_questionc                 C   sd   dddg}d}t jg f| j_| ||}| |g  | | jj | | jjd d | d S )Nr   r   r   zWant to cancel?r   )	r   r   r   r   r   rR   r"   r   r*   r   r   r   r   test_choose_names_user_cancel  s    
z.ChooseValuesTest.test_choose_names_user_cancelN)r1   r2   r3   r4   r5   r   r6   r7   r   r   r   r   r   r   r   r     s   


	r   __main__))r4   rC   ZunittestZjosepyZjoser    Zzope.componentr?   Zacmer   Zcertbot.tests.utilZtestsr   r6   Zcertbotr   r   Zcertbot.compatr   r   Zcertbot.displayr   r   ZJWKRSAloadZload_vectorrI   ZTestCaser	   ZTempDirTestCaser8   r[   rb   rq   r   r   r   r   r   r1   mainr   r   r   r   <module>   s6   A,0 G%
