U
    ˜­“]½#  ã                   @   s~   d 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 G dd	„ d	ejƒZed
krze ¡  dS )z9Test for certbot_apache.configurator for Debian overridesé    N)Úerrors)Úos)Úapache_util)Úobj)Úutilc                       sø   e Zd ZdZdZ‡ fdd„Zdd„ Zdd„ Ze 	d	¡e 	d
¡e 	d¡dd„ ƒƒƒZ
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Ze 	d	¡e 	d
¡dd„ ƒƒZe 	d	¡e 	d
¡dd„ ƒƒZe 	d	¡e 	d
¡dd„ ƒƒZdd„ Zd d!„ Ze 	d
¡d"d#„ ƒZ‡  ZS )$ÚMultipleVhostsTestDebianz1Multiple vhost tests for Debian family of distrosTc                    sN   t t| ƒ ¡  tj| j| j| j| jdd| _	|  
| j	¡| _	t | jd¡| _d S )NZdebian)Zos_infoz!debian_apache_2_4/multiple_vhosts)Úsuperr   ÚsetUpr   Úget_apache_configuratorÚconfig_pathÚ
vhost_pathÚ
config_dirÚwork_dirÚconfigÚmock_deploy_certZget_vh_truthZtemp_dirÚvh_truth©Úself©Ú	__class__© úB/usr/lib/python3/dist-packages/certbot_apache/tests/debian_test.pyr	      s       þÿzMultipleVhostsTestDebian.setUpc                    s$   | j jˆ _‡ fdd„}|| j _| j S )zA test for a mock deploy certc               
      sD   d}d}t  |¡( t  |¡ ˆ j| |Ž W 5 Q R X W 5 Q R X dS )z a helper to mock a deployed certz9certbot_apache.configurator.ApacheConfigurator.enable_modz<certbot_apache.override_debian.DebianConfigurator.enable_modN)ÚmockÚpatchÚreal_deploy_cert)ÚargsÚkwargsZg_modZd_mod©r   r   r   Úmocked_deploy_cert!   s
    zEMultipleVhostsTestDebian.mock_deploy_cert.<locals>.mocked_deploy_cert)r   Údeploy_certr   )r   r   r   r   r   r   r      s    
z)MultipleVhostsTestDebian.mock_deploy_certc                 C   s2   t  tj | jjjd¡¡ |  t	j
| jjd¡ d S )Nzmods-enabledÚssl)ÚshutilZrmtreer   ÚpathÚjoinr   ÚparserÚrootÚassertRaisesr   ÚNotSupportedErrorÚ
enable_modr   r   r   r   Ú test_enable_mod_unsupported_dirs+   s      ÿz9MultipleVhostsTestDebian.test_enable_mod_unsupported_dirszcertbot.util.run_scriptzcertbot.util.exe_existsz&certbot_apache.parser.subprocess.Popenc                 C   s\   d|ƒ j _d|ƒ _d|_| j d¡ |  d| jjjk¡ |  d| jjjk¡ |  |j¡ d S )N)zDefine: DUMP_RUN_CFGÚ r   Tr    Ú
ssl_moduleú	mod_ssl.c)	ZcommunicateÚreturn_valueÚ
returncoder   r(   Ú
assertTruer$   ÚmodulesÚcalled)r   Z
mock_popenÚmock_exe_existsZmock_run_scriptr   r   r   Útest_enable_mod0   s    
z(MultipleVhostsTestDebian.test_enable_modc                 C   s”   | j  | jd ¡}| j jj d¡ | j jj d¡ |  |j¡ | j  ddddd¡ |  	|j¡ d	|_|  |j¡ | j  ddddd¡ |  	|j¡ d S )
Nr   r+   r,   zencryption-example.demoúexample/cert.pemúexample/key.pemúexample/cert_chain.pemúexample/fullchain.pemF)
r   Zmake_vhost_sslr   r$   r0   ÚaddZassertFalseÚenabledr   r/   )r   Z	ssl_vhostr   r   r   Ú!test_deploy_cert_enable_new_vhost>   s,       þ   þz:MultipleVhostsTestDebian.test_deploy_cert_enable_new_vhostc                 C   sj   d| j j_t d¡L}d|_t d¡0}d|_|  tj| j j	t
 ddtƒ dd¡¡ W 5 Q R X W 5 Q R X d S )Nú/tmp/nonexistentzcertbot.compat.os.path.isdirTzcertbot.compat.os.path.islinkFZasdfZafsaf)r   r$   r%   r   r   r-   r&   r   r'   Úenable_siter   ZVirtualHostÚset)r   Zmock_dirZ	mock_linkr   r   r   Útest_enable_site_failureP   s    
ýz1MultipleVhostsTestDebian.test_enable_site_failurec                 C   s6  t j| j| j| j| jdd| _|  | j¡| _| jjj	 
d¡ | jjj	 
d¡ | jd | jjd< | j dddd	d
¡ | j ¡  |  | jd j¡ |  d| jjj	k¡ | jj dd
| jd j¡}| jj dd| jd j¡}|  t|ƒd¡ |  t |d ¡| jd j¡ |  t|ƒd¡ |  t |d ¡| jd j¡ d S )N©é   é   é   ©Úversionr+   r,   é   úrandom.demor4   r5   r6   r7   ZsslcertificatefileZsslcertificateKeyfiler   )r   r
   r   r   r   r   r   r   r$   r0   r8   r   Úassocr   Zsaver/   r9   Zfind_dirr"   ZassertEqualÚlenr   Zget_file_pathZfilep)r   Zloc_certZloc_keyr   r   r   Útest_deploy_cert_newssl[   sR       þ   þ
 
