Add cookies.txt authentication support
This commit is contained in:
@@ -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, []))
|
||||
|
||||
Reference in New Issue
Block a user