// Routes: POST /agent/models/{endpoints,endpoint/call,endpoint/operations,publisher} // Without a credential every one is 503 google_not_configured and sends // nothing; with one, bad input is 422 before any network traffic; gated // actions are dry runs unless confirm:true; GET is 405. #include #include "support/GoogleRig.hpp" using namespace molo::example::test; namespace { struct Route { const char* path; const char* valid; const char* bad; }; const Route kRoutes[] = { {"/agent/models/endpoints", "{\"action\":\"list_beta\"}", "{\"action\":\"patch\",\"endpoint\":\"7/7\",\"body\":{},\"updateMask\":\"displayName\"}"}, {"/agent/models/endpoint/call", "{\"kind\":\"compute_tokens\",\"endpoint\":\"7\",\"contents\":[{\"role\":\"user\",\"parts\":[{\"text\":\"x\"}]}]}", "{\"kind\":\"compute_tokens\",\"endpoint\":\"7\",\"contents\":[]}"}, {"/agent/models/endpoint/operations", "{\"action\":\"list\",\"endpoint\":\"7\"}", "{\"action\":\"get\",\"endpoint\":\"7\"}"}, {"/agent/models/publisher", "{\"action\":\"beta_get\",\"model\":\"gemini-2.5-flash\"}", "{\"action\":\"beta_get\",\"model\":\"a/b\"}"}, }; // More rejected bodies, one per kind of rule. const Route kMore[] = { {"/agent/models/endpoints", "", "{\"action\":\"explode\"}"}, {"/agent/models/endpoints", "", "{\"action\":\"create\"}"}, {"/agent/models/endpoints", "", "{\"action\":\"create\",\"body\":{\"notAField\":1}}"}, {"/agent/models/endpoints", "", "{\"action\":\"patch\",\"endpoint\":\"7\",\"body\":{},\"updateMask\":\"bad mask!\"}"}, {"/agent/models/endpoints", "", "{\"action\":\"get_iam\",\"endpoint\":\"7\",\"policyVersion\":9}"}, {"/agent/models/endpoints", "", "{\"action\":\"test_iam\",\"endpoint\":\"7\",\"permissions\":[]}"}, {"/agent/models/endpoints", "", "{\"action\":\"delete\",\"endpoint\":\"7\",\"confirm\":\"yes\"}"}, {"/agent/models/endpoint/call", "", "{\"kind\":\"openapi\",\"openapiKind\":\"images\",\"body\":{}}"}, {"/agent/models/endpoint/call", "", "{\"kind\":\"direct_raw_predict\",\"endpoint\":\"7\",\"methodName\":\"bad method\",\"input\":\"AAAA\"}"}, {"/agent/models/endpoint/call", "", "{\"kind\":\"invoke\",\"endpoint\":\"7\",\"invokeId\":\"../x\",\"body\":{}}"}, {"/agent/models/endpoint/operations", "", "{\"action\":\"wait\",\"endpoint\":\"7\",\"operation\":\"9\",\"timeoutSeconds\":9999}"}, {"/agent/models/publisher", "", "{\"action\":\"responses_get\",\"publisher\":\"a/b\",\"responseId\":\"r1\"}"}, {"/agent/models/publisher", "", "{\"action\":\"beta_post\",\"model\":\"m\",\"callId\":\"beta.generate\",\"body\":{}}"}, {"/agent/models/publisher", "", "{\"action\":\"stream_predict\",\"model\":\"m\",\"inputs\":[]}"}, {"/agent/models/publisher", "", "{\"action\":\"beta_generate\",\"model\":\"m\",\"prompt\":\"\"}"}, }; // Gated actions: a dry run without confirm. const Route kGated[] = { {"/agent/models/endpoints", "{\"action\":\"delete\",\"endpoint\":\"7\"}", ""}, {"/agent/models/endpoints", "{\"action\":\"deploy\",\"endpoint\":\"7\",\"deployedModel\":{\"model\":\"m\"}}", ""}, {"/agent/models/endpoints", "{\"action\":\"undeploy\",\"endpoint\":\"7\",\"deployedModelId\":\"3\"}", ""}, {"/agent/models/endpoints", "{\"action\":\"mutate\",\"endpoint\":\"7\",\"deployedModel\":{},\"updateMask\":\"x\"}", ""}, {"/agent/models/endpoints", "{\"action\":\"set_iam\",\"endpoint\":\"7\",\"policy\":{}}", ""}, {"/agent/models/endpoint/call", "{\"kind\":\"response_delete\",\"endpoint\":\"7\",\"responseId\":\"r1\"}", ""}, {"/agent/models/endpoint/operations", "{\"action\":\"delete\",\"endpoint\":\"7\",\"operation\":\"9\"}", ""}, {"/agent/models/publisher", "{\"action\":\"export\",\"model\":\"gemma3\",\"destination\":\"gs://b/x\"}", ""}, {"/agent/models/publisher", "{\"action\":\"set_config\",\"model\":\"gemma3\",\"publisherModelConfig\":{},\"updateMask\":\"x\"}", ""}, {"/agent/models/publisher", "{\"action\":\"enable\",\"model\":\"gemma3\"}", ""}, {"/agent/models/publisher", "{\"action\":\"responses_delete\",\"responseId\":\"r1\"}", ""}, }; } // namespace int main() { setenv("GOOGLE_CLOUD_PROJECT", "demo-proj", 1); { GoogleRig none(false); Fixture f(none.client); for (const Route& r : kRoutes) { auto res = f.client->post(r.path, r.valid, "application/json"); check(res.status == 503 && contains(res.body, "google_not_configured"), std::string(r.path) + ": 503 google_not_configured without a credential"); check(f.client->get(r.path).status == 405, std::string(r.path) + ": GET is 405"); } for (const Route& r : kGated) { auto res = f.client->post(r.path, r.valid, "application/json"); check(res.status == 503, std::string(r.path) + ": gated action is 503 without a credential"); } check(none.mock->send_count() == 0, "side effect: nothing sent without a credential"); } { GoogleRig rig; Fixture f(rig.client); for (const Route& r : kRoutes) { auto res = f.client->post(r.path, r.bad, "application/json"); check(res.status == 422 && contains(res.body, "validation_error"), std::string(r.path) + ": bad input is 422 (got " + std::to_string(res.status) + ")"); check(!contains(res.body, kTestKey), std::string(r.path) + ": credential never in a response"); } for (const Route& r : kMore) { auto res = f.client->post(r.path, r.bad, "application/json"); check(res.status == 422, std::string(r.path) + ": rejected " + r.bad + " (got " + std::to_string(res.status) + ")"); } for (const Route& r : kGated) { auto res = f.client->post(r.path, r.valid, "application/json"); check(res.status == 200 && contains(res.body, "\"dry_run\":true"), std::string(r.path) + ": " + r.valid + " is a dry run (got " + std::to_string(res.status) + ")"); } std::string huge = "{\"action\":\"create\",\"body\":{\"displayName\":\"" + std::string(300 * 1024, 'x') + "\"}}"; check(f.client->post("/agent/models/endpoints", huge, "application/json").status == 413, "oversize body is 413"); check(f.client->post("/agent/models/endpoints", "not json", "application/json").status == 422, "non JSON body is 422"); check(rig.mock->send_count() == 0, "side effect: rejected input and dry runs never reached Google"); } return finish("agent/models/admin"); }