Merge openai admin package

This commit is contained in:
2026-10-24 07:43:33 +02:00
47 changed files with 5533 additions and 3 deletions
@@ -0,0 +1,27 @@
// Route: POST /ai/openai/admin/read (422 first, then 503 without a credential)
#include "support/Assert.hpp"
using namespace molo::example::test;
int main() {
unsetenv("OPENAI_ADMIN_KEY");
unsetenv("OPENAI_API_KEY");
Fixture f;
auto post = [&](const std::string& b) {
return f.client->post("/ai/openai/admin/read", b, "application/json");
};
auto ok = post("{\"call\":\"users.list\",\"query\":{\"limit\":5}}");
check(ok.status == 503 && contains(ok.body, "not_configured"), "valid read without key is 503");
check(post("{\"call\":\"webhook_event_types.list\"}").status == 503, "project-key call without key is 503");
check(post("{}").status == 422, "missing call is 422");
check(post("{\"call\":\"nope.list\"}").status == 422, "unknown call is 422");
check(post("{\"call\":\"users.update\",\"ids\":[\"u\"]}").status == 422, "write via read is 422");
check(post("{\"call\":\"users.get\"}").status == 422, "missing id is 422");
check(post("{\"call\":\"users.get\",\"ids\":[\"../x\"]}").status == 422, "bad id is 422");
check(post("{\"call\":\"users.list\",\"query\":{\"nope\":1}}").status == 422, "foreign query name is 422");
check(post("{\"call\":\"users.list\",\"region\":\"zz\"}").status == 422, "bad region is 422");
check(post("[1]").status == 422, "non-object body is 422");
check(post("{bad").status == 422, "bad json is 422");
check(f.client->get("/ai/openai/admin/read").status == 405, "get is 405");
return finish("ai/openai/admin/read");
}
@@ -0,0 +1,26 @@
// Route: GET /ai/openai/admin, /status, /calls, /audit-events, /webhook-events (no credential needed)
#include "support/Assert.hpp"
using namespace molo::example::test;
int main() {
unsetenv("OPENAI_ADMIN_KEY");
unsetenv("OPENAI_API_KEY");
Fixture f;
auto page = f.client->get("/ai/openai/admin");
check(page.status == 200 && contains(page.body, "OpenAI administration"), "page 200");
check(contains(page.body, "/embedded/ai_openai_admin.js"), "page loads its script");
auto st = f.client->get("/ai/openai/admin/status");
check(st.status == 200 && contains(st.body, "\"configured\":false"), "status without key");
check(contains(st.body, "\"calls\":121"), "status counts the table");
auto calls = f.client->get("/ai/openai/admin/calls");
check(calls.status == 200 && contains(calls.body, "projects.service_accounts.api_keys.create"), "call table listed");
check(contains(calls.body, "\"destructive\":true"), "destructive rows flagged");
auto audit = f.client->get("/ai/openai/admin/audit-events");
check(audit.status == 200 && contains(audit.body, "project.archived"), "audit catalog");
auto hooks = f.client->get("/ai/openai/admin/webhook-events");
check(hooks.status == 200 && contains(hooks.body, "response.completed"), "webhook catalog");
check(f.client->post("/ai/openai/admin/status", "{}", "application/json").status == 405, "post status is 405");
check(contains(f.client->get("/").body, "/ai/openai/admin"), "matrix row for the page");
return finish("ai/openai/admin/status");
}
@@ -0,0 +1,27 @@
// Route: POST /ai/openai/admin/usage, /costs, /audit
#include "support/Assert.hpp"
using namespace molo::example::test;
int main() {
unsetenv("OPENAI_ADMIN_KEY");
unsetenv("OPENAI_API_KEY");
Fixture f;
auto p = [&](const char* path, const std::string& b) {
return f.client->post(std::string("/ai/openai/admin/") + path, b, "application/json");
};
check(p("usage", "{\"kind\":\"completions\",\"start_time\":1730419200}").status == 503, "usage without key is 503");
check(p("usage", "{\"kind\":\"completions\"}").status == 422, "missing start_time is 422");
check(p("usage", "{\"kind\":\"nope\",\"start_time\":1}").status == 422, "unknown kind is 422");
check(p("usage", "{\"kind\":\"completions\",\"start_time\":1,\"limit\":999}").status == 422, "limit over cap is 422");
check(p("usage", "{\"kind\":\"completions\",\"start_time\":1,\"sizes\":[\"1024x1024\"]}").status == 422, "foreign filter is 422");
check(p("usage", "{\"kind\":\"images\",\"start_time\":1,\"group_by\":[\"size\"],\"walk\":true}").status == 503, "walk without key is 503");
check(p("costs", "{\"start_time\":1730419200,\"limit\":7}").status == 503, "costs without key is 503");
check(p("costs", "{\"start_time\":1730419200,\"limit\":181}").status == 422, "costs limit over 180 is 422");
check(p("costs", "{\"start_time\":1,\"bucket_width\":\"1h\"}").status == 422, "costs are daily only");
check(p("audit", "{\"limit\":20,\"event_types\":[\"project.created\"]}").status == 503, "audit without key is 503");
check(p("audit", "{\"event_types\":[\"project.exploded\"]}").status == 422, "unknown event type is 422");
check(p("audit", "{\"after\":\"a\",\"before\":\"b\"}").status == 422, "after and before together is 422");
check(f.client->get("/ai/openai/admin/usage").status == 405, "get is 405");
return finish("ai/openai/admin/usage");
}
@@ -0,0 +1,29 @@
// Route: POST /ai/openai/admin/webhook/verify (local, no credential)
#include "support/Assert.hpp"
using namespace molo::example::test;
int main() {
Fixture f;
auto v = [&](const std::string& b) {
return f.client->post("/ai/openai/admin/webhook/verify", b, "application/json");
};
// Standard Webhooks reference vector; signature recomputed independently with python3 hmac.
const std::string head = "{\"secret\":\"whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw\",\"now\":";
const std::string tail =
",\"headers\":{\"Webhook-Id\":\"msg_p5jXN8AQM9LWM0D4loKWxJek\",\"Webhook-Timestamp\":\"1614265330\","
"\"Webhook-Signature\":\"v1,g0hM9SsE+OTPJTGt/tmIKtSyZlE3uFJELVlNIOLJ1OE=\"},";
auto ok = v(head + "1614265330" + tail + "\"body\":\"{\\\"test\\\": 2432232314}\"}");
check(ok.status == 200 && contains(ok.body, "\"ok\":true"), "reference vector verifies");
check(contains(ok.body, "event_error"), "non-event body is reported, not fatal");
auto old = v(head + "1614266000" + tail + "\"body\":\"{\\\"test\\\": 2432232314}\"}");
check(old.status == 200 && contains(old.body, "timestamp_too_old"), "stale timestamp rejected");
auto tampered = v(head + "1614265330" + tail + "\"body\":\"{\\\"test\\\": 2432232315}\"}");
check(tampered.status == 200 && contains(tampered.body, "signature_mismatch"), "tampered body rejected");
check(v("{\"headers\":{},\"body\":\"x\"}").status == 422, "missing secret is 422");
check(v("{\"secret\":\"whsec_!!\",\"headers\":{},\"body\":\"x\"}").status == 422, "bad secret is 422");
check(v("{\"secret\":\"whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw\",\"body\":\"x\"}").status == 422, "missing headers is 422");
check(contains(f.client->get("/ai/openai/admin/status").body, "\"signature_mismatch\":1"), "counters exposed");
check(f.client->get("/ai/openai/admin/webhook/verify").status == 405, "get is 405");
return finish("ai/openai/admin/webhook_verify");
}
@@ -0,0 +1,31 @@
// Route: POST /ai/openai/admin/write and /destroy (destructive calls are dry runs unless confirm:true)
#include "support/Assert.hpp"
using namespace molo::example::test;
int main() {
unsetenv("OPENAI_ADMIN_KEY");
unsetenv("OPENAI_API_KEY");
Fixture f;
auto w = [&](const std::string& b) { return f.client->post("/ai/openai/admin/write", b, "application/json"); };
auto d = [&](const std::string& b) { return f.client->post("/ai/openai/admin/destroy", b, "application/json"); };
check(w("{\"call\":\"invites.create\",\"body\":{\"email\":\"a@example.com\",\"role\":\"reader\"}}").status == 503,
"valid write without key is 503");
check(w("{\"call\":\"invites.create\",\"body\":{\"email\":\"a@example.com\"}}").status == 422, "missing required field is 422");
check(w("{\"call\":\"invites.create\",\"body\":{\"email\":\"a@example.com\",\"role\":\"reader\",\"x\":1}}").status == 422,
"foreign body field is 422");
check(w("{\"call\":\"users.delete\",\"ids\":[\"u\"]}").status == 422, "destructive via write is 422");
check(w("{\"call\":\"users.list\"}").status == 422, "read via write is 422");
auto dry = d("{\"call\":\"users.delete\",\"ids\":[\"user_1\"]}");
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true"), "default is a dry run");
check(contains(dry.body, "DELETE /organization/users/user_1"), "dry run names the call");
auto arch = d("{\"call\":\"projects.archive\",\"ids\":[\"proj_1\"]}");
check(arch.status == 200 && contains(arch.body, "dry_run"), "archive is a dry run");
auto real = d("{\"call\":\"users.delete\",\"ids\":[\"user_1\"],\"confirm\":true}");
check(real.status == 503 && contains(real.body, "not_configured"), "confirmed delete without key is 503");
check(d("{\"call\":\"users.get\",\"ids\":[\"u\"]}").status == 422, "non-destructive call on destroy is 422");
check(d("{\"call\":\"users.delete\",\"confirm\":\"yes\"}").status == 422, "non-bool confirm is 422");
check(d("{\"call\":\"users.delete\"}").status == 422, "dry run still needs the id");
check(f.client->get("/ai/openai/admin/destroy").status == 405, "get is 405");
return finish("ai/openai/admin/write");
}
+480
View File
@@ -0,0 +1,480 @@
// OpenAI organization administration: call table, credential kinds,
// destructive guards, request construction, query validation, paging
// ceilings, webhook verification and event envelopes. Local and
// deterministic; the mock transport only records the requests we send.
#include <cstdlib>
#include <iostream>
#include <memory>
#include "framework.h"
#include "clients/transports/mock_transport.h"
#include "clients/transports/http_transport.h"
#include "openai/admin/caps.h"
#include "openai/admin/openai_admin.h"
using namespace molo::openai::admin;
using molo::clients::ClientResponse;
using molo::clients::HeaderMap;
using molo::clients::Method;
using molo::clients::body::Json;
using molo::clients::transports::MockTransport;
using molo::llm::ApiError;
using molo::llm::NotConfiguredError;
namespace {
ClientResponse ok_json(const std::string& body = "{}") {
HeaderMap h;
h.set("Content-Type", "application/json");
return ClientResponse(200, h, body);
}
struct Rig {
MockTransport* mock;
std::shared_ptr<AdminApiClient> client;
explicit Rig(const std::string& key = "sk-admin-TESTKEY") {
auto t = std::make_unique<MockTransport>();
mock = t.get();
client = AdminApiClient::with_transport(std::move(t), key);
}
void accept(Method m, const std::string& path) {
molo::clients::transports::MockExpectation ex;
ex.method = m;
ex.path = path;
ex.response = ok_json();
ex.times = 1000;
mock->expect(ex);
}
MockTransport::SeenRequest last() { return mock->seen().back(); }
};
bool throws_invalid(const std::function<void()>& f) {
try {
f();
} catch (const ApiError& e) {
return e.http_status() == 400 && e.type() == "INVALID_ARGUMENT";
}
return false;
}
} // namespace
TEST(openai_admin, table_is_ordered_complete_and_renders_every_path) {
ASSERT_EQ(CallTable::size(), 121u);
int project_calls = 0;
for (std::size_t i = 0; i < CallTable::size(); ++i) {
const CallSpec& s = CallTable::at(i);
ASSERT_EQ(static_cast<std::size_t>(s.call), i);
std::vector<std::string> ids(CallTable::path_params(s.call).size(), "id1");
std::string path = CallTable::render(s.call, ids);
ASSERT_TRUE(path.find('{') == std::string::npos);
if (CallTable::auth(s.call) == AuthKind::kProject) ++project_calls;
if (std::string(s.http_method) == "DELETE") ASSERT_TRUE(CallTable::destructive(s.call));
}
ASSERT_EQ(project_calls, 8); // /webhook_endpoints* and /webhook_event_types
ASSERT_TRUE(CallTable::schema_count() > 80u);
ASSERT_EQ(AuditEventCatalog::all().size(), 149u);
ASSERT_EQ(WebhookEvents::sent().size(), 30u);
ASSERT_EQ(WebhookEvents::subscribable().size(), 27u);
}
TEST(openai_admin, render_rejects_bad_ids_and_wrong_arity) {
ASSERT_TRUE(throws_invalid([] { CallTable::render(Call::kUsersGet, {"../x"}); }));
ASSERT_TRUE(throws_invalid([] { CallTable::render(Call::kUsersGet, {"a/b"}); }));
ASSERT_TRUE(throws_invalid([] { CallTable::render(Call::kUsersGet, {}); }));
ASSERT_TRUE(throws_invalid([] { CallTable::render(Call::kUsersGet, {"a", "b"}); }));
ASSERT_EQ(CallTable::render(Call::kProjectsUsersGet, {"proj_1", "user-2"}),
"/organization/projects/proj_1/users/user-2");
}
TEST(openai_admin, only_admin_keys_reach_the_organization_api) {
for (const char* key : {"sk-proj-abc", "sk-abc", "sk-svcacct-x"}) {
try {
Rig r(key);
FAIL("regular key accepted");
} catch (const ApiError& e) {
ASSERT_EQ(e.http_status(), 401);
ASSERT_EQ(e.type(), "WRONG_KEY_KIND");
ASSERT_EQ(e.message().find(key), std::string::npos);
}
}
Rig good;
ASSERT_TRUE(good.client->configured());
Rig none("");
ASSERT_FALSE(none.client->configured());
Users users(none.client);
ASSERT_THROW(users.list(), NotConfiguredError);
ASSERT_EQ(none.mock->send_count(), 0u);
// The webhook endpoint client is the mirror image: no admin keys.
ASSERT_THROW(ProjectApiClient::with_transport(std::make_unique<MockTransport>(), "sk-admin-x"),
ApiError);
}
TEST(openai_admin, bearer_header_region_and_base_url) {
Rig r;
r.accept(Method::kGet, "/v1/organization/users");
Users(r.client).list();
ASSERT_EQ(r.last().headers.get_or("Authorization", ""), "Bearer sk-admin-TESTKEY");
ASSERT_EQ(Regions::base_url(Region::kEu), "https://eu.api.openai.com/v1");
ASSERT_EQ(Regions::base_url(Region::kGlobal), "https://api.openai.com/v1");
ASSERT_TRUE(Regions::parse("JP").has_value());
ASSERT_FALSE(Regions::parse("zz").has_value());
ASSERT_TRUE(*Regions::from_residency("EU_STORAGE_PROCESSING") == Region::kEu);
ASSERT_FALSE(Regions::from_residency("MOON").has_value());
}
TEST(openai_admin, service_refuses_a_call_of_the_other_credential_kind) {
Rig r;
WebhookEndpoints wrong(
ProjectApiClient::with_transport(std::make_unique<MockTransport>(), "sk-proj-1"));
ASSERT_TRUE(throws_invalid([&] { wrong.list(); }) == false); // fine: project call
// An admin service cannot run a project call: probe through Users with a webhook row.
struct Probe : AdminService {
explicit Probe(std::shared_ptr<AdminApiClient> c) : AdminService(std::move(c)) {}
Json run() { return execute(Call::kWebhookEndpointsList); }
} probe(r.client);
ASSERT_TRUE(throws_invalid([&] { probe.run(); }));
ASSERT_EQ(r.mock->send_count(), 0u);
}
TEST(openai_admin, destructive_calls_need_confirm_and_send_nothing_without_it) {
Rig r;
r.accept(Method::kDelete, "/v1/organization/users/user_1");
r.accept(Method::kPost, "/v1/organization/projects/proj_1/archive");
Users users(r.client);
Projects projects(r.client);
ASSERT_TRUE(throws_invalid([&] { users.destroy("user_1", false); }));
ASSERT_TRUE(throws_invalid([&] { projects.archive("proj_1", false); }));
ASSERT_EQ(r.mock->send_count(), 0u);
users.destroy("user_1", true);
ASSERT_EQ(r.last().method, "DELETE");
projects.archive("proj_1", true);
ASSERT_EQ(r.last().method, "POST");
ASSERT_TRUE(r.last().target.find("/organization/projects/proj_1/archive") != std::string::npos);
// retention change and certificate deactivation are guarded too
ProjectPolicies pol(r.client);
ASSERT_TRUE(throws_invalid([&] { pol.set_data_retention("proj_1", "none", false); }));
Certificates certs(r.client);
ASSERT_TRUE(throws_invalid([&] { certs.deactivate({"cert_1"}, false); }));
OrganizationPolicies org(r.client);
ASSERT_TRUE(throws_invalid([&] { org.set_data_retention("zero_data_retention", false); }));
}
TEST(openai_admin, request_bodies_follow_the_table) {
Rig r;
r.accept(Method::kPost, "/v1/organization/invites");
r.accept(Method::kPost, "/v1/organization/projects");
r.accept(Method::kPost, "/v1/organization/projects/proj_1/rate_limits/rl-x");
Invites(r.client).create({"a@example.com", "reader", {{"proj_1", "member"}}});
ASSERT_EQ(r.last().body,
"{\"email\":\"a@example.com\",\"projects\":[{\"id\":\"proj_1\",\"role\":\"member\"}],"
"\"role\":\"reader\"}");
ProjectCreate pc;
pc.name = "Prod";
pc.residency = "EU_STORAGE_PROCESSING";
Projects(r.client).create(pc);
ASSERT_EQ(r.last().body, "{\"name\":\"Prod\",\"residency\":\"EU_STORAGE_PROCESSING\"}");
RateLimitUpdate rl;
rl.max_requests_per_1_minute = 100;
RateLimits(r.client).update("proj_1", "rl-x", rl);
ASSERT_EQ(r.last().body, "{\"max_requests_per_1_minute\":100}");
}
TEST(openai_admin, request_validation_rejects_before_traffic) {
Rig r;
Invites inv(r.client);
ASSERT_TRUE(throws_invalid([&] { inv.create({"not-an-email", "reader", {}}); }));
ASSERT_TRUE(throws_invalid([&] { inv.create({"a@b.co", "admin", {}}); }));
ProjectCreate bad;
bad.name = "x";
bad.residency = "MOON";
ASSERT_TRUE(throws_invalid([&] { Projects(r.client).create(bad); }));
ASSERT_TRUE(throws_invalid([&] { Projects(r.client).update("p", ProjectUpdate{}); }));
ProjectUserCreate both{"u1", "a@b.co", "owner"};
ASSERT_TRUE(throws_invalid([&] { both.to_json(); }));
ASSERT_TRUE(throws_invalid([&] { AdminApiKeys(r.client).create("k", 31536001); }));
ASSERT_TRUE(throws_invalid([&] { CertificateUpload{"", "-----BEGIN PRIVATE KEY-----"}.to_json(); }));
ASSERT_TRUE(throws_invalid([&] { SpendAlertSpec{0, {"a@b.co"}, ""}.to_json(); }));
ASSERT_TRUE(throws_invalid([&] { (ListOptions{0, "", "", ""}).to_query(); }));
ASSERT_TRUE(throws_invalid([&] { (ListOptions{101, "", "", ""}).to_query(); }));
ASSERT_TRUE(throws_invalid([&] { (ListOptions{std::nullopt, "", "", "up"}).to_query(); }));
// users.list has no `order` parameter in the table
ASSERT_TRUE(throws_invalid([&] { Users(r.client).list(ListOptions{std::nullopt, "", "", "asc"}); }));
ASSERT_EQ(r.mock->send_count(), 0u);
}
TEST(openai_admin, usage_queries_are_checked_per_endpoint) {
Rig r;
r.accept(Method::kGet, "/v1/organization/usage/completions");
r.accept(Method::kGet, "/v1/organization/costs");
Usage usage(r.client);
UsageQuery q;
q.start_time = 1730419200;
q.bucket_width = "1h";
q.limit = 24;
q.project_ids = {"proj_a", "proj_b"};
q.group_by = {"model", "project_id"};
q.batch = false;
usage.query(UsageKind::kCompletions, q);
const std::string target = r.last().target;
ASSERT_TRUE(target.find("start_time=1730419200") != std::string::npos);
ASSERT_TRUE(target.find("project_ids=proj_a&project_ids=proj_b") != std::string::npos);
ASSERT_TRUE(target.find("group_by=model&group_by=project_id") != std::string::npos);
ASSERT_TRUE(target.find("batch=false") != std::string::npos);
UsageQuery bad = q;
bad.limit = 169; // 1h max is 168
ASSERT_TRUE(throws_invalid([&] { usage.query(UsageKind::kCompletions, bad); }));
bad = q;
bad.sizes = {"1024x1024"}; // images only
ASSERT_TRUE(throws_invalid([&] { usage.query(UsageKind::kCompletions, bad); }));
bad = q;
bad.group_by = {"size"}; // images only
ASSERT_TRUE(throws_invalid([&] { usage.query(UsageKind::kCompletions, bad); }));
bad = q;
bad.start_time = 0;
ASSERT_TRUE(throws_invalid([&] { usage.query(UsageKind::kCompletions, bad); }));
bad = q;
bad.end_time = 5;
ASSERT_TRUE(throws_invalid([&] { usage.query(UsageKind::kCompletions, bad); }));
CostQuery c;
c.start_time = 1730419200;
c.limit = 180;
c.line_items = {"gpt, input"};
Costs(r.client).query(c);
ASSERT_TRUE(r.last().target.find("line_items=gpt%2C%20input") != std::string::npos);
c.limit = 181;
ASSERT_TRUE(throws_invalid([&] { Costs(r.client).query(c); }));
c.limit = 7;
c.bucket_width = "1h"; // costs are daily only
ASSERT_TRUE(throws_invalid([&] { Costs(r.client).query(c); }));
ASSERT_EQ(UsageEndpoints::all().size(), 11u);
}
TEST(openai_admin, audit_queries_use_spec_names_and_the_catalog) {
Rig r;
r.accept(Method::kGet, "/v1/organization/audit_logs");
AuditQuery q;
q.event_types = {"project.created", "api_key.deleted"};
q.project_ids = {"proj_1"};
q.effective_at_gte = 1730419200;
q.limit = 100;
q.tenant_only = true;
AuditLogs(r.client).list(q);
const std::string t = r.last().target;
ASSERT_TRUE(t.find("event_types%5B%5D=project.created&event_types%5B%5D=api_key.deleted") != std::string::npos);
ASSERT_TRUE(t.find("effective_at%5Bgte%5D=1730419200") != std::string::npos);
ASSERT_TRUE(t.find("tenant_only=true") != std::string::npos);
AuditQuery bad;
bad.event_types = {"project.exploded"};
ASSERT_TRUE(throws_invalid([&] { AuditLogs(r.client).list(bad); }));
bad = AuditQuery{};
bad.after = "a";
bad.before = "b";
ASSERT_TRUE(throws_invalid([&] { AuditLogs(r.client).list(bad); }));
ASSERT_TRUE(AuditEventCatalog::known("tenant.user.added"));
ASSERT_EQ(AuditEventCatalog::family("rate_limit.updated"), "rate_limit");
}
TEST(openai_admin, walks_stop_at_ceilings_and_say_so) {
int calls = 0;
WalkResult r = PageWalker::walk([&](const std::string& cursor) {
++calls;
Json page = Json::object();
Json data = Json::array();
data.push(Json(cursor.empty() ? "first" : cursor));
page.set("data", data);
page.set("has_more", Json(true));
page.set("last_id", Json("id" + std::to_string(calls)));
return page;
});
ASSERT_TRUE(r.truncated);
ASSERT_EQ(r.reason, "page ceiling");
ASSERT_EQ(r.pages, AdminCaps::kMaxListPages);
ASSERT_TRUE(r.to_json().find("truncated_reason") != nullptr);
int n = 0;
WalkResult done = PageWalker::walk([&](const std::string&) {
Json page = Json::object();
page.set("data", Json::array());
page.set("has_more", Json(++n < 3));
page.set("next_page", Json("tok" + std::to_string(n)));
return page;
});
ASSERT_FALSE(done.truncated);
ASSERT_EQ(done.pages, 3);
ASSERT_THROW(PageWalker::walk([](const std::string&) { return Json::object(); }), ApiError);
}
TEST(openai_admin, usage_summary_adds_up_buckets) {
// Shape from the OpenAPI document: UsageResponse / UsageCompletionsResult / CostsResult.
Json doc = Json::parse(
"{\"object\":\"page\",\"has_more\":false,\"next_page\":null,\"data\":[{\"object\":\"bucket\","
"\"start_time\":1,\"end_time\":2,\"results\":[{\"object\":\"organization.usage.completions.result\","
"\"input_tokens\":10,\"output_tokens\":5,\"num_model_requests\":1},{\"object\":\"x\",\"input_tokens\":2,"
"\"output_tokens\":1,\"num_model_requests\":1}]}]}");
auto totals = UsageSummary::totals(doc);
ASSERT_EQ(totals["input_tokens"], 12.0);
ASSERT_EQ(totals["num_model_requests"], 2.0);
Json costs = Json::parse(
"{\"data\":[{\"results\":[{\"amount\":{\"value\":0.5,\"currency\":\"usd\"}},"
"{\"amount\":{\"value\":0.25,\"currency\":\"usd\"}}]}]}");
ASSERT_EQ(UsageSummary::cost_by_currency(costs)["usd"], 0.75);
}
namespace {
// Standard Webhooks reference vector (secret, id, timestamp, body) used by the
// libraries' own tests; the expected signature was recomputed independently
// with python3 hmac/hashlib (it is not copied from OpenAI).
const char* kSecret = "whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw";
const char* kId = "msg_p5jXN8AQM9LWM0D4loKWxJek";
const char* kTs = "1614265330";
const char* kBody = "{\"test\": 2432232314}";
const char* kSig = "v1,g0hM9SsE+OTPJTGt/tmIKtSyZlE3uFJELVlNIOLJ1OE=";
} // namespace
TEST(openai_admin, webhook_signature_known_vector) {
WebhookVerifier v(kSecret);
ASSERT_EQ(v.sign(kId, kTs, kBody), kSig);
WebhookHeaders h{kId, kTs, kSig};
ASSERT_TRUE(v.verify(kBody, h, 1614265330).ok);
ASSERT_TRUE(v.verify(kBody, h, 1614265330 + 300).ok); // edge of tolerance
ASSERT_TRUE(v.verify(kBody, h, 1614265330 - 300).ok);
}
TEST(openai_admin, webhook_rejections_are_specific_and_counted) {
WebhookVerifier v(kSecret);
WebhookHeaders h{kId, kTs, kSig};
const auto before = WebhookCounters::value(WebhookFailure::kMismatch);
ASSERT_TRUE(v.verify("{\"test\": 2432232315}", h, 1614265330).failure == WebhookFailure::kMismatch);
ASSERT_EQ(WebhookCounters::value(WebhookFailure::kMismatch), before + 1);
ASSERT_TRUE(v.verify(kBody, h, 1614265330 + 301).failure == WebhookFailure::kTooOld);
ASSERT_TRUE(v.verify(kBody, h, 1614265330 - 301).failure == WebhookFailure::kTooNew);
WebhookHeaders w = h;
w.id = "msg_other";
ASSERT_TRUE(v.verify(kBody, w, 1614265330).failure == WebhookFailure::kMismatch);
w = h;
w.timestamp = "";
ASSERT_TRUE(v.verify(kBody, w, 1614265330).failure == WebhookFailure::kMissingTimestamp);
w = h;
w.timestamp = "16x4";
ASSERT_TRUE(v.verify(kBody, w, 1614265330).failure == WebhookFailure::kBadTimestamp);
w = h;
w.signature = "";
ASSERT_TRUE(v.verify(kBody, w, 1614265330).failure == WebhookFailure::kMissingSignature);
w = h;
w.id = "";
ASSERT_TRUE(v.verify(kBody, w, 1614265330).failure == WebhookFailure::kMissingId);
// unprefixed and other-version entries are never accepted
w = h;
w.signature = std::string(kSig).substr(3);
ASSERT_TRUE(v.verify(kBody, w, 1614265330).failure == WebhookFailure::kBadSignatureHeader);
w.signature = "v1a," + std::string(kSig).substr(3);
ASSERT_TRUE(v.verify(kBody, w, 1614265330).failure == WebhookFailure::kBadSignatureHeader);
// several entries: any match passes
w.signature = "v1,AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= " + std::string(kSig);
ASSERT_TRUE(v.verify(kBody, w, 1614265330).ok);
// wrong secret
WebhookVerifier other("whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSx");
ASSERT_FALSE(other.verify(kBody, h, 1614265330).ok);
ASSERT_THROW(other.require(kBody, h, 1614265330), ApiError);
// body cap
ASSERT_TRUE(v.verify(std::string(AdminCaps::kMaxWebhookBodyBytes + 1, 'x'), h, 1614265330).failure ==
WebhookFailure::kBodyTooLarge);
ASSERT_TRUE(WebhookCounters::snapshot().count("signature_mismatch") == 1);
}
TEST(openai_admin, webhook_secret_rules) {
ASSERT_TRUE(throws_invalid([] { WebhookVerifier(""); }));
ASSERT_TRUE(throws_invalid([] { WebhookVerifier("whsec_!!!!"); }));
ASSERT_TRUE(throws_invalid([] { WebhookVerifier("whsec_YWJj"); })); // 3 bytes
ASSERT_TRUE(throws_invalid([] { WebhookVerifier(kSecret, 3601); }));
ASSERT_TRUE(throws_invalid([] { WebhookVerifier(kSecret, -1); }));
WebhookVerifier raw("plain-secret-bytes"); // no prefix: used as raw bytes (as the SDKs do)
WebhookHeaders h = WebhookHeaders::from({{"Webhook-Id", "i1"}, {"WEBHOOK-TIMESTAMP", "100"},
{"webhook-signature", raw.sign("i1", "100", "{}")}});
ASSERT_TRUE(raw.verify("{}", h, 100).ok);
}
TEST(openai_admin, webhook_unwrap_parses_after_verifying) {
// Envelope shape from the OpenAPI `webhooks` map (batch.completed).
const std::string body =
"{\"id\":\"evt_1\",\"object\":\"event\",\"created_at\":1750287018,\"type\":\"batch.completed\","
"\"data\":{\"id\":\"batch_abc\"}}";
WebhookVerifier v(kSecret);
WebhookHeaders h{"wh_1", "1750287078", v.sign("wh_1", "1750287078", body)};
WebhookEvent e = v.unwrap(body, h, 1750287080);
ASSERT_EQ(e.type, "batch.completed");
ASSERT_EQ(e.family(), "batch");
ASSERT_EQ(e.resource_id(), "batch_abc");
ASSERT_TRUE(e.known);
ASSERT_TRUE(e.missing_data_fields.empty());
ASSERT_THROW(v.unwrap(body + " ", h, 1750287080), ApiError); // not parsed unverified
WebhookEvent unknown = WebhookEnvelope::parse(
"{\"id\":\"e\",\"object\":\"event\",\"created_at\":1,\"type\":\"future.thing\",\"data\":{}}");
ASSERT_FALSE(unknown.known);
WebhookEvent partial = WebhookEnvelope::parse(
"{\"id\":\"e\",\"created_at\":1,\"type\":\"agent.session.created\",\"data\":{\"id\":\"s\"}}");
ASSERT_EQ(partial.missing_data_fields.size(), 1u); // environment_type
ASSERT_TRUE(throws_invalid([] { WebhookEnvelope::parse("[]"); }));
ASSERT_TRUE(throws_invalid([] { WebhookEnvelope::parse("{\"id\":\"e\"}"); }));
}
TEST(openai_admin, webhook_endpoint_calls_validate_event_types) {
WebhookEndpointSpec s;
s.name = "n";
s.url = "http://insecure.example/x";
s.event_types = {"batch.completed"};
ASSERT_TRUE(throws_invalid([&] { s.to_json(true); }));
s.url = "https://hooks.example/x";
s.event_types = {"live.call.incoming"}; // sent, but not selectable
ASSERT_TRUE(throws_invalid([&] { s.to_json(true); }));
s.event_types = {"video.completed"};
ASSERT_EQ(s.to_json(true).dump(),
"{\"event_types\":[\"video.completed\"],\"name\":\"n\",\"url\":\"https://hooks.example/x\"}");
ASSERT_TRUE(throws_invalid([] { WebhookEndpointSpec{}.to_json(false); }));
}
TEST(openai_admin, certificate_scope_rules) {
Rig r;
Certificates proj(r.client, "proj_1");
ASSERT_TRUE(throws_invalid([&] { proj.rename("c1", "n"); }));
ASSERT_TRUE(throws_invalid([&] { proj.get("c1", false); }));
ASSERT_TRUE(throws_invalid([&] { proj.activate({}); }));
ASSERT_EQ(r.mock->send_count(), 0u);
}
// Opt-in live checks (plan rule 7): unauthenticated-grade probe with an
// invalid key, and read-only list calls with a real admin key.
// Captured at startup: other suites change the environment while running.
static const bool kLiveNet = std::getenv("MOLO_LIVE_NET") && std::string(std::getenv("MOLO_LIVE_NET")) == "1";
static const std::string kLiveAdminKey = std::getenv("OPENAI_ADMIN_KEY") ? std::getenv("OPENAI_ADMIN_KEY") : "";
TEST(openai_admin, live_read_only) {
if (!kLiveNet) {
std::cout << "SKIP openai_admin.live_read_only: set MOLO_LIVE_NET=1 (and OPENAI_ADMIN_KEY)\n";
return;
}
auto probe = AdminApiClient::with_transport(
std::make_unique<molo::clients::transports::HttpTransport>(), "sk-admin-invalid");
try {
Users(probe).list();
FAIL("invalid key accepted");
} catch (const ApiError& e) {
ASSERT_EQ(e.http_status(), 401);
ASSERT_EQ(e.type(), "invalid_request_error");
}
auto client = AdminApiClient::with_transport(
std::make_unique<molo::clients::transports::HttpTransport>(), kLiveAdminKey);
if (!client->configured()) {
std::cout << "SKIP openai_admin.live_read_only (key part): OPENAI_ADMIN_KEY not set\n";
return;
}
ListOptions one;
one.limit = 1;
Json projects = Projects(client).list(one);
ASSERT_TRUE(projects.find("data") != nullptr);
Json users = Users(client).list(one);
ASSERT_TRUE(users.find("data") != nullptr);
}