o
    ¯bQ,  ã                   @   s¨   d Z ddlmZ ddlmZ ddlmZ dZdZdZ	dZ
G d	d
„ d
ejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZdS )z4
Tests for L{twisted.words.protocols.jabber.error}.
é    )Úunittest)Úerror)Údomishz$http://www.w3.org/XML/1998/namespaceú http://etherx.jabber.org/streamsz#urn:ietf:params:xml:ns:xmpp-streamsz#urn:ietf:params:xml:ns:xmpp-stanzasc                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚBaseErrorTestsc                 C   s6   t  d¡}| ¡ }|  |jd¡ |  t|jƒd¡ dS )z<
        Test getting an element for a plain error.
        úfeature-not-implementedNé   )r   Ú	BaseErrorÚ
getElementÚassertIdenticalÚuriÚassertEqualÚlenÚchildren©ÚselfÚeÚelement© r   úE/usr/lib/python3/dist-packages/twisted/words/test/test_jabbererror.pyÚtest_getElementPlain   s   
z#BaseErrorTests.test_getElementPlainc                 C   sT   t  dd¡}| ¡ }|  t|jƒd¡ |  t|jƒd¡ |  |j t	df¡d¡ dS )zC
        Test getting an element for an error with a text.
        r   Útexté   ÚlangN)
r   r	   r
   r   r   r   Ústrr   ÚgetAttributeÚNS_XMLr   r   r   r   Útest_getElementText   s
   z"BaseErrorTests.test_getElementTextc                 C   sT   t  ddd¡}| ¡ }|  t|jƒd¡ |  t|jƒd¡ |  |jtdf d¡ dS )zP
        Test getting an element for an error with a text and language.
        r   r   Úen_USr   r   N)	r   r	   r
   r   r   r   r   r   r   r   r   r   r   Útest_getElementTextLang'   s
   z&BaseErrorTests.test_getElementTextLangc                 C   sD   t  d¡}tjd|d}| ¡ }|  t|jƒd¡ |  |j|¡ dS )zV
        Test getting an element for an error with an app specific condition.
        )ÚtestnsÚmyerrorr   )ÚappConditionr   N)	r   ÚElementr   r	   r
   r   r   r   r!   )r   Úacr   r   r   r   r   Útest_getElementAppCondition1   s
   
z*BaseErrorTests.test_getElementAppConditionN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r%   r   r   r   r   r      s
    	

r   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚStreamErrorTestsc                 C   s$   t  d¡}| ¡ }|  |jt¡ dS )zK
        Test namespace of the element representation of an error.
        r   N)r   ÚStreamErrorr
   r   r   Ú
NS_STREAMSr   r   r   r   r   =   s   
z%StreamErrorTests.test_getElementPlainc                 C   ó*   t  d¡}| ¡ }|  tt|dƒj¡ dS ©zR
        Test that the error condition element has the correct namespace.
        r   N)r   r*   r
   r   ÚNS_XMPP_STREAMSÚgetattrr   r   r   r   r   Ú!test_getElementConditionNamespaceE   ó
   
ÿz2StreamErrorTests.test_getElementConditionNamespacec                 C   s(   t  dd¡}| ¡ }|  t|jj¡ dS )úM
        Test that the error text element has the correct namespace.
        r   r   N)r   r*   r
   r   r.   r   r   r   r   r   r   Útest_getElementTextNamespaceO   s   z-StreamErrorTests.test_getElementTextNamespaceN)r&   r'   r(   r   r0   r3   r   r   r   r   r)   <   s    
r)   c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚStanzaErrorTestsz)
    Tests for L{error.StreamError}.
    c                 C   s*   t  d¡}|  d|j¡ |  d|j¡ dS )zI
        Remote Server Timeout should yield type wait, code 504.
        zremote-server-timeoutÚwaitÚ504N)r   ÚStanzaErrorr   ÚtypeÚcode)r   r   r   r   r   Útest_typeRemoteServerTimeout]   s   
