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.

perf(api): reuse auth matrix routes and batch test data setup (#43128)

Stephen Zhou3813c4d55273
diff --git a/api/tests/unit_tests/controllers/openapi/test_auth_matrix.py b/api/tests/unit_tests/controllers/openapi/test_auth_matrix.py
index 5e3b75e580c..c3c9370c654 100644
--- a/api/tests/unit_tests/controllers/openapi/test_auth_matrix.py
+++ b/api/tests/unit_tests/controllers/openapi/test_auth_matrix.py
@@ -835,14 +835,20 @@ def token_rows() -> dict[str, ResolvedRow]:
return {}
-def matrix_app(monkeypatch: pytest.MonkeyPatch) -> Iterator[Flask]:
- """The openapi blueprint on the real factory app with a login manager and the admission probe."""
[email protected](scope="module")
+def _matrix_app() -> Flask:
+ """Register the matrix's unchanging routes once; each case gets a fresh client."""
app = create_flask_app_with_configs()
app.config["TESTING"] = True
app.secret_key = "openapi-auth-matrix"
LoginManager(app)
app.register_blueprint(openapi_bp)
+ return app
+
+
+def matrix_app(_matrix_app: Flask, monkeypatch: pytest.MonkeyPatch) -> Iterator[Flask]:
+ """Keep rate-limit overrides and the admission probe scoped to each case."""
monkeypatch.setattr(
rate_limit_module,
@@ -856,7 +862,7 @@ def matrix_app(monkeypatch: pytest.MonkeyPatch) -> Iterator[Flask]:
user_logged_in.connect(_admit_on_mount)
try:
- yield app
+ yield _matrix_app
finally:
user_logged_in.disconnect(_admit_on_mount)
@@ -895,29 +901,25 @@ def world(sqlite_session_factory: sessionmaker[Session], token_rows: dict[str, R
other_workspace = Tenant(name="other workspace")
other_workspace.id = built.other_workspace_id
- with sqlite_session_factory() as session:
- session.add_all(
- [
- workspace,
- other_workspace,
- account(built.member_account_id, "[email protected]"),
- account(built.low_role_account_id, "[email protected]"),
- account(built.outsider_account_id, "[email protected]"),
- application(built.app_id, enable_api=True),
- application(built.disabled_app_id, enable_api=False),
- TenantAccountJoin(
- tenant_id=built.workspace_id,
- account_id=built.member_account_id,
- role=TenantAccountRole.OWNER,
- ),
- TenantAccountJoin(
- tenant_id=built.workspace_id,
- account_id=built.low_role_account_id,
- role=TenantAccountRole.NORMAL,
- ),
- ]
- )
- session.commit()
+ rows: list[object] = [
+ workspace,
+ other_workspace,
+ account(built.member_account_id, "[email protected]"),
+ account(built.low_role_account_id, "[email protected]"),
+ account(built.outsider_account_id, "[email protected]"),
+ application(built.app_id, enable_api=True),
+ application(built.disabled_app_id, enable_api=False),
+ TenantAccountJoin(
+ tenant_id=built.workspace_id,
+ account_id=built.member_account_id,
+ role=TenantAccountRole.OWNER,
+ ),
+ TenantAccountJoin(
+ tenant_id=built.workspace_id,
+ account_id=built.low_role_account_id,
+ role=TenantAccountRole.NORMAL,
+ ),
+ ]
def mint(bearer: Bearer, prefix: str, *, account_id: str | None, email: str) -> None:
"""Register a bearer with the fake resolver *and* persist the session row it
@@ -945,9 +947,7 @@ def world(sqlite_session_factory: sessionmaker[Session], token_rows: dict[str, R
expires_at=datetime.now(UTC) + timedelta(days=365),
)
row.id = str(token_id)
- with sqlite_session_factory() as session:
- session.add(row)
- session.commit()
+ rows.append(row)
built.tokens[bearer] = raw
built.session_ids[bearer] = str(token_id)
@@ -970,6 +970,8 @@ def world(sqlite_session_factory: sessionmaker[Session], token_rows: dict[str, R
)
mint(Bearer.EXTERNAL, TokenType.OAUTH_EXTERNAL_SSO.prefix, account_id=None, email="[email protected]")
+ with sqlite_session_factory.begin() as session:
+ session.add_all(rows)
return built