
    M/ed=                     t   d 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 dd
lmZ ddlm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  ej<                         5   ej>                  de        ddl!m"Z" ddd       ddl#m$Z$ ddl%m&Z' dZ(ejR                  jU                   e'jV                  d            Z, e-d      Z.eZ/ ed e             Z0 ed e             Z1 G d de$e
      Z2 G d d e
      Z3 G d! d"ejh                        Z5 G d# d$      Z6 G d% d&e$e
      Z7 G d' d(ejh                        Z8ejr                  d)eeeef   ddf   fd*       Z: G d+ d,      Z; ee e;ejx                  e=               ejx                  e=<   y# 1 sw Y   xY w)-z8Base test class for DNS authenticators built on Lexicon.    N)
ModuleType)Any)cast)	Generator)List)Protocol)Tuple)mock)	MagicMock)Response)	HTTPError)RequestException)errors)AnnotatedChallenge)dns_test_commonignore)category)LexiconClient)_AuthenticatorCallableTestCase)utilzexample.comzrsa512_key.pemzNo domain foundz400 Client Error: ...)responsez500 Surprise! Error: ...c                   &    e Zd ZU dZeed<   eed<   y)%_AuthenticatorCallableLexiconTestCasezq
    Protocol describing a TestCase suitable to test challenges against
    a mocked LexiconClient instance.
    mock_clientachallN)__name__
__module____qualname____doc__r   __annotations__r        I/usr/lib/python3/dist-packages/certbot/plugins/dns_test_common_lexicon.pyr   r   (   s     r"   r   c                   |    e Zd ZU dZeed<   eed<   eed<   eed<   eed<   eed<   eed<   eed	<   eed
<   de	ddfdZ
y)_LexiconAwareTestCasezX
    Protocol describing a TestCase suitable to test a real LexiconClient instance.
    clientprovider_mockrecord_prefixrecord_namerecord_contentDOMAIN_NOT_FOUNDGENERIC_ERRORLOGIN_ERRORUNKNOWN_LOGIN_ERRORunused_argsreturnNc                      y)zl
        See
        https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises
        Nr!   )selfr/   s     r#   assertRaisesz"_LexiconAwareTestCase.assertRaisesA   s    r"   )r   r   r   r   r   r    r   str	Exceptionr   r3   r!   r"   r#   r%   r%   1   sR     ""  r"   r%   c                   X    e Zd Z ej                         dededdfd       ZdeddfdZy)BaseLexiconAuthenticatorTestr2   unused_mock_get_utilityr0   Nc                    | j                   j                  | j                  g       t        j                  j                  t        dt        z   t        j                        g}| j                  || j                  j                         y )N_acme-challenge.)authperformr   r
   calladd_txt_recordDOMAINANYassertEqualr   
mock_calls)r2   r8   expecteds      r#   test_performz)BaseLexiconAuthenticatorTest.test_performM   s`     			4;;-(II,,V5G5NPTPXPXYZ4#3#3#>#>?r"   c                 4   d| j                   _        | j                   j                  | j                  g       t        j
                  j                  t        dt        z   t        j                        g}| j                  || j                  j                         y )NTr:   )r;   _attempt_cleanupcleanupr   r
   r=   del_txt_recordr?   r@   rA   r   rB   )r2   rC   s     r#   test_cleanupz)BaseLexiconAuthenticatorTest.test_cleanupU   sj    %)		"		4;;-(II,,V5G5NPTPXPXYZ4#3#3#>#>?r"   )	r   r   r   	test_utilpatch_display_utilr   r   rD   rI   r!   r"   r#   r7   r7   K   sS    !Y!!#@@ @.1@6:@ $@@@ @T @r"   r7   c                      e Zd ZeZeZeZeZdZedz   ez   Z	dZ
