async def test_game_connection_not_restored_if_game_state_prohibits( lobbyconnection: LobbyConnection, game_service: GameService, game_stats_service, game_state, mocker, database ): del lobbyconnection.player.game_connection lobbyconnection.send = CoroutineMock() lobbyconnection.player.state = PlayerState.IDLE lobbyconnection.game_service = game_service game = mock.create_autospec(Game(42, database, game_service, game_stats_service)) game.state = game_state game.password = None game.game_mode = "faf" game.id = 42 game_service._games[42] = game await lobbyconnection.on_message_received({ "command": "restore_game_session", "game_id": 42 }) assert not lobbyconnection.game_connection assert lobbyconnection.player.state == PlayerState.IDLE lobbyconnection.send.assert_any_call({ "command": "notice", "style": "info", "text": "The game you were connected to is no longer available" })
async def test_game_connection_not_restored_if_game_state_prohibits( lobbyconnection: LobbyConnection, game_service: GameService, game_stats_service, game_state, mock_player, mocker): protocol = mocker.patch.object(lobbyconnection, 'protocol') lobbyconnection.player = mock_player lobbyconnection.player.game_connection = None lobbyconnection.player.state = PlayerState.IDLE lobbyconnection.game_service = game_service game = mock.create_autospec(Game(42, game_service, game_stats_service)) game.state = game_state game.password = None game.game_mode = 'faf' game.id = 42 game_service.games[42] = game lobbyconnection.command_restore_game_session({'game_id': 42}) assert not lobbyconnection.game_connection assert lobbyconnection.player.state == PlayerState.IDLE protocol.send_message.assert_any_call({ "command": "notice", "style": "info", "text": "The game you were connected to is no longer available" })
def lobbyconnection(loop, mock_context, mock_protocol, mock_games, mock_players, mock_player): lc = LobbyConnection(loop, context=mock_context, games=mock_games, players=mock_players) lc.player = mock_player lc.connectivity = mock.create_autospec(Connectivity) lc.protocol = mock_protocol return lc
def lobbyconnection(loop, mock_context, mock_protocol, mock_games, mock_players, mock_player): lc = LobbyConnection(loop, context=mock_context, games=mock_games, players=mock_players) lc.player = mock_player lc.connectivity = mock.create_autospec(Connectivity) lc.protocol = mock_protocol return lc
async def test_game_subscription(lobbyconnection: LobbyConnection): game = Mock() game.handle_action = CoroMock() lobbyconnection.game_connection = game lobbyconnection.ensure_authenticated = lambda _: True await lobbyconnection.on_message_received({'command': 'test', 'args': ['foo', 42], 'target': 'game'}) game.handle_action.assert_called_with('test', ['foo', 42])
async def test_game_subscription(lobbyconnection: LobbyConnection): game = Mock() game.handle_action = CoroMock() lobbyconnection.game_connection = game lobbyconnection.ensure_authenticated = lambda _: True await lobbyconnection.on_message_received({'command': 'test', 'args': ['foo', 42], 'target': 'game'}) game.handle_action.assert_called_with('test', ['foo', 42])
def lobbyconnection(loop, mock_protocol, mock_games, mock_players, mock_player, mock_geoip): lc = LobbyConnection(geoip=mock_geoip, games=mock_games, players=mock_players, nts_client=mock_nts_client, ladder_service=mock.create_autospec(LadderService)) lc.player = mock_player lc.protocol = mock_protocol lc.player_service.get_permission_group.return_value = 0 lc.player_service.fetch_player_data = CoroMock() lc.peer_address = Address('127.0.0.1', 1234) return lc
async def test_game_connection_restored_if_game_exists( lobbyconnection: LobbyConnection, game_service: GameService, game_stats_service, game_state, database ): del lobbyconnection.player.game_connection lobbyconnection.player.state = PlayerState.IDLE lobbyconnection.game_service = game_service game = mock.create_autospec(Game(42, database, game_service, game_stats_service)) game.state = game_state game.password = None game.game_mode = "faf" game.id = 42 game_service._games[42] = game await lobbyconnection.on_message_received({ "command": "restore_game_session", "game_id": 42 }) assert lobbyconnection.game_connection assert lobbyconnection.player.state is PlayerState.PLAYING assert lobbyconnection.player.game is game
async def test_broadcast_during_disconnect(lobbyconnection: LobbyConnection, mocker): player = mocker.patch.object(lobbyconnection, 'player') player.login = '******' player.admin = True player.lobby_connection = asynctest.create_autospec(LobbyConnection) tuna = mock.Mock() tuna.id = 55 # To simulate when a player has been recently disconnected so that they # still appear in the player_service list, but their lobby_connection # object has already been destroyed tuna.lobby_connection = None lobbyconnection.player_service = [player, tuna] # This should not leak any exceptions await lobbyconnection.on_message_received({ 'command': 'admin', 'action': 'broadcast', 'message': "This is a test message" }) player.lobby_connection.send_warning.assert_called_with( "This is a test message")
async def test_broadcast_connection_error(lobbyconnection: LobbyConnection, mocker): player = mocker.patch.object(lobbyconnection, 'player') player.login = '******' player.admin = True player.lobby_connection = asynctest.create_autospec(LobbyConnection) tuna = mock.Mock() tuna.id = 55 tuna.lobby_connection = asynctest.create_autospec(LobbyConnection) tuna.lobby_connection.send_warning.side_effect = ConnectionError( "Some error") lobbyconnection.player_service = [player, tuna] # This should not leak any exceptions await lobbyconnection.on_message_received({ 'command': 'admin', 'action': 'broadcast', 'message': "This is a test message" }) player.lobby_connection.send_warning.assert_called_with( "This is a test message")
async def test_broadcast_connection_error(lobbyconnection: LobbyConnection, player_factory): player = lobbyconnection.player player.lobby_connection = lobbyconnection player.id = 1 tuna = player_factory("Tuna", player_id=55, with_lobby_connection=True) tuna.lobby_connection.write_warning.side_effect = DisconnectedError( "Some error") data = {player.id: player, tuna.id: tuna} lobbyconnection.player_service.__iter__.side_effect = data.values( ).__iter__ lobbyconnection.write_warning = Mock() # This should not leak any exceptions await lobbyconnection.on_message_received({ "command": "admin", "action": "broadcast", "message": "This is a test message" }) player.lobby_connection.write_warning.assert_called_with( "This is a test message")
async def test_broadcast_during_disconnect(lobbyconnection: LobbyConnection, player_factory): player = lobbyconnection.player player.lobby_connection = lobbyconnection player.id = 1 # To simulate when a player has been recently disconnected so that they # still appear in the player_service list, but their lobby_connection # object has already been destroyed tuna = player_factory("Tuna", player_id=55, with_lobby_connection=False) data = {player.id: player, tuna.id: tuna} lobbyconnection.player_service.__iter__.side_effect = data.values( ).__iter__ lobbyconnection.write_warning = Mock() # This should not leak any exceptions await lobbyconnection.on_message_received({ "command": "admin", "action": "broadcast", "message": "This is a test message" }) player.lobby_connection.write_warning.assert_called_with( "This is a test message")
async def test_uid(lobbyconnection: LobbyConnection, test_data, mocker): protocol = mocker.patch.object(lobbyconnection, 'protocol') player_id = test_data[0] steam_id = test_data[1] unique_id_hash = test_data[2] expected_result = test_data[3] lobbyconnection.decodeUniqueId = mock.Mock(return_value=(unique_id_hash, ())) async with db.db_pool.get() as conn: cursor = await conn.cursor() unique_id_ok = await lobbyconnection.validate_unique_id( cursor, player_id, steam_id, "") await cursor.execute( "SELECT count(*) FROM unique_id_users WHERE user_id = %s and uniqueid_hash = %s", (player_id, unique_id_hash)) result = await cursor.fetchone() assert result[0] == 1 assert unique_id_ok == expected_result if not expected_result: protocol.writer.close.assert_called_once_with()
def make_connection() -> LobbyConnection: return LobbyConnection(database=mock.Mock(), geoip=mock.Mock(), game_service=mock.Mock(), nts_client=mock.Mock(), players=mock.Mock(), ladder_service=mock.Mock())
async def test_game_connection_restored_if_game_exists( lobbyconnection: LobbyConnection, game_service: GameService, game_stats_service, game_state, mock_player): lobbyconnection.player = mock_player lobbyconnection.player.game_connection = None lobbyconnection.player.state = PlayerState.IDLE lobbyconnection.game_service = game_service game = mock.create_autospec(Game(42, game_service, game_stats_service)) game.state = game_state game.password = None game.game_mode = 'faf' game.id = 42 game_service.games[42] = game lobbyconnection.command_restore_game_session({'game_id': 42}) assert lobbyconnection.game_connection assert lobbyconnection.player.state == PlayerState.PLAYING
async def test_game_connection_not_restored_if_no_such_game_exists( lobbyconnection: LobbyConnection, mocker, mock_player): protocol = mocker.patch.object(lobbyconnection, 'protocol') lobbyconnection.player = mock_player lobbyconnection.player.game_connection = None lobbyconnection.player.state = PlayerState.IDLE lobbyconnection.command_restore_game_session({'game_id': 123}) assert not lobbyconnection.player.game_connection assert lobbyconnection.player.state == PlayerState.IDLE protocol.send_message.assert_any_call({ "command": "notice", "style": "info", "text": "The game you were connected to does no longer exist" })
async def test_register_non_disposable_email(mocker, lobbyconnection: LobbyConnection): lobbyconnection.generate_expiring_request = mock.Mock(return_value=('iv', 'ciphertext', 'verification_hex')) lobbyconnection.player_service.has_blacklisted_domain.return_value = False await lobbyconnection.command_create_account({ 'login': '******', 'email': "*****@*****.**", 'password': "******" }) assert lobbyconnection.generate_expiring_request.mock_calls
async def test_command_avatar_select(mocker, db_engine, lobbyconnection: LobbyConnection, mock_player: Player): lobbyconnection.player = mock_player lobbyconnection.player.id = 2 # Dostya test user lobbyconnection._authenticated = True await lobbyconnection.on_message_received({ 'command': 'avatar', 'action': 'select', 'avatar': "http://content.faforever.com/faf/avatars/qai2.png" }) async with db_engine.acquire() as conn: result = await conn.execute( "SELECT selected from avatars where idUser=2") row = await result.fetchone() assert row[0] == 1
async def test_game_subscription(lobbyconnection: LobbyConnection): game = Mock() game.handle_action = CoroutineMock() lobbyconnection.game_connection = game await lobbyconnection.on_message_received({ "command": "test", "args": ["foo", 42], "target": "game" }) game.handle_action.assert_called_with("test", ["foo", 42])
def lobbyconnection( event_loop, database, mock_protocol, mock_games, mock_players, mock_player, mock_geoip, mock_nts_client ): lc = LobbyConnection( database=database, geoip=mock_geoip, game_service=mock_games, players=mock_players, nts_client=mock_nts_client, ladder_service=asynctest.create_autospec(LadderService) ) lc.player = mock_player lc.protocol = mock_protocol lc.player_service.fetch_player_data = CoroutineMock() lc.peer_address = Address("127.0.0.1", 1234) lc._authenticated = True return lc
async def test_command_avatar_list(mocker, lobbyconnection: LobbyConnection): lobbyconnection.send = CoroutineMock() lobbyconnection.player.id = 2 # Dostya test user await lobbyconnection.on_message_received({ "command": "avatar", "action": "list_avatar" }) lobbyconnection.send.assert_any_call({ "command": "avatar", "avatarlist": [{"url": "https://content.faforever.com/faf/avatars/qai2.png", "tooltip": "QAI"}, {"url": "https://content.faforever.com/faf/avatars/UEF.png", "tooltip": "UEF"}] })
async def test_command_avatar_list(mocker, lobbyconnection: LobbyConnection, mock_player: Player): protocol = mocker.patch.object(lobbyconnection, 'protocol') lobbyconnection.player = mock_player lobbyconnection.player.id = 2 # Dostya test user await lobbyconnection.command_avatar({ 'action': 'list_avatar' }) protocol.send_message.assert_any_call({ "command": "avatar", "avatarlist": [{'url': 'http://content.faforever.com/faf/avatars/qai2.png', 'tooltip': 'QAI'}] })
async def test_command_avatar_list(mocker, lobbyconnection: LobbyConnection, mock_player: Player): protocol = mocker.patch.object(lobbyconnection, 'protocol') lobbyconnection.player = mock_player lobbyconnection.player.id = 2 # Dostya test user await lobbyconnection.command_avatar({ 'action': 'list_avatar' }) protocol.send_message.assert_any_call({ "command": "avatar", "avatarlist": [{'url': 'http://content.faforever.com/faf/avatars/qai2.png', 'tooltip': 'QAI'}] })
async def test_command_avatar_select(mocker, lobbyconnection: LobbyConnection, mock_player: Player): lobbyconnection.player = mock_player lobbyconnection.player.id = 2 # Dostya test user await lobbyconnection.command_avatar({ 'action': 'select', 'avatar': "http://content.faforever.com/faf/avatars/qai2.png" }) async with db.db_pool.get() as conn: cursor = await conn.cursor() await cursor.execute("SELECT selected from avatars where idUser=2") result = await cursor.fetchone() assert result == (1,)
async def test_command_avatar_select(mocker, lobbyconnection: LobbyConnection, mock_player: Player): lobbyconnection.player = mock_player lobbyconnection.player.id = 2 # Dostya test user await lobbyconnection.command_avatar({ 'action': 'select', 'avatar': "http://content.faforever.com/faf/avatars/qai2.png" }) async with db.db_pool.get() as conn: cursor = await conn.cursor() await cursor.execute("SELECT selected from avatars where idUser=2") result = await cursor.fetchone() assert result == (1,)
async def test_command_ice_servers(lobbyconnection: LobbyConnection, mock_nts_client): lobbyconnection.send = CoroutineMock() lobbyconnection.coturn_generator.server_tokens = Mock( return_value=["coturn_tokens"]) mock_nts_client.server_tokens.return_value = ["twilio_tokens"] await lobbyconnection.on_message_received({"command": "ice_servers"}) mock_nts_client.server_tokens.assert_called_once() lobbyconnection.send.assert_called_once_with({ "command": "ice_servers", "ice_servers": ["coturn_tokens", "twilio_tokens"], "ttl": config.TWILIO_TTL })
async def test_broadcast(lobbyconnection: LobbyConnection, mocker): mocker.patch.object(lobbyconnection, 'protocol') player = mocker.patch.object(lobbyconnection, 'player') player.login = '******' player.admin = True tuna = mock.Mock() tuna.id = 55 lobbyconnection.player_service = [player, tuna] await lobbyconnection.command_admin({ 'command': 'admin', 'action': 'broadcast', 'message': "This is a test message" }) player.lobby_connection.send_warning.assert_called_with("This is a test message") tuna.lobby_connection.send_warning.assert_called_with("This is a test message")
async def test_game_connection_not_restored_if_no_such_game_exists(lobbyconnection: LobbyConnection, mocker): del lobbyconnection.player.game_connection lobbyconnection.send = CoroutineMock() lobbyconnection.player.state = PlayerState.IDLE await lobbyconnection.on_message_received({ "command": "restore_game_session", "game_id": 123 }) assert not lobbyconnection.player.game_connection assert lobbyconnection.player.state == PlayerState.IDLE lobbyconnection.send.assert_any_call({ "command": "notice", "style": "info", "text": "The game you were connected to does no longer exist" })
async def test_broadcast(lobbyconnection: LobbyConnection, mocker): mocker.patch.object(lobbyconnection, 'protocol') player = mocker.patch.object(lobbyconnection, 'player') player.login = '******' player.admin = True tuna = mock.Mock() tuna.id = 55 lobbyconnection.player_service = [player, tuna] await lobbyconnection.command_admin({ 'command': 'admin', 'action': 'broadcast', 'message': "This is a test message" }) player.lobby_connection.send_warning.assert_called_with( "This is a test message") tuna.lobby_connection.send_warning.assert_called_with( "This is a test message")