
    M/eJ!                        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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
ej&                  ej(                        Z G d dej&                        Z G d dej&                        Z G d dej0                        Zedk(  r4 ej6                   ej8                  ej:                  dd egz                yy)z%Tests for certbot.plugins.dns_common.    N)mock)errors)util)os)
dns_common)dns_test_commonc                       e Zd Z G d dej                        Z G d d      Z fdZ ej                         d        Z
d Z ej                         d        Z ej                         d	        Z ej                         d
        Z ej                         d        Zd Z ej                         d        Zd Z xZS )DNSAuthenticatorTestc                   x    e Zd Z ej                         Z ej                         Z ej                         Zd Zy)*DNSAuthenticatorTest._FakeDNSAuthenticatorc                      y)Nz!A fake authenticator for testing. selfs    Q/usr/lib/python3/dist-packages/certbot/_internal/tests/plugins/dns_common_test.py	more_infoz4DNSAuthenticatorTest._FakeDNSAuthenticator.more_info   s    6    N)	__name__
__module____qualname__r   	MagicMock_setup_credentials_perform_cleanupr   r   r   r   _FakeDNSAuthenticatorr      s2    +T^^-!4>>#!4>>#	7r   r   c                       e Zd ZdZdZdZdZy) DNSAuthenticatorTest._FakeConfigr      N)r   r   r   fake_propagation_secondsfake_config_keyfake_other_keyfake_file_pathr   r   r   _FakeConfigr      s    #$ r   r#   c                     t         |           t        j                         | _        t        j                  | j                  d      | _        y )Nfake)supersetUpr
   r#   configr   authr   	__class__s    r   r'   zDNSAuthenticatorTest.setUp%   s4    *668(>>t{{FS	r   c                     | j                   j                  | j                  g       | j                   j                  j	                  t
        j                  t        j                  t        j                         y N)	r)   performachallr   assert_called_once_withr   DOMAINr   ANY)r   unused_mock_get_utilitys     r   test_performz!DNSAuthenticatorTest.test_perform,   sG    		4;;-(		22?3I3I488UYU]U]^r   c                    d| j                   _        | j                   j                  | j                  g       | j                   j                  j                  t        j                  t        j                  t        j                         y )NT)
r)   _attempt_cleanupcleanupr/   r   r0   r   r1   r   r2   r   s    r   test_cleanupz!DNSAuthenticatorTest.test_cleanup2   sS    %)		"		4;;-(		22?3I3I488UYU]U]^r   c                      |       }t         j                  dft         j                  dff|j                  _        | j                  j                  dd       | j                  j                  j                  dk(  sJ y )N value	other_key)display_utilOKinputside_effectr)   
_configurer(   r!   r   mock_get_utilitymock_displays      r   test_promptz DNSAuthenticatorTest.test_prompt9   sg    ')+7??B*@+7??G*E*G& 			["-yy..'999r   c                      |       }t         j                  dff|j                  _        t	        j
                  t        j                        5  | j                  j                  dd       d d d        y # 1 sw Y   y xY w)Ncr<   r:   )
r=   CANCELr?   r@   pytestraisesr   PluginErrorr)   rA   rB   s      r   test_prompt_canceledz)DNSAuthenticatorTest.test_prompt_canceledB   sa    ')+7+>+>*E)G&]]6--. 	2II  b1	2 	2 	2   A33A<c                    t         j                  j                  | j                  d      }t	        |d      j                           |       }t        j                  dft        j                  dft        j                  | j                  ft        j                  |ff|j                  _	        | j                  j                  dd       | j                  j                  j                  |k(  sJ y )Nfile.iniwbr:   not-a-file.ini	file_path)r   pathjointempdiropencloser=   r>   directory_selectr@   r)   _configure_filer(   r"   )r   rC   rS   rD   s       r   test_prompt_filez%DNSAuthenticatorTest.test_prompt_fileJ   s    ww||DLL*5T4 ')6Boor5K6BooGW5Y6Boot||5T6Boot5M5O%%1
 			!!+r2yy..$666r   c                      |       }t         j                  dff|j                  _        t	        j
                  t        j                        5  | j                  j                  dd       d d d        y # 1 sw Y   y xY w)NrG   rR   r:   )
