def test_select_game_should_fail_if_game_had_been_registered( self, get_games_mock, mock_keyboard ): 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 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 already registered', reply_markup=mock_keyboard )
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 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 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_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 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 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_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 index(request): try: info = json.loads(request.body) player = info['value2'] game = info['value1'] turn = int(info['value3']) except: notes.sendSlack('invalid webhook received', '#bot-testing') notes.sendSlack(request.body, '#bot-testing') return HttpResponse("Invalid Request") try: game_player = Game.objects.get(name=game, player=player) if game_player.turn == turn: #duplicate webhook check return JsonResponse(info) game_player.turn = turn game_player.updated = timezone.now() except: game_player = Game(name=game, player=player, turn=turn) game_player.save() game_player.refresh_from_db() notes.sendPlayerNotices(game_player) return JsonResponse(info)
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_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_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_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_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)
def test_select_should_add_the_game_to_group(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') 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( f'Subscribed to {game_mock.name}. This chat will now start receiving notifications for the ' 'game. To get notifications, send /register to me as private message', reply_markup=mock_keyboard) sub = Subscription.get_or_none(Subscription.game == game_mock, Subscription.chat_id == 1234) self.assertIsNotNone(sub)
def test_get_game_data_should_return_data_for_selected_game( 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=27000, owner=user_mock, name='Test Game') with open((os.path.dirname(__file__)) + '/fixtures/game_request.json') as f: json_data = json.load(f) game_data = json_data['Games'][0] mock_request.get( "https://www.fake.com/api/Diplomacy/GetGamesAndPlayers?p" "layerIDText=76561190000000007&authKey=test_auth_key", json=json_data) games = gmr.get_game_data(game_mock) self.assertEqual(game_data, games) mock_url.assert_called_with('GMR_URL')
def test_tee_should_send_voice(self, get_games_mock, mock_get_user_profile, mock_gtts, mock_voice_fp): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game, Player, Subscription]) bot_mock = Mock() update_mock = Mock() update_mock.message.from_user.id = 0 update_mock.message.chat_id = 1234 mock_gtts.return_value = mock_gtts mock_voice_fp.return_value = mock_voice_fp Player.insert_many([{ 'steam_id': 76561190000000007, 'game_id': 27000, 'order': 6 }, { 'steam_id': 76561190000000003, 'game_id': 27000, 'order': 2 }, { 'steam_id': 76561190000000005, 'game_id': 27000, 'order': 4 }, { 'steam_id': 76561190000000004, 'game_id': 27000, 'order': 3 }, { 'steam_id': 76561190000000001, 'game_id': 27000, 'order': 0 }, { 'steam_id': 76561190000000002, 'game_id': 27000, 'order': 1 }, { 'steam_id': 76561190000000006, 'game_id': 27000, 'order': 5 }, { 'steam_id': 76561190000000008, 'game_id': 27000, 'order': 7 }]).execute() mock_get_user_profile.personaname = "Player1" 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 user_mock = User.create(id=111, steam_id=76561190000000000, authorization_key='') game_mock = Game.create(id=27000, owner=user_mock, name='Test Game') Subscription.create(game=game_mock, chat_id=1234) cmd_tee.tee(bot_mock, update_mock) mock_gtts.write_to_fp.assert_called_with(mock_voice_fp) mock_voice_fp.seek.assert_called_with(0) bot_mock.send_voice.assert_called_with( chat_id=update_mock.message.chat_id, voice=mock_voice_fp)
def test_order_should_display_order_of_players(self, get_games_mock, mock_get_user_profile): database = SqliteDatabase(':memory:') database_proxy.initialize(database) database.create_tables([User, Game, Player, Subscription]) bot_mock = Mock() update_mock = Mock() update_mock.message.from_user.id = 0 update_mock.message.chat_id = 1234 Player.insert_many([{ 'steam_id': 76561190000000007, 'game_id': 27000, 'order': 6 }, { 'steam_id': 76561190000000003, 'game_id': 27000, 'order': 2 }, { 'steam_id': 76561190000000005, 'game_id': 27000, 'order': 4 }, { 'steam_id': 76561190000000004, 'game_id': 27000, 'order': 3 }, { 'steam_id': 76561190000000001, 'game_id': 27000, 'order': 0 }, { 'steam_id': 76561190000000002, 'game_id': 27000, 'order': 1 }, { 'steam_id': 76561190000000006, 'game_id': 27000, 'order': 5 }, { 'steam_id': 76561190000000008, 'game_id': 27000, 'order': 7 }]).execute() mock_get_user_profile.side_effect = self.get_name_from_steam_id 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 user_mock = User.create(id=111, steam_id=76561190000000000, authorization_key='') game_mock = Game.create(id=27000, 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('Order is:\n' 'Player1\n' 'Player2\n' 'Player3\n' 'Player4\n' 'Player5\n' 'Player6\n' 'Player7\n' 'Player8')
def poll_games(bot, job): for game in Game.select(): if game.active: poll_game(bot, game)