From 3217f726f84cab7239d7a3ade58cc2615f5b9f9a Mon Sep 17 00:00:00 2001 From: James Lu Date: Sun, 6 Sep 2015 23:39:10 -0700 Subject: [PATCH] Fix those test cases again. --- classes.py | 27 +++++++---------- tests/test_coreplugin.py | 2 +- tests/test_fakeirc.py | 2 +- tests/test_proto_inspircd.py | 32 ++++++++++++--------- tests/test_relay.py | 8 +++--- tests/test_utils.py | 2 +- tests/tests_common.py | 56 +++++++++++++++++++----------------- 7 files changed, 65 insertions(+), 64 deletions(-) diff --git a/classes.py b/classes.py index 97e7a86..a622b74 100644 --- a/classes.py +++ b/classes.py @@ -391,29 +391,22 @@ class Protocol(): self.casemapping = 'rfc1459' self.hook_map = {} -class FakeProto(): +class FakeProto(Protocol): """Dummy protocol module for testing purposes.""" - def __init__(self): - self.hook_map = {} - self.casemapping = 'rfc1459' - self.__name__ = 'FakeProto' - - @staticmethod - def handle_events(irc, data): + def handle_events(self, data): pass - @staticmethod - def connect(irc): + def connect(self): pass - @staticmethod - def spawnClient(irc, nick, *args, **kwargs): + def spawnClient(self, nick, *args, **kwargs): uid = randint(1, 10000000000) ts = int(time.time()) - irc.users[uid] = user = IrcUser(nick, ts, uid) + self.irc.users[uid] = user = IrcUser(nick, ts, uid) return user - @staticmethod - def joinClient(irc, client, channel): - irc.channels[channel].users.add(client) - irc.users[client].channels.add(channel) + def joinClient(self, client, channel): + self.irc.channels[channel].users.add(client) + self.irc.users[client].channels.add(channel) + +FakeProto.Class = FakeProto diff --git a/tests/test_coreplugin.py b/tests/test_coreplugin.py index 4480297..45bd7b1 100644 --- a/tests/test_coreplugin.py +++ b/tests/test_coreplugin.py @@ -29,7 +29,7 @@ class CorePluginTestCase(tests_common.PluginTestCase): self.assertNotEqual(self.irc.pseudoclient.uid, spmain[0]['olduser']) def testKickrejoin(self): - self.proto.kickClient(self.irc, self.u, '#pylink', self.u, 'test') + self.proto.kickClient(self.u, '#pylink', self.u, 'test') msgs = self.irc.takeMsgs() commands = self.irc.takeCommands(msgs) self.assertIn('FJOIN', commands) diff --git a/tests/test_fakeirc.py b/tests/test_fakeirc.py index c69c604..14a08e0 100644 --- a/tests/test_fakeirc.py +++ b/tests/test_fakeirc.py @@ -7,7 +7,7 @@ import unittest class TestFakeIRC(unittest.TestCase): def setUp(self): - self.irc = classes.FakeIRC('unittest', classes.FakeProto()) + self.irc = classes.FakeIRC('unittest', classes.FakeProto) def testFakeIRC(self): self.irc.run('this should do nothing') diff --git a/tests/test_proto_inspircd.py b/tests/test_proto_inspircd.py index c8b8c08..fdc8ec2 100644 --- a/tests/test_proto_inspircd.py +++ b/tests/test_proto_inspircd.py @@ -19,7 +19,7 @@ class InspIRCdTestCase(tests_common.CommonProtoTestCase): self.assertRaises(classes.ProtocolError, self.irc.run, 'SERVER somehow.someday BADPASS 0 0AL :Somehow Server - McMurdo Station, Antarctica') def testConnect(self): - self.proto.connect(self.irc) + self.proto.connect() initial_messages = self.irc.takeMsgs() commands = self.irc.takeCommands(initial_messages) # SERVER pylink.unittest abcd 0 9PY :PyLink Service @@ -40,20 +40,20 @@ class InspIRCdTestCase(tests_common.CommonProtoTestCase): def testHandleSQuit(self): # Spawn a messy network map, just because! - self.proto.spawnServer(self.irc, 'level1.pylink', '34P') - self.proto.spawnServer(self.irc, 'level2.pylink', '34Q', uplink='34P') - self.proto.spawnServer(self.irc, 'level3.pylink', '34Z', uplink='34Q') - self.proto.spawnServer(self.irc, 'level4.pylink', '34Y', uplink='34Z') + self.proto.spawnServer('level1.pylink', '34P') + self.proto.spawnServer('level2.pylink', '34Q', uplink='34P') + self.proto.spawnServer('level3.pylink', '34Z', uplink='34Q') + self.proto.spawnServer('level4.pylink', '34Y', uplink='34Z') self.assertEqual(self.irc.servers['34Y'].uplink, '34Z') - s4u = self.proto.spawnClient(self.irc, 'person1', 'person', 'users.overdrive.pw', server='34Y').uid - s3u = self.proto.spawnClient(self.irc, 'person2', 'person', 'users.overdrive.pw', server='34Z').uid - self.proto.joinClient(self.irc, s3u, '#pylink') - self.proto.joinClient(self.irc, s4u, '#pylink') + s4u = self.proto.spawnClient('person1', 'person', 'users.overdrive.pw', server='34Y').uid + s3u = self.proto.spawnClient('person2', 'person', 'users.overdrive.pw', server='34Z').uid + self.proto.joinClient(s3u, '#pylink') + self.proto.joinClient(s4u, '#pylink') self.irc.run(':34Z SQUIT 34Y :random squit messsage') self.assertNotIn(s4u, self.irc.users) self.assertNotIn('34Y', self.irc.servers) # Netsplits are obviously recursive, so all these should be removed. - self.proto.handle_squit(self.irc, '9PY', 'SQUIT', ['34P']) + self.proto.handle_squit('9PY', 'SQUIT', ['34P']) self.assertNotIn(s3u, self.irc.users) self.assertNotIn('34P', self.irc.servers) self.assertNotIn('34Q', self.irc.servers) @@ -136,11 +136,16 @@ class InspIRCdTestCase(tests_common.CommonProtoTestCase): self.assertEqual(expected, hookdata) def testHandleFMode(self): + # Default channels start with +nt + self.irc.run(':70M FMODE #pylink 1423790412 -nt') + self.assertEqual(set(), self.irc.channels['#pylink'].modes) + self.irc.takeHooks() + self.irc.run(':70M FMODE #pylink 1423790412 +ikl herebedragons 100') self.assertEqual({('i', None), ('k', 'herebedragons'), ('l', '100')}, self.irc.channels['#pylink'].modes) self.irc.run(':70M FMODE #pylink 1423790413 -ilk+m herebedragons') self.assertEqual({('m', None)}, self.irc.channels['#pylink'].modes) - + hookdata = self.irc.takeHooks() expected = [['70M', 'FMODE', {'target': '#pylink', 'modes': [('+i', None), ('+k', 'herebedragons'), @@ -175,9 +180,10 @@ class InspIRCdTestCase(tests_common.CommonProtoTestCase): def testHandleFModeRemovesOldParams(self): self.irc.run(':70M FMODE #pylink 1423790412 +l 50') - self.assertEqual({('l', '50')}, self.irc.channels['#pylink'].modes) + self.assertIn(('l', '50'), self.irc.channels['#pylink'].modes) self.irc.run(':70M FMODE #pylink 1423790412 +l 30') - self.assertEqual({('l', '30')}, self.irc.channels['#pylink'].modes) + self.assertIn(('l', '30'), self.irc.channels['#pylink'].modes) + self.assertNotIn(('l', '50'), self.irc.channels['#pylink'].modes) hookdata = self.irc.takeHooks() expected = [['70M', 'FMODE', {'target': '#pylink', 'modes': [('+l', '50')], 'ts': 1423790412}], ['70M', 'FMODE', {'target': '#pylink', 'modes': [('+l', '30')], 'ts': 1423790412}]] diff --git a/tests/test_relay.py b/tests/test_relay.py index 5c999fe..fe6b864 100644 --- a/tests/test_relay.py +++ b/tests/test_relay.py @@ -12,13 +12,13 @@ def dummyf(): class TestRelay(unittest.TestCase): def setUp(self): - self.irc = classes.FakeIRC('unittest', classes.FakeProto()) + self.irc = classes.FakeIRC('fakeirc', classes.FakeProto) self.irc.maxnicklen = 20 self.f = lambda nick: relay.normalizeNick(self.irc, 'unittest', nick) # Fake our protocol name to something that supports slashes in nicks. # relay uses a whitelist for this to prevent accidentally introducing # bad nicks: - self.irc.proto.__name__ = "inspircd" + self.irc.protoname = "inspircd" def testNormalizeNick(self): # Second argument simply states the suffix. @@ -36,10 +36,10 @@ class TestRelay(unittest.TestCase): self.assertEqual(self.f('helloworld'), 'helloworl///unittest') def testNormalizeNickRemovesSlashes(self): - self.irc.proto.__name__ = "charybdis" + self.irc.protoname = "charybdis" try: self.assertEqual(self.f('helloworld'), 'helloworld|unittest') self.assertEqual(self.f('abcde/eJanus'), 'abcde|eJanu|unittest') self.assertEqual(self.f('ObnoxiouslyLongNick'), 'Obnoxiously|unittest') finally: - self.irc.proto.__name__ = "inspircd" + self.irc.protoname = "inspircd" diff --git a/tests/test_utils.py b/tests/test_utils.py index 8ecf82d..94a0194 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -13,7 +13,7 @@ def dummyf(): class TestUtils(unittest.TestCase): def setUp(self): - self.irc = classes.FakeIRC('fakeirc', classes.FakeProto()) + self.irc = classes.FakeIRC('fakeirc', classes.FakeProto) def testTS6UIDGenerator(self): uidgen = utils.TS6UIDGenerator('9PY') diff --git a/tests/tests_common.py b/tests/tests_common.py index b3f19f2..57bf066 100644 --- a/tests/tests_common.py +++ b/tests/tests_common.py @@ -17,80 +17,82 @@ class PluginTestCase(unittest.TestCase): self.u = self.irc.pseudoclient.uid self.maxDiff = None # Dummy servers/users used in tests below. - self.proto.spawnServer(self.irc, 'whatever.', sid='10X') + self.proto.spawnServer('whatever.', sid='10X') for x in range(3): - self.proto.spawnClient(self.irc, 'user%s' % x, server='10X') + self.proto.spawnClient('user%s' % x, server='10X') class CommonProtoTestCase(PluginTestCase): def testJoinClient(self): u = self.u - self.proto.joinClient(self.irc, u, '#Channel') + self.proto.joinClient(u, '#Channel') self.assertIn(u, self.irc.channels['#channel'].users) # Non-existant user. - self.assertRaises(LookupError, self.proto.joinClient, self.irc, '9PYZZZZZZ', '#test') + self.assertRaises(LookupError, self.proto.joinClient, '9PYZZZZZZ', '#test') def testKickClient(self): - target = self.proto.spawnClient(self.irc, 'soccerball', 'soccerball', 'abcd').uid - self.proto.joinClient(self.irc, target, '#pylink') + target = self.proto.spawnClient('soccerball', 'soccerball', 'abcd').uid + self.proto.joinClient(target, '#pylink') self.assertIn(self.u, self.irc.channels['#pylink'].users) self.assertIn(target, self.irc.channels['#pylink'].users) - self.proto.kickClient(self.irc, self.u, '#pylink', target, 'Pow!') + self.proto.kickClient(self.u, '#pylink', target, 'Pow!') self.assertNotIn(target, self.irc.channels['#pylink'].users) def testModeClient(self): - testuser = self.proto.spawnClient(self.irc, 'testcakes') + testuser = self.proto.spawnClient('testcakes') self.irc.takeMsgs() - self.proto.modeClient(self.irc, self.u, testuser.uid, [('+i', None), ('+w', None)]) + self.proto.modeClient(self.u, testuser.uid, [('+i', None), ('+w', None)]) self.assertEqual({('i', None), ('w', None)}, testuser.modes) - self.proto.modeClient(self.irc, self.u, '#pylink', [('+s', None), ('+l', '30')]) - self.assertEqual({('s', None), ('l', '30')}, self.irc.channels['#pylink'].modes) + # Default channels start with +nt + self.assertEqual({('n', None), ('t', None)}, self.irc.channels['#pylink'].modes) + self.proto.modeClient(self.u, '#pylink', [('+s', None), ('+l', '30')]) + self.assertEqual({('s', None), ('l', '30'), ('n', None), ('t', None)}, self.irc.channels['#pylink'].modes) cmds = self.irc.takeCommands(self.irc.takeMsgs()) self.assertEqual(cmds, ['MODE', 'FMODE']) def testNickClient(self): - self.proto.nickClient(self.irc, self.u, 'NotPyLink') + self.proto.nickClient(self.u, 'NotPyLink') self.assertEqual('NotPyLink', self.irc.users[self.u].nick) def testPartClient(self): u = self.u - self.proto.joinClient(self.irc, u, '#channel') - self.proto.partClient(self.irc, u, '#channel') + self.proto.joinClient(u, '#channel') + self.proto.partClient(u, '#channel') self.assertNotIn(u, self.irc.channels['#channel'].users) def testQuitClient(self): - u = self.proto.spawnClient(self.irc, 'testuser3', 'moo', 'hello.world').uid - self.proto.joinClient(self.irc, u, '#channel') - self.assertRaises(LookupError, self.proto.quitClient, self.irc, '9PYZZZZZZ', 'quit reason') - self.proto.quitClient(self.irc, u, 'quit reason') + u = self.proto.spawnClient('testuser3', 'moo', 'hello.world').uid + self.proto.joinClient(u, '#channel') + self.assertRaises(LookupError, self.proto.quitClient, '9PYZZZZZZ', 'quit reason') + self.proto.quitClient(u, 'quit reason') self.assertNotIn(u, self.irc.channels['#channel'].users) self.assertNotIn(u, self.irc.users) self.assertNotIn(u, self.irc.servers[self.irc.sid].users) def testSpawnClient(self): - u = self.proto.spawnClient(self.irc, 'testuser3', 'moo', 'hello.world').uid + u = self.proto.spawnClient('testuser3', 'moo', 'hello.world').uid # Check the server index and the user index self.assertIn(u, self.irc.servers[self.irc.sid].users) self.assertIn(u, self.irc.users) # Raise ValueError when trying to spawn a client on a server that's not ours - self.assertRaises(ValueError, self.proto.spawnClient, self.irc, 'abcd', 'user', 'dummy.user.net', server='44A') + self.assertRaises(ValueError, self.proto.spawnClient, 'abcd', 'user', 'dummy.user.net', server='44A') # Unfilled args should get placeholder fields and not error. - self.proto.spawnClient(self.irc, 'testuser4') + self.proto.spawnClient('testuser4') def testSpawnClientOnServer(self): - self.proto.spawnServer(self.irc, 'subserver.pylink', '34Q') - u = self.proto.spawnClient(self.irc, 'person1', 'person', 'users.overdrive.pw', server='34Q') + self.proto.spawnServer('subserver.pylink', '34Q') + u = self.proto.spawnClient('person1', 'person', 'users.overdrive.pw', server='34Q') # We're spawning clients on the right server, hopefully... self.assertIn(u.uid, self.irc.servers['34Q'].users) self.assertNotIn(u.uid, self.irc.servers[self.irc.sid].users) def testSpawnServer(self): # Incorrect SID length - self.assertRaises(Exception, self.proto.spawnServer, self.irc, 'subserver.pylink', '34Q0') - self.proto.spawnServer(self.irc, 'subserver.pylink', '34Q') + self.assertRaises(Exception, self.proto.spawnServer, 'subserver.pylink', '34Q0') + self.proto.spawnServer('subserver.pylink', '34Q') # Duplicate server name - self.assertRaises(Exception, self.proto.spawnServer, self.irc, 'Subserver.PyLink', '34Z') + self.assertRaises(Exception, self.proto.spawnServer, 'Subserver.PyLink', '34Z') # Duplicate SID - self.assertRaises(Exception, self.proto.spawnServer, self.irc, 'another.Subserver.PyLink', '34Q') + self.assertRaises(Exception, self.proto.spawnServer, 'another.Subserver.PyLink', '34Q') self.assertIn('34Q', self.irc.servers)