U
    ]#                     @   s   d 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lmZ ddlmZ ddlmZ ddlm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'Tests for letsencrypt.plugins.selection    N)List)errors)
interfaces)os)util)PluginsRegistryc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )ConveniencePickPluginTestz+Tests for certbot.plugins.selection.pick_*.c              	   C   s`   t  }t  }t  }t d"}d|_| d||||d W 5 Q R X ||||d| d S )Nz%certbot.plugins.selection.pick_pluginfoo	Question?)mockMockpatchreturn_valueassertEqualassert_called_once_with)selfZfunifacesconfigdefaultpluginsZmock_p r   @/usr/lib/python3/dist-packages/certbot/plugins/selection_test.py_test   s         zConveniencePickPluginTest._testc                 C   s    ddl m} | |tjf d S )Nr   )pick_authenticator)certbot.plugins.selectionr   r   r   IAuthenticator)r   r   r   r   r   test_authenticator    s    z,ConveniencePickPluginTest.test_authenticatorc                 C   s    ddl m} | |tjf d S )Nr   )pick_installer)r   r   r   r   
IInstaller)r   r   r   r   r   test_installer$   s    z(ConveniencePickPluginTest.test_installerc                 C   s$   ddl m} | |tjtjf d S )Nr   )pick_configurator)r   r    r   r   r   r   )r   r    r   r   r   test_configurator(   s    
z+ConveniencePickPluginTest.test_configuratorN)__name__
__module____qualname____doc__r   r   r   r!   r   r   r   r   r      s
   r   c                   @   sX   e Zd Z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 )PickPluginTestz0Tests for certbot.plugins.selection.pick_plugin.c                 C   s.   t jdd| _d | _t  | _d| _g | _d S )NF)Znoninteractive_moder
   )r   r   r   r   	MagicMockregquestionr   r   r   r   r   setUp1   s
    
zPickPluginTest.setUpc                 C   s&   ddl m} || j| j| j| j| jS )Nr   )pick_plugin)r   r,   r   r   r(   r)   r   )r   r,   r   r   r   _call8   s
     zPickPluginTest._callc                 C   s$   d| _ |   | d| jjj d S )Nr	      )r   r-   r   r(   filter
call_countr*   r   r   r   test_default_provided=   s    z$PickPluginTest.test_default_providedc                 C   s"   |    | d| j jj d S Nr.   )r-   r   r(   visibler   r0   r*   r   r   r   test_no_defaultB   s    zPickPluginTest.test_no_defaultc                 C   s   |  |  d k d S N)
assertTruer-   r*   r   r   r   test_no_candidateF   s    z PickPluginTest.test_no_candidatec                 C   sD   t  }d|j_d|_d|i| j   j	_| 
d|   d S )Nr	   Fbar)r   r'   initr   misconfiguredr(   r3   r   verify	availabler   r-   r   	plugin_epr   r   r   test_singleI   s     zPickPluginTest.test_singlec                 C   sF   t  }d|j_d|_d|i| j   j	_| 
|  d k d S )Nr	   Tr8   )r   r'   r9   r   r:   r(   r3   r   r;   r<   r6   r-   r=   r   r   r   test_single_misconfiguredR   s     z(PickPluginTest.test_single_misconfiguredc              	   C   sn   t  }d|j_||d| j   j_t 	d}||_| 
d|   W 5 Q R X |||g| j d S )Nr	   r8   Zbaz'certbot.plugins.selection.choose_plugin)r   r'   r9   r   r(   r3   r   r;   r<   r   r   r-   r   r)   )r   r>   mock_chooser   r   r   test_multiple[   s     zPickPluginTest.test_multiplec              	   C   sN   d d d| j    j_td}d |_| | 	 d k W 5 Q R X d S )NrA   rB   )
r(   r3   r   r;   r<   r   r   r   r6   r-   )r   rC   r   r   r   test_choose_plugin_noneh   s    z&PickPluginTest.test_choose_plugin_noneN)r"   r#   r$   r%   r+   r-   r1   r4   r7   r?   r@   rD   rE   r   r   r   r   r&   .   s   		r&   c                   @   sh   e Zd Z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S )ChoosePluginTestz2Tests for certbot.plugins.selection.choose_plugin.c                 C   s^   t jttjd tjddd| _	d| j	_
tjddd| _d| j j_| j	| jg| _d S )NFaTdescription_with_namer:   apachesZ
standalone)zopeZ	componentZprovideUtilitydisplay_utilZFileDisplaysysstdoutr   r   Zmock_apachename
mock_standr9   	more_infor   r   r*   r   r   r   r+   v   s       zChoosePluginTest.setUpc                 C   s   ddl m} || jdS )Nr   )choose_pluginr
   )r   rS   r   )r   rS   r   r   r   r-      s    zChoosePluginTest._callz certbot.plugins.selection.z_utilc                 C   sB   t jdft jdfg| j_| | j|   | | jjd d S )Nr   r.   )	rM   OKmenuside_effectr   rQ   r-   Znotificationr0   r   	mock_utilr   r   r   test_selection   s
    
zChoosePluginTest.test_selectionc                 C   s(   t jdfg| j_| | j|   d S r2   )rM   rT   rU   rV   r   rQ   r-   rW   r   r   r   test_more_info   s    
zChoosePluginTest.test_more_infoc                 C   s&   t jdf| j_| |  d k d S )Nr   )rM   CANCELrU   r   r6   r-   rW   r   r   r   test_no_choice   s    zChoosePluginTest.test_no_choicec                 C   s   t jddd}d| j_d|_|| jd< tjdf| j	_t
jdd k}|rXd	t
jd< z|   W 5 |rrt
jd= X | d
| j	jd k d S )NnFrH   znginx pluginZnginxr.   r   ZCERTBOT_AUTOr	   r   )r   r   r9   rR   r   rP   r   rM   r[   rU   r   environgetr-   r6   Z	call_args)r   rX   Z
mock_nginxZunset_cb_autor   r   r   test_new_interaction_avoidance   s      


z/ChoosePluginTest.test_new_interaction_avoidanceN)r"   r#   r$   r%   r+   r-   	test_utilZpatch_get_utilityrY   rZ   r\   r`   r   r   r   r   rF   s   s   


