1313 _setup_model_list_interception ,
1414 setup_network_interception_and_scripts ,
1515)
16+ from config import settings
1617
1718
1819@pytest .mark .asyncio
19- async def test_setup_disabled ():
20- """Test early return when network interception disabled"""
21- mock_context = AsyncMock ()
22-
23- with (
24- patch ("config.settings.NETWORK_INTERCEPTION_ENABLED" , False ),
25- patch ("config.settings.ENABLE_SCRIPT_INJECTION" , False ),
26- ):
27- await setup_network_interception_and_scripts (mock_context )
28-
29- mock_context .route .assert_not_called ()
30-
31-
32- @pytest .mark .asyncio
33- async def test_setup_enabled ():
34- """Test setup when both flags enabled"""
35- mock_context = AsyncMock ()
36-
37- with (
38- patch ("config.settings.NETWORK_INTERCEPTION_ENABLED" , True ),
39- patch ("config.settings.ENABLE_SCRIPT_INJECTION" , True ),
40- patch (
41- "browser_utils.initialization.network._setup_model_list_interception"
42- ) as mock_setup ,
43- patch (
44- "browser_utils.initialization.network.add_init_scripts_to_context"
45- ) as mock_scripts ,
46- ):
47- await setup_network_interception_and_scripts (mock_context )
48- mock_setup .assert_called_once_with (mock_context )
49- mock_scripts .assert_called_once_with (mock_context )
50-
51-
52- @pytest .mark .asyncio
53- async def test_setup_granular_control ():
54- """Test granular control of network interception and script injection"""
20+ @pytest .mark .parametrize (
21+ "interception_enabled, scripts_enabled, should_intercept, should_scripts" ,
22+ [
23+ (False , False , False , False ),
24+ (True , True , True , True ),
25+ (True , False , True , False ),
26+ (False , True , False , True ),
27+ ],
28+ )
29+ async def test_setup_network_and_scripts_combinations (
30+ interception_enabled , scripts_enabled , should_intercept , should_scripts
31+ ):
32+ """Test all combinations of network interception and script injection toggles"""
5533 mock_context = AsyncMock ()
5634
57- # Case 1: Network enabled, Scripts disabled
5835 with (
59- patch ( "config. settings. NETWORK_INTERCEPTION_ENABLED" , True ),
60- patch ( "config. settings. ENABLE_SCRIPT_INJECTION" , False ),
36+ patch . object ( settings , " NETWORK_INTERCEPTION_ENABLED" , interception_enabled ),
37+ patch . object ( settings , " ENABLE_SCRIPT_INJECTION" , scripts_enabled ),
6138 patch (
6239 "browser_utils.initialization.network._setup_model_list_interception"
6340 ) as mock_setup ,
@@ -66,26 +43,16 @@ async def test_setup_granular_control():
6643 ) as mock_scripts ,
6744 ):
6845 await setup_network_interception_and_scripts (mock_context )
69- mock_setup .assert_called_once_with (mock_context )
70- mock_scripts .assert_not_called ()
7146
72- mock_setup .reset_mock ()
73- mock_scripts .reset_mock ()
47+ if should_intercept :
48+ mock_setup .assert_called_once_with (mock_context )
49+ else :
50+ mock_setup .assert_not_called ()
7451
75- # Case 2: Network disabled, Scripts enabled
76- with (
77- patch ("config.settings.NETWORK_INTERCEPTION_ENABLED" , False ),
78- patch ("config.settings.ENABLE_SCRIPT_INJECTION" , True ),
79- patch (
80- "browser_utils.initialization.network._setup_model_list_interception"
81- ) as mock_setup ,
82- patch (
83- "browser_utils.initialization.network.add_init_scripts_to_context"
84- ) as mock_scripts ,
85- ):
86- await setup_network_interception_and_scripts (mock_context )
87- mock_setup .assert_not_called ()
88- mock_scripts .assert_called_once_with (mock_context )
52+ if should_scripts :
53+ mock_scripts .assert_called_once_with (mock_context )
54+ else :
55+ mock_scripts .assert_not_called ()
8956
9057
9158@pytest .mark .asyncio
@@ -133,7 +100,8 @@ async def test_setup_exception_handling():
133100 mock_context = AsyncMock ()
134101
135102 with (
136- patch ("config.settings.ENABLE_SCRIPT_INJECTION" , True ),
103+ patch .object (settings , "ENABLE_SCRIPT_INJECTION" , True ),
104+ patch .object (settings , "NETWORK_INTERCEPTION_ENABLED" , True ),
137105 patch (
138106 "browser_utils.initialization.network._setup_model_list_interception" ,
139107 side_effect = RuntimeError ("Route setup failed" ),
0 commit comments