U
    ]E                     @   s4  d 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 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ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd dejZG dd dejZdd Zed kr0e  dS )!zTests for certbot.hooks.    N)List)errors)util)os)
filesystemc                   @   s.   e Zd ZdZedd Zeddd ZdS )ValidateHooksTestz'Tests for certbot.hooks.validate_hooks.c                 O   s   ddl m} |||S )Nr   )validate_hooks)certbot.hooksr   )clsargskwargsr    r   9/usr/lib/python3/dist-packages/certbot/tests/hook_test.py_call   s    zValidateHooksTest._callzcertbot.hooks.validate_hookc                 C   sR   t  }| | dd |jD }| tdt|d d  | d|d  d S )Nc                 S   s   g | ]}|d  d qS )r      r   ).0callr   r   r   
<listcomp>   s     z-ValidateHooksTest.test_it.<locals>.<listcomp>)ZpreZpostZdeployrenew)mockZ	MagicMockr   call_args_listassertEqualset)selfZmock_validate_hookconfigtypesr   r   r   test_it   s
    
zValidateHooksTest.test_itN)	__name__
__module____qualname____doc__classmethodr   r   patchr   r   r   r   r   r      s
   
r   c                   @   sH   e Zd ZdZedd Zdd Zeddd Z	ed	d
d Z
dS )ValidateHookTestz&Tests for certbot.hooks.validate_hook.c                 O   s   ddl m} |||S )Nr   )validate_hook)r	   r%   )r
   r   r   r%   r   r   r   r   $   s    zValidateHookTest._callc              
   C   sH   t d4 t jddd | tj| jdd W 5 Q R X W 5 Q R X d S )N$certbot.hooks.plug_util.path_surgeryz&certbot.hooks.filesystem.is_executableF)return_valueZdummyfoo)r   r#   assertRaisesr   HookCommandNotFoundr   r   r   r   r   test_hook_not_executable)   s    z)ValidateHookTest.test_hook_not_executablezcertbot.hooks.util.exe_existsc              	   C   s@   d|_ td}| tj| jdd W 5 Q R X | |j d S )NFr&   r(   bar)	r'   r   r#   r)   r   r*   r   
assertTruecalled)r   Zmock_exe_existsZmock_psr   r   r   test_not_found2   s    zValidateHookTest.test_not_foundzcertbot.hooks._progc                 C   s   |  d d | |j d S Nr(   )r   assertFalser/   )r   Z	mock_progr   r   r   
test_unset9   s    zValidateHookTest.test_unsetN)r   r   r    r!   r"   r   r,   r   r#   r0   r3   r   r   r   r   r$   !   s   
	
r$   c                   @   s(   e Zd ZdZedd Zedd ZdS )HookTestz!Common base class for hook tests.c                 O   s   t dS )z7Calls the method being tested with the given arguments.N)NotImplementedError)r
   r   r   r   r   r   r   B   s    zHookTest._callc              	   O   s,   t d}d|_| j|| W 5 Q R X |S )zCalls self._call after mocking out certbot.hooks.execute.

        The mock execute object is returned rather than the return value
        of self._call.

        certbot.hooks.execute r8   )r   r#   r'   r   )r
   r   r   mock_executer   r   r   _call_with_mock_executeG   s    z HookTest._call_with_mock_executeN)r   r   r    r!   r"   r   r:   r   r   r   r   r4   ?   s
   
r4   c                       s   e Zd ZdZedd Z fd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dd Zdd Z  ZS )PreHookTestz!Tests for certbot.hooks.pre_hook.c                 O   s   ddl m} |||S )Nr   )pre_hook)r	   r<   )r
   r   r   r<   r   r   r   r   X   s    zPreHookTest._callc                    sN   t t|   d| j_t| jj tj	
