mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-24 09:55:12 +00:00
chore(release): promote 6.0.0 to main
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
"""``dl.get_cdm`` builds each CDM once per run.
|
||||
|
||||
Regression: the callers ask for a CDM once per title, and the dedupe guard at the
|
||||
call site compares object identity. No CDM class defines ``__eq__``, so the guard
|
||||
was always true and every title paid a fresh build. A remote device costs an RSA
|
||||
key pair and a blocking request to its host.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any, Optional
|
||||
|
||||
import pytest
|
||||
|
||||
import unshackle.core.cdm as cdm_mod
|
||||
from unshackle.commands.dl import dl
|
||||
from unshackle.core.cdm import loader
|
||||
from unshackle.core.config import config
|
||||
|
||||
|
||||
class StubCdm:
|
||||
"""A distinct object per build, so a reused CDM is visible by identity."""
|
||||
|
||||
def __init__(self, cdm_name: str, service_name: str) -> None:
|
||||
self.cdm_name = cdm_name
|
||||
self.service_name = service_name
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def builds(monkeypatch) -> list[tuple[str, str]]:
|
||||
"""Record every ``load_cdm`` call and hand back a fresh stub for each one."""
|
||||
calls: list[tuple[str, str]] = []
|
||||
|
||||
def fake_load_cdm(cdm_name: str, *, service_name: str = "", vaults: Optional[Any] = None) -> StubCdm:
|
||||
calls.append((cdm_name, service_name))
|
||||
return StubCdm(cdm_name, service_name)
|
||||
|
||||
monkeypatch.setitem(cdm_mod.__dict__, "load_cdm", fake_load_cdm)
|
||||
return calls
|
||||
|
||||
|
||||
def make_dl() -> dl:
|
||||
instance = dl.__new__(dl)
|
||||
instance.log = logging.getLogger("test_cdm_cache")
|
||||
instance.cdm_override = None
|
||||
instance.vaults = None
|
||||
return instance
|
||||
|
||||
|
||||
def test_repeated_calls_build_the_cdm_once(builds, monkeypatch) -> None:
|
||||
monkeypatch.setattr(config, "cdm", {"EXAMPLE": "device_a"})
|
||||
runner = make_dl()
|
||||
|
||||
first = runner.get_cdm("EXAMPLE", "default")
|
||||
for _ in range(4):
|
||||
assert runner.get_cdm("EXAMPLE", "default") is first
|
||||
|
||||
assert builds == [("device_a", "EXAMPLE")]
|
||||
|
||||
|
||||
def test_the_title_loop_guard_sees_the_same_object(builds, monkeypatch) -> None:
|
||||
"""The call site keeps its CDM when ``get_cdm`` returns what it already holds."""
|
||||
monkeypatch.setattr(config, "cdm", {"EXAMPLE": "device_a"})
|
||||
runner = make_dl()
|
||||
|
||||
held = runner.get_cdm("EXAMPLE", "default")
|
||||
for _ in range(3):
|
||||
quality_based_cdm = runner.get_cdm("EXAMPLE", "default", drm="widevine", quality=2160)
|
||||
assert not (quality_based_cdm and quality_based_cdm != held)
|
||||
|
||||
assert len(builds) == 1
|
||||
|
||||
|
||||
def test_a_different_service_gets_its_own_cdm(builds, monkeypatch) -> None:
|
||||
"""A CDM is built with its ``service_name``, so two services must not share one."""
|
||||
monkeypatch.setattr(config, "cdm", {"EXAMPLE": "device_a", "OTHER": "device_a"})
|
||||
runner = make_dl()
|
||||
|
||||
example = runner.get_cdm("EXAMPLE", "default")
|
||||
other = runner.get_cdm("OTHER", "default")
|
||||
|
||||
assert example is not other
|
||||
assert builds == [("device_a", "EXAMPLE"), ("device_a", "OTHER")]
|
||||
|
||||
|
||||
def test_a_different_profile_gets_its_own_cdm(builds, monkeypatch) -> None:
|
||||
monkeypatch.setattr(config, "cdm", {"EXAMPLE": {"one": "device_one", "two": "device_two"}})
|
||||
runner = make_dl()
|
||||
|
||||
one = runner.get_cdm("EXAMPLE", "one")
|
||||
two = runner.get_cdm("EXAMPLE", "two")
|
||||
|
||||
assert one is not two
|
||||
assert runner.get_cdm("EXAMPLE", "one") is one
|
||||
assert builds == [("device_one", "EXAMPLE"), ("device_two", "EXAMPLE")]
|
||||
|
||||
|
||||
def test_quality_selection_reuses_the_device_it_resolves_to(builds, monkeypatch) -> None:
|
||||
"""Two spellings of the same device share one CDM; a different device does not."""
|
||||
monkeypatch.setattr(config, "cdm", {"EXAMPLE": {">=1080": "device_hd", "<1080": "device_sd"}})
|
||||
runner = make_dl()
|
||||
|
||||
hd = runner.get_cdm("EXAMPLE", "default", quality=2160)
|
||||
assert runner.get_cdm("EXAMPLE", "default", quality=1080) is hd
|
||||
sd = runner.get_cdm("EXAMPLE", "default", quality=720)
|
||||
|
||||
assert sd is not hd
|
||||
assert builds == [("device_hd", "EXAMPLE"), ("device_sd", "EXAMPLE")]
|
||||
|
||||
|
||||
def test_an_override_is_cached_under_the_name_it_resolves_to(builds, monkeypatch) -> None:
|
||||
monkeypatch.setattr(config, "cdm", {"EXAMPLE": "device_a"})
|
||||
runner = make_dl()
|
||||
runner.cdm_override = "device_b"
|
||||
|
||||
override = runner.get_cdm("EXAMPLE", "default")
|
||||
|
||||
assert runner.get_cdm("EXAMPLE", "default") is override
|
||||
assert builds == [("device_b", "EXAMPLE")]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def leaked_shadow(monkeypatch) -> None:
|
||||
"""Reproduce the residue that a ``setattr`` on the CDM package leaves behind.
|
||||
|
||||
``load_cdm`` reaches the package only through its lazy ``__getattr__``. A ``setattr`` on
|
||||
the package writes the real function into the package dict when it undoes, which shadows
|
||||
``__getattr__`` for the rest of the process. Two remote tests do exactly that.
|
||||
"""
|
||||
monkeypatch.setitem(cdm_mod.__dict__, "load_cdm", loader.load_cdm)
|
||||
|
||||
|
||||
def test_a_shadowed_package_attribute_still_reaches_the_stub(leaked_shadow, builds, monkeypatch) -> None:
|
||||
"""The stub intercepts ``get_cdm`` whatever an earlier test left on the CDM package."""
|
||||
monkeypatch.setattr(config, "cdm", {"EXAMPLE": "device_a"})
|
||||
runner = make_dl()
|
||||
|
||||
cdm = runner.get_cdm("EXAMPLE", "default")
|
||||
|
||||
assert isinstance(cdm, StubCdm)
|
||||
assert builds == [("device_a", "EXAMPLE")]
|
||||
@@ -0,0 +1,145 @@
|
||||
"""Cookie files that use spaces instead of tabs must still load."""
|
||||
|
||||
from http.cookiejar import CookieJar
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from unshackle.commands.dl import dl
|
||||
|
||||
HEADER = "# Netscape HTTP Cookie File\n"
|
||||
|
||||
|
||||
def _names(jar) -> dict:
|
||||
return {c.name: c.value for c in jar}
|
||||
|
||||
|
||||
def test_space_separated_cookie_file(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_text(HEADER + ".example.com TRUE / TRUE 1234567890 sid abc123\n", "utf8")
|
||||
|
||||
assert _names(dl.load_cookie_file(file)) == {"sid": "abc123"}
|
||||
|
||||
|
||||
def test_tab_separated_value_keeps_spaces(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_text(
|
||||
HEADER + "\t".join([".example.com", "TRUE", "/", "TRUE", "1234567890", "sid", "a b c"]) + "\n", "utf8"
|
||||
)
|
||||
|
||||
assert _names(dl.load_cookie_file(file)) == {"sid": "a b c"}
|
||||
|
||||
|
||||
def test_space_separated_empty_value(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_text(HEADER + ".example.com TRUE / TRUE 1234567890 sid \n", "utf8")
|
||||
|
||||
assert _names(dl.load_cookie_file(file)) == {"sid": ""}
|
||||
|
||||
|
||||
def test_space_separated_httponly(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_text(HEADER + "#HttpOnly_.example.com TRUE / TRUE 1234567890 sid abc123\n", "utf8")
|
||||
|
||||
assert _names(dl.load_cookie_file(file)) == {"sid": "abc123"}
|
||||
|
||||
|
||||
def test_mixed_separators_in_one_file(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_text(
|
||||
HEADER
|
||||
+ "\t".join([".a.com", "TRUE", "/", "TRUE", "1234567890", "tabbed", "1"])
|
||||
+ "\n.b.com TRUE / TRUE 1234567890 spaced 2\n",
|
||||
"utf8",
|
||||
)
|
||||
|
||||
assert _names(dl.load_cookie_file(file)) == {"tabbed": "1", "spaced": "2"}
|
||||
|
||||
|
||||
def test_malformed_row_is_reported(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_text(HEADER + ".a.com\tTRUE\t/\tTRUE\t1\tsid\tv\nnot a cookie\n", "utf8")
|
||||
|
||||
with pytest.raises(ValueError, match="line 3"):
|
||||
dl.load_cookie_file(file)
|
||||
|
||||
|
||||
def test_missing_header_is_added(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_text(".example.com\tTRUE\t/\tTRUE\t1234567890\tsid\tabc123\n", "utf8")
|
||||
|
||||
assert _names(dl.load_cookie_file(file)) == {"sid": "abc123"}
|
||||
|
||||
|
||||
def test_utf8_bom_is_ignored(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_bytes(b"\xef\xbb\xbf" + (HEADER + ".example.com\tTRUE\t/\tTRUE\t1234567890\tsid\tabc123\n").encode())
|
||||
|
||||
assert _names(dl.load_cookie_file(file)) == {"sid": "abc123"}
|
||||
|
||||
|
||||
def test_json_export_gives_clear_error(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_text('[{"domain": ".example.com", "name": "sid", "value": "abc123"}]\n', "utf8")
|
||||
|
||||
with pytest.raises(ValueError, match="JSON export"):
|
||||
dl.load_cookie_file(file)
|
||||
|
||||
|
||||
def test_leading_blank_line_is_not_json(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_text("\n" + HEADER + ".example.com TRUE / TRUE 1234567890 sid abc123\n", "utf8")
|
||||
|
||||
assert _names(dl.load_cookie_file(file)) == {"sid": "abc123"}
|
||||
|
||||
|
||||
def test_dot_flag_mismatch_does_not_abort_file(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_text(HEADER + "example.com\tTRUE\t/\tTRUE\t1\tsid\tv\n", "utf8")
|
||||
|
||||
assert _names(dl.load_cookie_file(file)) == {"sid": "v"}
|
||||
|
||||
|
||||
def test_the_users_file_is_never_rewritten(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
original = ".example.com TRUE / TRUE 1234567890 sid abc123\n"
|
||||
file.write_text(original, "utf8")
|
||||
|
||||
dl.load_cookie_file(file)
|
||||
|
||||
assert file.read_text("utf8") == original
|
||||
|
||||
|
||||
def test_save_cookies_onto_an_unrepaired_file(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_text(".example.com TRUE / TRUE 1234567890 sid abc123\n", "utf8")
|
||||
|
||||
dl.save_cookies(file, CookieJar())
|
||||
|
||||
assert _names(dl.load_cookie_file(file)) == {"sid": "abc123"}
|
||||
|
||||
|
||||
def test_tabbed_row_is_never_resplit_on_spaces(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_text(HEADER + ".a.com\tTRUE\t/\tFALSE\thello world\n", "utf8")
|
||||
|
||||
with pytest.raises(ValueError, match="not a Netscape cookie row"):
|
||||
dl.load_cookie_file(file)
|
||||
|
||||
|
||||
def test_file_without_cookies_is_reported(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_text(HEADER + "# nothing else here\n", "utf8")
|
||||
|
||||
with pytest.raises(ValueError, match="holds no cookies"):
|
||||
dl.load_cookie_file(file)
|
||||
|
||||
|
||||
def test_round_trip_keeps_httponly_and_blank_expiry(tmp_path: Path) -> None:
|
||||
file = tmp_path / "cookies.txt"
|
||||
file.write_text(HEADER + "#HttpOnly_.example.com TRUE / TRUE 1234567890 sid abc123\n", "utf8")
|
||||
|
||||
dl.save_cookies(file, CookieJar())
|
||||
|
||||
assert _names(dl.load_cookie_file(file)) == {"sid": "abc123"}
|
||||
assert "#HttpOnly_" in file.read_text("utf8")
|
||||
@@ -0,0 +1,58 @@
|
||||
"""The search command skips the login for a service that sets ANONYMOUS_SEARCH."""
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from unshackle.commands import search as search_command
|
||||
from unshackle.core.search_result import SearchResult
|
||||
|
||||
|
||||
class _SearchService:
|
||||
ANONYMOUS_SEARCH = False
|
||||
|
||||
def __init__(self):
|
||||
self.calls = []
|
||||
self.session = SimpleNamespace(cookies=object())
|
||||
|
||||
def authenticate(self, cookies, credential):
|
||||
self.calls.append("authenticate")
|
||||
|
||||
def search(self):
|
||||
yield SearchResult(id_="abc", title="A Title")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def login_calls(monkeypatch, tmp_path):
|
||||
calls = []
|
||||
|
||||
def record(name, value):
|
||||
def call(*args):
|
||||
calls.append(name)
|
||||
return value
|
||||
|
||||
return call
|
||||
|
||||
dl = search_command.dl
|
||||
monkeypatch.setattr(dl, "get_cookie_jar", record("get_cookie_jar", None))
|
||||
monkeypatch.setattr(dl, "get_credentials", record("get_credentials", None))
|
||||
monkeypatch.setattr(dl, "get_cookie_path", record("get_cookie_path", tmp_path / "cookies.txt"))
|
||||
monkeypatch.setattr(dl, "save_cookies", record("save_cookies", None))
|
||||
return calls
|
||||
|
||||
|
||||
def test_search_authenticates_and_saves_cookies_by_default(login_calls):
|
||||
service = _SearchService()
|
||||
search_command.result(service)
|
||||
|
||||
assert service.calls == ["authenticate"]
|
||||
assert login_calls == ["get_cookie_jar", "get_credentials", "get_cookie_path", "save_cookies"]
|
||||
|
||||
|
||||
def test_anonymous_search_touches_no_login_material_and_no_cookie_file(login_calls):
|
||||
service = _SearchService()
|
||||
service.ANONYMOUS_SEARCH = True
|
||||
search_command.result(service)
|
||||
|
||||
assert service.calls == []
|
||||
assert login_calls == []
|
||||
Reference in New Issue
Block a user