def test_send_recv(self): agent = StubAgent(self.bot) mod = StubModule(self.bot) self.bot.add_agent_instance('stub_agent', agent) self.bot.add_module_instance('stub_mod', mod) foo = halibot.Message(body='foo') bar = halibot.Message(body='bar') baz = halibot.Message(body='baz', origin='glub_agent') agent.connect(mod) agent.dispatch(foo) agent.send_to(bar, ['stub_mod']) agent.dispatch(baz) # TODO do something sensible here timeout = 10 increment = .1 while timeout > 0 and len(mod.received) != 3: time.sleep(increment) timeout -= increment self.assertEqual(3, len(mod.received)) self.assertEqual(foo.body, mod.received[0].body) self.assertEqual(bar.body, mod.received[1].body) self.assertEqual(baz.body, mod.received[2].body) self.assertEqual('stub_mod', mod.received[0].target) self.assertEqual('stub_mod', mod.received[1].target) self.assertEqual('stub_mod', mod.received[2].target) self.assertEqual('stub_agent', mod.received[0].origin) self.assertEqual('stub_agent', mod.received[1].origin) self.assertEqual('glub_agent', mod.received[2].origin)
def test_hasperm_dec_fail(self): self.bot.auth.enabled = True stub = StubModuleDecFail(self.bot) self.bot.add_instance('stub_mod', stub) ri = "test/foobar" user = "******" perm = "Foo" msg_bad = halibot.Message(body="", origin="", identity=user) msg_good = halibot.Message(body="", origin=ri, identity=user) # Empty origin stub.temp = stub.valid try: stub.receive(msg_bad) self.assertTrue(False) # We should catch an Exception here except halibot.message.MalformedMsgException: pass self.assertFalse(stub.called) self.bot.auth.grantPermission(ri, user, perm) try: stub.receive(msg_bad) self.assertTrue(False) # We should catch an Exception here except halibot.message.MalformedMsgException: pass self.assertFalse(stub.called) stub.called = False self.bot.auth.revokePermission(ri, user, perm) try: stub.receive(msg_bad) self.assertTrue(False) # We should catch an Exception here except halibot.message.MalformedMsgException: pass self.assertFalse(stub.called) # Not actually a message object, missing identity stub.temp = stub.not_msg_entry stub.called = False try: stub.receive(msg_good) self.assertTrue(False) # We should catch an Exception here except halibot.message.MalformedMsgException: pass self.assertFalse(stub.called) # Bad arg key stub.temp = stub.no_msg_key stub.called = False try: stub.receive(msg_good) self.assertTrue(False) # We should catch an Exception here except KeyError: pass self.assertFalse(stub.called)
def test_inbound_filter(self): # --- Borrowed from test_send_reply --- agent = StubAgent(self.bot) mod = StubReplier(self.bot) filter = StubFilter(self.bot) self.bot.add_instance('stub_agent', agent) self.bot.add_instance('stub_module', mod) self.bot.add_instance('stub_filter', filter) self.assertTrue(agent.inited) self.assertTrue(mod.inited) self.assertTrue(filter.inited) self.assertTrue(agent.eventloop.is_running()) self.assertTrue(mod.eventloop.is_running()) self.assertTrue(filter.eventloop.is_running()) foo = halibot.Message(body='foo') # --- end borrow --- self.bot.config["filters"] = { "inbound": { "stub_module": ["stub_filter"] } } agent.send_to(foo, ['stub_module']) util.waitOrTimeout(50, lambda: len(agent.received) != 0) self.assertEqual(len(agent.received), 1) self.assertEqual(agent.received[0].body, "foobar") self.assertTrue(filter.ran)
def test_drop_filter(self): agent = StubAgent(self.bot) mod = StubReplier(self.bot) filter = StubRejectFilter(self.bot) self.bot.add_instance('stub_agent', agent) self.bot.add_instance('stub_module', mod) self.bot.add_instance('stub_filter', filter) self.assertTrue(agent.inited) self.assertTrue(mod.inited) self.assertTrue(filter.inited) self.assertTrue(agent.eventloop.is_running()) self.assertTrue(mod.eventloop.is_running()) self.assertTrue(filter.eventloop.is_running()) foo = halibot.Message(body='foo') self.bot.config["filters"] = { "inbound": { "stub_module": ["stub_filter"] } } filter.accept = False agent.send_to(foo, ['stub_module']) time.sleep(1) self.assertEqual(len(agent.received), 0) filter.accept = True agent.send_to(foo, ['stub_module']) util.waitOrTimeout(50, lambda: len(agent.received) != 0) self.assertEqual(len(agent.received), 1)
def test_hasperm_func(self): self.bot.auth.enabled = True stub = StubModuleFunc(self.bot) self.bot.add_instance('stub_mod', stub) ri = "test/foobar" user = "******" perm = "Foo" msg = halibot.Message(body="", origin=ri, identity=user) stub.receive(msg) self.assertFalse(stub.called) self.bot.auth.grantPermission(ri, user, perm) stub.receive(msg) self.assertTrue(stub.called) stub.called = False self.bot.auth.revokePermission(ri, user, perm) stub.receive(msg) self.assertFalse(stub.called) # NOTE: This assumes that enabled = allow all perms. # Is this really expected behavior? stub.called = False self.bot.auth.enabled = False self.bot.auth.revokePermission(ri, user, perm) stub.receive(msg) self.assertTrue(stub.called)
def test_sync_send(self): agent = StubAgent(self.bot) mod = StubReplier(self.bot) self.bot.add_instance('stub_agent', agent) self.bot.add_instance('stub_module', mod) foo = halibot.Message(body='foo') rep = agent.sync_send_to(foo, ['stub_module']) self.assertEqual(rep["stub_module"][0].body, "foobar")
def test_help(self): agent = StubAgent(self.bot) mod = StubModule(self.bot) self.bot.add_instance('stub_agent', agent) self.bot.add_instance('stub_module', mod) msgt0 = halibot.Message(type='help', body=[]) msgt1 = halibot.Message(type='help', body=['topic1']) msgt2 = halibot.Message(type='help', body=['topic2']) self.assertEqual( set( agent.sync_send_to(msgt0, ['stub_module'])['stub_module'][0].body), set(['topic1', 'topic2'])) self.assertEqual( agent.sync_send_to(msgt1, ['stub_module'])['stub_module'][0].body, topic1_text) self.assertEqual( agent.sync_send_to(msgt2, ['stub_module'])['stub_module'][0].body, topic2_text)
def test_hasperm_regex(self): self.bot.auth.enabled = True stub = StubModuleDec(self.bot) self.bot.add_instance('stub_mod', stub) ri = "test/foobar" user = "******" perm = "Foo" msg = halibot.Message(body="", origin=ri, identity=user) self.bot.auth.grantPermission(".*", user, ".*") stub.receive(msg) self.assertTrue(stub.called)
def test_send_reply(self): agent = StubAgent(self.bot) mod = StubReplier(self.bot) self.bot.add_instance('stub_agent', agent) self.bot.add_instance('stub_module', mod) self.assertTrue(agent.inited) self.assertTrue(mod.inited) self.assertTrue(agent.eventloop.is_running()) foo = halibot.Message(body='foo') agent.send_to(foo, ['stub_module']) util.waitOrTimeout(100, lambda: len(agent.received) != 0) self.assertEqual(len(agent.received), 1) self.assertEqual(agent.received[0].body, "foobar")
def test_hasperm_dec(self): self.bot.auth.enabled = True stub = StubModuleDec(self.bot) self.bot.add_module_instance('stub_mod', stub) ri = "test/foobar" user = "******" perm = "Foo" msg = halibot.Message(body="", origin=ri, author=user) stub.receive(msg) self.assertFalse(stub.called) self.bot.auth.grantPermission(ri, user, perm) stub.receive(msg) self.assertTrue(stub.called)
def test_send_recv(self): agent = StubAgent(self.bot) mod = StubModule(self.bot) mod2 = StubModule(self.bot) self.bot.add_instance('stub_agent', agent) self.bot.add_instance('stub_mod', mod) self.bot.add_instance('stub_mod2', mod2) # mod should receive: foo, bar, baz, qua # mod2 should receive: foo, bar, qua foo = halibot.Message(body='foo') bar = halibot.Message(body='bar') baz = halibot.Message(body='baz', origin='glub_agent') qua = halibot.Message(body='qua') qua2 = halibot.Message(body='qua', type='mytype') agent.dispatch(foo) # 0 agent.send_to(bar, ['stub_mod/able', 'stub_mod2/baker']) # 1 agent.connect(mod) agent.dispatch(baz) # 2 agent.connect(mod2) agent.dispatch(qua) # 3 agent.dispatch(qua2) # 3 util.waitOrTimeout( 100, lambda: len(mod.received) == 4 and len(mod2.received) == 3) # Check mod received mesages self.assertEqual(4, len(mod.received)) self.assertEqual(foo.body, mod.received[0].body) self.assertEqual(bar.body, mod.received[1].body) self.assertEqual(baz.body, mod.received[2].body) self.assertEqual(qua.body, mod.received[3].body) self.assertEqual('', mod.received[0].whom()) self.assertEqual('able', mod.received[1].whom()) self.assertEqual('', mod.received[2].whom()) self.assertEqual('', mod.received[3].whom()) self.assertEqual('stub_mod', mod.received[0].target) self.assertEqual('stub_mod/able', mod.received[1].target) self.assertEqual('stub_mod', mod.received[2].target) self.assertEqual('stub_mod', mod.received[3].target) self.assertEqual('stub_agent', mod.received[0].origin) self.assertEqual('stub_agent', mod.received[1].origin) self.assertEqual('glub_agent', mod.received[2].origin) self.assertEqual('stub_agent', mod.received[3].origin) # Check mod2 received mesages self.assertEqual(3, len(mod2.received)) self.assertEqual(foo.body, mod2.received[0].body) self.assertEqual(bar.body, mod2.received[1].body) self.assertEqual(qua.body, mod2.received[2].body) self.assertEqual('', mod.received[0].whom()) self.assertEqual('able', mod.received[1].whom()) self.assertEqual('', mod.received[2].whom()) self.assertEqual('stub_mod2', mod2.received[0].target) self.assertEqual('stub_mod2/baker', mod2.received[1].target) self.assertEqual('stub_mod2', mod2.received[2].target) self.assertEqual('stub_agent', mod2.received[0].origin) self.assertEqual('stub_agent', mod2.received[1].origin) self.assertEqual('stub_agent', mod2.received[2].origin) # Check mytype messages self.assertEqual(1, len(mod.received_mytype)) self.assertEqual(1, len(mod2.received_mytype)) self.assertEqual(qua2.body, mod.received_mytype[0].body) self.assertEqual(qua2.body, mod2.received_mytype[0].body)
import util import halibot import unittest testMessage = halibot.Message(body="foo", type="bar", author="Foo Barrington", identity="yes", origin="Assyria", misc="[\"foo\", \"bar\"]", target="bees?") class TestMessage(util.HalibotTestCase): def testCreation(self): self.assertEqual("foo", testMessage.body) self.assertEqual("bar", testMessage.type) self.assertEqual("Foo Barrington", testMessage.author) self.assertEqual("yes", testMessage.identity) self.assertEqual("Assyria", testMessage.origin) self.assertEqual("[\"foo\", \"bar\"]", testMessage.misc) self.assertEqual("bees?", testMessage.target) # Test that repr will eval to a duplicate message def testRepr(self): m = eval(testMessage.__repr__()) self.assertEqual("foo", m.body) self.assertEqual("bar", m.type) self.assertEqual("Foo Barrington", m.author) self.assertEqual("yes", m.identity) self.assertEqual("Assyria", m.origin) self.assertEqual("[\"foo\", \"bar\"]", m.misc) self.assertEqual("bees?", m.target) if __name__ == '__main__': unittest.main()