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.

test: implement API key auth fixture protocols (#43388)

Asuka Minatod0cafb31be14
diff --git a/api/tests/unit_tests/services/auth/test_data_source_api_key_auth_service.py b/api/tests/unit_tests/services/auth/test_data_source_api_key_auth_service.py
index 4ab25292f30..43aff336fe9 100644
--- a/api/tests/unit_tests/services/auth/test_data_source_api_key_auth_service.py
+++ b/api/tests/unit_tests/services/auth/test_data_source_api_key_auth_service.py
@@ -1,10 +1,14 @@
"""Unit tests for the data-source API-key auth application service and ports."""
from datetime import datetime
-from unittest.mock import MagicMock
from machinery.context import RequestContext
-from services.data_source.auth.api_key_service import DataSourceApiKeyAuthService
+from services.data_source.auth.api_key_service import (
+ ApiKeyAuthCredentialEncryptor,
+ ApiKeyAuthCredentialValidator,
+ DataSourceApiKeyAuthBindingRepository,
+ DataSourceApiKeyAuthService,
+)
from services.data_source.entities.api_key_auth import (
DataSourceApiKeyAuthBindingCreate,
DataSourceApiKeyAuthBindingRecord,
@@ -12,6 +16,51 @@ from services.data_source.entities.api_key_auth import (
)
+class RecordingBindings(DataSourceApiKeyAuthBindingRepository):
+ def __init__(self, events: list[str]) -> None:
+ self.events = events
+ self.records: list[DataSourceApiKeyAuthBindingRecord] = []
+ self.list_calls: list[str] = []
+ self.create_calls: list[tuple[str, str, str, DataSourceApiKeyAuthCredentials]] = []
+ self.delete_calls: list[tuple[str, str]] = []
+
+ def list_enabled(self, workspace_id: str) -> list[DataSourceApiKeyAuthBindingRecord]:
+ self.list_calls.append(workspace_id)
+ return self.records
+
+ def create(
+ self, workspace_id: str, category: str, provider: str, credentials: DataSourceApiKeyAuthCredentials
+ ) -> None:
+ self.events.append("create")
+ self.create_calls.append((workspace_id, category, provider, credentials))
+
+ def delete(self, workspace_id: str, binding_id: str) -> None:
+ self.delete_calls.append((workspace_id, binding_id))
+
+
+class RecordingValidator(ApiKeyAuthCredentialValidator):
+ def __init__(self, events: list[str]) -> None:
+ self.events = events
+ self.accepted = True
+ self.calls: list[tuple[str, DataSourceApiKeyAuthCredentials]] = []
+
+ def validate(self, provider: str, credentials: DataSourceApiKeyAuthCredentials) -> bool:
+ self.events.append("validate")
+ self.calls.append((provider, credentials))
+ return self.accepted
+
+
+class RecordingEncryptor(ApiKeyAuthCredentialEncryptor):
+ def __init__(self, events: list[str]) -> None:
+ self.events = events
+ self.calls: list[tuple[str, str]] = []
+
+ def encrypt(self, workspace_id: str, token: str) -> str:
+ self.events.append("encrypt")
+ self.calls.append((workspace_id, token))
+ return "encrypted-secret"
+
+
def _context() -> RequestContext:
return RequestContext(
request_id="request-1",
@@ -33,10 +82,11 @@ def _command() -> DataSourceApiKeyAuthBindingCreate:
)
-def _service() -> tuple[DataSourceApiKeyAuthService, MagicMock, MagicMock, MagicMock]:
- bindings = MagicMock()
- validator = MagicMock()
- encryptor = MagicMock()
+def _service() -> tuple[DataSourceApiKeyAuthService, RecordingBindings, RecordingValidator, RecordingEncryptor]:
+ events: list[str] = []
+ bindings = RecordingBindings(events)
+ validator = RecordingValidator(events)
+ encryptor = RecordingEncryptor(events)
return (
DataSourceApiKeyAuthService(bindings=bindings, validator=validator, encryptor=encryptor),
bindings,
@@ -55,43 +105,45 @@ def test_list_bindings_uses_active_workspace() -> None:
created_at=datetime(2026, 1, 1),
updated_at=datetime(2026, 1, 2),
)
- bindings.list_enabled.return_value = [record]
+ bindings.records = [record]
assert service.list_bindings(_context()) == (record,)
- bindings.list_enabled.assert_called_once_with("workspace-1")
+ assert bindings.list_calls == ["workspace-1"]
def test_create_binding_validates_before_encrypting_and_persisting() -> None:
service, bindings, validator, encryptor = _service()
command = _command()
- validator.validate.return_value = True
- encryptor.encrypt.return_value = "encrypted-secret"
service.create_binding(_context(), command)
- validator.validate.assert_called_once_with("firecrawl", command.credentials)
- encryptor.encrypt.assert_called_once_with("workspace-1", "secret")
- bindings.create.assert_called_once_with(
- "workspace-1",
- "search",
- "firecrawl",
- DataSourceApiKeyAuthCredentials(
- auth_type="bearer",
- api_key="encrypted-secret",
- options={"base_url": "https://example.com"},
- ),
- )
+ assert validator.calls == [("firecrawl", command.credentials)]
+ assert encryptor.calls == [("workspace-1", "secret")]
+ assert bindings.events == ["validate", "encrypt", "create"]
+ assert bindings.create_calls == [
+ (
+ "workspace-1",
+ "search",
+ "firecrawl",
+ DataSourceApiKeyAuthCredentials(
+ auth_type="bearer",
+ api_key="encrypted-secret",
+ options={"base_url": "https://example.com"},
+ ),
+ )
+ ]
assert command.credentials.api_key == "secret"
def test_create_binding_does_not_persist_rejected_credentials() -> None:
service, bindings, validator, encryptor = _service()
- validator.validate.return_value = False
+ validator.accepted = False
service.create_binding(_context(), _command())
- encryptor.encrypt.assert_not_called()
- bindings.create.assert_not_called()
+ assert bindings.events == ["validate"]
+ assert encryptor.calls == []
+ assert bindings.create_calls == []
def test_delete_binding_scopes_to_active_workspace() -> None:
@@ -99,4 +151,4 @@ def test_delete_binding_scopes_to_active_workspace() -> None:
service.delete_binding(_context(), "binding-1")
- bindings.delete.assert_called_once_with("workspace-1", "binding-1")
+ assert bindings.delete_calls == [("workspace-1", "binding-1")]