U
    ÝÁ]º   ã                   @   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 ddlmZ G dd	„ d	ejejƒZG d
d„ dejƒZG dd„ dejƒZG dd„ dejƒZedkrÔe ¡  dS )z%Tests for certbot.plugins.dns_common.é    N)Úerrors)Úutil)Úos)Ú
dns_common)Údns_test_commonc                       s¦   e Zd ZG dd„ dejƒZG dd„ deƒZ‡ fdd„Zdd„ Z	d	d
„ Z
e ¡ dd„ ƒZe ¡ dd„ ƒZe ¡ dd„ ƒZe ¡ dd„ ƒZdd„ Ze ¡ dd„ ƒZ‡  ZS )ÚDNSAuthenticatorTestc                   @   s,   e Zd Ze ¡ Ze ¡ Ze ¡ Zdd„ ZdS )z*DNSAuthenticatorTest._FakeDNSAuthenticatorc                 C   s   dS )Nz!A fake authenticator for testing.© ©Úselfr   r   úA/usr/lib/python3/dist-packages/certbot/plugins/dns_common_test.pyÚ	more_info   s    z4DNSAuthenticatorTest._FakeDNSAuthenticator.more_infoN)	Ú__name__Ú
__module__Ú__qualname__ÚmockZ	MagicMockZ_setup_credentialsÚ_performÚ_cleanupr   r   r   r   r   Ú_FakeDNSAuthenticator   s   r   c                   @   s   e Zd ZdZdZdZdZdS )z DNSAuthenticatorTest._FakeConfigr   é   N)r   r   r   Zfake_propagation_secondsZfake_config_keyÚfake_other_keyÚfake_file_pathr   r   r   r   Ú_FakeConfig   s   r   c                    s,   t t| ƒ ¡  t ¡ | _t | jd¡| _d S )NZfake)Úsuperr   ÚsetUpr   Úconfigr   Úauthr	   ©Ú	__class__r   r   r   #   s    
zDNSAuthenticatorTest.setUpc                 C   s,   | j  | jg¡ | j j tjtjtj¡ d S ©N)	r   ZperformÚachallr   Úassert_called_once_withr   ÚDOMAINr   ÚANYr	   r   r   r   Útest_perform*   s    z!DNSAuthenticatorTest.test_performc                 C   s4   d| j _| j  | jg¡ | j j tjtj	tj	¡ d S )NT)
r   Z_attempt_cleanupZcleanupr   r   r    r   r!   r   r"   r	   r   r   r   Útest_cleanup/   s    z!DNSAuthenticatorTest.test_cleanupc                 C   sB   |ƒ }t jdft jdff|j_| j dd¡ |  | jjjd¡ d S )NÚ ÚvalueÚ	other_key)	Údisplay_utilÚOKÚinputÚside_effectr   Ú
_configureÚassertEqualr   r   ©r
   Úmock_get_utilityÚmock_displayr   r   r   Útest_prompt6   s    ÿz DNSAuthenticatorTest.test_promptc                 C   s0   |ƒ }t jdff|j_|  tj| jjdd¡ d S )NÚcr'   r%   )	r(   ÚCANCELr*   r+   ÚassertRaisesr   ÚPluginErrorr   r,   r.   r   r   r   Útest_prompt_canceled?   s    z)DNSAuthenticatorTest.test_prompt_canceledc                 C   sr   t j | jd¡}t|dƒ ¡  |ƒ }tjdftjdftj| jftj|ff|j_	| j
 dd¡ |  | j
jj|¡ d S )Núfile.iniÚwbr%   únot-a-file.iniÚ	file_path)r   ÚpathÚjoinÚtempdirÚopenÚcloser(   r)   Údirectory_selectr+   r   Ú_configure_filer-   r   r   )r
   r/   r;   r0   r   r   r   Útest_prompt_fileF   s    
