Merge branch 'worktree-agent-a6c7d57da30a90b4f'
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
// Route: POST /google/enterprise/answer
|
||||
#include "google/enterprise/cases.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
const std::string sc =
|
||||
"projects/p1/locations/global/collections/default_collection/dataStores/d1/servingConfigs/default_search";
|
||||
check_enterprise_post_route("/google/enterprise/answer",
|
||||
"{\"serving_config\":\"" + sc + "\",\"query\":\"what is covered\"}",
|
||||
"{\"serving_config\":\"" + sc + "\",\"query\":\"\"}", "empty query");
|
||||
check_enterprise_dry_run("/google/enterprise/answer",
|
||||
"{\"serving_config\":\"" + sc + "\",\"query\":\"what is covered\"}",
|
||||
"servingConfigs/default_search:answer");
|
||||
return finish("google/enterprise/answer");
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Route: POST /google/enterprise/call (any row of the table)
|
||||
#include "google/enterprise/cases.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
const std::string get_ds =
|
||||
"{\"api\":\"discoveryengine\",\"rpc\":\"discoveryengine.projects.locations.collections.dataStores.get\","
|
||||
"\"path\":{\"name\":\"projects/p1/locations/global/collections/default_collection/dataStores/ds1\"}}";
|
||||
check_enterprise_post_route("/google/enterprise/call", get_ds,
|
||||
"{\"api\":\"discoveryengine\",\"rpc\":\"nope\",\"path\":{}}", "unknown rpc");
|
||||
{
|
||||
GoogleRig rig;
|
||||
Fixture f(rig.client);
|
||||
const std::string j = "application/json";
|
||||
const std::string list = "\"rpc\":\"retail.projects.locations.catalogs.list\"";
|
||||
check(f.client->post("/google/enterprise/call", "not json", j).status == 422, "bad json");
|
||||
check(f.client->post("/google/enterprise/call", "[1]", j).status == 422, "array body");
|
||||
check(f.client->post("/google/enterprise/call", "{\"rpc\":\"x\"}", j).status == 422, "api required");
|
||||
check(f.client->post("/google/enterprise/call", "{\"api\":\"x\",\"rpc\":\"y\"}", j).status == 422, "unknown api");
|
||||
check(f.client->post("/google/enterprise/call",
|
||||
"{\"api\":\"retail\"," + list + ",\"path\":{\"parent\":\"../x\"}}", j).status == 422,
|
||||
"path traversal");
|
||||
check(f.client->post("/google/enterprise/call",
|
||||
"{\"api\":\"retail\"," + list +
|
||||
",\"path\":{\"parent\":\"projects/p/locations/global\"},\"query\":{\"bogus\":\"1\"}}",
|
||||
j).status == 422,
|
||||
"unknown query parameter");
|
||||
check(f.client->post("/google/enterprise/call", std::string("{\"x\":\"") + std::string(300 * 1024, 'a') + "\"}", j)
|
||||
.status == 413,
|
||||
"oversize body is 413");
|
||||
check(rig.mock->send_count() == 0, "side effect: nothing sent");
|
||||
}
|
||||
check_enterprise_dry_run(
|
||||
"/google/enterprise/call",
|
||||
"{\"api\":\"discoveryengine\",\"rpc\":\"discoveryengine.projects.locations.collections.dataStores.delete\","
|
||||
"\"path\":{\"name\":\"projects/p1/locations/global/collections/default_collection/dataStores/ds1\"}}",
|
||||
"dataStores/ds1");
|
||||
{
|
||||
// A read attempts exactly one upstream request, to the table-derived URL.
|
||||
GoogleRig rig;
|
||||
Fixture f(rig.client);
|
||||
auto res = f.client->post("/google/enterprise/call", get_ds, "application/json");
|
||||
check(res.status != 422 && res.status != 503, "a read gets past validation and credential");
|
||||
auto seen = rig.mock->seen();
|
||||
check(seen.size() == 1 && seen[0].method == "GET" &&
|
||||
contains(seen[0].target, "/v1/projects/p1/locations/global/collections/default_collection/dataStores/ds1"),
|
||||
"one GET to the table-derived path");
|
||||
}
|
||||
return finish("google/enterprise/call");
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Route: GET /google/enterprise/calls (the call table)
|
||||
#include "support/GoogleRig.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
GoogleRig rig(false);
|
||||
Fixture f(rig.client);
|
||||
auto res = f.client->get("/google/enterprise/calls?api=discoveryengine&q=servingConfigs.search");
|
||||
check(res.status == 200, "200 without a credential");
|
||||
check(contains(res.body, "servingConfigs.search") && contains(res.body, "v1/{+servingConfig}:search"), "rows shown");
|
||||
check(contains(res.body, "\"truncated\":false"), "not truncated");
|
||||
auto all = f.client->get("/google/enterprise/calls?api=dialogflow-es");
|
||||
check(contains(all.body, "\"truncated\":true") && contains(all.body, "\"cap_items\":200"), "truncation is loud");
|
||||
check(f.client->get("/google/enterprise/calls?api=nope").status == 422, "unknown api is 422");
|
||||
check(f.client->get("/google/enterprise/calls?api=ces&q=" + std::string(101, 'a')).status == 422, "q cap");
|
||||
check(f.client->post("/google/enterprise/calls").status == 405, "post is 405");
|
||||
check(rig.mock->send_count() == 0, "side effect: no upstream traffic");
|
||||
return finish("google/enterprise/calls");
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
// Shared case for the enterprise route tests (not a test itself). Every route
|
||||
// that would call Google is a POST that must: answer 503 google_not_configured
|
||||
// without a credential, answer 422 validation_error on bad input before any
|
||||
// upstream call, and answer 405 to GET.
|
||||
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
#include "support/GoogleRig.hpp"
|
||||
|
||||
namespace molo::example::test {
|
||||
|
||||
inline void check_enterprise_post_route(const std::string& path, const std::string& good_body,
|
||||
const std::string& bad_body, const std::string& what) {
|
||||
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
|
||||
const std::string json = "application/json";
|
||||
{
|
||||
GoogleRig rig(false);
|
||||
Fixture f(rig.client);
|
||||
auto res = f.client->post(path, good_body, json);
|
||||
check(res.status == 503, path + ": no credential is 503");
|
||||
check(contains(res.body, "google_not_configured"), "error code google_not_configured");
|
||||
check(contains(res.headers.get("Content-Type"), "application/json"), "error body is json");
|
||||
check(rig.mock->send_count() == 0, "side effect: no upstream traffic");
|
||||
}
|
||||
{
|
||||
GoogleRig rig;
|
||||
Fixture f(rig.client);
|
||||
auto res = f.client->post(path, bad_body, json);
|
||||
check(res.status == 422, path + ": " + what + " is 422");
|
||||
check(contains(res.body, "validation_error"), "error code validation_error");
|
||||
check(rig.mock->send_count() == 0, "side effect: rejected before any upstream call");
|
||||
check(f.client->get(path).status == 405, path + ": get is 405");
|
||||
}
|
||||
}
|
||||
|
||||
// With a credential, a non-read call is a dry run: 200, dry_run true, nothing sent.
|
||||
inline void check_enterprise_dry_run(const std::string& path, const std::string& good_body,
|
||||
const std::string& expect_in_url) {
|
||||
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
|
||||
GoogleRig rig;
|
||||
Fixture f(rig.client);
|
||||
auto res = f.client->post(path, good_body, "application/json");
|
||||
check(res.status == 200, path + ": dry run is 200");
|
||||
check(contains(res.body, "\"dry_run\":true"), "dry_run is true without confirm");
|
||||
check(contains(res.body, expect_in_url), "dry run shows the url: " + expect_in_url);
|
||||
check(!contains(res.body, kTestKey), "credential never shown");
|
||||
check(rig.mock->send_count() == 0, "side effect: a dry run sends nothing");
|
||||
}
|
||||
|
||||
} // namespace molo::example::test
|
||||
@@ -0,0 +1,14 @@
|
||||
// Route: POST /google/enterprise/ces/run
|
||||
#include "google/enterprise/cases.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
const std::string good = "{\"session\":\"projects/p1/locations/us/apps/a1/sessions/s1\",\"text\":\"hello\"}";
|
||||
check_enterprise_post_route("/google/enterprise/ces/run", good,
|
||||
"{\"session\":\"projects/p1/locations/us/apps/a1/sessions/s1\",\"text\":\"\"}",
|
||||
"empty text");
|
||||
check_enterprise_dry_run("/google/enterprise/ces/run", good,
|
||||
"ces.us.rep.googleapis.com/v1/projects/p1/locations/us/apps/a1/sessions/s1:runSession");
|
||||
return finish("google/enterprise/ces_run");
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Route: POST /google/enterprise/detect (Dialogflow ES and CX detectIntent)
|
||||
#include "google/enterprise/cases.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
const std::string es = "{\"kind\":\"es\",\"session\":\"projects/p1/agent/sessions/s1\",\"text\":\"hello\"}";
|
||||
check_enterprise_post_route("/google/enterprise/detect", es,
|
||||
"{\"kind\":\"es\",\"session\":\"projects/p1/agent/sessions/s1\",\"text\":\"\"}",
|
||||
"empty text");
|
||||
check_enterprise_dry_run("/google/enterprise/detect", es,
|
||||
"dialogflow.googleapis.com/v2/projects/p1/agent/sessions/s1:detectIntent");
|
||||
check_enterprise_dry_run(
|
||||
"/google/enterprise/detect",
|
||||
"{\"kind\":\"cx\",\"session\":\"projects/p1/locations/europe-west1/agents/a1/sessions/s1\","
|
||||
"\"text\":\"hi\",\"language\":\"nl\"}",
|
||||
"europe-west1-dialogflow.googleapis.com/v3/projects/p1/locations/europe-west1/agents/a1/sessions/s1:detectIntent");
|
||||
{
|
||||
GoogleRig rig;
|
||||
Fixture f(rig.client);
|
||||
check(f.client->post("/google/enterprise/detect", "{\"kind\":\"x\",\"session\":\"s\",\"text\":\"t\"}",
|
||||
"application/json").status == 422,
|
||||
"unknown kind");
|
||||
}
|
||||
return finish("google/enterprise/detect");
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Route: POST /google/enterprise/ground (check grounding)
|
||||
#include "google/enterprise/cases.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
const std::string gc = "projects/p1/locations/global/groundingConfigs/default_grounding_config";
|
||||
const std::string facts = "[{\"factText\":\"The sky is blue.\"}]";
|
||||
const std::string good = "{\"grounding_config\":\"" + gc + "\",\"answer\":\"Blue.\",\"facts\":" + facts + "}";
|
||||
check_enterprise_post_route("/google/enterprise/ground", good,
|
||||
"{\"grounding_config\":\"" + gc + "\",\"answer\":\"Blue.\",\"facts\":\"nope\"}",
|
||||
"facts not an array");
|
||||
check_enterprise_dry_run("/google/enterprise/ground", good, "groundingConfigs/default_grounding_config:check");
|
||||
return finish("google/enterprise/ground");
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Route: GET /google/enterprise (the page)
|
||||
#include "support/GoogleRig.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
GoogleRig rig(false);
|
||||
Fixture f(rig.client);
|
||||
auto res = f.client->get("/google/enterprise");
|
||||
check(res.status == 200, "page is 200 without a credential");
|
||||
check(contains(res.headers.get("Content-Type"), "text/html"), "html");
|
||||
check(contains(res.body, "Enterprise APIs") && contains(res.body, "e-run"), "page content");
|
||||
check(contains(res.body, "/embedded/google_enterprise.js"), "script linked");
|
||||
check(rig.mock->send_count() == 0, "side effect: no upstream traffic");
|
||||
check(f.client->post("/google/enterprise").status == 405, "post is 405");
|
||||
check(f.client->get("/embedded/google_enterprise.js").status == 200, "asset served");
|
||||
return finish("google/enterprise/page");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// Route: POST /google/enterprise/predict (Retail and Recommendations AI)
|
||||
#include "google/enterprise/cases.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
const std::string pl = "projects/p1/locations/global/catalogs/default_catalog/placements/pl";
|
||||
const std::string retail = "{\"kind\":\"retail\",\"placement\":\"" + pl +
|
||||
"\",\"user_event\":{\"eventType\":\"home-page-view\",\"visitorId\":\"v1\"}}";
|
||||
check_enterprise_post_route("/google/enterprise/predict", retail,
|
||||
"{\"kind\":\"retail\",\"placement\":\"" + pl + "\",\"user_event\":\"nope\"}",
|
||||
"user_event not an object");
|
||||
check_enterprise_dry_run("/google/enterprise/predict", retail,
|
||||
"retail.googleapis.com/v2/projects/p1/locations/global/catalogs/default_catalog/placements/pl:predict");
|
||||
check_enterprise_dry_run(
|
||||
"/google/enterprise/predict",
|
||||
"{\"kind\":\"recommendations\",\"placement\":\"projects/p1/locations/global/catalogs/c/eventStores/e/placements/pl\","
|
||||
"\"user_event\":{\"eventType\":\"page-visit\"}}",
|
||||
"recommendationengine.googleapis.com/v1beta1/projects/p1/locations/global/catalogs/c/eventStores/e/placements/pl:predict");
|
||||
return finish("google/enterprise/predict");
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Route: POST /google/enterprise/rank
|
||||
#include "google/enterprise/cases.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
const std::string rc = "projects/p1/locations/global/rankingConfigs/default_ranking_config";
|
||||
const std::string good =
|
||||
"{\"ranking_config\":\"" + rc + "\",\"query\":\"q\",\"records\":[{\"id\":\"1\",\"content\":\"a\"}]}";
|
||||
check_enterprise_post_route("/google/enterprise/rank", good,
|
||||
"{\"ranking_config\":\"" + rc + "\",\"query\":\"q\",\"records\":[]}", "no records");
|
||||
check_enterprise_dry_run("/google/enterprise/rank", good, "rankingConfigs/default_ranking_config:rank");
|
||||
return finish("google/enterprise/rank");
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Route: POST /google/enterprise/search
|
||||
#include "google/enterprise/cases.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
const std::string sc =
|
||||
"projects/p1/locations/global/collections/default_collection/engines/e1/servingConfigs/default_search";
|
||||
check_enterprise_post_route("/google/enterprise/search", "{\"serving_config\":\"" + sc + "\",\"query\":\"refunds\"}",
|
||||
"{\"serving_config\":\"" + sc + "\",\"query\":\"x\",\"page_size\":0}", "page_size 0");
|
||||
check_enterprise_dry_run("/google/enterprise/search", "{\"serving_config\":\"" + sc + "\",\"query\":\"refunds\"}",
|
||||
"engines/e1/servingConfigs/default_search:search");
|
||||
return finish("google/enterprise/search");
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Route: GET /google/enterprise/status
|
||||
#include <cstdlib>
|
||||
|
||||
#include "support/GoogleRig.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
|
||||
{
|
||||
GoogleRig rig(false);
|
||||
Fixture f(rig.client);
|
||||
auto res = f.client->get("/google/enterprise/status");
|
||||
check(res.status == 200, "status 200 without a credential");
|
||||
check(contains(res.headers.get("Content-Type"), "application/json"), "json");
|
||||
check(contains(res.body, "\"configured\":false") && contains(res.body, "\"project_set\":true"), "state shown");
|
||||
check(contains(res.body, "discoveryengine.googleapis.com") && contains(res.body, "ces.googleapis.com") &&
|
||||
contains(res.body, "recommendationengine.googleapis.com"),
|
||||
"services listed");
|
||||
check(contains(res.body, "project=test-project"), "activation url uses the project");
|
||||
check(contains(res.body, "\"max_list_items\":500"), "caps shown");
|
||||
check(rig.mock->send_count() == 0, "side effect: no upstream traffic");
|
||||
}
|
||||
{
|
||||
GoogleRig rig;
|
||||
Fixture f(rig.client);
|
||||
auto res = f.client->get("/google/enterprise/status");
|
||||
check(contains(res.body, "\"configured\":true"), "configured with a credential");
|
||||
check(!contains(res.body, kTestKey), "credential never shown");
|
||||
check(f.client->post("/google/enterprise/status").status == 405, "post is 405");
|
||||
}
|
||||
return finish("google/enterprise/status");
|
||||
}
|
||||
Reference in New Issue
Block a user