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()
示例#5
0
    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!')
示例#6
0
    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")
示例#7
0
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
示例#8
0
    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_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_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 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()
示例#12
0
    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()
示例#13
0
    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)
示例#14
0
    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()
示例#15
0
    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")
示例#16
0
    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)
示例#18
0
    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()
示例#19
0
    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')
示例#20
0
    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)
示例#21
0
    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)
示例#22
0
    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)
示例#23
0
    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)
示例#24
0
    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')
示例#25
0
    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)
示例#26
0
    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')