r=   rH   rX   r@   rI   rJ   r   rK   r)   rY   rB   s      r   test_prompt_file_canceledz.DNSAuthenticatorTest.test_prompt_file_canceledX   sa    ')6B6I6I35P4R%%1]]6--. 	7II%%k26	7 	7 	7rM   c                     t         j                  j                  | j                  d      }t	        j
                  ddi|       t        | j                  d|       | j                  j                  ddddi      }|j                  d      dk(  sJ y )NrO   	fake_testr;   fake_credentialscredentialsr:   test)r   rS   rT   rU   r   writesetattrr(   r)   _configure_credentialsconf)r   rS   r`   s      r   test_configure_credentialsz/DNSAuthenticatorTest.test_configure_credentials`   sv    ww||DLL*5{G4d;/6ii66}b6SU,W'7222r   c                    t         j                  j                  | j                  d      }t	        j
                  ddi|       t         j                  j                  | j                  d      }t	        j
                  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                  j                  d
dddi      }|j                  d      dk(  sJ y )Nzbad-file.ini
fake_otherother_valuerO   r^   r;   r_   r:   rQ   r`   ra   )r   rS   rT   rU   r   rb   rc   r(   r=   r>   rX   r@   r)   rd   re   )r   rC   bad_pathrS   rD   r`   s         r   test_prompt_credentialsz,DNSAuthenticatorTest.test_prompt_credentialsi   s    77<<n=|];XFww||DLL*5{G4d;/4')6Boor5K6BooGW5Y6Boot||5T6Boox5P6Boot5M	5O%%1 ii66}b6SU,W'7222r   c                 h    d| j                   j                  t        j                         g      v sJ y )Nz@try increasing --fake-propagation-seconds (currently 0 seconds).)r)   	auth_hintr   r   r   s    r   test_auth_hintz#DNSAuthenticatorTest.test_auth_hint|   s0    QII!1 234 	4 4r   )r   r   r   r   DNSAuthenticatorr   r#   r'   	test_utilpatch_display_utilr4   r8   rE   rL   rZ   r\   rf   rk   rn   __classcell__r+   s   @r   r
   r
      s    7
 ; ; 7 T "Y!!#_ $_
_ "Y!!#: $: "Y!!#2 $2 "Y!!#7 $7 "Y!!#7 $73 "Y!!#3 $3$4r   r
   c                   H    e Zd Z G d dej                        Zd Zd Zd Zy)CredentialsConfigurationTestc                   .     e Zd ZdZ fdZd Zd Z xZS )0CredentialsConfigurationTest._MockLoggingHandlerNc                 D    | j                          t        |   |i | y r-   )resetr&   __init__)r   argskwargsr+   s      r   rz   z9CredentialsConfigurationTest._MockLoggingHandler.__init__   s    JJLGd-f-r   c                     | j                   |j                  j                            j                  |j	                                y r-   )messages	levelnamelowerappend
getMessage)r   records     r   emitz5CredentialsConfigurationTest._MockLoggingHandler.emit   s1    MM&**0023::6;L;L;NOr   c                 @    t        j                  t              | _        y)z-Allows the handler to be reset between tests.N)collectionsdefaultdictlistr~   r   s    r   ry   z6CredentialsConfigurationTest._MockLoggingHandler.reset   s    '33D9DMr   )r   r   r   r~   rz   r   ry   rr   rs   s   @r   _MockLoggingHandlerrw      s    	.	P	:r   r   c                    t         j                  j                  | j                  d      }t	        j
                  ddd|       t        j                  |      }d|j                  d      k(  sJ d|j                  d      k(  sJ y )Ntoo-permissive-file.inir;   r   ra   otherra   1r   )	r   rS   rT   rU   r   rb   r   CredentialsConfigurationre   )r   rS   credentials_configurations      r   test_valid_filez,CredentialsConfigurationTest.test_valid_file   su    ww||DLL*CDw;TB$.$G$G$M!388@@@@/44W====r   c                     t         j                  j                  | j                  d      }t	        j
                  t        j                        5  t        j                  |       d d d        y # 1 sw Y   y xY w)NrQ   )
r   rS   rT   rU   rI   rJ   r   rK   r   r   )r   rS   s     r   test_nonexistent_filez2CredentialsConfigurationTest.test_nonexistent_file   sP    ww||DLL*:;]]6--. 	6//5	6 	6 	6s   A--A6c           	         | j                         }t        j                  j                  |       t        j
                  j                  | j                  d      }t        j                  |dd      j                          t        j                  |       dt        |j                  d   D cg c]  }|j                  d      s| c}      k(  sJ y c c}w )Nr   rP   i  r   warningUnsafe)r   r   logger