þ  
ÿ
þ
þz0MultipleVhostsTestDebian.test_deploy_cert_newsslc                    sx   t jˆ jˆ jˆ jˆ jddˆ _ˆ  ˆ j¡ˆ _ˆ jjj	 
d¡ ˆ jjj	 
d¡ ˆ jd ˆ jjd< ˆ  tj‡ fdd„¡ d S )	Nr?   rC   r+   r,   rE   rF   c                      s   ˆ j  ddd¡S ©NrF   r4   r5   ©r   r   r   r   r   r   Ú<lambda>Š   s    þzOMultipleVhostsTestDebian.test_deploy_cert_newssl_no_fullchain.<locals>.<lambda>©r   r
   r   r   r   r   r   r   r$   r0   r8   r   rG   r&   r   ZPluginErrorr   r   r   r   Ú$test_deploy_cert_newssl_no_fullchain   s       þ
ÿz=MultipleVhostsTestDebian.test_deploy_cert_newssl_no_fullchainc                    sx   t jˆ jˆ jˆ jˆ jddˆ _ˆ  ˆ j¡ˆ _ˆ jjj	 
d¡ ˆ jjj	 
d¡ ˆ jd ˆ jjd< ˆ  tj‡ fdd„¡ d S )	N©r@   rA   é   rC   r+   r,   rE   rF   c                      s   ˆ j  ddd¡S rJ   rK   r   r   r   r   rL   ™   s    þzOMultipleVhostsTestDebian.test_deploy_cert_old_apache_no_chain.<locals>.<lambda>rM   r   r   r   r   Ú$test_deploy_cert_old_apache_no_chainŽ   s       þ
ÿz=MultipleVhostsTestDebian.test_deploy_cert_old_apache_no_chainc                 C   sf   t  ¡ | jj_| jjj d¡ t jdd| j_d|_| j 	d¡ | j 
dd¡ |  d| jjjk¡ d S )Nr,   rO   ©r-   Túcertbot.demozstaple-ocspZsocache_shmcb_module)r   ÚMockr   r$   Úupdate_runtime_variablesr0   r8   Úget_versionr-   Úchoose_vhostÚenhancer/   ©r   Zmock_exeÚ_r   r   r   Útest_ocsp_stapling_enable_mod   s    z6MultipleVhostsTestDebian.test_ocsp_stapling_enable_modc                 C   sX   t  ¡ | jj_| jjj d¡ d|_| j d¡ | j 	ddd¡ |  
d| jjjk¡ d S )Nr,   TrS   zensure-http-headerzStrict-Transport-SecurityZheaders_module)r   rT   r   r$   rU   r0   r8   r-   rW   rX   r/   rY   r   r   r   Ú"test_ensure_http_header_enable_mod©   s    
ÿz;MultipleVhostsTestDebian.test_ensure_http_header_enable_modc                 C   sV   t  ¡ | jj_d|_t jdd| j_| j d¡ | j dd¡ |  	d| jjj
k¡ d S )NT)r@   r@   rR   rS   ZredirectZrewrite_module)r   rT   r   r$   rU   r-   rV   rW   rX   r/   r0   rY   r   r   r   Útest_redirect_enable_mod¶   s    z1MultipleVhostsTestDebian.test_redirect_enable_modc                 C   s(   |   | jd j¡ | j | jd ¡ d S )NrE   )r/   r   r9   r   r<   r   r   r   r   Ú test_enable_site_already_enabledÁ   s    z9MultipleVhostsTestDebian.test_enable_site_already_enabledc              	   C   sL   t  d¡8}d| jj_| jd }d|_| j |¡ |  |j	¡ W 5 Q R X d S )Nz:certbot_apache.configurator.ApacheConfigurator.enable_siter;   r   F)
r   r   r   r$   r%   r   r9   r<   r/   r1   )r   Ze_sZvhr   r   r   Útest_enable_site_call_parentÅ   s    ÿ

z5MultipleVhostsTestDebian.test_enable_site_call_parentc                 C   s   d|_ |  tj| jjd¡ d S )NFr    )r-   r&   r   ZMisconfigurationErrorr   r(   )r   r2   r   r   r   Útest_enable_mod_no_disableÎ   s      ÿz3MultipleVhostsTestDebian.test_enable_mod_no_disable)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z_multiprocess_can_split_r	   r   r)   r   r   r3   r:   r>   rI   rN   rQ   r[   r\   r]   r^   r_   r`   Ú__classcell__r   r   r   r   r      s6   	$
		r   Ú__main__)rd   r!   Zunittestr   Zcertbotr   Zcertbot.compatr   Zcertbot_apacher   r   Zcertbot_apache.testsr   Z
ApacheTestr   ra   Úmainr   r   r   r   Ú<module>   s    F