Compare commits

...

13 Commits
0.50 ... 0.55

Author SHA1 Message Date
Romuald Bierbasz
1585bab203 Wait for drain before writing 2019-10-22 11:30:01 +02:00
Aleksej Pawlowskij
92caf682d8 Increment version 2019-10-21 16:13:09 +02:00
Aleksej Pawlowskij
062d6a9428 Add user presence import feature 2019-10-21 16:11:10 +02:00
Romuald Juchnowicz-Bierbasz
c874bc1d6e Increment version 2019-10-21 14:54:23 +02:00
Romuald Bierbasz
2dc56571d6 Revert "Add more logs"
This reverts commit 21ab8bf33d3c2714b8c7af2da8036fe8baae20ea.
2019-10-21 14:50:33 +02:00
Romuald Bierbasz
eb216a50a8 Fix mypy ignores 2019-10-21 14:20:21 +02:00
Rafal Makagon
c9b1c8fcae Increment version 2019-10-15 12:58:48 +02:00
Aleksej Pawlowskij
a19a6cf11f Add Rockstar platform 2019-10-07 14:12:44 +02:00
Aleksej Pawlowskij
98cff9cfb8 SDK-3069: add OS compatibility import 2019-10-02 15:41:16 +02:00
Rafal Makagon
2e2aa8c4a0 Increment version 2019-10-01 11:21:32 +02:00
Rafal Makagon
f57e03db2d Add game library settings feature 2019-09-27 16:15:57 +02:00
Rafal Makagon
66085e2239 Revert "Add ignoring not having windll to mypy"
This reverts commit 55c7fcfd61e0391287e2717117da4fca03b77dec.
2019-09-27 15:37:21 +02:00
Romuald Bierbasz
4d3c9b78c4 Add RegistryMonitor 2019-09-25 11:46:18 +02:00
22 changed files with 1098 additions and 38 deletions

1
.gitignore vendored
View File

@@ -7,3 +7,4 @@ docs/build/
Pipfile Pipfile
.idea .idea
docs/source/_build docs/source/_build
.mypy_cache

View File

@@ -2,7 +2,7 @@
pytest==4.2.0 pytest==4.2.0
pytest-asyncio==0.10.0 pytest-asyncio==0.10.0
pytest-mock==1.10.3 pytest-mock==1.10.3
pytest-mypy==0.3.2 pytest-mypy==0.4.1
pytest-flakes==4.0.0 pytest-flakes==4.0.0
# because of pip bug https://github.com/pypa/pip/issues/4780 # because of pip bug https://github.com/pypa/pip/issues/4780
aiohttp==3.5.4 aiohttp==3.5.4

View File

