
    tf\                     &   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j2                  Z3 G d de1ej2                  Z4 G d d e1ej2                  Z5 eej6                    G d! d"          Z7 G d# d$ej2                  Z8dS )%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                 N    t          j        |                                           S N)base64	b64encodestrip)ss    ]/var/www/surfInsights/venv3-11/lib/python3.11/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
dS )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                     |                                  | _        d| _        d| _        d| _        t          j        | j                  | _        d S )Ns   foos   dreids   S3CuR1Ty)makeRequestrequestrealmusernamepasswordr   r   credentialFactoryselfs    r$   setUpzBasicAuthTestsMixin.setUp0   sE    ''))
 #!&!=dj!I!Ir%      GETNc                 0    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^"U"U"UVVVr%   c                 `    |                      t          t          | j                             dS )zM
        L{BasicCredentialFactory} implements L{ICredentialFactory}.
        N
assertTruer   r   r.   r/   s    r$   test_interfacez"BasicAuthTestsMixin.test_interface?   *     	%79OPPQQQQQr%   c                    t          d                    | j        d| j        g                    }| j                            || j                  }|                     t          j	        |                     |                     |
                    | j                             |                     |
                    | j        dz                        dS )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*   r:   r
   
providedBycheckPasswordassertFalser0   responsecredss      r$   test_usernamePasswordz)BasicAuthTestsMixin.test_usernamePasswordE   s     SXXt}dDM&JKKLL&--hEE)4U;;<<<++DM::;;;,,T]X-EFFGGGGGr%   c                 x   t          d                    | j        d| j        g                    }|                    d          }| j                            || j                  }|                     t          t          |                     |                     |                    | j                             dS )zz
        L{basic.BasicCredentialFactory.decode} decodes a base64-encoded
        response with incorrect padding.
        r%   r>      =N)r!   r?   r,   r-   r"   r.   r@   r*   r:   r   r
   rB   rD   s      r$   test_incorrectPaddingz)BasicAuthTestsMixin.test_incorrectPaddingR   s    
 SXXt}dDM&JKKLL>>$''&--hEE%6>>???++DM::;;;;;r%   c                     d}|                      t          j        | j        j        ||                                            dS )z
        L{basic.BasicCredentialFactory.decode} raises L{LoginFailed} if passed
        a response which is not base64-encoded.
           xN)assertRaisesr   LoginFailedr.   r@   r)   r0   rE   s     r$   test_invalidEncodingz(BasicAuthTestsMixin.test_invalidEncoding^   sL    
 ")		
 	
 	
 	
 	
r%   c                     t          d          }|                     t          j        | j        j        ||                                            dS )z
        L{basic.BasicCredentialFactory.decode} raises L{LoginFailed} when
        passed a response which is not valid base64-encoded text.
        s   123abc+/N)r!   rM   r   rN   r.   r@   r)   rO   s     r$   test_invalidCredentialsz+BasicAuthTestsMixin.test_invalidCredentialsk   sT    
 [))")		
 	
 	
 	
 	
r%   r2   N)__name__
__module____qualname____doc__r1   r)   r;   rG   rJ   rP   rR    r%   r$   r'   r'   )   s         J J JW W W WR R RH H H
< 
< 
<
 
 

 
 
 
 
r%   r'   c                       e Zd ZddZdS )RequestMixinr2   Nc                 f    |t          ddd          }t          d          }||_        ||_        |S )zo
        Create a L{DummyRequest} (change me to create a
        L{twisted.web.http.Request} instead).
        NTCP	localhosti     /)r   r   r6   client)r0   r6   r7   r*   s       r$   r)   zRequestMixin.makeRequestz   s;    
  '{DAAMt$$&r%   rS   )rT   rU   rV   r)   rX   r%   r$   rZ   rZ   y   s(        
 
 
 
 
 
r%   rZ   c                       e Zd ZdZdS )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                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )DigestAuthTestszL
    Digest authentication tests which use L{twisted.web.http.Request}.
    c                     d| _         d| _        t          j        | j        | j                   | _        |                                 | _        dS )z>
        Create a DigestCredentialFactory for testing
        
   test realm   md5N)r+   	algorithmr   DigestCredentialFactoryr.   r)   r*   r/   s    r$   r1   zDigestAuthTests.setUp   sI     #
