o
    ¯b:'  ã                   @   s„   d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZ G dd„ deƒZG d	d
„ d
eƒZdZG dd„ deƒZdS )z5
Tests for IRC portions of L{twisted.words.service}.
é    )ÚcheckersÚportal)Úproto_helpers)Úirc)ÚInMemoryWordsRealmÚ
IRCFactoryÚIRCUser)ÚIRCTestCasec                   @   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d„ Z
dd„ Zdd„ Zdd„ ZdS )ÚIRCUserTestsz'
    Isolated tests for L{IRCUser}
    c                 C   sp   t dƒ| _t ¡ | _t | j| jg¡| _| j dd¡ t| j| jƒ| _	| j	 
d¡| _t ¡ | _| j | j¡ dS )zm
        Sets up a Realm, Portal, Factory, IRCUser, Transport, and Connection
        for our tests.
        úexample.comÚjohnÚpassN)r   Úrealmr   Ú'InMemoryUsernamePasswordDatabaseDontUseÚcheckerr   ÚPortalÚaddUserr   ÚfactoryÚbuildProtocolÚircUserr   ÚStringTransportÚstringTransportÚmakeConnection©Úself© r   úE/usr/lib/python3/dist-packages/twisted/words/test/test_irc_service.pyÚsetUp   s   


zIRCUserTests.setUpc                 C   s<   | j  ddg¡ | j ¡  | j  d¡ |  | j ¡ d¡ dS )z�
        Sending a message to a user after they have sent NICK, but before they
        have authenticated, results in a message from "example.com".
        Ú ÚmynickÚfooz:example.com foo mynick
N)r   Úirc_NICKr   ÚclearÚsendMessageÚassertEqualBufferValueÚvaluer   r   r   r   Útest_sendMessage"   s   

ÿzIRCUserTests.test_sendMessagec                 C   sL   d  ¡ }| j dd  ¡ g¡ | j ¡  | j d  ¡ ¡ |  | j ¡ |¡ dS )zÁ
        When a UTF8 message is sent with sendMessage and the current IRCUser
        has a UTF8 nick and is set to UTF8 encoding, the message will be
        written to the transport.
        u   :example.com Ñ‚ÐµÑ�Ñ‚ Ð½Ð¸Ðº
r   u   Ð½Ð¸Ðºu   Ñ‚ÐµÑ�Ñ‚N)Úencoder   r!   r   r"   r#   r$   r%   )r   ÚexpectedResultr   r   r   Útest_utf8Messages.   s
   
zIRCUserTests.test_utf8Messagesc                 C   s   | j  ddg¡ |  t¡ dS )zÖ
        A NICK command sent with a nickname that cannot be decoded with the
        current IRCUser's encoding results in a PRIVMSG from NickServ
        indicating that the nickname could not be decoded.
        r   s   ÔÅÓÔN)r   r!   ÚassertRaisesÚUnicodeErrorr   r   r   r   Útest_invalidEncodingNick;   s   z%IRCUserTests.test_invalidEncodingNickc                 C   sJ   | j j ¡ }| j j ¡  ttkrt|tƒr| d¡}| ¡ }dd„ |D ƒS )zC
        Grabs our responses and then clears the transport
        zutf-8c                 S   s   g | ]}t  |¡‘qS r   )r   Úparsemsg)Ú.0Úrr   r   r   Ú
<listcomp>M   s    z)IRCUserTests.response.<locals>.<listcomp>)	r   Ú	transportr%   r"   ÚbytesÚstrÚ
isinstanceÚdecodeÚ
splitlines©r   Úresponser   r   r   r8   D   s   
zIRCUserTests.responsec                 c   s.   � t |ƒD ]\}}|d |kr||fV  qdS )a8  
        Gets messages out of a response

        @param response: The parsed IRC messages of the response, as returned
        by L{IRCUserTests.response}

        @param messageType: The string type of the desired messages.

        @return: An iterator which yields 2-tuples of C{(index, ircMessage)}
        é   N)Ú	enumerate)r   r8   ÚmessageTypeÚnÚmessager   r   r   ÚscanResponseO   s   €
