Files
unshackle/tests/remote/sessions.py
T

79 lines
3.1 KiB
Python

"""A real remote session entry for the tests that call the session and licence handlers directly."""
from collections.abc import Callable
from types import SimpleNamespace
from typing import Any, NoReturn
import pytest
import requests
from unshackle.core.api import handlers, key_policy
from unshackle.core.api.handlers import Launched, SessionLaunch
from unshackle.core.api.input_bridge import InputBridge
from unshackle.core.api.key_policy import KeyPolicy
from unshackle.core.api.session_store import SessionEntry
from unshackle.core.remote_lifetime import RemoteSessionLifetime
from unshackle.core.remote_protocol import LicensingMode, Licensor
from unshackle.core.remote_service import RemoteClient
def session_entry(**fields: Any) -> SessionEntry:
"""A remote session that the server CDM licenses, unless *fields* set another :class:`SessionEntry` value."""
return SessionEntry(
**{
"session_id": "sess",
"service_tag": "EXAMPLE",
"service_instance": SimpleNamespace(),
"licensing": LicensingMode(Licensor.SERVER_CDM),
**fields,
}
)
def session_launch(licensor: Licensor = Licensor.NO_DEVICE, **fields: Any) -> SessionLaunch:
"""The session purpose of ``launch_served``: no device and no server account, unless *fields* set another value."""
return SessionLaunch(
**{
"proxy": (None, []),
"policy": KeyPolicy.of(None),
"server_account": False,
"licensing": LicensingMode(licensor),
"input_bridge": InputBridge(),
"cache_dir": "_sessions/test/EXAMPLE",
**fields,
}
)
def launch_session(
service: str,
title: str,
data: dict,
profile: str | None = None,
licensor: Licensor = Licensor.NO_DEVICE,
**fields: Any,
) -> Launched:
"""``launch_served`` for a remote session of *service* with the :func:`session_launch` purpose."""
return handlers.launch_served(session_launch(licensor, **fields), data, service, title, profile)
def stub_service_build(monkeypatch: pytest.MonkeyPatch, build: Callable[[], Any]) -> None:
"""Make every served launch build its service with *build*, with no service config or CDM behind it."""
monkeypatch.setattr(key_policy, "load_service_config", lambda tag: {})
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None)
monkeypatch.setattr(handlers, "instantiate_service", lambda *a, **k: build())
def hold_session(service: Any, session_id: str | None = "sess") -> None:
"""Give a remote client built without its constructor a session lifetime, with *session_id* open in it."""
def unreachable(*args: Any, **kwargs: Any) -> NoReturn:
raise requests.ConnectionError("EXAMPLE server is down")
client = getattr(service, "client", None)
if not isinstance(client, RemoteClient):
client = SimpleNamespace(send=unreachable, timeout=1, tunnel=None)
service.lifetime = RemoteSessionLifetime(client, lambda cache: None) # type: ignore[arg-type]
if session_id:
service.lifetime.adopt(service.lifetime.name(), session_id)