Skip to content

Commit 77b2266

Browse files
committed
fix(tests): Update test mocks to patch auth.get_current_access_token
Commit acc403b changed the import pattern from: from .auth import get_current_access_token to: from . import auth # then calling auth.get_current_access_token() This was necessary to fix HTTP authentication in streamable-http transport, where the http_auth_integration module patches auth.get_current_access_token at runtime to inject tokens from HTTP headers. The old import pattern created local references that bypassed the runtime patching, breaking HTTP authentication. The new pattern ensures all calls go through the auth module, allowing the HTTP auth patching to work. However, tests were still patching 'api.get_current_access_token' and 'duplication.get_current_access_token', which no longer exist with the new pattern. This caused 86 test failures. This commit updates all test mocks to patch at the source: @patch('meta_ads_mcp.core.auth.get_current_access_token') This is the correct approach as it: 1. Preserves HTTP authentication functionality 2. Mocks at the source where the function is actually defined 3. Aligns with how the code actually calls the function Result: All 257 tests now pass (was 86 failures, 147 passes) Files updated: - 12 test files with updated mock paths - Removed temporary compatibility imports from api.py and duplication.py
1 parent 0553d56 commit 77b2266

12 files changed

Lines changed: 62 additions & 62 deletions

‎tests/test_account_info_access_fix.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async def test_account_info_direct_access_success(self):
3838
}
3939

4040
with patch('meta_ads_mcp.core.accounts.make_api_request', new_callable=AsyncMock) as mock_api:
41-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
41+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
4242
mock_auth.return_value = "test_access_token"
4343
mock_api.return_value = mock_account_response
4444

@@ -95,7 +95,7 @@ async def test_account_info_permission_error_with_helpful_message(self):
9595
}
9696

9797
with patch('meta_ads_mcp.core.accounts.make_api_request', new_callable=AsyncMock) as mock_api:
98-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
98+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
9999
mock_auth.return_value = "test_access_token"
100100

101101
# First call returns permission error, second call returns accessible accounts
@@ -158,7 +158,7 @@ async def test_account_info_non_permission_error_passthrough(self):
158158
}
159159

160160
with patch('meta_ads_mcp.core.accounts.make_api_request', new_callable=AsyncMock) as mock_api:
161-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
161+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
162162
mock_auth.return_value = "test_access_token"
163163
mock_api.return_value = mock_error_response
164164

@@ -180,7 +180,7 @@ async def test_account_info_non_permission_error_passthrough(self):
180180
async def test_account_info_missing_account_id_error(self):
181181
"""Test that missing account_id parameter returns appropriate error"""
182182

183-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
183+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
184184
mock_auth.return_value = "test_access_token"
185185

186186
result = await get_account_info(account_id=None)
@@ -207,7 +207,7 @@ async def test_account_info_act_prefix_handling(self):
207207
}
208208

209209
with patch('meta_ads_mcp.core.accounts.make_api_request', new_callable=AsyncMock) as mock_api:
210-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
210+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
211211
mock_auth.return_value = "test_access_token"
212212
mock_api.return_value = mock_account_response
213213

@@ -236,7 +236,7 @@ async def test_account_info_european_dsa_detection(self):
236236
}
237237

238238
with patch('meta_ads_mcp.core.accounts.make_api_request', new_callable=AsyncMock) as mock_api:
239-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
239+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
240240
mock_auth.return_value = "test_access_token"
241241
mock_api.return_value = mock_account_response
242242

@@ -272,7 +272,7 @@ async def test_regression_basic_account_info_still_works(self):
272272
}
273273

274274
with patch('meta_ads_mcp.core.accounts.make_api_request', new_callable=AsyncMock) as mock_api:
275-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
275+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
276276
mock_auth.return_value = "test_access_token"
277277
mock_api.return_value = mock_account_response
278278

‎tests/test_budget_update.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def mock_api_request(self):
4040
def mock_auth_manager(self):
4141
"""Mock for the authentication manager"""
4242
with patch('meta_ads_mcp.core.api.auth_manager') as mock, \
43-
patch('meta_ads_mcp.core.api.get_current_access_token') as mock_get_token:
43+
patch('meta_ads_mcp.core.auth.get_current_access_token') as mock_get_token:
4444
# Mock a valid access token
4545
mock.get_current_access_token.return_value = "test_access_token"
4646
mock.is_token_valid.return_value = True
@@ -433,7 +433,7 @@ async def test_budget_update_workflow(self):
433433
# Test that the function accepts the new parameters
434434
with patch('meta_ads_mcp.core.adsets.make_api_request') as mock_api, \
435435
patch('meta_ads_mcp.core.api.auth_manager') as mock_auth, \
436-
patch('meta_ads_mcp.core.api.get_current_access_token') as mock_get_token:
436+
patch('meta_ads_mcp.core.auth.get_current_access_token') as mock_get_token:
437437

438438
mock_api.return_value = {"id": "test_id", "daily_budget": "5000"}
439439
mock_auth.get_current_access_token.return_value = "test_access_token"