@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup( setup(
name="galaxy.plugin.api", name="galaxy.plugin.api",
version="0.50", version="0.55",
description="GOG Galaxy Integrations Python API", description="GOG Galaxy Integrations Python API",
author='Galaxy team', author='Galaxy team',
author_email='galaxy@gog.com', author_email='galaxy@gog.com',

View File

@@ -1 +1 @@
__path__: str = __import__('pkgutil').extend_path(__path__, __name__) __path__: str = __import__('pkgutil').extend_path(__path__, __name__) # type: ignore

View File

@@ -90,6 +90,7 @@ class Platform(Enum):
Playfire = "playfire" Playfire = "playfire"
Oculus = "oculus" Oculus = "oculus"
Test = "test" Test = "test"
Rockstar = "rockstar"
class Feature(Enum): class Feature(Enum):
@@ -110,6 +111,9 @@ class Feature(Enum):
ImportFriends = "ImportFriends" ImportFriends = "ImportFriends"
ShutdownPlatformClient = "ShutdownPlatformClient" ShutdownPlatformClient = "ShutdownPlatformClient"
LaunchPlatformClient = "LaunchPlatformClient" LaunchPlatformClient = "LaunchPlatformClient"
ImportGameLibrarySettings = "ImportGameLibrarySettings"
ImportOSCompatibility = "ImportOSCompatibility"
ImportUserPresence = "ImportUserPresence"
class LicenseType(Enum): class LicenseType(Enum):
@@ -128,3 +132,20 @@ class LocalGameState(Flag):
None_ = 0 None_ = 0
Installed = 1 Installed = 1
Running = 2 Running = 2
class OSCompatibility(Flag):
"""Possible game OS compatibility.
Use "bitwise or" to express multiple OSs compatibility, e.g. ``os=OSCompatibility.Windows|OSCompatibility.MacOS``
"""
Windows = 0b001
MacOS = 0b010
Linux = 0b100
class PresenceState(Enum):
""""Possible states of a user."""
Unknown = "unknown"
Online = "online"
Offline = "offline"
Away = "away"

View File

@@ -88,6 +88,7 @@ class Server():
self._methods = {} self._methods = {}
self._notifications = {} self._notifications = {}
self._task_manager = TaskManager("jsonrpc server") self._task_manager = TaskManager("jsonrpc server")
self._write_lock = asyncio.Lock()
def register_method(self, name, callback, immediate, sensitive_params=False): def register_method(self, name, callback, immediate, sensitive_params=False):
""" """
@@ -129,8 +130,9 @@ class Server():
await asyncio.sleep(0) # To not starve task queue await asyncio.sleep(0) # To not starve task queue
def close(self): def close(self):
logging.info("Closing JSON-RPC server - not more messages will be read") if self._active:
self._active = False logging.info("Closing JSON-RPC server - not more messages will be read")
self._active = False
async def wait_closed(self): async def wait_closed(self):
await self._task_manager.wait() await self._task_manager.wait()
@@ -222,12 +224,16 @@ class Server():
raise InvalidRequest() raise InvalidRequest()
def _send(self, data): def _send(self, data):
async def send_task(data_):
async with self._write_lock:
self._writer.write(data_)
await self._writer.drain()
try: try:
line = self._encoder.encode(data) line = self._encoder.encode(data)
logging.debug("Sending data: %s", line) logging.debug("Sending data: %s", line)
data = (line + "\n").encode("utf-8") data = (line + "\n").encode("utf-8")
self._writer.write(data) self._task_manager.create_task(send_task(data), "send")
self._task_manager.create_task(self._writer.drain(), "drain")
except TypeError as error: except TypeError as error:
logging.error(str(error)) logging.error(str(error))
@@ -262,6 +268,7 @@ class NotificationClient():
self._encoder = encoder self._encoder = encoder
self._methods = {} self._methods = {}
self._task_manager = TaskManager("notification client") self._task_manager = TaskManager("notification client")
self._write_lock = asyncio.Lock()
def notify(self, method, params, sensitive_params=False): def notify(self, method, params, sensitive_params=False):
""" """
@@ -281,15 +288,20 @@ class NotificationClient():
self._send(notification) self._send(notification)
async def close(self): async def close(self):
self._task_manager.cancel()
await self._task_manager.wait() await self._task_manager.wait()
def _send(self, data): def _send(self, data):
async def send_task(data_):
async with self._write_lock:
self._writer.write(data_)
await self._writer.drain()
try: try:
line = self._encoder.encode(data) line = self._encoder.encode(data)
data = (line + "\n").encode("utf-8") data = (line + "\n").encode("utf-8")
logging.debug("Sending %d byte of data", len(data)) logging.debug("Sending %d byte of data", len(data))
self._writer.write(data) self._task_manager.create_task(send_task(data), "send")
self._task_manager.create_task(self._writer.drain(), "drain")
except TypeError as error: except TypeError as error:
logging.error("Failed to parse outgoing message: %s", str(error)) logging.error("Failed to parse outgoing message: %s", str(error))

View File

@@ -7,12 +7,15 @@ import sys
from enum import Enum from enum import Enum
from typing import Any, Dict, List, Optional, Set, Union from typing import Any, Dict, List, Optional, Set, Union
from galaxy.api.consts import Feature from galaxy.api.consts import Feature, OSCompatibility
from galaxy.api.errors import ImportInProgress, UnknownError from galaxy.api.errors import ImportInProgress, UnknownError
from galaxy.api.jsonrpc import ApplicationError, NotificationClient, Server from galaxy.api.jsonrpc import ApplicationError, NotificationClient, Server
from galaxy.api.types import Achievement, Authentication, FriendInfo, Game, GameTime, LocalGame, NextStep from galaxy.api.types import (
Achievement, Authentication, FriendInfo, Game, GameLibrarySettings, GameTime, LocalGame, NextStep, UserPresence
)
from galaxy.task_manager import TaskManager from galaxy.task_manager import TaskManager
class JSONEncoder(json.JSONEncoder): class JSONEncoder(json.JSONEncoder):
def default(self, o): # pylint: disable=method-hidden def default(self, o): # pylint: disable=method-hidden
if dataclasses.is_dataclass(o): if dataclasses.is_dataclass(o):
@@ -46,6 +49,9 @@ class Plugin:
self._achievements_import_in_progress = False self._achievements_import_in_progress = False
self._game_times_import_in_progress = False self._game_times_import_in_progress = False
self._game_library_settings_import_in_progress = False
self._os_compatibility_import_in_progress = False
self._user_presence_import_in_progress = False
self._persistent_cache = dict() self._persistent_cache = dict()
@@ -109,6 +115,15 @@ class Plugin:
self._register_method("start_game_times_import", self._start_game_times_import) self._register_method("start_game_times_import", self._start_game_times_import)
self._detect_feature(Feature.ImportGameTime, ["get_game_time"]) self._detect_feature(Feature.ImportGameTime, ["get_game_time"])
self._register_method("start_game_library_settings_import", self._start_game_library_settings_import)
self._detect_feature(Feature.ImportGameLibrarySettings, ["get_game_library_settings"])
self._register_method("start_os_compatibility_import", self._start_os_compatibility_import)
self._detect_feature(Feature.ImportOSCompatibility, ["get_os_compatibility"])
self._register_method("start_user_presence_import", self._start_user_presence_import)
self._detect_feature(Feature.ImportUserPresence, ["get_user_presence"])
async def __aenter__(self): async def __aenter__(self):
return self return self
@@ -169,11 +184,13 @@ class Plugin:
def _wrap_external_method(self, handler, name: str): def _wrap_external_method(self, handler, name: str):
async def wrapper(*args, **kwargs): async def wrapper(*args, **kwargs):
return await self._external_task_manager.create_task(handler(*args, **kwargs), name, False) return await self._external_task_manager.create_task(handler(*args, **kwargs), name, False)
return wrapper return wrapper
async def run(self): async def run(self):
"""Plugin's main coroutine.""" """Plugin's main coroutine."""
await self._server.run() await self._server.run()
logging.debug("Plugin run loop finished")
def close(self) -> None: def close(self) -> None:
if not self._active: if not self._active:
@@ -186,10 +203,12 @@ class Plugin:
self._active = False self._active = False
async def wait_closed(self) -> None: async def wait_closed(self) -> None:
logging.debug("Waiting for plugin to close")
await self._external_task_manager.wait() await self._external_task_manager.wait()
await self._internal_task_manager.wait() await self._internal_task_manager.wait()
await self._server.wait_closed() await self._server.wait_closed()
await self._notification_client.close() await self._notification_client.close()
logging.debug("Plugin closed")
def create_task(self, coro, description): def create_task(self, coro, description):
"""Wrapper around asyncio.create_task - takes care of canceling tasks on shutdown""" """Wrapper around asyncio.create_task - takes care of canceling tasks on shutdown"""
@@ -252,7 +271,7 @@ class Plugin:
""" """
# temporary solution for persistent_cache vs credentials issue # temporary solution for persistent_cache vs credentials issue
self.persistent_cache['credentials'] = credentials # type: ignore self.persistent_cache["credentials"] = credentials # type: ignore
self._notification_client.notify("store_credentials", credentials, sensitive_params=True) self._notification_client.notify("store_credentials", credentials, sensitive_params=True)
@@ -402,6 +421,62 @@ class Plugin:
def _game_times_import_finished(self) -> None: def _game_times_import_finished(self) -> None:
self._notification_client.notify("game_times_import_finished", None) self._notification_client.notify("game_times_import_finished", None)
def _game_library_settings_import_success(self, game_library_settings: GameLibrarySettings) -> None:
params = {"game_library_settings": game_library_settings}
self._notification_client.notify("game_library_settings_import_success", params)
def _game_library_settings_import_failure(self, game_id: str, error: ApplicationError) -> None:
params = {
"game_id": game_id,
"error": error.json()
}
self._notification_client.notify("game_library_settings_import_failure", params)
def _game_library_settings_import_finished(self) -> None:
self._notification_client.notify("game_library_settings_import_finished", None)
def _os_compatibility_import_success(self, game_id: str, os_compatibility: Optional[OSCompatibility]) -> None:
self._notification_client.notify(
"os_compatibility_import_success",
{
"game_id": game_id,
"os_compatibility": os_compatibility
}
)
def _os_compatibility_import_failure(self, game_id: str, error: ApplicationError) -> None:
self._notification_client.notify(
"os_compatibility_import_failure",
{
"game_id": game_id,
"error": error.json()
}
)
def _os_compatibility_import_finished(self) -> None:
self._notification_client.notify("os_compatibility_import_finished", None)
def _user_presence_import_success(self, user_id: str, user_presence: UserPresence) -> None:
self._notification_client.notify(
"user_presence_import_success",
{
"user_id": user_id,
"presence": user_presence
}
)
def _user_presence_import_failure(self, user_id: str, error: ApplicationError) -> None:
self._notification_client.notify(
"user_presence_import_failure",
{
"user_id": user_id,
"error": error.json()
}
)
def _user_presence_import_finished(self) -> None:
self._notification_client.notify("user_presence_import_finished", None)
def lost_authentication(self) -> None: def lost_authentication(self) -> None:
"""Notify the client that integration has lost authentication for the """Notify the client that integration has lost authentication for the
current user and is unable to perform actions which would require it. current user and is unable to perform actions which would require it.
@@ -750,6 +825,176 @@ class Plugin:
(like updating cache). (like updating cache).
""" """
async def _start_game_library_settings_import(self, game_ids: List[str]) -> None:
if self._game_library_settings_import_in_progress:
raise ImportInProgress()
context = await self.prepare_game_library_settings_context(game_ids)
async def import_game_library_settings(game_id, context_):
try:
game_library_settings = await self.get_game_library_settings(game_id, context_)
self._game_library_settings_import_success(game_library_settings)
except ApplicationError as error:
self._game_library_settings_import_failure(game_id, error)
except Exception:
logging.exception("Unexpected exception raised in import_game_library_settings")
self._game_library_settings_import_failure(game_id, UnknownError())
async def import_game_library_settings_set(game_ids_, context_):
try:
imports = [import_game_library_settings(game_id, context_) for game_id in game_ids_]
await asyncio.gather(*imports)
finally:
self._game_library_settings_import_finished()
self._game_library_settings_import_in_progress = False
self.game_library_settings_import_complete()
self._external_task_manager.create_task(
import_game_library_settings_set(game_ids, context),
"game library settings import",
handle_exceptions=False
)
self._game_library_settings_import_in_progress = True
async def prepare_game_library_settings_context(self, game_ids: List[str]) -> Any:
"""Override this method to prepare context for get_game_library_settings.
This allows for optimizations like batch requests to platform API.
Default implementation returns None.
:param game_ids: the ids of the games for which game library settings are imported
:return: context
"""
return None
async def get_game_library_settings(self, game_id: str, context: Any) -> GameLibrarySettings:
"""Override this method to return the game library settings for the game
identified by the provided game_id.
This method is called by import task initialized by GOG Galaxy Client.
:param game_id: the id of the game for which the game library settings are imported
:param context: the value returned from :meth:`prepare_game_library_settings_context`
:return: GameLibrarySettings object
"""
raise NotImplementedError()
def game_library_settings_import_complete(self) -> None:
"""Override this method to handle operations after game library settings import is finished
(like updating cache).
"""
async def _start_os_compatibility_import(self, game_ids: List[str]) -> None:
if self._os_compatibility_import_in_progress:
raise ImportInProgress()
context = await self.prepare_os_compatibility_context(game_ids)
async def import_os_compatibility(game_id, context_):
try:
os_compatibility = await self.get_os_compatibility(game_id, context_)
self._os_compatibility_import_success(game_id, os_compatibility)
except ApplicationError as error:
self._os_compatibility_import_failure(game_id, error)
except Exception:
logging.exception("Unexpected exception raised in import_os_compatibility")
self._os_compatibility_import_failure(game_id, UnknownError())
async def import_os_compatibility_set(game_ids_, context_):
try:
await asyncio.gather(*[
import_os_compatibility(game_id, context_) for game_id in game_ids_
])
finally:
self._os_compatibility_import_finished()
self._os_compatibility_import_in_progress = False
self.os_compatibility_import_complete()
self._external_task_manager.create_task(
import_os_compatibility_set(game_ids, context),
"game OS compatibility import",
handle_exceptions=False
)
self._os_compatibility_import_in_progress = True
async def prepare_os_compatibility_context(self, game_ids: List[str]) -> Any:
"""Override this method to prepare context for get_os_compatibility.
This allows for optimizations like batch requests to platform API.
Default implementation returns None.
:param game_ids: the ids of the games for which game os compatibility is imported
:return: context
"""
return None
async def get_os_compatibility(self, game_id: str, context: Any) -> Optional[OSCompatibility]:
"""Override this method to return the OS compatibility for the game with the provided game_id.
This method is called by import task initialized by GOG Galaxy Client.
:param game_id: the id of the game for which the game os compatibility is imported
:param context: the value returned from :meth:`prepare_os_compatibility_context`
:return: OSCompatibility flags indicating compatible OSs, or None if compatibility is not know
"""
raise NotImplementedError()
def os_compatibility_import_complete(self) -> None:
"""Override this method to handle operations after OS compatibility import is finished (like updating cache)."""
async def _start_user_presence_import(self, user_ids: List[str]) -> None:
if self._user_presence_import_in_progress:
raise ImportInProgress()
context = await self.prepare_user_presence_context(user_ids)
async def import_user_presence(user_id, context_) -> None:
try:
self._user_presence_import_success(user_id, await self.get_user_presence(user_id, context_))
except ApplicationError as error:
self._user_presence_import_failure(user_id, error)
except Exception:
logging.exception("Unexpected exception raised in import_user_presence")
self._user_presence_import_failure(user_id, UnknownError())
async def import_user_presence_set(user_ids_, context_) -> None:
try:
await asyncio.gather(*[
import_user_presence(user_id, context_)
for user_id in user_ids_
])
finally:
self._user_presence_import_finished()
self._user_presence_import_in_progress = False
self.user_presence_import_complete()
self._external_task_manager.create_task(
import_user_presence_set(user_ids, context),
"user presence import",
handle_exceptions=False
)
self._user_presence_import_in_progress = True
async def prepare_user_presence_context(self, user_ids: List[str]) -> Any:
"""Override this method to prepare context for get_user_presence.
This allows for optimizations like batch requests to platform API.
Default implementation returns None.
:param user_ids: the ids of the users for whom presence information is imported
:return: context
"""
return None
async def get_user_presence(self, user_id: str, context: Any) -> UserPresence:
"""Override this method to return presence information for the user with the provided user_id.
This method is called by import task initialized by GOG Galaxy Client.
:param user_id: the id of the user for whom presence information is imported
:param context: the value returned from :meth:`prepare_user_presence_context`
:return: UserPresence presence information of the provided user
"""
raise NotImplementedError()
def user_presence_import_complete(self) -> None:
"""Override this method to handle operations after presence import is finished (like updating cache)."""
def create_and_run_plugin(plugin_class, argv): def create_and_run_plugin(plugin_class, argv):
"""Call this method as an entry point for the implemented integration. """Call this method as an entry point for the implemented integration.

View File

@@ -1,10 +1,11 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import List, Dict, Optional from typing import Dict, List, Optional
from galaxy.api.consts import LicenseType, LocalGameState, PresenceState
from galaxy.api.consts import LicenseType, LocalGameState
@dataclass @dataclass
class Authentication(): class Authentication:
"""Return this from :meth:`.authenticate` or :meth:`.pass_login_credentials` """Return this from :meth:`.authenticate` or :meth:`.pass_login_credentials`
to inform the client that authentication has successfully finished. to inform the client that authentication has successfully finished.
@@ -14,8 +15,9 @@ class Authentication():
user_id: str user_id: str
user_name: str user_name: str
@dataclass @dataclass
class Cookie(): class Cookie:
"""Cookie """Cookie
:param name: name of the cookie :param name: name of the cookie
@@ -28,8 +30,9 @@ class Cookie():
domain: Optional[str] = None domain: Optional[str] = None
path: Optional[str] = None path: Optional[str] = None
@dataclass @dataclass
class NextStep(): class NextStep:
"""Return this from :meth:`.authenticate` or :meth:`.pass_login_credentials` to open client built-in browser with given url. """Return this from :meth:`.authenticate` or :meth:`.pass_login_credentials` to open client built-in browser with given url.
For example: For example:
@@ -67,8 +70,9 @@ class NextStep():
cookies: Optional[List[Cookie]] = None cookies: Optional[List[Cookie]] = None
js: Optional[Dict[str, List[str]]] = None js: Optional[Dict[str, List[str]]] = None
@dataclass @dataclass
class LicenseInfo(): class LicenseInfo:
"""Information about the license of related product. """Information about the license of related product.
:param license_type: type of license :param license_type: type of license
@@ -77,8 +81,9 @@ class LicenseInfo():
license_type: LicenseType license_type: LicenseType
owner: Optional[str] = None owner: Optional[str] = None
@dataclass @dataclass
class Dlc(): class Dlc:
"""Downloadable content object. """Downloadable content object.
:param dlc_id: id of the dlc :param dlc_id: id of the dlc
@@ -89,8 +94,9 @@ class Dlc():
dlc_title: str dlc_title: str
license_info: LicenseInfo license_info: LicenseInfo
@dataclass @dataclass
class Game(): class Game:
"""Game object. """Game object.
:param game_id: unique identifier of the game, this will be passed as parameter for methods such as launch_game :param game_id: unique identifier of the game, this will be passed as parameter for methods such as launch_game
@@ -103,8 +109,9 @@ class Game():
dlcs: Optional[List[Dlc]] dlcs: Optional[List[Dlc]]
license_info: LicenseInfo license_info: LicenseInfo
@dataclass @dataclass
class Achievement(): class Achievement:
"""Achievement, has to be initialized with either id or name. """Achievement, has to be initialized with either id or name.
:param unlock_time: unlock time of the achievement :param unlock_time: unlock time of the achievement
@@ -119,8 +126,9 @@ class Achievement():
assert self.achievement_id or self.achievement_name, \ assert self.achievement_id or self.achievement_name, \
"One of achievement_id or achievement_name is required" "One of achievement_id or achievement_name is required"
@dataclass @dataclass
class LocalGame(): class LocalGame:
"""Game locally present on the authenticated user's computer. """Game locally present on the authenticated user's computer.
:param game_id: id of the game :param game_id: id of the game
@@ -129,8 +137,9 @@ class LocalGame():
game_id: str game_id: str
local_game_state: LocalGameState local_game_state: LocalGameState
@dataclass @dataclass
class FriendInfo(): class FriendInfo:
"""Information about a friend of the currently authenticated user. """Information about a friend of the currently authenticated user.
:param user_id: id of the user :param user_id: id of the user
@@ -139,8 +148,9 @@ class FriendInfo():
user_id: str user_id: str
user_name: str user_name: str
@dataclass @dataclass
class GameTime(): class GameTime:
"""Game time of a game, defines the total time spent in the game """Game time of a game, defines the total time spent in the game
and the last time the game was played. and the last time the game was played.
@@ -151,3 +161,31 @@ class GameTime():
game_id: str game_id: str
time_played: Optional[int] time_played: Optional[int]
last_played_time: Optional[int] last_played_time: Optional[int]
@dataclass
class GameLibrarySettings:
"""Library settings of a game, defines assigned tags and visibility flag.
:param game_id: id of the related game
:param tags: collection of tags assigned to the game
:param hidden: indicates if the game should be hidden in GOG Galaxy application
"""
game_id: str
tags: Optional[List[str]]
hidden: Optional[bool]
@dataclass
class UserPresence:
"""Presence information of a user.
:param presence_state: the state of the user
:param game_id: id of the game a user is currently in
:param game_title: name of the game a user is currently in
:param presence_status: detailed user's presence description
"""
presence_state: PresenceState
game_id: Optional[str] = None
game_title: Optional[str] = None
presence_status: Optional[str] = None

View File

@@ -78,7 +78,8 @@ def create_tcp_connector(*args, **kwargs) -> aiohttp.TCPConnector:
ssl_context.load_verify_locations(certifi.where()) ssl_context.load_verify_locations(certifi.where())
kwargs.setdefault("ssl", ssl_context) kwargs.setdefault("ssl", ssl_context)
kwargs.setdefault("limit", DEFAULT_LIMIT) kwargs.setdefault("limit", DEFAULT_LIMIT)
return aiohttp.TCPConnector(*args, **kwargs) # type: ignore due to https://github.com/python/mypy/issues/4001 # due to https://github.com/python/mypy/issues/4001
return aiohttp.TCPConnector(*args, **kwargs) # type: ignore
def create_client_session(*args, **kwargs) -> aiohttp.ClientSession: def create_client_session(*args, **kwargs) -> aiohttp.ClientSession:
@@ -103,7 +104,8 @@ def create_client_session(*args, **kwargs) -> aiohttp.ClientSession:
kwargs.setdefault("connector", create_tcp_connector()) kwargs.setdefault("connector", create_tcp_connector())
kwargs.setdefault("timeout", aiohttp.ClientTimeout(total=DEFAULT_TIMEOUT)) kwargs.setdefault("timeout", aiohttp.ClientTimeout(total=DEFAULT_TIMEOUT))
kwargs.setdefault("raise_for_status", True) kwargs.setdefault("raise_for_status", True)
return aiohttp.ClientSession(*args, **kwargs) # type: ignore due to https://github.com/python/mypy/issues/4001 # due to https://github.com/python/mypy/issues/4001
return aiohttp.ClientSession(*args, **kwargs) # type: ignore
@contextmanager @contextmanager

View File

@@ -0,0 +1,98 @@
import sys
if sys.platform == "win32":
import logging
import ctypes
from ctypes.wintypes import LONG, HKEY, LPCWSTR, DWORD, BOOL, HANDLE, LPVOID
LPSECURITY_ATTRIBUTES = LPVOID
RegOpenKeyEx = ctypes.windll.advapi32.RegOpenKeyExW
RegOpenKeyEx.restype = LONG
RegOpenKeyEx.argtypes = [HKEY, LPCWSTR, DWORD, DWORD, ctypes.POINTER(HKEY)]
RegCloseKey = ctypes.windll.advapi32.RegCloseKey
RegCloseKey.restype = LONG
RegCloseKey.argtypes = [HKEY]
RegNotifyChangeKeyValue = ctypes.windll.advapi32.RegNotifyChangeKeyValue
RegNotifyChangeKeyValue.restype = LONG
RegNotifyChangeKeyValue.argtypes = [HKEY, BOOL, DWORD, HANDLE, BOOL]
CloseHandle = ctypes.windll.kernel32.CloseHandle
CloseHandle.restype = BOOL
CloseHandle.argtypes = [HANDLE]
CreateEvent = ctypes.windll.kernel32.CreateEventW
CreateEvent.restype = BOOL
CreateEvent.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCWSTR]
WaitForSingleObject = ctypes.windll.kernel32.WaitForSingleObject
WaitForSingleObject.restype = DWORD
WaitForSingleObject.argtypes = [HANDLE, DWORD]
ERROR_SUCCESS = 0x00000000
KEY_READ = 0x00020019
KEY_QUERY_VALUE = 0x00000001
REG_NOTIFY_CHANGE_NAME = 0x00000001
REG_NOTIFY_CHANGE_LAST_SET = 0x00000004
WAIT_OBJECT_0 = 0x00000000
WAIT_TIMEOUT = 0x00000102
class RegistryMonitor:
def __init__(self, root, subkey):
self._root = root
self._subkey = subkey
self._event = CreateEvent(None, False, False, None)
self._key = None
self._open_key()
if self._key:
self._set_key_update_notification()
def close(self):
CloseHandle(self._event)
if self._key:
RegCloseKey(self._key)
self._key = None
def is_updated(self):
wait_result = WaitForSingleObject(self._event, 0)
# previously watched
if wait_result == WAIT_OBJECT_0:
self._set_key_update_notification()
return True
# no changes or no key before
if wait_result != WAIT_TIMEOUT:
# unexpected error
logging.warning("Unexpected WaitForSingleObject result %s", wait_result)
return False
if self._key is None:
self._open_key()
if self._key is None:
return False
self._set_key_update_notification()
return True
def _set_key_update_notification(self):
filter_ = REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET
status = RegNotifyChangeKeyValue(self._key, True, filter_, self._event, True)
if status != ERROR_SUCCESS:
# key was deleted
RegCloseKey(self._key)
self._key = None
def _open_key(self):
access = KEY_QUERY_VALUE | KEY_READ
self._key = HKEY()
rc = RegOpenKeyEx(self._root, self._subkey, 0, access, ctypes.byref(self._key))
if rc != ERROR_SUCCESS:
self._key = None

View File

@@ -1,33 +1,38 @@
from contextlib import ExitStack
import logging import logging
from unittest.mock import patch, MagicMock from contextlib import ExitStack
from unittest.mock import MagicMock, patch
import pytest import pytest
from galaxy.api.plugin import Plugin
from galaxy.api.consts import Platform from galaxy.api.consts import Platform
from galaxy.api.plugin import Plugin
from galaxy.unittest.mock import async_return_value from galaxy.unittest.mock import async_return_value
@pytest.fixture() @pytest.fixture()
def reader(): def reader():
stream = MagicMock(name="stream_reader") stream = MagicMock(name="stream_reader")
stream.read = MagicMock() stream.read = MagicMock()
yield stream yield stream
@pytest.fixture() @pytest.fixture()
async def writer(): async def writer():
stream = MagicMock(name="stream_writer") stream = MagicMock(name="stream_writer")
stream.drain.side_effect = lambda: async_return_value(None) stream.drain.side_effect = lambda: async_return_value(None)
yield stream yield stream
@pytest.fixture() @pytest.fixture()
def read(reader): def read(reader):
yield reader.read yield reader.read
@pytest.fixture() @pytest.fixture()
def write(writer): def write(writer):
yield writer.write yield writer.write
@pytest.fixture() @pytest.fixture()
async def plugin(reader, writer): async def plugin(reader, writer):
"""Return plugin instance with all feature methods mocked""" """Return plugin instance with all feature methods mocked"""
@@ -49,7 +54,16 @@ async def plugin(reader, writer):
"game_times_import_complete", "game_times_import_complete",
"shutdown_platform_client", "shutdown_platform_client",
"shutdown", "shutdown",
"tick" "tick",
"get_game_library_settings",
"prepare_game_library_settings_context",
"game_library_settings_import_complete",
"get_os_compatibility",
"prepare_os_compatibility_context",
"os_compatibility_import_complete",
"get_user_presence",
"prepare_user_presence_context",
"user_presence_import_complete",
) )
with ExitStack() as stack: with ExitStack() as stack:

