def test_select_game_should_fail_if_game_does_not_exist( self, get_games_mock, mock_keyboard ): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User]) User.create(id=111, steam_id=0, authorization_key='') bot_mock = Mock() update_mock = Mock() update_mock.message.from_user.id = 111 update_mock.message.text = 'game' mock_keyboard.return_value = mock_keyboard with open( (os.path.dirname(__file__))+'/fixtures/game_request.json' ) as f: json_data = json.load(f) game_data = json_data['Games'] get_games_mock.return_value = game_data self.assertEqual( ConversationHandler.END, cmd_new_game.select_game(bot_mock, update_mock) ) update_mock.message.reply_text.assert_called_with( 'Game does not exist', reply_markup=mock_keyboard )
def test_new_game_should_return_list_of_games( self, get_games_mock, mock_keyboard ): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game]) User.create(id=111, steam_id=0, authorization_key='') mock_keyboard.return_value = mock_keyboard bot_mock = Mock() update_mock = Mock() update_mock.message.from_user.id = 111 with open( (os.path.dirname(__file__))+'/fixtures/game_request.json' ) as f: json_data = json.load(f) game_data = json_data['Games'] get_games_mock.return_value = game_data self.assertEqual( cmd_new_game.SELECT, cmd_new_game.new_game(bot_mock, update_mock) ) update_mock.message.reply_text.assert_called_with( 'Chose the game', reply_markup=mock_keyboard ) mock_keyboard.assert_called()
def test_add_game_should_display_error_if_no_games_fetched(self, get_games_mock): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game]) User.create(id=111, steam_id=0, authorization_key='') bot_mock = Mock() update_mock = Mock() update_mock.message.from_user.id = 111 get_games_mock.return_value = [] self.assertEqual(ConversationHandler.END, cmd_new_game.new_game(bot_mock, update_mock)) update_mock.message.reply_text.assert_called_with('You are not part of any games')
def test_register_fails_if_registered(self): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User]) User.create(id=111, steam_id=0, authorization_key='') bot_mock = Mock() update_mock = Mock() update_mock.message.from_user.id = 111 self.assertEqual(ConversationHandler.END, cmd_register.register(bot_mock, update_mock)) update_mock.message.reply_text.assert_called()
def test_add_game_should_fail_if_not_admin_of_group(self): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User]) User.create(id=111, steam_id=0, authorization_key='') admin_mock = Mock() admin_mock.user.id = 23 bot_mock = Mock() bot_mock.get_chat_administrators.return_value = [admin_mock] update_mock = Mock() update_mock.message.from_user.id = 111 self.assertEqual(ConversationHandler.END, cmd_add_game.add_game(bot_mock, update_mock)) update_mock.message.reply_text.assert_called_with('You are not admin of the group!')
def test_add_game_should_display_error_if_no_registered_games(self): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game]) User.create(id=111, steam_id=0, authorization_key='') admin_mock = Mock() admin_mock.user.id = 111 bot_mock = Mock() bot_mock.get_chat_administrators.return_value = [admin_mock] update_mock = Mock() update_mock.message.from_user.id = 111 self.assertEqual(ConversationHandler.END, cmd_add_game.add_game(bot_mock, update_mock)) update_mock.message.reply_text.assert_called_with("You don't have any registered games")
def new_game(bot, update): user = User.get_or_none(User.id == update.message.from_user.id) if not user: update.message.reply_text('You are not registered!') return ConversationHandler.END games = gmr.get_games(user.steam_id, user.authorization_key) if len(games) == 0: update.message.reply_text('You are not part of any games') return ConversationHandler.END games = list( filter( lambda x: not Game.select().where(Game.id == x['GameId']).exists(), games ) ) if len(games) == 0: update.message.reply_text('No games to be added') return ConversationHandler.END custom_keyboard = [] for game in games: custom_keyboard.append([game['Name']]) custom_keyboard.append(['cancel']) reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) update.message.reply_text('Chose the game', reply_markup=reply_markup) return SELECT
def test_add_game_should_display_error_if_no_available_games( self, get_games_mock ): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game]) mock_user = User.create(id=111, steam_id=0, authorization_key='') Game.create(id=27000, owner=mock_user, name='Test Game') bot_mock = Mock() update_mock = Mock() update_mock.message.from_user.id = 111 with open( (os.path.dirname(__file__))+'/fixtures/game_request.json' ) as f: json_data = json.load(f) game_data = json_data['Games'] get_games_mock.return_value = game_data self.assertEqual( ConversationHandler.END, cmd_new_game.new_game(bot_mock, update_mock) ) update_mock.message.reply_text.assert_called_with( 'No games to be added' )
def authkey(bot, update): try: steam_id = gmr.get_steam_id_from_auth(update.message.text) except InvalidAuthKey: update.message.reply_text( "Authkey incorrect, try again (/cancel to end)") return AUTHKEY User.create(id=update.message.from_user.id, steam_id=steam_id, authorization_key=update.message.text) update.message.reply_text( f"Successfully registered with steam id {steam_id}") return ConversationHandler.END
def test_poll_game_should_return_false_if_it_is_not_in_any_chats( self, mock_gmr ): bot_mock = Mock() database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game, Subscription]) with open((os.path.dirname(__file__)) + '/fixtures/game_request.json') as f: json_data = json.load(f) mock_gmr.return_value = json_data['Games'][0] user = User.create(id=111, steam_id=0, authorization_key='') game = Game.create( id=27000, name='Test Game', owner=user ) self.assertEqual(False, job_games.poll_game(bot_mock, game)) game.refresh() self.assertEqual(True, game.active)
def start_message(bot, update): user = User.get_or_none(User.id == update.message.from_user.id) msg = 'Welcome to telegram bot 2.0' if not user: msg += '\nRun /register to get started' update.message.reply_text(msg)
def test_verify_should_remove_user_if_yes(self, mock_keyboard): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User]) User.create(id=111, steam_id=0, authorization_key='') bot_mock = Mock() update_mock = Mock() update_mock.message.from_user.id = 111 update_mock.message.text = 'Yes' mock_keyboard.return_value = mock_keyboard self.assertEqual(ConversationHandler.END, cmd_unregister.verify(bot_mock, update_mock)) update_mock.message.reply_text.assert_called_with( "Unregistered, your user data was removed!", reply_markup=mock_keyboard ) self.assertIsNone(User.get_or_none(User.id == 111))
def add_game(bot, update): user = User.get_or_none(User.id == update.message.from_user.id) if not user: update.message.reply_text('You are not registered!') return ConversationHandler.END chat_id = update.message.chat_id if update.message.chat.type != 'private': admin_ids = [ admin.user.id for admin in bot.get_chat_administrators(chat_id) ] if update.message.from_user.id not in admin_ids: update.message.reply_text('You are not admin of the group!') return ConversationHandler.END games = user.games if len(games) == 0: update.message.reply_text("You don't have any registered games") return ConversationHandler.END games = list( filter( lambda g: not ( Subscription.select().where( Subscription.game == g ).where( Subscription.chat_id == chat_id ).exists() ), games ) ) if len(games) == 0: update.message.reply_text( "You don't have any registered games not in this chat" ) return ConversationHandler.END games = list(filter(lambda g: g.active, games)) if len(games) == 0: update.message.reply_text("You don't have any active games") return ConversationHandler.END custom_keyboard = [] for game in games: custom_keyboard.append([game.name]) custom_keyboard.append(['cancel']) reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) update.message.reply_text('Chose the game', reply_markup=reply_markup) return SELECT
def test_unregister_should_go_to_verify(self, mock_keyboard): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User]) User.create(id=111, steam_id=0, authorization_key='') mock_keyboard.return_value = mock_keyboard bot_mock = Mock() update_mock = Mock() update_mock.message.from_user.id = 111 self.assertEqual(cmd_unregister.VERIFY, cmd_unregister.unregister(bot_mock, update_mock)) update_mock.message.reply_text.assert_called_with( "Are you sure you want to unregister." "You will stop receiving notifications from current games." "Your steam id is still kept in order for active games to function." "You can register back anytime to continue receiving notifications.", reply_markup=mock_keyboard ) mock_keyboard.assert_called()
def register(bot, update): user = User.get_or_none(User.id == update.message.from_user.id) if user: update.message.reply_text("You are already registered!") return ConversationHandler.END update.message.reply_text("Provide GMR Authentication key" "Authentication key can be acquired " "from http://multiplayerrobot.com/Download") return AUTHKEY
def test_poll_game_should_return_true_and_ping_chat_if_turn_has_changed( self, mock_gmr ): bot_mock = Mock() bot_mock.get_chat.return_value.username = '******' database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game, Player, Subscription]) user = User.create( id=111, steam_id=76561190000000003, authorization_key='' ) game = Game.create( id=27000, name='Test Game', owner=user, current_steam_id=76561190000000002 ) Subscription.create(game=game, chat_id=1234) Player.insert_many( [ {'steam_id': 76561190000000001, 'game_id': 27000, 'order': 0}, {'steam_id': 76561190000000002, 'game_id': 27000, 'order': 1}, {'steam_id': 76561190000000003, 'game_id': 27000, 'order': 2}, {'steam_id': 76561190000000004, 'game_id': 27000, 'order': 3}, {'steam_id': 76561190000000005, 'game_id': 27000, 'order': 4}, {'steam_id': 76561190000000006, 'game_id': 27000, 'order': 5}, {'steam_id': 76561190000000007, 'game_id': 27000, 'order': 6}, {'steam_id': 76561190000000008, 'game_id': 27000, 'order': 7} ] ).execute() with open((os.path.dirname(__file__)) + '/fixtures/game_request.json') as f: json_data = json.load(f) mock_gmr.return_value = json_data['Games'][0] self.assertEqual(True, job_games.poll_game(bot_mock, game)) game.refresh() self.assertEqual(True, game.active) self.assertEqual(76561190000000003, game.current_steam_id) bot_mock.send_message.assert_called_with( chat_id=1234, text="It's now your turn @test_user" ) bot_mock.get_chat.assert_called()
def test_select_should_fail_if_wrong_game(self, mock_keyboard): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game, Subscription]) user_mock = User.create(id=111, steam_id=0, authorization_key='') Game.create(id=1, owner=user_mock, name='test game') mock_keyboard.return_value = mock_keyboard bot_mock = Mock() update_mock = Mock() update_mock.message.from_user.id = 111 update_mock.message.text = 'failed game' self.assertEqual(ConversationHandler.END, cmd_add_game.select_game(bot_mock, update_mock)) update_mock.message.reply_text.assert_called_with('Game does not exist', reply_markup=mock_keyboard)
def select_game(bot, update): if update.message.text == 'cancel': update.message.reply_text( 'Canceled', reply_markup=telegram.ReplyKeyboardRemove() ) return ConversationHandler.END user = User.get_or_none(User.id == update.message.from_user.id) games = gmr.get_games(user.steam_id, user.authorization_key) games_data = [g for g in games if g['Name'] == update.message.text] if len(games_data) == 0: update.message.reply_text( 'Game does not exist', reply_markup=telegram.ReplyKeyboardRemove() ) return ConversationHandler.END game_data = games_data[0] if Game.select().where(Game.id == game_data['GameId']).exists(): update.message.reply_text( 'Game already registered', reply_markup=telegram.ReplyKeyboardRemove() ) return ConversationHandler.END game = Game.create( id=game_data['GameId'], owner=user, name=game_data['Name'], current_steam_id=game_data['CurrentTurn']['UserId'] ) for player in game_data['Players']: Player.create( steam_id=player['UserId'], game=game, order=player['TurnOrder'] ) update.message.reply_text( f'Game {game.name} registered', reply_markup=telegram.ReplyKeyboardRemove() ) return ConversationHandler.END
def verify(bot, update): user = User.get(User.id == update.message.from_user.id) msg = update.message.text reply_markup = telegram.ReplyKeyboardRemove() if msg == 'Yes': user.delete_instance() update.message.reply_text("Unregistered, your user data was removed!", reply_markup=reply_markup) return ConversationHandler.END update.message.reply_text( "Canceling unregistering, your data was not removed!", reply_markup=reply_markup) return ConversationHandler.END
def test_select_should_fail_if_game_was_added(self, mock_keyboard): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game, Subscription]) user_mock = User.create(id=111, steam_id=0, authorization_key='') game_mock = Game.create(id=1, owner=user_mock, name='test game') Subscription.create(game=game_mock, chat_id=1234) mock_keyboard.return_value = mock_keyboard bot_mock = Mock() update_mock = Mock() update_mock.message.from_user.id = 111 update_mock.message.chat_id = 1234 update_mock.message.text = game_mock.name self.assertEqual(ConversationHandler.END, cmd_add_game.select_game(bot_mock, update_mock)) update_mock.message.reply_text.assert_called_with('Game has already been added', reply_markup=mock_keyboard)
def test_select_game_should_create_new_game_object( self, get_games_mock, mock_keyboard ): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Player, Game]) mock_user = User.create(id=111, steam_id=0, authorization_key='') bot_mock = Mock() update_mock = Mock() update_mock.message.from_user.id = 111 update_mock.message.text = 'Test Game' mock_keyboard.return_value = mock_keyboard with open( (os.path.dirname(__file__))+'/fixtures/game_request.json' ) as f: json_data = json.load(f) game_data = json_data['Games'] get_games_mock.return_value = game_data self.assertEqual( ConversationHandler.END, cmd_new_game.select_game(bot_mock, update_mock) ) update_mock.message.reply_text.assert_called_with( 'Game Test Game registered', reply_markup=mock_keyboard ) game = Game.get_or_none(Game.id == 27000) self.assertIsNotNone(game) self.assertEqual(mock_user, game.owner) self.assertEqual('Test Game', game.name) self.assertEqual(76561190000000003, game.current_steam_id) self.assertEqual(True, game.active) self.assertEqual(8, game.players.count()) players = game.players.order_by(Player.order) for i in range(0, 7): player = players[i] self.assertEqual(76561190000000000+1+i, player.steam_id) self.assertEqual(i, player.order)
def test_poll_games_should_not_poll_inactive_game(self, mock_poll_game): bot_mock = Mock() job_mock = Mock() database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game]) user = User.create(id=111, steam_id=0, authorization_key='') Game.create( id=27000, name='Test Game', owner=user, active=False ) job_games.poll_games(bot_mock, job_mock) mock_poll_game.assert_not_called()
def test_add_game_should_display_error_if_no_active_games(self): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game, Subscription]) user_mock = User.create(id=111, steam_id=0, authorization_key='') Game.create(id=1, owner=user_mock, name='test game', active=False) admin_mock = Mock() admin_mock.user.id = 111 bot_mock = Mock() bot_mock.get_chat_administrators.return_value = [admin_mock] update_mock = Mock() update_mock.message.from_user.id = 111 update_mock.message.chat_id = 1234 self.assertEqual(ConversationHandler.END, cmd_add_game.add_game(bot_mock, update_mock)) update_mock.message.reply_text.assert_called_with("You don't have any active games")
def unregister(bot, update): user = User.get_or_none(User.id == update.message.from_user.id) if not user: update.message.reply_text("You are not registered!") return ConversationHandler.END custom_keyboard = [['Yes'], ['No']] reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) update.message.reply_text( "Are you sure you want to unregister." "You will stop receiving notifications from current games." "Your steam id is still kept in order for active games to function." "You can register back anytime to continue receiving notifications.", reply_markup=reply_markup) return VERIFY
def select_game(bot, update): if update.message.text == 'cancel': update.message.reply_text( 'Canceled', reply_markup=telegram.ReplyKeyboardRemove() ) return ConversationHandler.END user = User.get_or_none(User.id == update.message.from_user.id) game = [g for g in user.games if g.name == update.message.text] if len(game) == 0: update.message.reply_text( 'Game does not exist', reply_markup=telegram.ReplyKeyboardRemove() ) return ConversationHandler.END game = game[0] chat_id = update.message.chat_id subscriptions = Subscription.select().where( Subscription.game == game ).where( Subscription.chat_id == chat_id ) if subscriptions.exists(): update.message.reply_text( 'Game has already been added', reply_markup=telegram.ReplyKeyboardRemove() ) return ConversationHandler.END Subscription.create( game=game, chat_id=chat_id ) update.message.reply_text( f'Subscribed to {game.name}.' f' This chat will now start receiving notifications for the ' 'game. To get notifications, send /register to me as private message', reply_markup=telegram.ReplyKeyboardRemove()) return ConversationHandler.END
def test_order_should_fail_if_game_does_not_exist_anymore( self, get_games_mock): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game, Subscription]) bot_mock = Mock() update_mock = Mock() update_mock.message.from_user.id = 0 update_mock.message.chat_id = 1234 get_games_mock.return_value = [] user_mock = User.create(id=111, steam_id=0, authorization_key='') game_mock = Game.create(id=1, owner=user_mock, name='test game') Subscription.create(game=game_mock, chat_id=1234) cmd_order.order(bot_mock, update_mock) update_mock.message.reply_text.assert_called_with('Game is over')
def test_add_game_should_display_list_of_games(self, mock_keyboard): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game, Subscription]) user_mock = User.create(id=111, steam_id=0, authorization_key='') Game.create(id=1, owner=user_mock, name='test game') mock_keyboard.return_value = mock_keyboard admin_mock = Mock() admin_mock.user.id = 111 bot_mock = Mock() bot_mock.get_chat_administrators.return_value = [admin_mock] update_mock = Mock() update_mock.message.from_user.id = 111 update_mock.message.chat_id = 1234 self.assertEqual(cmd_add_game.SELECT, cmd_add_game.add_game(bot_mock, update_mock)) update_mock.message.reply_text.assert_called_with('Chose the game', reply_markup=mock_keyboard) mock_keyboard.assert_called()
def test_get_game_data_should_raise_exception_if_game_does_not_exist( self, mock_request, mock_url): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game]) user_mock = User.create(id=111, steam_id=76561190000000007, authorization_key='test_auth_key') game_mock = Game.create(id=27001, owner=user_mock, name='Test Game') with open((os.path.dirname(__file__)) + '/fixtures/game_request.json') as f: json_data = json.load(f) mock_request.get( "https://www.fake.com/api/Diplomacy/GetGamesAndPlayers?p" f"layerIDText=76561190000000007&authKey=test_auth_key", json=json_data) self.assertRaises(GameNoLongerExist, gmr.get_game_data, game_mock)
def test_poll_game_should_fail_and_deactivate_game_if_it_no_longer_exists( self, mock_gmr ): bot_mock = Mock() database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game]) user = User.create(id=111, steam_id=0, authorization_key='') game = Game.create( id=27000, name='Test Game', owner=user ) mock_gmr.side_effect = GameNoLongerExist self.assertEqual(False, job_games.poll_game(bot_mock, game)) game.refresh() self.assertEqual(False, game.active)
def test_poll_games_should_call_poll_for_active_game( self, mock_game_select, mock_poll_game ): bot_mock = Mock() job_mock = Mock() database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game]) user = User.create(id=111, steam_id=0, authorization_key='') game = Game.create( id=27000, name='Test Game', owner=user ) mock_game_select.return_value = [game] job_games.poll_games(bot_mock, job_mock) mock_poll_game.assert_called_with(bot_mock, game)