| jjd| _t| j |   d S Nr(   r-   )superr;   setUpr   r<   r   makedirsZrenewal_pre_hooks_dirr   pathjoindir_hookcreate_hook_reset_pre_hook_alreadyr+   	__class__r   r   r?   ]   s    
zPreHookTest.setUpc                    s   |    tt|   d S N)rE   r>   r;   tearDownr+   rF   r   r   rI   h   s    zPreHookTest.tearDownc                 C   s   ddl m} |  d S )Nr   )executed_pre_hooks)r	   rJ   clear)r   rJ   r   r   r   rE   m   s    z#PreHookTest._reset_pre_hook_alreadyc                 C   s   d| j _|   d S )Ncertonlyr   verb_test_nonrenew_commonr+   r   r   r   test_certonlyq   s    zPreHookTest.test_certonlyc                 C   s   d| j _|   d S )NrunrM   r+   r   r   r   test_runu   s    zPreHookTest.test_runc                 C   s(   |  | j}|d| jj |   d S )Npre-hook)r:   r   assert_called_once_withr<   _test_no_executions_commonr   r9   r   r   r   rO   y   s    z!PreHookTest._test_nonrenew_commonc              	   C   s\   d | j _d| j _t| j td}| | j }W 5 Q R X | 	|j
 | 	|jj
 d S )Nr   certbot.hooks.logger)r   r<   rN   r   removerC   r   r#   r:   r2   r/   infor   mock_loggerr9   r   r   r   test_no_hooks~   s    zPreHookTest.test_no_hooksc                 C   s0   d| j _| | j }|d| j j |   d S )NFrS   )r   directory_hooksr:   rT   r<   rU   rV   r   r   r   test_renew_disabled_dir_hooks   s    z)PreHookTest.test_renew_disabled_dir_hooksc                 C   s>   d| j _| | j }|d| j |d| j j |   d S Nr   rS   )r   rN   r:   assert_any_callrC   assert_called_withr<   rU   rV   r   r   r   test_renew_no_overlap   s
    z!PreHookTest.test_renew_no_overlapc                 C   s8   | j | j_d| j_| | j}|d| j  |   d S r_   )rC   r   r<   rN   r:   rT   rU   rV   r   r   r   test_renew_with_overlap   s
    
z#PreHookTest.test_renew_with_overlapc              	   C   s@   t d}| | j}W 5 Q R X | |j | |jj d S )NrW   )r   r#   r:   r   r2   r/   r.   rY   rZ   r   r   r   rU      s    z&PreHookTest._test_no_executions_common)r   r   r    r!   r"   r   r?   rI   rE   rP   rR   rO   r\   r^   rb   rc   rU   __classcell__r   r   rF   r   r;   U   s   

r;   c                       s   e Zd ZdZedd Z fd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dd Zdd Zdd Zdd Z  ZS )PostHookTestz"Tests for certbot.hooks.post_hook.c                 O   s   ddl m} |||S )Nr   )	post_hook)r	   rf   )r
   r   r   rf   r   r   r   r      s    zPostHookTest._callc                    sN   t t|   d| j_t| jj tj	
