Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

refactor(api): decouple console API-based extension management (#43340)

KenXiong81f627cfa1eb
diff --git a/api/.importlinter b/api/.importlinter
index 207c7f8340a..ed0d82d4485 100644
--- a/api/.importlinter
+++ b/api/.importlinter
@@ -832,6 +832,36 @@ forbidden_modules =
sqlalchemy
werkzeug
+[importlinter:contract:api-based-extension-application-service-boundary]
+name = API-based extension application service is framework and persistence neutral
+type = forbidden
+source_modules =
+ services.api_based_extension_application_service
+forbidden_modules =
+ configs
+ controllers
+ core
+ extensions
+ flask
+ models
+ repositories
+ sqlalchemy
+ werkzeug
+
+[importlinter:contract:console-extension-controller-boundary]
+name = Console extension controller reaches persistence only through the composition root
+type = forbidden
+source_modules =
+ controllers.console.extension
+forbidden_modules =
+ extensions.ext_database
+ models
+ repositories
+ sqlalchemy
+# The composition root (extensions.ext_application_services) wires repositories,
+# so only direct imports are checked here.
+allow_indirect_imports = True
+
[importlinter:contract:tool-file-download-service-boundary]
name = Tool file download application service is framework and persistence neutral
type = forbidden
diff --git a/api/controllers/console/extension.py b/api/controllers/console/extension.py
index 6c1904f111f..97a27b9f678 100644
--- a/api/controllers/console/extension.py
+++ b/api/controllers/console/extension.py
@@ -6,17 +6,23 @@ from flask_restx import Resource
from pydantic import BaseModel, Field, RootModel, field_validator
from constants import HIDDEN_VALUE
-from extensions.ext_database import db
+from controllers.console.flask_admission import console_account_admission
+from extensions.ext_application_services import application_services
from fields.base import ResponseModel
from libs.helper import dump_response, to_timestamp
from libs.login import login_required
-from models.api_based_extension import APIBasedExtension
-from services.api_based_extension_service import APIBasedExtensionService
+from machinery.context import RequestContext
+from services.api_based_extension_application_service import (
+ APIBasedExtensionError,
+ APIBasedExtensionInput,
+ APIBasedExtensionRecord,
+ APIBasedExtensionUpdate,
+)
from services.code_based_extension_service import CodeBasedExtensionService
from ..common.schema import query_params_from_model, register_response_schema_models, register_schema_models
from . import console_ns
-from .wraps import account_initialization_required, model_validate, setup_required, with_current_tenant_id
+from .wraps import account_initialization_required, model_validate, setup_required
class CodeBasedExtensionQuery(BaseModel):
@@ -77,6 +83,10 @@ register_response_schema_models(
)
+def _extension_response(extension: APIBasedExtensionRecord) -> dict[str, Any]:
+ return dump_response(APIBasedExtensionResponse, extension)
+
+
@console_ns.route("/code-based-extension")
class CodeBasedExtensionAPI(Resource):
@console_ns.doc("get_code_based_extension")
@@ -103,41 +113,32 @@ class APIBasedExtensionAPI(Resource):
@console_ns.doc("get_api_based_extensions")
@console_ns.doc(description="Get all API-based extensions for current tenant")
@console_ns.response(200, "Success", console_ns.models[APIBasedExtensionListResponse.__name__])
- @setup_required
- @login_required
- @account_initialization_required
- @with_current_tenant_id
- def get(self, current_tenant_id: str):
- return dump_response(
- APIBasedExtensionListResponse,
- APIBasedExtensionService.get_all_by_tenant_id(current_tenant_id, session=db.session()),
- )
+ @console_account_admission()
+ def get(self, request_context: RequestContext):
+ extensions = application_services().api_based_extensions.list_extensions(request_context)
+ return dump_response(APIBasedExtensionListResponse, extensions)
@console_ns.doc("create_api_based_extension")
@console_ns.doc(description="Create a new API-based extension")
@console_ns.expect(console_ns.models[APIBasedExtensionPayload.__name__])
@console_ns.response(201, "Extension created successfully", console_ns.models[APIBasedExtensionResponse.__name__])
- @setup_required
- @login_required
- @account_initialization_required
- @with_current_tenant_id
+ @console_account_admission()
@model_validate(APIBasedExtensionPayload)
- def post(self, req_data: APIBasedExtensionPayload, current_tenant_id: str):
- extension_data = APIBasedExtension(
- tenant_id=current_tenant_id,
- name=req_data.name,
- api_endpoint=req_data.api_endpoint,
- api_key=req_data.api_key,
- )
-
- extension = APIBasedExtensionService.save(extension_data, session=db.session())
- return APIBasedExtensionResponse(
- id=extension.id,
- name=extension.name,
- api_endpoint=extension.api_endpoint,
- api_key=req_data.api_key,
- created_at=to_timestamp(extension.created_at),
- ).model_dump(mode="json"), 201
+ def post(self, req_data: APIBasedExtensionPayload, request_context: RequestContext):
+ try:
+ extension = application_services().api_based_extensions.create_extension(
+ request_context,
+ APIBasedExtensionInput(
+ name=req_data.name,
+ api_endpoint=req_data.api_endpoint,
+ api_key=req_data.api_key,
+ ),
+ )
+ except APIBasedExtensionError as error:
+ # Legacy clients receive the 400 `invalid_param` envelope for every extension failure.
+ raise ValueError(str(error)) from None
+
+ return _extension_response(extension), 201
@console_ns.route("/api-based-extension/<uuid:id>")
@@ -146,70 +147,48 @@ class APIBasedExtensionDetailAPI(Resource):
@console_ns.doc(description="Get API-based extension by ID")
@console_ns.doc(params={"id": "Extension ID"})
@console_ns.response(200, "Success", console_ns.models[APIBasedExtensionResponse.__name__])
- @setup_required
- @login_required
- @account_initialization_required
- @with_current_tenant_id
- def get(self, current_tenant_id: str, id: UUID):
- api_based_extension_id = str(id)
+ @console_account_admission()
+ def get(self, request_context: RequestContext, id: UUID):
+ try:
+ extension = application_services().api_based_extensions.get_extension(request_context, str(id))
+ except APIBasedExtensionError as error:
+ raise ValueError(str(error)) from None
- return dump_response(
- APIBasedExtensionResponse,
- APIBasedExtensionService.get_with_tenant_id(
- current_tenant_id, api_based_extension_id, session=db.session()
- ),
- )
+ return _extension_response(extension)
@console_ns.doc("update_api_based_extension")
@console_ns.doc(description="Update API-based extension")
@console_ns.doc(params={"id": "Extension ID"})
@console_ns.expect(console_ns.models[APIBasedExtensionPayload.__name__])
@console_ns.response(200, "Extension updated successfully", console_ns.models[APIBasedExtensionResponse.__name__])
- @setup_required
- @login_required
- @account_initialization_required
- @with_current_tenant_id
+ @console_account_admission()
@model_validate(APIBasedExtensionPayload)
- def post(self, req_data: APIBasedExtensionPayload, current_tenant_id: str, id: UUID):
- api_based_extension_id = str(id)
-
- extension_data_from_db = APIBasedExtensionService.get_with_tenant_id(
- current_tenant_id, api_based_extension_id, session=db.session()
- )
-
- api_key_for_response = extension_data_from_db.api_key
-
- extension_data_from_db.name = req_data.name
- extension_data_from_db.api_endpoint = req_data.api_endpoint
-
- if req_data.api_key != HIDDEN_VALUE:
- extension_data_from_db.api_key = req_data.api_key
- api_key_for_response = req_data.api_key
-
- APIBasedExtensionService.save(extension_data_from_db, session=db.session())
- return APIBasedExtensionResponse(
- id=extension_data_from_db.id,
- name=extension_data_from_db.name,
- api_endpoint=extension_data_from_db.api_endpoint,
- api_key=api_key_for_response,
- created_at=to_timestamp(extension_data_from_db.created_at),
- ).model_dump(mode="json")
+ def post(self, req_data: APIBasedExtensionPayload, request_context: RequestContext, id: UUID):
+ try:
+ extension = application_services().api_based_extensions.update_extension(
+ request_context,
+ str(id),
+ APIBasedExtensionUpdate(
+ name=req_data.name,
+ api_endpoint=req_data.api_endpoint,
+ # The console echoes the masked key back; keep the stored key when it is sent unchanged.
+ api_key=None if req_data.api_key == HIDDEN_VALUE else req_data.api_key,
+ ),
+ )
+ except APIBasedExtensionError as error:
+ raise ValueError(str(error)) from None
+
+ return _extension_response(extension)
@console_ns.doc("delete_api_based_extension")
@console_ns.doc(description="Delete API-based extension")
@console_ns.doc(params={"id": "Extension ID"})
@console_ns.response(204, "Extension deleted successfully")
- @setup_required
- @login_required
- @account_initialization_required
- @with_current_tenant_id
- def delete(self, current_tenant_id: str, id: UUID):
- api_based_extension_id = str(id)
-
- extension_data_from_db = APIBasedExtensionService.get_with_tenant_id(
- current_tenant_id, api_based_extension_id, session=db.session()
- )
-
- APIBasedExtensionService.delete(extension_data_from_db, session=db.session())
+ @console_account_admission()
+ def delete(self, request_context: RequestContext, id: UUID):
+ try:
+ application_services().api_based_extensions.delete_extension(request_context, str(id))
+ except APIBasedExtensionError as error:
+ raise ValueError(str(error)) from None
return "", 204
diff --git a/api/extensions/ext_application_services.py b/api/extensions/ext_application_services.py
index cb36a69716f..f86e4b10500 100644
--- a/api/extensions/ext_application_services.py
+++ b/api/extensions/ext_application_services.py
@@ -49,6 +49,7 @@ from models.model import EndUser
from repositories.account.repository import SQLAlchemyAccountRepository
from repositories.account_activation_repository import SQLAlchemyAccountActivationRepository
from repositories.account_integration_repository import SQLAlchemyAccountIntegrationRepository
+from repositories.api_based_extension_repository import APIBasedExtensionRepository
from repositories.app.mcp_server_repository import AppMCPServerRepository
from repositories.app.site_command_repository import AppSiteCommandRepository
from repositories.app.tracing_config_repository import SQLAlchemyAppTracingConfigRepository
@@ -92,6 +93,8 @@ from services.account.login_adapters import RedisConsoleAuthSecurityGateway
from services.account.service import AccountSetupProvisioner
from services.account_password_hasher import DefaultAccountPasswordHasher
from services.agent.roster_package_exporter import RosterAgentPackageExporter
+from services.api_based_extension_adapters import APIBasedExtensionPingProbe, WorkspaceTokenCipher
+from services.api_based_extension_application_service import APIBasedExtensionApplicationService
from services.app.advanced_prompt_template_service import AdvancedPromptTemplateService
from services.app.api_key_service import AppApiKeyService
from services.app.mcp_server_service import AppMCPServerService
@@ -240,6 +243,7 @@ class AppScopedEndUserServices:
class ApplicationServices:
agent_apps: AgentAppServices
advanced_prompt_templates: AdvancedPromptTemplateService
+ api_based_extensions: APIBasedExtensionApplicationService
credential_queries: CredentialQuery
accounts: AccountServices
app_api_keys: AppApiKeyService
@@ -677,6 +681,11 @@ def build_application_services(
now=lambda: datetime.now(UTC),
access_token_expire_minutes=dify_config.ACCESS_TOKEN_EXPIRE_MINUTES,
),
+ api_based_extensions=APIBasedExtensionApplicationService(
+ extensions=APIBasedExtensionRepository(session_factory=database_client),
+ secrets=WorkspaceTokenCipher(),
+ probe=APIBasedExtensionPingProbe(),
+ ),
tags=tags,
workflow_statistics=WorkflowStatisticQueryService(
workflow_runs=DifyAPIRepositoryFactory.create_api_workflow_run_repository(
diff --git a/api/repositories/api_based_extension_repository.py b/api/repositories/api_based_extension_repository.py
new file mode 100644
index 00000000000..c67652aecbf
--- /dev/null
+++ b/api/repositories/api_based_extension_repository.py
@@ -0,0 +1,104 @@
+"""SQLAlchemy persistence adapter for Console API-based extensions."""
+
+from typing import override
+
+from sqlalchemy import select
+from sqlalchemy.orm import Session, sessionmaker
+
+from models.api_based_extension import APIBasedExtension
+from services.api_based_extension_application_service import (
+ APIBasedExtensionInput,
+ APIBasedExtensionNotFoundError,
+ APIBasedExtensionRecord,
+ APIBasedExtensionStore,
+)
+
+
+class APIBasedExtensionRepository(APIBasedExtensionStore):
+ def __init__(self, *, session_factory: sessionmaker[Session]) -> None:
+ self._session_factory = session_factory
+
+ @override
+ def list_extensions(self, workspace_id: str) -> tuple[APIBasedExtensionRecord, ...]:
+ with self._session_factory() as session:
+ return tuple(
+ self._to_record(extension)
+ for extension in session.scalars(
+ select(APIBasedExtension)
+ .where(APIBasedExtension.tenant_id == workspace_id)
+ .order_by(APIBasedExtension.created_at.desc(), APIBasedExtension.id.desc())
+ ).all()
+ )
+
+ @override
+ def find_extension(self, workspace_id: str, extension_id: str) -> APIBasedExtensionRecord | None:
+ with self._session_factory() as session:
+ extension = self._find(session, workspace_id, extension_id)
+ return self._to_record(extension) if extension is not None else None
+
+ @override
+ def name_exists(self, workspace_id: str, name: str, *, exclude_id: str | None = None) -> bool:
+ stmt = select(APIBasedExtension.id).where(
+ APIBasedExtension.tenant_id == workspace_id,
+ APIBasedExtension.name == name,
+ )
+ if exclude_id is not None:
+ stmt = stmt.where(APIBasedExtension.id != exclude_id)
+ with self._session_factory() as session:
+ return session.scalar(stmt.limit(1)) is not None
+
+ @override
+ def create_extension(self, workspace_id: str, extension: APIBasedExtensionInput) -> APIBasedExtensionRecord:
+ with self._session_factory.begin() as session:
+ model = APIBasedExtension(
+ tenant_id=workspace_id,
+ name=extension.name,
+ api_endpoint=extension.api_endpoint,
+ api_key=extension.api_key,
+ )
+ session.add(model)
+ session.flush()
+ session.refresh(model)
+ return self._to_record(model)
+
+ @override
+ def update_extension(
+ self, workspace_id: str, extension_id: str, extension: APIBasedExtensionInput
+ ) -> APIBasedExtensionRecord:
+ with self._session_factory.begin() as session:
+ model = self._require(session, workspace_id, extension_id)
+ model.name = extension.name
+ model.api_endpoint = extension.api_endpoint
+ model.api_key = extension.api_key
+ session.flush()
+ return self._to_record(model)
+
+ @override
+ def delete_extension(self, workspace_id: str, extension_id: str) -> None:
+ with self._session_factory.begin() as session:
+ session.delete(self._require(session, workspace_id, extension_id))
+
+ @staticmethod
+ def _find(session: Session, workspace_id: str, extension_id: str) -> APIBasedExtension | None:
+ return session.scalar(
+ select(APIBasedExtension)
+ .where(APIBasedExtension.tenant_id == workspace_id, APIBasedExtension.id == extension_id)
+ .limit(1)
+ )
+
+ @classmethod
+ def _require(cls, session: Session, workspace_id: str, extension_id: str) -> APIBasedExtension:
+ extension = cls._find(session, workspace_id, extension_id)
+ if extension is None:
+ raise APIBasedExtensionNotFoundError
+ return extension
+
+ @staticmethod
+ def _to_record(extension: APIBasedExtension) -> APIBasedExtensionRecord:
+ return APIBasedExtensionRecord(
+ id=extension.id,
+ name=extension.name,
+ api_endpoint=extension.api_endpoint,
+ api_key=extension.api_key,
+ created_at=extension.created_at,
+ )
diff --git a/api/services/api_based_extension_adapters.py b/api/services/api_based_extension_adapters.py
new file mode 100644
index 00000000000..866d09f026c
--- /dev/null
+++ b/api/services/api_based_extension_adapters.py
@@ -0,0 +1,40 @@
+"""Runtime adapters for the API-based extension application service."""
+
+from typing import override
+
+from core.extension.api_based_extension_requestor import APIBasedExtensionRequestor
+from core.helper.encrypter import decrypt_token, encrypt_token
+from models.api_based_extension import APIBasedExtensionPoint
+from services.api_based_extension_application_service import (
+ APIBasedExtensionConnectionError,
+ APIBasedExtensionEndpointProbe,
+ APIBasedExtensionSecretCipher,
+)
+
+
+class WorkspaceTokenCipher(APIBasedExtensionSecretCipher):
+ """Encrypts API keys with the workspace key provider used by the rest of the platform."""
+
+ @override
+ def encrypt(self, workspace_id: str, value: str) -> str:
+ return encrypt_token(workspace_id, value)
+
+ @override
+ def decrypt(self, workspace_id: str, value: str) -> str:
+ return decrypt_token(workspace_id, value)
+
+
+class APIBasedExtensionPingProbe(APIBasedExtensionEndpointProbe):
+ """Sends the ``ping`` extension point through the SSRF-guarded extension requestor."""
+
+ @override
+ def ping(self, api_endpoint: str, api_key: str) -> None:
+ try:
+ response = APIBasedExtensionRequestor(api_endpoint, api_key).request(
+ point=APIBasedExtensionPoint.PING, params={}
+ )
+ except Exception as error:
+ raise APIBasedExtensionConnectionError(str(error)) from error
+ # The requestor only promises JSON; an endpoint may answer with a list or null instead of an object.
+ if not isinstance(response, dict) or response.get("result") != "pong":
+ raise APIBasedExtensionConnectionError(str(response))
diff --git a/api/services/api_based_extension_application_service.py b/api/services/api_based_extension_application_service.py
new file mode 100644
index 00000000000..1916fe91310
--- /dev/null
+++ b/api/services/api_based_extension_application_service.py
@@ -0,0 +1,178 @@
+"""Application boundary for Console API-based extension management."""
+
+from collections.abc import Sequence
+from datetime import datetime
+from typing import NamedTuple, Protocol
+
+from machinery.context import RequestContext
+
+_MIN_API_KEY_LENGTH = 5
+
+
+class APIBasedExtensionRecord(NamedTuple):
+ """One API-based extension of a workspace.
+
+ The store persists and returns ``api_key`` as ciphertext; the application service
+ hands callers the plaintext key.
+ """
+
+ id: str
+ name: str
+ api_endpoint: str
+ api_key: str
+ created_at: datetime
+
+
+class APIBasedExtensionInput(NamedTuple):
+ """Field values for creating an extension; ``api_key`` is plaintext."""
+
+ name: str
+ api_endpoint: str
+ api_key: str
+
+
+class APIBasedExtensionUpdate(NamedTuple):
+ """Field edits for an extension; a ``None`` ``api_key`` keeps the stored key."""
+
+ name: str
+ api_endpoint: str
+ api_key: str | None
+
+
+class APIBasedExtensionStore(Protocol):
+ """Persistence for API-based extensions, always scoped by workspace."""
+
+ def list_extensions(self, workspace_id: str) -> Sequence[APIBasedExtensionRecord]: ...
+
+ def find_extension(self, workspace_id: str, extension_id: str) -> APIBasedExtensionRecord | None: ...
+
+ def name_exists(self, workspace_id: str, name: str, *, exclude_id: str | None = None) -> bool: ...
+
+ def create_extension(self, workspace_id: str, extension: APIBasedExtensionInput) -> APIBasedExtensionRecord:
+ """Insert a new extension; ``extension.api_key`` is already ciphertext."""
+ ...
+
+ def update_extension(
+ self, workspace_id: str, extension_id: str, extension: APIBasedExtensionInput
+ ) -> APIBasedExtensionRecord:
+ """Overwrite all editable fields; raise ``APIBasedExtensionNotFoundError`` if absent from this scope."""
+ ...
+
+ def delete_extension(self, workspace_id: str, extension_id: str) -> None:
+ """Delete the extension; raise ``APIBasedExtensionNotFoundError`` if absent from this scope."""
+ ...
+
+
+class APIBasedExtensionSecretCipher(Protocol):
+ """Workspace-scoped encryption for stored API keys."""
+
+ def encrypt(self, workspace_id: str, value: str) -> str: ...
+
+ def decrypt(self, workspace_id: str, value: str) -> str: ...
+
+
+class APIBasedExtensionEndpointProbe(Protocol):
+ """Verifies that an extension endpoint answers the ``ping`` extension point."""
+
+ def ping(self, api_endpoint: str, api_key: str) -> None:
+ """Return normally when the endpoint answers ``pong``; raise ``APIBasedExtensionConnectionError`` otherwise."""
+ ...
+
+
+class APIBasedExtensionError(Exception):
+ """Base class for framework-neutral API-based extension failures."""
+
+
+class APIBasedExtensionNotFoundError(APIBasedExtensionError):
+ def __init__(self) -> None:
+ super().__init__("API based extension is not found")
+
+
+class APIBasedExtensionNameConflictError(APIBasedExtensionError):
+ def __init__(self) -> None:
+ super().__init__("name must be unique, it is already existed")
+
+
+class APIBasedExtensionInvalidError(APIBasedExtensionError):
+ """A field value does not satisfy the extension contract."""
+
+
+class APIBasedExtensionConnectionError(APIBasedExtensionError):
+ def __init__(self, reason: str) -> None:
+ super().__init__(f"connection error: {reason}")
+
+
+class APIBasedExtensionApplicationService:
+ """Owns the extension rules: non-empty fields, a workspace-unique name, a key of at least five
+ characters, an endpoint that answers ``ping``, and keys stored only as ciphertext."""
+
+ def __init__(
+ self,
+ *,
+ extensions: APIBasedExtensionStore,
+ secrets: APIBasedExtensionSecretCipher,
+ probe: APIBasedExtensionEndpointProbe,
+ ) -> None:
+ self._extensions = extensions
+ self._secrets = secrets
+ self._probe = probe
+
+ def list_extensions(self, context: RequestContext) -> tuple[APIBasedExtensionRecord, ...]:
+ workspace_id = context.active_workspace_id
+ return tuple(
+ self._with_plaintext_key(workspace_id, record) for record in self._extensions.list_extensions(workspace_id)
+ )
+
+ def get_extension(self, context: RequestContext, extension_id: str) -> APIBasedExtensionRecord:
+ workspace_id = context.active_workspace_id
+ return self._with_plaintext_key(workspace_id, self._require_extension(workspace_id, extension_id))
+
+ def create_extension(self, context: RequestContext, extension: APIBasedExtensionInput) -> APIBasedExtensionRecord:
+ workspace_id = context.active_workspace_id
+ self._validate(workspace_id, extension, exclude_id=None)
+ stored = self._extensions.create_extension(
+ workspace_id,
+ extension._replace(api_key=self._secrets.encrypt(workspace_id, extension.api_key)),
+ )
+ return stored._replace(api_key=extension.api_key)
+
+ def update_extension(
+ self, context: RequestContext, extension_id: str, update: APIBasedExtensionUpdate
+ ) -> APIBasedExtensionRecord:
+ workspace_id = context.active_workspace_id
+ current = self._require_extension(workspace_id, extension_id)
+ api_key = update.api_key if update.api_key is not None else self._secrets.decrypt(workspace_id, current.api_key)
+ extension = APIBasedExtensionInput(name=update.name, api_endpoint=update.api_endpoint, api_key=api_key)
+ self._validate(workspace_id, extension, exclude_id=extension_id)
+ stored = self._extensions.update_extension(
+ workspace_id,
+ extension_id,
+ extension._replace(api_key=self._secrets.encrypt(workspace_id, api_key)),
+ )
+ return stored._replace(api_key=api_key)
+
+ def delete_extension(self, context: RequestContext, extension_id: str) -> None:
+ # The store raises APIBasedExtensionNotFoundError itself, so no separate existence read is needed.
+ self._extensions.delete_extension(context.active_workspace_id, extension_id)
+
+ def _require_extension(self, workspace_id: str, extension_id: str) -> APIBasedExtensionRecord:
+ record = self._extensions.find_extension(workspace_id, extension_id)
+ if record is None:
+ raise APIBasedExtensionNotFoundError
+ return record
+
+ def _with_plaintext_key(self, workspace_id: str, record: APIBasedExtensionRecord) -> APIBasedExtensionRecord:
+ return record._replace(api_key=self._secrets.decrypt(workspace_id, record.api_key))
+
+ def _validate(self, workspace_id: str, extension: APIBasedExtensionInput, *, exclude_id: str | None) -> None:
+ if not extension.name:
+ raise APIBasedExtensionInvalidError("name must not be empty")
+ if self._extensions.name_exists(workspace_id, extension.name, exclude_id=exclude_id):
+ raise APIBasedExtensionNameConflictError
+ if not extension.api_endpoint:
+ raise APIBasedExtensionInvalidError("api_endpoint must not be empty")
+ if not extension.api_key:
+ raise APIBasedExtensionInvalidError("api_key must not be empty")
+ if len(extension.api_key) < _MIN_API_KEY_LENGTH:
+ raise APIBasedExtensionInvalidError(f"api_key must be at least {_MIN_API_KEY_LENGTH} characters")
+ self._probe.ping(extension.api_endpoint, extension.api_key)
diff --git a/api/services/api_based_extension_service.py b/api/services/api_based_extension_service.py
deleted file mode 100644
index e855780d6a1..00000000000
--- a/api/services/api_based_extension_service.py
+++ /dev/null
@@ -1,111 +0,0 @@
-from sqlalchemy import select
-from sqlalchemy.orm import Session
-
-from core.extension.api_based_extension_requestor import APIBasedExtensionRequestor
-from core.helper.encrypter import decrypt_token, encrypt_token
-from models.api_based_extension import APIBasedExtension, APIBasedExtensionPoint
-
-
-class APIBasedExtensionService:
- @staticmethod
- def get_all_by_tenant_id(tenant_id: str, *, session: Session) -> list[APIBasedExtension]:
- extension_list = list(
- session.scalars(
- select(APIBasedExtension)
- .where(APIBasedExtension.tenant_id == tenant_id)
- .order_by(APIBasedExtension.created_at.desc())
- ).all()
- )
-
- for extension in extension_list:
- extension.api_key = decrypt_token(extension.tenant_id, extension.api_key)
-
- return extension_list
-
- @classmethod
- def save(cls, extension_data: APIBasedExtension, *, session: Session) -> APIBasedExtension:
- cls._validation(session, extension_data)
-
- extension_data.api_key = encrypt_token(extension_data.tenant_id, extension_data.api_key)
-
- session.add(extension_data)
- session.commit()
- return extension_data
-
- @staticmethod
- def delete(extension_data: APIBasedExtension, *, session: Session):
- session.delete(extension_data)
- session.commit()
-
- @staticmethod
- def get_with_tenant_id(tenant_id: str, api_based_extension_id: str, *, session: Session) -> APIBasedExtension:
- extension = session.scalar(
- select(APIBasedExtension)
- .where(APIBasedExtension.tenant_id == tenant_id, APIBasedExtension.id == api_based_extension_id)
- .limit(1)
- )
-
- if not extension:
- raise ValueError("API based extension is not found")
-
- extension.api_key = decrypt_token(extension.tenant_id, extension.api_key)
-
- return extension
-
- @classmethod
- def _validation(cls, session: Session, extension_data: APIBasedExtension):
- # name
- if not extension_data.name:
- raise ValueError("name must not be empty")
-
- if not extension_data.id:
- # case one: check new data, name must be unique
- is_name_existed = session.scalar(
- select(APIBasedExtension)
- .where(
- APIBasedExtension.tenant_id == extension_data.tenant_id,
- APIBasedExtension.name == extension_data.name,
- )
- .limit(1)
- )
-
- if is_name_existed:
- raise ValueError("name must be unique, it is already existed")
- else:
- # case two: check existing data, name must be unique
- is_name_existed = session.scalar(
- select(APIBasedExtension)
- .where(
- APIBasedExtension.tenant_id == extension_data.tenant_id,
- APIBasedExtension.name == extension_data.name,
- APIBasedExtension.id != extension_data.id,
- )
- .limit(1)
- )
-
- if is_name_existed:
- raise ValueError("name must be unique, it is already existed")
-
- # api_endpoint
- if not extension_data.api_endpoint:
- raise ValueError("api_endpoint must not be empty")
-
- # api_key
- if not extension_data.api_key:
- raise ValueError("api_key must not be empty")
-
- if len(extension_data.api_key) < 5:
- raise ValueError("api_key must be at least 5 characters")
-
- # check endpoint
- cls._ping_connection(extension_data)
-
- @staticmethod
- def _ping_connection(extension_data: APIBasedExtension):
- try:
- client = APIBasedExtensionRequestor(extension_data.api_endpoint, extension_data.api_key)
- resp = client.request(point=APIBasedExtensionPoint.PING, params={})
- if resp.get("result") != "pong":
- raise ValueError(resp)
- except Exception as e:
- raise ValueError(f"connection error: {e}")
diff --git a/api/tests/test_containers_integration_tests/controllers/console/test_api_based_extension.py b/api/tests/test_containers_integration_tests/controllers/console/test_api_based_extension.py
index 4cca4c2170f..349ec8dceb5 100644
--- a/api/tests/test_containers_integration_tests/controllers/console/test_api_based_extension.py
+++ b/api/tests/test_containers_integration_tests/controllers/console/test_api_based_extension.py
@@ -10,9 +10,9 @@ from sqlalchemy import select
from sqlalchemy.orm import Session
from constants import HIDDEN_VALUE
+from core.helper.encrypter import encrypt_token
from libs.rsa import generate_key_pair
from models.api_based_extension import APIBasedExtension
-from services.api_based_extension_service import APIBasedExtensionService
from tests.test_containers_integration_tests.controllers.console.helpers import (
authenticate_console_client,
create_console_account_and_tenant,
@@ -41,7 +41,7 @@ def api_extension_client(
@pytest.fixture(autouse=True)
def mock_api_based_extension_ping():
- with patch("services.api_based_extension_service.APIBasedExtensionRequestor") as requestor:
+ with patch("services.api_based_extension_adapters.APIBasedExtensionRequestor") as requestor:
requestor.return_value.request.return_value = {"result": "pong"}
yield requestor
@@ -96,15 +96,16 @@ def test_list_scopes_api_based_extensions_to_authenticated_tenant(
)
assert account_create_response.status_code == 201
- APIBasedExtensionService.save(
+ # Seed another workspace's extension directly; the list endpoint must not expose it.
+ db_session_with_containers.add(
APIBasedExtension(
tenant_id=foreign_tenant_id,
name="Foreign API",
api_endpoint="https://foreign.example.com/hook",
- api_key="foreign-secret-12345",
- ),
- session=db_session_with_containers,
+ api_key=encrypt_token(foreign_tenant_id, "foreign-secret-12345"),
+ )
)
+ db_session_with_containers.commit()
response = test_client_with_containers.get(
"/console/api/api-based-extension",
diff --git a/api/tests/test_containers_integration_tests/pyrefly.toml b/api/tests/test_containers_integration_tests/pyrefly.toml
index 8d50124640e..6d4f39dce3b 100644
--- a/api/tests/test_containers_integration_tests/pyrefly.toml
+++ b/api/tests/test_containers_integration_tests/pyrefly.toml
@@ -52,7 +52,6 @@ project-excludes = [
"services/test_advanced_prompt_template_service.py",
"services/test_agent_service.py",
"services/test_annotation_service.py",
- "services/test_api_based_extension_service.py",
"services/test_app_dsl_service.py",
"services/test_app_generate_service.py",
"services/test_app_service.py",
diff --git a/api/tests/test_containers_integration_tests/services/test_api_based_extension_service.py b/api/tests/test_containers_integration_tests/services/test_api_based_extension_service.py
deleted file mode 100644
index ed813d62879..00000000000
--- a/api/tests/test_containers_integration_tests/services/test_api_based_extension_service.py
+++ /dev/null
@@ -1,678 +0,0 @@
-from unittest.mock import patch
-
-import pytest
-from faker import Faker
-from sqlalchemy.orm import Session
-
-from models.api_based_extension import APIBasedExtension
-from services.api_based_extension_service import APIBasedExtensionService
-from tests.test_containers_integration_tests.helpers import accounts as account_fixtures
-from tests.test_containers_integration_tests.helpers import generate_valid_password
-
-
-class TestAPIBasedExtensionService:
- """Integration tests for APIBasedExtensionService using testcontainers."""
-
- @pytest.fixture
- def mock_external_service_dependencies(self):
- """Mock setup for external service dependencies."""
- with (
- patch("services.account.login_adapters.SystemFeatureService") as mock_account_feature_service,
- patch("services.api_based_extension_service.APIBasedExtensionRequestor") as mock_requestor,
- ):
- # Mock successful ping response
- mock_requestor_instance = mock_requestor.return_value
- mock_requestor_instance.request.return_value = {"result": "pong"}
-
- yield {
- "account_feature_service": mock_account_feature_service,
- "requestor": mock_requestor,
- "requestor_instance": mock_requestor_instance,
- }
-
- def _create_test_account_and_tenant(self, db_session_with_containers: Session, mock_external_service_dependencies):
- """
- Helper method to create a test account and tenant for testing.
-
- Args:
- db_session_with_containers: Database session from testcontainers infrastructure
- mock_external_service_dependencies: Mock dependencies
-
- Returns:
- tuple: (account, tenant) - Created account and tenant instances
- """
- fake = Faker()
-
- # Setup mocks for account creation
- mock_external_service_dependencies["account_feature_service"].is_registration_allowed.return_value = True
-
- # Create account and tenant
- account = account_fixtures.create_account(
- email=fake.email(),
- name=fake.name(),
- interface_language="en-US",
- password=generate_valid_password(fake),
- session=db_session_with_containers,
- )
- account_fixtures.create_owner_workspace(account, name=fake.company(), session=db_session_with_containers)
- tenant = account.current_tenant
-
- return account, tenant
-
- def test_save_extension_success(self, db_session_with_containers: Session, mock_external_service_dependencies):
- """
- Test successful saving of API-based extension.
- """
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant is not None
- # Setup extension data
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name=fake.company(),
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- )
-
- # Save extension
- saved_extension = APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- # Verify extension was saved correctly
- assert saved_extension.id is not None
- assert saved_extension.tenant_id == tenant.id
- assert saved_extension.name == extension_data.name
- assert saved_extension.api_endpoint == extension_data.api_endpoint
- assert saved_extension.api_key == extension_data.api_key # Should be decrypted when retrieved
- assert saved_extension.created_at is not None
-
- # Verify extension was saved to database
-
- db_session_with_containers.refresh(saved_extension)
- assert saved_extension.id is not None
-
- # Verify ping connection was called
- mock_external_service_dependencies["requestor_instance"].request.assert_called_once()
-
- def test_save_extension_validation_errors(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test validation errors when saving extension with invalid data.
- """
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant is not None
- # Test empty name
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name="",
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- )
-
- with pytest.raises(ValueError, match="name must not be empty"):
- APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- # Test empty api_endpoint
- extension_data.name = fake.company()
- extension_data.api_endpoint = ""
-
- with pytest.raises(ValueError, match="api_endpoint must not be empty"):
- APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- # Test empty api_key
- extension_data.api_endpoint = f"https://{fake.domain_name()}/api"
- extension_data.api_key = ""
-
- with pytest.raises(ValueError, match="api_key must not be empty"):
- APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- def test_get_all_by_tenant_id_success(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test successful retrieval of all extensions by tenant ID.
- """
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
-
- # Create multiple extensions
- extensions = []
- assert tenant is not None
- for i in range(3):
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name=f"Extension {i}: {fake.company()}",
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- )
-
- saved_extension = APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
- extensions.append(saved_extension)
-
- # Get all extensions for tenant
- extension_list = APIBasedExtensionService.get_all_by_tenant_id(tenant.id, session=db_session_with_containers)
-
- # Verify results
- assert len(extension_list) == 3
-
- # Verify all extensions belong to the correct tenant and are ordered by created_at desc
- for i, extension in enumerate(extension_list):
- assert extension.tenant_id == tenant.id
- assert extension.api_key is not None # Should be decrypted
- if i > 0:
- # Verify descending order (newer first)
- assert extension.created_at <= extension_list[i - 1].created_at
-
- def test_get_with_tenant_id_success(self, db_session_with_containers: Session, mock_external_service_dependencies):
- """
- Test successful retrieval of extension by tenant ID and extension ID.
- """
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant is not None
- # Create an extension
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name=fake.company(),
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- )
-
- created_extension = APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- # Get extension by ID
- retrieved_extension = APIBasedExtensionService.get_with_tenant_id(
- tenant.id, created_extension.id, session=db_session_with_containers
- )
-
- # Verify extension was retrieved correctly
- assert retrieved_extension is not None
- assert retrieved_extension.id == created_extension.id
- assert retrieved_extension.tenant_id == tenant.id
- assert retrieved_extension.name == extension_data.name
- assert retrieved_extension.api_endpoint == extension_data.api_endpoint
- assert retrieved_extension.api_key == extension_data.api_key # Should be decrypted
- assert retrieved_extension.created_at is not None
-
- def test_get_with_tenant_id_not_found(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test retrieval of extension when extension is not found.
- """
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- non_existent_extension_id = fake.uuid4()
-
- # Try to get non-existent extension
- with pytest.raises(ValueError, match="API based extension is not found"):
- APIBasedExtensionService.get_with_tenant_id(
- tenant.id, non_existent_extension_id, session=db_session_with_containers
- )
-
- def test_delete_extension_success(self, db_session_with_containers: Session, mock_external_service_dependencies):
- """
- Test successful deletion of extension.
- """
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant is not None
- # Create an extension first
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name=fake.company(),
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- )
-
- created_extension = APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
- extension_id = created_extension.id
-
- # Delete the extension
- APIBasedExtensionService.delete(created_extension, session=db_session_with_containers)
-
- # Verify extension was deleted
-
- deleted_extension = (
- db_session_with_containers.query(APIBasedExtension).where(APIBasedExtension.id == extension_id).first()
- )
- assert deleted_extension is None
-
- def test_save_extension_duplicate_name(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test validation error when saving extension with duplicate name.
- """
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant is not None
- # Create first extension
- extension_data1 = APIBasedExtension(
- tenant_id=tenant.id,
- name="Test Extension",
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- )
-
- APIBasedExtensionService.save(extension_data1, session=db_session_with_containers)
- # Try to create second extension with same name
- extension_data2 = APIBasedExtension(
- tenant_id=tenant.id,
- name="Test Extension", # Same name
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- )
-
- with pytest.raises(ValueError, match="name must be unique, it is already existed"):
- APIBasedExtensionService.save(extension_data2, session=db_session_with_containers)
-
- def test_save_extension_update_existing(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test successful update of existing extension.
- """
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant is not None
- # Create initial extension
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name=fake.company(),
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- )
-
- created_extension = APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- # Save original values for later comparison
- original_name = created_extension.name
- original_endpoint = created_extension.api_endpoint
-
- # Update the extension with guaranteed different values
- new_name = fake.company()
- # Ensure new endpoint is different from original
- new_endpoint = f"https://{fake.domain_name()}/api"
- # If by chance they're the same, generate a new one
- while new_endpoint == original_endpoint:
- new_endpoint = f"https://{fake.domain_name()}/api"
- new_api_key = fake.password(length=20)
-
- created_extension.name = new_name
- created_extension.api_endpoint = new_endpoint
- created_extension.api_key = new_api_key
-
- updated_extension = APIBasedExtensionService.save(created_extension, session=db_session_with_containers)
-
- # Verify extension was updated correctly
- assert updated_extension.id == created_extension.id
- assert updated_extension.tenant_id == tenant.id
- assert updated_extension.name == new_name
- assert updated_extension.api_endpoint == new_endpoint
-
- # Verify original values were changed
- assert updated_extension.name != original_name
- assert updated_extension.api_endpoint != original_endpoint
-
- # Verify ping connection was called for both create and update
- assert mock_external_service_dependencies["requestor_instance"].request.call_count == 2
-
- # Verify the update by retrieving the extension again
- retrieved_extension = APIBasedExtensionService.get_with_tenant_id(
- tenant.id, created_extension.id, session=db_session_with_containers
- )
- assert retrieved_extension.name == new_name
- assert retrieved_extension.api_endpoint == new_endpoint
- assert retrieved_extension.api_key == new_api_key # Should be decrypted when retrieved
-
- def test_save_extension_connection_error(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test connection error when saving extension with invalid endpoint.
- """
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
-
- # Mock connection error
- mock_external_service_dependencies["requestor_instance"].request.side_effect = ValueError(
- "connection error: request timeout"
- )
- assert tenant is not None
- # Setup extension data
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name=fake.company(),
- api_endpoint="https://invalid-endpoint.com/api",
- api_key=fake.password(length=20),
- )
-
- # Try to save extension with connection error
- with pytest.raises(ValueError, match="connection error: request timeout"):
- APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- def test_save_extension_invalid_api_key_length(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test validation error when saving extension with API key that is too short.
- """
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant is not None
- # Setup extension data with short API key
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name=fake.company(),
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key="1234", # Less than 5 characters
- )
-
- # Try to save extension with short API key
- with pytest.raises(ValueError, match="api_key must be at least 5 characters"):
- APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- def test_save_extension_empty_fields(self, db_session_with_containers: Session, mock_external_service_dependencies):
- """
- Test validation errors when saving extension with empty required fields.
- """
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant is not None
- # Test with None values
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name=None, # type: ignore # why str become None here???
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- )
-
- with pytest.raises(ValueError, match="name must not be empty"):
- APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- # Test with None api_endpoint
- extension_data.name = fake.company()
- extension_data.api_endpoint = None
-
- with pytest.raises(ValueError, match="api_endpoint must not be empty"):
- APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- # Test with None api_key
- extension_data.api_endpoint = f"https://{fake.domain_name()}/api"
- extension_data.api_key = None
-
- with pytest.raises(ValueError, match="api_key must not be empty"):
- APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- def test_get_all_by_tenant_id_empty_list(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test retrieval of extensions when no extensions exist for tenant.
- """
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
-
- # Get all extensions for tenant (none exist)
- extension_list = APIBasedExtensionService.get_all_by_tenant_id(tenant.id, session=db_session_with_containers)
-
- # Verify empty list is returned
- assert len(extension_list) == 0
- assert extension_list == []
-
- def test_save_extension_invalid_ping_response(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test validation error when ping response is invalid.
- """
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
-
- # Mock invalid ping response
- mock_external_service_dependencies["requestor_instance"].request.return_value = {"result": "invalid"}
- assert tenant is not None
- # Setup extension data
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name=fake.company(),
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- )
-
- # Try to save extension with invalid ping response
- with pytest.raises(ValueError, match="{'result': 'invalid'}"):
- APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- def test_save_extension_missing_ping_result(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test validation error when ping response is missing result field.
- """
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
-
- # Mock ping response without result field
- mock_external_service_dependencies["requestor_instance"].request.return_value = {"status": "ok"}
- assert tenant is not None
- # Setup extension data
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name=fake.company(),
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- )
-
- # Try to save extension with missing ping result
- with pytest.raises(ValueError, match="{'status': 'ok'}"):
- APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- def test_get_with_tenant_id_wrong_tenant(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test retrieval of extension when tenant ID doesn't match.
- """
- fake = Faker()
- account1, tenant1 = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
-
- # Create second account and tenant
- account2, tenant2 = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant1 is not None
- # Create extension in first tenant
- extension_data = APIBasedExtension(
- tenant_id=tenant1.id,
- name=fake.company(),
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- )
-
- created_extension = APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- # Try to get extension with wrong tenant ID
- with pytest.raises(ValueError, match="API based extension is not found"):
- APIBasedExtensionService.get_with_tenant_id(
- tenant2.id, created_extension.id, session=db_session_with_containers
- )
-
- def test_save_extension_api_key_exactly_four_chars_rejected(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """API key with exactly 4 characters should be rejected (boundary)."""
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant is not None
-
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name=fake.company(),
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key="1234",
- )
-
- with pytest.raises(ValueError, match="api_key must be at least 5 characters"):
- APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- def test_save_extension_api_key_exactly_five_chars_accepted(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """API key with exactly 5 characters should be accepted (boundary)."""
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant is not None
-
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name=fake.company(),
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key="12345",
- )
-
- saved = APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
- assert saved.id is not None
-
- def test_save_extension_requestor_constructor_error(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """Exception raised by requestor constructor is wrapped in ValueError."""
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant is not None
-
- mock_external_service_dependencies["requestor"].side_effect = RuntimeError("bad config")
-
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name=fake.company(),
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- )
-
- with pytest.raises(ValueError, match="connection error: bad config"):
- APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- def test_save_extension_network_exception(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """Network exceptions during ping are wrapped in ValueError."""
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant is not None
-
- mock_external_service_dependencies["requestor_instance"].request.side_effect = ConnectionError(
- "network failure"
- )
-
- extension_data = APIBasedExtension(
- tenant_id=tenant.id,
- name=fake.company(),
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- )
-
- with pytest.raises(ValueError, match="connection error: network failure"):
- APIBasedExtensionService.save(extension_data, session=db_session_with_containers)
-
- def test_save_extension_update_duplicate_name_rejected(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """Updating an existing extension to use another extension's name should fail."""
- fake = Faker()
- account, tenant = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant is not None
-
- ext1 = APIBasedExtensionService.save(
- APIBasedExtension(
- tenant_id=tenant.id,
- name="Extension Alpha",
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- ),
- session=db_session_with_containers,
- )
- ext2 = APIBasedExtensionService.save(
- APIBasedExtension(
- tenant_id=tenant.id,
- name="Extension Beta",
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- ),
- session=db_session_with_containers,
- )
-
- # Try to rename ext2 to ext1's name
- ext2.name = "Extension Alpha"
- with pytest.raises(ValueError, match="name must be unique, it is already existed"):
- APIBasedExtensionService.save(ext2, session=db_session_with_containers)
-
- def test_get_all_returns_empty_for_different_tenant(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """Extensions from one tenant should not be visible to another."""
- fake = Faker()
- _, tenant1 = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- _, tenant2 = self._create_test_account_and_tenant(
- db_session_with_containers, mock_external_service_dependencies
- )
- assert tenant1 is not None
-
- APIBasedExtensionService.save(
- APIBasedExtension(
- tenant_id=tenant1.id,
- name=fake.company(),
- api_endpoint=f"https://{fake.domain_name()}/api",
- api_key=fake.password(length=20),
- ),
- session=db_session_with_containers,
- )
-
- assert tenant2 is not None
- result = APIBasedExtensionService.get_all_by_tenant_id(tenant2.id, session=db_session_with_containers)
- assert result == []
diff --git a/api/tests/unit_tests/controllers/console/test_extension.py b/api/tests/unit_tests/controllers/console/test_extension.py
index e097930ced8..4497c505874 100644
--- a/api/tests/unit_tests/controllers/console/test_extension.py
+++ b/api/tests/unit_tests/controllers/console/test_extension.py
@@ -2,8 +2,10 @@ from __future__ import annotations
import builtins
import uuid
+from collections.abc import Callable, Iterator
from datetime import UTC, datetime
-from unittest.mock import ANY, MagicMock
+from types import SimpleNamespace
+from unittest.mock import MagicMock, patch
import pytest
from flask import Flask
@@ -17,35 +19,48 @@ if not hasattr(builtins, "MethodView"):
builtins.__dict__["MethodView"] = FlaskMethodView
_NEEDS_METHOD_VIEW_CLEANUP = True
+import controllers.console.extension as module
from constants import HIDDEN_VALUE
from controllers.console.extension import (
APIBasedExtensionAPI,
APIBasedExtensionDetailAPI,
+ APIBasedExtensionPayload,
CodeBasedExtensionAPI,
)
from enums import DeploymentEdition
if _NEEDS_METHOD_VIEW_CLEANUP:
del builtins.__dict__["MethodView"]
+from machinery.context import RequestContext
from models.account import Account, AccountStatus, Tenant
-from models.api_based_extension import APIBasedExtension
+from services.api_based_extension_application_service import (
+ APIBasedExtensionInput,
+ APIBasedExtensionNameConflictError,
+ APIBasedExtensionNotFoundError,
+ APIBasedExtensionRecord,
+ APIBasedExtensionUpdate,
+)
+
+
+def unwrap(func: Callable[..., object]) -> Callable[..., object]:
+ while hasattr(func, "__wrapped__"):
+ func = func.__wrapped__
+ return func
-def _make_extension(
+def _record(
*,
name: str = "Sample Extension",
api_endpoint: str = "https://example.com/api",
api_key: str = "super-secret-key",
-) -> APIBasedExtension:
- extension = APIBasedExtension(
- tenant_id="tenant-123",
+) -> APIBasedExtensionRecord:
+ return APIBasedExtensionRecord(
+ id=str(uuid.uuid4()),
name=name,
api_endpoint=api_endpoint,
api_key=api_key,
+ created_at=datetime(2024, 1, 1, 12, 0, tzinfo=UTC),
)
- extension.id = f"{uuid.uuid4()}"
- extension.created_at = datetime.now(tz=UTC)
- return extension
def _masked_api_key(api_key: str) -> str:
@@ -56,7 +71,7 @@ def _masked_api_key(api_key: str) -> str:
@pytest.fixture(autouse=True)
def _mock_console_guards(monkeypatch: pytest.MonkeyPatch) -> Account:
- """Bypass console decorators so handlers can run in isolation."""
+ """Bypass console decorators so the code-based handler can run in isolation."""
from controllers.console import wraps as wraps_module
@@ -82,12 +97,29 @@ def _mock_console_guards(monkeypatch: pytest.MonkeyPatch) -> Account:
@pytest.fixture(autouse=True)
-def _restx_mask_defaults(app: Flask):
+def _restx_mask_defaults(app: Flask) -> None:
app.config.setdefault("RESTX_MASK_HEADER", "X-Fields")
app.config.setdefault("RESTX_MASK_SWAGGER", False)
-def test_code_based_extension_get_returns_service_data(app: Flask, monkeypatch: pytest.MonkeyPatch):
+def request_context() -> RequestContext:
+ return RequestContext(
+ request_id="request-1",
+ trace_id=None,
+ account_id="account-123",
+ active_workspace_id="tenant-123",
+ )
+
+
+def extensions_service() -> Iterator[MagicMock]:
+ service = MagicMock()
+ with patch.object(module, "application_services", return_value=SimpleNamespace(api_based_extensions=service)):
+ yield service
+
+
+def test_code_based_extension_get_returns_service_data(app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:
service_result = [{"entrypoint": "main:agent"}]
service_mock = MagicMock(return_value=service_result)
monkeypatch.setattr(
@@ -113,146 +145,151 @@ def test_code_based_extension_get_rejects_a_missing_module(app: Flask) -> None:
CodeBasedExtensionAPI().get()
-def test_api_based_extension_get_returns_tenant_extensions(app: Flask, monkeypatch: pytest.MonkeyPatch):
- extension = _make_extension(name="Weather API", api_key="abcdefghi123")
- service_mock = MagicMock(return_value=[extension])
- monkeypatch.setattr(
- "controllers.console.extension.APIBasedExtensionService.get_all_by_tenant_id",
- service_mock,
- )
-
- with app.test_request_context("/console/api/api-based-extension", method="GET"):
- response = APIBasedExtensionAPI().get()
-
- assert response[0]["id"] == extension.id
- assert response[0]["name"] == "Weather API"
- assert response[0]["api_endpoint"] == extension.api_endpoint
- assert response[0]["api_key"].startswith(extension.api_key[:3])
- service_mock.assert_called_once_with("tenant-123", session=ANY)
-
-
-def test_api_based_extension_post_creates_extension(app: Flask, monkeypatch: pytest.MonkeyPatch):
- saved_extension = _make_extension(name="Docs API", api_key="encrypted-token-from-save")
- save_mock = MagicMock(return_value=saved_extension)
- monkeypatch.setattr("controllers.console.extension.APIBasedExtensionService.save", save_mock)
-
- payload = {
- "name": "Docs API",
- "api_endpoint": "https://docs.example.com/hook",
- "api_key": "plain-secret",
- }
-
- with app.test_request_context("/console/api/api-based-extension", method="POST", json=payload):
- response, status = APIBasedExtensionAPI().post()
-
- args, _ = save_mock.call_args
- created_extension: APIBasedExtension = args[0]
- assert created_extension.tenant_id == "tenant-123"
- assert created_extension.name == payload["name"]
- assert created_extension.api_endpoint == payload["api_endpoint"]
- assert created_extension.api_key == payload["api_key"]
- assert status == 201
- assert response["name"] == saved_extension.name
- assert response["api_key"] == _masked_api_key(payload["api_key"])
- save_mock.assert_called_once()
-
-
-def test_api_based_extension_detail_get_fetches_extension(app: Flask, monkeypatch: pytest.MonkeyPatch):
- extension = _make_extension(name="Docs API", api_key="abcdefg12345")
- service_mock = MagicMock(return_value=extension)
- monkeypatch.setattr(
- "controllers.console.extension.APIBasedExtensionService.get_with_tenant_id",
- service_mock,
- )
-
- extension_id = uuid.uuid4()
- with app.test_request_context(f"/console/api/api-based-extension/{extension_id}", method="GET"):
- response = APIBasedExtensionDetailAPI().get(extension_id)
-
- assert response["id"] == extension.id
- assert response["name"] == extension.name
- service_mock.assert_called_once_with("tenant-123", str(extension_id), session=ANY)
-
-
-def test_api_based_extension_detail_post_keeps_hidden_api_key(app: Flask, monkeypatch: pytest.MonkeyPatch):
- existing_extension = _make_extension(name="Docs API", api_key="keep-me")
- get_mock = MagicMock(return_value=existing_extension)
- save_mock = MagicMock(return_value=existing_extension)
- monkeypatch.setattr(
- "controllers.console.extension.APIBasedExtensionService.get_with_tenant_id",
- get_mock,
- )
- monkeypatch.setattr("controllers.console.extension.APIBasedExtensionService.save", save_mock)
-
- payload = {
- "name": "Docs API Updated",
- "api_endpoint": "https://docs.example.com/v2",
- "api_key": HIDDEN_VALUE,
- }
-
- extension_id = uuid.uuid4()
- with app.test_request_context(
- f"/console/api/api-based-extension/{extension_id}",
- method="POST",
- json=payload,
- ):
- response = APIBasedExtensionDetailAPI().post(extension_id)
-
- assert existing_extension.name == payload["name"]
- assert existing_extension.api_endpoint == payload["api_endpoint"]
- assert existing_extension.api_key == "keep-me"
- save_mock.assert_called_once_with(existing_extension, session=ANY)
- assert response["name"] == payload["name"]
- assert response["api_key"] == _masked_api_key("keep-me")
-
-
-def test_api_based_extension_detail_post_updates_api_key_when_provided(app: Flask, monkeypatch: pytest.MonkeyPatch):
- existing_extension = _make_extension(name="Docs API", api_key="old-secret")
- get_mock = MagicMock(return_value=existing_extension)
- save_mock = MagicMock(return_value=existing_extension)
- monkeypatch.setattr(
- "controllers.console.extension.APIBasedExtensionService.get_with_tenant_id",
- get_mock,
- )
- monkeypatch.setattr("controllers.console.extension.APIBasedExtensionService.save", save_mock)
-
- payload = {
- "name": "Docs API Updated",
- "api_endpoint": "https://docs.example.com/v2",
- "api_key": "new-secret",
- }
-
- extension_id = uuid.uuid4()
- with app.test_request_context(
- f"/console/api/api-based-extension/{extension_id}",
- method="POST",
- json=payload,
- ):
- response = APIBasedExtensionDetailAPI().post(extension_id)
-
- assert existing_extension.api_key == "new-secret"
- save_mock.assert_called_once_with(existing_extension, session=ANY)
- assert response["name"] == payload["name"]
- assert response["api_key"] == _masked_api_key(payload["api_key"])
-
-
-def test_api_based_extension_detail_delete_removes_extension(app: Flask, monkeypatch: pytest.MonkeyPatch):
- existing_extension = _make_extension()
- get_mock = MagicMock(return_value=existing_extension)
- delete_mock = MagicMock()
- monkeypatch.setattr(
- "controllers.console.extension.APIBasedExtensionService.get_with_tenant_id",
- get_mock,
- )
- monkeypatch.setattr("controllers.console.extension.APIBasedExtensionService.delete", delete_mock)
-
- extension_id = uuid.uuid4()
- with app.test_request_context(
- f"/console/api/api-based-extension/{extension_id}",
- method="DELETE",
- ):
- response, status = APIBasedExtensionDetailAPI().delete(extension_id)
-
- delete_mock.assert_called_once_with(existing_extension, session=ANY)
- assert status == 204
- assert response == ""
+class TestAPIBasedExtensionAPI:
+ def test_get_lists_workspace_extensions_with_masked_keys(
+ self, app: Flask, request_context: RequestContext, extensions_service: MagicMock
+ ) -> None:
+ extension = _record(name="Weather API", api_key="abcdefghi123")
+ extensions_service.list_extensions.return_value = (extension,)
+
+ with app.test_request_context("/console/api/api-based-extension", method="GET"):
+ response = unwrap(APIBasedExtensionAPI().get)(APIBasedExtensionAPI(), request_context)
+
+ extensions_service.list_extensions.assert_called_once_with(request_context)
+ assert response == [
+ {
+ "id": extension.id,
+ "name": "Weather API",
+ "api_endpoint": extension.api_endpoint,
+ "api_key": _masked_api_key("abcdefghi123"),
+ "created_at": int(extension.created_at.timestamp()),
+ }
+ ]
+
+ def test_post_creates_extension_and_masks_the_submitted_key(
+ self, app: Flask, request_context: RequestContext, extensions_service: MagicMock
+ ) -> None:
+ created = _record(name="Docs API", api_endpoint="https://docs.example.com/hook", api_key="plain-secret")
+ extensions_service.create_extension.return_value = created
+ payload = APIBasedExtensionPayload(
+ name="Docs API", api_endpoint="https://docs.example.com/hook", api_key="plain-secret"
+ )
+
+ with app.test_request_context("/console/api/api-based-extension", method="POST"):
+ response, status = unwrap(APIBasedExtensionAPI().post)(APIBasedExtensionAPI(), payload, request_context)
+
+ extensions_service.create_extension.assert_called_once_with(
+ request_context,
+ APIBasedExtensionInput(
+ name="Docs API", api_endpoint="https://docs.example.com/hook", api_key="plain-secret"
+ ),
+ )
+ assert status == 201
+ assert response["id"] == created.id
+ assert response["name"] == "Docs API"
+ assert response["api_key"] == _masked_api_key("plain-secret")
+
+ def test_post_maps_domain_errors_to_legacy_value_error(
+ self, app: Flask, request_context: RequestContext, extensions_service: MagicMock
+ ) -> None:
+ extensions_service.create_extension.side_effect = APIBasedExtensionNameConflictError()
+ payload = APIBasedExtensionPayload(name="Docs API", api_endpoint="https://docs.example.com", api_key="secret")
+
+ with app.test_request_context("/console/api/api-based-extension", method="POST"):
+ with pytest.raises(ValueError, match="name must be unique"):
+ unwrap(APIBasedExtensionAPI().post)(APIBasedExtensionAPI(), payload, request_context)
+
+
+class TestAPIBasedExtensionDetailAPI:
+ def test_get_fetches_extension(
+ self, app: Flask, request_context: RequestContext, extensions_service: MagicMock
+ ) -> None:
+ extension = _record(name="Docs API", api_key="abcdefg12345")
+ extensions_service.get_extension.return_value = extension
+ extension_id = uuid.uuid4()
+
+ with app.test_request_context(f"/console/api/api-based-extension/{extension_id}", method="GET"):
+ response = unwrap(APIBasedExtensionDetailAPI().get)(
+ APIBasedExtensionDetailAPI(), request_context, extension_id
+ )
+
+ extensions_service.get_extension.assert_called_once_with(request_context, str(extension_id))
+ assert response["id"] == extension.id
+ assert response["name"] == "Docs API"
+ assert response["api_key"] == _masked_api_key("abcdefg12345")
+
+ def test_get_maps_missing_extension_to_legacy_value_error(
+ self, app: Flask, request_context: RequestContext, extensions_service: MagicMock
+ ) -> None:
+ extensions_service.get_extension.side_effect = APIBasedExtensionNotFoundError()
+ extension_id = uuid.uuid4()
+
+ with app.test_request_context(f"/console/api/api-based-extension/{extension_id}", method="GET"):
+ with pytest.raises(ValueError, match="not found"):
+ unwrap(APIBasedExtensionDetailAPI().get)(APIBasedExtensionDetailAPI(), request_context, extension_id)
+
+ def test_post_keeps_hidden_api_key(
+ self, app: Flask, request_context: RequestContext, extensions_service: MagicMock
+ ) -> None:
+ updated = _record(name="Docs API Updated", api_endpoint="https://docs.example.com/v2", api_key="keep-me")
+ extensions_service.update_extension.return_value = updated
+ payload = APIBasedExtensionPayload(
+ name="Docs API Updated", api_endpoint="https://docs.example.com/v2", api_key=HIDDEN_VALUE
+ )
+ extension_id = uuid.uuid4()
+
+ with app.test_request_context(f"/console/api/api-based-extension/{extension_id}", method="POST"):
+ response = unwrap(APIBasedExtensionDetailAPI().post)(
+ APIBasedExtensionDetailAPI(), payload, request_context, extension_id
+ )
+
+ extensions_service.update_extension.assert_called_once_with(
+ request_context,
+ str(extension_id),
+ APIBasedExtensionUpdate(name="Docs API Updated", api_endpoint="https://docs.example.com/v2", api_key=None),
+ )
+ assert response["name"] == "Docs API Updated"
+ assert response["api_key"] == _masked_api_key("keep-me")
+
+ def test_post_forwards_a_new_api_key(
+ self, app: Flask, request_context: RequestContext, extensions_service: MagicMock
+ ) -> None:
+ updated = _record(name="Docs API Updated", api_endpoint="https://docs.example.com/v2", api_key="new-secret")
+ extensions_service.update_extension.return_value = updated
+ payload = APIBasedExtensionPayload(
+ name="Docs API Updated", api_endpoint="https://docs.example.com/v2", api_key="new-secret"
+ )
+ extension_id = uuid.uuid4()
+
+ with app.test_request_context(f"/console/api/api-based-extension/{extension_id}", method="POST"):
+ response = unwrap(APIBasedExtensionDetailAPI().post)(
+ APIBasedExtensionDetailAPI(), payload, request_context, extension_id
+ )
+
+ update = extensions_service.update_extension.call_args.args[2]
+ assert update.api_key == "new-secret"
+ assert response["api_key"] == _masked_api_key("new-secret")
+
+ def test_delete_removes_extension(
+ self, app: Flask, request_context: RequestContext, extensions_service: MagicMock
+ ) -> None:
+ extension_id = uuid.uuid4()
+
+ with app.test_request_context(f"/console/api/api-based-extension/{extension_id}", method="DELETE"):
+ response, status = unwrap(APIBasedExtensionDetailAPI().delete)(
+ APIBasedExtensionDetailAPI(), request_context, extension_id
+ )
+
+ extensions_service.delete_extension.assert_called_once_with(request_context, str(extension_id))
+ assert status == 204
+ assert response == ""
+
+ def test_delete_maps_missing_extension_to_legacy_value_error(
+ self, app: Flask, request_context: RequestContext, extensions_service: MagicMock
+ ) -> None:
+ extensions_service.delete_extension.side_effect = APIBasedExtensionNotFoundError()
+ extension_id = uuid.uuid4()
+
+ with app.test_request_context(f"/console/api/api-based-extension/{extension_id}", method="DELETE"):
+ with pytest.raises(ValueError, match="not found"):
+ unwrap(APIBasedExtensionDetailAPI().delete)(APIBasedExtensionDetailAPI(), request_context, extension_id)
diff --git a/api/tests/unit_tests/extensions/test_ext_application_services.py b/api/tests/unit_tests/extensions/test_ext_application_services.py
index 7bddfe84111..f2d8182c471 100644
--- a/api/tests/unit_tests/extensions/test_ext_application_services.py
+++ b/api/tests/unit_tests/extensions/test_ext_application_services.py
@@ -88,6 +88,8 @@ from services.account.oauth_adapters import (
WorkspaceProvisioningOAuthGateway,
)
from services.account_avatar_file_gateway import SQLAlchemyAccountAvatarFileGateway
+from services.api_based_extension_adapters import APIBasedExtensionPingProbe, WorkspaceTokenCipher
+from services.api_based_extension_application_service import APIBasedExtensionApplicationService
from services.app.api_key_service import AppApiKeyService
from services.app.creators_platform_gateway import CreatorsPlatformGateway
from services.app_generate_service import AppGenerateService
@@ -354,6 +356,21 @@ def test_build_application_services_wires_tag_boundary(
assert isinstance(services.tags, TagApplicationService)
+def test_build_application_services_wires_api_based_extension_boundary(
+ sqlite_session_factory: sessionmaker[Session],
+) -> None:
+ services = ext_application_services.build_application_services(
+ database_client=sqlite_session_factory,
+ deployment_edition=DeploymentEdition.COMMUNITY,
+ initialization_password="",
+ redis=_redis(),
+ )
+
+ assert isinstance(services.api_based_extensions, APIBasedExtensionApplicationService)
+ assert isinstance(services.api_based_extensions._secrets, WorkspaceTokenCipher)
+ assert isinstance(services.api_based_extensions._probe, APIBasedExtensionPingProbe)
+
+
def test_build_application_services_reuses_file_service(
sqlite_session_factory: sessionmaker[Session],
) -> None:
diff --git a/api/tests/unit_tests/repositories/test_api_based_extension_repository.py b/api/tests/unit_tests/repositories/test_api_based_extension_repository.py
new file mode 100644
index 00000000000..c08ea7bc69a
--- /dev/null
+++ b/api/tests/unit_tests/repositories/test_api_based_extension_repository.py
@@ -0,0 +1,139 @@
+from datetime import datetime
+
+import pytest
+from sqlalchemy import select
+from sqlalchemy.orm import Session, sessionmaker
+
+from models.api_based_extension import APIBasedExtension
+from repositories.api_based_extension_repository import APIBasedExtensionRepository
+from services.api_based_extension_application_service import (
+ APIBasedExtensionInput,
+ APIBasedExtensionNotFoundError,
+)
+
+
+def _extension(
+ extension_id: str,
+ *,
+ workspace_id: str,
+ name: str,
+ created_at: datetime,
+ api_key: str = "cipher-text",
+) -> APIBasedExtension:
+ extension = APIBasedExtension(
+ tenant_id=workspace_id,
+ name=name,
+ api_endpoint=f"https://{name}.example.com",
+ api_key=api_key,
+ )
+ extension.id = extension_id
+ extension.created_at = created_at
+ return extension
+
+
+def test_list_extensions_is_workspace_scoped_and_newest_first(
+ sqlite_session_factory: sessionmaker[Session],
+) -> None:
+ with sqlite_session_factory.begin() as session:
+ session.add_all(
+ [
+ _extension("ext-1", workspace_id="workspace-1", name="older", created_at=datetime(2024, 1, 1, 12, 0)),
+ _extension("ext-2", workspace_id="workspace-1", name="newer", created_at=datetime(2024, 1, 2, 12, 0)),
+ _extension("ext-3", workspace_id="workspace-2", name="other", created_at=datetime(2024, 1, 3, 12, 0)),
+ ]
+ )
+
+ repository = APIBasedExtensionRepository(session_factory=sqlite_session_factory)
+
+ records = repository.list_extensions("workspace-1")
+
+ assert [record.id for record in records] == ["ext-2", "ext-1"]
+ assert records[0].api_key == "cipher-text"
+ assert records[0].api_endpoint == "https://newer.example.com"
+
+
+def test_find_extension_does_not_cross_workspaces(sqlite_session_factory: sessionmaker[Session]) -> None:
+ with sqlite_session_factory.begin() as session:
+ session.add(_extension("ext-1", workspace_id="workspace-1", name="mine", created_at=datetime(2024, 1, 1)))
+
+ repository = APIBasedExtensionRepository(session_factory=sqlite_session_factory)
+
+ found = repository.find_extension("workspace-1", "ext-1")
+ assert found is not None
+ assert found.name == "mine"
+ assert repository.find_extension("workspace-2", "ext-1") is None
+ assert repository.find_extension("workspace-1", "missing") is None
+
+
+def test_name_exists_honours_workspace_and_exclusion(sqlite_session_factory: sessionmaker[Session]) -> None:
+ with sqlite_session_factory.begin() as session:
+ session.add(_extension("ext-1", workspace_id="workspace-1", name="taken", created_at=datetime(2024, 1, 1)))
+
+ repository = APIBasedExtensionRepository(session_factory=sqlite_session_factory)
+
+ assert repository.name_exists("workspace-1", "taken") is True
+ assert repository.name_exists("workspace-1", "taken", exclude_id="ext-1") is False
+ assert repository.name_exists("workspace-1", "taken", exclude_id="ext-9") is True
+ assert repository.name_exists("workspace-2", "taken") is False
+ assert repository.name_exists("workspace-1", "free") is False
+
+
+def test_create_extension_persists_the_given_ciphertext(sqlite_session_factory: sessionmaker[Session]) -> None:
+ repository = APIBasedExtensionRepository(session_factory=sqlite_session_factory)
+
+ record = repository.create_extension(
+ "workspace-1",
+ APIBasedExtensionInput(name="Docs", api_endpoint="https://docs.example.com", api_key="cipher-text"),
+ )
+
+ assert record.name == "Docs"
+ assert record.api_key == "cipher-text"
+ assert isinstance(record.created_at, datetime)
+ with sqlite_session_factory() as session:
+ stored = session.scalar(select(APIBasedExtension).where(APIBasedExtension.id == record.id))
+ assert stored is not None
+ assert stored.tenant_id == "workspace-1"
+ assert stored.api_key == "cipher-text"
+
+
+def test_update_extension_overwrites_fields_within_the_workspace(
+ sqlite_session_factory: sessionmaker[Session],
+) -> None:
+ with sqlite_session_factory.begin() as session:
+ session.add(_extension("ext-1", workspace_id="workspace-1", name="old", created_at=datetime(2024, 1, 1)))
+
+ repository = APIBasedExtensionRepository(session_factory=sqlite_session_factory)
+
+ record = repository.update_extension(
+ "workspace-1",
+ "ext-1",
+ APIBasedExtensionInput(name="new", api_endpoint="https://new.example.com", api_key="new-cipher"),
+ )
+
+ assert (record.name, record.api_endpoint, record.api_key) == ("new", "https://new.example.com", "new-cipher")
+ with sqlite_session_factory() as session:
+ stored = session.get(APIBasedExtension, "ext-1")
+ assert stored is not None
+ assert stored.name == "new"
+
+ with pytest.raises(APIBasedExtensionNotFoundError):
+ repository.update_extension(
+ "workspace-2",
+ "ext-1",
+ APIBasedExtensionInput(name="x", api_endpoint="https://x.example.com", api_key="x"),
+ )
+
+
+def test_delete_extension_requires_a_scoped_match(sqlite_session_factory: sessionmaker[Session]) -> None:
+ with sqlite_session_factory.begin() as session:
+ session.add(_extension("ext-1", workspace_id="workspace-1", name="doomed", created_at=datetime(2024, 1, 1)))
+
+ repository = APIBasedExtensionRepository(session_factory=sqlite_session_factory)
+
+ with pytest.raises(APIBasedExtensionNotFoundError):
+ repository.delete_extension("workspace-2", "ext-1")
+
+ repository.delete_extension("workspace-1", "ext-1")
+
+ with sqlite_session_factory() as session:
+ assert session.get(APIBasedExtension, "ext-1") is None
diff --git a/api/tests/unit_tests/services/test_api_based_extension_adapters.py b/api/tests/unit_tests/services/test_api_based_extension_adapters.py
new file mode 100644
index 00000000000..3c2a2c3499b
--- /dev/null
+++ b/api/tests/unit_tests/services/test_api_based_extension_adapters.py
@@ -0,0 +1,60 @@
+from unittest.mock import MagicMock
+
+import pytest
+
+import services.api_based_extension_adapters as module
+from models.api_based_extension import APIBasedExtensionPoint
+from services.api_based_extension_adapters import APIBasedExtensionPingProbe, WorkspaceTokenCipher
+from services.api_based_extension_application_service import APIBasedExtensionConnectionError
+
+
+def test_workspace_token_cipher_delegates_to_the_platform_encrypter(monkeypatch: pytest.MonkeyPatch) -> None:
+ encrypt_token = MagicMock(return_value="cipher")
+ decrypt_token = MagicMock(return_value="plain")
+ monkeypatch.setattr(module, "encrypt_token", encrypt_token)
+ monkeypatch.setattr(module, "decrypt_token", decrypt_token)
+ cipher = WorkspaceTokenCipher()
+
+ assert cipher.encrypt("workspace-1", "plain") == "cipher"
+ assert cipher.decrypt("workspace-1", "cipher") == "plain"
+ encrypt_token.assert_called_once_with("workspace-1", "plain")
+ decrypt_token.assert_called_once_with("workspace-1", "cipher")
+
+
+def test_ping_probe_accepts_pong(monkeypatch: pytest.MonkeyPatch) -> None:
+ requestor = MagicMock()
+ requestor.return_value.request.return_value = {"result": "pong"}
+ monkeypatch.setattr(module, "APIBasedExtensionRequestor", requestor)
+
+ APIBasedExtensionPingProbe().ping("https://ext.example.com", "secret")
+
+ requestor.assert_called_once_with("https://ext.example.com", "secret")
+ requestor.return_value.request.assert_called_once_with(point=APIBasedExtensionPoint.PING, params={})
+
+
+def test_ping_probe_rejects_unexpected_replies(monkeypatch: pytest.MonkeyPatch) -> None:
+ requestor = MagicMock()
+ requestor.return_value.request.return_value = {"result": "nope"}
+ monkeypatch.setattr(module, "APIBasedExtensionRequestor", requestor)
+
+ with pytest.raises(APIBasedExtensionConnectionError, match="connection error: .*nope"):
+ APIBasedExtensionPingProbe().ping("https://ext.example.com", "secret")
+
+
[email protected]("reply", [[], None, "pong", 0])
+def test_ping_probe_rejects_non_object_replies(monkeypatch: pytest.MonkeyPatch, reply: object) -> None:
+ requestor = MagicMock()
+ requestor.return_value.request.return_value = reply
+ monkeypatch.setattr(module, "APIBasedExtensionRequestor", requestor)
+
+ with pytest.raises(APIBasedExtensionConnectionError, match="connection error: "):
+ APIBasedExtensionPingProbe().ping("https://ext.example.com", "secret")
+
+
+def test_ping_probe_wraps_transport_failures(monkeypatch: pytest.MonkeyPatch) -> None:
+ requestor = MagicMock()
+ requestor.return_value.request.side_effect = ValueError("request timeout")
+ monkeypatch.setattr(module, "APIBasedExtensionRequestor", requestor)
+
+ with pytest.raises(APIBasedExtensionConnectionError, match="connection error: request timeout"):
+ APIBasedExtensionPingProbe().ping("https://ext.example.com", "secret")
diff --git a/api/tests/unit_tests/services/test_api_based_extension_application_service.py b/api/tests/unit_tests/services/test_api_based_extension_application_service.py
new file mode 100644
index 00000000000..724ae8e2e88
--- /dev/null
+++ b/api/tests/unit_tests/services/test_api_based_extension_application_service.py
@@ -0,0 +1,246 @@
+from datetime import datetime
+from unittest.mock import MagicMock
+
+import pytest
+
+from machinery.context import RequestContext
+from services.api_based_extension_application_service import (
+ APIBasedExtensionApplicationService,
+ APIBasedExtensionConnectionError,
+ APIBasedExtensionInput,
+ APIBasedExtensionInvalidError,
+ APIBasedExtensionNameConflictError,
+ APIBasedExtensionNotFoundError,
+ APIBasedExtensionRecord,
+ APIBasedExtensionUpdate,
+)
+
+
+def _record(extension_id: str = "ext-1", *, api_key: str = "enc:secret") -> APIBasedExtensionRecord:
+ return APIBasedExtensionRecord(
+ id=extension_id,
+ name="Docs",
+ api_endpoint="https://docs.example.com",
+ api_key=api_key,
+ created_at=datetime(2024, 1, 1, 12, 0),
+ )
+
+
+class _FakeCipher:
+ """Reversible stand-in cipher that records which workspace each call was scoped to."""
+
+ def __init__(self) -> None:
+ self.workspaces: list[str] = []
+
+ def encrypt(self, workspace_id: str, value: str) -> str:
+ self.workspaces.append(workspace_id)
+ return f"enc:{value}"
+
+ def decrypt(self, workspace_id: str, value: str) -> str:
+ self.workspaces.append(workspace_id)
+ assert value.startswith("enc:"), value
+ return value.removeprefix("enc:")
+
+
+def context() -> RequestContext:
+ return RequestContext("request-1", None, "account-1", "workspace-1")
+
+
+def store() -> MagicMock:
+ store = MagicMock()
+ store.name_exists.return_value = False
+ return store
+
+
+def probe() -> MagicMock:
+ return MagicMock()
+
+
+def cipher() -> _FakeCipher:
+ return _FakeCipher()
+
+
+def service(store: MagicMock, probe: MagicMock, cipher: _FakeCipher) -> APIBasedExtensionApplicationService:
+ return APIBasedExtensionApplicationService(extensions=store, secrets=cipher, probe=probe)
+
+
+def test_list_extensions_decrypts_stored_keys(
+ service: APIBasedExtensionApplicationService, store: MagicMock, cipher: _FakeCipher, context: RequestContext
+) -> None:
+ store.list_extensions.return_value = [_record("ext-1", api_key="enc:one"), _record("ext-2", api_key="enc:two")]
+
+ records = service.list_extensions(context)
+
+ store.list_extensions.assert_called_once_with("workspace-1")
+ assert [record.api_key for record in records] == ["one", "two"]
+ assert cipher.workspaces == ["workspace-1", "workspace-1"]
+
+
+def test_get_extension_decrypts_or_raises(
+ service: APIBasedExtensionApplicationService, store: MagicMock, context: RequestContext
+) -> None:
+ store.find_extension.return_value = _record(api_key="enc:secret")
+
+ assert service.get_extension(context, "ext-1").api_key == "secret"
+ store.find_extension.assert_called_once_with("workspace-1", "ext-1")
+
+ store.find_extension.return_value = None
+ with pytest.raises(APIBasedExtensionNotFoundError):
+ service.get_extension(context, "missing")
+
+
+def test_create_extension_validates_pings_and_stores_ciphertext(
+ service: APIBasedExtensionApplicationService, store: MagicMock, probe: MagicMock, context: RequestContext
+) -> None:
+ store.create_extension.return_value = _record(api_key="enc:plain-secret")
+ extension = APIBasedExtensionInput(name="Docs", api_endpoint="https://docs.example.com", api_key="plain-secret")
+
+ created = service.create_extension(context, extension)
+
+ store.name_exists.assert_called_once_with("workspace-1", "Docs", exclude_id=None)
+ probe.ping.assert_called_once_with("https://docs.example.com", "plain-secret")
+ store.create_extension.assert_called_once_with("workspace-1", extension._replace(api_key="enc:plain-secret"))
+ assert created.api_key == "plain-secret"
+
+
+ ("extension", "message"),
+ [
+ (APIBasedExtensionInput(name="", api_endpoint="https://x.example.com", api_key="secret"), "name must not"),
+ (APIBasedExtensionInput(name="Docs", api_endpoint="", api_key="secret"), "api_endpoint must not"),
+ (APIBasedExtensionInput(name="Docs", api_endpoint="https://x.example.com", api_key=""), "api_key must not"),
+ (APIBasedExtensionInput(name="Docs", api_endpoint="https://x.example.com", api_key="abcd"), "at least 5"),
+ ],
+)
+def test_create_extension_rejects_invalid_fields_before_pinging(
+ service: APIBasedExtensionApplicationService,
+ store: MagicMock,
+ probe: MagicMock,
+ context: RequestContext,
+ extension: APIBasedExtensionInput,
+ message: str,
+) -> None:
+ with pytest.raises(APIBasedExtensionInvalidError, match=message):
+ service.create_extension(context, extension)
+
+ probe.ping.assert_not_called()
+ store.create_extension.assert_not_called()
+
+
[email protected]("api_key", ["abcde", "12345", "a-b-c"])
+def test_create_extension_accepts_a_five_character_key(
+ service: APIBasedExtensionApplicationService,
+ store: MagicMock,
+ probe: MagicMock,
+ context: RequestContext,
+ api_key: str,
+) -> None:
+ store.create_extension.return_value = _record(api_key=f"enc:{api_key}")
+ extension = APIBasedExtensionInput(name="Docs", api_endpoint="https://docs.example.com", api_key=api_key)
+
+ created = service.create_extension(context, extension)
+
+ probe.ping.assert_called_once_with("https://docs.example.com", api_key)
+ store.create_extension.assert_called_once_with("workspace-1", extension._replace(api_key=f"enc:{api_key}"))
+ assert created.api_key == api_key
+
+
+def test_update_extension_accepts_a_five_character_key(
+ service: APIBasedExtensionApplicationService, store: MagicMock, probe: MagicMock, context: RequestContext
+) -> None:
+ store.find_extension.return_value = _record(api_key="enc:old-secret")
+ store.update_extension.return_value = _record(api_key="enc:abcde")
+
+ updated = service.update_extension(
+ context, "ext-1", APIBasedExtensionUpdate(name="Docs", api_endpoint="https://docs.example.com", api_key="abcde")
+ )
+
+ probe.ping.assert_called_once_with("https://docs.example.com", "abcde")
+ assert store.update_extension.call_args.args[2].api_key == "enc:abcde"
+ assert updated.api_key == "abcde"
+
+
+def test_create_extension_rejects_duplicate_names_and_unreachable_endpoints(
+ service: APIBasedExtensionApplicationService, store: MagicMock, probe: MagicMock, context: RequestContext
+) -> None:
+ extension = APIBasedExtensionInput(name="Docs", api_endpoint="https://docs.example.com", api_key="secret")
+
+ store.name_exists.return_value = True
+ with pytest.raises(APIBasedExtensionNameConflictError):
+ service.create_extension(context, extension)
+
+ store.name_exists.return_value = False
+ probe.ping.side_effect = APIBasedExtensionConnectionError("request timeout")
+ with pytest.raises(APIBasedExtensionConnectionError, match="connection error: request timeout"):
+ service.create_extension(context, extension)
+
+ store.create_extension.assert_not_called()
+
+
+def test_update_extension_keeps_the_stored_key_when_none_is_given(
+ service: APIBasedExtensionApplicationService, store: MagicMock, probe: MagicMock, context: RequestContext
+) -> None:
+ store.find_extension.return_value = _record(api_key="enc:keep-me")
+ store.update_extension.return_value = _record(api_key="enc:keep-me")
+
+ updated = service.update_extension(
+ context,
+ "ext-1",
+ APIBasedExtensionUpdate(name="Docs v2", api_endpoint="https://docs.example.com/v2", api_key=None),
+ )
+
+ store.name_exists.assert_called_once_with("workspace-1", "Docs v2", exclude_id="ext-1")
+ probe.ping.assert_called_once_with("https://docs.example.com/v2", "keep-me")
+ store.update_extension.assert_called_once_with(
+ "workspace-1",
+ "ext-1",
+ APIBasedExtensionInput(name="Docs v2", api_endpoint="https://docs.example.com/v2", api_key="enc:keep-me"),
+ )
+ assert updated.api_key == "keep-me"
+
+
+def test_update_extension_encrypts_a_new_key(
+ service: APIBasedExtensionApplicationService, store: MagicMock, context: RequestContext
+) -> None:
+ store.find_extension.return_value = _record(api_key="enc:old")
+ store.update_extension.return_value = _record(api_key="enc:new-secret")
+
+ updated = service.update_extension(
+ context,
+ "ext-1",
+ APIBasedExtensionUpdate(name="Docs", api_endpoint="https://docs.example.com", api_key="new-secret"),
+ )
+
+ assert store.update_extension.call_args.args[2].api_key == "enc:new-secret"
+ assert updated.api_key == "new-secret"
+
+
+def test_update_requires_an_existing_extension(
+ service: APIBasedExtensionApplicationService, store: MagicMock, context: RequestContext
+) -> None:
+ store.find_extension.return_value = None
+
+ with pytest.raises(APIBasedExtensionNotFoundError):
+ service.update_extension(
+ context, "missing", APIBasedExtensionUpdate(name="x", api_endpoint="https://x.example.com", api_key="y")
+ )
+
+ store.update_extension.assert_not_called()
+
+
+def test_delete_extension_delegates_existence_to_the_store(
+ service: APIBasedExtensionApplicationService, store: MagicMock, context: RequestContext
+) -> None:
+ service.delete_extension(context, "ext-1")
+
+ store.delete_extension.assert_called_once_with("workspace-1", "ext-1")
+ store.find_extension.assert_not_called()
+
+ store.delete_extension.side_effect = APIBasedExtensionNotFoundError()
+ with pytest.raises(APIBasedExtensionNotFoundError):
+ service.delete_extension(context, "missing")