async def test_search_with_api(f_bot): api = cog.inara.InaraApi() f_bot.wait_for.async_return_value = fake_msg_gears('stop') cog.util.BOT = f_bot f_msg = fake_msg_gears('!whois gearsandcogs') cmdr = await api.search_with_api('gearsandcogs', f_msg) assert cmdr['name'] == "GearsandCogs" assert cmdr["req_id"] == 0
async def test_select_from_multiple_stop(f_bot): api = cog.inara.InaraApi() msg = fake_msg_gears(cog.inara.BUT_CANCEL) f_bot.wait_for.async_return_value = Interaction( 'multiple_stop', message=msg, user=msg.author, comp_label=cog.inara.BUT_CANCEL) cog.util.BOT = f_bot with pytest.raises(cog.exc.CmdAborted): await api.search_with_api('gears', fake_msg_gears('!whois gears'))
async def test_rate_limiter_decrement(f_bot): msg = fake_msg_gears("!fort") rate_limit = cog.inara.RateLimiter(max_rate=12, resume_rate=9) await rate_limit.increment(f_bot, msg.channel) assert rate_limit.rate == 1 await rate_limit.decrement(delay=1) assert rate_limit.rate == 0
async def test_rate_limiter_increment_wait(f_bot): msg = fake_msg_gears("!fort") rate_limit = cog.inara.RateLimiter(max_rate=2, resume_rate=1) await rate_limit.increment(f_bot, msg.channel) assert rate_limit.rate == 1 asyncio.ensure_future(rate_limit.increment(f_bot, msg.channel)) await asyncio.sleep(1) assert rate_limit.rate == 2
async def test_select_from_multiple_exact(f_bot): api = cog.inara.InaraApi() msg = fake_msg_gears('stop') f_bot.wait_for.async_side_effect = [ Interaction('reply_api', message=msg, user=msg.author, comp_label='GearsandCogs'), Interaction('reply_api', message=msg, user=msg.author, comp_label=cog.inara.BUT_DENY), ] cog.util.BOT = f_bot cmdr = await api.search_with_api('gears', fake_msg_gears('!whois gears')) assert cmdr["name"] == "GearsandCogs" with pytest.raises(KeyError): cmdr["otherNamesFound"] # pylint: disable=pointless-statement
async def test_reply_with_api_result(f_bot): api = cog.inara.InaraApi() msg = fake_msg_gears('stop') comp = aiomock.Mock() comp.label = cog.inara.BUT_CANCEL f_bot.wait_for.async_return_value = Interaction('reply_api', message=msg, user=msg.author, component=comp) f_bot.send_message.async_return_value = fake_msg_gears( "A message to send.") cog.util.BOT = f_bot f_msg = fake_msg_gears('!whois gearsandcogs') cmdr = await api.search_with_api('gearsandcogs', f_msg) await api.reply_with_api_result(cmdr["req_id"], cmdr["event_data"], f_msg) # TODO: Very lazy check :/ assert 'Should the CMDR GearsandCogs be added as friendly or hostile?' in str( f_bot.send_message.call_args)
async def test_rate_limiter_decrement_wait(f_bot): rate_limit = cog.inara.RateLimiter(max_rate=2, resume_rate=1) msg = fake_msg_gears("!fort") async def run_test(): await rate_limit.increment(f_bot, msg.channel) await rate_limit.decrement(delay=2) await asyncio.gather( run_test(), run_test(), ) assert rate_limit.rate == 0
async def test_inara_api_key_unset(f_bot): try: old_key = cog.inara.HEADER_PROTO cog.inara.HEADER_PROTO = None api = cog.inara.InaraApi() cog.util.BOT = f_bot await api.search_with_api('gearsandcogs', fake_msg_gears('!whois gearsandcogs')) assert "!whois is currently disabled." in str( f_bot.send_message.call_args) finally: cog.inara.HEADER_PROTO = old_key
async def test_search_inara_and_kos_match_exact(f_bot): api = cog.inara.InaraApi() cog.util.BOT = f_bot f_bot.wait_for.async_side_effect = [mock_inter([cog.inara.BUT_CANCEL])] msg = fake_msg_gears('!whois gearsandcogs') result = await api.search_inara_and_kos('gearsandcogs', msg) expect = { 'add': False, 'cmdr': 'GearsandCogs', 'is_friendly': False, 'reason': 'unknown', 'squad': 'Federal Republic Command - Hudson Powerplay' } assert result == expect
def test_check_interaction_response(f_dusers, f_admins): message = fake_msg_gears('hello') message2 = fake_msg_newuser('goodbye') inter = Interaction('inter1', user=message2.author, message=message2) # Same everything assert cog.inara.check_interaction_response(message2.author, message2, inter) # Different messages, always reject assert not cog.inara.check_interaction_response(message.author, message, inter) # Same message, author is admin inter = Interaction('inter1', user=message.author, message=message2) assert cog.inara.check_interaction_response(message2.author, message2, inter)
async def test_search_inara_and_kos_no_match(f_bot): api = cog.inara.InaraApi() cog.util.BOT = f_bot f_bot.wait_for.async_side_effect = [mock_inter([cog.inara.BUT_HOSTILE])] msg = fake_msg_gears('!whois gearsandcogs') result = await api.search_inara_and_kos('notInInaraForSure', msg) expect = { 'add': True, 'cmdr': 'notInInaraForSure', 'is_friendly': False, 'reason': 'Manual report after a !whois in Channel: live_hudson by cmdr Member: User1', 'squad': 'unknown' } assert result == expect