!'!?NDJ"
 "
 ''))r%   c                 D    dddgt                       fd}                      j        j        d|                                t          dd                    } j                            |                                d                    d	S )
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.1r2   Fc                                          |                                 |                                |           dd<   d S )NTr   )assertEqual)	_response_method_hostdonehostr6   rE   r0   s      r$   checkz*DigestAuthTests.test_decode.<locals>.check   sR    Xy111VW---T5)))DGGGr%   r@   r\   Q   r   N)objectpatchr.   r   r)   r   r@   r:   )r0   rq   reqro   rp   r6   rE   s   `  @@@@r$   test_decodezDigestAuthTests.test_decode   s     w88	 	 	 	 	 	 	 	 	 	

4)0(EBBBv{5$'C'CDD%%h444Q     r%   c                 `    |                      t          t          | j                             dS )zN
        L{DigestCredentialFactory} implements L{ICredentialFactory}.
        Nr9   r/   s    r$   r;   zDigestAuthTests.test_interface   r<   r%   c                    | j                             | j                  }|                     |d         d           |                     |d         d           |                     |d         d           |                     d|           |                     d|           |                                D ]}|                     d	|           d
S )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E	5)73337+];;;;/888gy)))h	***!!## 	' 	'AUA&&&&	' 	'r%   c                 f   |                      dd          }| j                            |          }|                     |d         d           |                     |d         d           |                     |d         d           |                     d	|           |                     d
|           dS )z
        L{DigestCredentialFactory.getChallenge} can issue a challenge even if
        the L{Request} it is passed returns L{None} from C{getClientIP}.
        r2   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0*77@@	5)73337+];;;;/888gy)))h	*****r%   N)	rT   rU   rV   rW   r1   rv   r;   r   r   rX   r%   r$   rc   rc      sl         	* 	* 	*! ! !,R R R' ' '"+ + + + +r%   rc   c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	UnauthorizedResourceTestsz,
    Tests for L{UnauthorizedResource}.
    c                     t          g           }|                     |                    dd          |           |                     |                    dd          |           dS )zF
        An L{UnauthorizedResource} is every child of itself.
        fooNbar)r   assertIdenticalgetChildWithDefault)r0   resources     r$   test_getChildWithDefaultz2UnauthorizedResourceTests.test_getChildWithDefault   sc     (++X99%FFQQQX99%FFQQQQQr%   c                     t          t          d          g          }|                    |           |                     |j        d           |                     |j                            d          dg           dS )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   s     ()?)N)N(OPPx   -s333#112EFF)*	
 	
 	
 	
 	
r%   c                     |                                  }|                     |           |                     dd                    |j                             dS )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   sS     ""$$$$W---#((7?*C*CDDDDDr%   c                     |                      d          }|                     |           |                     dd                    |j                             dS )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)r6   r%   Nr   r   s     r$   test_renderHEADz)UnauthorizedResourceTests.test_renderHEAD   sX     ""'"22$$W---chhw7788888r%   c                     t          t          d          g          }|                                 }|                    |           |                     |j                            d          dg           dS )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  s}     ()?)P)P(QRR""$$x   #112EFF/0	
 	
 	
 	
 	
r%   c                 6   t          t          j        dd          g          }|                                 }|                    |           |j                            d          d         }|                     d|           |                     d|           dS )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  s     (+F4DEEF
 
 ""$$x   ,::;NOOPQR
2J???j*-----r%   N)
rT   rU   rV   rW   r   r   r   r   r   r   rX   r%   r$   r   r      s~         R R R
 
 
E E E9 9 9
 
 
. . . . .r%   r   c                   $    e Zd ZdZd Zd Zd ZdS )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                 0    d| _         d| _        || _        d S )Nr   )	loggedOutloggedInavatarFactory)r0   r   s     r$   __init__zRealm.__init__5  s    *r%   c                     t           |v r2| xj        dz  c_        t           |                     |          | j        fS t	                      N   )r   r   r   logoutr4   )r0   avatarIdmind
interfacess       r$   requestAvatarzRealm.requestAvatar:  sF    
""MMQMMd00::DKGG!###r%   c                 &    | xj         dz  c_         d S r   )r   r/   s    r$   r   zRealm.logout@  s    !r%   N)rT   rU   rV   rW   r   r   r   rX   r%   r$   r   r   (  sK        
 