€þzIRCUserTests.scanResponsec                 C   sx   | j  ddg¡ |  ¡ }t|  |tj¡ƒ}t|  |tj¡ƒ}|  |dddddgffg¡ |  |ddddd	gffg¡ d
S )zð
        Receiving NICK without authenticating sends the MOTD Start and MOTD End
        messages, which is required by certain popular IRC clients (such as
        Pidgin) before a connection is considered to be fully established.
        r   r   r   r   Ú375ú#- example.com Message of the Day - r9   Ú376úEnd of /MOTD command.N)	r   r!   r8   Úlistr>   r   ÚRPL_MOTDSTARTÚRPL_ENDOFMOTDÚassertEqual)r   r8   ÚstartÚendr   r   r   Útest_sendNickSendsGreeting^   s"   ýþÿþÿz'IRCUserTests.test_sendNickSendsGreetingc                 C   s¾   | j  ddg¡ | j  ddg¡ | j  ddg¡ d | jjd ¡}d | jjd ¡}|  |  ¡ d	d
ddgfd	dddgfd	dddgfd	dd|gfd	dd|gfd	ddd	| jjd ddgfg¡ dS )zz
        Receiving USER, PASS, NICK will log in the user, and transmit the
        appropriate response messages.
        r   zjohn doer   r   z,Your host is example.com, running version {}ÚserviceVersionzThis server was created on {}ÚcreationDater   r?   r@   rA   rB   Ú001zconnected to Twisted IRCÚ002Ú003Ú004Úwr<   N)	r   Úirc_USERÚirc_PASSr!   Úformatr   Ú_serverInforF   r8   )r   ÚversionÚcreationr   r   r   Útest_fullLoginy   s6   
ÿ
ÿ
ûýúþzIRCUserTests.test_fullLoginc                 C   sð   | j  ddg¡ |  ¡ }| j j ¡  |  |d d tj¡ | j  ddg¡ |  ¡ }| j j ¡  |  |d d tj	¡ | j  
dddg¡ |  ¡ }| j j ¡  |  |d d tj¡ | j  
dddg¡ |  ¡ }| j j ¡  |  |d d tj¡ d	S )
z
        irc_PART
        Útestuserr   r   r9   Úsomechannels   somechannels   boogaÚboogaN)r   r!   r8   r1   r"   rF   r   rD   Úirc_JOINÚERR_NOSUCHCHANNELÚirc_PARTÚERR_NOTONCHANNELr7   r   r   r   Ú	test_PART    s    zIRCUserTests.test_PARTc                 C   s^   | j  ddg¡ | j  ddg¡ | j j ¡  | j  ddg¡ |  ¡ }|  |d d tj	¡ dS )z
        irc_NAMES
        r   rX   rY   r   r9   N)
r   r!   r[   r1   r"   Ú	irc_NAMESr8   rF   r   ÚRPL_ENDOFNAMESr7   r   r   r   Ú
test_NAMESµ   s   zIRCUserTests.test_NAMESN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r&   r)   r,   r8   r>   rI   rW   r_   rb   r   r   r   r   r
      s    	'r
   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚMocksyIRCUserc                 C   s   t dƒ| _g | _d S )Nr   )r   r   ÚmockedCodesr   r   r   r   Ú__init__Â   s   

