3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-25 04:02:45 +01:00

Add tests for pr/inspircd, a common FakeIRC class, and tests for that too

This commit is contained in:
James Lu 2015-07-03 18:07:47 -07:00
parent fed107b12b
commit 8ad68023d6
2 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,92 @@
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import main
import classes
from collections import defaultdict
import unittest
class FakeIRC(main.Irc):
def __init__(self, proto):
self.connected = False
self.users = {}
self.channels = defaultdict(classes.IrcChannel)
self.name = 'fakeirc'
self.servers = {}
self.proto = proto
self.serverdata = {'netname': 'fakeirc',
'ip': '0.0.0.0',
'port': 7000,
'recvpass': "abcd",
'sendpass': "abcd",
'protocol': "testingonly",
'hostname': "pylink.unittest",
'sid': "9PY",
'channels': ["#pylink"],
}
self.conf = {'server': self.serverdata}
ip = self.serverdata["ip"]
port = self.serverdata["port"]
self.sid = self.serverdata["sid"]
self.socket = None
self.messages = []
def run(self, data):
"""Queues a message to the fake IRC server."""
print('-> ' + data)
self.proto.handle_events(self, data)
def send(self, data):
self.messages.append(data)
print('<- ' + data)
def takeMsgs(self):
"""Returns a list of messages sent by the protocol module since
the last takeMsgs() call, so we can track what has been sent."""
msgs = self.messages
self.messages = []
return msgs
def takeCommands(self, msgs):
"""Returns a list of commands parsed from the output of takeMsgs()."""
sidprefix = ':' + self.sid
commands = []
for m in msgs:
args = m.split()
if m.startswith(sidprefix):
commands.append(args[1])
else:
commands.append(args[0])
return commands
class FakeProto():
"""Dummy protocol module for testing purposes."""
@staticmethod
def handle_events(irc, data):
pass
@staticmethod
def connect(irc):
pass
# Yes, we're going to even test the testing classes. Testception? I think so.
class Test_TestProtoCommon(unittest.TestCase):
def setUp(self):
self.irc = FakeIRC(FakeProto())
def testFakeIRC(self):
self.irc.run('this should do nothing')
self.irc.send('ADD this message')
self.irc.send(':add THIS message too')
msgs = self.irc.takeMsgs()
self.assertEqual(['ADD this message', ':add THIS message too'],
msgs)
def testFakeIRC_takeMsgs(self):
msgs = ['ADD this message', ':9PY THIS message too']
self.assertEqual(['ADD', 'THIS'], self.irc.takeCommands(msgs))
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,37 @@
import sys
import os
sys.path += [os.getcwd(), os.path.join(os.getcwd(), 'protocols')]
import unittest
import inspircd
import test_proto_common
from classes import ProtocolError
class TestInspIRCdProtocol(unittest.TestCase):
def setUp(self):
self.irc = test_proto_common.FakeIRC(inspircd)
self.sdata = self.irc.serverdata
def test_connect(self):
self.irc.proto.connect(self.irc)
initial_messages = self.irc.takeMsgs()
commands = self.irc.takeCommands(initial_messages)
# SERVER pylink.unittest abcd 0 9PY :PyLink Service
serverline = 'SERVER %s %s 0 %s :PyLink Service' % (
self.sdata['hostname'], self.sdata['sendpass'], self.sdata['sid'])
self.assertIn(serverline, initial_messages)
self.assertIn('BURST', commands)
self.assertIn('ENDBURST', commands)
# Is it creating our lovely PyLink PseudoClient?
self.assertIn('UID', commands)
self.assertIn('FJOIN', commands)
def test_checkRecvpass(self):
# Correct recvpass here.
self.irc.run('SERVER somehow.someday abcd 0 0AL :Somehow Server - McMurdo Station, Antarctica')
# Incorrect recvpass here; should raise ProtocolError.
self.assertRaises(ProtocolError, self.irc.run, 'SERVER somehow.someday BADPASS 0 0AL :Somehow Server - McMurdo Station, Antarctica')
if __name__ == '__main__':
unittest.main()