deddfdZdeddfdZdeddfd	Zdeddfd
ZdeddfdZdeddfdZdeddfdZdeddfdZdeddfdZdeddfdZdeddfdZdeddfdZdeddfdZy)BaseLexiconClientTestz_acme-challenge.barr2   r0   Nc                     | j                   j                  t        | j                  | j                         | j
                  j                  j                  d| j                  | j                         y NTXTrtypenamecontent)r&   r>   r?   r)   r*   r'   create_recordassert_called_withr2   s    r#   test_add_txt_recordz)BaseLexiconClientTest.test_add_txt_recordg   X    ""64+;+;T=P=PQ((;;%AEAQAQDHDWDW 	< 	Yr"   c                 4   | j                   dg| j                  j                  _        | j                  j                  t        | j                  | j                         | j                  j                  j                  d| j                  | j                         y )N rR   rS   )r+   r'   authenticateside_effectr&   r>   r?   r)   r*   rW   rX   rY   s    r#   ,test_add_txt_record_try_twice_to_find_domainzBBaseLexiconClientTest.test_add_txt_record_try_twice_to_find_domainn   sw    7;7L7Lb6Q''3""64+;+;T=P=PQ((;;%AEAQAQDHDWDW 	< 	Yr"   c                    | j                   | j                   | j                   g| j                  j                  _        | j	                  t
        j                  | j                  j                  t        | j                  | j                         y N)r+   r'   r^   r_   r3   r   PluginErrorr&   r>   r?   r)   r*   rY   s    r#   'test_add_txt_record_fail_to_find_domainz=BaseLexiconClientTest.test_add_txt_record_fail_to_find_domainw   sm    7;7L7L7;7L7L7;7L7L7O''3 	&,,++44 $"2"2D4G4G	Ir"   c                     | j                   | j                  j                  _        | j	                  t
        j                  | j                  j                  t        | j                  | j                         y rb   )r-   r'   r^   r_   r3   r   rc   r&   r>   r?   r)   r*   rY   s    r#   (test_add_txt_record_fail_to_authenticatez>BaseLexiconClientTest.test_add_txt_record_fail_to_authenticate   sS    6:6F6F''3&,,++44 $"2"2D4G4G	Ir"   c                     | j                   | j                  j                  _        | j	                  t
        j                  | j                  j                  t        | j                  | j                         y rb   )r.   r'   r^   r_   r3   r   rc   r&   r>   r?   r)   r*   rY   s    r#   ;test_add_txt_record_fail_to_authenticate_with_unknown_errorzQBaseLexiconClientTest.test_add_txt_record_fail_to_authenticate_with_unknown_error   sS    6:6N6N''3&,,++44 $"2"2D4G4G	Ir"   c                     | j                   | j                  j                  _        | j	                  t
        j                  | j                  j                  t        | j                  | j                         y rb   )r,   r'   r^   r_   r3   r   rc   r&   r>   r?   r)   r*   rY   s    r#   (test_add_txt_record_error_finding_domainz>BaseLexiconClientTest.test_add_txt_record_error_finding_domain   sS    6:6H6H''3&,,++44 $"2"2D4G4G	Ir"   c                     | j                   | j                  j                  _        | j	                  t
        j                  | j                  j                  t        | j                  | j                         y rb   )r,   r'   rW   r_   r3   r   rc   r&   r>   r?   r)   r*   rY   s    r#   'test_add_txt_record_error_adding_recordz=BaseLexiconClientTest.test_add_txt_record_error_adding_record   sS    7;7I7I((4&,,++44 $"2"2D4G4G	Ir"   c                     | j                   j                  t        | j                  | j                         | j
                  j                  j                  d| j                  | j                         y rQ   )r&   rH   r?   r)   r*   r'   delete_recordrX   rY   s    r#   test_del_txt_recordz)BaseLexiconClientTest.test_del_txt_record   r[   r"   c                     | j                   | j                   | j                   g| j                  j                  _        | j                  j                  t        | j                  | j                         y rb   )	r+   r'   r^   r_   r&   rH   r?   r)   r*   rY   s    r#   'test_del_txt_record_fail_to_find_domainz=BaseLexiconClientTest.test_del_txt_record_fail_to_find_domain   sX    7;7L7L7;7L7L7;7L7L7P''3 	""64+;+;T=P=PQr"   c                     | j                   | j                  j                  _        | j                  j                  t        | j                  | j                         y rb   )	r-   r'   r^   r_   r&   rH   r?   r)   r*   rY   s    r#   (test_del_txt_record_fail_to_authenticatez>BaseLexiconClientTest.test_del_txt_record_fail_to_authenticate   s>    6:6F6F''3""64+;+;T=P=PQr"   c                     | j                   | j                  j                  _        | j                  j                  t        | j                  | j                         y rb   )	r.   r'   r^   r_   r&   rH   r?   r)   r*   rY   s    r#   ;test_del_txt_record_fail_to_authenticate_with_unknown_errorzQBaseLexiconClientTest.test_del_txt_record_fail_to_authenticate_with_unknown_error   s>    6:6N6N''3""64+;+;T=P=PQr"   c                     | j                   | j                  j                  _        | j                  j                  t        | j                  | j                         y rb   )	r,   r'   r^   r_   r&   rH   r?   r)   r*   rY   s    r#   (test_del_txt_record_error_finding_domainz>BaseLexiconClientTest.test_del_txt_record_error_finding_domain   s>    6:6H6H''3""64+;+;T=P=PQr"   c                     | j                   | j                  j                  _        | j                  j                  t        | j                  | j                         y rb   )	r,   r'   rn   r_   r&   rH   r?   r)   r*   rY   s    r#   )test_del_txt_record_error_deleting_recordz?BaseLexiconClientTest.test_del_txt_record_error_deleting_record   s>    7;7I7I((4""64+;+;T=P=PQr"   )r   r   r   r+   r,   r-   r.   r(   r?   r)   r*   r%   rZ   r`   rd   rf   rh   rj   rl   ro   rq   rs   ru   rw   ry   r!   r"   r#   rM   rM   ]   sg   '!MK-%M#%.KNY"7 YD YY;P YUY YI6K IPT II7L IQU II'I,0II7L IQU II6K IPT IY"7 YD YR6K RPT RR7L RQU R
