mirror of
https://github.com/bohd4nx/FragmentAPI.git
synced 2026-07-25 06:14:29 +00:00
feat: enhance cookie handling by introducing CookieResult and updating get_cookies_from_browser to return structured results
This commit is contained in:
+32
-14
@@ -9,7 +9,7 @@ from pyfragment.types.constants import REQUIRED_COOKIE_KEYS
|
||||
from pyfragment.utils import get_cookies_from_browser
|
||||
|
||||
FAKE_JAR = [
|
||||
{"name": "stel_ssid", "value": "abc123", "domain": "fragment.com"},
|
||||
{"name": "stel_ssid", "value": "abc123", "domain": "fragment.com", "expires": "2027-04-03T20:52:16.375Z"},
|
||||
{"name": "stel_dt", "value": "-120", "domain": "fragment.com"},
|
||||
{"name": "stel_token", "value": "tok_xyz", "domain": "fragment.com"},
|
||||
{"name": "stel_ton_token", "value": "ton_xyz", "domain": "fragment.com"},
|
||||
@@ -23,6 +23,9 @@ def _mock_rookiepy(jar: list[dict] | None = None) -> MagicMock:
|
||||
return mock
|
||||
|
||||
|
||||
PATCH = "pyfragment.utils.cookies.rookiepy"
|
||||
|
||||
|
||||
# unsupported browser tests
|
||||
|
||||
|
||||
@@ -35,20 +38,35 @@ def test_unsupported_browser_raises() -> None:
|
||||
|
||||
|
||||
def test_returns_required_keys_only() -> None:
|
||||
with patch.dict("sys.modules", {"rookiepy": _mock_rookiepy()}):
|
||||
with patch(PATCH, _mock_rookiepy()):
|
||||
result = get_cookies_from_browser("chrome")
|
||||
|
||||
assert set(result.keys()) == set(REQUIRED_COOKIE_KEYS)
|
||||
assert result["stel_ssid"] == "abc123"
|
||||
assert result["stel_dt"] == "-120"
|
||||
assert result["stel_token"] == "tok_xyz"
|
||||
assert result["stel_ton_token"] == "ton_xyz"
|
||||
assert "unrelated" not in result
|
||||
assert set(result.cookies.keys()) == set(REQUIRED_COOKIE_KEYS)
|
||||
assert result.cookies["stel_ssid"] == "abc123"
|
||||
assert result.cookies["stel_dt"] == "-120"
|
||||
assert result.cookies["stel_token"] == "tok_xyz"
|
||||
assert result.cookies["stel_ton_token"] == "ton_xyz"
|
||||
assert "unrelated" not in result.cookies
|
||||
|
||||
|
||||
def test_returns_expiry() -> None:
|
||||
with patch(PATCH, _mock_rookiepy()):
|
||||
result = get_cookies_from_browser("chrome")
|
||||
|
||||
assert result.expires == "2027-04-03T20:52:16.375000+00:00"
|
||||
|
||||
|
||||
def test_returns_none_expiry_when_missing() -> None:
|
||||
jar = [{"name": k, "value": "v"} for k in REQUIRED_COOKIE_KEYS]
|
||||
with patch(PATCH, _mock_rookiepy(jar)):
|
||||
result = get_cookies_from_browser("chrome")
|
||||
|
||||
assert result.expires is None
|
||||
|
||||
|
||||
def test_default_browser_is_chrome() -> None:
|
||||
mock_rp = _mock_rookiepy()
|
||||
with patch.dict("sys.modules", {"rookiepy": mock_rp}):
|
||||
with patch(PATCH, mock_rp):
|
||||
get_cookies_from_browser()
|
||||
|
||||
mock_rp.chrome.assert_called_once_with(["fragment.com"])
|
||||
@@ -56,10 +74,10 @@ def test_default_browser_is_chrome() -> None:
|
||||
|
||||
def test_browser_name_is_case_insensitive() -> None:
|
||||
mock_rp = _mock_rookiepy()
|
||||
with patch.dict("sys.modules", {"rookiepy": mock_rp}):
|
||||
with patch(PATCH, mock_rp):
|
||||
result = get_cookies_from_browser("Chrome")
|
||||
|
||||
assert result["stel_ssid"] == "abc123"
|
||||
assert result.cookies["stel_ssid"] == "abc123"
|
||||
|
||||
|
||||
# missing cookies tests
|
||||
@@ -71,7 +89,7 @@ def test_missing_cookies_raises() -> None:
|
||||
{"name": "stel_dt", "value": "-120"},
|
||||
# stel_token and stel_ton_token missing
|
||||
]
|
||||
with patch.dict("sys.modules", {"rookiepy": _mock_rookiepy(partial_jar)}):
|
||||
with patch(PATCH, _mock_rookiepy(partial_jar)):
|
||||
with pytest.raises(CookieError, match="Fragment cookies not found in chrome"):
|
||||
get_cookies_from_browser("chrome")
|
||||
|
||||
@@ -83,7 +101,7 @@ def test_empty_cookie_value_treated_as_missing() -> None:
|
||||
{"name": "stel_token", "value": "tok_xyz"},
|
||||
{"name": "stel_ton_token", "value": "ton_xyz"},
|
||||
]
|
||||
with patch.dict("sys.modules", {"rookiepy": _mock_rookiepy(jar_with_empty)}):
|
||||
with patch(PATCH, _mock_rookiepy(jar_with_empty)):
|
||||
with pytest.raises(CookieError, match="Fragment cookies not found in chrome"):
|
||||
get_cookies_from_browser("chrome")
|
||||
|
||||
@@ -94,6 +112,6 @@ def test_empty_cookie_value_treated_as_missing() -> None:
|
||||
def test_browser_read_error_raises() -> None:
|
||||
mock_rp = MagicMock()
|
||||
mock_rp.chrome.side_effect = PermissionError("locked")
|
||||
with patch.dict("sys.modules", {"rookiepy": mock_rp}):
|
||||
with patch(PATCH, mock_rp):
|
||||
with pytest.raises(CookieError, match="Failed to read chrome cookies"):
|
||||
get_cookies_from_browser("chrome")
|
||||
|
||||
Reference in New Issue
Block a user