async def test_connect_authenticate(client, protocol_client, servers_cache, websocket): servers_cache.get.return_value = async_return_value( ["wss://abc.com/websocket"]) protocol_client.run.return_value = async_raise(AssertionError) credentials_mock = {'password': PASSWORD, "two_factor": TWO_FACTOR} plugin_queue_mock = AsyncMock() websocket_queue_mock = AsyncMock() websocket_queue_mock.get.return_value = credentials_mock error_queue_mock = AsyncMock() error_queue_mock.get.return_value = MagicMock() client.communication_queues = { 'plugin': plugin_queue_mock, 'websocket': websocket_queue_mock, 'errors': error_queue_mock } client._user_info_cache = MagicMock() client._user_info_cache.old_flow = False client._user_info_cache.token = False client._user_info_cache.account_username = ACCOUNT_NAME client._user_info_cache.two_step = None protocol_client.authenticate_password.return_value = async_return_value( UserActionRequired.NoActionRequired) protocol_client.close.return_value = async_return_value(None) protocol_client.wait_closed.return_value = async_return_value(None) with pytest.raises(AssertionError): await client.run() servers_cache.get.assert_called_once_with(0) protocol_client.run.assert_called_once_with() protocol_client.authenticate_password.assert_called_once_with( ACCOUNT_NAME, PASSWORD, TWO_FACTOR, ANY, ANY)
async def test_login_two_step(authenticated_plugin): credentials = {} credentials['end_uri'] = 'two_factor_mobile_finished?code=abc' plugin_queue_mock = AsyncMock() websocket_queue_mock = AsyncMock() websocket_queue_mock.get.return_value = MagicMock() error_queue_mock = AsyncMock() error_queue_mock.get.return_value = MagicMock() authenticated_plugin._steam_client.communication_queues = { 'plugin': plugin_queue_mock, 'websocket': websocket_queue_mock, 'errors': error_queue_mock } authenticated_plugin._auth_data = MagicMock() authenticated_plugin._get_websocket_auth_step = AsyncMock() authenticated_plugin._get_websocket_auth_step.return_value = UserActionRequired.NoActionRequired authenticated_plugin.store_credentials = MagicMock() authenticated_plugin._user_info_cache = MagicMock() assert isinstance( await authenticated_plugin.pass_login_credentials("", credentials, {}), Authentication) authenticated_plugin._get_websocket_auth_step.assert_called() authenticated_plugin.store_credentials.assert_called()
async def test_websocket_close_reconnect(client, protocol_client, websocket_list, websocket): websocket_list.get.side_effect = [ async_return_value(["wss://abc.com/websocket"]), async_return_value(["wss://abc.com/websocket"]) ] protocol_client.run.side_effect = [ async_raise(websockets.ConnectionClosedError(1002, ""), 10), async_raise(AssertionError) ] credentials_mock = {'password': PASSWORD, "two_factor": TWO_FACTOR} plugin_queue_mock = AsyncMock() websocket_queue_mock = AsyncMock() websocket_queue_mock.get.return_value = credentials_mock error_queue_mock = AsyncMock() error_queue_mock.get.return_value = MagicMock() client.communication_queues = {'plugin': plugin_queue_mock, 'websocket': websocket_queue_mock, 'errors': error_queue_mock} protocol_client.close.return_value = async_return_value(None) protocol_client.wait_closed.return_value = async_return_value(None) protocol_client.authenticate_token = AsyncMock() protocol_client.authenticate_token.return_value = async_return_value(None) websocket.close.return_value = async_return_value(None) websocket.wait_closed.return_value = async_return_value(None) client._user_info_cache = MagicMock() client._user_info_cache.old_flow = False with pytest.raises(AssertionError): await client.run() assert websocket_list.get.call_count == 2 assert protocol_client.authenticate_token.call_count == 2 assert protocol_client.run.call_count == 2
def steam_client(): mock = MagicMock(spec=()) mock.start = AsyncMock() mock.close = AsyncMock() mock.wait_closed = AsyncMock() mock.run = AsyncMock() mock.get_friends_info = AsyncMock() return mock
def protocol_client(mocker): protocol_client = mocker.patch( "protocol.websocket_client.ProtocolClient").return_value protocol_client.get_steam_app_ownership_ticket = AsyncMock( return_value=async_return_value(None)) protocol_client.register_auth_ticket_with_cm = AsyncMock( return_value=async_return_value(None)) return protocol_client
def twitch_launcher_mock(is_launcher_installed_mock, cookies_db_path_mock): twitch_launcher = MagicMock(spec=()) type(twitch_launcher).is_installed = is_launcher_installed_mock type(twitch_launcher).cookies_db_path = cookies_db_path_mock twitch_launcher.update_install_path = MagicMock() twitch_launcher.start_launcher = AsyncMock() twitch_launcher.launch_game = AsyncMock() twitch_launcher.uninstall_game = MagicMock() return twitch_launcher
def backend_client(): mock = MagicMock(spec=()) mock.get_profile = AsyncMock() mock.get_profile_data = AsyncMock() mock.get_games = AsyncMock() mock.get_achievements = AsyncMock() mock.get_friends = AsyncMock() mock.set_cookie_jar = MagicMock() mock.set_auth_lost_callback = MagicMock() mock.set_cookies_updated_callback = MagicMock() return mock
def steam_client(): mock = MagicMock(spec=()) mock.start = AsyncMock() mock.close = AsyncMock() mock.wait_closed = AsyncMock() mock.run = AsyncMock() mock.get_friends_info = AsyncMock() mock.get_friends = AsyncMock() mock.get_friends_nicknames = AsyncMock() mock.refresh_game_stats = AsyncMock() mock.communication_queues = {'plugin': AsyncMock(), 'websocket': AsyncMock()} return mock
async def test_refresh_token(http_client, http_request, oauth_response): http_request.return_value = oauth_response await http_client.authenticate_with_refresh_token("TOKEN") http_request.reset_mock() authorized_response = MagicMock() authorized_response.status = 200 http_client._authorized_get = AsyncMock() http_client._authorized_get.side_effect = [AuthenticationRequired(), authorized_response] http_client._authenticate = AsyncMock() response = await http_client.get("url") assert response.status == authorized_response.status
def websocket(mocker): websocket_ = MagicMock() mocker.patch( "protocol.websocket_client.websockets.connect", side_effect=lambda *args, **kwargs: async_return_value(AsyncMock()) ) return websocket_
async def test_no_games(authenticated_plugin, backend_client, miniprofile): backend_client.get_owned_ids.return_value = [] authenticated_plugin._games_cache = MagicMock() authenticated_plugin._games_cache.__iter__.return_value = [] authenticated_plugin._games_cache.wait_ready = AsyncMock() result = await authenticated_plugin.get_owned_games() assert result == []
async def test_license_import(client): licenses_to_check = {'123': {'shared': False}, '321': {'shared': True}} client._protobuf_client.get_packages_info = AsyncMock() await client._license_import_handler(licenses_to_check) client._games_cache.reset_storing_map.assert_called_once() client._protobuf_client.get_packages_info.assert_called_once_with( ['123', '321'])
async def test_license_import(client): licenses_to_check = [SteamLicense(ProtoResponse(123), False), SteamLicense(ProtoResponse(321), True)] client._protobuf_client.get_packages_info = AsyncMock() await client._license_import_handler(licenses_to_check) client._games_cache.reset_storing_map.assert_called_once() client._protobuf_client.get_packages_info.assert_called_once_with(licenses_to_check)
def backend_client(): mock = MagicMock(spec=()) mock.get_profile = AsyncMock() mock.get_profile_data = AsyncMock() mock.get_games = AsyncMock() mock.get_achievements = AsyncMock() mock.get_friends = AsyncMock() mock.get_authentication_data = AsyncMock() mock.set_cookie_jar = MagicMock() mock.set_auth_lost_callback = MagicMock() mock.set_cookies_updated_callback = MagicMock() mock.get_servers = AsyncMock() mock.get_owned_ids = AsyncMock() mock.get_steamcommunity_response_status = AsyncMock() return mock
def oauth_response(access_token, refresh_token, account_id): response = MagicMock() response.status = 200 response.json = AsyncMock() response.json.return_value = { "access_token": access_token, "refresh_token": refresh_token, "account_id": account_id } return response
async def test_connect_error_all_servers(client, protocol_client, websocket_list, mocker, exception): websocket_list.get.side_effect = [ async_return_value(["wss://websocket_1"]), async_return_value(["wss://websocket_1"]), ] connect = mocker.patch( "protocol.websocket_client.websockets.connect", side_effect=[ async_raise(exception), async_return_value(AsyncMock()) ] ) sleep = mocker.patch("protocol.websocket_client.asyncio.sleep", side_effect=lambda x: async_return_value(None)) protocol_client.run.return_value = async_raise(AssertionError) client._authenticate = AsyncMock() with pytest.raises(AssertionError): await client.run() connect.assert_has_calls([call("wss://websocket_1", max_size=ANY, ssl=ANY), call("wss://websocket_1", max_size=ANY, ssl=ANY)]) sleep.assert_any_call(RECONNECT_INTERVAL_SECONDS)
async def test_no_games(authenticated_plugin, backend_client, miniprofile): backend_client.get_owned_ids.return_value = [] authenticated_plugin._games_cache = MagicMock() authenticated_plugin._games_cache.__iter__.return_value = [ (281990, "Stellaris"), (236850, "Europa Universalis IV") ] authenticated_plugin._games_cache.wait_ready = AsyncMock() result = await authenticated_plugin.get_owned_games() assert result == [] backend_client.get_owned_ids.assert_called_with(miniprofile)
async def test_log_on_password_message(client, websocket): client._get_obfuscated_private_ip = AsyncMock(return_value=PRIVATE_IP) await client.log_on_password(ACCOUNT_NAME, PASSWORD, TWO_FACTOR, TWO_FACTOR_TYPE, MACHINE_ID, OS_VALUE, SENTRY) msg_to_send = str(websocket.send.call_args[0][0]) assert ACCOUNT_NAME in msg_to_send assert str(USED_SERVER_CELL_ID) in msg_to_send assert MACHINE_ID.decode('utf-8') in msg_to_send assert str(OS_VALUE) in msg_to_send assert str(PRIVATE_IP) in msg_to_send assert HOST_NAME in msg_to_send assert CLIENT_LANGUAGE in msg_to_send assert TWO_FACTOR in msg_to_send
async def test_ensure_open_exception(client, socket_state, monkeypatch, mocker): mocker.patch('asyncio.shield', AsyncMock(return_value=MagicMock())) client = ProtobufClient(websockets.WebSocketCommonProtocol()) client._socket.close_code = 1 client._socket.close_reason = "Close reason" client._socket.close_connection_task = MagicMock() client._socket.state = socket_state with pytest.raises( (websockets.ConnectionClosedError, websockets.InvalidState)): await client._get_obfuscated_private_ip()
async def test_servers_cache_retry(client, protocol_client, servers_cache, mocker, exception, websocket): servers_cache.get.side_effect = [ async_raise(exception), async_return_value(["wss://abc.com/websocket"]) ] protocol_client.run.return_value = async_raise(AssertionError) sleep = mocker.patch("protocol.websocket_client.asyncio.sleep", side_effect=lambda x: async_return_value(None)) client._authenticate = AsyncMock() with pytest.raises(AssertionError): await client.run() assert servers_cache.get.call_count == 2 sleep.assert_any_call(RECONNECT_INTERVAL_SECONDS)
async def function(steam_id, login, miniprofile, cache): plugin = create_plugin() plugin._user_info_cache.initialized.wait = AsyncMock() backend_client.get_profile.return_value = "http://url" backend_client.get_profile_data.return_value = steam_id, miniprofile, login credentials = {"account_id":"MTIz", "account_username":"******", "persona_name":"YWJj", "sentry":"Y2Jh", "steam_id":"MTIz", "token":"Y2Jh"} plugin.handshake_complete() await plugin.authenticate(credentials) return plugin
async def test_connect_error(client, protocol_client, websocket_list, mocker, exception): websocket_list.get.side_effect = [ async_return_value(["wss://websocket_1", "wss://websocket_2"]) ] connect = mocker.patch( "protocol.websocket_client.websockets.connect", side_effect=[ async_raise(exception), async_return_value(MagicMock()) ] ) protocol_client.run.return_value = async_raise(AssertionError) client._authenticate = AsyncMock() with pytest.raises(AssertionError): await client.run() connect.assert_has_calls([call("wss://websocket_1", max_size=ANY, ssl=ANY), call("wss://websocket_2", max_size=ANY, ssl=ANY)])
async def test_multiple_games(authenticated_plugin, backend_client, miniprofile): # only fields important for the logic backend_client.get_owned_ids.return_value = [ 281990, 236850, ] authenticated_plugin._games_cache = MagicMock() authenticated_plugin._games_cache.__iter__.return_value = [ (281990, "Stellaris"), (236850, "Europa Universalis IV") ] authenticated_plugin._games_cache.wait_ready = AsyncMock() result = await authenticated_plugin.get_owned_games() assert result == [ Game("281990", "Stellaris", [], LicenseInfo(LicenseType.SinglePurchase, None)), Game("236850", "Europa Universalis IV", [], LicenseInfo(LicenseType.SinglePurchase, None)) ]
def user_info_cache(): return AsyncMock()
async def test_user_info(client, protobuf_client, friends_cache): user_id = 15 user_info = ProtoUserInfo("Ola") friends_cache.update = AsyncMock() await protobuf_client.user_info_handler(user_id, user_info) friends_cache.update.assert_called_once_with(user_id, user_info)
def websocket(): websocket_ = MagicMock() websocket_.send = AsyncMock() return websocket_
def games_cache_mock(): mock = MagicMock(spec=()) mock.dump = Mock() mock.wait_ready = AsyncMock() return mock
def stream_reader(): reader = MagicMock() reader.read = AsyncMock() return reader
def reader(): stream = MagicMock(name="stream_reader") stream.read = AsyncMock() yield stream
def writer(): stream = MagicMock(name="stream_writer") stream.write = MagicMock() stream.drain = AsyncMock() yield stream