mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-30 13:05:08 +00:00
refactor(wire): put the tracks reply codec in wire and stamp service flags in serialize_title
This commit is contained in:
@@ -7,7 +7,7 @@ from tests.remote.sessions import 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
|
||||
from unshackle.core.remote_service import apply_session_cookies
|
||||
from unshackle.core.wire import apply_session_cookies
|
||||
|
||||
|
||||
def _session(client_auth):
|
||||
@@ -75,44 +75,92 @@ def test_rnet_session_sends_client_and_set_cookies():
|
||||
}
|
||||
|
||||
|
||||
def test_tracks_response_carries_the_cookie_jar():
|
||||
def call_session_handler(handler, body, service, **entry_fields):
|
||||
"""The JSON body that a session route handler answers for a remote session with these fields."""
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from unshackle.core.api import session_store as store_mod
|
||||
from unshackle.core.api.handlers import session_tracks_handler
|
||||
from unshackle.core.tracks import Tracks
|
||||
|
||||
cookie = SimpleNamespace(name="sid", value="secret", domain=".example.com", path="/", secure=True)
|
||||
|
||||
class StubService:
|
||||
session = SimpleNamespace(headers={}, cookies=[cookie])
|
||||
|
||||
def get_tracks(self, title):
|
||||
return Tracks()
|
||||
|
||||
def get_chapters(self, title):
|
||||
raise NotImplementedError
|
||||
|
||||
async def run():
|
||||
store = store_mod.get_session_store()
|
||||
entry = await store.create(
|
||||
service_tag="EXAMPLE", service_instance=StubService(), session_id="jar", owner_key=None
|
||||
)
|
||||
entry.title_map = {"t1": "Example Title"}
|
||||
entry.client_auth = True
|
||||
entry = await store.create(service_tag="EXAMPLE", service_instance=service, session_id="s", owner_key=None)
|
||||
for name, value in entry_fields.items():
|
||||
setattr(entry, name, value)
|
||||
try:
|
||||
return json.loads((await session_tracks_handler({"title_id": "t1"}, "jar")).body)
|
||||
return json.loads((await handler(body, "s")).body)
|
||||
finally:
|
||||
await store.delete("jar")
|
||||
await store.delete("s")
|
||||
|
||||
body = asyncio.run(run())
|
||||
return asyncio.run(run())
|
||||
|
||||
|
||||
class StubService:
|
||||
def __init__(self, tracks=(), cookies=()):
|
||||
self.session = SimpleNamespace(headers={}, cookies=list(cookies))
|
||||
self.tracks = list(tracks)
|
||||
|
||||
def get_tracks(self, title):
|
||||
from unshackle.core.tracks import Tracks
|
||||
|
||||
return Tracks(self.tracks)
|
||||
|
||||
def get_chapters(self, title):
|
||||
raise NotImplementedError
|
||||
|
||||
def decrypts_samples(self, track):
|
||||
return False
|
||||
|
||||
|
||||
def stub_video():
|
||||
from unshackle.core.tracks import Video
|
||||
|
||||
return Video(url="https://cdn.example.com/v.mp4", language="en", codec=Video.Codec.AVC, id_="v1")
|
||||
|
||||
|
||||
def test_tracks_response_carries_the_cookie_jar():
|
||||
from unshackle.core.api.handlers import session_tracks_handler
|
||||
|
||||
cookie = SimpleNamespace(name="sid", value="secret", domain=".example.com", path="/", secure=True)
|
||||
body = call_session_handler(
|
||||
session_tracks_handler,
|
||||
{"title_id": "t1"},
|
||||
StubService(cookies=[cookie]),
|
||||
title_map={"t1": "Example Title"},
|
||||
client_auth=True,
|
||||
)
|
||||
assert body["session_cookies"] == {"sid": "secret"}
|
||||
assert body["session_cookie_jar"] == [
|
||||
{"name": "sid", "value": "secret", "domain": ".example.com", "path": "/", "secure": True}
|
||||
]
|
||||
|
||||
|
||||
def test_tracks_reply_scrubs_track_data_on_a_server_login():
|
||||
from unshackle.core.api.handlers import session_tracks_handler
|
||||
|
||||
track = stub_video()
|
||||
track.data.update(api_token="secret", keep=1)
|
||||
body = call_session_handler(
|
||||
session_tracks_handler,
|
||||
{"title_id": "t1"},
|
||||
StubService(tracks=[track]),
|
||||
title_map={"t1": "Example Title"},
|
||||
client_auth=False,
|
||||
)
|
||||
assert body["video"][0]["data"] == {"keep": 1}
|
||||
|
||||
|
||||
def test_segments_data_drops_the_parser_keys():
|
||||
from unshackle.core.api.handlers import session_segments_handler
|
||||
|
||||
track = stub_video()
|
||||
track.data.update(dash={"representation": object()}, hls={"playlist": object()}, service_value={"a": 1})
|
||||
body = call_session_handler(
|
||||
session_segments_handler, {"track_ids": ["v1"]}, StubService(), tracks={"v1": track}, client_auth=True
|
||||
)
|
||||
assert body["tracks"]["v1"]["data"] == {"service_value": {"a": 1}}
|
||||
|
||||
|
||||
def test_remote_get_tracks_scopes_server_cookies():
|
||||
import logging
|
||||
|
||||
|
||||
Reference in New Issue
Block a user