| jjd| _t| j |   d S )Nr-   r(   )r>   re   r?   r   rf   r   r@   Zrenewal_post_hooks_dirr   rA   rB   rC   rD   _reset_post_hook_eventuallyr+   rF   r   r   r?      s    
zPostHookTest.setUpc                    s   |    tt|   d S rH   )rg   r>   re   rI   r+   rF   r   r   rI      s    zPostHookTest.tearDownc                 C   s   ddl m} |d d = d S Nr   )
post_hooksr	   ri   r   ri   r   r   r   rg      s    z(PostHookTest._reset_post_hook_eventuallyc                 C   s@   dD ]6}|| j _| | j }|d| j j | |   qd S )NrL   rQ   	post-hook)r   rN   r:   rT   rf   r2   _get_eventually)r   rN   r9   r   r   r   test_certonly_and_run_with_hook   s
    z,PostHookTest.test_certonly_and_run_with_hookc                 C   s@   d | j _dD ].}|| j _| | | j j | |   qd S )Nrl   )r   rf   rN   r2   r:   r/   rn   )r   rN   r   r   r   #test_cert_only_and_run_without_hook   s
    z0PostHookTest.test_cert_only_and_run_without_hookc                 C   s   d| j _| | j jg d S )NF)r   r]   _test_renew_commonrf   r+   r   r   r   r^      s    z*PostHookTest.test_renew_disabled_dir_hooksc                 C   s   d | j _| | jg d S rH   )r   rf   rq   rC   r+   r   r   r   test_renew_no_config_hook   s    z&PostHookTest.test_renew_no_config_hookc                 C   s    t | j | | jjg d S rH   )r   rX   rC   rq   r   rf   r+   r   r   r   test_renew_no_dir_hook   s    z#PostHookTest.test_renew_no_dir_hookc                 C   s"   d | j _t| j | g  d S rH   )r   rf   r   rX   rC   rq   r+   r   r   r   test_renew_no_hooks   s    z PostHookTest.test_renew_no_hooksc                 C   s<   | j | jjg}| | d| j_|| jj | | d S )Nbaz)rC   r   rf   rq   append)r   expectedr   r   r   rb      s
    
z"PostHookTest.test_renew_no_overlapc                 C   s   | j | j_| | j g d S rH   )rC   r   rf   rq   r+   r   r   r   rc      s    
z$PostHookTest.test_renew_with_overlapc                 C   s6   d| j _tdD ] }| | j  | |  | qd S )Nr      )r   rN   ranger   r   rn   )r   rw   _r   r   r   rq      s    zPostHookTest._test_renew_commonc                 C   s   ddl m} |S rh   rj   rk   r   r   r   rn      s    zPostHookTest._get_eventually)r   r   r    r!   r"   r   r?   rI   rg   ro   rp   r^   rr   rs   rt   rb   rc   rq   rn   rd   r   r   rF   r   re      s    
re   c                       sL   e Zd ZdZedd Zdd Z fddZdd	 Zd
d Z	dd Z
  ZS )RunSavedPostHooksTestz-Tests for certbot.hooks.run_saved_post_hooks.c                 O   s   ddl m} | S )Nr   )run_saved_post_hooks)r	   r|   )r
   r   r   r|   r   r   r   r      s    zRunSavedPostHooksTest._callc              
   O   s8   d}t j|| jd | j||W  5 Q R  S Q R X dS )zCall run_saved_post_hooks but mock out execute and eventually

        certbot.hooks.post_hooks is replaced with
        self.eventually. The mock execute object is returned rather than
        the return value of run_saved_post_hooks.

        zcertbot.hooks.post_hooks)newN)r   r#   
eventuallyr:   )r   r   r   Zeventually_pathr   r   r   &_call_with_mock_execute_and_eventually   s    z<RunSavedPostHooksTest._call_with_mock_execute_and_eventuallyc                    s   t t|   g | _d S rH   )r>   r{   r?   r~   r+   rF   r   r   r?   
  s    zRunSavedPostHooksTest.setUpc                 C   s   |  |  j d S rH   )r2   r   r/   r+   r   r   r   
test_empty  s    z RunSavedPostHooksTest.test_emptyc                 C   sJ   ddddg| _ |  }|j}t|| j D ]\}}| |d d | q(d S )Nr(   r-   ru   quxr   r   )r~   r   r   zipr   )r   r9   ZcallsZactual_callZexpected_argr   r   r   test_multiple  s
    z#RunSavedPostHooksTest.test_multiplec                 C   s&   dg| _ |  }|d| j d  d S )Nr(   rm   r   )r~   r   rT   rV   r   r   r   test_single  s    z!RunSavedPostHooksTest.test_single)r   r   r    r!   r"   r   r   r?   r   r   r   rd   r   r   rF   r   r{      s   
