
    Vh\                     &   d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
mZ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mZ ddlmZ ddl m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z. d Z/ G d d      Z0 G d d      Z1 G d de1e0ejd                        Z3 G d de1ejd                        Z4 G d d e1ejd                        Z5 eejl                          G d! d"      Z7 G d# d$ejd                        Z8y)%z!
Tests for L{twisted.web._auth}.
    N)implementer)verifyObject)errorportal)	ANONYMOUSAllowAnonymousAccess'InMemoryUsernamePasswordDatabaseDontUse)IUsernamePassword)IPv4Address)ConnectionDone)EventLoggingObserver)globalLogPublisher)Failure)unittest)basicdigest)BasicCredentialFactory)HTTPAuthSessionWrapperUnauthorizedResource)ICredentialFactory)	IResourceResourcegetChildForRequestNOT_DONE_YET)Data)DummyRequestc                 H    t        j                  |       j                         S N)base64	b64encodestrip)ss    N/home/dcms/DCMS/lib/python3.12/site-packages/twisted/web/test/test_httpauth.pyr!   r!   %   s    A$$&&    c                   <    e Zd ZdZd Zd
dZd Zd Zd Zd Z	d	 Z
y)BasicAuthTestsMixinz
    L{TestCase} mixin class which defines a number of tests for
    L{basic.BasicCredentialFactory}.  Because this mixin defines C{setUp}, it
    must be inherited before L{TestCase}.
    c                     | j                         | _        d| _        d| _        d| _        t        j                  | j                        | _        y )Ns   foos   dreids   S3CuR1Ty)makeRequestrequestrealmusernamepasswordr   r   credentialFactoryselfs    r$   setUpzBasicAuthTestsMixin.setUp0   s>    '')
 #!&!=!=djj!Ir%   Nc                 2    t        | j                  d      )z
        Create a request object to be passed to
        L{basic.BasicCredentialFactory.decode} along with a response value.
        Override this in a subclass.
        z did not implement makeRequest)NotImplementedError	__class__)r0   methodclientAddresss      r$   r)   zBasicAuthTestsMixin.makeRequest7   s     "T^^$66T"UVVr%   c                 V    | j                  t        t        | j                               y)zM
        L{BasicCredentialFactory} implements L{ICredentialFactory}.
        N
assertTruer   r   r.   r/   s    r$   test_interfacez"BasicAuthTestsMixin.test_interface?        	%79O9OPQr%   c                    t        dj                  | j                  d| j                  g            }| j                  j                  || j                        }| j                  t        j                  |             | j                  |j                  | j                               | j                  |j                  | j                  dz                y)z
        L{basic.BasicCredentialFactory.decode} turns a base64-encoded response
        into a L{UsernamePassword} object with a password which reflects the
        one which was encoded in the response.
        r%      :s   wrongN)r!   joinr,   r-   r.   decoder*   r9   r
   
providedBycheckPasswordassertFalser0   responsecredss      r$   test_usernamePasswordz)BasicAuthTestsMixin.test_usernamePasswordE   s     SXXt}}dDMM&JKL&&--hE)44U;<++DMM:;,,T]]X-EFGr%   c                 f   t        dj                  | j                  d| j                  g            }|j	                  d      }| j
                  j                  || j                        }| j                  t        t        |             | j                  |j                  | j                               y)zz
        L{basic.BasicCredentialFactory.decode} decodes a base64-encoded
        response with incorrect padding.
        r%   r=      =N)r!   r>   r,   r-   r"   r.   r?   r*   r9   r   r
   rA   rC   s      r$   test_incorrectPaddingz)BasicAuthTestsMixin.test_incorrectPaddingR   s    
 SXXt}}dDMM&JKL>>$'&&--hE%6>?++DMM:;r%   c                     d}| j                  t        j                  | j                  j                  || j                                y)z
        L{basic.BasicCredentialFactory.decode} raises L{LoginFailed} if passed
        a response which is not base64-encoded.
           xN)assertRaisesr   LoginFailedr.   r?   r)   r0   rD   s     r$   test_invalidEncodingz(BasicAuthTestsMixin.test_invalidEncoding^   s>    
 ""))		
