예제 #1
0
def send_irc_msg(event, context):  # pragma: no cover
    logger.debug("Received an SNS event: %s" % json.dumps(event))

    # This is going to hurt if there are more than one irc messages needing to
    # be delivered, in the same SNS payload. Something to optimize for perhaps
    # later.

    for record in event['Records']:
        msg_str = record['Sns'].get('Message')
        msg_json = json.loads(msg_str)
        logger.debug("Record: %s" % msg_json)

        irc_client = IRCClient(network=msg_json.get('irc-server'),
                               port=msg_json.get('irc-port'),
                               nickname=msg_json.get('irc-nickname'))
        irc_client.connect()
        irc_client.send_msg(msg=msg_json.get('irc-message'),
                            channel=msg_json.get('irc-channel'))
class TestIRCClient(unittest.TestCase):

    def setUp(self):
        self.client = IRCClient("foo.bar.net",
                                1234,
                                "nickname")
        self.client.main_loop = MagicMock()
        self.client.server = MagicMock()

    def test_nicknames(self):
        self.assertEqual(self.client.nickname, "nickname")
        self.client = IRCClient("foo.bar.net", 1234, "")
        self.assertEqual(self.client.nickname, "irchooky")

    def test_send_empty_msg(self):
        with self.assertRaises(Exception):
            self.client.send_msg("", "#channel")

    def test_send_empty_channel(self):
        with self.assertRaises(Exception):
            self.client.send_msg("message", "")

    def test_send_msg(self):
        self.client.send_msg("message", "#channel")
        self.assertEqual(self.client.main_loop.mock_calls, [call()])
        self.assertEqual(len(self.client.server.mock_calls), 8)
예제 #3
0
class TestIRCClient(unittest.TestCase):
    def setUp(self):
        self.client = IRCClient("foo.bar.net", 1234, "nickname")
        self.client.main_loop = MagicMock()
        self.client.server = MagicMock()

    def test_nicknames(self):
        self.assertEqual(self.client.nickname, "nickname")
        self.client = IRCClient("foo.bar.net", 1234, "")
        self.assertEqual(self.client.nickname, "irchooky")

    def test_send_empty_msg(self):
        with self.assertRaises(Exception):
            self.client.send_msg("", "#channel")

    def test_send_empty_channel(self):
        with self.assertRaises(Exception):
            self.client.send_msg("message", "")

    def test_send_msg(self):
        self.client.send_msg("message", "#channel")
        self.assertEqual(self.client.main_loop.mock_calls, [call()])
        self.assertEqual(len(self.client.server.mock_calls), 8)