‎tests/test_dsa_beneficiary.py‎

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async def test_dsa_requirement_detection_business_account(self):
3030
}
3131

3232
with patch('meta_ads_mcp.core.accounts.make_api_request', new_callable=AsyncMock) as mock_api:
33-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
33+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
3434
mock_auth.return_value = "test_access_token"
3535
mock_api.return_value = mock_account_response
3636

@@ -62,7 +62,7 @@ async def test_dsa_requirement_detection_non_dsa_region(self):
6262
}
6363

6464
with patch('meta_ads_mcp.core.accounts.make_api_request', new_callable=AsyncMock) as mock_api:
65-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
65+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
6666
mock_auth.return_value = "test_access_token"
6767
mock_api.return_value = mock_account_response
6868

@@ -81,7 +81,7 @@ async def test_dsa_requirement_detection_non_dsa_region(self):
8181
async def test_dsa_requirement_detection_error_handling(self):
8282
"""Test error handling when account info cannot be retrieved"""
8383
with patch('meta_ads_mcp.core.accounts.make_api_request', new_callable=AsyncMock) as mock_api:
84-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
84+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
8585
mock_auth.return_value = "test_access_token"
8686
mock_api.side_effect = Exception("API Error")
8787

@@ -100,7 +100,7 @@ async def test_dsa_requirement_detection_error_handling(self):
100100
async def test_account_info_requires_account_id(self):
101101
"""Test that get_account_info requires an account_id parameter"""
102102

103-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
103+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
104104
mock_auth.return_value = "test_access_token"
105105

106106
# Test without account_id parameter
@@ -140,7 +140,7 @@ async def test_account_info_inaccessible_account_error(self):
140140
}
141141

142142
with patch('meta_ads_mcp.core.accounts.make_api_request', new_callable=AsyncMock) as mock_api:
143-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
143+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
144144
mock_auth.return_value = "test_access_token"
145145
# First call returns permission error, second call returns accessible accounts
146146
mock_api.side_effect = [mock_permission_error, mock_accessible_accounts]
@@ -175,7 +175,7 @@ async def test_create_adset_with_dsa_beneficiary_success(self):
175175
}
176176

177177
with patch('meta_ads_mcp.core.adsets.make_api_request', new_callable=AsyncMock) as mock_api:
178-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
178+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
179179
mock_auth.return_value = "test_access_token"
180180
mock_api.return_value = mock_response
181181

@@ -209,7 +209,7 @@ async def test_create_adset_with_dsa_beneficiary_validation_error(self):
209209
}
210210

211211
with patch('meta_ads_mcp.core.adsets.make_api_request', new_callable=AsyncMock) as mock_api:
212-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
212+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
213213
mock_auth.return_value = "test_access_token"
214214
mock_api.side_effect = Exception("DSA beneficiary required for European compliance")
215215

@@ -237,7 +237,7 @@ async def test_create_adset_with_dsa_beneficiary_validation_error(self):
237237
async def test_create_adset_without_dsa_beneficiary_dsa_required(self):
238238
"""Test error when DSA beneficiary is required but not provided"""
239239
with patch('meta_ads_mcp.core.adsets.make_api_request', new_callable=AsyncMock) as mock_api:
240-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
240+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
241241
mock_auth.return_value = "test_access_token"
242242
mock_api.side_effect = Exception("Enter the person or organization that benefits from ads in this ad set")
243243

@@ -265,7 +265,7 @@ async def test_create_adset_without_dsa_beneficiary_dsa_required(self):
265265
async def test_create_adset_dsa_beneficiary_in_targeting(self):
266266
"""Test that DSA beneficiary is not added to targeting spec"""
267267
with patch('meta_ads_mcp.core.adsets.make_api_request', new_callable=AsyncMock) as mock_api:
268-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
268+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
269269
mock_auth.return_value = "test_access_token"
270270
mock_api.return_value = {"id": "23842588888640185"}
271271

@@ -300,7 +300,7 @@ async def test_create_adset_dsa_beneficiary_parameter_formats(self):
300300

301301
for beneficiary_name in test_cases:
302302
with patch('meta_ads_mcp.core.adsets.make_api_request', new_callable=AsyncMock) as mock_api:
303-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
303+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
304304
mock_auth.return_value = "test_access_token"
305305
mock_api.return_value = {"id": "23842588888640185"}
306306

@@ -326,7 +326,7 @@ class TestDSAPermissionHandling:
326326
async def test_dsa_beneficiary_missing_business_management_permission(self):
327327
"""Test error handling when business_management permissions are missing"""
328328
with patch('meta_ads_mcp.core.adsets.make_api_request', new_callable=AsyncMock) as mock_api:
329-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
329+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
330330
mock_auth.return_value = "test_access_token"
331331
mock_api.side_effect = Exception("Permission denied: business_management permission required")
332332

@@ -354,7 +354,7 @@ async def test_dsa_beneficiary_missing_business_management_permission(self):
354354
async def test_dsa_beneficiary_api_limitation_handling(self):
355355
"""Test handling when API doesn't support dsa_beneficiary parameter"""
356356
with patch('meta_ads_mcp.core.adsets.make_api_request', new_callable=AsyncMock) as mock_api:
357-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
357+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
358358
mock_auth.return_value = "test_access_token"
359359
mock_api.side_effect = Exception("Parameter dsa_beneficiary is not supported")
360360

