o
    >h&                     @   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   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-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   s8   | j j }| j j  |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>L   s    z)IRCUserTests.response.<locals>.<listcomp>)r   	transportr%   r"   decode
splitlinesr   responser   r   r   r5   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   r5   messageTypenmessager   r   r   scanResponseN   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 - r6   376End of /MOTD command.N)	r   r!   r5   listr;   r   RPL_MOTDSTARTRPL_ENDOFMOTDassertEqual)r   r5   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=   r>   r?   001zconnected to Twisted IRC002003004wr9   N)	r   irc_USERirc_PASSr!   formatr   _serverInforC   r5   )r   versioncreationr   r   r   test_fullLoginx   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   r6   somechannels   somechannels   boogaboogaN)r   r!   r5   r1   r"   rC   r   rA   irc_JOINERR_NOSUCHCHANNELirc_PARTERR_NOTONCHANNELr4   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   rU   rV   r   r6   N)
r   r!   rX   r1   r"   	irc_NAMESr5   rC   r   RPL_ENDOFNAMESr4   r   r   r   
test_NAMES   s   zIRCUserTests.test_NAMESN)__name__
__module____qualname____doc__r   r&   r)   r,   r5   r;   rF   rT   r\   r_   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)re   append)r   code___r   r   r   r#      s   zMocksyIRCUser.sendMessageN)r`   ra   rb   rf   r#   r   r   r   r   rd      s    rd      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 rg   )rd   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   BADTEXTrC   re   )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rr   r   rY   r   r   r   r   	test_JOIN      z!IRCUserBadEncodingTests.test_JOINc                 C   rs   )zl
        Tests that irc_NAMES sends ERR_NOSUCHCHANNEL if the channel name can't
        be decoded.
        NAMESNru   r   r   r   r   r_      rw   z"IRCUserBadEncodingTests.test_NAMESc                 C   rs   )zl
        Tests that irc_TOPIC sends ERR_NOSUCHCHANNEL if the channel name can't
        be decoded.
        TOPICNru   r   r   r   r   
test_TOPIC   rw   z"IRCUserBadEncodingTests.test_TOPICc                 C   rs   )zk
        Tests that irc_LIST sends ERR_NOSUCHCHANNEL if the channel name can't
        be decoded.
        LISTNru   r   r   r   r   	test_LIST   rw   z!IRCUserBadEncodingTests.test_LISTc                 C   rs   )zg
        Tests that irc_MODE sends ERR_NOSUCHNICK if the target name can't
        be decoded.
        MODENrr   r   ERR_NOSUCHNICKr   r   r   r   	test_MODE  rw   z!IRCUserBadEncodingTests.test_MODEc                 C   rs   )zj
        Tests that irc_PRIVMSG sends ERR_NOSUCHNICK if the target name can't
        be decoded.
        PRIVMSGNr~   r   r   r   r   test_PRIVMSG  rw   z$IRCUserBadEncodingTests.test_PRIVMSGc                 C   rs   )zh
        Tests that irc_WHOIS sends ERR_NOSUCHNICK if the target name can't
        be decoded.
        WHOISNr~   r   r   r   r   
test_WHOIS  rw   z"IRCUserBadEncodingTests.test_WHOISc                 C   rs   )zi
        Tests that irc_PART sends ERR_NOTONCHANNEL if the target name can't
        be decoded.
        PARTN)rr   r   r[   r   r   r   r   r\     rw   z!IRCUserBadEncodingTests.test_PARTc                 C   rs   )zo
        Tests that irc_WHO immediately ends the WHO list if the target name
        can't be decoded.
        WHON)rr   r   RPL_ENDOFWHOr   r   r   r   test_WHO%  rw   z IRCUserBadEncodingTests.test_WHON)r`   ra   rb   rc   r   rr   rv   r_   rz   r|   r   r   r   r\   r   r   r   r   r   rm      s    			rm   N)rc   twisted.credr   r   twisted.testr   twisted.words.protocolsr   twisted.words.servicer   r   r   twisted.words.test.test_ircr	   r
   rd   ro   rm   r   r   r   r   <module>   s    2	