Files

102 lines
5.6 KiB
C++

// Routes: every POST under /agent/armor. Without a credential a valid request
// answers 503 google_not_configured; bad input answers 422 before any
// traffic; GET answers 405. Google replies are not scripted anywhere.
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
namespace {
const char* kJson = "application/json";
std::string big(std::size_t n) { return std::string(n, 'a'); }
} // namespace
int main() {
setenv("GOOGLE_CLOUD_PROJECT", "demo-project", 1);
unsetenv("GOOGLE_CLOUD_LOCATION");
struct Route {
std::string path;
std::string valid;
};
const std::vector<Route> routes = {
{"/agent/armor/sanitize/prompt", "{\"template_id\":\"t1\",\"text\":\"hello\"}"},
{"/agent/armor/sanitize/response", "{\"template_id\":\"t1\",\"text\":\"hello\",\"prompt\":\"q\"}"},
{"/agent/armor/guard", "{\"template_id\":\"t1\",\"text\":\"hello\"}"},
{"/agent/armor/templates/list", "{}"},
{"/agent/armor/templates/get", "{\"template_id\":\"t1\"}"},
{"/agent/armor/templates/create",
"{\"template_id\":\"t1\",\"pi\":\"HIGH\",\"rai\":\"HATE_SPEECH:HIGH,DANGEROUS\",\"malicious_uri\":\"true\"}"},
{"/agent/armor/templates/delete", "{\"template_id\":\"t1\",\"confirm\":\"delete\"}"},
{"/agent/armor/floor/get", "{}"},
{"/agent/armor/findings", "{\"scope\":\"organizations/1\",\"severity\":\"HIGH\"}"},
{"/agent/armor/dashboard", "{\"scope\":\"organizations/1\"}"},
};
{
// No credential: valid input is a loud 503, nothing leaves the process.
GoogleRig rig(false);
Fixture f(rig.client);
for (const auto& r : routes) {
auto res = f.client->post(r.path, r.valid, kJson);
check(res.status == 503 && contains(res.body, "google_not_configured"),
"503 google_not_configured: " + r.path);
}
check(rig.mock->send_count() == 0, "no upstream traffic without a credential");
}
{
// Bad input: 422 before any traffic, even with a credential.
GoogleRig rig;
Fixture f(rig.client);
auto expect422 = [&](const std::string& path, const std::string& body, const std::string& what) {
auto res = f.client->post(path, body, kJson);
check(res.status == 422 && contains(res.body, "validation_error"), "422 " + what);
};
expect422("/agent/armor/sanitize/prompt", "{\"text\":\"hello\"}", "prompt without template_id");
expect422("/agent/armor/sanitize/prompt", "{\"template_id\":\"../x\",\"text\":\"hello\"}", "prompt with a path-like template_id");
expect422("/agent/armor/sanitize/prompt", "{\"template_id\":\"t1\"}", "prompt without text");
expect422("/agent/armor/sanitize/response", "{\"template_id\":\"t1\"}", "response without text");
expect422("/agent/armor/guard", "{\"template_id\":\"t1\"}", "guard without text");
// Limit case: text over the 100000 character ceiling.
expect422("/agent/armor/sanitize/prompt",
"{\"template_id\":\"t1\",\"text\":\"" + big(100001) + "\"}", "prompt over the character cap");
expect422("/agent/armor/templates/get", "{}", "get without template_id");
expect422("/agent/armor/templates/list", "{\"page_size\":\"101\"}", "list over the page cap");
expect422("/agent/armor/templates/list", "{\"page_size\":\"x\"}", "list with a non-numeric page size");
expect422("/agent/armor/templates/create", "{\"template_id\":\"t1\",\"rai\":\"NOPE\"}", "unknown rai type");
expect422("/agent/armor/templates/create", "{\"template_id\":\"t1\",\"pi\":\"MAYBE\"}", "unknown confidence");
expect422("/agent/armor/templates/create", "{\"template_id\":\"t1\",\"enforcement\":\"MAYBE\"}", "unknown enforcement");
expect422("/agent/armor/templates/delete", "{\"template_id\":\"t1\"}", "delete without confirm");
expect422("/agent/armor/templates/delete", "{\"template_id\":\"t1\",\"confirm\":\"yes\"}", "delete with a wrong confirm");
expect422("/agent/armor/floor/get", "{\"scope\":\"buckets/1\"}", "floor with a bad scope");
expect422("/agent/armor/findings", "{\"scope\":\"buckets/1\"}", "findings with a bad scope");
expect422("/agent/armor/findings", "{\"scope\":\"organizations/1\",\"severity\":\"SEVERE\"}", "findings with a bad severity");
expect422("/agent/armor/findings", "{\"scope\":\"organizations/1\",\"page_size\":\"1001\"}", "findings over the page cap");
expect422("/agent/armor/findings", "{\"scope\":\"organizations/1\",\"max_pages\":\"11\"}", "findings over the page limit");
expect422("/agent/armor/findings", "{\"scope\":\"organizations/1\",\"max_pages\":\"0\"}", "findings with zero pages");
expect422("/agent/armor/findings", "{\"scope\":\"organizations/1\",\"order_by\":\"color\"}", "findings with a bad order_by");
expect422("/agent/armor/findings", "{\"scope\":\"organizations/1\",\"category\":\"a\\\"b\"}", "findings with a quote in a category");
expect422("/agent/armor/dashboard", "{\"scope\":\"organizations/1\",\"severity\":\"SEVERE\"}", "dashboard with a bad severity");
check(rig.mock->send_count() == 0, "no upstream traffic for rejected input");
}
{
// Missing project: loud 422, not a malformed URL.
GoogleRig rig;
Fixture f(rig.client);
unsetenv("GOOGLE_CLOUD_PROJECT");
auto res = f.client->post("/agent/armor/templates/get", "{\"template_id\":\"t1\"}", kJson);
check(res.status == 422 && contains(res.body, "GOOGLE_CLOUD_PROJECT"), "422 names the missing project");
check(rig.mock->send_count() == 0, "no upstream traffic without a project");
setenv("GOOGLE_CLOUD_PROJECT", "demo-project", 1);
}
{
// GET on a POST-only route is 405.
Fixture f;
for (const auto& r : routes)
check(f.client->get(r.path).status == 405, "GET is 405: " + r.path);
}
return finish("agent/armor/routes");
}