zMocksyIRCUser.__init__c                 O   s   | j  |¡ d S ©N)rh   Úappend)r   ÚcodeÚ_Ú__r   r   r   r#   Æ   s   zMocksyIRCUser.sendMessageN)rc   rd   re   ri   r#   r   r   r   r   rg   Á   s    rg   ó   ÿ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d„ Zdd„ Zdd„ Zdd„ ZdS )ÚIRCUserBadEncodingTestszw
    Verifies that L{IRCUser} sends the correct error messages back to clients
    when given indecipherable bytes
    c                 C   s   t ƒ | _d S rj   )rg   r   r   r   r   r   r   Õ   s   zIRCUserBadEncodingTests.setUpc                 C   s.   t | jd| ƒdtgƒ |  | jj|g¡ dS )ak  
        Asserts that IRCUser sends the relevant error code when a given irc_x
        dispatch method is given undecodable bytes.

        @param irc_x: the name of the irc_FOO method to test.
        For example, irc_x = 'PRIVMSG' will check irc_PRIVMSG

        @param error: the error code irc_x should send. For example,
        irc.ERR_NOTONCHANNEL
        zirc_%sN)Úgetattrr   ÚBADTEXTrF   rh   )r   Úirc_xÚerrorr   r   r   ÚassertChokesOnBadBytesØ   s   z.IRCUserBadEncodingTests.assertChokesOnBadBytesc                 C   ó   |   dtj¡ dS )zk
        Tests that irc_JOIN sends ERR_NOSUCHCHANNEL if the channel name can't
        be decoded.
        ÚJOINN©ru   r   r\   r   r   r   r   Ú	test_JOINè   ó   z!IRCUserBadEncodingTests.test_JOINc                 C   rv   )zl
        Tests that irc_NAMES sends ERR_NOSUCHCHANNEL if the channel name can't
        be decoded.
        ÚNAMESNrx   r   r   r   r   rb   ï   rz   z"IRCUserBadEncodingTests.test_NAMESc                 C   rv   )zl
        Tests that irc_TOPIC sends ERR_NOSUCHCHANNEL if the channel name can't
        be decoded.
        ÚTOPICNrx   r   r   r   r   Ú
test_TOPICö   rz   z"IRCUserBadEncodingTests.test_TOPICc                 C   rv   )zk
        Tests that irc_LIST sends ERR_NOSUCHCHANNEL if the channel name can't
        be decoded.
        ÚLISTNrx   r   r   r   r   Ú	test_LISTý   rz   z!IRCUserBadEncodingTests.test_LISTc                 C   rv   )zg
        Tests that irc_MODE sends ERR_NOSUCHNICK if the target name can't
        be decoded.
        ÚMODEN©ru   r   ÚERR_NOSUCHNICKr   r   r   r   Ú	test_MODE  rz   z!IRCUserBadEncodingTests.test_MODEc                 C   rv   )zj
        Tests that irc_PRIVMSG sends ERR_NOSUCHNICK if the target name can't
        be decoded.
        ÚPRIVMSGNr�   r   r   r   r   Útest_PRIVMSG  rz   z$IRCUserBadEncodingTests.test_PRIVMSGc                 C   rv   )zh
        Tests that irc_WHOIS sends ERR_NOSUCHNICK if the target name can't
        be decoded.
        ÚWHOISNr�   r   r   r   r   Ú
test_WHOIS  rz   z"IRCUserBadEncodingTests.test_WHOISc                 C   rv   )zi
        Tests that irc_PART sends ERR_NOTONCHANNEL if the target name can't
        be decoded.
        ÚPARTN)ru   r   r^   r   r   r   r   r_     rz   z!IRCUserBadEncodingTests.test_PARTc                 C   rv   )zo
        Tests that irc_WHO immediately ends the WHO list if the target name
        can't be decoded.
        ÚWHON)ru   r   ÚRPL_ENDOFWHOr   r   r   r   Útest_WHO&  rz   z IRCUserBadEncodingTests.test_WHON)rc   rd   re   rf   r   ru   ry   rb   r}   r   rƒ   r…   r‡   r_   r‹   r   r   r   r   rp   Í   s    			rp   N)rf   Útwisted.credr   r   Útwisted.testr   Útwisted.words.protocolsr   Útwisted.words.servicer   r   r   Útwisted.words.test.test_ircr	   r
   rg   rr   rp   r   r   r   r   Ú<module>   s    3	