test_legacy_feishu_credentials.py 1.4 KB

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