Files
dify/api/tests/unit_tests/controllers/openapi/test__audit.py
T
583615e412 feat: support personal access token (#45540)
Co-authored-by: twwu <twwu@dify.ai>
Co-authored-by: Eldar Dadashov <52511672+eldardada@users.noreply.github.com>
Co-authored-by: Byron.wang <byron@dify.ai>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai>
2026-11-11 03:00:24 +00:00

56 lines
1.6 KiB
Python

import logging
from typing import Protocol, cast
import pytest
from controllers.openapi._audit import EVENT_APP_RUN_OPENAPI, emit_app_run, personal_audit_targets
class _AuditLogRecord(Protocol):
audit: bool
event: str
app_id: str
tenant_id: str
caller_kind: str
mode: str
surface: str
def test_event_constant() -> None:
assert EVENT_APP_RUN_OPENAPI == "app.run.openapi"
def test_personal_audit_targets_exclude_unreviewed_fields_even_with_uuid_values() -> None:
value = "00000000-0000-4000-8000-000000000001"
assert personal_audit_targets(
{
"app_id": value,
"workspace_id": value,
"run_id": value,
"future_secret_id": value,
"form_token": value,
"secret": value,
"task_id": "not-a-uuid",
"import_id": None,
}
) == {"app_id": value, "workspace_id": value, "run_id": value}
def test_emit_app_run_logs_with_audit_extra(caplog: pytest.LogCaptureFixture) -> None:
with caplog.at_level(logging.INFO, logger="controllers.openapi._audit"):
emit_app_run(
app_id="app1",
tenant_id="t1",
caller_kind="account",
mode="chat",
surface="apps",
)
record = cast(_AuditLogRecord, next(r for r in caplog.records if r.message and "app.run.openapi" in r.message))
assert record.audit is True
assert record.event == EVENT_APP_RUN_OPENAPI
assert record.app_id == "app1"
assert record.tenant_id == "t1"
assert record.caller_kind == "account"
assert record.mode == "chat"
assert record.surface == "apps"