U
    ÓZjÀ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   úZ/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-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>)ÚpreÚ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_valueÚ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   ÚassertFalser2   )r   Z	mock_progr   r   r   Ú
test_unset9   s    zValidateHookTest.test_unsetN)r    r!   r"   r#   r$   r   r/   r   r%   r3   r6   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 )z¤Calls 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©Ú r;   )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   r7   ?   s
   
r7   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+   r0   )Úsuperr>   ÚsetUpr   r?   r   ÚmakedirsZrenewal_pre_hooks_dirr   ÚpathÚjoinÚdir_hookÚcreate_hookÚ_reset_pre_hook_alreadyr.   ©Ú	__class__r   r   rB   ]   s    
zPreHookTest.setUpc                    s   |   ¡  tt| ƒ ¡  d S ©N)rH   rA   r>   ÚtearDownr.   rI   r   r   rL   h   s    zPreHookTest.tearDownc                 C   s   ddl m} | ¡  d S )Nr   )Úexecuted_pre_hooks)r	   rM   Úclear)r   rM   r   r   r   rH   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ÚrunrP   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   r<   r   r   r   rR   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?   rQ   r   ÚremoverF   r   r%   r=   r5   r2   Úinfo©r   Úmock_loggerr<   r   r   r   Útest_no_hooks~   s    zPreHookTest.test_no_hooksc                 C   s0   d| j _|  | j ¡}| d| j j¡ |  ¡  d S )NFrV   )r   Údirectory_hooksr=   rW   r?   rX   rY   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   rV   )r   rQ   r=   Úassert_any_callrF   Úassert_called_withr?   rX   rY   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 rb   )rF   r   r?   rQ   r=   rW   rX   rY   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 )NrZ   )r   r%   r=   r   r5   r2   r1   r\   r]   r   r   r   rX   œ   s    z&PreHookTest._test_no_executions_common)r    r!   r"   r#   r$   r   rB   rL   rH   rS   rU   rR   r_   ra   re   rf   rX   Ú__classcell__r   r   rI   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	   ri   )r
   r   r   ri   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 )Nr0   r+   )rA   rh   rB   r   ri   r   rC   Zrenewal_post_hooks_dirr   rD   rE   rF   rG   Ú_reset_post_hook_eventuallyr.   rI   r   r   rB   «   s    
zPostHookTest.setUpc                    s   |   ¡  tt| ƒ ¡  d S rK   )rj   rA   rh   rL   r.   rI   r   r   rL   ¶   s    zPostHookTest.tearDownc                 C   s   ddl m} |d d …= d S ©Nr   )Ú
post_hooks©r	   rl   ©r   rl   r   r   r   rj   »   s    z(PostHookTest._reset_post_hook_eventuallyc                 C   s@   dD ]6}|| j _|  | j ¡}| d| j j¡ |  |  ¡ ¡ qd S )N©rO   rT   ú	post-hook)r   rQ   r=   rW   ri   r5   Ú_get_eventually)r   rQ   r<   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 )Nro   )r   ri   rQ   r5   r=   r2   rq   )r   rQ   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_commonri   r.   r   r   r   ra   Í   s    z*PostHookTest.test_renew_disabled_dir_hooksc                 C   s   d | j _|  | jg¡ d S rK   )r   ri   rt   rF   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 rK   )r   r[   rF   rt   r   ri   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 rK   )r   ri   r   r[   rF   rt   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)rF   r   ri   rt   Úappend)r   Úexpectedr   r   r   re   Þ   s
    
