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(console): dep-inject the activation payload with @model_validate (#43056)

Denis Ermakov1ff3d3db6885
diff --git a/api/controllers/console/auth/activate.py b/api/controllers/console/auth/activate.py
index 685342d5a67..adf88afc1da 100644
--- a/api/controllers/console/auth/activate.py
+++ b/api/controllers/console/auth/activate.py
@@ -115,14 +115,14 @@ class ActivateApi(Resource):
console_ns.models[ActivationResponse.__name__],
)
@console_ns.response(400, "Already activated or invalid token")
- def post(self):
+ @model_validate(ActivatePayload)
+ def post(self, args: ActivatePayload):
"""Accept an invitation without letting an existing session act for another account.
Token-only activation remains available for legacy clients. When the request already
carries a console session, that session must belong to the account encoded in the
invitation before the token is consumed or tenant membership is changed.
"""
- args = ActivatePayload.model_validate(console_ns.payload or {})
authenticated_account_id: str | None = None
if extract_access_token(request) is not None:
authenticated_account_id = current_account_with_tenant().account.id
diff --git a/api/tests/unit_tests/controllers/console/auth/test_account_activation.py b/api/tests/unit_tests/controllers/console/auth/test_account_activation.py
index f3ca84a309f..e909ebbea2e 100644
--- a/api/tests/unit_tests/controllers/console/auth/test_account_activation.py
+++ b/api/tests/unit_tests/controllers/console/auth/test_account_activation.py
@@ -1,6 +1,5 @@
"""Transport-boundary tests for account invitation activation."""
-from inspect import unwrap
from types import SimpleNamespace
from unittest.mock import Mock, patch
@@ -150,7 +149,7 @@ class TestActivateApi:
return_value=SimpleNamespace(account=SimpleNamespace(id="account-123")),
),
):
- response = unwrap(ActivateApi.post)(ActivateApi())
+ response = ActivateApi().post()
assert response == {"result": "success"}
activation_service.activate.assert_called_once_with(
@@ -181,7 +180,7 @@ class TestActivateApi:
patch("controllers.console.auth.activate.extract_access_token", return_value=None),
patch("controllers.console.auth.activate.current_account_with_tenant") as resolve_account,
):
- response = unwrap(ActivateApi.post)(ActivateApi())
+ response = ActivateApi().post()
assert response == {"result": "success"}
activation_service.activate.assert_called_once_with(
@@ -217,4 +216,20 @@ class TestActivateApi:
patch("controllers.console.auth.activate.extract_access_token", return_value=None),
pytest.raises(http_error),
):
- unwrap(ActivateApi.post)(ActivateApi())
+ ActivateApi().post()
+
+ def test_rejects_request_without_token(self, app: Flask, activation_service: Mock) -> None:
+ """`token` is required, so a tokenless payload must not resolve the session or reach the service."""
+ with (
+ app.test_request_context("/activate", method="POST", json={"workspace_id": "workspace-123"}),
+ patch(
+ "controllers.console.auth.activate.application_services",
+ return_value=_services(activation_service),
+ ),
+ patch("controllers.console.auth.activate.extract_access_token") as extract_token,
+ pytest.raises(UnprocessableEntity),
+ ):
+ ActivateApi().post()
+
+ extract_token.assert_not_called()
+ activation_service.activate.assert_not_called()