addHandlerr   rS   rT   rU   r   	safe_openrW   r   lenr~   
startswith)r   logrS   _s       r   'test_valid_file_with_unsafe_permissionszDCredentialsConfigurationTest.test_valid_file_with_unsafe_permissions   s    &&($$S)ww||DLL*CDtT5)//1++D1CCLL$;Vqq||H?UVWWWWVs   +C
C
N)	r   r   r   loggingHandlerr   r   r   r   r   r   r   ru   ru      s"    :goo :>6	Xr   ru   c                   H     e Zd Z fdZd Zd Zd Zd Zd Zd Z	d Z
 xZS )	#CredentialsConfigurationRequireTestc                     t         |           t        j                  j	                  | j
                  d      | _        y )NrO   )r&   r'   r   rS   rT   rU   r*   s    r   r'   z)CredentialsConfigurationRequireTest.setUp   s&    GGLLz:	r   c                 D    t        j                  || j                         y r-   )r   rb   rS   )r   valuess     r   _writez*CredentialsConfigurationRequireTest._write   s    fdii0r   c                     | j                  ddd       t        j                  | j                        }|j	                  ddd       y )Nr;   r   r   r:   r   r   r   rS   requirer   r   s     r   
test_validz.CredentialsConfigurationRequireTest.test_valid   s;    Wq12$.$G$G		$R!!))2*CDr   c                     | j                  ddd       t        j                  | j                        }|j	                  ddi       y )Nr;   r   r   ra   r:   r   r   s     r   test_valid_but_extraz8CredentialsConfigurationRequireTest.test_valid_but_extra   s:    Wq12$.$G$G		$R!!))62,7r   c                     | j                  i        t        j                  | j                        }|j	                  i        y r-   r   r   s     r   test_valid_emptyz4CredentialsConfigurationRequireTest.test_valid_empty   s0    B$.$G$G		$R!!))"-r   c                     | j                  i        t        j                  | j                        }t	        j
                  t        j                        5  |j                  ddi       d d d        y # 1 sw Y   y xY wNra   r:   	r   r   r   rS   rI   rJ   r   rK   r   r   s     r   test_missingz0CredentialsConfigurationRequireTest.test_missing   s[    B$.$G$G		$R!]]6--. 	<%--vrl;	< 	< 	<s   A11A:c                     | j                  ddi       t        j                  | j                        }t	        j
                  t        j                        5  |j                  ddi       d d d        y # 1 sw Y   y xY wr   r   r   s     r   
test_blankz.CredentialsConfigurationRequireTest.test_blank   s`    VRL!$.$G$G		$R!]]6--. 	<%--vrl;	< 	< 	<   A33A<c                     | j                  ddi       t        j                  | j                        }t	        j
                  t        j                        5  |j                  ddi       d d d        y # 1 sw Y   y xY w)Ntetsztypo!ra   r:   r   r   s     r   	test_typoz-CredentialsConfigurationRequireTest.test_typo   sa    VW%&$.$G$G		$R!]]6--. 	<%--vrl;	< 	< 	<r   )r   r   r   r'   r   r   r   r   r   r   r   rr   rs   s   @r   r   r      s,    ;
1E8.<<<r   r   c                       e Zd Zd Zd Zd Zy)DomainNameGuessTestc                 6    dt        j                  d      v sJ y )Nexample.comr   base_domain_name_guessesr   s    r   test_simple_casez$DomainNameGuessTest.test_simple_case   s"    //>? 	? ?r   c                 6    dt        j                  d      v sJ y )Nr   zfoo.bar.baz.example.comr   r   s    r   test_sub_domainz#DomainNameGuessTest.test_sub_domain   s&    //0IJK 	K Kr   c                 6    dt        j                  d      v sJ y )Nzexample.co.ukzfoo.bar.baz.example.co.ukr   r   s    r   test_second_level_domainz,DomainNameGuessTest.test_second_level_domain   s&    //0KLM 	M Mr   N)r   r   r   r   r   r   r   r   r   r   r      s    ?KMr   r   __main__r   )__doc__r   r   sysunittestr   rI   certbotr   r   certbot.compatr   certbot.displayr=   certbot.pluginsr   r   certbot.testsrp   TempDirTestCaseBaseAuthenticatorTestr
   ru   r   TestCaser   r   exitmainargv__file__r   r   r   <module>r      s    +   
       0 & + +j4944o6[6[ j4Z'X9#<#< 'XT/<)*C*C /<dM(++ M zCHH[V[[!"
234 r   