r%   c                     t        d      }| j                  t        j                  | j                  j
                  || j                                y)z
        L{basic.BasicCredentialFactory.decode} raises L{LoginFailed} when
        passed a response which is not valid base64-encoded text.
        s   123abc+/N)r!   rL   r   rM   r.   r?   r)   rN   s     r$   test_invalidCredentialsz+BasicAuthTestsMixin.test_invalidCredentialsk   sC    
 [)""))		
r%      GETN)__name__
__module____qualname____doc__r1   r)   r:   rF   rI   rO   rQ    r%   r$   r'   r'   )   s.    JWRH
<

r%   r'   c                       e Zd ZddZy)RequestMixinNc                 V    |t        ddd      }t        d      }||_        ||_        |S )zo
        Create a L{DummyRequest} (change me to create a
        L{twisted.web.http.Request} instead).
        TCP	localhosti     /)r   r   r5   client)r0   r5   r6   r*   s       r$   r)   zRequestMixin.makeRequestz   s5    
  '{DAMt$&r%   rR   )rT   rU   rV   r)   rX   r%   r$   rZ   rZ   y   s    
r%   rZ   c                       e Zd ZdZy)BasicAuthTestszK
    Basic authentication tests which use L{twisted.web.http.Request}.
    N)rT   rU   rV   rW   rX   r%   r$   ra   ra      s    r%   ra   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)DigestAuthTestszL
    Digest authentication tests which use L{twisted.web.http.Request}.
    c                     d| _         d| _        t        j                  | j                  | j                         | _        | j                         | _        y)z>
        Create a DigestCredentialFactory for testing
        
   test realm   md5N)r+   	algorithmr   DigestCredentialFactoryr.   r)   r*   r/   s    r$   r1   zDigestAuthTests.setUp   sD     #
!'!?!?NNDJJ"
 '')r%   c                 *    dddgt                fd} j                   j                  j                  d|        j	                  t        dd            } j                  j                  |        j                  d          y	)