z-StanzaErrorTests.test_typeRemoteServerTimeoutc                 C   sD   t  d¡}| ¡ }|  |jd¡ |  |d d¡ |  |d d¡ dS )zC
        Test getting an element for a plain stanza error.
        r   Nr8   Úcancelr9   Ú501©r   r7   r
   r   r   r   r   r   r   r   e   s
   
z%StanzaErrorTests.test_getElementPlainc                 C   sF   t  dd¡}| ¡ }|  |jd¡ |  |d d¡ |  |d d¡ dS )zO
        Test getting an element for a stanza error with a given type.
        r   ÚauthNr8   r9   r<   r=   r   r   r   r   Útest_getElementTypeo   s
   z$StanzaErrorTests.test_getElementTypec                 C   r,   r-   )r   r7   r
   r   ÚNS_XMPP_STANZASr/   r   r   r   r   r   r0   y   r1   z2StanzaErrorTests.test_getElementConditionNamespacec                 C   s*   t jddd}| ¡ }|  t|jj¡ dS )r2   r   r   )r   N)r   r7   r
   r   r@   r   r   r   r   r   r   r3   ƒ   s   z-StanzaErrorTests.test_getElementTextNamespacec                 C   s®   t  d¡}d|d< d|d< d|d< t d¡}| |¡}|  ||¡ |  |d d¡ |  |d d¡ |  |d d	¡ |  |jjd
 jd¡ |  |jd d¡ |  	|j|j¡ dS )zë
        Test an error response is generated from a stanza.

        The addressing on the (new) response stanza should be reversed, an
        error child (with proper properties) added and the type set to
        C{'error'}.
        )zjabber:clientÚmessageÚchatr8   zuser1@example.comÚtozuser2@example.com/resourceÚfromúservice-unavailabler   r   r;   N)
r   r#   r   r7   Ú
toResponseÚassertNotIdenticalr   r   ÚnameÚassertNotEqual)r   Ústanzar   Úresponser   r   r   Útest_toResponse‹   s   


z StanzaErrorTests.test_toResponseN)
r&   r'   r(   Ú__doc__r:   r   r?   r0   r3   rL   r   r   r   r   r4   X   s    


r4   c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚParseErrorTestsz)
    Tests for L{error._parseError}.
    c                 C   s   t  d¡| _d S )N)Nr   )r   r#   r   )r   r   r   r   ÚsetUp§   s   zParseErrorTests.setUpc                 C   s(   t  | j d¡}|  dddddœ|¡ dS )z:
        Test parsing of the empty error element.
        ÚerrornsN)Ú	conditionr   ÚtextLangr"   )r   Ú_parseErrorr   ©r   Úresultr   r   r   Ú
test_emptyª   s
   þzParseErrorTests.test_emptyc                 C   s.   | j  d¡ t  | j d¡}|  d|d ¡ dS )zD
        Test parsing of an error element with a condition.
        )rP   úbad-requestrP   rW   rQ   N©r   Ú
addElementrS   r   rT   r   r   r   Útest_condition´   ó   zParseErrorTests.test_conditionc                 C   sH   | j  d¡}| d¡ t  | j d¡}|  d|d ¡ |  d|d ¡ dS )z?
        Test parsing of an error element with a text.
        ©rP   r   ÚtestrP   r   NrR   )r   rY   Ú
addContentrS   r   ©r   r   rU   r   r   r   Ú	test_text¼   s
   
zParseErrorTests.test_textc                 C   sD   | j  d¡}d|tdf< | d¡ t  | j d¡}|  d|d ¡ dS )zW
        Test parsing of an error element with a text with a defined language.
        r\   r   r   r]   rP   rR   N)r   rY   r   r^   rS   r   r_   r   r   r   Útest_textLangÆ   s
   
zParseErrorTests.test_textLangc                 C   s.   | j  d¡}t  | j d¡}|  ||d ¡ dS )zR
        Test parsing of an error element with an app specific condition.
        ©r    rQ   rP   r"   NrX   ©r   rQ   rU   r   r   r   Útest_appConditionÐ   r[   z!ParseErrorTests.test_appConditionc                 C   s:   | j  d¡ | j  d¡}t  | j d¡}|  ||d ¡ dS )zY
        Test parsing of an error element with multiple app specific conditions.
        rb   )r    Ú