r{   c                       s4   e Zd ZdZdd Z fddZ fddZ  ZS )RenewalHookTestz1Common base class for testing deploy/renew hooks.c              	      sl   d|kr|d n|d  d|kr(|d n|d  fdd}t d}||_j|| W 5 Q R X |S )zCalls self._call after mocking out certbot.hooks.execute.

        The mock execute object is returned rather than the return value
        of self._call. The mock execute object asserts that environment
        variables were properly set.

        domainsr   lineagerx   c                     s.    tjd d   tjd  dS )zAssert environment variables are properly set.

            :returns: two strings imitating no output from the hook
            :rtype: `tuple` of `str`

            RENEWED_DOMAINS RENEWED_LINEAGEr7   )r   r   environrB   )Zunused_argsZunused_kwargsr   r   r   r   r   execute_side_effect/  s    zDRenewalHookTest._call_with_mock_execute.<locals>.execute_side_effectr6   )r   r#   Zside_effectr   )r   r   r   r   r9   r   r   r   r:   $  s    z'RenewalHookTest._call_with_mock_executec                    s&   t t|   tdd dD | _d S )Nc                 s   s   | ]}|t jkr|V  qd S rH   )r   r   )r   varr   r   r   	<genexpr>A  s   
z(RenewalHookTest.setUp.<locals>.<genexpr>)r   r   )r>   r   r?   r   vars_to_clearr+   rF   r   r   r?   ?  s    zRenewalHookTest.setUpc                    s,   | j D ]}tj|d  qtt|   d S rH   )r   r   r   popr>   r   rI   )r   r   rF   r   r   rI   E  s    
zRenewalHookTest.tearDown)r   r   r    r!   r:   r?   rI   rd   r   r   rF   r   r     s   r   c                   @   sH   e Zd ZdZedd Zeddd Zeddd Z	d	d
 Z
dS )DeployHookTestz$Tests for certbot.hooks.deploy_hook.c                 O   s   ddl m} |||S )Nr   )deploy_hook)r	   r   )r
   r   r   r   r   r   r   r   N  s    zDeployHookTest._callrW   c                 C   s@   d| j _d| j _| | j dgd}| |j | |jj d S )Nr(   Texample.org/foo/bar)r   r   dry_runr:   r2   r/   r.   warningrZ   r   r   r   test_dry_runS  s      zDeployHookTest.test_dry_runc                 C   s8   d | j _| | j dgd}| |j | |jj d S )Nr   r   )r   r   r:   r2   r/   rY   rZ   r   r   r   test_no_hook\  s      zDeployHookTest.test_no_hookc                 C   s8   ddg}d}d| j _| | j ||}|d| j j d S )Nr   example.netr   r(   deploy-hook)r   r   r:   rT   )r   r   r   r9   r   r   r   test_successd  s      zDeployHookTest.test_successN)r   r   r    r!   r"   r   r   r#   r   r   r   r   r   r   r   r   K  s   


r   c                       s^   e Zd ZdZedd Z fddZdd Ze	dd	d
 Z
dd Zdd Zdd Z  ZS )RenewHookTestz"Tests for certbot.hooks.renew_hookc                 O   s   ddl m} |||S )Nr   )
renew_hook)r	   r   )r
   r   r   r   r   r   r   r   p  s    zRenewHookTest._callc                    sF   t t|   d| j_t| jj tj	
