Ticket #4915: Twisted-tests-better.diff

File Twisted-tests-better.diff, 3.9 KB (added by devinj, 6 years ago)

Updated tests, now with a more twisted style and docstrings.

  • Twisted/twisted/words/test/test_irc_service.py

     
    77
    88from twisted.trial import unittest
    99from twisted.test import proto_helpers
    10 from twisted.words.service import InMemoryWordsRealm, IRCFactory
     10from twisted.words.service import InMemoryWordsRealm, IRCFactory, IRCUser
    1111from twisted.words.protocols import irc
    1212from twisted.cred import checkers, portal
    1313
     
    108108             ('example.com', '004',
    109109              ['john', 'example.com', self.factory._serverInfo["serviceVersion"],
    110110               'w', 'n'])])
     111
     112
     113
     114class MocksyIRCUser(IRCUser):
     115    def __init__(self):
     116        self.mockedCodes = []
     117   
     118    def sendMessage(self, code, *_, **__):
     119        self.mockedCodes.append(code)
     120
     121BADTEXT = '\xff'
     122
     123class IRCUserBadEncodingTestCase(unittest.TestCase):
     124    """
     125    Verifies that L{IRCUser} sends the correct error messages back to clients
     126    when given indecipherable bytes
     127    """
     128    # TODO: irc_NICK -- but NICKSERV is used for that, so it isn't as easy.
     129   
     130    def setUp(self):
     131        self.ircuser = MocksyIRCUser()
     132   
     133    def assertChokesOnBadBytes(self, irc_x, error):
     134        """
     135        Asserts that IRCUser sends the relevant error code when a given irc_x
     136        dispatch method is given undecodable bytes.
     137       
     138        @param irc_x: the name of the irc_FOO method to test.
     139        For example, irc_x = 'PRIVMSG' will check irc_PRIVMSG
     140       
     141        @param error: the error code irc_x should send. For example,
     142        irc.ERR_NOTONCHANNEL
     143        """
     144        getattr(self.ircuser, 'irc_%s' % irc_x)(None, [BADTEXT])
     145        self.assertEqual(self.ircuser.mockedCodes, [error])
     146   
     147    # no such channel
     148   
     149    def test_JOIN(self):
     150        """
     151        Tests that irc_JOIN sends ERR_NOSUCHCHANNEL if the channel name can't
     152        be decoded.
     153        """
     154        self.assertChokesOnBadBytes('JOIN', irc.ERR_NOSUCHCHANNEL)
     155   
     156    def test_NAMES(self):
     157        """
     158        Tests that irc_NAMES sends ERR_NOSUCHCHANNEL if the channel name can't
     159        be decoded.
     160        """
     161        self.assertChokesOnBadBytes('NAMES', irc.ERR_NOSUCHCHANNEL)
     162   
     163    def test_TOPIC(self):
     164        """
     165        Tests that irc_TOPIC sends ERR_NOSUCHCHANNEL if the channel name can't
     166        be decoded.
     167        """
     168        self.assertChokesOnBadBytes('TOPIC', irc.ERR_NOSUCHCHANNEL)
     169   
     170    def test_LIST(self):
     171        """
     172        Tests that irc_LIST sends ERR_NOSUCHCHANNEL if the channel name can't
     173        be decoded.
     174        """
     175        self.assertChokesOnBadBytes('LIST', irc.ERR_NOSUCHCHANNEL)
     176   
     177    # no such nick
     178   
     179    def test_MODE(self):
     180        """
     181        Tests that irc_MODE sends ERR_NOSUCHNICK if the target name can't
     182        be decoded.
     183        """
     184        self.assertChokesOnBadBytes('MODE', irc.ERR_NOSUCHNICK)
     185   
     186    def test_PRIVMSG(self):
     187        """
     188        Tests that irc_PRIVMSG sends ERR_NOSUCHNICK if the target name can't
     189        be decoded.
     190        """
     191        self.assertChokesOnBadBytes('PRIVMSG', irc.ERR_NOSUCHNICK)
     192   
     193    def test_WHOIS(self):
     194        """
     195        Tests that irc_WHOIS sends ERR_NOSUCHNICK if the target name can't
     196        be decoded.
     197        """
     198        self.assertChokesOnBadBytes('WHOIS', irc.ERR_NOSUCHNICK)
     199   
     200    # not on channel
     201   
     202    def test_PART(self):
     203        """
     204        Tests that irc_PART sends ERR_NOTONCHANNEL if the target name can't
     205        be decoded.
     206        """
     207        self.assertChokesOnBadBytes('PART', irc.ERR_NOTONCHANNEL)
     208   
     209    # probably nothing
     210   
     211    def test_WHO(self):
     212        """
     213        Tests that irc_WHO immediately ends the WHO list if the target name
     214        can't be decoded.
     215        """
     216        self.assertChokesOnBadBytes('WHO', irc.RPL_ENDOFWHO)
     217