From 0afb568caa60ed2bf8580861cee9fa7ebe425cdd Mon Sep 17 00:00:00 2001 From: arj Date: Tue, 19 Feb 2019 15:09:07 +0400 Subject: [PATCH] added tests --- honeybot/CONNECT.conf | 5 ++ honeybot/PLUGINS.conf | 3 +- honeybot/lab.py | 4 ++ honeybot/main.py | 23 +++++---- honeybot/plugins/debug.py | 30 +++++++++++ honeybot/test.py | 104 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 159 insertions(+), 10 deletions(-) create mode 100644 honeybot/lab.py create mode 100644 honeybot/plugins/debug.py create mode 100644 honeybot/test.py diff --git a/honeybot/CONNECT.conf b/honeybot/CONNECT.conf index e69de29..a949dca 100644 --- a/honeybot/CONNECT.conf +++ b/honeybot/CONNECT.conf @@ -0,0 +1,5 @@ +[INFO] + +server_url = chat.freenode.net +port = 6667 +name = appinventormuBot \ No newline at end of file diff --git a/honeybot/PLUGINS.conf b/honeybot/PLUGINS.conf index 3c2ede3..e83625b 100644 --- a/honeybot/PLUGINS.conf +++ b/honeybot/PLUGINS.conf @@ -4,4 +4,5 @@ joke maths calc conv_sniff -caesar_cipher \ No newline at end of file +caesar_cipher +debug \ No newline at end of file diff --git a/honeybot/lab.py b/honeybot/lab.py new file mode 100644 index 0000000..aef4f55 --- /dev/null +++ b/honeybot/lab.py @@ -0,0 +1,4 @@ +from main import Bot_core as Bot + +bot = Bot() +print(bot.specific_send_command('#abc', 'greetings')) \ No newline at end of file diff --git a/honeybot/main.py b/honeybot/main.py index 77046a9..7f95c9e 100644 --- a/honeybot/main.py +++ b/honeybot/main.py @@ -4,15 +4,18 @@ import socket import os import importlib +import configparser +config = configparser.ConfigParser() +config.read('CONNECT.conf') plugins = [] class Bot_core(object): def __init__(self, - server_url='chat.freenode.net', - port=6667, - name='appinventormuBot', + server_url=config['INFO']['server_url'], + port=int(config['INFO']['port']), + name=config['INFO']['name'], owners=['appinventorMu', 'appinv'], password='', friends=['haruno', 'keiserr', 'loganaden'], @@ -43,7 +46,7 @@ def set_nick_command(self): return 'NICK ' + self.name + '\r\n' def present_command(self): - return 'USER ' + self.name + ' ' + self.name + ' ' + + return 'USER ' + self.name + ' ' + self.name + ' ' +\ self.name + ' : ' + self.name + ' IRC\r\n' def identify_command(self): @@ -131,6 +134,9 @@ def methods(self): } def run_plugins(self, listfrom, incoming): + ''' + incoming is the unparsed string. refer to test.py + ''' for plugin in listfrom: plugin.run(incoming, self.methods(), self.info(incoming)) @@ -173,8 +179,7 @@ def pull(self): """.format(msg)) if len(data) == 0: try: - self.irc.close() - self.registered_run() + print('') except Exception as e: print(e) except Exception as e: @@ -210,6 +215,6 @@ def stay_alive(self, incoming): '''.format(part[1])) self.irc.recv(2048).decode("UTF-8") - -x = Bot_core() -x.registered_run() +if __name__ == '__main__': + x = Bot_core() + x.registered_run() diff --git a/honeybot/plugins/debug.py b/honeybot/plugins/debug.py new file mode 100644 index 0000000..93e4c3d --- /dev/null +++ b/honeybot/plugins/debug.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +""" +[debug.py] +Debug plugin : Un + +[Author] +Abdur-Rahmaan Janhangeer, pythonmembers.club + +[About] +prints all parameters passed to bot + +[Commands] +>>> .debug +prints all parameters +""" + + +class Plugin: + def __init__(self): + pass + + def run(self, incoming, methods, info): + try: + # if '!~' in info['prefix']: + # print(info) + if info['command'] == 'PRIVMSG' and info['args'][1] == '.debug': + methods['send'](info['address'], info['prefix']) + except Exception as e: + print('woops plug', e) + diff --git a/honeybot/test.py b/honeybot/test.py new file mode 100644 index 0000000..182218f --- /dev/null +++ b/honeybot/test.py @@ -0,0 +1,104 @@ +import unittest +import configparser +from main import Bot_core as Bot +''' +':appinv!c5e342c5@gateway/web/cgi-irc/kiwiirc.com/ip.197.227.66.197 PRIVMSG ##bottestingmu :ef' +{ +'prefix': 'appinv!c5e342c5@gateway/web/cgi-irc/kiwiirc.com/ip.197.227.66.197', +'command': 'PRIVMSG', +'address': '##bottestingmu', +'args': ['##bottestingmu', 'ef'] +} +''' +config = configparser.ConfigParser() +config.read('CONNECT.conf') + +# incoming +incoming = ':appinv!c5e342c5@gateway/web/cgi-irc/kiwiirc.com/ip.200.200.22.200 PRIVMSG ##bottestingmu :ef' +bot = Bot() + + +class HoneybotTests(unittest.TestCase): + ''' + basic info + ''' + def test_name(self): + self.assertEqual( + bot.name, + config['INFO']['name']) + + def test_server_url(self): + self.assertEqual( + bot.server_url, + config['INFO']['server_url']) + + def test_port(self): + self.assertEqual( + bot.port, + int(config['INFO']['port'])) + ''' + info function + ''' + def test_info_prefix(self): + self.assertEqual( + bot.info(incoming)['prefix'], + 'appinv!c5e342c5@gateway/web/cgi-irc/kiwiirc.com/ip.200.200.22.200') + + def test_info_command(self): + self.assertEqual( + bot.info(incoming)['command'], + 'PRIVMSG') + + def test_info_address(self): + self.assertEqual( + bot.info(incoming)['address'], + '##bottestingmu') + + def test_info_args(self): + self.assertEqual( + bot.info(incoming)['args'], + ['##bottestingmu', 'ef']) + + ''' + commands + ''' + def test_set_nick_command(self): + self.assertEqual( + bot.set_nick_command(), + 'NICK {0}\r\n'.format(config['INFO']['name']) + ) + + def test_present_command(self): + self.assertEqual( + bot.present_command(), + 'USER {0} {0} {0} : {0} IRC\r\n'.format(config['INFO']['name']) + ) + + def test_identify_command(self): + self.assertEqual( + bot.identify_command(), + 'msg NickServ identify \r\n' + ) + + def test_join_command(self): + self.assertEqual( + bot.join_channel_command('#abc'), + 'JOIN #abc \r\n' + ) + + def test_specific_send_command(self): + self.assertEqual( + bot.specific_send_command('#abc', 'greetings'), + 'PRIVMSG #abc :greetings\r\n' + ) + + def test_pong_return(self): + self.assertEqual( + bot.pong_return(), + 'PONG \r\n' + ) + + + +if __name__ == '__main__': + unittest.main()