| 1 | """Tests for browser cookie extraction integration in env.py.""" |
| 2 | |
| 3 | import os |
| 4 | from unittest.mock import patch |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | from lib.env import ConfigLoadPolicy, extract_browser_credentials, COOKIE_DOMAINS |
| 9 | |
| 10 | |
| 11 | def test_research_reports_permission_denied_without_cookie_values(capsys): |
| 12 | with patch( |
| 13 | "lib.cookie_extract.extract_cookies", |
| 14 | side_effect=PermissionError(1, "Operation not permitted", "/private/secret/Cookies"), |
| 15 | ): |
| 16 | extracted = extract_browser_credentials( |
| 17 | _base_config(FROM_BROWSER="edge", BROWSER_CONSENT="true") |
| 18 | ) |
| 19 | assert extracted == {} |
| 20 | notice = capsys.readouterr().err |
| 21 | assert "permission denied" in notice.lower() |
| 22 | assert "edge" in notice |
| 23 | assert "AUTH_TOKEN" in notice and "CT0" in notice |
| 24 | assert "/private/secret" not in notice |
| 25 | |
| 26 | |
| 27 | def _base_config(**overrides): |
| 28 | """Return a minimal config dict with common defaults.""" |
| 29 | cfg = { |
| 30 | "AUTH_TOKEN": None, |
| 31 | "CT0": None, |
| 32 | "TRUTHSOCIAL_TOKEN": None, |
| 33 | "FROM_BROWSER": None, |
| 34 | "SETUP_COMPLETE": None, |
| 35 | } |
| 36 | cfg.update(overrides) |
| 37 | return cfg |
| 38 | |
| 39 | |
| 40 | class TestExtractBrowserCredentials: |
| 41 | """Unit tests for extract_browser_credentials().""" |
| 42 | |
| 43 | @patch("lib.cookie_extract.extract_cookies") |
| 44 | def test_auto_populates_credentials(self, mock_extract): |
| 45 | mock_extract.return_value = {"auth_token": "tok123", "ct0": "ct0val"} |
| 46 | config = _base_config(FROM_BROWSER="auto") |
| 47 | result = extract_browser_credentials(config) |
| 48 | assert result["AUTH_TOKEN"] == "tok123" |
| 49 | assert result["CT0"] == "ct0val" |
| 50 | # auto mode tries firefox first, then safari, then chrome |
| 51 | mock_extract.assert_any_call("firefox", ".x.com", ["auth_token", "ct0"]) |
| 52 | |
| 53 | @patch("lib.cookie_extract.extract_cookies") |
| 54 | def test_explicit_auth_token_skips_x_extraction(self, mock_extract): |
| 55 | mock_extract.return_value = None |
| 56 | config = _base_config( |
| 57 | AUTH_TOKEN="explicit_token", CT0="explicit_ct0", |
| 58 | FROM_BROWSER="auto", |
| 59 | ) |
| 60 | result = extract_browser_credentials(config) |
| 61 | assert "AUTH_TOKEN" not in result |
| 62 | assert "CT0" not in result |
| 63 | for call in mock_extract.call_args_list: |
| 64 | assert call[0][1] != ".x.com" |
| 65 | |
| 66 | @patch("lib.cookie_extract.extract_cookies") |
| 67 | def test_from_browser_off_skips_all(self, mock_extract): |
| 68 | config = _base_config(FROM_BROWSER="off") |
| 69 | result = extract_browser_credentials(config) |
| 70 | assert result == {} |
| 71 | mock_extract.assert_not_called() |
| 72 | |
| 73 | @patch("lib.cookie_extract.extract_cookies") |
| 74 | def test_no_from_browser_skips_all(self, mock_extract): |
| 75 | """Default (no FROM_BROWSER): reads no browser cookies.""" |
| 76 | mock_extract.return_value = None |
| 77 | config = _base_config() |
| 78 | result = extract_browser_credentials(config) |
| 79 | assert result == {} |
| 80 | mock_extract.assert_not_called() |
| 81 | |
| 82 | @patch("lib.cookie_extract.extract_cookies") |
| 83 | def test_from_browser_firefox_only(self, mock_extract): |
| 84 | mock_extract.return_value = {"auth_token": "ff_tok", "ct0": "ff_ct0"} |
| 85 | config = _base_config(FROM_BROWSER="firefox") |
| 86 | result = extract_browser_credentials(config) |
| 87 | assert result["AUTH_TOKEN"] == "ff_tok" |
| 88 | for call in mock_extract.call_args_list: |
| 89 | assert call[0][0] == "firefox" |
| 90 | |
| 91 | @patch("lib.cookie_extract.extract_cookies") |
| 92 | def test_extraction_returns_none_config_unchanged(self, mock_extract): |
| 93 | mock_extract.return_value = None |
| 94 | config = _base_config(FROM_BROWSER="auto") |
| 95 | result = extract_browser_credentials(config) |
| 96 | assert "AUTH_TOKEN" not in result |
| 97 | assert "CT0" not in result |
| 98 | |
| 99 | @patch("lib.cookie_extract.extract_cookies") |
| 100 | def test_extraction_raises_exception_caught(self, mock_extract): |
| 101 | mock_extract.side_effect = RuntimeError("database locked") |
| 102 | config = _base_config(FROM_BROWSER="auto") |
| 103 | result = extract_browser_credentials(config) |
| 104 | assert "AUTH_TOKEN" not in result |
| 105 | assert "CT0" not in result |
| 106 | |
| 107 | @patch("lib.cookie_extract.extract_cookies") |
| 108 | def test_partial_credentials_only_fills_missing(self, mock_extract): |
| 109 | mock_extract.return_value = {"auth_token": "cookie_tok", "ct0": "cookie_ct0"} |
| 110 | config = _base_config( |
| 111 | AUTH_TOKEN="explicit", CT0=None, |
| 112 | FROM_BROWSER="auto", |
| 113 | ) |
| 114 | result = extract_browser_credentials(config) |
| 115 | assert "AUTH_TOKEN" not in result |
| 116 | assert result["CT0"] == "cookie_ct0" |
| 117 | |
| 118 | @patch("lib.cookie_extract.extract_cookies") |
| 119 | def test_partial_pair_does_not_shadow_complete_browser(self, mock_extract): |
| 120 | """A partial X pair (lone ct0 from a logged-out session) must not |
| 121 | stop the scan: the complete pair from a later browser wins.""" |
| 122 | def side_effect(browser, domain, cookie_names): |
| 123 | if domain == ".x.com": |
| 124 | if browser == "firefox": |
| 125 | return {"ct0": "logged_out_ct0"} |
| 126 | if browser == "safari": |
| 127 | return {"auth_token": "tok", "ct0": "ct0"} |
| 128 | if domain == ".truthsocial.com": |
| 129 | return {"_session_id": "sess"} |
| 130 | return None |
| 131 | mock_extract.side_effect = side_effect |
| 132 | config = _base_config(FROM_BROWSER="firefox,safari") |
| 133 | result = extract_browser_credentials(config) |
| 134 | assert result["AUTH_TOKEN"] == "tok" |
| 135 | assert result["CT0"] == "ct0" |
| 136 | |
| 137 | @patch("lib.cookie_extract.extract_cookies") |
| 138 | def test_complementary_partials_never_merge_across_browsers(self, mock_extract): |
| 139 | """auth_token from one browser and ct0 from another can belong to |
| 140 | different X sessions: only the first browser's partial is kept.""" |
| 141 | def side_effect(browser, domain, cookie_names): |
| 142 | if domain == ".x.com": |
| 143 | if browser == "firefox": |
| 144 | return {"auth_token": "tok"} |
| 145 | if browser == "safari": |
| 146 | return {"ct0": "other_session_ct0"} |
| 147 | return None |
| 148 | mock_extract.side_effect = side_effect |
| 149 | config = _base_config(FROM_BROWSER="firefox,safari") |
| 150 | result = extract_browser_credentials(config) |
| 151 | assert result["AUTH_TOKEN"] == "tok" |
| 152 | assert "CT0" not in result |
| 153 | |
| 154 | @patch("lib.cookie_extract.extract_cookies") |
| 155 | def test_partial_then_complete_uses_only_complete_browser(self, mock_extract): |
| 156 | """A complete pair from a later browser replaces an earlier partial |
| 157 | wholesale; no value from the partial browser survives.""" |
| 158 | def side_effect(browser, domain, cookie_names): |
| 159 | if domain == ".x.com": |
| 160 | if browser == "firefox": |
| 161 | return {"auth_token": "stale_tok"} |
| 162 | if browser == "safari": |
| 163 | return {"auth_token": "tok", "ct0": "ct0"} |
| 164 | return None |
| 165 | mock_extract.side_effect = side_effect |
| 166 | config = _base_config(FROM_BROWSER="firefox,safari") |
| 167 | result = extract_browser_credentials(config) |
| 168 | assert result == {"AUTH_TOKEN": "tok", "CT0": "ct0"} |
| 169 | |
| 170 | |
| 171 | class TestGetConfigCookieIntegration: |
| 172 | """Integration tests for policy-gated cookie extraction in get_config().""" |
| 173 | |
| 174 | @patch("lib.cookie_extract.extract_cookies") |
| 175 | @patch("lib.env._find_project_env", return_value=None) |
| 176 | @patch("lib.env.load_env_file", return_value={}) |
| 177 | @patch("lib.env._load_keychain", return_value={}) |
| 178 | @patch("lib.env.get_openai_auth") |
| 179 | def test_get_config_default_does_not_extract_cookies( |
| 180 | self, mock_openai, mock_keychain, mock_load, mock_proj, mock_extract |
| 181 | ): |
| 182 | from lib.env import get_config, OpenAIAuth |
| 183 | mock_openai.return_value = OpenAIAuth( |
| 184 | token=None, source="none", status="missing", |
| 185 | ) |
| 186 | mock_extract.return_value = {"auth_token": "browser_tok", "ct0": "browser_ct0"} |
| 187 | env_patch = { |
| 188 | "SETUP_COMPLETE": "true", |
| 189 | "FROM_BROWSER": "auto", |
| 190 | "LAST30DAYS_CONFIG_DIR": "", |
| 191 | } |
| 192 | with patch.dict(os.environ, env_patch, clear=False): |
| 193 | config = get_config() |
| 194 | assert config["AUTH_TOKEN"] is None |
| 195 | assert config["CT0"] is None |
| 196 | mock_extract.assert_not_called() |
| 197 | |
| 198 | @patch("lib.cookie_extract.extract_cookies") |
| 199 | @patch("lib.env._find_project_env", return_value=None) |
| 200 | @patch("lib.env.load_env_file", return_value={}) |
| 201 | @patch("lib.env._load_keychain", return_value={}) |
| 202 | @patch("lib.env.get_openai_auth") |
| 203 | def test_get_config_with_cookie_policy_injects_cookies( |
| 204 | self, mock_openai, mock_keychain, mock_load, mock_proj, mock_extract |
| 205 | ): |
| 206 | from lib.env import get_config, OpenAIAuth |
| 207 | mock_openai.return_value = OpenAIAuth( |
| 208 | token=None, source="none", status="missing", |
| 209 | ) |
| 210 | mock_extract.return_value = {"auth_token": "browser_tok", "ct0": "browser_ct0"} |
| 211 | env_patch = { |
| 212 | "SETUP_COMPLETE": "true", |
| 213 | "FROM_BROWSER": "auto", |
| 214 | "LAST30DAYS_CONFIG_DIR": "", |
| 215 | } |
| 216 | with patch.dict(os.environ, env_patch, clear=False), \ |
| 217 | patch("lib.chrome_cdp.read_x_cookies", return_value=None): |
| 218 | config = get_config(policy=ConfigLoadPolicy(browser_cookies="read")) |
| 219 | assert config["AUTH_TOKEN"] == "browser_tok" |
| 220 | assert config["CT0"] == "browser_ct0" |
| 221 |