Files
moloapp/tests/example/ai/xai/platform/routes_test.cpp
T

65 lines
3.4 KiB
C++

// Routes: POST /ai/xai/platform/{account,files,collections,search,voices,tts,stt,
// realtime/secret,realtime/session,realtime/phone-numbers,realtime/calls,keys,
// billing,audit}
// Without a key a well formed request is 503 not_configured; bad input is 422
// and never reaches xAI; GET is 405. Nothing here touches the network.
#include <cstdlib>
#include "support/Assert.hpp"
using namespace molo::example::test;
namespace {
struct Route {
const char* path;
const char* valid;
const char* bad;
};
const char* kTeam = "5ea6f6bd-7815-4b8a-9135-28b2d7ba6722";
} // namespace
int main() {
unsetenv("XAI_API_KEY");
unsetenv("XAI_MANAGEMENT_KEY");
Fixture f;
const std::string team = std::string("\"team_id\":\"") + kTeam + "\"";
const std::string billing_ok = "{\"action\":\"prepaid_balance\"," + team + "}";
const std::string audit_ok = "{" + team + "}";
const Route routes[] = {
{"/ai/xai/platform/account", "{\"action\":\"api_key\"}", "{\"action\":\"x\"}"},
{"/ai/xai/platform/files", "{\"action\":\"list\"}", "{\"action\":\"get\"}"},
{"/ai/xai/platform/collections", "{\"action\":\"list\"}", "{\"action\":\"get\"}"},
{"/ai/xai/platform/search", "{\"query\":\"q\",\"collection_ids\":[\"collection_x\"]}", "{\"query\":\"q\"}"},
{"/ai/xai/platform/voices", "{\"action\":\"list\"}", "{\"action\":\"get\"}"},
{"/ai/xai/platform/tts", "{\"text\":\"hi\",\"confirm\":true}", "{\"text\":\"\"}"},
{"/ai/xai/platform/stt", "{\"url\":\"https://example.com/a.mp3\",\"confirm\":true}", "{\"confirm\":true}"},
{"/ai/xai/platform/realtime/secret", "{\"confirm\":true}", "{\"expires_seconds\":9999}"},
{"/ai/xai/platform/realtime/session", "{\"text\":\"hi\",\"confirm\":true}", "{\"confirm\":true}"},
{"/ai/xai/platform/realtime/phone-numbers", "{\"action\":\"list\"}",
"{\"action\":\"create\",\"name\":\"n\",\"phone_number\":\"123\"}"},
{"/ai/xai/platform/realtime/calls", "{\"action\":\"hangup\",\"call_id\":\"c1\",\"confirm\":true}",
"{\"action\":\"dial\",\"call_id\":\"c1\"}"},
{"/ai/xai/platform/keys", "{\"action\":\"validate\"}", "{\"action\":\"list\",\"team_id\":\"x\"}"},
{"/ai/xai/platform/billing", billing_ok.c_str(), "{\"action\":\"prepaid_balance\",\"team_id\":\"x\"}"},
{"/ai/xai/platform/audit", audit_ok.c_str(), "{\"team_id\":\"x\"}"},
};
for (const Route& r : routes) {
std::string p = r.path;
auto ok = f.client->post(p, r.valid, "application/json");
check(ok.status == 503 && contains(ok.body, "not_configured"), p + ": no key is 503 not_configured");
auto bad = f.client->post(p, r.bad, "application/json");
check(bad.status == 422 && contains(bad.body, "validation_error"), p + ": bad input is 422");
check(f.client->post(p, "{not json", "application/json").status == 422, p + ": broken json is 422");
check(f.client->post(p, "[1]", "application/json").status == 422, p + ": non-object json is 422");
check(f.client->get(p).status == 405, p + ": get is 405");
}
// The keys decide the host: the API key alone does not open management routes.
setenv("XAI_API_KEY", "k-test", 1);
Fixture g;
auto m = g.client->post("/ai/xai/platform/collections", "{\"action\":\"list\"}", "application/json");
check(m.status == 503 && contains(m.body, "XAI_MANAGEMENT_KEY"), "collections need the management key");
unsetenv("XAI_API_KEY");
return finish("ai/xai/platform/routes");
}