z
        L{digest.DigestCredentialFactory.decode} calls the C{decode} method on
        L{twisted.cred.digest.DigestCredentialFactory} with the HTTP method and
        host of the request.
        s   169.254.0.1rS   Fc                 |    j                  |        j                  |       j                  |       dd<   y )NTr   )assertEqual)	_response_method_hostdonehostr5   rD   r0   s      r$   checkz*DigestAuthTests.test_decode.<locals>.check   s<    Xy1VW-T5)DGr%   r?   r\   Q   r   N)objectpatchr.   r   r)   r   r?   r9   )r0   rq   reqro   rp   r5   rD   s   `  @@@@r$   test_decodezDigestAuthTests.test_decode   s     w8	 	 	

4))00(EBv{5$'CD%%h4Q r%   c                 V    | j                  t        t        | j                               y)zN
        L{DigestCredentialFactory} implements L{ICredentialFactory}.
        Nr8   r/   s    r$   r:   zDigestAuthTests.test_interface   r;   r%   c                 b   | j                   j                  | j                        }| j                  |d   d       | j                  |d   d       | j                  |d   d       | j	                  d|       | j	                  d|       |j                         D ]  }| j                  d	|        y
)ah  
        The challenge issued by L{DigestCredentialFactory.getChallenge} must
        include C{'qop'}, C{'realm'}, C{'algorithm'}, C{'nonce'}, and
        C{'opaque'} keys.  The values for the C{'realm'} and C{'algorithm'}
        keys must match the values supplied to the factory's initializer.
        None of the values may have newlines in them.
        qop   authr+   re   rg   rf   nonceopaque   
N)r.   getChallenger*   rk   assertInvaluesassertNotIn)r0   	challengevs      r$   test_getChallengez!DigestAuthTests.test_getChallenge   s     **77E	5)737+];;/8gy)h	*!!# 	'AUA&	'r%   c                 $   | j                  dd      }| j                  j                  |      }| j                  |d   d       | j                  |d   d       | j                  |d   d       | j	                  d	|       | j	                  d
|       y)z
        L{DigestCredentialFactory.getChallenge} can issue a challenge even if
        the L{Request} it is passed returns L{None} from C{getClientIP}.
        rS   Nry   rz   r+   re   rg   rf   r{   r|   )r)   r.   r~   rk   r   )r0   r*   r   s      r$    test_getChallengeWithoutClientIPz0DigestAuthTests.test_getChallengeWithoutClientIP   s    
 ""640**77@	5)737+];;/8gy)h	*r%   N)	rT   rU   rV   rW   r1   rv   r:   r   r   rX   r%   r$   rc   rc      s!    	*!,R'"+r%   rc   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	UnauthorizedResourceTestsz,
    Tests for L{UnauthorizedResource}.
    c                     t        g       }| j                  |j                  dd      |       | j                  |j                  dd      |       y)zF
        An L{UnauthorizedResource} is every child of itself.
        fooNbar)r   assertIdenticalgetChildWithDefault)r0   resources     r$   test_getChildWithDefaultz2UnauthorizedResourceTests.test_getChildWithDefault   sH     (+X99%FQX99%FQr%   c                     t        t        d      g      }|j                  |       | j                  |j                  d       | j                  |j
                  j                  d      dg       y)z
        Render L{UnauthorizedResource} for the given request object and verify
        that the response code is I{Unauthorized} and that a I{WWW-Authenticate}
        header is set in the response containing a challenge.
        example.com     www-authenticates   basic realm="example.com"N)r   r   renderrk   responseCoderesponseHeadersgetRawHeaders)r0   r*   r   s      r$   _unauthorizedRenderTestz1UnauthorizedResourceTests._unauthorizedRenderTest   sb     ()?)N(OPx --s3##112EF)*	
r%   c                     | j                         }| j                  |       | j                  ddj                  |j                               y)z
        L{UnauthorizedResource} renders with a 401 response code and a
        I{WWW-Authenticate} header and puts a simple unauthorized message
        into the response body.
        s   Unauthorizedr%   Nr)   r   rk   r>   writtenr0   r*   s     r$   test_renderz%UnauthorizedResourceTests.test_render   s=     ""$$$W-#((7??*CDr%   c                     | j                  d      }| j                  |       | j                  ddj                  |j                               y)z
        The rendering behavior of L{UnauthorizedResource} for a I{HEAD} request
        is like its handling of a I{GET} request, but no response body is
        written.
        s   HEAD)r5   r%   Nr   r   s     r$   test_renderHEADz)UnauthorizedResourceTests.test_renderHEAD   sB     ""'"2$$W-chhw78r%   c                     t        t        d      g      }| j                         }|j                  |       | j	                  |j
                  j                  d      dg       y)z
        The realm value included in the I{WWW-Authenticate} header set in
        the response when L{UnauthorizedResounrce} is rendered has quotes
        and backslashes escaped.
        zexample\"foor   s   basic realm="example\\\"foo"N)r   r   r)   r   rk   r   r   )r0   r   r*   s      r$   test_renderQuotesRealmz0UnauthorizedResourceTests.test_renderQuotesRealm  sZ     ()?)P(QR""$x ##112EF/0	
r%   c                 
   t        t        j                  dd      g      }| j                         }|j	                  |       |j
                  j                  d      d   }| j                  d|       | j                  d|       y)z
        The digest value included in the I{WWW-Authenticate} header
        set in the response when L{UnauthorizedResource} is rendered
        has quotes and backslashes escaped.
        rf   s   example\"foor   r   s   realm="example\\\"foo"s   hm="md5N)r   r   rh   r)   r   r   r   r   )r0   r   r*   
authHeaders       r$   test_renderQuotesDigestz1UnauthorizedResourceTests.test_renderQuotesDigest  sx     (++F4DEF
 ""$x ,,::;NOPQR
2J?j*-r%   N)
rT   rU   rV   rW   r   r   r   r   r   r   rX   r%   r$   r   r      s'    R
E9
.r%   r   c                   "    e Zd ZdZd Zd Zd Zy)RealmaJ  
    A simple L{IRealm} implementation which gives out L{WebAvatar} for any
    avatarId.

    @type loggedIn: C{int}
    @ivar loggedIn: The number of times C{requestAvatar} has been invoked for
        L{IResource}.

    @type loggedOut: C{int}
    @ivar loggedOut: The number of times the logout callback has been invoked.
    c                 .    d| _         d| _        || _        y )Nr   )	loggedOutloggedInavatarFactory)r0   r   s     r$   __init__zRealm.__init__5  s    *r%   c                     t         |v r7| xj                  dz  c_        t         | j                  |      | j                  fS t	               N   )r   r   r   logoutr3   )r0   avatarIdmind
interfacess       r$   requestAvatarzRealm.requestAvatar:  s=    
"MMQMd00:DKKGG!##r%   c                 .    | xj                   dz  c_         y r   )r   r/   s    r$   r   zRealm.logout@  s    !r%   N)rT   rU   rV   rW   r   r   r   rX   r%   r$   r   r   (  s    
+
$r%   r   c                       e Zd ZdZeZd Zd Zd Zd Z	d Z
d Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)HTTPAuthHeaderTestsz.
    Tests for L{HTTPAuthSessionWrapper}.
    c                    d| _         d| _        d| _        d| _        d| _        t               | _        | j                  j                  | j                   | j                         t        | j                  d      | _	        | j                  j                  | j                  t        | j                  d             | j                   | j                  i| _        t        | j                  j                        | _        t        j                   | j                  | j                  g      | _        g | _        t%        | j                  | j"                        | _        y)z\
        Create a realm, portal, and L{HTTPAuthSessionWrapper} to use in the tests.
        s   foo bars   bar bazs&   contents of the avatar resource itselfs	   foo-childs'   contents of the foo child of the avatar
text/plainN)r,   r-   avatarContent	childNamechildContentr	   checkeraddUserr   avatarputChildavatarsr   getr+   r   PortalcredentialFactoriesr   wrapperr/   s    r$   r1   zHTTPAuthHeaderTests.setUpK  s     #"F%F>@T]]DMM:4--|<T^^T$2C2C\-RSt{{34<<++,
mmDJJ?#% -dkk4;S;STr%   c                     t        | j                  dz   | j                  z         }|j                  j	                  dd|z          t        | j                  |      S )z
        Add an I{basic authorization} header to the given request and then
        dispatch it, starting from C{self.wrapper} and returning the resulting
        L{IResource}.
        r=      authorization   Basic )r!   r,   r-   requestHeadersaddRawHeaderr   r   )r0   r*   authorizations      r$   _authorizedBasicLoginz)HTTPAuthHeaderTests._authorizedBasicLogin^  sM     "$--$"6"FG++,<i->WX!$,,88r%   c                       j                   j                  g      t         j                        }j	                         } fd}|j                  |       j                  |       |S )z
        Resource traversal which encounters an L{HTTPAuthSessionWrapper}
        results in an L{UnauthorizedResource} instance when the request does
        not have the required I{Authorization} headers.
        c                 >    j                  j                  d       y Nr   rk   r   resultr*   r0   s    r$   
cbFinishedz@HTTPAuthHeaderTests.test_getChildWithDefault.<locals>.cbFinishedr      W1137r%   )r)   r   r   r   notifyFinishaddCallbackr   r0   childdr   r*   s   `   @r$   r   z,HTTPAuthHeaderTests.test_getChildWithDefaulth  s^     ""DNN#34"4<<9  "	8 	
j!ur%   c                 ^     j                   j                  t        d              j                   j                  g      j
                  j                  d|       t         j                        }j                         } fd}|j                  |       j                  |       |S )a(  
        Create a request with the given value as the value of an
        I{Authorization} header and perform resource traversal with it,
        starting at C{self.wrapper}.  Assert that the result is a 401 response
        code.  Return a L{Deferred} which fires when this is all done.
        r   r   c                 >    j                  j                  d       y r   r   r   s    r$   r   zAHTTPAuthHeaderTests._invalidAuthorizationTest.<locals>.cbFinished  r   r%   )r   appendr   r)   r   r   r   r   r   r   r   r   )r0   rD   r   r   r   r*   s   `    @r$   _invalidAuthorizationTestz-HTTPAuthHeaderTests._invalidAuthorizationTesty  s     	  ''(>}(MN""DNN#34++,<hG"4<<9  "	8 	
j!ur%   c                 <    | j                  dt        d      z         S )z
        Resource traversal which enouncters an L{HTTPAuthSessionWrapper}
        results in an L{UnauthorizedResource} when the request has an
        I{Authorization} header with a user which does not exist.
        r   s   foo:bar)r   r!   r/   s    r$   (test_getChildWithDefaultUnauthorizedUserz<HTTPAuthHeaderTests.test_getChildWithDefaultUnauthorizedUser  s     --i)J:O.OPPr%   c                 V    | j                  dt        | j                  dz         z         S )z
        Resource traversal which enouncters an L{HTTPAuthSessionWrapper}
        results in an L{UnauthorizedResource} when the request has an
        I{Authorization} header with a user which exists and the wrong
        password.
        r   s   :bar)r   r!   r,   r/   s    r$   ,test_getChildWithDefaultUnauthorizedPasswordz@HTTPAuthHeaderTests.test_getChildWithDefaultUnauthorizedPassword  s-     --	$--'"9::
 	
r%   c                 $    | j                  d      S )z
        Resource traversal which enouncters an L{HTTPAuthSessionWrapper}
        results in an L{UnauthorizedResource} when the request has an
        I{Authorization} header with an unrecognized scheme.
        s   Quux foo bar baz)r   r/   s    r$   *test_getChildWithDefaultUnrecognizedSchemez>HTTPAuthHeaderTests.test_getChildWithDefaultUnrecognizedScheme  s     --.ABBr%   c                      j                   j                  t        d              j                   j                  g       j                        }j                         } fd}|j                  |       j                  |       |S )z
        Resource traversal which encounters an L{HTTPAuthSessionWrapper}
        results in an L{IResource} which renders the L{IResource} avatar
        retrieved from the portal when the request has a valid I{Authorization}
        header.
        r   c                 T    j                  j                  j                  g       y r   )rk   r   r   ignoredr*   r0   s    r$   r   zJHTTPAuthHeaderTests.test_getChildWithDefaultAuthorized.<locals>.cbFinished  s     W__t/@/@.ABr%   )	r   r   r   r)   r   r   r   r   r   r   s   `   @r$   "test_getChildWithDefaultAuthorizedz6HTTPAuthHeaderTests.test_getChildWithDefaultAuthorized  sy     	  ''(>}(MN""DNN#34**73  "	C 	
