U
    ÔZj=#  ã                   @   sn   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
 G dd„ de
jƒZedkrje ¡  dS )	z;Test for certbot_apache.configurator AutoHSTS functionalityé    N)Úerrors)Ú	constants)Úutilc                       s$  e Zd ZdZ‡ fdd„Zdd„ Ze d¡e d¡dd	„ ƒƒZe d¡d
d„ ƒZ	e dd¡e d¡e d¡dd„ ƒƒƒZ
e d¡e d¡dd„ ƒƒZe d¡e dd¡dd„ ƒƒZe dd¡e d¡dd„ ƒƒZdd„ Zdd„ Ze d¡dd„ ƒZe d¡e d¡d d!„ ƒƒZd"d#„ Zd$d%„ Z‡  ZS )&ÚAutoHSTSTestzTests for AutoHSTS featurec                    s|   t t| ƒ ¡  t | j| j| j| j¡| _	| j	j
j d¡ | j	j
j d¡ | j	j
j d¡ | j	j
j d¡ t | jd¡| _d S )NÚheaders_modulezmod_headers.cZ
ssl_modulez	mod_ssl.cz!debian_apache_2_4/multiple_vhosts)Úsuperr   ÚsetUpr   Zget_apache_configuratorZconfig_pathZ
vhost_pathZ
config_dirZwork_dirÚconfigÚparserÚmodulesÚaddZget_vh_truthÚtemp_dirÚvh_truth©Úself©Ú	__class__© úe/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/certbot_apache/tests/autohsts_test.pyr      s       ÿ ÿzAutoHSTSTest.setUpc                 C   s`   | j j dd|¡}|r\d}|D ]<}t || j jj |¡ ¡ ¡r| j jj | dd¡¡  S qdS )z1 Get value from Strict-Transport-Security header ÚHeaderNz/(?:[ "]|^)(strict-transport-security)(?:[ "]|$)zarg[3]zarg[4])	r	   r
   Úfind_dirÚreÚsearchÚaugÚgetÚlowerÚreplace)r   Zvh_pathZheader_pathÚpatÚheadr   r   r   Úget_autohsts_value   s    

ÿ
zAutoHSTSTest.get_autohsts_valuez6certbot_apache.configurator.ApacheConfigurator.restartz9certbot_apache.configurator.ApacheConfigurator.enable_modc                 C   sD   | j jj d¡ | j jj d¡ | j  t ¡ dg¡ |  |j¡ d S )Nr   zmod_header.cúocspvhost.com)	r	   r
   r   ÚdiscardÚenable_autohstsÚmockÚ	MagicMockÚ
assertTrueÚcalled)r   Zmock_enableÚ_restartr   r   r   Ú test_autohsts_enable_headers_mod*   s    z-AutoHSTSTest.test_autohsts_enable_headers_modc                 C   s4   | j  t ¡ dg¡ |  tj| j jt ¡ dg¡ d S )Nr    )r	   r"   r#   r$   ÚassertRaisesr   ZPluginEnhancementAlreadyPresent)r   r'   r   r   r   Ú#test_autohsts_deploy_already_exists2   s     þz0AutoHSTSTest.test_autohsts_deploy_already_existsz&certbot_apache.constants.AUTOHSTS_FREQr   z6certbot_apache.configurator.ApacheConfigurator.preparec                 C   s”   d| j _d}| tjd ¡}| tjd ¡}| j  t ¡ dg¡ |  |  	| j
d j¡|¡ | j  t ¡ ¡ |  |  	| j
d j¡|¡ |  |j¡ d S )NFú"max-age={0}"r   é   r    é   )r	   Z	_preparedÚformatr   ÚAUTOHSTS_STEPSr"   r#   r$   ÚassertEqualr   r   ÚpathÚupdate_autohstsr%   r&   )r   Zmock_prepareÚ_mock_restartÚmaxageÚinitial_valZinc_valr   r   r   Útest_autohsts_increase9   s    ÿÿz#AutoHSTSTest.test_autohsts_increasezAcertbot_apache.configurator.ApacheConfigurator._autohsts_increasec                 C   sb   d}|  tjd ¡}| j t ¡ dg¡ |  |  | j	d j
¡|¡ | j t ¡ ¡ |  |j¡ d S )Nr+   r   r    r-   )r.   r   r/   r	   r"   r#   r$   r0   r   r   r1   r2   ÚassertFalser&   )r   Zmock_increaser'   r4   r5   r   r   r   Útest_autohsts_increase_noopM   s    ÿz(AutoHSTSTest.test_autohsts_increase_noopc                 C   sv   | j  t ¡ dg¡ | j j dd | jd j¡}d |d  	d¡d d… ¡}| j jj
 |¡ |  tj| j jt ¡ ¡ d S )Nr    r   r-   ú/r   éÿÿÿÿ)r	   r"   r#   r$   r
   r   r   r1   ÚjoinÚsplitr   Úremover)   r   ÚPluginErrorr2   )r   r'   Zdir_locsZdir_locr   r   r   Ú test_autohsts_increase_no_header\   s    
ÿþz-AutoHSTSTest.test_autohsts_increase_no_headerc                 C   sþ   d}|  tj¡}t ¡ }d|_| j t ¡ dg¡ tt	tj
ƒd ƒD ]h}| j |¡ |  |  | jd j¡|¡ | j t ¡ ¡ |  tj
|d  ¡}|  |  | jd j¡|¡ qD|  |  | jd j¡|  tj
d ¡¡ | j |¡ |  |  | jd j¡|¡ d S )Nr+   z#/etc/apache2/ssl/key-certbot_15.pemr    r,   r-   r:   )r.   r   ZAUTOHSTS_PERMANENTr#   r$   Zkey_pathr	   r"   ÚrangeÚlenr/   Údeploy_autohstsZassertNotEqualr   r   r1   r2   r0   )r   r3   r4   Zmax_valZmock_lineageÚiZcur_valr   r   r   Ú)test_autohsts_increase_and_make_permanenti   s,    ÿÿÿÿz6AutoHSTSTest.test_autohsts_increase_and_make_permanentc              	   C   s6   t  d¡�"}| j t  ¡ ¡ |  |j¡ W 5 Q R X d S )Nz	time.time)r#   Úpatchr	   r2   r$   r7   r&   )r   Z	mock_timer   r   r   Útest_autohsts_update_noopƒ   s    z&AutoHSTSTest.test_autohsts_update_noopc                 C   s4   t  ¡ | jj_| j t  ¡ ¡ |  | jjjj¡ d S )N)r#   r$   r	   ZstorageÚputrB   r7   r&   r   r   r   r   Ú!test_autohsts_make_permanent_noopŠ   s    z.AutoHSTSTest.test_autohsts_make_permanent_noopz'certbot_apache.display_ops.select_vhostc              	   C   sX   | j d |_t d¡�8}|  tj| jjt 	¡ d¡ |  
d|jd d k¡ W 5 Q R X d S )Nr   ú*certbot_apache.configurator.logger.warningzinvalid.example.comz Certbot was not able to find SSL)r   Úreturn_valuer#   rE   r)   r   r>   r	   r"   r$   r%   Ú	call_args)r   Zmock_selectÚmock_logr   r   r   Útest_autohsts_no_ssl_vhost�   s     þÿz'AutoHSTSTest.test_autohsts_no_ssl_vhostz;certbot_apache.configurator.ApacheConfigurator.add_vhost_idc                 C   s.   d|_ | j t ¡ ddg¡ |  |jd¡ d S )NZ1234567r    r,   )rJ   r	   r"   r#   r$   r0   Z
call_count)r   Zmock_idr'   r   r   r   Ú test_autohsts_dont_enhance_twiceš   s
    ÿz-AutoHSTSTest.test_autohsts_dont_enhance_twicec                 C   sl   | j  ¡  dddœ| j jd< | j  ¡  | j  t ¡ ¡ |  d| j jk¡ d | j _| j  ¡  |  | j j¡ d S )Nr   ©ZlaststepÚ	timestampÚ	orphan_id)r	   Ú_autohsts_fetch_stateÚ	_autohstsÚ_autohsts_save_stater2   r#   r$   r7   r   r   r   r   Útest_autohsts_remove_orphaned¢   s    


z*AutoHSTSTest.test_autohsts_remove_orphanedc              	   C   st   | j  ¡  dddœ| j jd< | j  ¡  t d¡�:}| j  t ¡ ¡ |  |j	¡ |  d|j
d d k¡ W 5 Q R X d S )Niç  r   rO   rQ   rI   z%VirtualHost with id orphan_id was not)r	   rR   rS   rT   r#   rE   rB   r$   r%   r&   rK   )r   rL   r   r   r   Ú,test_autohsts_make_permanent_vhost_not_found¯   s    

ÿz9AutoHSTSTest.test_autohsts_make_permanent_vhost_not_found)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r#   rE   r(   r*   r6   r8   r?   rD   rF   rH   rM   rN   rU   rV   Ú__classcell__r   r   r   r   r      s<   




	r   Ú__main__)rZ   r   Zunittestr#   ÚsixZcertbotr   Zcertbot_apacher   Zcertbot_apache.testsr   Z
ApacheTestr   rW   Úmainr   r   r   r   Ú<module>   s    .