ýz%DNSAuthenticatorTest.test_prompt_filec                 C   s0   |ƒ }t jdff|j_|  tj| jjdd¡ d S )Nr2   r:   r%   )	r(   r3   r@   r+   r4   r   r5   r   rA   r.   r   r   r   Útest_prompt_file_canceledT   s    z.DNSAuthenticatorTest.test_prompt_file_canceledc                 C   sX   t j | jd¡}t ddi|¡ t| jd|ƒ | j 	ddddi¡}|  
| d¡d¡ d S )Nr7   Ú	fake_testr&   Úfake_credentialsÚcredentialsr%   Útest)r   r;   r<   r=   r   ÚwriteÚsetattrr   r   Ú_configure_credentialsr-   Úconf)r
   r;   rF   r   r   r   Útest_configure_credentials[   s
    z/DNSAuthenticatorTest.test_configure_credentialsc                 C   s°   t j | jd¡}t ddi|¡ t j | jd¡}t ddi|¡ t| jddƒ |ƒ }tj	dftj	d	ftj	| jftj	|ftj	|ff|j
_| j d
dddi¡}|  | d¡d¡ d S )Nzbad-file.iniZ
fake_otherZother_valuer7   rD   r&   rE   r%   r9   rF   rG   )r   r;   r<   r=   r   rH   rI   r   r(   r)   r@   r+   r   rJ   r-   rK   )r
   r/   Zbad_pathr;   r0   rF   r   r   r   Útest_prompt_credentialsd   s    
üz,DNSAuthenticatorTest.test_prompt_credentials)r   r   r   r   ZDNSAuthenticatorr   Úobjectr   r   r#   r$   Ú	test_utilZpatch_get_utilityr1   r6   rB   rC   rL   rM   Ú__classcell__r   r   r   r   r      s    



	r   c                   @   s6   e Zd ZG dd„ dejƒZdd„ Zdd„ Zdd„ Zd	S )
ÚCredentialsConfigurationTestc                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )z0CredentialsConfigurationTest._MockLoggingHandlerNc                 O   s    |   ¡  tjj| f|ž|Ž d S r   )ÚresetÚloggingÚHandlerÚ__init__)r
   ÚargsÚkwargsr   r   r   rU   |   s    z9CredentialsConfigurationTest._MockLoggingHandler.__init__c                 C   s   | j |j ¡   | ¡ ¡ d S r   )ÚmessagesZ	levelnameÚlowerÚappendZ
getMessage)r
   Úrecordr   r   r   Úemit€   s    z5CredentialsConfigurationTest._MockLoggingHandler.emitc                 C   s   t  t¡| _dS )z-Allows the handler to be reset between tests.N)ÚcollectionsÚdefaultdictÚlistrX   r	   r   r   r   rR   ƒ   s    z6CredentialsConfigurationTest._MockLoggingHandler.reset)r   r   r   rX   rU   r\   rR   r   r   r   r   Ú_MockLoggingHandlery   s   r`   c                 C   sT   t j | jd¡}t dddœ|¡ t |¡}|  d| 	d¡¡ |  d| 	d¡¡ d S )Nútoo-permissive-file.inir&   r   ©rG   ÚotherrG   Ú1rc   )
r   r;   r<   r=   r   rH   r   ÚCredentialsConfigurationr-   rK   )r
   r;   Úcredentials_configurationr   r   r   Útest_valid_file‡   s
    
z,CredentialsConfigurationTest.test_valid_filec                 C   s&   t j | jd¡}|  tjtj|¡ d S )Nr9   )	r   r;   r<   r=   r4   r   r5   r   re   )r
   r;   r   r   r   Útest_nonexistent_file   s    z2CredentialsConfigurationTest.test_nonexistent_filec                 C   sd   |   ¡ }tj |¡ tj | jd¡}t 	|dd¡ 
¡  t |¡ |  dtdd„ |jd D ƒƒ¡ d S )Nra   r8   iä  r   c                 S   s   g | ]}|  d ¡r|‘qS )ZUnsafe)Ú
startswith)Ú.0Ú_r   r   r   Ú
<listcomp>ž   s     
 zXCredentialsConfigurationTest.test_valid_file_with_unsafe_permissions.<locals>.<listcomp>Zwarning)r`   r   ZloggerZ
