AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(blocks): ask HubSpot users for a service key or private app token instead of a retired API key (#15367)
Bentlyed3fb3ef0f63
diff --git a/autogpt_platform/backend/backend/blocks/hubspot/_auth.py b/autogpt_platform/backend/backend/blocks/hubspot/_auth.pyindex b32456d5d5..41993b0057 100644--- a/autogpt_platform/backend/backend/blocks/hubspot/_auth.py+++ b/autogpt_platform/backend/backend/blocks/hubspot/_auth.py@@ -15,15 +15,23 @@ HubSpotCredentialsInput = CredentialsMetaInput[def HubSpotCredentialsField() -> HubSpotCredentialsInput:"""Creates a HubSpot credentials input on a block."""return CredentialsField(- description="The HubSpot integration requires an API Key.",+ description=(+ "A HubSpot service key, sent as a Bearer token. Create one in HubSpot "+ "under Development > Keys > Service keys with the scopes for the "+ "objects you use: crm.objects.companies.read and "+ "crm.objects.companies.write for companies, crm.objects.contacts.read "+ "and crm.objects.contacts.write for contacts and email engagements. "+ "An existing private app access token works too. HubSpot no longer "+ "accepts legacy API keys."+ ),)TEST_CREDENTIALS = APIKeyCredentials(id="01234567-89ab-cdef-0123-456789abcdef",provider="hubspot",- api_key=SecretStr("mock-hubspot-api-key"),- title="Mock HubSpot API key",+ api_key=SecretStr("mock-hubspot-access-token"),+ title="Mock HubSpot access token",expires_at=None,)diff --git a/autogpt_platform/backend/backend/blocks/hubspot/_config.py b/autogpt_platform/backend/backend/blocks/hubspot/_config.pyindex 9227b1fd62..9bb69b7334 100644--- a/autogpt_platform/backend/backend/blocks/hubspot/_config.py+++ b/autogpt_platform/backend/backend/blocks/hubspot/_config.py@@ -5,6 +5,6 @@ from backend.sdk import ProviderBuilderhubspot = (ProviderBuilder("hubspot").with_description("CRM, contacts, and deals")- .with_supported_auth_types("api_key", "oauth2")+ .with_supported_auth_types("api_key").build())diff --git a/autogpt_platform/backend/backend/blocks/hubspot/auth_test.py b/autogpt_platform/backend/backend/blocks/hubspot/auth_test.pynew file mode 100644index 0000000000..10b4498a18--- /dev/null+++ b/autogpt_platform/backend/backend/blocks/hubspot/auth_test.py@@ -0,0 +1,66 @@+from unittest.mock import AsyncMock, Mock, patch++import pytest+from pydantic import SecretStr++from backend.api.features.integrations.models import get_supported_auth_types+from backend.blocks.hubspot._auth import TEST_CREDENTIALS, TEST_CREDENTIALS_INPUT+from backend.blocks.hubspot._config import hubspot+from backend.blocks.hubspot.company import HubSpotCompanyBlock+from backend.blocks.hubspot.contact import HubSpotContactBlock+from backend.blocks.hubspot.engagement import HubSpotEngagementBlock+from backend.data.model import APIKeyCredentials++BLOCKS = [+ (HubSpotCompanyBlock, "company", {"operation": "create"}),+ (HubSpotContactBlock, "contact", {"operation": "create"}),+ (HubSpotEngagementBlock, "engagement", {"operation": "send_email"}),+]+++def test_hubspot_offers_no_oauth_sign_in():+ # There is no HubSpot OAuth handler, so an OAuth option can only 404.+ assert hubspot.supported_auth_types == {"api_key"}+ assert get_supported_auth_types("hubspot") == ["api_key"]++[email protected]("block_cls", [b[0] for b in BLOCKS])+def test_credential_field_asks_for_a_token_hubspot_still_issues(block_cls):+ schema = block_cls.Input.model_json_schema()["properties"]["credentials"]+ description = schema["description"]++ assert "service key" in description+ assert "private app access token" in description+ assert "Bearer" in description+ assert "crm.objects.companies.write" in description+ assert "crm.objects.contacts.write" in description+ assert "Legacy apps" not in description+ assert "requires an API Key" not in description+ field = block_cls.Input.get_credentials_fields_info()["credentials"]+ assert field.supported_types == frozenset({"api_key"})++[email protected](("block_cls", "module", "inputs"), BLOCKS)+async def test_token_is_sent_as_bearer(block_cls, module, inputs):+ token = "test-hubspot-access-token"+ credentials = APIKeyCredentials(+ id=TEST_CREDENTIALS.id,+ provider="hubspot",+ api_key=SecretStr(token),+ title="HubSpot private app",+ expires_at=None,+ )+ block = block_cls()+ response = Mock()+ response.json.return_value = {"id": "1"}+ with patch(f"backend.blocks.hubspot.{module}.Requests") as requests:+ requests.return_value.post = AsyncMock(return_value=response)+ input_data = block_cls.Input(credentials=TEST_CREDENTIALS_INPUT, **inputs)+ async for _ in block.run(input_data, credentials=credentials):+ pass++ post = requests.return_value.post+ post.assert_awaited_once()+ headers = post.await_args.kwargs["headers"]+ assert headers["Authorization"] == f"Bearer {token}"+ assert "hapikey" not in str(post.await_args)