View File

@@ -5,7 +5,7 @@ from pytest import raises
from galaxy.api.types import Achievement from galaxy.api.types import Achievement
from galaxy.api.errors import BackendError from galaxy.api.errors import BackendError
from galaxy.unittest.mock import async_return_value from galaxy.unittest.mock import async_return_value, skip_loop
from tests import create_message, get_messages from tests import create_message, get_messages
@@ -201,6 +201,7 @@ async def test_import_in_progress(plugin, read, write):
async def test_unlock_achievement(plugin, write): async def test_unlock_achievement(plugin, write):
achievement = Achievement(achievement_id="lvl20", unlock_time=1548422395) achievement = Achievement(achievement_id="lvl20", unlock_time=1548422395)
plugin.unlock_achievement("14", achievement) plugin.unlock_achievement("14", achievement)
await skip_loop()
response = json.loads(write.call_args[0][0]) response = json.loads(write.call_args[0][0])
assert response == { assert response == {

View File

@@ -5,7 +5,7 @@ from galaxy.api.errors import (
UnknownError, InvalidCredentials, NetworkError, LoggedInElsewhere, ProtocolError, UnknownError, InvalidCredentials, NetworkError, LoggedInElsewhere, ProtocolError,
BackendNotAvailable, BackendTimeout, BackendError, TemporaryBlocked, Banned, AccessDenied BackendNotAvailable, BackendTimeout, BackendError, TemporaryBlocked, Banned, AccessDenied
) )
from galaxy.unittest.mock import async_return_value from galaxy.unittest.mock import async_return_value, skip_loop
from tests import create_message, get_messages from tests import create_message, get_messages
@@ -97,6 +97,7 @@ async def test_store_credentials(plugin, write):
"token": "ABC" "token": "ABC"
} }
plugin.store_credentials(credentials) plugin.store_credentials(credentials)
await skip_loop()
assert get_messages(write) == [ assert get_messages(write) == [
{ {
@@ -110,6 +111,7 @@ async def test_store_credentials(plugin, write):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_lost_authentication(plugin, write): async def test_lost_authentication(plugin, write):
plugin.lost_authentication() plugin.lost_authentication()
await skip_loop()
assert get_messages(write) == [ assert get_messages(write) == [
{ {

View File

@@ -14,7 +14,10 @@ def test_base_class():
Feature.ImportGameTime, Feature.ImportGameTime,
Feature.ImportFriends, Feature.ImportFriends,
Feature.ShutdownPlatformClient, Feature.ShutdownPlatformClient,
Feature.LaunchPlatformClient Feature.LaunchPlatformClient,
Feature.ImportGameLibrarySettings,
Feature.ImportOSCompatibility,
Feature.ImportUserPresence
} }

View File

@@ -1,6 +1,6 @@
from galaxy.api.types import FriendInfo from galaxy.api.types import FriendInfo
from galaxy.api.errors import UnknownError from galaxy.api.errors import UnknownError
from galaxy.unittest.mock import async_return_value from galaxy.unittest.mock import async_return_value, skip_loop
import pytest import pytest
@@ -67,6 +67,7 @@ async def test_add_friend(plugin, write):
friend = FriendInfo("7", "Kuba") friend = FriendInfo("7", "Kuba")
plugin.add_friend(friend) plugin.add_friend(friend)
await skip_loop()
assert get_messages(write) == [ assert get_messages(write) == [
{ {
@@ -82,6 +83,7 @@ async def test_add_friend(plugin, write):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_remove_friend(plugin, write): async def test_remove_friend(plugin, write):
plugin.remove_friend("5") plugin.remove_friend("5")
await skip_loop()
assert get_messages(write) == [ assert get_messages(write) == [
{ {

View File

@@ -0,0 +1,196 @@
from unittest.mock import call
import pytest
from galaxy.api.types import GameLibrarySettings
from galaxy.api.errors import BackendError
from galaxy.unittest.mock import async_return_value
from tests import create_message, get_messages
@pytest.mark.asyncio
async def test_get_library_settings_success(plugin, read, write):
plugin.prepare_game_library_settings_context.return_value = async_return_value("abc")
request = {
"jsonrpc": "2.0",
"id": "3",
"method": "start_game_library_settings_import",
"params": {
"game_ids": ["3", "5", "7"]
}
}
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
plugin.get_game_library_settings.side_effect = [
async_return_value(GameLibrarySettings("3", None, True)),
async_return_value(GameLibrarySettings("5", [], False)),
async_return_value(GameLibrarySettings("7", ["tag1", "tag2", "tag3"], None)),
]
await plugin.run()
plugin.get_game_library_settings.assert_has_calls([
call("3", "abc"),
call("5", "abc"),
call("7", "abc"),
])
plugin.game_library_settings_import_complete.assert_called_once_with()
assert get_messages(write) == [
{
"jsonrpc": "2.0",
"id": "3",
"result": None
},
{
"jsonrpc": "2.0",
"method": "game_library_settings_import_success",
"params": {
"game_library_settings": {
"game_id": "3",
"hidden": True
}
}
},
{
"jsonrpc": "2.0",
"method": "game_library_settings_import_success",
"params": {
"game_library_settings": {
"game_id": "5",
"tags": [],
"hidden": False
}
}
},
{
"jsonrpc": "2.0",
"method": "game_library_settings_import_success",
"params": {
"game_library_settings": {
"game_id": "7",
"tags": ["tag1", "tag2", "tag3"]
}
}
},
{
"jsonrpc": "2.0",
"method": "game_library_settings_import_finished",
"params": None
}
]
@pytest.mark.asyncio
@pytest.mark.parametrize("exception,code,message", [
(BackendError, 4, "Backend error"),
(KeyError, 0, "Unknown error")
])
async def test_get_game_library_settings_error(exception, code, message, plugin, read, write):
plugin.prepare_game_library_settings_context.return_value = async_return_value(None)
request = {
"jsonrpc": "2.0",
"id": "3",
"method": "start_game_library_settings_import",
"params": {
"game_ids": ["6"]
}
}
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
plugin.get_game_library_settings.side_effect = exception
await plugin.run()
plugin.get_game_library_settings.assert_called()
plugin.game_library_settings_import_complete.assert_called_once_with()
assert get_messages(write) == [
{
"jsonrpc": "2.0",
"id": "3",
"result": None
},
{
"jsonrpc": "2.0",
"method": "game_library_settings_import_failure",
"params": {
"game_id": "6",
"error": {
"code": code,
"message": message
}
}
},
{
"jsonrpc": "2.0",
"method": "game_library_settings_import_finished",
"params": None
}
]
@pytest.mark.asyncio
async def test_prepare_get_game_library_settings_context_error(plugin, read, write):
plugin.prepare_game_library_settings_context.side_effect = BackendError()
request = {
"jsonrpc": "2.0",
"id": "3",
"method": "start_game_library_settings_import",
"params": {
"game_ids": ["6"]
}
}
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
await plugin.run()
assert get_messages(write) == [
{
"jsonrpc": "2.0",
"id": "3",
"error": {
"code": 4,
"message": "Backend error"
}
}
]
@pytest.mark.asyncio
async def test_import_in_progress(plugin, read, write):
plugin.prepare_game_library_settings_context.return_value = async_return_value(None)
requests = [
{
"jsonrpc": "2.0",
"id": "3",
"method": "start_game_library_settings_import",
"params": {
"game_ids": ["6"]
}
},
{
"jsonrpc": "2.0",
"id": "4",
"method": "start_game_library_settings_import",
"params": {
"game_ids": ["7"]
}
}
]
read.side_effect = [
async_return_value(create_message(requests[0])),
async_return_value(create_message(requests[1])),
async_return_value(b"", 10)
]
await plugin.run()
messages = get_messages(write)
assert {
"jsonrpc": "2.0",
"id": "3",
"result": None
} in messages
assert {
"jsonrpc": "2.0",
"id": "4",
"error": {
"code": 600,
"message": "Import already in progress"
}
} in messages

View File

@@ -3,7 +3,7 @@ from unittest.mock import call
import pytest import pytest
from galaxy.api.types import GameTime from galaxy.api.types import GameTime
from galaxy.api.errors import BackendError from galaxy.api.errors import BackendError
from galaxy.unittest.mock import async_return_value from galaxy.unittest.mock import async_return_value, skip_loop
from tests import create_message, get_messages from tests import create_message, get_messages
@@ -199,6 +199,7 @@ async def test_import_in_progress(plugin, read, write):
async def test_update_game(plugin, write): async def test_update_game(plugin, write):
game_time = GameTime("3", 60, 1549550504) game_time = GameTime("3", 60, 1549550504)
plugin.update_game_time(game_time) plugin.update_game_time(game_time)
await skip_loop()
assert get_messages(write) == [ assert get_messages(write) == [
{ {

View File

@@ -3,7 +3,7 @@ import pytest
from galaxy.api.types import LocalGame from galaxy.api.types import LocalGame
from galaxy.api.consts import LocalGameState from galaxy.api.consts import LocalGameState
from galaxy.api.errors import UnknownError, FailedParsingManifest from galaxy.api.errors import UnknownError, FailedParsingManifest
from galaxy.unittest.mock import async_return_value from galaxy.unittest.mock import async_return_value, skip_loop
from tests import create_message, get_messages from tests import create_message, get_messages
@@ -83,6 +83,7 @@ async def test_failure(plugin, read, write, error, code, message):
async def test_local_game_state_update(plugin, write): async def test_local_game_state_update(plugin, write):
game = LocalGame("1", LocalGameState.Running) game = LocalGame("1", LocalGameState.Running)
plugin.update_local_game_status(game) plugin.update_local_game_status(game)
await skip_loop()
assert get_messages(write) == [ assert get_messages(write) == [
{ {

View File

@@ -0,0 +1,187 @@
from unittest.mock import call
import pytest
from galaxy.api.consts import OSCompatibility
from galaxy.api.errors import BackendError
from galaxy.unittest.mock import async_return_value
from tests import create_message, get_messages
@pytest.mark.asyncio
async def test_get_os_compatibility_success(plugin, read, write):
context = "abc"
plugin.prepare_os_compatibility_context.return_value = async_return_value(context)
request = {
"jsonrpc": "2.0",
"id": "11",
"method": "start_os_compatibility_import",
"params": {"game_ids": ["666", "13", "42"]}
}
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
plugin.get_os_compatibility.side_effect = [
async_return_value(OSCompatibility.Linux),
async_return_value(None),
async_return_value(OSCompatibility.Windows | OSCompatibility.MacOS),
]
await plugin.run()
plugin.get_os_compatibility.assert_has_calls([
call("666", context),
call("13", context),
call("42", context),
])
plugin.os_compatibility_import_complete.assert_called_once_with()
assert get_messages(write) == [
{
"jsonrpc": "2.0",
"id": "11",
"result": None
},
{
"jsonrpc": "2.0",
"method": "os_compatibility_import_success",
"params": {
"game_id": "666",
"os_compatibility": OSCompatibility.Linux.value
}
},
{
"jsonrpc": "2.0",
"method": "os_compatibility_import_success",
"params": {
"game_id": "13",
"os_compatibility": None
}
},
{
"jsonrpc": "2.0",
"method": "os_compatibility_import_success",
"params": {
"game_id": "42",
"os_compatibility": (OSCompatibility.Windows | OSCompatibility.MacOS).value
}
},
{
"jsonrpc": "2.0",
"method": "os_compatibility_import_finished",
"params": None
}
]
@pytest.mark.asyncio
@pytest.mark.parametrize("exception,code,message", [
(BackendError, 4, "Backend error"),
(KeyError, 0, "Unknown error")
])
async def test_get_os_compatibility_error(exception, code, message, plugin, read, write):
game_id = "6"
request_id = "55"
plugin.prepare_os_compatibility_context.return_value = async_return_value(None)
request = {
"jsonrpc": "2.0",
"id": request_id,
"method": "start_os_compatibility_import",
"params": {"game_ids": [game_id]}
}
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
plugin.get_os_compatibility.side_effect = exception
await plugin.run()
plugin.get_os_compatibility.assert_called()
plugin.os_compatibility_import_complete.assert_called_once_with()
assert get_messages(write) == [
{
"jsonrpc": "2.0",
"id": request_id,
"result": None
},
{
"jsonrpc": "2.0",
"method": "os_compatibility_import_failure",
"params": {
"game_id": game_id,
"error": {
"code": code,
"message": message
}
}
},
{
"jsonrpc": "2.0",
"method": "os_compatibility_import_finished",
"params": None
}
]
@pytest.mark.asyncio
async def test_prepare_get_os_compatibility_context_error(plugin, read, write):
request_id = "31415"
plugin.prepare_os_compatibility_context.side_effect = BackendError()
request = {
"jsonrpc": "2.0",
"id": request_id,
"method": "start_os_compatibility_import",
"params": {"game_ids": ["6"]}
}
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
await plugin.run()
assert get_messages(write) == [
{
"jsonrpc": "2.0",
"id": request_id,
"error": {
"code": 4,
"message": "Backend error"
}
}
]
@pytest.mark.asyncio
async def test_import_already_in_progress_error(plugin, read, write):
plugin.prepare_os_compatibility_context.return_value = async_return_value(None)
requests = [
{
"jsonrpc": "2.0",
"id": "3",
"method": "start_os_compatibility_import",
"params": {
"game_ids": ["42"]
}
},
{
"jsonrpc": "2.0",
"id": "4",
"method": "start_os_compatibility_import",
"params": {
"game_ids": ["666"]
}
}
]
read.side_effect = [
async_return_value(create_message(requests[0])),
async_return_value(create_message(requests[1])),
async_return_value(b"", 10)
]
await plugin.run()
responses = get_messages(write)
assert {
"jsonrpc": "2.0",
"id": "3",
"result": None
} in responses
assert {
"jsonrpc": "2.0",
"id": "4",
"error": {
"code": 600,
"message": "Import already in progress"
}
} in responses

View File

@@ -3,7 +3,7 @@ import pytest
from galaxy.api.types import Game, Dlc, LicenseInfo from galaxy.api.types import Game, Dlc, LicenseInfo
from galaxy.api.consts import LicenseType from galaxy.api.consts import LicenseType
from galaxy.api.errors import UnknownError from galaxy.api.errors import UnknownError
from galaxy.unittest.mock import async_return_value from galaxy.unittest.mock import async_return_value, skip_loop
from tests import create_message, get_messages from tests import create_message, get_messages
@@ -100,6 +100,7 @@ async def test_failure(plugin, read, write):
async def test_add_game(plugin, write): async def test_add_game(plugin, write):
game = Game("3", "Doom", None, LicenseInfo(LicenseType.SinglePurchase, None)) game = Game("3", "Doom", None, LicenseInfo(LicenseType.SinglePurchase, None))
plugin.add_game(game) plugin.add_game(game)
await skip_loop()
assert get_messages(write) == [ assert get_messages(write) == [
{ {
"jsonrpc": "2.0", "jsonrpc": "2.0",
@@ -120,6 +121,7 @@ async def test_add_game(plugin, write):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_remove_game(plugin, write): async def test_remove_game(plugin, write):
plugin.remove_game("5") plugin.remove_game("5")
await skip_loop()
assert get_messages(write) == [ assert get_messages(write) == [
{ {
"jsonrpc": "2.0", "jsonrpc": "2.0",
@@ -135,6 +137,7 @@ async def test_remove_game(plugin, write):
async def test_update_game(plugin, write): async def test_update_game(plugin, write):
game = Game("3", "Doom", None, LicenseInfo(LicenseType.SinglePurchase, None)) game = Game("3", "Doom", None, LicenseInfo(LicenseType.SinglePurchase, None))
plugin.update_game(game) plugin.update_game(game)
await skip_loop()
assert get_messages(write) == [ assert get_messages(write) == [
{ {
"jsonrpc": "2.0", "jsonrpc": "2.0",

View File

@@ -1,6 +1,6 @@
import pytest import pytest
from galaxy.unittest.mock import async_return_value from galaxy.unittest.mock import async_return_value, skip_loop
from tests import create_message, get_messages from tests import create_message, get_messages
@@ -57,6 +57,7 @@ async def test_set_cache(plugin, write, cache_data):
plugin.persistent_cache.update(cache_data) plugin.persistent_cache.update(cache_data)
plugin.push_cache() plugin.push_cache()
await skip_loop()
assert_rpc_request(write, "push_cache", cache_data) assert_rpc_request(write, "push_cache", cache_data)
assert cache_data == plugin.persistent_cache assert cache_data == plugin.persistent_cache
@@ -68,6 +69,7 @@ async def test_clear_cache(plugin, write, cache_data):
plugin.persistent_cache.clear() plugin.persistent_cache.clear()
plugin.push_cache() plugin.push_cache()
await skip_loop()
assert_rpc_request(write, "push_cache", {}) assert_rpc_request(write, "push_cache", {})
assert {} == plugin.persistent_cache assert {} == plugin.persistent_cache

231
tests/test_user_presence.py Normal file
View File

@@ -0,0 +1,231 @@
from unittest.mock import call
import pytest
from galaxy.api.consts import PresenceState
from galaxy.api.errors import BackendError
from galaxy.api.types import UserPresence
from galaxy.unittest.mock import async_return_value
from tests import create_message, get_messages
@pytest.mark.asyncio
async def test_get_user_presence_success(plugin, read, write):
context = "abc"
user_ids = ["666", "13", "42", "69"]
plugin.prepare_user_presence_context.return_value = async_return_value(context)
request = {
"jsonrpc": "2.0",
"id": "11",
"method": "start_user_presence_import",
"params": {"user_ids": user_ids}
}
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
plugin.get_user_presence.side_effect = [
async_return_value(UserPresence(
PresenceState.Unknown,
"game-id1",
None,
"unknown state"
)),
async_return_value(UserPresence(
PresenceState.Offline,
None,
None,
"Going to grandma's house"
)),
async_return_value(UserPresence(
PresenceState.Online,
"game-id3",
"game-title3",
"Pew pew"
)),
async_return_value(UserPresence(
PresenceState.Away,
None,
"game-title4",
"AFKKTHXBY"
)),
]
await plugin.run()
plugin.get_user_presence.assert_has_calls([
call(user_id, context) for user_id in user_ids
])
plugin.user_presence_import_complete.assert_called_once_with()
assert get_messages(write) == [
{
"jsonrpc": "2.0",
"id": "11",
"result": None
},
{
"jsonrpc": "2.0",
"method": "user_presence_import_success",
"params": {
"user_id": "666",
"presence": {
"presence_state": PresenceState.Unknown.value,
"game_id": "game-id1",
"presence_status": "unknown state"
}
}
},
{
"jsonrpc": "2.0",
"method": "user_presence_import_success",
"params": {
"user_id": "13",
"presence": {
"presence_state": PresenceState.Offline.value,
"presence_status": "Going to grandma's house"
}
}
},
{
"jsonrpc": "2.0",
"method": "user_presence_import_success",
"params": {
"user_id": "42",
"presence": {
"presence_state": PresenceState.Online.value,
"game_id": "game-id3",
"game_title": "game-title3",
"presence_status": "Pew pew"
}
}
},
{
"jsonrpc": "2.0",
"method": "user_presence_import_success",
"params": {
"user_id": "69",
"presence": {
"presence_state": PresenceState.Away.value,
"game_title": "game-title4",
"presence_status": "AFKKTHXBY"
}
}
},
{
"jsonrpc": "2.0",
"method": "user_presence_import_finished",
"params": None
}
]
@pytest.mark.asyncio
@pytest.mark.parametrize("exception,code,message", [
(BackendError, 4, "Backend error"),
(KeyError, 0, "Unknown error")
])
async def test_get_user_presence_error(exception, code, message, plugin, read, write):
user_id = "69"
request_id = "55"
plugin.prepare_user_presence_context.return_value = async_return_value(None)
request = {
"jsonrpc": "2.0",
"id": request_id,
"method": "start_user_presence_import",
"params": {"user_ids": [user_id]}
}
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
plugin.get_user_presence.side_effect = exception
await plugin.run()
plugin.get_user_presence.assert_called()
plugin.user_presence_import_complete.assert_called_once_with()
assert get_messages(write) == [
{
"jsonrpc": "2.0",
"id": request_id,
"result": None
},
{
"jsonrpc": "2.0",
"method": "user_presence_import_failure",
"params": {
"user_id": user_id,
"error": {
"code": code,
"message": message
}
}
},
{
"jsonrpc": "2.0",
"method": "user_presence_import_finished",
"params": None
}
]
@pytest.mark.asyncio
async def test_prepare_get_user_presence_context_error(plugin, read, write):
request_id = "31415"
plugin.prepare_user_presence_context.side_effect = BackendError()
request = {
"jsonrpc": "2.0",
"id": request_id,
"method": "start_user_presence_import",
"params": {"user_ids": ["6"]}
}
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
await plugin.run()
assert get_messages(write) == [
{
"jsonrpc": "2.0",
"id": request_id,
"error": {
"code": 4,
"message": "Backend error"
}
}
]
@pytest.mark.asyncio
async def test_import_already_in_progress_error(plugin, read, write):
plugin.prepare_user_presence_context.return_value = async_return_value(None)
requests = [
{
"jsonrpc": "2.0",
"id": "3",
"method": "start_user_presence_import",
"params": {
"user_ids": ["42"]
}
},
{
"jsonrpc": "2.0",
"id": "4",
"method": "start_user_presence_import",
"params": {
"user_ids": ["666"]
}
}
]
read.side_effect = [
async_return_value(create_message(requests[0])),
async_return_value(create_message(requests[1])),
async_return_value(b"", 10)
]
await plugin.run()
responses = get_messages(write)
assert {
"jsonrpc": "2.0",
"id": "3",
"result": None
} in responses
assert {
"jsonrpc": "2.0",
"id": "4",
"error": {
"code": 600,
"message": "Import already in progress"
}
} in responses