rF   c                       sH   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	  Z
S )GetUnpreparedInstallerTestz=Tests for certbot.plugins.selection.get_unprepared_installer.c                    sh   t t|   tjdd| _d| j_tjdd| _d| j_t | _	| j	| jj
_t| j| jd| _d S )Nafail)rI   rJ   )rc   rJ   )superrb   r+   r   r   mock_apache_fail_eprP   Zmock_apache_epr'   mock_apache_pluginr9   r   r   r   r*   	__class__r   r   r+      s    
z GetUnpreparedInstallerTest.setUpc                 C   s   ddl m} || j| jS )Nr   )get_unprepared_installer)r   ri   r   r   )r   ri   r   r   r   r-      s    z GetUnpreparedInstallerTest._callc                 C   s   d | j _| |  d  d S r5   )r   configuratorr   r-   r*   r   r   r   test_no_installer_defined   s    z4GetUnpreparedInstallerTest.test_no_installer_definedc                 C   s&   d| j _ti | _| tj| j d S NrJ   )r   rj   r   r   assertRaisesr   PluginSelectionErrorr-   r*   r   r   r   test_no_available_installers   s    
z7GetUnpreparedInstallerTest.test_no_available_installersc                 C   s$   d| j _|  }| || jk d S rl   )r   rj   r-   r6   rf   )r   Z	installerr   r   r   test_get_plugin   s    z*GetUnpreparedInstallerTest.test_get_pluginc                 C   s$   d| j _d| j_| tj| j d S rl   )r   rj   re   rP   rm   r   rn   r-   r*   r   r   r   !test_multiple_installers_returned   s    z<GetUnpreparedInstallerTest.test_multiple_installers_returned)r"   r#   r$   r%   r+   r-   rk   ro   rp   rq   __classcell__r   r   rg   r   rb      s   rb   __main__)r%   rN   Zunittestr   Zzope.componentrL   Zacme.magic_typingr   Zcertbotr   r   Zcertbot.compatr   Zcertbot.displayr   rM   Zcertbot.plugins.discor   Zcertbot.testsra   ZTestCaser   r&   rF   ZConfigTestCaserb   r"   mainr   r   r   r   <module>   s"   E=+