patchwise-agent-studio / coworker /tests /test_agent_studio_bootstrap.py
EnergyVenom's picture
Patchwise Agent Studio: Oracle AI Agent Studio cycles
0ca03eb verified
Raw History Blame Contribute Delete
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):
@classmethod
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()