async def _relay(bot, listener, target, contents, source): targets = _get_linked_targets(listener, target) for target in targets: target_listener = get_listener(bot, target['listener']) for (author, message) in _get_relay_messages( listener, contents, source ): await _relay_message(target_listener, bot, target, author, message)
async def _handle_request(bot, request, **kwargs): try: logger.info("Received request from GitHub webhook.") if not _check_signature(await request.text(), request.headers): logger.info("GitHub request contained invalid signature, ignoring.") raise BotError("Invalid signature.") payload = await request.json() repo_cfgs = _get_repo_cfgs(payload["repository"]["id"]) logger.info('Event for repository {payload["repository"]["name"]}.') event_handler = _get_event_handler(request.headers) for cfg in repo_cfgs: asyncio.ensure_future( event_handler(get_listener(bot, cfg["listener"]), payload, cfg) ) return web.Response(body="Received.") except InvalidListener: return web.Response(body="Invalid listener configured.", status=500) except BotError as e: return web.Response(body=e.args[0], status=500)
def test_get_listener_discord(): bot = Mock(discord_listener=1, irc_listeners={'a': 2, 'b': 3}) assert 1 == get_listener(bot, 'DiscordListener')
def test_get_listener_nonexistent(): with pytest.raises(InvalidListener): get_listener(Mock(), 'AppleListener')
def test_get_listener_irc_nonexistent(): bot = Mock(discord_listener=1, irc_listeners={'a': 2, 'b': 3}) with pytest.raises(InvalidListener): get_listener(bot, 'IRCListener@c')
def test_get_listener_irc(): bot = Mock(discord_listener=1, irc_listeners={'a': 2, 'b': 3}) assert 2 == get_listener(bot, 'IRCListener@a')
def test_get_listener_discord(): bot = Mock(discord_listener=1, irc_listeners={"a": 2, "b": 3}) assert 1 == get_listener(bot, "DiscordListener")
def test_get_listener_irc_nonexistent(): bot = Mock(discord_listener=1, irc_listeners={"a": 2, "b": 3}) with pytest.raises(InvalidListener): get_listener(bot, "IRCListener@c")
def test_get_listener_irc(): bot = Mock(discord_listener=1, irc_listeners={"a": 2, "b": 3}) assert 2 == get_listener(bot, "IRCListener@a")