@@ -397,7 +397,7 @@ async def test_dsa_compliance_european_regions(self):
397397
}
398398

399399
with patch('meta_ads_mcp.core.accounts.make_api_request', new_callable=AsyncMock) as mock_api:
400-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
400+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
401401
mock_auth.return_value = "test_access_token"
402402
mock_api.return_value = mock_account_response
403403

@@ -422,7 +422,7 @@ async def test_dsa_compliance_non_european_regions(self):
422422
}
423423

424424
with patch('meta_ads_mcp.core.accounts.make_api_request', new_callable=AsyncMock) as mock_api:
425-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
425+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
426426
mock_auth.return_value = "test_access_token"
427427
mock_api.return_value = mock_account_response
428428

@@ -451,7 +451,7 @@ async def test_dsa_beneficiary_validation_by_region(self):
451451

452452
# Test European account
453453
with patch('meta_ads_mcp.core.accounts.make_api_request', new_callable=AsyncMock) as mock_api:
454-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
454+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
455455
mock_auth.return_value = "test_access_token"
456456
mock_api.return_value = european_mock_response
457457

@@ -462,7 +462,7 @@ async def test_dsa_beneficiary_validation_by_region(self):
462462

463463
# Test US account
464464
with patch('meta_ads_mcp.core.accounts.make_api_request', new_callable=AsyncMock) as mock_api:
465-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
465+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
466466
mock_auth.return_value = "test_access_token"
467467
mock_api.return_value = us_mock_response
468468

@@ -487,7 +487,7 @@ async def test_dsa_beneficiary_clear_error_message(self):
487487

488488
for error_message, expected_keyword in error_scenarios:
489489
with patch('meta_ads_mcp.core.adsets.make_api_request', new_callable=AsyncMock) as mock_api:
490-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
490+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
491491
mock_auth.return_value = "test_access_token"
492492
mock_api.side_effect = Exception(error_message)
493493

@@ -514,7 +514,7 @@ async def test_dsa_beneficiary_clear_error_message(self):
514514
async def test_dsa_beneficiary_fallback_behavior(self):
515515
"""Test fallback behavior for unexpected DSA-related errors"""
516516
with patch('meta_ads_mcp.core.adsets.make_api_request', new_callable=AsyncMock) as mock_api:
517-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
517+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
518518
mock_auth.return_value = "test_access_token"
519519
mock_api.side_effect = Exception("Unexpected DSA-related error")
520520

@@ -562,7 +562,7 @@ async def test_get_adset_details_with_dsa_beneficiary(self):
562562
}
563563

564564
with patch('meta_ads_mcp.core.adsets.make_api_request', new_callable=AsyncMock) as mock_api:
565-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
565+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
566566
mock_auth.return_value = "test_access_token"
567567
mock_api.return_value = mock_adset_response
568568

@@ -595,7 +595,7 @@ async def test_get_adset_details_without_dsa_beneficiary(self):
595595
}
596596

597597
with patch('meta_ads_mcp.core.adsets.make_api_request', new_callable=AsyncMock) as mock_api:
598-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
598+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
599599
mock_auth.return_value = "test_access_token"
600600
mock_api.return_value = mock_adset_response
601601

@@ -625,7 +625,7 @@ async def test_get_adset_details_empty_dsa_beneficiary(self):
625625
}
626626

627627
with patch('meta_ads_mcp.core.adsets.make_api_request', new_callable=AsyncMock) as mock_api:
628-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
628+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
629629
mock_auth.return_value = "test_access_token"
630630
mock_api.return_value = mock_adset_response
631631

@@ -640,7 +640,7 @@ async def test_get_adset_details_empty_dsa_beneficiary(self):
640640
async def test_get_adset_details_dsa_beneficiary_field_requested(self):
641641
"""Test that the API request includes dsa_beneficiary in the fields parameter"""
642642
with patch('meta_ads_mcp.core.adsets.make_api_request', new_callable=AsyncMock) as mock_api:
643-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
643+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
644644
mock_auth.return_value = "test_access_token"
645645
mock_api.return_value = {"id": "120229746629010183"}
646646

@@ -655,7 +655,7 @@ async def test_get_adset_details_dsa_beneficiary_field_requested(self):
655655
async def test_get_adset_details_error_handling(self):
656656
"""Test error handling when retrieving ad set details fails"""
657657
with patch('meta_ads_mcp.core.adsets.make_api_request', new_callable=AsyncMock) as mock_api:
658-
with patch('meta_ads_mcp.core.api.get_current_access_token', new_callable=AsyncMock) as mock_auth:
658+
with patch('meta_ads_mcp.core.auth.get_current_access_token', new_callable=AsyncMock) as mock_auth:
659659
mock_auth.return_value = "test_access_token"
660660
mock_api.side_effect = Exception("Ad set not found")
661661

0 commit comments

Comments
 (0)