addHandlerr   r;   r<   r=   r   Z	safe_openr?   re   r-   ÚlenrX   )r
   Úlogr;   r   r   r   Ú'test_valid_file_with_unsafe_permissions•   s    
zDCredentialsConfigurationTest.test_valid_file_with_unsafe_permissionsN)	r   r   r   rS   rT   r`   rg   rh   ro   r   r   r   r   rQ   x   s   	rQ   c                       sT   e Z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
‡  ZS )Ú#CredentialsConfigurationRequireTestc                    s$   t t| ƒ ¡  tj | jd¡| _d S )Nr7   )r   rp   r   r   r;   r<   r=   r	   r   r   r   r   £   s    z)CredentialsConfigurationRequireTest.setUpc                 C   s   t  || j¡ d S r   )r   rH   r;   )r
   Úvaluesr   r   r   Ú_write¨   s    z*CredentialsConfigurationRequireTest._writec                 C   s0   |   dddœ¡ t | j¡}| dddœ¡ d S )Nr&   r   rb   r%   ©rr   r   re   r;   Úrequire©r
   rf   r   r   r   Ú
test_valid«   s    z.CredentialsConfigurationRequireTest.test_validc                 C   s.   |   dddœ¡ t | j¡}| ddi¡ d S )Nr&   r   rb   rG   r%   rs   ru   r   r   r   Útest_valid_but_extra±   s    z8CredentialsConfigurationRequireTest.test_valid_but_extrac                 C   s$   |   i ¡ t | j¡}| i ¡ d S r   rs   ru   r   r   r   Útest_valid_empty·   s    
z4CredentialsConfigurationRequireTest.test_valid_emptyc                 C   s0   |   i ¡ t | j¡}|  tj|jddi¡ d S ©NrG   r%   ©rr   r   re   r;   r4   r   r5   rt   ru   r   r   r   Útest_missing½   s    
z0CredentialsConfigurationRequireTest.test_missingc                 C   s4   |   ddi¡ t | j¡}|  tj|jddi¡ d S ry   rz   ru   r   r   r   Ú
test_blankÃ   s    z.CredentialsConfigurationRequireTest.test_blankc                 C   s4   |   ddi¡ t | j¡}|  tj|jddi¡ d S )NZtetsztypo!rG   r%   rz   ru   r   r   r   Ú	test_typoÉ   s    z-CredentialsConfigurationRequireTest.test_typo)r   r   r   r   rr   rv   rw   rx   r{   r|   r}   rP   r   r   r   r   rp   ¡   s   rp   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚDomainNameGuessTestc                 C   s   |   dt d¡k¡ d S )Núexample.com©Z
assertTruer   Zbase_domain_name_guessesr	   r   r   r   Útest_simple_caseÒ   s
    ÿÿz$DomainNameGuessTest.test_simple_casec                 C   s   |   dt d¡k¡ d S )Nr   zfoo.bar.baz.example.comr€   r	   r   r   r   Útest_sub_domainØ   s
    ÿÿz#DomainNameGuessTest.test_sub_domainc                 C   s   |   dt d¡k¡ d S )Nzexample.co.ukzfoo.bar.baz.example.co.ukr€   r	   r   r   r   Útest_second_level_domainÞ   s
    ÿÿz,DomainNameGuessTest.test_second_level_domainN)r   r   r   r   r‚   rƒ   r   r   r   r   r~   Ð   s   r~   Ú__main__)Ú__doc__r]   rS   Zunittestr   Zcertbotr   r   Zcertbot.compatr   Zcertbot.displayr(   Zcertbot.pluginsr   r   Zcertbot.testsrO   ZTempDirTestCaseZBaseAuthenticatorTestr   rQ   rp   ZTestCaser~   r   Úmainr   r   r   r   Ú<module>   s"   f)/