diff --git a/blackjackbot/commands/game/commands.py b/blackjackbot/commands/game/commands.py index 428b16e..5b25a09 100644 --- a/blackjackbot/commands/game/commands.py +++ b/blackjackbot/commands/game/commands.py @@ -115,6 +115,8 @@ def join_callback(update, context): try: game.add_player(user.id, user.first_name) + # Players joining a group game never sent /start, so they might not be stored in the database yet + Database().add_user(user.id, user.language_code, user.first_name, user.last_name, user.username) update.effective_message.edit_text(text=translator("mp_request_join").format(game.get_player_list()), reply_markup=get_join_keyboard(game.id, lang_id)) update.callback_query.answer(translator("mp_join_callback").format(user.first_name)) diff --git a/blackjackbot/commands/game/tests/commands_test.py b/blackjackbot/commands/game/tests/commands_test.py new file mode 100644 index 0000000..82c34f1 --- /dev/null +++ b/blackjackbot/commands/game/tests/commands_test.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +import unittest +from unittest.mock import Mock, patch + +from blackjack.game import BlackJackGame +from blackjackbot.commands.game.commands import join_callback +from blackjackbot.gamestore import GameStore + + +class GameCommandsTest(unittest.TestCase): + + @patch("blackjackbot.commands.util.decorators.Database") + @patch("blackjackbot.commands.game.commands.Database") + def test_join_callback_stores_user(self, db_mock, decorator_db_mock): + """Players joining a group game via the join button must be stored in the database, so their stats are tracked""" + db_mock.return_value.get_lang_id.return_value = "en" + decorator_db_mock.return_value.get_lang_id.return_value = "en" + chat_id = -1004321 + + game = BlackJackGame(gametype=BlackJackGame.Type.MULTIPLAYER_GROUP) + game.add_player(1, "A") + + game_store = GameStore() + game_store.add_game(chat_id, game) + self.addCleanup(game_store.remove_game, chat_id) + + update = Mock() + update.effective_chat.id = chat_id + update.effective_user.id = 2 + update.effective_user.first_name = "B" + update.effective_user.last_name = "Bee" + update.effective_user.username = "bbb" + update.effective_user.language_code = "de" + update.callback_query.data = "join_{}".format(game.id) + + join_callback(update, Mock()) + + self.assertEqual(2, len(game.players)) + db_mock.return_value.add_user.assert_called_once_with(2, "de", "B", "Bee", "bbb") + + +if __name__ == '__main__': + unittest.main()