j!ur%   c                      j                   j                  t        d              j                  g        j	                        }j                         } fd}|j                  |       j                  |       |S )a   
        Resource traversal which terminates at an L{HTTPAuthSessionWrapper}
        and includes correct authentication headers results in the
        L{IResource} avatar (not one of its children) retrieved from the
        portal being rendered.
        r   c                 T    j                  j                  j                  g       y r   )rk   r   r   r   s    r$   r   z=HTTPAuthHeaderTests.test_renderAuthorized.<locals>.cbFinished  s     W__t/A/A.BCr%   )r   r   r   r)   r   r   r   r   r   s   `   @r$   test_renderAuthorizedz)HTTPAuthHeaderTests.test_renderAuthorized  sr     	  ''(>}(MN""2&**73  "	D 	
j!ur%   c                 ^    t        t               G d d             } |        j                  j                          j	                   j
                  g      t         j                        }j                         } fd}|j                  |       j                  |       |S )z
        When L{HTTPAuthSessionWrapper} finds an L{ICredentialFactory} to issue
        a challenge, it calls the C{getChallenge} method with the request as an
        argument.
        c                       e Zd ZdZd Zd Zy)UHTTPAuthHeaderTests.test_getChallengeCalledWithRequest.<locals>.DumbCredentialFactorys   dumbc                     g | _         y r   )requestsr/   s    r$   r   z^HTTPAuthHeaderTests.test_getChallengeCalledWithRequest.<locals>.DumbCredentialFactory.__init__  s	     "r%   c                 <    | j                   j                  |       i S r   )r   r   r   s     r$   r~   zbHTTPAuthHeaderTests.test_getChallengeCalledWithRequest.<locals>.DumbCredentialFactory.getChallenge  s    $$W-	r%   N)rT   rU   rV   schemer   r~   rX   r%   r$   DumbCredentialFactoryr     s    F#r%   r   c                 @    j                  j                  g       y r   )rk   r   )r   factoryr*   r0   s    r$   r   zJHTTPAuthHeaderTests.test_getChallengeCalledWithRequest.<locals>.cbFinished  s    W--y9r%   )r   r   r   r   r)   r   r   r   r   r   r   )r0   r   r   r   r   r   r*   s   `    @@r$   "test_getChallengeCalledWithRequestz6HTTPAuthHeaderTests.test_getChallengeCalledWithRequest  s     
