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.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
from __future__ import annotations
import pytest
from dify_app import DifyApp
from extensions import ext_set_secretkey
from tests.helpers.unit.config_override import apply_config_overrides
class InMemoryStorage:
def __init__(self, files: dict[str, bytes] | None = None) -> None:
self.files = files or {}
self.saved_files: list[tuple[str, bytes]] = []
def load_once(self, filename: str) -> bytes:
try:
return self.files[filename]
except KeyError as caught_error:
raise FileNotFoundError(filename) from caught_error
def save(self, filename: str, data: bytes) -> None:
self.files[filename] = data
self.saved_files.append((filename, data))
def test_init_app_uses_configured_secret_key(monkeypatch: pytest.MonkeyPatch) -> None:
secret_key = "configured-secret-key"
storage = InMemoryStorage()
apply_config_overrides(monkeypatch, SECRET_KEY=secret_key)
monkeypatch.setattr("configs.secret_key.storage", storage)
app = DifyApp(__name__)
app.config["SECRET_KEY"] = secret_key
ext_set_secretkey.init_app(app)
assert app.secret_key == secret_key
assert app.config["SECRET_KEY"] == secret_key
assert storage.saved_files == []
def test_init_app_generates_and_persists_secret_key_when_missing(
monkeypatch: pytest.MonkeyPatch,
) -> None:
storage = InMemoryStorage()
apply_config_overrides(monkeypatch, SECRET_KEY="")
monkeypatch.setattr("configs.secret_key.storage", storage)
app = DifyApp(__name__)
app.config["SECRET_KEY"] = ""
ext_set_secretkey.init_app(app)
persisted_key = storage.files[".dify_secret_key"].decode("utf-8").strip()
assert persisted_key
assert storage.saved_files == [(".dify_secret_key", f"{persisted_key}\n".encode())]
assert persisted_key == ext_set_secretkey.dify_config.SECRET_KEY
assert persisted_key == app.config["SECRET_KEY"]
assert persisted_key == app.secret_key
def test_init_app_reuses_persisted_secret_key_when_missing(
monkeypatch: pytest.MonkeyPatch,
) -> None:
persisted_key = "persisted-secret-key"
storage = InMemoryStorage({".dify_secret_key": f"{persisted_key}\n".encode()})
apply_config_overrides(monkeypatch, SECRET_KEY="")
monkeypatch.setattr("configs.secret_key.storage", storage)
app = DifyApp(__name__)
app.config["SECRET_KEY"] = ""
ext_set_secretkey.init_app(app)
assert persisted_key == ext_set_secretkey.dify_config.SECRET_KEY
assert persisted_key == app.config["SECRET_KEY"]
assert persisted_key == app.secret_key
assert storage.saved_files == []