| jjd| _t| j d S r=   )r>   r   r?   r   r   r   r@   Zrenewal_deploy_hooks_dirr   rA   rB   rC   rD   r+   rF   r   r   r?   u  s    zRenewHookTest.setUpc                 C   s.   d| j _| | j dgd}|d| j j d S )NFr   r   r   )r   r]   r:   rT   r   rV   r   r   r   test_disabled_dir_hooks~  s      z%RenewHookTest.test_disabled_dir_hooksrW   c                 C   s:   d| j _| | j dgd}| |j | |jjd d S )NTr   r   rx   )r   r   r:   r2   r/   r   r   Z
call_countrZ   r   r   r   r     s      zRenewHookTest.test_dry_runc              	   C   sZ   d | j _t| j td}| | j dgd}W 5 Q R X | |j	 | |j
j	 d S )NrW   r   r   )r   r   r   rX   rC   r   r#   r:   r2   r/   rY   rZ   r   r   r   r\     s      zRenewHookTest.test_no_hooksc                 C   s0   | j | j_| | jddgd}|d| j  d S )Nr   r   r   r   )rC   r   r   r:   rT   rV   r   r   r   test_overlap  s    
  zRenewHookTest.test_overlapc                 C   s4   |  | jdgd}|d| j |d| jj d S )Nr   r   r   )r:   r   r`   rC   ra   r   rV   r   r   r   test_no_overlap  s      zRenewHookTest.test_no_overlap)r   r   r    r!   r"   r   r?   r   r   r#   r   r\   r   r   rd   r   r   rF   r   r   m  s   
	

r   c                   @   s,   e Zd ZdZedd Zdd Zdd ZdS )	ExecuteTestz Tests for certbot.hooks.execute.c                 O   s   ddl m} |||S )Nr   )execute)r	   r   )r
   r   r   r   r   r   r   r     s    zExecuteTest._callc                 C   s6   t ddD ]&}dD ]}dD ]}| ||| qqq
d S )Nr   rx   )r8   zHello World!)r8   zGoodbye Cruel World!)ry   _test_common)r   
returncodestdoutstderrr   r   r   r     s    zExecuteTest.test_itc           	   
   C   s   d}d}t dJ}||f|jj_||j_t d}| | ||||f W 5 Q R X W 5 Q R X |jd d|jd d }| || |j	
d|| |r|j	
t jt jt j| |s|r| |jj d S )	Nr(   zfoo-hookzcertbot.hooks.PopenrW   r   r   r   zRunning %s command: %s)r   r#   r'   Zcommunicater   r   r   Z	call_argsgetrY   r`   ANYr.   errorr/   )	r   r   r   r   Zgiven_commandZ
given_nameZ
mock_popenr[   Zexecuted_commandr   r   r   r     s.    ,
   zExecuteTest._test_commonN)r   r   r    r!   r"   r   r   r   r   r   r   r   r     s
   
r   c                   @   s<   e Zd ZdZedd Zdd Zdd Zdd	 Zd
d Z	dS )ListHooksTestz#Tests for certbot.hooks.list_hooks.c                 O   s   ddl m} |||S )Nr   )
list_hooks)r	   r   )r
   r   r   r   r   r   r   r     s    zListHooksTest._callc                 C   s   |  | | j d S rH   )r2   r   tempdirr+   r   r   r   r     s    zListHooksTest.test_emptyc                    s@   t  fdddD }|D ]}t| q   j| d S )Nc                 3   s   | ]}t j j|V  qd S rH   )r   rA   rB   r   )r   basenamer+   r   r   r     s   z.ListHooksTest.test_multiple.<locals>.<genexpr>)r(   r-   ru   r   )sortedrD   r   r   r   )r   namesnamer   r+   r   r     s    
zListHooksTest.test_multiplec                 C   s2   t j| jd}t| | | | j|g d S r1   r   rA   rB   r   rD   r   r   r   r   r   r   r   r     s    zListHooksTest.test_singlec                 C   s0   t j| jd}t| | | | jg  d S )Nzfoo~r   r   r   r   r   test_ignore_tilde  s    zListHooksTest.test_ignore_tildeN)
r   r   r    r!   r"   r   r   r   r   r   r   r   r   r   r     s   

r   c                 C   s   t j| ddd  dS )zmCreates an executable file at the specified path.

    :param str file_path: path to create the file at

    wi  )modechmodN)r   Z	safe_openclose)Z	file_pathr   r   r   rD     s    rD   __main__)r!   Zunittestr   Zacme.magic_typingr   Zcertbotr   r   Zcertbot.compatr   r   Zcertbot.testsZ	test_utilZTestCaser   ZTempDirTestCaser$   ZConfigTestCaser4   r;   re   r{   r   r   r   r   r   rD   r   mainr   r   r   r   <module>   s,   NS),"6$"	