+ + +
$ $ $    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dS )HTTPAuthHeaderTestsz.
    Tests for L{HTTPAuthSessionWrapper}.
    c                 N   d| _         d| _        d| _        d| _        d| _        t                      | _        | j                            | j         | j                   t          | j        d          | _	        | j	        
                    | j        t          | j        d                     | j         | j	        i| _        t          | j        j                  | _        t          j        | j        | j        g          | _        g | _        t%          | j        | j                  | _        dS )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]DM:::4-|<<T^T$2C\-R-RSSSt{34<+,,
mDJ??#% -dk4;STTr%   c                     t          | j        dz   | j        z             }|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^  sP     "$-$"6"FGG++,<i->WXXX!$,888r%   c                                             j        g          t           j                  }                                } fd}|                    |                               |           |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        d           d S Nr   rk   r   resultr*   r0   s    r$   
cbFinishedz@HTTPAuthHeaderTests.test_getChildWithDefault.<locals>.cbFinishedr  "    W1377777r%   )r)   r   r   r   notifyFinishaddCallbackr   r0   childdr   r*   s   `   @r$   r   z,HTTPAuthHeaderTests.test_getChildWithDefaulth  s     ""DN#344"4<99  ""	8 	8 	8 	8 	8 	8 	
j!!!ur%   c                 v     j                             t          d                                           j        g          j                            d|           t           j                  }	                                } fd}|
                    |                               |           |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        d           d S r   r   r   s    r$   r   zAHTTPAuthHeaderTests._invalidAuthorizationTest.<locals>.cbFinished  r   r%   )r   appendr   r)   r   r   r   r   r   r   r   r   )r0   rE   r   r   r   r*   s   `    @r$   _invalidAuthorizationTestz-HTTPAuthHeaderTests._invalidAuthorizationTesty  s     	 ''(>}(M(MNNN""DN#344++,<hGGG"4<99  ""	8 	8 	8 	8 	8 	8 	
j!!!ur%   c                 L    |                      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.OPPPr%   c                 \    |                      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  s3     --	$-'"9:::
 
 	
r%   c                 ,    |                      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BBr%   c                 @     j                             t          d                                           j        g                                         }                                } fd}|                    |                               |           |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                 J                         j        j        g           d S r   )rk   r   r   ignoredr*   r0   s    r$   r   zJHTTPAuthHeaderTests.test_getChildWithDefaultAuthorized.<locals>.cbFinished  s'    W_t/@.ABBBBBr%   )	r   r   r   r)   r   r   r   r   r   r   s   `   @r$   "test_getChildWithDefaultAuthorizedz6HTTPAuthHeaderTests.test_getChildWithDefaultAuthorized  s     	 ''(>}(M(MNNN""DN#344**733  ""	C 	C 	C 	C 	C 	C 	
j!!!ur%   c                 4     j                             t          d                                          g                                          }                                } fd}|                    |                               |           |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                 J                         j        j        g           d S r   )rk   r   r   r   s    r$   r   z=HTTPAuthHeaderTests.test_renderAuthorized.<locals>.cbFinished  s'    W_t/A.BCCCCCr%   )r   r   r   r)   r   r   r   r   r   s   `   @r$   test_renderAuthorizedz)HTTPAuthHeaderTests.test_renderAuthorized  s     	 ''(>}(M(MNNN""2&&**733  ""	D 	D 	D 	D 	D 	D 	
j!!!ur%   c                     t          t                     G d d                      } |             j                                                             j        g          t           j                  }                                } fd}|	                    |           
                    |           |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dS )UHTTPAuthHeaderTests.test_getChallengeCalledWithRequest.<locals>.DumbCredentialFactorys   dumbc                     g | _         d S r   )requestsr/   s    r$   r   z^HTTPAuthHeaderTests.test_getChallengeCalledWithRequest.<locals>.DumbCredentialFactory.__init__  s     "r%   c                 :    | 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     s7        F# # #    r%   r   c                 @                         j        g           d S r   )rk   r   )r   factoryr*   r0   s    r$   r   zJHTTPAuthHeaderTests.test_getChallengeCalledWithRequest.<locals>.cbFinished  s$    W-y9999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00""DN#344"4<99  ""	: 	: 	: 	: 	: 	: 	: 	
