test_legacy_feishu_credentials.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from __future__ import annotations
  2. import ast
  3. from pathlib import Path
  4. import pytest
  5. _FEISHU_FILES = (
  6. "chat.py",
  7. "chat_test.py",
  8. "feishu_client.py",
  9. )
  10. _CREDENTIAL_NAMES = {"FEISHU_APP_ID", "FEISHU_APP_SECRET"}
  11. @pytest.mark.parametrize("filename", _FEISHU_FILES)
  12. def test_legacy_feishu_credentials_have_no_nonempty_fallback(filename: str) -> None:
  13. path = (
  14. Path(__file__).parents[1] / "agent" / "tools" / "builtin" / "feishu" / filename
  15. )
  16. tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
  17. for node in ast.walk(tree):
  18. if not isinstance(node, ast.Call) or not isinstance(node.func, ast.Attribute):
  19. continue
  20. if node.func.attr != "getenv" or not node.args:
  21. continue
  22. name = node.args[0]
  23. if not isinstance(name, ast.Constant) or name.value not in _CREDENTIAL_NAMES:
  24. continue
  25. if len(node.args) > 1:
  26. fallback = node.args[1]
  27. assert isinstance(fallback, ast.Constant)
  28. assert fallback.value in {None, ""}
  29. for node in ast.walk(tree):
  30. if not isinstance(node, ast.Assign) or not isinstance(node.value, ast.Constant):
  31. continue
  32. for target in node.targets:
  33. if not isinstance(target, ast.Subscript):
  34. continue
  35. key = target.slice
  36. if isinstance(key, ast.Constant) and key.value in _CREDENTIAL_NAMES:
  37. assert node.value.value in {None, ""}