'	(	 	 
)	 ()  ''0""DNN#34"4<<9  "	: 	
j!ur%   c                    | j                   j                  t        d              G d dt              }| j                  j                  | j                   |              | j                  | j                  g      }| j                  |      }|j                  |       | j                  | j                  j                  d       |S )a  
        Issue a request for an authentication-protected resource using valid
        credentials and then return the C{DummyRequest} instance which was
        used.

        This is a helper for tests about the behavior of the logout
        callback.
        r   c                       e Zd Zd Zy)7HTTPAuthHeaderTests._logoutTest.<locals>.SlowerResourcec                     t         S r   r   r   s     r$   r   z>HTTPAuthHeaderTests._logoutTest.<locals>.SlowerResource.render  s    ##r%   N)rT   rU   rV   r   rX   r%   r$   SlowerResourcer     s    $r%   r   r   )r   r   r   r   r   r   r   r)   r   r   rk   r+   r   )r0   r   r*   r   s       r$   _logoutTestzHTTPAuthHeaderTests._logoutTest  s     	  ''(>}(MN	$X 	$ 	T^^^-=>""DNN#34**73u--q1r%   c                     | j                         }|j                          | j                  | j                  j                  d       y)zX
        The realm's logout callback is invoked after the resource is rendered.
        r   N)r   finishrk   r+   r   r   s     r$   test_logoutzHTTPAuthHeaderTests.test_logout  s6     ""$--q1r%   c                     | j                         }|j                  t        t        d                   | j	                  | j
                  j                  d       y)z
        The realm's logout callback is also invoked if there is an error
        generating the response (for example, if the client disconnects
        early).
        zSimulated disconnectr   N)r   processingFailedr   r   rk   r+   r   r   s     r$   test_logoutOnErrorz&HTTPAuthHeaderTests.test_logoutOnError  sE     ""$  8N)O!PQ--q1r%   c                    | j                   j                  t        d             | j                  | j                  g      }|j
                  j                  dd       t        | j                  |      }| j                  |t               y)z
        Resource traversal which enouncters an L{HTTPAuthSessionWrapper}
        results in an L{UnauthorizedResource} when the request has a I{Basic
        Authorization} header which cannot be decoded using base64.
        r   r   s   Basic decode should failN)r   r   r   r)   r   r   r   r   r   assertIsInstancer   )r0   r*   r   s      r$   test_decodeRaisesz%HTTPAuthHeaderTests.test_decodeRaises  sr     	  ''(>}(MN""DNN#34++9	
 #4<<9e%9:r%   c                    d}| j                  | j                  j                  |      d       t        d      }| j                  j                  |       | j                  | j                  j                  |      |df       y)z
        L{HTTPAuthSessionWrapper._selectParseHeader} returns a two-tuple giving
        the L{ICredentialFactory} to use to parse the header and a string
        containing the portion of the header which remains to be parsed.
        s   Basic abcdef123456)NNr   s   abcdef123456N)rk   r   _selectParseHeaderr   r   r   )r0   basicAuthorizationr   s      r$   test_selectParseResponsez,HTTPAuthHeaderTests.test_selectParseResponse$  sv     3LL++,>?	
 )7  ''0LL++,>?o&	