j!!!ur%   c                    | j                             t          d                      G d dt                    }| j                            | j         |                       |                     | j        g          }|                     |          }|	                    |           | 
                    | 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dS )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(MNNN	$ 	$ 	$ 	$ 	$X 	$ 	$ 	$ 	T^^^-=-=>>>""DN#344**733u-q111r%   c                     |                                  }|                                 |                     | j        j        d           dS )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  sF     ""$$-q11111r%   c                     |                                  }|                    t          t          d                               |                     | j        j        d           dS )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  s[     ""$$  8N)O)O!P!PQQQ-q11111r%   c                     | j                             t          d                     |                     | j        g          }|j                            dd           t          | j        |          }| 	                    |t                     dS )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  s     	 ''(>}(M(MNNN""DN#344++9	
 	
 	
 #4<99e%9:::::r%   c                    d}|                      | j                            |          d           t          d          }| j                            |           |                      | j                            |          |df           dS )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$  s     3L++,>??	
 	
 	
 )77 ''000L++,>??o&	
 	
 	
 	
 	
r%   c                    t          j        | t                    } G d dt                     G fdd          }| j                             |                       |                     | j        g          }|j        	                    dd           t          | j        |          }|                    |           |                     |j        d           |                     dt!          |                     |                     |d	         d
         j                   |                     t!          |                                         d           dS )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dS )KHTTPAuthHeaderTests.test_unexpectedDecodeError.<locals>.UnexpectedExceptionNrT   rU   rV   rX   r%   r$   UnexpectedExceptionr  =          Dr%   r  c                   $    e Zd ZdZd Z fdZdS )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   rE   r*   r  s      r$   r@   zIHTTPAuthHeaderTests.test_unexpectedDecodeError.<locals>.BadFactory.decodeF      ))+++r%   N)rT   rU   rV   r   r~   r@   r  s   r$   
BadFactoryr  @  sB        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  su    +<TCUVV	 	 	 	 	) 	 	 		, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	 ''

555""DN#344++,<jIII"4<99u-s333!S--...k!n];ACVWWWT334GHHII1MMMMMr%   c                    t          j        | t                    } G d dt                     G fdd          }| j                             |                       | j                            t          d                     | 	                    | j
        g          }|                     |          }|                    |           |                     |j        d           |                     dt!          |                     |                     |d         d	         j                   |                     t!          |                                         d           d
S )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dS )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dS )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$  ]  s7        $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V	 	 	 	 	) 	 	 		, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	##MMOO444 ''(>}(M(MNNN""DN#344**733u-s333!S--...k!n];ACVWWWT334GHHII1MMMMMr%   c                 <    dt                       j        t          <    j        t                                        j        t          d                      j                            t                                  j	        
                    t          d                                           j        g          t           j                  }                                } fd}|                    |                               |           |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        g           d S r   )rk   r   )r   r*   r0   unprotectedContentss    r$   r   z<HTTPAuthHeaderTests.test_anonymousAccess.<locals>.cbFinished  s%    W_/B.CDDDD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((ND!4lCC	
 	
 	
 	##$8$:$:;;; ''(>}(M(MNNN""DN#344"4<99  ""	E 	E 	E 	E 	E 	E 	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  sB         KU U U&9 9 9  "  (Q Q Q	
 	
 	
C C C  &  (  >  ,2 2 22 2 2; ; ;
 
 
"N N N<N 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     s7   
  & & & & & & . . . . . . & & & & & & & &         
 7 6 6 6 6 6 0 0 0 0 0 0 1 1 1 1 1 1 9 9 9 9 9 9 - - - - - - * * * * * * " " " " " " + + + + + + + + : : : : : : R R R R R R R R / / / / / / H H H H H H H H H H + + + + + + # # # # # # 2 2 2 2 2 2' ' 'M
 M
 M
 M
 M
 M
 M
 M
`           \#68I   H+ H+ H+ H+ H+lH$5 H+ H+ H+VJ. J. J. J. J.h.? J. J. J.Z FM          8@ @ @ @ @(+ @ @ @ @ @r%   