def test_send_new_messages_to_slack(monkeypatch, tmp_path, all_messages, sleepless): class MockResponseMessages: @staticmethod def json(): return all_messages def mock_post(*args, **kwargs): return MockResponseMessages() def mock_get_permalink(*args, **kwargs): return {"permalink": "https://2"} def mock_post_message(*args, **kwargs): return {"ts": "2"} monkeypatch.setattr(requests, "post", mock_post) monkeypatch.setattr(WebClient, "chat_getPermalink", mock_get_permalink) monkeypatch.setattr(WebClient, "chat_postMessage", mock_post_message) mbox_path = tmp_path / "mbox" messaging = Messaging("abcd") messages = messaging.get_messages_from_game("374955", "1") slack_messaging = SlackMessaging("abcd", "#channel") slack_messaging.send_new_messages_to_slack(messages, "374955", mbox_path) # We don't have a good way to see the messages were posted successfully other # than checking they exist in the mailbox afterwards mailbox_mbox = mailbox.mbox(mbox_path) assert mailbox_mbox.__len__() == 3
def test_get_system_message_participants(message_with_reply): messaging = Messaging("abcd") participants = messaging.get_message_participants(message_with_reply) assert len(participants) == 1 assert isinstance(participants, dict) assert "Leshy Sector" in participants assert (participants["Leshy Sector"] == '"Leshy Sector" <*****@*****.**>')
def test_construct_outlook_thread_index(message_with_reply): messaging = Messaging("abcd") thread_index = messaging.construct_outlook_thread_index(message_with_reply) assert thread_index == "AdZAbj23ui+pCroMkYwvKHLiyERMhA==" reply = message_with_reply["_replies"][0] reply_thread_index = messaging.construct_outlook_thread_index( reply, thread_index) assert reply_thread_index == "AdZAbj23ui+pCroMkYwvKHLiyERMhAADorox"
def test_get_message_participants(message_no_replies): messaging = Messaging("abcd") participants = messaging.get_message_participants(message_no_replies) assert len(participants) == 2 assert isinstance(participants, dict) assert "coen1970 (The Colonies)" in participants assert (participants["coen1970 (The Colonies)"] == '"coen1970 (The Colonies)" <*****@*****.**>')
def test_get_race_ids_for_game(monkeypatch): class MockResponseLoadinfo: @staticmethod def json(): return {"players": [{"raceid": "2"}, {"raceid": "11"}]} def mock_post(*args, **kwargs): return MockResponseLoadinfo() monkeypatch.setattr(requests, "post", mock_post) messaging = Messaging("abcd") race_ids = messaging.get_race_ids_for_game("1234") assert race_ids == ["2", "11"]
def _mbox(game_id: str, api_key: str, race_id: str, outfile: Path) -> None: logger.info("Fetching messages for game %s.", game_id) messaging = Messaging(api_key) if planets["race_id"]: messages = messaging.get_messages_from_game(game_id, race_id) else: messages = messaging.get_all_messages_from_game(game_id) if messages: logger.info("Saving messages from game %s to %s.", game_id, outfile) exporting = Exporting() exporting.to_mbox(messages, outfile) logger.info("Messages saved.") else: logger.error("Could not get messages from game %s", game_id) raise typer.Exit()
def test_get_messages_from_game(monkeypatch, all_messages): class MockResponseMessages: @staticmethod def json(): return all_messages def mock_post(*args, **kwargs): return MockResponseMessages() monkeypatch.setattr(requests, "post", mock_post) messaging = Messaging("abcd") messages = messaging.get_messages_from_game("1234", "2") assert isinstance(messages, list) assert len(messages) == 2
def test_icon_from_message(message_no_replies): icons = Messaging.icon_from_message(message_no_replies) assert len(icons) == 3 assert (icons["league"] == "https://mobile.planets.nu/img/ui/league-logo-400-drop.png") assert icons["player"] == "https://profiles2.planets.nu/5635" assert icons["race"] == "https://mobile.planets.nu/img/races/race-2.jpg"
def csv(outfile: Path = typer.Argument( ..., help="Path of the CSV file to use (will be overwritten!)")): """Export planets.nu in-game messages to a CSV (comma-separated values) file.""" logger.info("Fetching messages for game %s.", planets["game_id"]) messaging = Messaging(planets["api_key"]) if planets["race_id"]: messages = messaging.get_messages_from_game(planets["game_id"], planets["race_id"]) else: messages = messaging.get_all_messages_from_game(planets["game_id"]) if messages: logger.info("Saving messages from game %s to %s.", planets["game_id"], outfile) exporting = Exporting() exporting.to_csv(messages, outfile) logger.info("Messages saved.") else: logger.error("Could not get messages from game %s", planets["game_id"])
def test_get_all_messages_from_game(monkeypatch, all_messages): class MockResponseMessages: @staticmethod def json(): return all_messages def mock_race_ids(self, planets_game_id): return ["2", "11"] def mock_post(*args, **kwargs): return MockResponseMessages() monkeypatch.setattr(Messaging, "get_race_ids_for_game", mock_race_ids) monkeypatch.setattr(requests, "post", mock_post) messaging = Messaging("abcd") messages = messaging.get_all_messages_from_game("1234") assert isinstance(messages, list) # After deduping the list, we should have just two messages left assert len(messages) == 2
def slack( slack_bot_token: str = typer.Option(..., envvar="SLACK_BOT_TOKEN"), slack_channel_id: str = typer.Option(..., envvar="SLACK_CHANNEL_ID"), ): """Send planets.nu in-game messages to Slack.""" if not planets["race_id"]: logger.error("Player ID required when sending messages to Slack!") raise typer.Exit() logger.info("Fetching messages for game %s.", planets["game_id"]) messaging = Messaging(planets["api_key"]) messages = messaging.get_messages_from_game(planets["game_id"], planets["race_id"]) if messages: logger.info("Sending messages from game %s to Slack.", planets["game_id"]) slack_messaging = SlackMessaging( slack_bot_token, slack_channel_id, ) slack_messaging.send_new_messages_to_slack(messages, planets["game_id"]) logger.info("Messages sent.") else: logger.error("Could not get messages from game %s", planets["game_id"])
def test_email_from_name(): name = "foo (The Feds)" gameid = "1234" email = Messaging.email_from_name(name, gameid) assert email == '"foo (The Feds)" <*****@*****.**>'
def test_construct_msg_id(message_no_replies): msg_id = Messaging.construct_msg_id(message_no_replies) assert msg_id == "<*****@*****.**>"