Download coworker/tests/test_agent_studio_bootstrap.py from EnergyVenom/patchwise-agent-studio: direct link, hf CLI and curl.
- Browser
- Download file 2.28 kB
-
https://huggingface.co/spaces/EnergyVenom/patchwise-agent-studio/resolve/main/coworker/tests/test_agent_studio_bootstrap.py
- Command line
-
hf download hf://spaces/EnergyVenom/patchwise-agent-studio/coworker/tests/test_agent_studio_bootstrap.py
-
curl -L -o test_agent_studio_bootstrap.py https://huggingface.co/spaces/EnergyVenom/patchwise-agent-studio/resolve/main/coworker/tests/test_agent_studio_bootstrap.py
2.28 kB
| """The Fusion tool's OAuth client from the server's secrets: it exists after every start with the same id and secret | |
| (a server without saved state, such as a Space before Supabase), an admin's revoke stands, bad entries are skipped.""" | |
| from __future__ import annotations | |
| import json | |
| import os | |
| import unittest | |
| from unittest import mock | |
| from coworker.platform.agent_studio import AgentStudio | |
| from coworker.tests.test_ai_scenarios import fusion_app | |
| CID, SECRET = "pwc_00112233445566aa", "pws_" + "s" * 40 | |
| class TestBootstrapClients(unittest.TestCase): | |
| def setUpClass(cls): | |
| cls.app, cls.c, cls.svc, cls.h, _ = fusion_app() | |
| def boot(self, items): | |
| with mock.patch.dict(os.environ, {"FPT_SECRET_AGENT_STUDIO_BOOTSTRAP_CLIENTS": json.dumps(items)}): | |
| return AgentStudio(self.svc).ensure_from_env() | |
| def token(self, secret=SECRET): | |
| return self.c.post("/oauth/token", data={"grant_type": "client_credentials", "client_id": CID, "client_secret": secret, | |
| "scope": "patchwise.invoke"}) | |
| def test_a_the_client_exists_with_its_id_and_secret_and_again_after_a_restart(self): | |
| self.assertEqual(self.boot([{"client_id": CID, "client_secret": SECRET, "name": "Fusion dev61"}]), [CID]) | |
| self.assertEqual(self.token().status_code, 200) | |
| self.assertEqual(self.boot([{"client_id": CID, "client_secret": SECRET}]), [CID]) # idempotent | |
| self.assertEqual(len([c for c in AgentStudio(self.svc).clients() if c["client_id"] == CID]), 1) | |
| self.assertEqual(self.token("wrong-secret-" + "x" * 20).status_code, 401) | |
| def test_b_bad_entries_are_skipped(self): | |
| self.assertEqual(self.boot([{"client_id": "evil", "client_secret": SECRET}, {"client_id": "pwc_00112233445566bb", "client_secret": "short"}]), []) | |
| def test_z_an_admins_revoke_stands(self): | |
| self.boot([{"client_id": CID, "client_secret": SECRET}]) | |
| self.c.delete(f"/api/agent-studio/clients/{CID}", headers=self.h["admin"]) | |
| self.assertEqual(self.boot([{"client_id": CID, "client_secret": SECRET}]), []) | |
| self.assertEqual(self.token().status_code, 401) | |
| if __name__ == "__main__": | |
| unittest.main() | |