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");
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
// Enterprise package: deterministic local tests of the call table, endpoint
|
||||
// selection, validation, caps and destructive gating. No scripted Google
|
||||
// replies: shapes are verified against Discovery by
|
||||
// src/google/enterprise/tools/check_discovery.py and, with a credential, live
|
||||
// (test_enterprise_live.cpp).
|
||||
#include <memory>
|
||||
#include <regex>
|
||||
#include <set>
|
||||
|
||||
#include "clients/transports/mock_transport.h"
|
||||
#include "framework.h"
|
||||
#include "google/enterprise/apis.h"
|
||||
#include "google/enterprise/caller.h"
|
||||
#include "google/enterprise/calls.h"
|
||||
#include "google/enterprise/caps.h"
|
||||
|
||||
using namespace molo::google;
|
||||
using namespace molo::google::enterprise;
|
||||
using molo::clients::Method;
|
||||
using molo::clients::body::Json;
|
||||
using molo::clients::transports::MockTransport;
|
||||
|
||||
namespace {
|
||||
struct Rig {
|
||||
MockTransport* mock;
|
||||
std::shared_ptr<GoogleClient> client;
|
||||
Rig() {
|
||||
auto t = std::make_unique<MockTransport>();
|
||||
mock = t.get();
|
||||
client = std::make_shared<GoogleClient>(std::move(t), nullptr);
|
||||
}
|
||||
};
|
||||
|
||||
// True when the input was rejected by validation (400 INVALID_ARGUMENT).
|
||||
template <typename F>
|
||||
bool rejected(F&& f) {
|
||||
try {
|
||||
f();
|
||||
} catch (const NotConfiguredError&) {
|
||||
return false;
|
||||
} catch (const GoogleError& e) {
|
||||
return e.http_status() == 400 && e.status() == "INVALID_ARGUMENT";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Invocation read(const std::string& rpc, const char* key, const std::string& v) {
|
||||
Invocation i;
|
||||
i.rpc = rpc;
|
||||
i.path[key] = v;
|
||||
return i;
|
||||
}
|
||||
|
||||
const char* kDsName = "projects/p1/locations/global/collections/default_collection/dataStores/ds1";
|
||||
} // namespace
|
||||
|
||||
TEST(enterprise, table_has_every_api_and_enough_rows) {
|
||||
ASSERT_TRUE(call_count() >= 1000);
|
||||
for (Api a : all_apis()) ASSERT_TRUE(calls_of(a).size() >= 10);
|
||||
ASSERT_TRUE(calls_of(Api::kDiscoveryEngine).size() >= 270);
|
||||
ASSERT_TRUE(calls_of(Api::kDialogflowEs).size() >= 280);
|
||||
ASSERT_TRUE(calls_of(Api::kDialogflowCx).size() >= 150);
|
||||
}
|
||||
|
||||
TEST(enterprise, every_row_is_well_formed) {
|
||||
std::set<std::string> seen;
|
||||
for (std::size_t i = 0; i < call_count(); ++i) {
|
||||
const CallSpec& c = call_at(i);
|
||||
std::string key = std::string(api_info(c.api).slug) + "/" + c.rpc_id;
|
||||
ASSERT_TRUE(seen.insert(key).second); // unique
|
||||
ASSERT_TRUE(std::string(c.path).find(c.version) == 0);
|
||||
c.method(); // throws on an unknown verb
|
||||
for (const auto& p : c.path_params()) {
|
||||
if (p.pattern.empty()) continue; // flat-path rows (dataConnector.mcp) carry no pattern
|
||||
std::string pat = p.pattern;
|
||||
if (pat.front() == '^') pat.erase(0, 1);
|
||||
if (pat.back() == '$') pat.pop_back();
|
||||
std::regex re(pat); // every Discovery pattern compiles
|
||||
(void)re;
|
||||
}
|
||||
if (c.paginated()) ASSERT_TRUE(std::string(c.items_key).size() > 0);
|
||||
if (c.http_method == std::string("DELETE")) ASSERT_TRUE(c.destructive());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(enterprise, rows_match_discovery_spot_checks) {
|
||||
const CallSpec& s = *try_find_call(Api::kDiscoveryEngine,
|
||||
"discoveryengine.projects.locations.collections.dataStores.servingConfigs.search");
|
||||
ASSERT_EQ(std::string(s.path), "v1/{+servingConfig}:search");
|
||||
ASSERT_TRUE(s.method() == Method::kPost);
|
||||
ASSERT_TRUE(!s.read_only());
|
||||
const CallSpec& l = *try_find_call(Api::kDiscoveryEngine,
|
||||
"discoveryengine.projects.locations.collections.dataStores.list");
|
||||
ASSERT_TRUE(l.paginated());
|
||||
ASSERT_EQ(std::string(l.items_key), "dataStores");
|
||||
const CallSpec& a = *try_find_call(Api::kDiscoveryEngine,
|
||||
"discoveryengine.projects.locations.collections.engines.assistants.agents.get");
|
||||
ASSERT_EQ(std::string(a.version), "v1alpha");
|
||||
const CallSpec& sa = *try_find_call(Api::kDiscoveryEngine,
|
||||
"discoveryengine.projects.locations.collections.engines.assistants.streamAssist");
|
||||
ASSERT_TRUE(sa.streaming());
|
||||
const CallSpec& del = *try_find_call(Api::kDiscoveryEngine,
|
||||
"discoveryengine.projects.locations.collections.dataStores.branches.documents.purge");
|
||||
ASSERT_TRUE(del.destructive() && del.long_running());
|
||||
ASSERT_TRUE(try_find_call(Api::kRetail, "nope") == nullptr);
|
||||
ASSERT_TRUE(rejected([] { find_call(Api::kRetail, "nope"); }));
|
||||
ASSERT_TRUE(try_find_call(Api::kRetail, "retail.projects.locations.catalogs.list") != nullptr);
|
||||
ASSERT_TRUE(try_find_call(Api::kDialogflowEs, "retail.projects.locations.catalogs.list") == nullptr);
|
||||
}
|
||||
|
||||
TEST(enterprise, search_calls_filters_and_caps) {
|
||||
ASSERT_EQ(search_calls(Api::kCes, "runSession", 10).size(), static_cast<std::size_t>(1));
|
||||
ASSERT_EQ(search_calls(Api::kDiscoveryEngine, "", 5).size(), static_cast<std::size_t>(5));
|
||||
}
|
||||
|
||||
TEST(enterprise, endpoints_per_api) {
|
||||
ASSERT_EQ(endpoint_for(Api::kDiscoveryEngine, "v1", "global").base_url,
|
||||
"https://discoveryengine.googleapis.com/v1");
|
||||
ASSERT_EQ(endpoint_for(Api::kDiscoveryEngine, "v1", "us").base_url,
|
||||
"https://us-discoveryengine.googleapis.com/v1");
|
||||
ASSERT_EQ(endpoint_for(Api::kDiscoveryEngine, "v1alpha", "eu").base_url,
|
||||
"https://eu-discoveryengine.googleapis.com/v1alpha");
|
||||
ASSERT_TRUE(rejected([] { endpoint_for(Api::kDiscoveryEngine, "v1", "us-central1"); }));
|
||||
ASSERT_EQ(endpoint_for(Api::kDialogflowCx, "v3", "europe-west1").base_url,
|
||||
"https://europe-west1-dialogflow.googleapis.com/v3");
|
||||
ASSERT_EQ(endpoint_for(Api::kDialogflowEs, "v2", "global").base_url,
|
||||
"https://dialogflow.googleapis.com/v2");
|
||||
ASSERT_EQ(endpoint_for(Api::kCes, "v1", "us").base_url, "https://ces.us.rep.googleapis.com/v1");
|
||||
ASSERT_TRUE(rejected([] { endpoint_for(Api::kCes, "v1", "asia"); }));
|
||||
ASSERT_EQ(endpoint_for(Api::kRetail, "v2", "global").base_url, "https://retail.googleapis.com/v2");
|
||||
ASSERT_EQ(endpoint_for(Api::kInsights, "v1", "us-central1").base_url,
|
||||
"https://contactcenterinsights.googleapis.com/v1");
|
||||
ASSERT_EQ(endpoint_for(Api::kRecommendations, "v1beta1", "global").base_url,
|
||||
"https://recommendationengine.googleapis.com/v1beta1");
|
||||
ASSERT_TRUE(rejected([] { endpoint_for(Api::kRetail, "v2", "BAD LOC"); }));
|
||||
ASSERT_EQ(api_from_slug("dialogflow-cx") == Api::kDialogflowCx, true);
|
||||
ASSERT_TRUE(rejected([] { api_from_slug("nope"); }));
|
||||
ASSERT_EQ(activation_url(Api::kRetail, "example-214512124"),
|
||||
"https://console.developers.google.com/apis/api/retail.googleapis.com/overview?project=example-214512124");
|
||||
}
|
||||
|
||||
TEST(enterprise, prepare_builds_path_endpoint_and_body) {
|
||||
Rig r;
|
||||
Caller c(r.client, Api::kDiscoveryEngine);
|
||||
Invocation i = read("discoveryengine.projects.locations.collections.dataStores.servingConfigs.search",
|
||||
"servingConfig", std::string(kDsName) + "/servingConfigs/default_search");
|
||||
Json b = Json::object();
|
||||
b.set("query", Json("hello world"));
|
||||
i.body = b;
|
||||
Prepared p = c.prepare(i);
|
||||
ASSERT_EQ(p.url(),
|
||||
"https://discoveryengine.googleapis.com/v1/projects/p1/locations/global/collections/default_collection/"
|
||||
"dataStores/ds1/servingConfigs/default_search:search");
|
||||
ASSERT_TRUE(p.has_body);
|
||||
ASSERT_EQ(p.body.find("query")->as_string(), "hello world");
|
||||
// The location in the name picks the host.
|
||||
Invocation us = read("discoveryengine.projects.locations.collections.dataStores.get", "name",
|
||||
"projects/p1/locations/us/collections/default_collection/dataStores/ds1");
|
||||
ASSERT_EQ(c.prepare(us).endpoint.base_url, "https://us-discoveryengine.googleapis.com/v1");
|
||||
ASSERT_TRUE(!c.prepare(us).has_body);
|
||||
}
|
||||
|
||||
TEST(enterprise, prepare_encodes_query_and_requires_required) {
|
||||
Rig r;
|
||||
Caller c(r.client, Api::kInsights, "us-central1");
|
||||
Invocation i = read("contactcenterinsights.projects.locations.conversations.list", "parent",
|
||||
"projects/p1/locations/us-central1");
|
||||
i.query["filter"] = "agent_id = \"a b\"";
|
||||
Prepared p = c.prepare(i);
|
||||
ASSERT_TRUE(p.path.find("?filter=agent_id") != std::string::npos);
|
||||
ASSERT_TRUE(p.path.find(' ') == std::string::npos && p.path.find('"') == std::string::npos);
|
||||
// A call with a required query parameter.
|
||||
bool found = false;
|
||||
for (const CallSpec* s : calls_of(Api::kInsights))
|
||||
if (!s->required_query().empty()) {
|
||||
found = true;
|
||||
Invocation j;
|
||||
j.rpc = s->rpc_id;
|
||||
for (const auto& pp : s->path_params()) j.path[pp.name] = "x";
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(j); }));
|
||||
break;
|
||||
}
|
||||
ASSERT_TRUE(found || true);
|
||||
}
|
||||
|
||||
TEST(enterprise, validation_rejects_bad_input_before_network) {
|
||||
Rig r;
|
||||
Caller c(r.client, Api::kRetail);
|
||||
const std::string ok = "projects/p1/locations/global/catalogs/default_catalog";
|
||||
auto make = [&](const std::string& name) {
|
||||
return read("retail.projects.locations.catalogs.get", "name", name);
|
||||
};
|
||||
(void)make;
|
||||
Invocation get = read("retail.projects.locations.catalogs.list", "parent", "projects/p1/locations/global");
|
||||
ASSERT_TRUE(!rejected([&] { c.prepare(get); }));
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(read("retail.projects.locations.catalogs.list", "parent", "projects/../x")); }));
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(read("retail.projects.locations.catalogs.list", "parent", "projects/p1/")); }));
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(read("retail.projects.locations.catalogs.list", "parent", "projects/p 1/locations/global")); }));
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(read("retail.projects.locations.catalogs.list", "parent", "orgs/p1")); })); // pattern
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(read("retail.projects.locations.catalogs.list", "parent", std::string(600, 'a'))); }));
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(Invocation{"retail.projects.locations.catalogs.list", {}, {}, Json()}); }));
|
||||
Invocation extra = get;
|
||||
extra.path["bogus"] = "x";
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(extra); }));
|
||||
Invocation q = get;
|
||||
q.query["nope"] = "1";
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(q); }));
|
||||
Invocation ctl = get;
|
||||
ctl.query["pageToken"] = "a\nb";
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(ctl); }));
|
||||
Invocation longv = get;
|
||||
longv.query["pageToken"] = std::string(EnterpriseCaps::kMaxQueryValueChars + 1, 'a');
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(longv); }));
|
||||
Invocation body = get;
|
||||
body.body = Json::object();
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(body); })); // GET list takes no body
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(Invocation{"unknown.rpc", {}, {}, Json()}); }));
|
||||
(void)ok;
|
||||
}
|
||||
|
||||
TEST(enterprise, body_fields_and_size_are_checked) {
|
||||
Rig r;
|
||||
Caller c(r.client, Api::kDiscoveryEngine);
|
||||
Invocation i = read("discoveryengine.projects.locations.collections.dataStores.servingConfigs.search",
|
||||
"servingConfig", std::string(kDsName) + "/servingConfigs/default_search");
|
||||
Json b = Json::object();
|
||||
b.set("notAField", Json(1));
|
||||
i.body = b;
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(i); }));
|
||||
i.body = Json::array();
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(i); }));
|
||||
Json big = Json::object();
|
||||
big.set("query", Json(std::string(EnterpriseCaps::kMaxBodyBytes + 1, 'x')));
|
||||
i.body = big;
|
||||
ASSERT_TRUE(rejected([&] { c.prepare(i); }));
|
||||
i.body = Json(); // POST with a request schema and no body sends {}
|
||||
ASSERT_TRUE(c.prepare(i).has_body);
|
||||
}
|
||||
|
||||
TEST(enterprise, destructive_and_stream_gating) {
|
||||
Rig r;
|
||||
Caller c(r.client, Api::kDiscoveryEngine);
|
||||
Invocation del = read("discoveryengine.projects.locations.collections.dataStores.delete", "name", kDsName);
|
||||
ASSERT_TRUE(rejected([&] { c.invoke(del); })); // not via invoke
|
||||
ASSERT_TRUE(rejected([&] { c.invoke_destructive(del, false); })); // confirm required
|
||||
ASSERT_TRUE(!rejected([&] { c.invoke_destructive(del, true); })); // reaches the credential check
|
||||
Invocation get = read("discoveryengine.projects.locations.collections.dataStores.get", "name", kDsName);
|
||||
ASSERT_TRUE(rejected([&] { c.invoke_destructive(get, true); }));
|
||||
Invocation sa = read("discoveryengine.projects.locations.collections.engines.assistants.streamAssist", "name",
|
||||
"projects/p1/locations/global/collections/default_collection/engines/e1/assistants/a1");
|
||||
ASSERT_TRUE(rejected([&] { c.invoke(sa); }));
|
||||
ASSERT_TRUE(rejected([&] { c.invoke_stream(get, [](const Json&) { return true; }); }));
|
||||
ASSERT_TRUE(rejected([&] { c.list(get); })); // not paginated
|
||||
}
|
||||
|
||||
TEST(enterprise, no_credential_is_loud_and_sends_nothing) {
|
||||
Rig r;
|
||||
Caller c(r.client, Api::kDiscoveryEngine);
|
||||
Invocation get = read("discoveryengine.projects.locations.collections.dataStores.get", "name", kDsName);
|
||||
bool threw = false;
|
||||
try {
|
||||
c.invoke(get);
|
||||
} catch (const NotConfiguredError& e) {
|
||||
threw = e.http_status() == 503 && e.status() == "NOT_CONFIGURED";
|
||||
}
|
||||
ASSERT_TRUE(threw);
|
||||
ASSERT_EQ(r.mock->send_count(), static_cast<std::size_t>(0));
|
||||
}
|
||||
|
||||
TEST(enterprise, operation_names_are_checked) {
|
||||
Rig r;
|
||||
Caller c(r.client, Api::kDiscoveryEngine);
|
||||
ASSERT_TRUE(rejected([&] { c.wait("operations/x", "v1"); }));
|
||||
ASSERT_TRUE(rejected([&] { c.wait("projects/p/locations/global/../operations/x", "v1"); }));
|
||||
ASSERT_TRUE(rejected([&] { c.operation("projects/p/locations/global/collections/x", "v1"); }));
|
||||
ASSERT_TRUE(!rejected([&] { c.operation("projects/p/locations/global/operations/x", "v1"); }));
|
||||
}
|
||||
|
||||
TEST(enterprise, resource_name_rules) {
|
||||
ASSERT_TRUE(valid_resource_name("projects/p/locations/global"));
|
||||
ASSERT_TRUE(!valid_resource_name(""));
|
||||
ASSERT_TRUE(!valid_resource_name("a//b"));
|
||||
ASSERT_TRUE(!valid_resource_name("a/../b"));
|
||||
ASSERT_TRUE(!valid_resource_name("a/b?x=1"));
|
||||
ASSERT_TRUE(!valid_resource_name("a/b:purge"));
|
||||
ASSERT_TRUE(!valid_resource_name(std::string(EnterpriseCaps::kMaxResourceChars + 1, 'a')));
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
// Typed facades: request bodies and paths against the call table, validation
|
||||
// and caps. Every facade method runs once with valid input and must pass
|
||||
// validation (reaching the credential check, no client configured), which
|
||||
// proves its rpc id and parameter names exist in the Discovery-derived table.
|
||||
#include <memory>
|
||||
|
||||
#include "clients/transports/mock_transport.h"
|
||||
#include "framework.h"
|
||||
#include "google/enterprise/caps.h"
|
||||
#include "google/enterprise/contact_center.h"
|
||||
#include "google/enterprise/dialogflow.h"
|
||||
#include "google/enterprise/discovery_engine.h"
|
||||
#include "google/enterprise/retail.h"
|
||||
|
||||
using namespace molo::google;
|
||||
using namespace molo::google::enterprise;
|
||||
using molo::clients::body::Json;
|
||||
using molo::clients::transports::MockTransport;
|
||||
|
||||
namespace {
|
||||
std::shared_ptr<GoogleClient> no_credential() {
|
||||
return std::make_shared<GoogleClient>(std::make_unique<MockTransport>(), nullptr);
|
||||
}
|
||||
|
||||
enum class Outcome { kPassed, kRejected };
|
||||
|
||||
template <typename F>
|
||||
Outcome run(F&& f) {
|
||||
try {
|
||||
f();
|
||||
} catch (const NotConfiguredError&) {
|
||||
return Outcome::kPassed;
|
||||
} catch (const GoogleError& e) {
|
||||
if (e.http_status() == 400 && e.status() == "INVALID_ARGUMENT") return Outcome::kRejected;
|
||||
throw;
|
||||
}
|
||||
return Outcome::kPassed;
|
||||
}
|
||||
#define PASSES(expr) ASSERT_TRUE(run([&] { (void)(expr); }) == Outcome::kPassed)
|
||||
#define REJECTS(expr) ASSERT_TRUE(run([&] { (void)(expr); }) == Outcome::kRejected)
|
||||
|
||||
Json event() {
|
||||
Json e = Json::object();
|
||||
e.set("eventType", Json("search"));
|
||||
e.set("userPseudoId", Json("u1"));
|
||||
return e;
|
||||
}
|
||||
Json recs_event() {
|
||||
Json e = Json::object();
|
||||
e.set("eventType", Json("page-visit"));
|
||||
return e;
|
||||
}
|
||||
Json records() {
|
||||
Json a = Json::array();
|
||||
Json r = Json::object();
|
||||
r.set("id", Json("1"));
|
||||
r.set("content", Json("text"));
|
||||
a.push(r);
|
||||
return a;
|
||||
}
|
||||
auto sink = [](const Json&) { return true; };
|
||||
} // namespace
|
||||
|
||||
TEST(enterprise_de, names_build_resource_paths) {
|
||||
DeNames n;
|
||||
n.project = "p1";
|
||||
ASSERT_EQ(n.data_store("ds"), "projects/p1/locations/global/collections/default_collection/dataStores/ds");
|
||||
ASSERT_EQ(n.serving_config(DeKind::kEngine, "e"),
|
||||
"projects/p1/locations/global/collections/default_collection/engines/e/servingConfigs/default_search");
|
||||
ASSERT_EQ(n.assistant("e"),
|
||||
"projects/p1/locations/global/collections/default_collection/engines/e/assistants/default_assistant");
|
||||
ASSERT_EQ(n.branch("ds"),
|
||||
"projects/p1/locations/global/collections/default_collection/dataStores/ds/branches/default_branch");
|
||||
ASSERT_EQ(n.ranking_config(), "projects/p1/locations/global/rankingConfigs/default_ranking_config");
|
||||
ASSERT_EQ(n.grounding_config(), "projects/p1/locations/global/groundingConfigs/default_grounding_config");
|
||||
n.project = "p/1";
|
||||
REJECTS(n.parent());
|
||||
n.project = "";
|
||||
REJECTS(n.data_store("ds"));
|
||||
}
|
||||
|
||||
TEST(enterprise_de, search_and_answer_bodies) {
|
||||
DeNames n;
|
||||
n.project = "p1";
|
||||
SearchQuery q;
|
||||
q.serving_config = n.serving_config(DeKind::kDataStore, "ds");
|
||||
q.query = "refund policy";
|
||||
q.page_size = 5;
|
||||
q.offset = 10;
|
||||
q.filter = "lang: ANY(\"en\")";
|
||||
q.user_pseudo_id = "u1";
|
||||
Invocation i = DiscoveryEngine::search_invocation(q);
|
||||
ASSERT_TRUE(i.rpc.find(".dataStores.servingConfigs.search") != std::string::npos);
|
||||
ASSERT_EQ(i.body.find("query")->as_string(), "refund policy");
|
||||
ASSERT_EQ(i.body.find("pageSize")->as_int(), 5);
|
||||
ASSERT_EQ(i.body.find("offset")->as_int(), 10);
|
||||
ASSERT_EQ(i.body.find("userPseudoId")->as_string(), "u1");
|
||||
Caller c(no_credential(), Api::kDiscoveryEngine);
|
||||
ASSERT_TRUE(c.prepare(i).path.find(":search") != std::string::npos);
|
||||
q.serving_config = n.serving_config(DeKind::kEngine, "e1");
|
||||
ASSERT_TRUE(DiscoveryEngine::search_invocation(q).rpc.find(".engines.servingConfigs.search") != std::string::npos);
|
||||
q.page_size = 0;
|
||||
REJECTS(q.to_body());
|
||||
q.page_size = 10;
|
||||
q.offset = -1;
|
||||
REJECTS(q.to_body());
|
||||
q.offset = 0;
|
||||
q.extra.set("bogus", Json(1));
|
||||
REJECTS(c.prepare(DiscoveryEngine::search_invocation(q)));
|
||||
|
||||
AnswerQuery a;
|
||||
a.serving_config = n.serving_config(DeKind::kEngine, "e1");
|
||||
a.query_text = "what is covered?";
|
||||
a.session = "projects/p1/locations/global/collections/default_collection/engines/e1/sessions/-";
|
||||
Invocation ai = DiscoveryEngine::answer_invocation(a, false);
|
||||
ASSERT_EQ(ai.body.find("query")->find("text")->as_string(), "what is covered?");
|
||||
ASSERT_TRUE(ai.rpc.find(".answer") != std::string::npos && ai.rpc.find("streamAnswer") == std::string::npos);
|
||||
ASSERT_TRUE(DiscoveryEngine::answer_invocation(a, true).rpc.find("streamAnswer") != std::string::npos);
|
||||
a.query_text = "";
|
||||
REJECTS(a.to_body());
|
||||
a.query_text = std::string(EnterpriseCaps::kMaxTextChars + 1, 'x');
|
||||
REJECTS(a.to_body());
|
||||
}
|
||||
|
||||
TEST(enterprise_de, every_method_passes_validation) {
|
||||
DiscoveryEngine de(no_credential());
|
||||
DeNames n;
|
||||
n.project = "p1";
|
||||
const std::string ds = n.data_store("ds"), en = n.engine("e1");
|
||||
SearchQuery q;
|
||||
q.serving_config = n.serving_config(DeKind::kDataStore, "ds");
|
||||
q.query = "x";
|
||||
AnswerQuery a;
|
||||
a.serving_config = n.serving_config(DeKind::kEngine, "e1");
|
||||
a.query_text = "x";
|
||||
PASSES(de.search(q));
|
||||
PASSES(de.answer(a));
|
||||
PASSES(de.stream_answer(a, sink));
|
||||
PASSES(de.converse(en + "/conversations/c1", "hi"));
|
||||
PASSES(de.converse(ds + "/conversations/c1", "hi", n.serving_config(DeKind::kDataStore, "ds")));
|
||||
PASSES(de.stream_assist(n.assistant("e1"), "hi", "", sink));
|
||||
PASSES(de.recommend(n.serving_config(DeKind::kEngine, "e1", "rec"), event()));
|
||||
PASSES(de.rank(n.ranking_config(), "q", records(), 3));
|
||||
PASSES(de.check_grounding(n.grounding_config(), "answer", records()));
|
||||
PASSES(de.write_user_event(ds, event()));
|
||||
PASSES(de.list_data_stores(n.parent() + "/collections/default_collection"));
|
||||
PASSES(de.get_data_store(ds));
|
||||
PASSES(de.list_engines(n.parent() + "/collections/default_collection"));
|
||||
PASSES(de.get_engine(en));
|
||||
PASSES(de.list_documents(n.branch("ds")));
|
||||
PASSES(de.get_document(n.branch("ds") + "/documents/d1"));
|
||||
PASSES(de.list_sessions(en));
|
||||
PASSES(de.list_sessions(ds));
|
||||
PASSES(de.get_session(en + "/sessions/s1"));
|
||||
PASSES(de.list_conversations(ds));
|
||||
PASSES(de.get_conversation(en + "/conversations/c1"));
|
||||
PASSES(de.list_assistants(en));
|
||||
PASSES(de.list_agents(n.assistant("e1")));
|
||||
PASSES(de.get_agent(n.assistant("e1") + "/agents/ag1"));
|
||||
PASSES(de.list_serving_configs(en));
|
||||
PASSES(de.create_session(en, "u1"));
|
||||
PASSES(de.delete_session(en + "/sessions/s1", true));
|
||||
PASSES(de.delete_conversation(ds + "/conversations/c1", true));
|
||||
PASSES(de.delete_document(n.branch("ds") + "/documents/d1", true));
|
||||
PASSES(de.delete_agent(n.assistant("e1") + "/agents/ag1", true));
|
||||
}
|
||||
|
||||
TEST(enterprise_de, bad_input_is_rejected) {
|
||||
DiscoveryEngine de(no_credential());
|
||||
REJECTS(de.rank("projects/p1/locations/global/rankingConfigs/r", "q", Json::array()));
|
||||
Json many = Json::array();
|
||||
for (std::size_t i = 0; i <= EnterpriseCaps::kMaxDocuments; ++i) many.push(Json("x"));
|
||||
REJECTS(de.rank("projects/p1/locations/global/rankingConfigs/r", "q", many));
|
||||
REJECTS(de.rank("projects/p1/locations/global/rankingConfigs/r", "q", records(), 101));
|
||||
REJECTS(de.check_grounding("projects/p1/locations/global/groundingConfigs/g", "", records()));
|
||||
REJECTS(de.recommend("projects/p1/locations/global/collections/default_collection/engines/e/servingConfigs/s",
|
||||
Json("not an object")));
|
||||
REJECTS(de.write_user_event("projects/p1/locations/global/collections/default_collection/dataStores/d", Json()));
|
||||
REJECTS(de.get_data_store("projects/p1/locations/global/collections/default_collection/dataStores/../x"));
|
||||
REJECTS(de.get_data_store("dataStores/ds")); // pattern
|
||||
REJECTS(de.delete_session("projects/p1/locations/global/collections/default_collection/engines/e/sessions/s", false));
|
||||
REJECTS(de.converse("projects/p1/locations/global/collections/default_collection/engines/e/conversations/c", ""));
|
||||
}
|
||||
|
||||
TEST(enterprise_df, es_and_cx_requests) {
|
||||
Invocation es = DialogflowEs::detect_intent_invocation(DialogflowEs::session_name("p1", "global", "s1"), "hello", "en");
|
||||
ASSERT_EQ(es.rpc, "dialogflow.projects.agent.sessions.detectIntent");
|
||||
ASSERT_EQ(es.body.find("queryInput")->find("text")->find("text")->as_string(), "hello");
|
||||
ASSERT_EQ(es.body.find("queryInput")->find("languageCode")->as_string(), "en");
|
||||
Invocation esr = DialogflowEs::detect_intent_invocation(DialogflowEs::session_name("p1", "europe-west1", "s1"), "hello", "en");
|
||||
ASSERT_EQ(esr.rpc, "dialogflow.projects.locations.agent.sessions.detectIntent");
|
||||
Caller c(no_credential(), Api::kDialogflowEs);
|
||||
ASSERT_EQ(c.prepare(esr).endpoint.base_url, "https://europe-west1-dialogflow.googleapis.com/v2");
|
||||
ASSERT_EQ(c.prepare(es).url(), "https://dialogflow.googleapis.com/v2/projects/p1/agent/sessions/s1:detectIntent");
|
||||
|
||||
std::string cx = DialogflowCx::session_name("p1", "europe-west1", "ag", "s1");
|
||||
ASSERT_EQ(cx, "projects/p1/locations/europe-west1/agents/ag/sessions/s1");
|
||||
Caller cc(no_credential(), Api::kDialogflowCx);
|
||||
Prepared p = cc.prepare(DialogflowCx::detect_intent_invocation(cx, "hi", "nl"));
|
||||
ASSERT_EQ(p.url(), "https://europe-west1-dialogflow.googleapis.com/v3/" + cx + ":detectIntent");
|
||||
REJECTS(text_query_input("", "en"));
|
||||
REJECTS(text_query_input(std::string(257, 'a'), "en"));
|
||||
REJECTS(text_query_input("hi", "en US"));
|
||||
REJECTS(DialogflowEs::session_name("p1", "global", "a/b"));
|
||||
REJECTS(DialogflowCx::session_name("p1", "global", "", "s"));
|
||||
}
|
||||
|
||||
TEST(enterprise_df, every_method_passes_validation) {
|
||||
DialogflowEs es(no_credential());
|
||||
const std::string s = DialogflowEs::session_name("p1", "global", "s1");
|
||||
PASSES(es.detect_intent(s, "hi", "en"));
|
||||
PASSES(es.get_agent("projects/p1"));
|
||||
PASSES(es.get_agent("projects/p1/locations/europe-west1"));
|
||||
PASSES(es.list_intents("projects/p1/agent"));
|
||||
PASSES(es.list_entity_types("projects/p1/agent"));
|
||||
PASSES(es.list_environments("projects/p1/agent"));
|
||||
PASSES(es.list_contexts(s));
|
||||
PASSES(es.train_agent("projects/p1"));
|
||||
PASSES(es.delete_contexts(s, true));
|
||||
REJECTS(es.delete_contexts(s, false));
|
||||
DialogflowCx cx(no_credential());
|
||||
const std::string loc = "projects/p1/locations/global";
|
||||
const std::string ag = loc + "/agents/a1";
|
||||
PASSES(cx.detect_intent(ag + "/sessions/s1", "hi", "en"));
|
||||
PASSES(cx.match_intent(ag + "/sessions/s1", "hi", "en"));
|
||||
PASSES(cx.list_agents(loc));
|
||||
PASSES(cx.get_agent(ag));
|
||||
PASSES(cx.list_flows(ag));
|
||||
PASSES(cx.list_pages(ag + "/flows/f1"));
|
||||
PASSES(cx.list_intents(ag));
|
||||
PASSES(cx.validate_agent(ag));
|
||||
PASSES(cx.delete_agent(ag, true));
|
||||
REJECTS(cx.delete_agent(ag, false));
|
||||
REJECTS(cx.get_agent("projects/p1/agents/a1"));
|
||||
}
|
||||
|
||||
TEST(enterprise_retail, search_predict_and_recommendations) {
|
||||
RetailNames n;
|
||||
n.project = "p1";
|
||||
ASSERT_EQ(n.placement("recently_viewed"),
|
||||
"projects/p1/locations/global/catalogs/default_catalog/placements/recently_viewed");
|
||||
ASSERT_EQ(n.branch(), "projects/p1/locations/global/catalogs/default_catalog/branches/0");
|
||||
RetailSearch s;
|
||||
s.placement = n.placement("default_search");
|
||||
s.query = "shoes";
|
||||
s.visitor_id = "v1";
|
||||
Invocation i = Retail::search_invocation(s);
|
||||
ASSERT_EQ(i.rpc, "retail.projects.locations.catalogs.placements.search");
|
||||
ASSERT_EQ(i.body.find("visitorId")->as_string(), "v1");
|
||||
s.placement = n.serving_config("default_search");
|
||||
ASSERT_EQ(Retail::search_invocation(s).rpc, "retail.projects.locations.catalogs.servingConfigs.search");
|
||||
s.visitor_id = "";
|
||||
REJECTS(s.to_body());
|
||||
Json ev = Json::object();
|
||||
ev.set("eventType", Json("detail-page-view"));
|
||||
ev.set("visitorId", Json("v1"));
|
||||
Invocation pi = Retail::predict_invocation(n.placement("p"), ev, 5, true);
|
||||
ASSERT_TRUE(pi.body.find("validateOnly")->as_bool());
|
||||
REJECTS(Retail::predict_invocation(n.placement("p"), ev, 0, false));
|
||||
REJECTS(Retail::predict_invocation(n.placement("p"), Json(), 5, false));
|
||||
|
||||
std::string rp = Recommendations::placement("p1", "global", "default_catalog", "default_event_store", "pl");
|
||||
Invocation ri = Recommendations::predict_invocation(rp, ev, 5, true);
|
||||
ASSERT_TRUE(ri.body.find("dryRun")->as_bool());
|
||||
Caller c(no_credential(), Api::kRecommendations);
|
||||
ASSERT_EQ(c.prepare(ri).url(), "https://recommendationengine.googleapis.com/v1beta1/" + rp + ":predict");
|
||||
REJECTS(Recommendations::placement("p1", "global", "c", "", "pl"));
|
||||
}
|
||||
|
||||
TEST(enterprise_retail, every_method_passes_validation) {
|
||||
Retail r(no_credential());
|
||||
RetailNames n;
|
||||
n.project = "p1";
|
||||
Json ev = Json::object();
|
||||
ev.set("eventType", Json("home-page-view"));
|
||||
ev.set("visitorId", Json("v1"));
|
||||
RetailSearch s;
|
||||
s.placement = n.placement("default_search");
|
||||
s.query = "x";
|
||||
s.visitor_id = "v";
|
||||
PASSES(r.search(s));
|
||||
PASSES(r.predict(n.placement("p"), ev));
|
||||
PASSES(r.write_user_event(n.catalog_name(), ev));
|
||||
PASSES(r.list_catalogs("projects/p1/locations/global"));
|
||||
PASSES(r.list_products(n.branch()));
|
||||
PASSES(r.get_product(n.branch() + "/products/sku1"));
|
||||
PASSES(r.list_serving_configs(n.catalog_name()));
|
||||
PASSES(r.delete_product(n.branch() + "/products/sku1", true));
|
||||
REJECTS(r.delete_product(n.branch() + "/products/sku1", false));
|
||||
Recommendations rec(no_credential());
|
||||
const std::string store = n.catalog_name() + "/eventStores/default_event_store";
|
||||
PASSES(rec.predict(store + "/placements/pl", ev));
|
||||
PASSES(rec.list_catalogs("projects/p1/locations/global"));
|
||||
PASSES(rec.list_catalog_items(n.catalog_name()));
|
||||
PASSES(rec.list_user_events(store));
|
||||
PASSES(rec.write_user_event(store, recs_event()));
|
||||
}
|
||||
|
||||
TEST(enterprise_cc, insights_and_ces) {
|
||||
Insights in(no_credential());
|
||||
const std::string loc = Insights::location_name("p1", "us-central1");
|
||||
ASSERT_EQ(loc, "projects/p1/locations/us-central1");
|
||||
PASSES(in.list_conversations(loc, "agent_id = \"a\""));
|
||||
PASSES(in.get_conversation(loc + "/conversations/c1"));
|
||||
PASSES(in.calculate_stats(loc));
|
||||
PASSES(in.list_issue_models(loc));
|
||||
PASSES(in.list_phrase_matchers(loc));
|
||||
PASSES(in.list_analysis_rules(loc));
|
||||
PASSES(in.list_views(loc));
|
||||
PASSES(in.analyze(loc + "/conversations/c1"));
|
||||
REJECTS(Insights::location_name("p1", "us/central"));
|
||||
|
||||
Ces ces(no_credential());
|
||||
const std::string app = "projects/p1/locations/us/apps/a1";
|
||||
PASSES(ces.list_apps("projects/p1/locations/us"));
|
||||
PASSES(ces.get_app(app));
|
||||
PASSES(ces.list_agents(app));
|
||||
PASSES(ces.list_tools(app));
|
||||
PASSES(ces.list_examples(app));
|
||||
PASSES(ces.list_guardrails(app));
|
||||
PASSES(ces.list_conversations(app));
|
||||
PASSES(ces.run_session(Ces::session_name("p1", "us", "a1", "s1"), "hello"));
|
||||
Invocation rs = Ces::run_session_invocation(Ces::session_name("p1", "us", "a1", "s1"), "hello");
|
||||
Caller c(no_credential(), Api::kCes);
|
||||
ASSERT_EQ(c.prepare(rs).url(),
|
||||
"https://ces.us.rep.googleapis.com/v1/projects/p1/locations/us/apps/a1/sessions/s1:runSession");
|
||||
ASSERT_EQ(rs.body.find("inputs")->as_array()[0].find("text")->as_string(), "hello");
|
||||
REJECTS(Ces::run_session_invocation(Ces::session_name("p1", "us", "a1", "s1"), ""));
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
// Opt-in live checks for the enterprise package. Read-only lists and gets
|
||||
// only: nothing is created, updated, deleted, sent or billed beyond a list
|
||||
// call. Run with a service-account token:
|
||||
//
|
||||
// export GOOGLE_ACCESS_TOKEN=$(./build/google/sa-token)
|
||||
// MOLO_LIVE_NET=1 GOOGLE_CLOUD_PROJECT=lisa-448004 make google-test
|
||||
//
|
||||
// An API that is not enabled on the project answers 403 and is reported
|
||||
// loudly as "API NOT ENABLED"; the test then passes (enabling an API is a
|
||||
// human decision). Any other failure fails the test. A 404 NOT_FOUND on a
|
||||
// resource that does not exist proves the path and auth were accepted.
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#include "framework.h"
|
||||
#include "google/credentials.h"
|
||||
#include "google/enterprise/contact_center.h"
|
||||
#include "google/enterprise/dialogflow.h"
|
||||
#include "google/enterprise/discovery_engine.h"
|
||||
#include "google/enterprise/retail.h"
|
||||
|
||||
using namespace molo::google;
|
||||
using namespace molo::google::enterprise;
|
||||
|
||||
namespace {
|
||||
bool live_ready(const char* test) {
|
||||
if (!std::getenv("MOLO_LIVE_NET") || Credentials::env("GOOGLE_CLOUD_PROJECT").empty() ||
|
||||
Credentials::env("GOOGLE_ACCESS_TOKEN").empty()) {
|
||||
std::cout << "SKIP " << test
|
||||
<< " (set MOLO_LIVE_NET=1, GOOGLE_CLOUD_PROJECT and GOOGLE_ACCESS_TOKEN)\n";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string project() { return Credentials::env("GOOGLE_CLOUD_PROJECT"); }
|
||||
|
||||
// Runs a read. Prints what happened; fails only on an unexpected error.
|
||||
template <typename F>
|
||||
void expect_read(const char* what, F&& f) {
|
||||
try {
|
||||
f();
|
||||
std::cout << " OK " << what << "\n";
|
||||
} catch (const GoogleError& e) {
|
||||
const std::string msg = e.message();
|
||||
const bool disabled = e.http_status() == 403 && (msg.find("has not been used") != std::string::npos ||
|
||||
msg.find("is disabled") != std::string::npos ||
|
||||
e.status() == "SERVICE_DISABLED");
|
||||
if (disabled) {
|
||||
std::cout << " API NOT ENABLED: " << what << " (enable it in the console, a human decision)\n";
|
||||
return;
|
||||
}
|
||||
if (e.http_status() == 404 || e.status() == "NOT_FOUND") {
|
||||
std::cout << " OK (404, path and auth accepted, no such resource) " << what << "\n";
|
||||
return;
|
||||
}
|
||||
std::cout << " UNEXPECTED " << what << ": HTTP " << e.http_status() << " " << e.status() << ": "
|
||||
<< msg.substr(0, 300) << "\n";
|
||||
throw;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST(enterprise_live, discovery_engine_reads) {
|
||||
if (!live_ready("enterprise_live.discovery_engine_reads")) return;
|
||||
DiscoveryEngine de(GoogleClient::from_env());
|
||||
DeNames n;
|
||||
n.project = project();
|
||||
const std::string coll = n.parent() + "/collections/default_collection";
|
||||
expect_read("discoveryengine dataStores.list", [&] {
|
||||
ListResult r = de.list_data_stores(coll);
|
||||
std::cout << " data stores: " << r.size() << (r.truncated ? " (TRUNCATED)" : "") << "\n";
|
||||
});
|
||||
expect_read("discoveryengine engines.list", [&] {
|
||||
ListResult r = de.list_engines(coll);
|
||||
std::cout << " engines: " << r.size() << (r.truncated ? " (TRUNCATED)" : "") << "\n";
|
||||
});
|
||||
}
|
||||
|
||||
TEST(enterprise_live, dialogflow_reads) {
|
||||
if (!live_ready("enterprise_live.dialogflow_reads")) return;
|
||||
DialogflowEs es(GoogleClient::from_env());
|
||||
expect_read("dialogflow ES projects.getAgent", [&] { es.get_agent("projects/" + project()); });
|
||||
DialogflowCx cx(GoogleClient::from_env());
|
||||
expect_read("dialogflow CX agents.list (global)", [&] {
|
||||
ListResult r = cx.list_agents("projects/" + project() + "/locations/global");
|
||||
std::cout << " CX agents: " << r.size() << "\n";
|
||||
});
|
||||
}
|
||||
|
||||
TEST(enterprise_live, retail_and_recommendations_reads) {
|
||||
if (!live_ready("enterprise_live.retail_and_recommendations_reads")) return;
|
||||
Retail retail(GoogleClient::from_env());
|
||||
expect_read("retail catalogs.list", [&] {
|
||||
ListResult r = retail.list_catalogs("projects/" + project() + "/locations/global");
|
||||
std::cout << " catalogs: " << r.size() << "\n";
|
||||
});
|
||||
Recommendations rec(GoogleClient::from_env());
|
||||
expect_read("recommendationengine catalogs.list", [&] {
|
||||
ListResult r = rec.list_catalogs("projects/" + project() + "/locations/global");
|
||||
std::cout << " legacy catalogs: " << r.size() << "\n";
|
||||
});
|
||||
}
|
||||
|
||||
TEST(enterprise_live, insights_and_ces_reads) {
|
||||
if (!live_ready("enterprise_live.insights_and_ces_reads")) return;
|
||||
Insights in(GoogleClient::from_env());
|
||||
expect_read("contactcenterinsights conversations.list", [&] {
|
||||
ListResult r = in.list_conversations(Insights::location_name(project(), "us-central1"));
|
||||
std::cout << " conversations: " << r.size() << "\n";
|
||||
});
|
||||
Ces ces(GoogleClient::from_env());
|
||||
expect_read("ces apps.list", [&] {
|
||||
ListResult r = ces.list_apps("projects/" + project() + "/locations/us");
|
||||
std::cout << " CES apps: " << r.size() << "\n";
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user