|
@@ -0,0 +1,139 @@
|
|
|
|
|
+import contextlib
|
|
|
|
|
+import io
|
|
|
|
|
+import json
|
|
|
|
|
+import os
|
|
|
|
|
+import sys
|
|
|
|
|
+import tempfile
|
|
|
|
|
+import types
|
|
|
|
|
+import unittest
|
|
|
|
|
+import zipfile
|
|
|
|
|
+from pathlib import Path
|
|
|
|
|
+from unittest.mock import patch
|
|
|
|
|
+
|
|
|
|
|
+ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
+sys.path.insert(0, str(ROOT / "scripts"))
|
|
|
|
|
+import audit
|
|
|
|
|
+import feishu_credentials as cred
|
|
|
|
|
+with patch.dict(sys.modules, {"requests": types.ModuleType("requests")}):
|
|
|
|
|
+ import fetch_table
|
|
|
|
|
+ import rule_check
|
|
|
|
|
+ import sync_package
|
|
|
|
|
+
|
|
|
|
|
+NO_ENV = {"FEISHU_APP_ID": "", "FEISHU_APP_SECRET": ""}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class CredentialSourceTests(unittest.TestCase):
|
|
|
|
|
+ def setUp(self):
|
|
|
|
|
+ self.tmp = tempfile.TemporaryDirectory()
|
|
|
|
|
+ self.addCleanup(self.tmp.cleanup)
|
|
|
|
|
+ self.home = os.path.join(self.tmp.name, "home.json")
|
|
|
|
|
+ self.package = os.path.join(self.tmp.name, "package.json")
|
|
|
|
|
+ for target, value in ((cred, "HOME_FILE"), (cred, "PACKAGE_FILE")):
|
|
|
|
|
+ patcher = patch.object(target, value, self.home if value == "HOME_FILE" else self.package)
|
|
|
|
|
+ patcher.start()
|
|
|
|
|
+ self.addCleanup(patcher.stop)
|
|
|
|
|
+ patcher = patch.dict(os.environ, NO_ENV)
|
|
|
|
|
+ patcher.start()
|
|
|
|
|
+ self.addCleanup(patcher.stop)
|
|
|
|
|
+
|
|
|
|
|
+ def write(self, path, data, encoding="utf-8"):
|
|
|
|
|
+ with open(path, "w", encoding=encoding) as f:
|
|
|
|
|
+ f.write(data if isinstance(data, str) else json.dumps(data))
|
|
|
|
|
+
|
|
|
|
|
+ def test_environment_wins_over_files(self):
|
|
|
|
|
+ self.write(self.home, {"app_id": "home", "app_secret": "home-secret"})
|
|
|
|
|
+ self.write(self.package, {"app_id": "pkg", "app_secret": "pkg-secret"})
|
|
|
|
|
+ with patch.dict(os.environ, {"FEISHU_APP_ID": "env", "FEISHU_APP_SECRET": "env-secret"}):
|
|
|
|
|
+ self.assertEqual(cred.load(), ("env", "env-secret"))
|
|
|
|
|
+
|
|
|
|
|
+ def test_home_file_wins_over_package_file(self):
|
|
|
|
|
+ self.write(self.home, {"app_id": "home", "app_secret": "home-secret"})
|
|
|
|
|
+ self.write(self.package, {"app_id": "pkg", "app_secret": "pkg-secret"})
|
|
|
|
|
+ self.assertEqual(cred.load(), ("home", "home-secret"))
|
|
|
|
|
+
|
|
|
|
|
+ def test_package_file_is_used_when_nothing_else_is_configured(self):
|
|
|
|
|
+ self.write(self.package, {"app_id": "pkg", "app_secret": "pkg-secret"})
|
|
|
|
|
+ self.assertEqual(cred.load(), ("pkg", "pkg-secret"))
|
|
|
|
|
+
|
|
|
|
|
+ def test_package_file_with_bom_is_accepted(self):
|
|
|
|
|
+ self.write(self.package, {"app_id": "pkg", "app_secret": "pkg-secret"}, encoding="utf-8-sig")
|
|
|
|
|
+ self.assertEqual(cred.load(), ("pkg", "pkg-secret"))
|
|
|
|
|
+
|
|
|
|
|
+ def test_broken_or_incomplete_file_falls_through(self):
|
|
|
|
|
+ self.write(self.home, "{not json")
|
|
|
|
|
+ self.assertIsNone(cred.load())
|
|
|
|
|
+ self.write(self.home, {"app_id": "home"})
|
|
|
|
|
+ self.write(self.package, {"app_id": "pkg", "app_secret": "pkg-secret"})
|
|
|
|
|
+ self.assertEqual(cred.load(), ("pkg", "pkg-secret"))
|
|
|
|
|
+
|
|
|
|
|
+ def test_every_script_reads_the_package_file(self):
|
|
|
|
|
+ self.write(self.package, {"app_id": "pkg", "app_secret": "pkg-secret"})
|
|
|
|
|
+ for module in (rule_check, fetch_table, sync_package):
|
|
|
|
|
+ with self.subTest(module=module.__name__):
|
|
|
|
|
+ self.assertEqual(module.load_credentials(), ("pkg", "pkg-secret"))
|
|
|
|
|
+ sent = {}
|
|
|
|
|
+
|
|
|
|
|
+ def post(url, json=None, timeout=None):
|
|
|
|
|
+ sent.update(json)
|
|
|
|
|
+ return types.SimpleNamespace(json=lambda: {"code": 0, "tenant_access_token": "t"})
|
|
|
|
|
+
|
|
|
|
|
+ with patch.object(audit, "requests", types.SimpleNamespace(post=post)):
|
|
|
|
|
+ self.assertEqual(audit.feishu_token(), "t")
|
|
|
|
|
+ self.assertEqual(sent, {"app_id": "pkg", "app_secret": "pkg-secret"})
|
|
|
|
|
+
|
|
|
|
|
+ def test_missing_credentials_stop_every_script_without_echoing_values(self):
|
|
|
|
|
+ self.write(self.home, {"app_id": "leak-me", "app_secret": ""})
|
|
|
|
|
+ for call in (rule_check.load_credentials, fetch_table.load_credentials,
|
|
|
|
|
+ sync_package.load_credentials):
|
|
|
|
|
+ err = io.StringIO()
|
|
|
|
|
+ with self.subTest(call=call.__module__), contextlib.redirect_stderr(err), \
|
|
|
|
|
+ self.assertRaises(SystemExit):
|
|
|
|
|
+ call()
|
|
|
|
|
+ self.assertNotIn("leak-me", err.getvalue())
|
|
|
|
|
+ with patch.object(audit, "requests", types.SimpleNamespace()), \
|
|
|
|
|
+ self.assertRaises(SystemExit) as raised:
|
|
|
|
|
+ audit.feishu_token()
|
|
|
|
|
+ self.assertNotIn("leak-me", str(raised.exception))
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class PackageZipTests(unittest.TestCase):
|
|
|
|
|
+ def setUp(self):
|
|
|
|
|
+ self.tmp = tempfile.TemporaryDirectory()
|
|
|
|
|
+ self.addCleanup(self.tmp.cleanup)
|
|
|
|
|
+ self.skill = os.path.join(self.tmp.name, "sku-audit")
|
|
|
|
|
+ os.makedirs(os.path.join(self.skill, "scripts"))
|
|
|
|
|
+ os.makedirs(os.path.join(self.skill, "references"))
|
|
|
|
|
+ for rel, text in (("scripts/audit.py", "# audit"),
|
|
|
|
|
+ ("references/credentials.json", "{}"),
|
|
|
|
|
+ ("credentials.json", '{"app_id": "pkg", "app_secret": "pkg-secret"}')):
|
|
|
|
|
+ with open(os.path.join(self.skill, rel), "w", encoding="utf-8") as f:
|
|
|
|
|
+ f.write(text)
|
|
|
|
|
+ self.out = os.path.join(self.tmp.name, "dist")
|
|
|
|
|
+
|
|
|
|
|
+ def names(self, path):
|
|
|
|
|
+ with zipfile.ZipFile(path) as z:
|
|
|
|
|
+ return set(z.namelist())
|
|
|
|
|
+
|
|
|
|
|
+ def test_default_package_leaves_credentials_out(self):
|
|
|
|
|
+ path = sync_package.make_zip(self.skill, self.out, "9.9")
|
|
|
|
|
+ names = self.names(path)
|
|
|
|
|
+ self.assertNotIn("sku-audit/credentials.json", names)
|
|
|
|
|
+ self.assertIn("sku-audit/scripts/audit.py", names)
|
|
|
|
|
+ self.assertIn("sku-audit/references/credentials.json", names)
|
|
|
|
|
+ self.assertNotIn("含凭证", os.path.basename(path))
|
|
|
|
|
+
|
|
|
|
|
+ def test_hosted_package_carries_credentials_and_is_named_apart(self):
|
|
|
|
|
+ path = sync_package.make_zip(self.skill, self.out, "9.9", with_credentials=True)
|
|
|
|
|
+ self.assertIn("sku-audit/credentials.json", self.names(path))
|
|
|
|
|
+ self.assertIn("含凭证", os.path.basename(path))
|
|
|
|
|
+
|
|
|
|
|
+ def test_hosted_package_requires_a_usable_credentials_file(self):
|
|
|
|
|
+ with open(os.path.join(self.skill, "credentials.json"), "w", encoding="utf-8") as f:
|
|
|
|
|
+ f.write('{"app_id": "pkg"}')
|
|
|
|
|
+ with contextlib.redirect_stderr(io.StringIO()), self.assertRaises(SystemExit):
|
|
|
|
|
+ sync_package.make_zip(self.skill, self.out, "9.9", with_credentials=True)
|
|
|
|
|
+ self.assertFalse(os.path.isdir(self.out) and os.listdir(self.out))
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
|
+ unittest.main()
|