R'R,0RR7L RQU R
R8M RRV Rr"   rM   c                   D    e Zd ZU dZeed<   eed<   eed<   eed<   eed<   y)%_BaseLexiconDNSAuthenticatorTestProtoz6Protocol for BaseLexiconDNSAuthenticatorTest instancesr+   r,   r-   r.   r   N)r   r   r   r   r5   r    r   r!   r"   r#   r{   r{      s#    @""r"   r{   c                       e Zd ZeZeZeZeZdeddfdZdeddfdZ	deddfdZ
deddfdZdeddfdZdeddfd	Zdeddfd
ZdeddfdZdeddfdZdeddfdZdeddfdZdeddfdZdeddfdZy)BaseLexiconDNSAuthenticatorTestr2   r0   Nc                    t        j                         5  t               5 \  }}| j                  j	                  | j
                  g       d d d        d d d        j                          |j                  d   d   }| j                  t        |j                  d             j                  j                  ddt         t        j                         y # 1 sw Y   xY w# 1 sw Y   xY w)Nr   lexicon:domainrR   r:   rS   )rJ   rK   _patch_lexicon_clientr;   r<   r   assert_called	call_argsrA   r?   resolverW   rX   r
   r@   r2   r   mock_operationsconfigs       r#   test_perform_succeedz4BaseLexiconDNSAuthenticatorTest.test_perform_succeed   s    ))+ 	1&( 1,J[/		!!4;;-01	1 	!!#&&q)!,0@!AB%%88 09488 	9 	M1 1	1 	1s"   C!*C
C!C	C!!C*c                 6   t        j                         5  t               5 \  }}| j                  ||g|j                  j
                  _        | j                  j                  | j                  g       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wrb   )
