Add cookies.txt authentication support
All checks were successful
CI / test (3.10) (push) Successful in 1m51s
CI / test (3.11) (push) Successful in 29s
CI / test (3.12) (push) Successful in 24s

This commit is contained in:
2026-05-08 16:43:01 +02:00
parent 7c634e0d39
commit bc82333e07
5 changed files with 138 additions and 7 deletions

View File

@@ -83,6 +83,45 @@ def test_extract_cookies_from_jar_logs_missing_required_cookies(caplog) -> None:
assert "ct0=False" in caplog.text
def test_load_from_cookie_file_parses_netscape_cookie_dump(tmp_path) -> None:
cookie_file = tmp_path / "cookies.txt"
cookie_file.write_text(
"# Netscape HTTP Cookie File\n"
".x.com\tTRUE\t/\tTRUE\t0\tguest_id\tv1%3A123\n"
".x.com\tTRUE\t/\tTRUE\t0\tct0\tcsrf-token\n"
".x.com\tTRUE\t/\tTRUE\t0\tauth_token\tauth-token\n",
encoding="utf-8",
)
cookies = auth.load_from_cookie_file(str(cookie_file))
assert cookies is not None
assert cookies["auth_token"] == "auth-token"
assert cookies["ct0"] == "csrf-token"
assert "guest_id=v1%3A123" in cookies["cookie_string"]
def test_get_cookies_uses_cookie_file_before_browser(monkeypatch) -> None:
monkeypatch.setattr(auth, "load_from_env", lambda: None)
monkeypatch.setattr(
auth,
"load_from_cookie_file",
lambda path: {"auth_token": "file-token", "ct0": "file-csrf", "cookie_string": "a=1"},
)
monkeypatch.setattr(auth, "extract_from_browser", lambda: pytest.fail("should not extract from browser"))
seen = []
monkeypatch.setattr(
auth,
"verify_cookies",
lambda auth_token, ct0, cookie_string=None: seen.append((auth_token, ct0, cookie_string)) or {},
)
cookies = auth.get_cookies({"auth": {"cookieFile": "/tmp/cookies.txt"}})
assert cookies["auth_token"] == "file-token"
assert seen == [("file-token", "file-csrf", "a=1")]
def test_extract_from_browser_logs_warning_when_all_methods_fail(monkeypatch, caplog) -> None:
monkeypatch.setattr(auth, "_extract_in_process", lambda: (None, []))
monkeypatch.setattr(auth, "_extract_via_subprocess", lambda: (None, []))