40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
"""Tests for optional stealth browser launcher."""
|
|
|
|
import os
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from src.airbnb.browser import open_browser, use_stealth_browser
|
|
|
|
|
|
class TestUseStealthBrowser:
|
|
def test_default_off(self, monkeypatch):
|
|
monkeypatch.delenv("AIRBNB_STEALTH", raising=False)
|
|
assert use_stealth_browser() is False
|
|
|
|
def test_enabled(self, monkeypatch):
|
|
monkeypatch.setenv("AIRBNB_STEALTH", "1")
|
|
assert use_stealth_browser() is True
|
|
|
|
|
|
class TestOpenBrowser:
|
|
def test_chromium_path(self, monkeypatch):
|
|
monkeypatch.delenv("AIRBNB_STEALTH", raising=False)
|
|
mock_browser = MagicMock()
|
|
mock_pw = MagicMock()
|
|
mock_pw.chromium.launch.return_value = mock_browser
|
|
|
|
with patch("playwright.sync_api.sync_playwright") as mock_sync:
|
|
mock_sync.return_value.__enter__.return_value = mock_pw
|
|
with open_browser(headless=True) as browser:
|
|
assert browser is mock_browser
|
|
mock_browser.close.assert_called_once()
|
|
|
|
def test_stealth_requires_package(self, monkeypatch):
|
|
monkeypatch.setenv("AIRBNB_STEALTH", "1")
|
|
with patch.dict("sys.modules", {"invisible_playwright": None}):
|
|
with pytest.raises(ImportError, match="invisible_playwright"):
|
|
with open_browser(headless=True):
|
|
pass
|