mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-04-08 03:00:28 -04:00
removed unused oauth patching functions and tests
This commit is contained in:
@@ -146,12 +146,21 @@ class AutoRegistry:
|
||||
@classmethod
|
||||
def patch_integrations(cls) -> None:
|
||||
"""Patch existing integration points to use AutoRegistry."""
|
||||
# OAuth handlers are now handled by SDKAwareHandlersDict in oauth/__init__.py
|
||||
# OAuth handlers are handled by SDKAwareHandlersDict in oauth/__init__.py
|
||||
# No patching needed for OAuth handlers
|
||||
|
||||
# Patch webhook managers
|
||||
try:
|
||||
import backend.integrations.webhooks as webhooks
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
# Get the module from sys.modules to respect mocking
|
||||
if "backend.integrations.webhooks" in sys.modules:
|
||||
webhooks: Any = sys.modules["backend.integrations.webhooks"]
|
||||
else:
|
||||
import backend.integrations.webhooks
|
||||
|
||||
webhooks: Any = backend.integrations.webhooks
|
||||
|
||||
if hasattr(webhooks, "load_webhook_managers"):
|
||||
original_load = webhooks.load_webhook_managers
|
||||
|
||||
@@ -44,92 +44,6 @@ class MockWebhookManager(BaseWebhooksManager):
|
||||
pass
|
||||
|
||||
|
||||
class TestOAuthPatching:
|
||||
"""Test OAuth handler patching functionality."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Clear registry and set up mocks."""
|
||||
AutoRegistry.clear()
|
||||
|
||||
def test_oauth_handler_dictionary_patching(self):
|
||||
"""Test that OAuth handlers are correctly patched into HANDLERS_BY_NAME."""
|
||||
# Create original handlers
|
||||
original_handlers = {
|
||||
"existing_provider": Mock(spec=BaseOAuthHandler),
|
||||
}
|
||||
|
||||
# Create a mock oauth module
|
||||
mock_oauth_module = MagicMock()
|
||||
mock_oauth_module.HANDLERS_BY_NAME = original_handlers.copy()
|
||||
|
||||
# Register a new provider with OAuth
|
||||
(ProviderBuilder("new_oauth_provider").with_oauth(MockOAuthHandler).build())
|
||||
|
||||
# Apply patches
|
||||
with patch("backend.integrations.oauth", mock_oauth_module):
|
||||
AutoRegistry.patch_integrations()
|
||||
|
||||
patched_dict = mock_oauth_module.HANDLERS_BY_NAME
|
||||
|
||||
# Test that original handler still exists
|
||||
assert "existing_provider" in patched_dict
|
||||
assert (
|
||||
patched_dict["existing_provider"]
|
||||
== original_handlers["existing_provider"]
|
||||
)
|
||||
|
||||
# Test that new handler is accessible
|
||||
assert "new_oauth_provider" in patched_dict
|
||||
assert patched_dict["new_oauth_provider"] == MockOAuthHandler
|
||||
|
||||
# Test dict methods
|
||||
assert "existing_provider" in patched_dict.keys()
|
||||
assert "new_oauth_provider" in patched_dict.keys()
|
||||
|
||||
# Test .get() method
|
||||
assert patched_dict.get("new_oauth_provider") == MockOAuthHandler
|
||||
assert patched_dict.get("nonexistent", "default") == "default"
|
||||
|
||||
# Test __contains__
|
||||
assert "new_oauth_provider" in patched_dict
|
||||
assert "nonexistent" not in patched_dict
|
||||
|
||||
def test_oauth_patching_with_multiple_providers(self):
|
||||
"""Test patching with multiple OAuth providers."""
|
||||
|
||||
# Create another OAuth handler
|
||||
class AnotherOAuthHandler(BaseOAuthHandler):
|
||||
PROVIDER_NAME = ProviderName.GOOGLE
|
||||
|
||||
# Register multiple providers
|
||||
(ProviderBuilder("oauth_provider_1").with_oauth(MockOAuthHandler).build())
|
||||
|
||||
(ProviderBuilder("oauth_provider_2").with_oauth(AnotherOAuthHandler).build())
|
||||
|
||||
# Mock the oauth module
|
||||
mock_oauth_module = MagicMock()
|
||||
mock_oauth_module.HANDLERS_BY_NAME = {}
|
||||
|
||||
with patch("backend.integrations.oauth", mock_oauth_module):
|
||||
AutoRegistry.patch_integrations()
|
||||
|
||||
patched_dict = mock_oauth_module.HANDLERS_BY_NAME
|
||||
|
||||
# Both providers should be accessible
|
||||
assert patched_dict["oauth_provider_1"] == MockOAuthHandler
|
||||
assert patched_dict["oauth_provider_2"] == AnotherOAuthHandler
|
||||
|
||||
# Check values() method
|
||||
values = list(patched_dict.values())
|
||||
assert MockOAuthHandler in values
|
||||
assert AnotherOAuthHandler in values
|
||||
|
||||
# Check items() method
|
||||
items = dict(patched_dict.items())
|
||||
assert items["oauth_provider_1"] == MockOAuthHandler
|
||||
assert items["oauth_provider_2"] == AnotherOAuthHandler
|
||||
|
||||
|
||||
class TestWebhookPatching:
|
||||
"""Test webhook manager patching functionality."""
|
||||
|
||||
@@ -157,7 +71,9 @@ class TestWebhookPatching:
|
||||
mock_webhooks_module = MagicMock()
|
||||
mock_webhooks_module.load_webhook_managers = mock_load_webhook_managers
|
||||
|
||||
with patch("backend.integrations.webhooks", mock_webhooks_module):
|
||||
with patch.dict(
|
||||
"sys.modules", {"backend.integrations.webhooks": mock_webhooks_module}
|
||||
):
|
||||
AutoRegistry.patch_integrations()
|
||||
|
||||
# Call the patched function
|
||||
@@ -182,7 +98,9 @@ class TestWebhookPatching:
|
||||
.build()
|
||||
)
|
||||
|
||||
with patch("backend.integrations.webhooks", mock_webhooks_module):
|
||||
with patch.dict(
|
||||
"sys.modules", {"backend.integrations.webhooks": mock_webhooks_module}
|
||||
):
|
||||
# Should not raise an error
|
||||
AutoRegistry.patch_integrations()
|
||||
|
||||
@@ -190,73 +108,6 @@ class TestWebhookPatching:
|
||||
assert not hasattr(mock_webhooks_module, "load_webhook_managers")
|
||||
|
||||
|
||||
class TestPatchingEdgeCases:
|
||||
"""Test edge cases and error handling in patching."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Clear registry."""
|
||||
AutoRegistry.clear()
|
||||
|
||||
def test_patching_with_import_errors(self):
|
||||
"""Test that patching handles import errors gracefully."""
|
||||
# Register a provider
|
||||
(ProviderBuilder("test_provider").with_oauth(MockOAuthHandler).build())
|
||||
|
||||
# Make the oauth module import fail
|
||||
with patch("builtins.__import__", side_effect=ImportError("Mock import error")):
|
||||
# Should not raise an error
|
||||
AutoRegistry.patch_integrations()
|
||||
|
||||
def test_patching_with_attribute_errors(self):
|
||||
"""Test handling of missing attributes."""
|
||||
# Mock oauth module without HANDLERS_BY_NAME
|
||||
mock_oauth_module = MagicMock(spec=[])
|
||||
|
||||
(ProviderBuilder("test_provider").with_oauth(MockOAuthHandler).build())
|
||||
|
||||
with patch("backend.integrations.oauth", mock_oauth_module):
|
||||
# Should not raise an error
|
||||
AutoRegistry.patch_integrations()
|
||||
|
||||
def test_patching_preserves_thread_safety(self):
|
||||
"""Test that patching maintains thread safety."""
|
||||
import threading
|
||||
import time
|
||||
|
||||
results = []
|
||||
errors = []
|
||||
|
||||
def register_provider(name, delay=0):
|
||||
try:
|
||||
time.sleep(delay)
|
||||
(ProviderBuilder(name).with_oauth(MockOAuthHandler).build())
|
||||
results.append(name)
|
||||
except Exception as e:
|
||||
errors.append((name, str(e)))
|
||||
|
||||
# Create multiple threads
|
||||
threads = []
|
||||
for i in range(5):
|
||||
t = threading.Thread(
|
||||
target=register_provider, args=(f"provider_{i}", i * 0.01)
|
||||
)
|
||||
threads.append(t)
|
||||
t.start()
|
||||
|
||||
# Wait for all threads
|
||||
for t in threads:
|
||||
t.join()
|
||||
|
||||
# Check results
|
||||
assert len(errors) == 0
|
||||
assert len(results) == 5
|
||||
assert len(AutoRegistry._providers) == 5
|
||||
|
||||
# Verify all providers are registered
|
||||
for i in range(5):
|
||||
assert f"provider_{i}" in AutoRegistry._providers
|
||||
|
||||
|
||||
class TestPatchingIntegration:
|
||||
"""Test the complete patching integration flow."""
|
||||
|
||||
@@ -266,10 +117,7 @@ class TestPatchingIntegration:
|
||||
|
||||
def test_complete_provider_registration_and_patching(self):
|
||||
"""Test the complete flow from provider registration to patching."""
|
||||
# Mock both oauth and webhooks modules
|
||||
mock_oauth = MagicMock()
|
||||
mock_oauth.HANDLERS_BY_NAME = {"original": Mock()}
|
||||
|
||||
# Mock webhooks module
|
||||
mock_webhooks = MagicMock()
|
||||
mock_webhooks.load_webhook_managers = lambda: {"original": Mock()}
|
||||
|
||||
@@ -283,43 +131,19 @@ class TestPatchingIntegration:
|
||||
)
|
||||
|
||||
# Apply patches
|
||||
with patch("backend.integrations.oauth", mock_oauth):
|
||||
with patch("backend.integrations.webhooks", mock_webhooks):
|
||||
AutoRegistry.patch_integrations()
|
||||
|
||||
# Verify OAuth patching
|
||||
oauth_dict = mock_oauth.HANDLERS_BY_NAME
|
||||
assert "complete_provider" in oauth_dict
|
||||
assert oauth_dict["complete_provider"] == MockOAuthHandler
|
||||
assert "original" in oauth_dict # Original preserved
|
||||
|
||||
# Verify webhook patching
|
||||
webhook_result = mock_webhooks.load_webhook_managers()
|
||||
assert "complete_provider" in webhook_result
|
||||
assert webhook_result["complete_provider"] == MockWebhookManager
|
||||
assert "original" in webhook_result # Original preserved
|
||||
|
||||
def test_patching_is_idempotent(self):
|
||||
"""Test that calling patch_integrations multiple times is safe."""
|
||||
mock_oauth = MagicMock()
|
||||
mock_oauth.HANDLERS_BY_NAME = {}
|
||||
|
||||
# Register a provider
|
||||
(ProviderBuilder("test_provider").with_oauth(MockOAuthHandler).build())
|
||||
|
||||
with patch("backend.integrations.oauth", mock_oauth):
|
||||
# Patch multiple times
|
||||
AutoRegistry.patch_integrations()
|
||||
AutoRegistry.patch_integrations()
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"backend.integrations.webhooks": mock_webhooks,
|
||||
},
|
||||
):
|
||||
AutoRegistry.patch_integrations()
|
||||
|
||||
# Should still work correctly
|
||||
oauth_dict = mock_oauth.HANDLERS_BY_NAME
|
||||
assert "test_provider" in oauth_dict
|
||||
assert oauth_dict["test_provider"] == MockOAuthHandler
|
||||
|
||||
# Should not have duplicates or errors
|
||||
assert len([k for k in oauth_dict.keys() if k == "test_provider"]) == 1
|
||||
# Verify webhook patching
|
||||
webhook_result = mock_webhooks.load_webhook_managers()
|
||||
assert "complete_provider" in webhook_result
|
||||
assert webhook_result["complete_provider"] == MockWebhookManager
|
||||
assert "original" in webhook_result # Original preserved
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -247,39 +247,6 @@ class TestAutoRegistryPatching:
|
||||
"""Clear registry before each test."""
|
||||
AutoRegistry.clear()
|
||||
|
||||
@patch("backend.integrations.oauth.HANDLERS_BY_NAME", {})
|
||||
def test_oauth_handler_patching(self):
|
||||
"""Test that OAuth handlers are patched into the system."""
|
||||
|
||||
# Create a test OAuth handler
|
||||
class TestOAuthHandler(BaseOAuthHandler):
|
||||
PROVIDER_NAME = ProviderName.GITHUB
|
||||
|
||||
# Register a provider with OAuth
|
||||
provider = Provider(
|
||||
name="patched_provider",
|
||||
oauth_handler=TestOAuthHandler,
|
||||
webhook_manager=None,
|
||||
default_credentials=[],
|
||||
base_costs=[],
|
||||
supported_auth_types={"oauth2"},
|
||||
)
|
||||
|
||||
AutoRegistry.register_provider(provider)
|
||||
|
||||
# Mock the oauth module
|
||||
mock_oauth = MagicMock()
|
||||
mock_oauth.HANDLERS_BY_NAME = {}
|
||||
|
||||
with patch("backend.integrations.oauth", mock_oauth):
|
||||
# Apply patches
|
||||
AutoRegistry.patch_integrations()
|
||||
|
||||
# Verify the patched dict works
|
||||
patched_dict = mock_oauth.HANDLERS_BY_NAME
|
||||
assert "patched_provider" in patched_dict
|
||||
assert patched_dict["patched_provider"] == TestOAuthHandler
|
||||
|
||||
@patch("backend.integrations.webhooks.load_webhook_managers")
|
||||
def test_webhook_manager_patching(self, mock_load_managers):
|
||||
"""Test that webhook managers are patched into the system."""
|
||||
@@ -306,7 +273,9 @@ class TestAutoRegistryPatching:
|
||||
mock_webhooks = MagicMock()
|
||||
mock_webhooks.load_webhook_managers = mock_load_managers
|
||||
|
||||
with patch("backend.integrations.webhooks", mock_webhooks):
|
||||
with patch.dict(
|
||||
"sys.modules", {"backend.integrations.webhooks": mock_webhooks}
|
||||
):
|
||||
# Apply patches
|
||||
AutoRegistry.patch_integrations()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user