def test_from_json(tmp_file): # Write json to tmp file val = '{"key": "value", "dkey": "new"}' with open(tmp_file, 'w') as f: f.write(val) # Defaults defaults = {'dkey': 'dvalue'} c = Config(defaults) assert c['dkey'] == 'dvalue' # Should override defaults c.from_json_file(tmp_file) assert c['key'] == 'value' assert c['dkey'] == 'new'
def test_is_command(): re = BaseResponder(Config()) msg = make_message('#channel :.test') assert re.is_command(msg) msg = make_message('#channel :.test arg1 arg2') assert re.is_command(msg) msg = make_message('#channel :.test arg1 arg2') assert re.is_command(msg, 'test') msg = make_message('#channel :.testing arg1 arg2') assert re.is_command(msg, 'testing') assert not re.is_command(msg, 'test') msg = make_message('#channel :.testing') assert re.is_command(msg, 'testing') assert not re.is_command(msg, 'test') msg = make_message('#channel :.') assert not re.is_command(msg, 'test') msg = make_message('#channel ::test') assert re.is_command(msg, 'test', ':') assert re.is_command(msg, 'test', command_prefix=':') assert not re.is_command(msg, 'test') msg = make_message('#channel : ') assert not re.is_command(msg, 'test')
def test_admin_dispatching(): from botnet.modules.builtin.admin import Admin from modules.builtin.test_admin import admin_make_message, data4, send_data def make_admin_config(command_prefix='.'): config = { 'module_config': { 'botnet': { 'admin': { 'admins': ['nick4'] } } } } config = Config(config) return config admin_config = make_admin_config() t = Tester(Config()) ad = Admin(admin_config) msg = make_privmsg('#channel :.test') message_in.send(None, msg=msg) assert t.launched_main assert t.launched_command assert t.launched_priv assert not t.launched_admin_command assert not t.launched_admin_priv msg = admin_make_message('nick4', '.test') message_in.send(None, msg=msg) send_data(data4) assert t.launched_admin_command assert t.launched_admin_priv
def test_tell(request, tmp_file): m = TestTell(tmp_file, Config()) def teardown(): m.stop() request.addfinalizer(teardown) return m
def test_help(msg_t): """Test help command. Only Meta module should respond to that command without any parameters.""" msg = make_message('#channel :.help') re = BaseResponder(Config()) message_in.send(None, msg=msg) assert not msg_t.msg
def make_config(): config = { 'module_config': { 'botnet': { 'irc': {} } } } return Config(config)
def test_response(resource_path, msg_t): a = A(resource_path('events.json')) a._last_events['boreq/botnet'] = 0 g = G(Config()) g.api = a g.update() assert 'commits' in str(msg_t.msg)
def test_dispatching_prefix(): class PrefixTester(Tester): def get_command_prefix(self): return ':' msg = make_privmsg('#channel ::test arg1 arg2') t = PrefixTester(Config()) message_in.send(None, msg=msg) assert_normal(t)
def test_admin(rec_admin_msg, make_privmsg): g = Github(Config()) msg = make_privmsg('.github_track owner repo #channel') rec_admin_msg(msg) assert g.config_get('track') msg = make_privmsg('.github_untrack owner repo #channel') rec_admin_msg(msg) assert not g.config_get('track')
def test_unsubscribe_from_all(cl): r = BaseResponder(Config()) assert signals.message_in.receivers assert signals.admin_message_in.receivers signals.unsubscribe_from_all(r) assert not signals.message_in.receivers assert not signals.admin_message_in.receivers
def test_admin_multiple(rec_admin_msg, make_privmsg): g = Github(Config()) msg = make_privmsg('.github_track owner repo1 #channel') rec_admin_msg(msg) msg = make_privmsg('.github_track owner repo2 #channel') rec_admin_msg(msg) assert len(g.config_get('track')) == 2 msg = make_privmsg('.github_untrack owner repo1 #channel') rec_admin_msg(msg) assert len(g.config_get('track')) == 1
def make_admin_config(command_prefix='.'): config = { 'module_config': { 'botnet': { 'admin': { 'admins': ['nick4'] } } } } config = Config(config) return config
def test_admin_all_gone(rec_admin_msg, make_privmsg): g = Github(Config()) msg = make_privmsg('.github_track owner repo #channel1') rec_admin_msg(msg) msg = make_privmsg('.github_track owner repo #channel2') rec_admin_msg(msg) assert len(g.config_get('track')[0]['channels']) == 2 msg = make_privmsg('.github_untrack owner repo') rec_admin_msg(msg) assert not g.config_get('track')
def test_mod(tmp_file, msg_t, make_privmsg, rec_msg): class TestTell(Tell): default_config = {'message_data': tmp_file} m = TestTell(Config()) msg = make_privmsg('.tell target message text', nick='author') rec_msg(msg) assert 'Will do' in str(msg_t.msg) msg = make_privmsg('sth', nick='target') rec_msg(msg) assert 'message text' in str(msg_t.msg)
def test_quotes(msg_t, make_privmsg): dirname = os.path.dirname(os.path.realpath(__file__)) filename = os.path.join(dirname, 'quotes') q = Quotes(Config()) msg = make_privmsg('.lotr') message_in.send(None, msg=msg) assert not msg_t.msg q.config_set('lotr', filename) message_in.send(None, msg=msg) assert msg_t.msg
def test_respond(msg_t): """Test if BaseResponder.respond sends messages to proper targets.""" params = ( ('#channel :test message', '#channel', False), ('bot_nick :test message', 'nick', False), ('#channel :test message', 'nick', True), ('bot_nick :test message', 'nick', True), ) re = BaseResponder(Config()) for text, target, pm in params: msg_t.reset() msg = make_message(text) re.respond(msg, 'response', pm=pm) assert msg_t.msg.params[0] == target
def test_decorator_dont_launch(): class TestResponder(BaseResponder): def __init__(self, config): super(TestResponder, self).__init__(config) self.args = None @parse_command([('test_type', 1), ('something', '+')], launch_invalid=False) def command_test(self, msg, args): self.args = True msg = make_message('#channel :.test') re = TestResponder(Config()) message_in.send(None, msg=msg) assert re.args is None
def test_decorator(): class TestResponder(BaseResponder): def __init__(self, config): super(TestResponder, self).__init__(config) self.args = None @parse_command([('test_type', 1), ('something', '+')]) def command_test(self, msg, args): self.args = args msg = make_message('#channel :.test awesome one two three') re = TestResponder(Config()) message_in.send(None, msg=msg) assert re.args.command == ['.test'] assert re.args.test_type == ['awesome'] assert re.args.something == ['one', 'two', 'three']
def test_specific(msg_l): """Test help regarding a specific command.""" class Responder(BaseResponder): def command_test(self, msg): pass def admin_command_test(self, msg): pass msg = make_message('#channel :.help test') re = Responder(Config()) message_in.send(None, msg=msg) assert len(msg_l.msgs) == 1 message_in.send(None, msg=msg) assert len(msg_l.msgs) == 2
def test_simple_module(msg_t, make_privmsg, rec_msg): re = SimpleModule(Config()) msg = make_privmsg('.respond') rec_msg(msg) assert 'Responding' in str(msg_t.msg) msg = make_privmsg('.hi') rec_msg(msg) assert 'Hello' in str(msg_t.msg) msg = make_privmsg('.say text') rec_msg(msg) assert 'told me to ' in str(msg_t.msg) msg = make_privmsg('.say') rec_msg(msg) assert 'forgot' in str(msg_t.msg)
def test_config_gone(): class TestResponder(ConfigMixin, BaseModule): a = {} def __init__(self, config): super(TestResponder, self).__init__(config) self.register_default_config(self.a) self.register_config('namespace_a', 'module_name') self.register_config('namespace_b', 'module_name') t = TestResponder(Config()) with pytest.raises(KeyError): assert t.config_get('k') == 'v' assert t.config_set('k', 'v') assert t.config_get('k') == 'v' t.config_append('gone', 1) assert t.config_get('gone') == [1]
def test_default_config(): class TestResponder(ConfigMixin, BaseModule): a = { 'overwrite': { 'a': 'a', 'b': 'b', }, 'onlya': 'v' } b = { 'overwrite': { 'b': 'o', }, 'onlyb': 'v' } def __init__(self, config): super(TestResponder, self).__init__(config) self.register_default_config(self.a) self.register_default_config(self.b) t = TestResponder(Config()) # get assert t.config_get('onlya') == 'v' assert t.config_get('onlyb') == 'v' with pytest.raises(ValueError): assert t.config_get('onlya.invalid') == 'invalid' with pytest.raises(KeyError): assert t.config_get('invalid') == 'invalid' assert t.config_get('overwrite.a') == 'a' assert t.config_get('overwrite.b') == 'o' # set with pytest.raises(ValueError): t.config_set('new_key.a', 'v')
def get_config(): config = { 'modules': ['a', 'b'], 'module_config': { 'namespace_a': { 'module_name': { 'overwrite': { 'd': 'invalid', 'c': 'o', }, 'only_namespace_a': 'v' } }, 'namespace_b': { 'module_name': { 'overwrite': { 'd': 'o', }, 'only_namespace_b': 'v' } } } } return Config(config)
def test_dispatching(): t = Tester(Config()) msg = make_privmsg('#channel :.test') message_in.send(None, msg=msg) assert_normal(t)
def test_quotes_gone(make_privmsg): q = Quotes(Config()) msg = make_privmsg('.gone') q.config_set('gone', 'gone') message_in.send(None, msg=msg)
def make_config(): config = {'module_config': {'base_responder': {'command_prefix': '.'}}} config = Config(config) return config
def test_defaults(): defaults = {'key': 'value'} c = Config(defaults) assert c['key'] == 'value'
def test_base(): c = Config() assert c == {}
def test_dispatching_args(): msg = make_privmsg('#channel :.test arg1 arg2') t = Tester(Config()) message_in.send(None, msg=msg) assert_normal(t)