rJ   rK   r   r+   return_value	__enter__r_   r;   r<   r   )r2   r   r   s      r#   7test_perform_with_one_domain_resolution_failure_succeedzWBaseLexiconDNSAuthenticatorTest.test_perform_with_one_domain_resolution_failure_succeed   s    ))+ 	1&( 1,J[/))##B((22>
 		!!4;;-01	1 	11 1	1 	1s#   BAB2BB	BBc                 d   t        j                         5  t               5 \  }}| j                  |j                  j
                  _        | j                  t        j                  | j                  j                  | j                  g       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wrb   )rJ   rK   r   r+   r   r   r_   r3   r   rc   r;   r<   r   r2   r   _s      r#   6test_perform_with_two_domain_resolution_failures_raisezVBaseLexiconDNSAuthenticatorTest.test_perform_with_two_domain_resolution_failures_raise   s    ))+ 	1&( 1,<[!AEAVAV((22>!!&"4"4"&))"3"3#';;-11	1 	11 1	1 	1#   B&A)B	B&B#	B&&B/c                 d   t        j                         5  t               5 \  }}| j                  |j                  j
                  _        | j                  t        j                  | j                  j                  | j                  g       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wrb   )rJ   rK   r   r,   r   r   r_   r3   r   rc   r;   r<   r   r   s      r#   9test_perform_with_domain_resolution_general_failure_raisezYBaseLexiconDNSAuthenticatorTest.test_perform_with_domain_resolution_general_failure_raise   s    ))+ 	1&( 1,<[!AEASAS((22>!!&"4"4"&))"3"3#';;-11	1 	11 1	1 	1r   c                 <   t        j                         5  t               5 \  }}| j                  |_        | j                  t        j                  | j                  j                  | j                  g       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wrb   )rJ   rK   r   r-   r_   r3   r   rc   r;   r<   r   r   s      r#   $test_perform_with_auth_failure_raisezDBaseLexiconDNSAuthenticatorTest.test_perform_with_auth_failure_raise   s    ))+ 	1&( 1,<[!*.*:*:'!!&"4"4"&))"3"3#';;-11	1 	11 1	1 	1#   BAB5BB	BBc                 <   t        j                         5  t               5 \  }}| j                  |_        | j                  t        j                  | j                  j                  | j                  g       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wrb   )rJ   rK   r   r.   r_   r3   r   rc   r;   r<   r   r   s      r#   ,test_perform_with_unknown_auth_failure_raisezLBaseLexiconDNSAuthenticatorTest.test_perform_with_unknown_auth_failure_raise  s    ))+ 	1&( 1,<[!*.*B*B'!!&"4"4"&))"3"3#';;-11	1 	11 1	1 	1r   c                 P   t        j                         5  t               5 \  }}| j                  |j                  _        | j                  t        j                  | j                  j                  | j                  g       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wrb   )rJ   rK   r   r,   rW   r_   r3   r   rc   r;   r<   r   r2   r   r   s      r#   -test_perform_with_create_record_failure_raisezMBaseLexiconDNSAuthenticatorTest.test_perform_with_create_record_failure_raise  s    ))+ 	1&( 1,@Q<@<N<N--9!!&"4"4"&))"3"3#';;-11	1 	11 1	1 	1s#   BAB?BB	BB%c                    d| j                   _        t               5 \  }}| j                   j                  | j                  g       d d d        j                          |j                  d   d   }| j                  t        |j                  d             j                  j                  ddt         t        j                         y # 1 sw Y   xY w)NTr   r   rR   r:   rS   )r;   rF   r   rG   r   r   r   rA   r?   r   rn   rX   r
   r@   r   s       r#   test_cleanup_successz4BaseLexiconDNSAuthenticatorTest.test_cleanup_success  s    %)		""$ 	-(FoIIt{{m,	- 	!!#&&q)!,0@!AB%%88 09488 	9 	M	- 	-s   *C		Cc                     t               5 \  }}| j                  |_        | j                  j	                  | j
                  g       d d d        y # 1 sw Y   y xY wrb   r   r-   r_   r;   rG   r   r   s      r#   %test_cleanup_with_auth_failure_ignorezEBaseLexiconDNSAuthenticatorTest.test_cleanup_with_auth_failure_ignore!  sJ    "$ 	-(8a&*&6&6K#IIt{{m,	- 	- 	-   ;AAc                     t               5 \  }}| j                  |_        | j                  j	                  | j
                  g       d d d        y # 1 sw Y   y xY wrb   r   r   s      r#   -test_cleanup_with_unknown_auth_failure_ignorezMBaseLexiconDNSAuthenticatorTest.test_cleanup_with_unknown_auth_failure_ignore&  sJ    "$ 	-(8a&*&6&6K#IIt{{m,	- 	- 	-r   c                     t               5 \  }}| j                  |j                  j                  _        | j
                  j                  | j                  g       d d d        y # 1 sw Y   y xY wrb   )r   r+   r   r   r_   r;   rG   r   r   s      r#   2test_cleanup_with_domain_resolution_failure_ignorezRBaseLexiconDNSAuthenticatorTest.test_cleanup_with_domain_resolution_failure_ignore,  sV    "$ 	-(8a=A=R=RK$$..:IIt{{m,	- 	- 	-   AA##A,c                     t               5 \  }}| j                  |j                  j                  _        | j
                  j                  | j                  g       d d d        y # 1 sw Y   y xY wrb   )r   r,   r   r   r_   r;   rG   r   r   s      r#   :test_cleanup_with_domain_resolution_general_failure_ignorezZBaseLexiconDNSAuthenticatorTest.test_cleanup_with_domain_resolution_general_failure_ignore2  sV    "$ 	-(8a=A=O=OK$$..:IIt{{m,	- 	- 	-r   c                     t               5 \  }}| j                  |j                  _        | j                  j                  | j                  g       d d d        y # 1 sw Y   y xY wrb   )r   r,   rW   r_   r;   rG   r   r   s      r#   .test_cleanup_with_delete_record_failure_ignorezNBaseLexiconDNSAuthenticatorTest.test_cleanup_with_delete_record_failure_ignore8  sP    "$ 	-(<O8<8J8JO))5IIt{{m,	- 	- 	-s   AAA")r   r   r   r+   r,   r-   r.   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r"   r#   r}   r}      s-   '!MK-
