U
    ÝÁ]b  ã                   @   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 G d	d
„ d
ejƒZedkrŽe ¡  dS )z Tests for certbot.plugins.manualé    N)Ú
challenges)Úerrors)Úos)Ú
filesystem)Ú	acme_util)Úutilc                       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„ ƒZe	 
¡ dd„ ƒZdd„ Ze	 
¡ dd„ ƒZdd„ Z‡  ZS )ÚAuthenticatorTestz/Tests for certbot.plugins.manual.Authenticator.c                    sÖ   t t| ƒ ¡  tj| _tj| _tj| _	| j| j| j	g| _
dD ]}t tj | j|¡¡ q<tjdd d dddtj | jd¡tj | jd¡tj | jd¡tj | jd¡tj | jd¡d	| _dd
lm} || jdd| _d S )N)Ú
config_dirÚwork_dirZin_progressr   Fr	   r
   Ú
backup_dirÚtemp_checkpoint_dirZ
in_progess)Zhttp01_portÚmanual_auth_hookÚmanual_cleanup_hookÚmanual_public_ip_logging_okÚnoninteractive_modeÚvalidate_hooksr	   r
   r   r   Zin_progress_dir)ÚAuthenticatorZmanual)Úname)Úsuperr   ÚsetUpr   ZHTTP01_AÚhttp_achallZDNS01_AÚ
dns_achallZ	DNS01_A_2Zdns_achall_2Úachallsr   Úmkdirr   ÚpathÚjoinZtempdirÚmockZ	MagicMockÚconfigZcertbot.plugins.manualr   Úauth)ÚselfÚdr   ©Ú	__class__© ú=/usr/lib/python3/dist-packages/certbot/plugins/manual_test.pyr      s2        ÿ÷zAuthenticatorTest.setUpc                 C   s   d| j _|  tj| jj¡ d S ©NT)r   r   ÚassertRaisesr   ÚPluginErrorr   Úprepare©r   r#   r#   r$   Ú#test_prepare_no_hook_noninteractive-   s    z5AuthenticatorTest.test_prepare_no_hook_noninteractivec                 C   s0   t j t j¡| j_d| j_|  tj	| j
j¡ d S r%   )r   r   ÚabspathÚsepr   r   r   r&   r   ZHookCommandNotFoundr   r(   r)   r#   r#   r$   Útest_prepare_bad_hook1   s    z'AuthenticatorTest.test_prepare_bad_hookc                 C   s   |   t| j ¡ tjƒ¡ d S )N)Ú
assertTrueÚ
isinstancer   Z	more_infoÚsixZstring_typesr)   r#   r#   r$   Útest_more_info6   s    z AuthenticatorTest.test_more_infoc                 C   s    |   | j d¡tjtjg¡ d S )Nzexample.org)ÚassertEqualr   Zget_chall_prefr   ÚHTTP01ÚDNS01r)   r#   r#   r$   Útest_get_chall_pref9   s    
ÿz%AuthenticatorTest.test_get_chall_prefc                 C   s"   d|ƒ j _|  tj| jjg ¡ d S )NF)ÚyesnoÚreturn_valuer&   r   r'   r   Úperform©r   Úmock_get_utilityr#   r#   r$   Útest_ip_logging_not_ok=   s    
z(AuthenticatorTest.test_ip_logging_not_okc                 C   s(   d|ƒ j _| j g ¡ |  | jj¡ d S r%   )r6   r7   r   r8   r.   r   r   r9   r#   r#   r$   Útest_ip_logging_okB   s    
z$AuthenticatorTest.test_ip_logging_okc                 C   s¸   d| j _d tj¡| j _d | jjd| j | jj	¡¡}d | j
j| j
j d¡| j
 | j
j	¡¡}|  | j | j¡dd„ | jD ƒ¡ |  | jj| j d |¡ |  | jj| j
 d |¡ d S )	NTzå{0} -c "from __future__ import print_function;from certbot.compat import os;  print(os.environ.get('CERTBOT_DOMAIN'));print(os.environ.get('CERTBOT_TOKEN', 'notoken'));print(os.environ.get('CERTBOT_VALIDATION', 'novalidation'));"z{0}
{1}
{2}ZnotokenÚtokenc                 S   s   g | ]}|  |j¡‘qS r#   ©ZresponseÚaccount_key©Ú.0Úachallr#   r#   r$   Ú
<listcomp>Y   s     z9AuthenticatorTest.test_script_perform.<locals>.<listcomp>ÚCERTBOT_AUTH_OUTPUT)r   r   ÚformatÚsysÚ
executabler   r   ÚdomainÚ
validationr?   r   ÚchallÚencoder2   r   r8   r   Úenv)r   Zdns_expectedZhttp_expectedr#   r#   r$   Útest_script_performH   s6    üÿ þ þþþþz%AuthenticatorTest.test_script_performc                 C   s|   d| j _|  | j | j¡dd„ | jD ƒ¡ t|ƒ jjƒD ]>\}\}}| j| }|  	| 
|j¡|d k¡ |  |d ¡ q8d S )NTc                 S   s   g | ]}|  |j¡‘qS r#   r>   r@   r#   r#   r$   rC   f   s     z9AuthenticatorTest.test_manual_perform.<locals>.<listcomp>r   Zwrap)r   r   r2   r   r8   r   Ú	enumerateZnotificationZcall_args_listr.   rI   r?   ÚassertFalse)r   r:   ÚiÚargsÚkwargsrB   r#   r#   r$   Útest_manual_performa   s    þ
ÿz%AuthenticatorTest.test_manual_performc                 C   sÚ   d| j _d tj¡| j _d| j _| j | j	¡ | j	D ] }| j 
|g¡ |  tjd d¡ |  tjd |j¡ t|jtjtjfƒrš|  tjd | |j¡¡ t|jtjƒrÄ|  tjd |j d	¡¡ q4|  dtjk¡ q4d S )
NTz,{0} -c "import sys; sys.stdout.write('foo')"z	# cleanuprD   ZfooZCERTBOT_DOMAINZCERTBOT_VALIDATIONZCERTBOT_TOKENr=   )r   r   rE   rF   rG   r   r   r   r8   r   Zcleanupr2   r   ÚenvironrH   r/   rJ   r   r3   r4   rI   r?   rK   rO   )r   rB   r#   r#   r$   Útest_cleanupm   s*    ÿ

þ
þzAuthenticatorTest.test_cleanup)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r*   r-   r1   r5   Ú	test_utilZpatch_get_utilityr;   r<   rM   rS   rU   Ú__classcell__r#   r#   r!   r$   r      s   


r   Ú__main__)rY   ZunittestrF   r   r0   Zacmer   Zcertbotr   Zcertbot.compatr   r   Zcertbot.testsr   r   rZ   ZTempDirTestCaser   rV   Úmainr#   r#   r#   r$   Ú<module>   s   s