Files
robotex/tests/unit/test_profile.py
T
protokeyandClaude Opus 5.5 232385f8d8 fix: Chrome без менеджера паролей, перевода и автозаполнения (#111)
Перед каждым запуском окна в Default/Preferences профиля дописываются prefs:
credentials_enable_service, profile.password_manager_enabled, translate.enabled,
autofill.* = false. Применяется и к новым, и к существующим профилям, остальные
настройки Chrome не трогаются. Пузырь «Сохранить пароль?» и предложение
перевода больше не появляются, пароли не оседают в Login Data профиля.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 16:54:40 +04:00

108 lines
4.4 KiB
Python

import json
from api import profile
def test_fingerprint_generated_once(tmp_path):
first = profile.load_or_create(tmp_path)
assert profile.load_or_create(tmp_path) == first
assert json.loads((tmp_path / profile.SETTINGS_FILE).read_text()) == first
screen = first["fingerprint"]["screen"]
assert screen["width"] >= profile.MIN_SCREEN[0] and screen["height"] >= profile.MIN_SCREEN[1]
assert first["fingerprint"]["device_scale_factor"] >= 1
assert first["fingerprint"]["canvas_seed"] > 0
assert "user_agent" not in first
def test_old_profile_settings_get_fingerprint(tmp_path):
"""Профиль из версии, где сохранялся user_agent клиента: UA отбрасывается, отпечаток создаётся."""
(tmp_path / profile.SETTINGS_FILE).write_text(json.dumps({"user_agent": "First/1"}))
settings = profile.load_or_create(tmp_path)
assert set(settings) == {"fingerprint"}
def test_profiles_get_independent_fingerprints(tmp_path):
"""Отпечатки генерируются независимо: из выборки профилей получается больше одного варианта."""
variants = set()
for i in range(30):
d = tmp_path / str(i)
d.mkdir()
fp = profile.load_or_create(d)["fingerprint"]
variants.add((fp["screen"]["width"], fp["screen"]["height"], fp["device_scale_factor"]))
assert len(variants) > 1
def test_generate_falls_back_when_browserforge_fails(monkeypatch):
class Failing:
def generate(self, **kwargs):
raise ValueError("Cannot generate headers")
monkeypatch.setattr(profile, "FingerprintGenerator", Failing)
fp = profile.generate_fingerprint(attempts=2)
assert (fp["screen"], fp["device_scale_factor"]) == ({"width": 1920, "height": 1080}, 1)
assert fp["canvas_seed"] > 0
def test_browser_kwargs():
kwargs = profile.browser_kwargs({"fingerprint": {
"screen": {"width": 2560, "height": 1440}, "device_scale_factor": 1.25, "canvas_seed": 12345,
}})
assert kwargs["additional_args"] == {"screen": {"width": 2560, "height": 1440}, "device_scale_factor": 1.25}
assert "useragent" not in kwargs
script = kwargs["init_script_content"]
assert "const SEED = 12345 | 0;" in script and "__SEED__" not in script
def test_profile_before_canvas_noise_gets_seed_and_keeps_screen(tmp_path):
old = {"fingerprint": {"screen": {"width": 2560, "height": 1440}, "device_scale_factor": 2}}
(tmp_path / profile.SETTINGS_FILE).write_text(json.dumps(old))
fp = profile.load_or_create(tmp_path)["fingerprint"]
assert (fp["screen"], fp["device_scale_factor"]) == ({"width": 2560, "height": 1440}, 2)
assert fp["canvas_seed"] > 0
assert profile.load_or_create(tmp_path)["fingerprint"] == fp
def test_profiles_get_different_canvas_seeds(tmp_path):
seeds = set()
for i in range(20):
(tmp_path / str(i)).mkdir()
seeds.add(profile.load_or_create(tmp_path / str(i))["fingerprint"]["canvas_seed"])
assert len(seeds) == 20
def test_chrome_prefs_for_new_profile(tmp_path):
profile.apply_chrome_prefs(tmp_path)
prefs = json.loads((tmp_path / "Default" / "Preferences").read_text())
assert prefs["credentials_enable_service"] is False
assert prefs["profile"]["password_manager_enabled"] is False
assert prefs["translate"]["enabled"] is False
assert prefs["autofill"]["credit_card_enabled"] is False
def test_chrome_prefs_keep_existing_profile_settings(tmp_path):
"""Существующий профиль: prefs дописываются, остальные настройки Chrome остаются."""
path = tmp_path / "Default" / "Preferences"
path.parent.mkdir()
path.write_text(json.dumps({
"profile": {"name": "Person 1", "password_manager_enabled": True},
"translate": {"enabled": True},
"autofill": {"autofill_ai": {"opt_in_status": {}}},
"intl": {"accept_languages": "ru-RU,ru"},
}))
profile.apply_chrome_prefs(tmp_path)
prefs = json.loads(path.read_text())
assert prefs["profile"] == {"name": "Person 1", "password_manager_enabled": False, "password_manager_leak_detection": False}
assert prefs["translate"]["enabled"] is False
assert prefs["autofill"]["autofill_ai"] == {"opt_in_status": {}}
assert prefs["intl"] == {"accept_languages": "ru-RU,ru"}
assert prefs["credentials_enable_service"] is False
def test_chrome_prefs_replace_broken_preferences(tmp_path):
path = tmp_path / "Default" / "Preferences"
path.parent.mkdir()
path.write_text("{broken")
profile.apply_chrome_prefs(tmp_path)
assert json.loads(path.read_text())["translate"] == {"enabled": False}