condition2rP   r"   NrX   rc   r   r   r   Útest_appConditionMultipleØ   s   z)ParseErrorTests.test_appConditionMultipleN)r&   r'   r(   rM   rO   rV   rZ   r`   ra   rd   rf   r   r   r   r   rN   ¢   s    


rN   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚExceptionFromStanzaTestsc                 C   s¦   t  d¡}| d¡}| d¡ | d¡}d|d< | tdf¡ | d¡}d	|d
< t |¡}|  |tj¡ |  d|j	¡ |  d|j
¡ |  ||j¡ |  |g|j¡ dS )aí  
        Test basic operations of exceptionFromStanza.

        Given a realistic stanza, check if a sane exception is returned.

        Using this stanza::

          <iq type='error'
              from='pubsub.shakespeare.lit'
              to='francisco@denmark.lit/barracks'
              id='subscriptions1'>
            <pubsub xmlns='http://jabber.org/protocol/pubsub'>
              <subscriptions/>
            </pubsub>
            <error type='cancel'>
              <feature-not-implemented
                xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
              <unsupported xmlns='http://jabber.org/protocol/pubsub#errors'
                           feature='retrieve-subscriptions'/>
            </error>
          </iq>
        ©NrJ   )z!http://jabber.org/protocol/pubsubÚpubsubÚsubscriptionsr   r;   r8   r   )z(http://jabber.org/protocol/pubsub#errorsÚunsupportedzretrieve-subscriptionsÚfeatureN)r   r#   rY   r@   r   ÚexceptionFromStanzaÚassertIsInstancer7   r   rQ   r8   r"   r   )r   rJ   Úpr   ÚucrU   r   r   r   Ú
test_basicã   s   





z#ExceptionFromStanzaTests.test_basicc                 C   s„   t  d¡}|jddd}|jddd}d|d< t |¡}|  |tj¡ |  d	|j¡ |  d
|j	¡ |  d|j
¡ |  |g|j¡ dS )aÙ  
        Test legacy operations of exceptionFromStanza.

        Given a realistic stanza with only legacy (pre-XMPP) error information,
        check if a sane exception is returned.

        Using this stanza::

          <message type='error'
                   to='piers@pipetree.com/Home'
                   from='qmacro@jaber.org'>
            <body>Are you there?</body>
            <error code='502'>Unable to resolve hostname.</error>
          </message>
        rh   ÚbodyzAre you there?)Úcontentr   zUnable to resolve hostname.Ú502r9   rE   r5   N)r   r#   rY   r   rm   rn   r7   r   rQ   r8   r   r   )r   rJ   ro   r   rU   r   r   r   Útest_legacy  s   

z$ExceptionFromStanzaTests.test_legacyN)r&   r'   r(   rq   ru   r   r   r   r   rg   â   s    (rg   c                   @   s   e Zd Zdd„ ZdS )ÚExceptionFromStreamErrorTestsc                 C   sB   t  d¡}| tdf¡ t |¡}|  |tj¡ |  d|j	¡ dS )ac  
        Test basic operations of exceptionFromStreamError.

        Given a realistic stream error, check if a sane exception is returned.

        Using this error::

          <stream:error xmlns:stream='http://etherx.jabber.org/streams'>
            <xml-not-well-formed xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>
          </stream:error>
        )r   r   zxml-not-well-formedN)
r   r#   rY   r.   r   ÚexceptionFromStreamErrorrn   r*   r   rQ   )r   r   rU   r   r   r   rq   )  s
   

z(ExceptionFromStreamErrorTests.test_basicN)r&   r'   r(   rq   r   r   r   r   rv   (  s    rv   N)rM   Útwisted.trialr   Útwisted.words.protocols.jabberr   Útwisted.words.xishr   r   r+   r.   r@   ÚTestCaser   r)   r4   rN   rg   rv   r   r   r   r   Ú<module>   s   )J@F