r%   c                 t   t        j                  | t              } G d dt               G fdd      }| j                  j                   |              | j                  | j                  g      }|j                  j                  dd       t        | j                  |      }|j                  |       | j                  |j                  d       | j                  dt!        |             | j#                  |d	   d
   j$                         | j                  t!        | j'                              d       y)z
        Any unexpected exception raised by the credential factory's C{decode}
        method results in a 500 response code and causes the exception to be
        logged.
        c                       e Zd Zy)KHTTPAuthHeaderTests.test_unexpectedDecodeError.<locals>.UnexpectedExceptionNrT   rU   rV   rX   r%   r$   UnexpectedExceptionr  =      r%   r  c                   "    e Zd ZdZd Z fdZy)BHTTPAuthHeaderTests.test_unexpectedDecodeError.<locals>.BadFactorys   badc                     i S r   rX   )r0   r_   s     r$   r~   zOHTTPAuthHeaderTests.test_unexpectedDecodeError.<locals>.BadFactory.getChallengeC  s    	r%   c                             r   rX   )r0   rD   r*   r  s      r$   r?   zIHTTPAuthHeaderTests.test_unexpectedDecodeError.<locals>.BadFactory.decodeF      )++r%   N)rT   rU   rV   r   r~   r?   r  s   r$   