M#H 
MT 
M	17	1<@	1171<@1171<@113X 1]a 1171<@1171<@1
M#H 
MT 
M-4Y -^b -
-7-<@--7-<@--7-<@--7-<@-r"   r}   r0   c               #      K   t        j                  d      5 } t               }|| j                  j                  _        | |f d d d        y # 1 sw Y   y xY ww)Nz)certbot.plugins.dns_common_lexicon.Client)r
   patchr   r   r   )r   r   s     r#   r   r   ?  sO     	?	@ +K#+:I  **7?**+ + +s   A,A	AAAc                   b    e Zd ZdZdefdZdedefdZdededdfd	Z	dedefd
Z
dee   fdZy)_DeprecationModulez
    Internal class delegating to a module, and displaying warnings when attributes
    related to deprecated attributes in the current module.
    modulec                 "    || j                   d<   y N_module)__dict__)r2   r   s     r#   __init__z_DeprecationModule.__init__O  s    #)i r"   attrr0   c                     |dv r&t        j                  | dt         dt        d       t	        | j
                  |      S )N)r7   rM   z attribute in z/ module is deprecated and will be removed soon.   )
stacklevel)warningswarnr   DeprecationWarninggetattrr   r2   r   s     r#   __getattr__z_DeprecationModule.__getattr__R  sC    LLMMTF.
 ;6 6,< t||T**r"   valueNc                 2    t        | j                  ||       y rb   )setattrr   )r2   r   r   s      r#   __setattr__z_DeprecationModule.__setattr__Y  s    dE*r"   c                 0    t        | j                  |       y rb   )delattrr   r   s     r#   __delattr__z_DeprecationModule.__delattr__\  s    d#r"   c                 4    dgt        | j                        z   S r   )dirr   rY   s    r#   __dir__z_DeprecationModule.__dir___  s    {S...r"   )r   r   r   r   r   r   r4   r   r   r   r   r   r   r!   r"   r#   r   r   J  sc    *z *+ + ++ +C +D +$ $ $/c /r"   r   )>r   
contextlibsystypesr   typingr   r   r   r   r   r	   unittestr
   unittest.mockr   r   josepyjoserequestsr   requests.exceptionsr   r   certbotr   certbot.achallengesr   certbot.pluginsr   catch_warningsfilterwarningsr   "certbot.plugins.dns_common_lexiconr   certbot.plugins.dns_test_commonr   certbot.testsr   rJ   r?   JWKRSAloadload_vectorKEYr5   r+   r,   r-   r.   r   r%   BaseAuthenticatorTestr7   rM   r{   r}   contextmanagerr   r   modulesr   r!   r"   r#   <module>r      s   >  
         #    ) 0  2 +X AHH/AB@A K +	
kk,y,,-=>?./  /(*E :XZP ,JH H 4@?#H#H @$aR aRH,JH q-o&K&K q-h +yy)/C)DdD)PQ + +/ /4 Z);CKK<Q)RSH Y
A As   >F--F7