def test_sending_single_message(purge_and_init_memberdata_fx, mock_send_private_message_fx): """ Ensure that the correct message is sent out for a single quest. The format of the messages is thoroughly tested in when testing the _message function, so here the only thing to do is to test that a matching message is really sent to the correct recipient. """ purge_and_init_memberdata_fx() mock_messager = mock_send_private_message_fx command = ("quest-reminders\n" "```\n" "FirstQuest; @thisdoesntmatter\n" "quest; {}\n" "```" "".format(SIMPLE_USER["loginname"])) expected_message = ("You have a quest coming up in the queue: " "quest! It comes after FirstQuest, so when " "you notice that FirstQuest has ended, please " "send out the invite for quest.") test_command_msg = PrivateMessage("from_id", "to_id", content=command) reminder = SendQuestReminders() reminder.act(test_command_msg) mock_messager.assert_called_with(SIMPLE_USER["id"], expected_message)
def get_private_messages(self): """ Fetch private messages using Habitica API. If there are new messages, they are written to the database and returned. No paging is implemented: all new messages are assumed to fit into the returned data from the API. """ try: message_data = get_dict_from_api( self._header, "https://habitica.com/api/v3/inbox/messages") except requests.exceptions.HTTPError as err: raise CommunicationFailedException(err.response) from err messages = [None] * len(message_data) for i, message_dict in zip(range(len(message_data)), message_data): if message_dict["sent"]: recipient = message_dict["uuid"] sender = message_dict["ownerId"] else: recipient = message_dict["ownerId"] sender = message_dict["uuid"] messages[i] = PrivateMessage(sender, recipient, timestamp=timestamp_to_datetime( message_dict["timestamp"]), content=message_dict["text"], message_id=message_dict["id"]) self._logger.debug("Fetched %d messages from Habitica API", len(messages)) self.add_PMs_to_db(messages)
def test_send_reminder_called_with_correct_params( mocker, purge_and_init_memberdata_fx): """ Ensure that the message content is parsed correctly by checking how _send_reminder gets called """ purge_and_init_memberdata_fx() # these users are the ones added in database init user1 = "@testuser" user2 = "@somedude" mock_send = mocker.patch("habot.functionality.quests." "SendQuestReminders._send_reminder") command = ("quest-reminders\n" "```\n" "FirstQuest; @thisdoesntmatter\n" "Quest1; {user1}\n" "Quest number 2; {user2}\n" "Quest 3; {user1}, {user2}\n" "```" "".format(user1=user1, user2=user2)) test_message = PrivateMessage("from_id", "to_id", content=command) reminder = SendQuestReminders() reminder.act(test_message) expected_calls = [ call("Quest1", "testuser", 1, "FirstQuest"), call("Quest number 2", "somedude", 1, "Quest1"), call("Quest 3", "testuser", 2, "Quest number 2"), call("Quest 3", "somedude", 2, "Quest number 2"), ] mock_send.assert_has_calls(expected_calls)
def test_faulty_quest_queue(quests, expected_message_part, purge_and_init_memberdata_fx, mock_send_private_message_fx): """ Test that no messages are sent when given quest queue is faulty. The user @testuser found in the test data is inserted into the database during test database initialization. Note that the owner of the first quest doesn't have to be in the database: that information is not used by the bot. """ purge_and_init_memberdata_fx() mock_messager = mock_send_private_message_fx command = ("quest-reminders\n" "```\n" "{}\n" "```" "".format("\n".join(quests))) test_command_msg = PrivateMessage("from_id", "to_id", content=command) reminder = SendQuestReminders() response = reminder.act(test_command_msg) assert expected_message_part in response mock_messager.assert_not_called()
def handle_PMs(): """ React to commands given via private messages. """ # pylint: disable=invalid-name new_messages = PrivateMessage.messages_awaiting_reaction() for message in new_messages: react_to_message(message)
def test_handle_single_PM(mock_messages_awaiting_reaction_fx, mock_send_private_message_fx): """ Test running `handle_PMs` with a single message awaiting reaction. Ensure that the correct response is "sent" using a mocked function. """ pm_mock = mock_send_private_message_fx message = PrivateMessage("from_id", "to_id", content="ping") mock_messages_awaiting_reaction_fx([message]) handle_PMs() pm_mock.assert_has_calls([call("from_id", "Pong")])
def test_quest_queue_outside_code(mock_send_private_message_fx): """ Test that a well-formed quest queue is not accepted if outside a code block """ mock_messager = mock_send_private_message_fx command = ("quest-reminders\n" "q1;@user1\n" "q2;@user2\n") test_command_msg = PrivateMessage("from_id", "to_id", content=command) reminder = SendQuestReminders() response = reminder.act(test_command_msg) assert "code block was not found" in response mock_messager.assert_not_called()
def test_complex_quest_reminder(mocker, purge_and_init_memberdata_fx): """ Test that difficult but legal quest queue is parsed correctly. """ purge_and_init_memberdata_fx() # these users are the ones added in database init user1 = "testuser" user2 = "somedude" mock_send = mocker.patch("habot.functionality.quests." "SendQuestReminders._send_reminder") command = ("quest-reminders\n\n" "there's some weird content here\n" "but it shouldn't matter\n\n" "```\n" "FirstQuest;\n" "Quest1; @{user1}\n" "\n" " Quest number 2 ; {user2}\n" " Quest 3;{user1},{user2}\n" "Quest 3;{user1} , {user2}\n" "```" "".format(user1=user1, user2=user2)) test_message = PrivateMessage("from_id", "to_id", content=command) reminder = SendQuestReminders() reminder.act(test_message) expected_calls = [ call("Quest1", "testuser", 1, "FirstQuest"), call("Quest number 2", "somedude", 1, "Quest1"), call("Quest 3", "testuser", 2, "Quest number 2"), call("Quest 3", "somedude", 2, "Quest number 2"), ] mock_send.assert_has_calls(expected_calls)
def test_ping(): """ Ensure that the "ping" command gets "pong" back as a response. """ command_msg = PrivateMessage("from_id", "to_id") assert Ping().act(command_msg) == "Pong"