3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-26 20:52:45 +01:00
PyLink/tests/tests_common.py

99 lines
4.3 KiB
Python
Raw Normal View History

import sys
import os
sys.path += [os.getcwd(), os.path.join(os.getcwd(), 'protocols')]
import unittest
import world
import classes
world.started.set()
class PluginTestCase(unittest.TestCase):
def setUp(self):
self.irc = classes.FakeIRC('unittest', world.testing)
self.proto = self.irc.proto
self.irc.connect()
self.sdata = self.irc.serverdata
self.u = self.irc.pseudoclient.uid
self.maxDiff = None
# Dummy servers/users used in tests below.
2015-09-07 08:39:10 +02:00
self.proto.spawnServer('whatever.', sid='10X')
for x in range(3):
2015-09-07 08:39:10 +02:00
self.proto.spawnClient('user%s' % x, server='10X')
class CommonProtoTestCase(PluginTestCase):
def testJoinClient(self):
u = self.u
2015-09-07 08:39:10 +02:00
self.proto.joinClient(u, '#Channel')
self.assertIn(u, self.irc.channels['#channel'].users)
# Non-existant user.
2015-09-07 08:39:10 +02:00
self.assertRaises(LookupError, self.proto.joinClient, '9PYZZZZZZ', '#test')
def testKickClient(self):
2015-09-07 08:39:10 +02:00
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)
2015-09-07 08:39:10 +02:00
self.proto.kickClient(self.u, '#pylink', target, 'Pow!')
self.assertNotIn(target, self.irc.channels['#pylink'].users)
def testModeClient(self):
2015-09-07 08:39:10 +02:00
testuser = self.proto.spawnClient('testcakes')
self.irc.takeMsgs()
2015-09-07 08:39:10 +02:00
self.proto.modeClient(self.u, testuser.uid, [('+i', None), ('+w', None)])
self.assertEqual({('i', None), ('w', None)}, testuser.modes)
2015-09-07 08:39:10 +02:00
# 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):
2015-09-07 08:39:10 +02:00
self.proto.nickClient(self.u, 'NotPyLink')
self.assertEqual('NotPyLink', self.irc.users[self.u].nick)
def testPartClient(self):
u = self.u
2015-09-07 08:39:10 +02:00
self.proto.joinClient(u, '#channel')
self.proto.partClient(u, '#channel')
self.assertNotIn(u, self.irc.channels['#channel'].users)
def testQuitClient(self):
2015-09-07 08:39:10 +02:00
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):
2015-09-07 08:39:10 +02:00
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
2015-09-07 08:39:10 +02:00
self.assertRaises(ValueError, self.proto.spawnClient, 'abcd', 'user', 'dummy.user.net', server='44A')
# Unfilled args should get placeholder fields and not error.
2015-09-07 08:39:10 +02:00
self.proto.spawnClient('testuser4')
def testSpawnClientOnServer(self):
2015-09-07 08:39:10 +02:00
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
2015-09-07 08:39:10 +02:00
self.assertRaises(Exception, self.proto.spawnServer, 'subserver.pylink', '34Q0')
self.proto.spawnServer('subserver.pylink', '34Q')
# Duplicate server name
2015-09-07 08:39:10 +02:00
self.assertRaises(Exception, self.proto.spawnServer, 'Subserver.PyLink', '34Z')
# Duplicate SID
2015-09-07 08:39:10 +02:00
self.assertRaises(Exception, self.proto.spawnServer, 'another.Subserver.PyLink', '34Q')
self.assertIn('34Q', self.irc.servers)