z"PostHookTest.test_renew_no_overlapc                 C   s   | j | j_|  | j g¡ d S rK   )rF   r   ri   rt   r.   r   r   r   rf   æ   s    
z$PostHookTest.test_renew_with_overlapc                 C   s6   d| j _tdƒD ] }|  | j ¡ |  |  ¡ |¡ qd S )Nr   é   )r   rQ   Úranger   r   rq   )r   rz   Ú_r   r   r   rt   ê   s    zPostHookTest._test_renew_commonc                 C   s   ddl m} |S rk   rm   rn   r   r   r   rq   ñ   s    zPostHookTest._get_eventually)r    r!   r"   r#   r$   r   rB   rL   rj   rr   rs   ra   ru   rv   rw   re   rf   rt   rq   rg   r   r   rI   r   rh   £   s    
rh   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 )zõCall 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 rK   )rA   r~   rB   r�   r.   rI   r   r   rB   
  s    zRunSavedPostHooksTest.setUpc                 C   s   |   |  ¡ j¡ d S rK   )r5   r‚   r2   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+   r0   rx   Úquxr   r   )r�   r‚   r   Úzipr   )r   r<   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+   rp   r   )r�   r‚   rW   rY   r   r   r   Útest_single  s    z!RunSavedPostHooksTest.test_single)r    r!   r"   r#   r$   r   r‚   rB   rƒ   r†   r‡   rg   r   r   rI   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 )zúCalls 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   Úlineager{   c                     s.   ˆ  tjd d ˆ ¡¡ ˆ  tjd ˆ¡ dS )z¦Assert environment variables are properly set.

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

            ÚRENEWED_DOMAINSú ÚRENEWED_LINEAGEr:   )r   r   ÚenvironrE   )Zunused_argsZunused_kwargs©r‰   rŠ   r   r   r   Úexecute_side_effect/  s    zDRenewalHookTest._call_with_mock_execute.<locals>.execute_side_effectr9   )r   r%   Zside_effectr   )r   r   r   r�   r<   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 rK   )r   rŽ   )r   Úvarr   r   r   Ú	<genexpr>A  s   
ÿz(RenewalHookTest.setUp.<locals>.<genexpr>)r‹   r�   )rA   rˆ   rB   r   Úvars_to_clearr.   rI   r   r   rB   ?  s    ÿzRenewalHookTest.setUpc                    s,   | j D ]}tj |d ¡ qtt| ƒ ¡  d S rK   )r“   r   rŽ   ÚpoprA   rˆ   rL   )r   r‘   rI   r   r   rL   E  s    
zRenewalHookTest.tearDown)r    r!   r"   r#   r=   rB   rL   rg   r   r   rI   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._callrZ   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=   r5   r2   r1   Úwarningr]   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=   r5   r2   r\   r]   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=   rW   )r   r‰   rŠ   r<   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@   )rA   r    rB   r   r¡   r   rC   Zrenewal_deploy_hooks_dirr   rD   rE   rF   rG   r.   rI   r   r   rB   u  s    ÿzRenewHookTest.setUpc                 C   s.   d| j _|  | j dgd¡}| d| j j¡ d S )NFr—   r˜   rž   )r   r`   r=   rW   r¡   rY   r   r   r   Útest_disabled_dir_hooks~  s      ÿz%RenewHookTest.test_disabled_dir_hooksrZ   c                 C   s:   d| j _|  | j dgd¡}|  |j¡ |  |jjd¡ d S )NTr—   r˜   r{   )r   r™   r=   r5   r2   r   rš   Z
call_countr]   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 )NrZ   r—   r˜   )r   r¡   r   r[   rF   r   r%   r=   r5   r2   r\   r]   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ž   )rF   r   r¡   r=   rW   rY   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   rc   rF   rd   r¡   rY   r   r   r   Útest_no_overlapœ  s      ÿzRenewHookTest.test_no_overlap)r    r!   r"   r#   r$   r   rB   r¢   r   r%   r›   r_   r£   r¤   rg   r   r   rI   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   r{   )r;   zHello World!)r;   zGoodbye Cruel World!)r|   Ú_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.PopenrZ   r   r   r   zRunning %s command: %s)r   r%   r)   Úcommunicater¨   r   r   Z	call_argsÚgetr\   rc   ÚANYr1   Úerrorr2   )	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 rK   )r5   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 rK   )r   rD   rE   r±   )r   Úbasenamer.   r   r   r’   Ó  s   ÿz.ListHooksTest.test_multiple.<locals>.<genexpr>)r+   r0   rx   r„   )ÚsortedrG   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 r4   ©r   rD   rE   r±   rG   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)Ú	file_pathr   r   r   rG   é  s    rG   Ú__main__)r#   Zunittestr   Zacme.magic_typingr   Zcertbotr   r   Zcertbot.compatr   r   Zcertbot.testsZ	test_utilZTestCaser   ZTempDirTestCaser&   ZConfigTestCaser7   r>   rh   r~   rˆ   r•   r    r¥   r¯   rG   r    Úmainr   r   r   r   Ú<module>   s,   NS),"6$"	
