2015-07-04 02:05:44 +02:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
sys.path.append(os.getcwd())
|
|
|
|
import unittest
|
2015-07-06 21:38:45 +02:00
|
|
|
import itertools
|
2015-12-26 23:30:14 +01:00
|
|
|
from copy import deepcopy
|
2015-07-04 02:05:44 +02:00
|
|
|
|
|
|
|
import utils
|
2015-08-29 04:27:38 +02:00
|
|
|
import classes
|
2015-12-25 03:24:42 +01:00
|
|
|
import conf
|
2015-08-29 18:39:33 +02:00
|
|
|
import world
|
2015-07-04 02:05:44 +02:00
|
|
|
|
|
|
|
def dummyf():
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TestUtils(unittest.TestCase):
|
2015-08-29 04:27:38 +02:00
|
|
|
def setUp(self):
|
2015-12-25 03:24:42 +01:00
|
|
|
self.irc = classes.FakeIRC('fakeirc', classes.FakeProto, conf.testconf)
|
2015-08-29 04:27:38 +02:00
|
|
|
|
2015-07-04 02:05:44 +02:00
|
|
|
def testTS6UIDGenerator(self):
|
|
|
|
uidgen = utils.TS6UIDGenerator('9PY')
|
|
|
|
self.assertEqual(uidgen.next_uid(), '9PYAAAAAA')
|
|
|
|
self.assertEqual(uidgen.next_uid(), '9PYAAAAAB')
|
|
|
|
|
|
|
|
def test_add_cmd(self):
|
|
|
|
# Without name specified, add_cmd adds a command with the same name
|
|
|
|
# as the function
|
|
|
|
utils.add_cmd(dummyf)
|
|
|
|
utils.add_cmd(dummyf, 'TEST')
|
|
|
|
# All command names should be automatically lowercased.
|
2015-09-27 19:53:25 +02:00
|
|
|
self.assertIn('dummyf', world.commands)
|
|
|
|
self.assertIn('test', world.commands)
|
|
|
|
self.assertNotIn('TEST', world.commands)
|
2015-07-04 02:05:44 +02:00
|
|
|
|
|
|
|
def test_add_hook(self):
|
2015-07-05 04:00:29 +02:00
|
|
|
utils.add_hook(dummyf, 'join')
|
2015-09-27 19:53:25 +02:00
|
|
|
self.assertIn('JOIN', world.hooks)
|
2015-07-05 04:00:29 +02:00
|
|
|
# Command names stored in uppercase.
|
2015-09-27 19:53:25 +02:00
|
|
|
self.assertNotIn('join', world.hooks)
|
|
|
|
self.assertIn(dummyf, world.hooks['JOIN'])
|
2015-07-04 02:05:44 +02:00
|
|
|
|
|
|
|
def testIsNick(self):
|
|
|
|
self.assertFalse(utils.isNick('abcdefgh', nicklen=3))
|
|
|
|
self.assertTrue(utils.isNick('aBcdefgh', nicklen=30))
|
|
|
|
self.assertTrue(utils.isNick('abcdefgh1'))
|
|
|
|
self.assertTrue(utils.isNick('ABC-def'))
|
|
|
|
self.assertFalse(utils.isNick('-_-'))
|
|
|
|
self.assertFalse(utils.isNick(''))
|
|
|
|
self.assertFalse(utils.isNick(' i lost the game'))
|
|
|
|
self.assertFalse(utils.isNick(':aw4t*9e4t84a3t90$&*6'))
|
|
|
|
self.assertFalse(utils.isNick('9PYAAAAAB'))
|
|
|
|
self.assertTrue(utils.isNick('_9PYAAAAAB\\'))
|
|
|
|
|
|
|
|
def testIsChannel(self):
|
|
|
|
self.assertFalse(utils.isChannel(''))
|
|
|
|
self.assertFalse(utils.isChannel('lol'))
|
|
|
|
self.assertTrue(utils.isChannel('#channel'))
|
|
|
|
self.assertTrue(utils.isChannel('##ABCD'))
|
|
|
|
|
|
|
|
def testIsServerName(self):
|
2015-07-07 23:31:47 +02:00
|
|
|
self.assertFalse(utils.isServerName('Invalid'))
|
|
|
|
self.assertTrue(utils.isServerName('services.'))
|
2015-07-04 02:05:44 +02:00
|
|
|
self.assertFalse(utils.isServerName('.s.s.s'))
|
|
|
|
self.assertTrue(utils.isServerName('Hello.world'))
|
|
|
|
self.assertFalse(utils.isServerName(''))
|
2016-01-18 06:07:51 +01:00
|
|
|
self.assertTrue(utils.isServerName('pylink.somenet.local'))
|
2015-07-07 23:31:47 +02:00
|
|
|
self.assertFalse(utils.isServerName(' i lost th.e game'))
|
2015-07-04 02:05:44 +02:00
|
|
|
|
2015-07-06 21:38:45 +02:00
|
|
|
def testJoinModes(self):
|
2015-07-09 01:58:59 +02:00
|
|
|
res = utils.joinModes({('+l', '50'), ('+n', None), ('+t', None)})
|
2015-07-06 21:38:45 +02:00
|
|
|
# Sets are orderless, so the end mode could be scrambled in a number of ways.
|
|
|
|
# Basically, we're looking for a string that looks like '+ntl 50' or '+lnt 50'.
|
|
|
|
possible = ['+%s 50' % ''.join(x) for x in itertools.permutations('lnt', 3)]
|
|
|
|
self.assertIn(res, possible)
|
2015-07-09 01:58:59 +02:00
|
|
|
|
2015-07-06 21:38:45 +02:00
|
|
|
# Without any arguments, make sure there is no trailing space.
|
2015-07-09 01:58:59 +02:00
|
|
|
self.assertEqual(utils.joinModes({('+t', None)}), '+t')
|
|
|
|
|
|
|
|
# The +/- in the mode is not required; if it doesn't exist, assume we're
|
|
|
|
# adding modes always.
|
|
|
|
self.assertEqual(utils.joinModes([('t', None), ('n', None)]), '+tn')
|
|
|
|
|
|
|
|
# An empty query should return just '+'
|
2015-07-06 21:38:45 +02:00
|
|
|
self.assertEqual(utils.joinModes(set()), '+')
|
|
|
|
|
2015-07-09 01:58:59 +02:00
|
|
|
# More complex query now with both + and - modes being set
|
|
|
|
res = utils.joinModes([('+l', '50'), ('-n', None)])
|
|
|
|
self.assertEqual(res, '+l-n 50')
|
|
|
|
|
|
|
|
# If one modepair in the list lacks a +/- prefix, just follow the
|
|
|
|
# previous one's.
|
|
|
|
res = utils.joinModes([('+l', '50'), ('-n', None), ('m', None)])
|
|
|
|
self.assertEqual(res, '+l-nm 50')
|
|
|
|
res = utils.joinModes([('+l', '50'), ('m', None)])
|
|
|
|
self.assertEqual(res, '+lm 50')
|
|
|
|
res = utils.joinModes([('l', '50'), ('-m', None)])
|
|
|
|
self.assertEqual(res, '+l-m 50')
|
|
|
|
|
|
|
|
# Rarely in real life will we get a mode string this complex.
|
|
|
|
# Let's make sure it works, just in case.
|
|
|
|
res = utils.joinModes([('-o', '9PYAAAAAA'), ('+l', '50'), ('-n', None),
|
|
|
|
('-m', None), ('+k', 'hello'),
|
|
|
|
('+b', '*!*@*.badisp.net')])
|
|
|
|
self.assertEqual(res, '-o+l-nm+kb 9PYAAAAAA 50 hello *!*@*.badisp.net')
|
2015-07-19 23:59:51 +02:00
|
|
|
|
2015-12-26 23:30:14 +01:00
|
|
|
def _reverseModes(self, query, expected, target='#PyLink', oldobj=None):
|
|
|
|
res = utils.reverseModes(self.irc, target, query, oldobj=oldobj)
|
2015-09-13 07:29:53 +02:00
|
|
|
self.assertEqual(res, expected)
|
|
|
|
|
2015-08-29 04:27:38 +02:00
|
|
|
def testReverseModes(self):
|
2015-12-26 23:30:14 +01:00
|
|
|
# Initialize the channe, first.
|
|
|
|
utils.applyModes(self.irc, '#PyLink', [])
|
2015-08-29 04:27:38 +02:00
|
|
|
# Strings.
|
2015-09-13 23:05:07 +02:00
|
|
|
self._reverseModes("+mk-t test", "-mk+t test")
|
|
|
|
self._reverseModes("ml-n 111", "-ml+n")
|
2015-08-29 04:27:38 +02:00
|
|
|
# Lists.
|
2015-09-13 23:05:07 +02:00
|
|
|
self._reverseModes([('+m', None), ('+r', None), ('+l', '3')],
|
|
|
|
{('-m', None), ('-r', None), ('-l', None)})
|
2015-08-29 04:27:38 +02:00
|
|
|
# Sets.
|
2015-09-13 23:05:07 +02:00
|
|
|
self._reverseModes({('s', None)}, {('-s', None)})
|
2015-08-29 04:27:38 +02:00
|
|
|
# Combining modes with an initial + and those without
|
2015-09-13 07:29:53 +02:00
|
|
|
self._reverseModes({('s', None), ('+R', None)}, {('-s', None), ('-R', None)})
|
|
|
|
|
|
|
|
def testReverseModesUser(self):
|
|
|
|
self._reverseModes({('+i', None), ('l', 'asfasd')}, {('-i', None), ('-l', 'asfasd')},
|
|
|
|
target=self.irc.pseudoclient.uid)
|
|
|
|
|
|
|
|
def testReverseModesExisting(self):
|
2015-12-26 23:30:14 +01:00
|
|
|
utils.applyModes(self.irc, '#PyLink', [('+m', None), ('+l', '50'), ('+k', 'supersecret'),
|
2015-09-13 07:29:53 +02:00
|
|
|
('+o', '9PYAAAAAA')])
|
|
|
|
|
|
|
|
self._reverseModes({('+i', None), ('+l', '3')}, {('-i', None), ('+l', '50')})
|
|
|
|
self._reverseModes('-n', '+n')
|
|
|
|
self._reverseModes('-l', '+l 50')
|
|
|
|
self._reverseModes('+k derp', '+k supersecret')
|
|
|
|
self._reverseModes('-mk *', '+mk supersecret')
|
|
|
|
|
2015-12-26 23:30:14 +01:00
|
|
|
self.irc.proto.spawnClient("tester2")
|
|
|
|
oldobj = deepcopy(self.irc.channels['#PyLink'])
|
|
|
|
|
2015-09-13 07:29:53 +02:00
|
|
|
# Existing modes are ignored.
|
|
|
|
self._reverseModes([('+t', None)], set())
|
|
|
|
self._reverseModes('+n', '+')
|
2015-12-26 23:30:14 +01:00
|
|
|
#self._reverseModes('+oo 9PYAAAAAB 9PYAAAAAA', '-o 9PYAAAAAB', oldobj=oldobj)
|
2015-09-13 07:29:53 +02:00
|
|
|
self._reverseModes('+o 9PYAAAAAA', '+')
|
2015-12-26 23:30:14 +01:00
|
|
|
self._reverseModes('+vM 9PYAAAAAA', '-M')
|
2015-08-29 04:27:38 +02:00
|
|
|
|
2015-09-13 23:05:07 +02:00
|
|
|
# Ignore unsetting prefixmodes/list modes that were never set.
|
|
|
|
self._reverseModes([('-v', '10XAAAAAA')], set())
|
|
|
|
self._reverseModes('-ob 10XAAAAAA derp!*@*', '+')
|
2015-12-26 23:30:14 +01:00
|
|
|
utils.applyModes(self.irc, '#PyLink', [('+b', '*!user@badisp.tk')])
|
2015-09-13 23:05:07 +02:00
|
|
|
self._reverseModes('-bb *!*@* *!user@badisp.tk', '+b *!user@badisp.tk')
|
|
|
|
|
2015-07-04 02:05:44 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|