mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-03 06:25:10 +00:00
fix(title-cache): key a cookie-only login by its cookie file path
This commit is contained in:
@@ -15,6 +15,8 @@ from tests.lang_selection import test_kind_only_selection as harness
|
||||
from tests.lang_selection.test_kind_only_selection import FakeService
|
||||
from tests.remote.key_source import MemoryKeySource, remote_keys
|
||||
from unshackle.commands import dl as dl_module
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.credential import Credential
|
||||
from unshackle.core.drm import BlowfishStripe, ClearKeyCTR, PlayReady, Widevine
|
||||
from unshackle.core.drm.cenc import drm_stub
|
||||
from unshackle.core.import_service import ImportService
|
||||
@@ -80,6 +82,69 @@ def test_a_remote_session_and_an_import_have_no_title_cache(tmp_path: Path) -> N
|
||||
assert an_import(tmp_path).cache_identity() == CacheIdentity(None, "movie-1", None, None)
|
||||
|
||||
|
||||
COOKIE_LINE = ".example.com\tTRUE\t/\tFALSE\t0\tsession\t{}\n"
|
||||
|
||||
|
||||
class CookieOnlyService(FakeService):
|
||||
"""A cookie login whose authenticate does not hand the jar to the base class; it records its cache identity."""
|
||||
|
||||
identity: CacheIdentity | None = None
|
||||
|
||||
def get_titles_cached(self, title_id: str | None = None) -> Any:
|
||||
self.identity = self.cache_identity()
|
||||
raise SystemExit(0)
|
||||
|
||||
|
||||
def dl_login(
|
||||
tmp_path: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
profile: str,
|
||||
value: str | None = "one",
|
||||
) -> CacheIdentity:
|
||||
"""The cache identity of a cookie-only service after dl logs it in with the cookie file of ``profile``."""
|
||||
monkeypatch.setattr(config.directories, "cookies", tmp_path)
|
||||
if value is not None:
|
||||
cookie_file = tmp_path / "FAKE" / f"{profile}.txt"
|
||||
cookie_file.parent.mkdir(exist_ok=True)
|
||||
cookie_file.write_text("# Netscape HTTP Cookie File\n" + COOKIE_LINE.format(value))
|
||||
monkeypatch.setattr(harness.Harness, "profile", profile, raising=False)
|
||||
svc = CookieOnlyService({})
|
||||
assert harness.run(monkeypatch, caplog, {}, service=svc) == 0
|
||||
assert svc.identity is not None
|
||||
return svc.identity
|
||||
|
||||
|
||||
def test_two_cookie_profiles_have_their_own_title_cache_account(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
alpha = dl_login(tmp_path, monkeypatch, caplog, "alpha")
|
||||
beta = dl_login(tmp_path, monkeypatch, caplog, "beta")
|
||||
assert alpha.account_hash is not None
|
||||
assert alpha.account_hash != beta.account_hash
|
||||
|
||||
|
||||
def test_rotated_cookies_keep_the_title_cache_account(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
before = dl_login(tmp_path, monkeypatch, caplog, "alpha", "one")
|
||||
after = dl_login(tmp_path, monkeypatch, caplog, "alpha", "two")
|
||||
assert before.account_hash is not None
|
||||
assert before == after
|
||||
|
||||
|
||||
def test_a_login_with_no_cookies_and_no_credential_has_no_account(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
assert dl_login(tmp_path, monkeypatch, caplog, "alpha", None).account_hash is None
|
||||
|
||||
|
||||
def test_a_credential_login_keeps_its_account_hash() -> None:
|
||||
credential = Credential("user", "pass")
|
||||
svc = service(credential=credential, cookie_file="/cookies/EXAMPLE/alpha.txt")
|
||||
assert svc.cache_identity().account_hash == credential.sha1
|
||||
|
||||
|
||||
def test_only_a_service_saves_its_login_cookies(tmp_path: Path) -> None:
|
||||
session = requests.Session()
|
||||
assert service(session=session).login_cookies is session.cookies
|
||||
|
||||
Reference in New Issue
Block a user