diff --git a/.gitignore b/.gitignore index 51fedb0..39a92d2 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ __pycache__ /dist /build /pyircbot.egg-info +/.coverage +/htmlcov/ diff --git a/pyircbot/modules/LMGTFY.py b/pyircbot/modules/LMGTFY.py index 6ecc60c..68e46a0 100644 --- a/pyircbot/modules/LMGTFY.py +++ b/pyircbot/modules/LMGTFY.py @@ -24,8 +24,6 @@ class LMGTFY(ModuleBase): def createLink(self, message): finalUrl = BASE_URL - if type(message) == str: - message = message.split(" ") for word in message: finalUrl += urllib.parse.quote(word) diff --git a/pyircbot/modules/MySQL.py b/pyircbot/modules/MySQL.py index 360a793..344f16c 100755 --- a/pyircbot/modules/MySQL.py +++ b/pyircbot/modules/MySQL.py @@ -9,11 +9,7 @@ from pyircbot.modulebase import ModuleBase import sys - -try: - import MySQLdb # python 2.x -except: - import pymysql as MySQLdb # python 3.x +import pymysql as MySQLdb # python 3.x class MySQL(ModuleBase): diff --git a/pyircbot/modules/Tell.py b/pyircbot/modules/Tell.py index 82074c1..64d41ce 100644 --- a/pyircbot/modules/Tell.py +++ b/pyircbot/modules/Tell.py @@ -72,10 +72,11 @@ class Tell(ModuleBase): recip = cmd.args[0] message = ' '.join(cmd.args[1:]).strip() - if not message: - self.bot.act_PRIVMSG(msg.args[0], "%s: .tell - Tell someone something the next time " - "they're seen. Example: .tell antiroach Do your homework!" % - msg.prefix.nick) + c = self.db.query("SELECT COUNT(*) as `cnt` FROM `tells` WHERE `recip`=?;", (recip, )) + user_total = c.fetchall()[0]['cnt'] + c.close() + + if user_total >= self.config.get("max", 3): return self.db.query("INSERT INTO `tells` (`sender`, `channel`, `when`, `recip`, `message`) VALUES " diff --git a/requirements-test.txt b/requirements-test.txt index 4bae34c..b85621a 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -3,6 +3,7 @@ certifi==2017.4.17 chardet==3.0.4 cheroot==5.9.1 CherryPy==12.0.1 +coverage==4.4.2 decorator==4.0.11 idna==2.5 ipdb==0.10.3 @@ -10,6 +11,7 @@ ipython==6.0.0 ipython-genutils==0.2.0 jaraco.classes==1.4.3 jedi==0.10.2 +lxml==4.1.1 mock==2.0.0 msgbus==0.0.1 packaging==16.8 @@ -27,6 +29,7 @@ PyJWT==1.5.3 pyparsing==2.2.0 PySocks==1.6.7 pytest==3.2.5 +pytest-cov==2.5.1 pytz==2017.3 pyzmq==16.0.2 requests==2.18.1 diff --git a/run-tests.sh b/run-tests.sh index f7d45a2..50e3107 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -3,4 +3,4 @@ export PYTHONUNBUFFERED=1 export PYTHONPATH=. -py.test -s tests/ +py.test --cov=pyircbot --cov-report html -s tests/ diff --git a/tests/modules/test_calc.py b/tests/modules/test_calc.py index 95b8f22..da18133 100644 --- a/tests/modules/test_calc.py +++ b/tests/modules/test_calc.py @@ -16,10 +16,10 @@ def calcbot(fakebot): "delayMatch": 0} fakebot.loadmodule("SQLite") with closing(fakebot.moduleInstances["SQLite"].opendb("calc.db")) as db: - for q in ["DELETE FROM calc_addedby;", - "DELETE FROM calc_channels;", - "DELETE FROM calc_definitions;", - "DELETE FROM calc_words;"]: + for q in ["DROP TABLE calc_addedby;", + "DROP TABLE calc_channels;", + "DROP TABLE calc_definitions;", + "DROP TABLE calc_words;"]: db.query(q) fakebot.loadmodule("Calc") return fakebot diff --git a/tests/modules/test_modinfo.py b/tests/modules/test_modinfo.py index c76abd4..44e76b7 100644 --- a/tests/modules/test_modinfo.py +++ b/tests/modules/test_modinfo.py @@ -1,19 +1,29 @@ import pytest from tests.lib import * # NOQA - fixtures +from unittest.mock import call @pytest.fixture -def bot(fakebot): +def helpbot(fakebot): + """ + Provide a bot loaded with the ModInfo module + """ fakebot.loadmodule("ModInfo") return fakebot -def test_help(bot): - bot.feed_line(".help") - bot.act_PRIVMSG.assert_called_once_with('#test', - 'ModInfo: .help [command] show the manual for all or [commands]') +def test_helpindex(helpbot): + helpbot.feed_line(".helpindex") + helpbot.act_PRIVMSG.assert_called_once_with('#test', 'chatter: commands: .help, .helpindex') -def test_helpindex(bot): - bot.feed_line(".helpindex") - bot.act_PRIVMSG.assert_called_once_with('#test', 'chatter: commands: .help') +def test_help(helpbot): + helpbot.feed_line(".help") + helpbot.act_PRIVMSG.assert_has_calls([call('#test', 'ModInfo: .help [command] show the manual for all or [commands]'), + call('#test', 'ModInfo: .helpindex show a short list of all commands')], + any_order=True) + + +def test_help_one(helpbot): + helpbot.feed_line(".help .helpindex") + helpbot.act_PRIVMSG.assert_called_once_with('#test', 'RTFM: .helpindex: helpindex show a short list of all commands') diff --git a/tests/modules/test_nickuser.py b/tests/modules/test_nickuser.py index d6b014c..d74f8d6 100644 --- a/tests/modules/test_nickuser.py +++ b/tests/modules/test_nickuser.py @@ -19,9 +19,9 @@ def nickbot(fakebot): "delayMatch": 0} fakebot.loadmodule("SQLite") with closing(fakebot.moduleInstances["SQLite"].opendb("attributes.db")) as db: - for q in ["DELETE FROM attribute;", - "DELETE FROM items;", - "DELETE FROM `values`;"]: + for q in ["DROP TABLE attribute;", + "DROP TABLE items;", + "DROP TABLE `values`;"]: db.query(q) fakebot.loadmodule("AttributeStorageLite") fakebot.loadmodule("NickUser") @@ -56,6 +56,12 @@ def test_badpass(nickbot): nickbot.act_PRIVMSG.assert_called_once_with('chatter', '.login: incorrect password.') +def test_change_needspass(nickbot): + test_register(nickbot) + pm(nickbot, ".setpass oopsie") + nickbot.act_PRIVMSG.assert_called_once_with('chatter', '.setpass: You must provide the old password when setting a new one.') + + def test_logout(nickbot): test_register_login(nickbot) pm(nickbot, ".logout") @@ -72,3 +78,12 @@ def test_changepass(nickbot): nickbot.act_PRIVMSG.reset_mock() pm(nickbot, ".setpass wrong newpass2") nickbot.act_PRIVMSG.assert_called_once_with('chatter', '.setpass: Old password incorrect.') + + +def test_check(nickbot): + test_register_login(nickbot) + mod = nickbot.moduleInstances["NickUser"] + assert mod.check("chatter", "cia.gov") + assert not mod.check("chatter", "not-valid.hostname") + pm(nickbot, ".logout") + assert not mod.check("chatter", "cia.gov")