BadFactoryr  @  s    F,r%   r  r   s   Bad abc  r   r   log_failureN)r   createWithCleanupr   	Exceptionr   r   r)   r   r   r   r   r   r   rk   r   assertEqualslenr  valueflushLoggedErrors)r0   logObserverr  r*   r   r  s        @r$   test_unexpectedDecodeErrorz.HTTPAuthHeaderTests.test_unexpectedDecodeError5  s     +<<TCUV	) 		, 	, 	  ''
5""DNN#34++,<jI"4<<9u--s3!S-.k!n];AACVWT334GHI1Mr%   c                 z   t        j                  | t              } G d dt               G fdd      }| j                  j                   |              | j                  j                  t        d             | j                  | j                  g      }| j                  |      }|j                  |       | j                  |j                  d       | j                  dt!        |             | j#                  |d   d	   j$                         | j                  t!        | j'                              d       y
)z
        Any unexpected failure from L{Portal.login} results in a 500 response
        code and causes the failure to be logged.
        c                       e Zd Zy)JHTTPAuthHeaderTests.test_unexpectedLoginError.<locals>.UnexpectedExceptionNr  rX   r%   r$   r  r"  Z  r  r%   r  c                       e Zd ZefZ fdZy)DHTTPAuthHeaderTests.test_unexpectedLoginError.<locals>.BrokenCheckerc                             r   rX   )r0   credentialsr  s     r$   requestAvatarIdzTHTTPAuthHeaderTests.test_unexpectedLoginError.<locals>.BrokenChecker.requestAvatarId`  r  r%   N)rT   rU   rV   r
   credentialInterfacesr'  r  s   r$   BrokenCheckerr$  ]  s    $5#7 ,r%   r)  r   r  r   r   r  N)r   r  r   r  r   registerCheckerr   r   r   r)   r   r   r   rk   r   r  r  r  r  r  )r0   r  r)  r*   r   r  s        @r$   test_unexpectedLoginErrorz-HTTPAuthHeaderTests.test_unexpectedLoginErrorS  s    
 +<<TCUV	) 		, 	, 	##MO4  ''(>}(MN""DNN#34**73u--s3!S-.k!n];AACVWT334GHI1Mr%   c                     dt                j                  t        <    j                  t           j                   j                  t        d              j                  j                  t                       j                  j                  t        d              j                   j                  g      t         j                        }j                         } fd}|j!                  |       j#                  |       |S )zl
        Anonymous requests are allowed if a L{Portal} has an anonymous checker
        registered.
        s*   contents of the unprotected child resourcer   r   c                 @    j                  j                  g       y r   )rk   r   )r   r*   r0   unprotectedContentss    r$   r   z<HTTPAuthHeaderTests.test_anonymousAccess.<locals>.cbFinished  s    W__/B.CDr%   )r   r   r   r   r   r   r   r*  r   r   r   r   r)   r   r   r   r   r   )r0   r   r   r   r*   r.  s   `   @@r$   test_anonymousAccessz(HTTPAuthHeaderTests.test_anonymousAccessm  s    
 L"**YY((NND!4lC	
 	##$8$:;  ''(>}(MN""DNN#34"4<<9  "	E 	
j!ur%   N)rT   rU   rV   rW   r   r)   r1   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r+  r/  rX   r%   r$   r   r   D  sm     KU&9"(Q	
C&(>,22;
"N<N4r%   r   )9rW   r    zope.interfacer   zope.interface.verifyr   twisted.credr   r   twisted.cred.checkersr   r   r	   twisted.cred.credentialsr
   twisted.internet.addressr   twisted.internet.errorr   twisted.internet.testingr   twisted.loggerr   twisted.python.failurer   twisted.trialr   twisted.web._authr   r   twisted.web._auth.basicr   twisted.web._auth.wrapperr   r   twisted.web.iwebr   twisted.web.resourcer   r   r   twisted.web.serverr   twisted.web.staticr   twisted.web.test.test_webr   r!   r'   rZ   TestCasera   rc   r   IRealmr   r   rX   r%   r$   <module>rE     s   
  & . & 
 7 0 1 9 - * " + : R / H H + # 2'M
 M
` \#68I8I H+lH$5$5 H+VJ.h.?.? J.Z FMM  8@(++ @r%   