forked from kenzuya/unshackle
test: delete slow, timing-based and redundant tests
This commit is contained in:
@@ -3,7 +3,7 @@ from types import SimpleNamespace
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
from tests.remote.sessions import hold_session, session_entry, stub_service_build
|
||||
from tests.remote.sessions import hold_session, session_entry
|
||||
from unshackle.core.api import server_keys
|
||||
from unshackle.core.api.errors import APIError
|
||||
from unshackle.core.api.handlers import client_session_auth, resolve_handler_proxy, scrub_track_data
|
||||
@@ -190,13 +190,6 @@ def test_remote_get_tracks_scopes_server_cookies():
|
||||
assert service.session.cookies.get("sid", domain=".example.com") == "secret"
|
||||
|
||||
|
||||
def test_server_login_strips_auth():
|
||||
headers, cookies = client_session_auth(_session(False))
|
||||
assert headers == {"User-Agent": "ua"}
|
||||
assert cookies == []
|
||||
assert scrub_track_data({"token": "t", "hls": {}}, _session(False)) == {"hls": {}}
|
||||
|
||||
|
||||
def test_server_account_rejects_client_proxy_uri(monkeypatch):
|
||||
monkeypatch.setattr("unshackle.core.api.handlers.server_account_for", lambda request, service: True)
|
||||
with pytest.raises(APIError):
|
||||
@@ -293,15 +286,6 @@ def test_server_login_strips_nested_and_unlisted_secrets():
|
||||
assert scrub_track_data(data, _session(False)) == {"license": {"url": "u"}, "items": [{"id": 1}]}
|
||||
|
||||
|
||||
def test_track_own_pssh_passes_without_kids(monkeypatch):
|
||||
|
||||
monkeypatch.setattr(server_keys, "track_pssh", lambda track, drm_type: "OWN")
|
||||
monkeypatch.setattr(server_keys, "pssh_kids", lambda pssh, drm_type: set())
|
||||
server_keys.require_track_pssh(SimpleNamespace(id="t", drm=[]), "OWN", "widevine")
|
||||
with pytest.raises(APIError):
|
||||
server_keys.require_track_pssh(SimpleNamespace(id="t", drm=[]), "OTHER", "widevine")
|
||||
|
||||
|
||||
def test_download_rejects_path_profile_and_bad_output_dir(tmp_path, monkeypatch):
|
||||
from tests.admission import refusal
|
||||
from unshackle.core.api.handlers import config
|
||||
@@ -310,143 +294,3 @@ def test_download_rejects_path_profile_and_bad_output_dir(tmp_path, monkeypatch)
|
||||
assert refusal({"profile": "../x"})
|
||||
assert refusal({"profile": "ok"}) is None
|
||||
assert refusal({"output_dir": "a\x00b"})
|
||||
|
||||
|
||||
def test_safe_inflate_rejects_non_str_via_isinstance():
|
||||
# A non-string cache value must be caught before base64.b64decode raises a 500.
|
||||
# Mirror the guard used in session_create_handler.
|
||||
from unshackle.core.api.errors import APIError
|
||||
from unshackle.core.remote_protocol import APIErrorCode
|
||||
|
||||
def guard(content):
|
||||
if not isinstance(content, str):
|
||||
raise APIError(APIErrorCode.INVALID_INPUT, "cache values must be base64 strings")
|
||||
|
||||
guard("ok") # no raise
|
||||
for bad in (1, {"a": 1}, ["x"], None):
|
||||
with pytest.raises(APIError):
|
||||
guard(bad)
|
||||
|
||||
|
||||
def _run_session(monkeypatch, tmp_path, data, answer, server_account=False, fail=False):
|
||||
"""Run session_create_handler with a service that prompts during login; return the settled remote session."""
|
||||
import asyncio
|
||||
import base64
|
||||
import json
|
||||
import uuid
|
||||
import zlib
|
||||
|
||||
from unshackle.core.api import handlers
|
||||
from unshackle.core.api.session_store import get_session_store
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.remote_protocol import AuthStatus
|
||||
|
||||
class PromptingService:
|
||||
_input_bridge = None
|
||||
log = None
|
||||
cache = None
|
||||
|
||||
def authenticate(self, cookies, credential):
|
||||
if answer is not None:
|
||||
self._input_bridge.request_input("Enter code")
|
||||
if fail:
|
||||
raise ValueError("bad code")
|
||||
|
||||
monkeypatch.setattr(config.directories, "cache", tmp_path)
|
||||
monkeypatch.setattr(handlers, "validate_service", lambda service, request=None: service)
|
||||
monkeypatch.setattr(handlers, "resolve_handler_proxy", lambda *args: (None, []))
|
||||
monkeypatch.setattr(handlers, "server_account_for", lambda *args: server_account)
|
||||
monkeypatch.setattr(handlers, "next_server_profile", lambda *args: None)
|
||||
monkeypatch.setattr(handlers.Services, "load", lambda service: PromptingService)
|
||||
stub_service_build(monkeypatch, PromptingService)
|
||||
if data.get("cache") == "tokens":
|
||||
data["cache"] = {"tokens_default": base64.b64encode(zlib.compress(b"{}")).decode("ascii")}
|
||||
|
||||
async def run():
|
||||
body = {"service": "EXAMPLE", "title_id": "t", "session_id": str(uuid.uuid4()), **data}
|
||||
resp = await handlers.session_create_handler(body)
|
||||
session = get_session_store().peek(json.loads(resp.body)["session_id"])
|
||||
for _ in range(200):
|
||||
if answer is not None:
|
||||
for ask in session.input_bridge.claim_for_channel():
|
||||
session.input_bridge.answer(ask.id, answer)
|
||||
if session.auth_status in (AuthStatus.AUTHENTICATED, AuthStatus.FAILED):
|
||||
break
|
||||
await asyncio.sleep(0.01)
|
||||
await get_session_store().delete(session.session_id)
|
||||
return session
|
||||
|
||||
return asyncio.run(run())
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"data, answer, server_account, fail, expected",
|
||||
[
|
||||
({}, None, False, False, True), # no prompt: a pair code shown in the log, approved on another device
|
||||
({}, "CODE", False, False, True), # device code the client approved
|
||||
({}, "CODE", False, True, False), # answered, but the login failed
|
||||
({}, "CODE", True, False, False), # server account OTP answered by the client
|
||||
({"cache": "tokens"}, None, False, False, True), # client uploaded its own earlier login
|
||||
({}, None, True, False, False), # server account
|
||||
],
|
||||
)
|
||||
def test_client_auth_follows_login_provenance(monkeypatch, tmp_path, data, answer, server_account, fail, expected):
|
||||
session = _run_session(monkeypatch, tmp_path, dict(data), answer, server_account, fail)
|
||||
assert session.client_auth is expected
|
||||
|
||||
|
||||
def test_a_failed_login_reports_its_error(monkeypatch, tmp_path):
|
||||
from unshackle.core.remote_protocol import AuthStatus
|
||||
|
||||
session = _run_session(monkeypatch, tmp_path, {}, "CODE", fail=True)
|
||||
assert (session.auth_status, session.auth_error) == (AuthStatus.FAILED, "bad code")
|
||||
|
||||
|
||||
def test_login_that_outlives_its_session_leaves_no_cache(monkeypatch, tmp_path):
|
||||
"""A login loop that finishes after DELETE must not leave its tokens on the server's disk."""
|
||||
import asyncio
|
||||
import json
|
||||
import threading
|
||||
import uuid
|
||||
|
||||
from unshackle.core.api import handlers
|
||||
from unshackle.core.api.session_store import get_session_store
|
||||
from unshackle.core.config import config
|
||||
|
||||
paired = threading.Event()
|
||||
finished = threading.Event()
|
||||
|
||||
class SlowPairingService:
|
||||
_input_bridge = None
|
||||
log = None
|
||||
cache = None
|
||||
|
||||
def authenticate(self, cookies, credential):
|
||||
paired.wait(5)
|
||||
self.cache.get("tokens").set("token")
|
||||
finished.set()
|
||||
|
||||
monkeypatch.setattr(config.directories, "cache", tmp_path)
|
||||
monkeypatch.setattr(handlers, "validate_service", lambda service, request=None: service)
|
||||
monkeypatch.setattr(handlers, "resolve_handler_proxy", lambda *args: (None, []))
|
||||
monkeypatch.setattr(handlers, "server_account_for", lambda *args: False)
|
||||
monkeypatch.setattr(handlers.Services, "load", lambda service: SlowPairingService)
|
||||
stub_service_build(monkeypatch, SlowPairingService)
|
||||
|
||||
async def run():
|
||||
resp = await handlers.session_create_handler(
|
||||
{"service": "EXAMPLE", "title_id": "t", "session_id": str(uuid.uuid4())}
|
||||
)
|
||||
session = get_session_store().peek(json.loads(resp.body)["session_id"])
|
||||
await get_session_store().delete(session.session_id)
|
||||
paired.set()
|
||||
for _ in range(200):
|
||||
if finished.is_set() and session.auth_status.value == "authenticated":
|
||||
break
|
||||
await asyncio.sleep(0.01)
|
||||
await asyncio.sleep(0.05)
|
||||
return session
|
||||
|
||||
session = asyncio.run(run())
|
||||
assert session.auth_status.value == "authenticated"
|
||||
assert not (tmp_path / session.cache_tag).exists()
|
||||
|
||||
Reference in New Issue
Block a user