60 lines
3.9 KiB
C++
60 lines
3.9 KiB
C++
// Destructive and paid routes are dry runs unless the body says "confirm":true.
|
|||
|
|
// A dry run needs no key and sends nothing; confirm without a key is 503; a
|
||
|
|
// non-boolean confirm is 422.
|
||
|
|
#include <cstdlib>
|
||
|
|
|
||
|
|
#include "support/Assert.hpp"
|
||
|
|
|
||
|
|
using namespace molo::example::test;
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
unsetenv("XAI_API_KEY");
|
||
|
|
unsetenv("XAI_MANAGEMENT_KEY");
|
||
|
|
Fixture f;
|
||
|
|
const std::string team = "5ea6f6bd-7815-4b8a-9135-28b2d7ba6722";
|
||
|
|
const struct {
|
||
|
|
const char* path;
|
||
|
|
std::string body; // without the closing brace; confirm is appended
|
||
|
|
std::string expect_path;
|
||
|
|
} cases[] = {
|
||
|
|
{"/ai/xai/platform/files", "{\"action\":\"delete\",\"id\":\"file_abc\"", "/v1/files/file_abc"},
|
||
|
|
{"/ai/xai/platform/files", "{\"action\":\"revoke_url\",\"id\":\"file_abc\"", "/v1/files/file_abc/public-url/revoke"},
|
||
|
|
{"/ai/xai/platform/collections", "{\"action\":\"delete\",\"collection_id\":\"collection_x\"", "/v1/collections/collection_x"},
|
||
|
|
{"/ai/xai/platform/collections", "{\"action\":\"remove_document\",\"collection_id\":\"collection_x\",\"file_id\":\"file_y\"",
|
||
|
|
"/v1/collections/collection_x/documents/file_y"},
|
||
|
|
{"/ai/xai/platform/voices", "{\"action\":\"custom_delete\",\"voice_id\":\"abcd1234\"", "/v1/custom-voices/abcd1234"},
|
||
|
|
{"/ai/xai/platform/keys", "{\"action\":\"delete\",\"api_key_id\":\"" + team + "\"", "/auth/api-keys/" + team},
|
||
|
|
{"/ai/xai/platform/keys", "{\"action\":\"rotate\",\"api_key_id\":\"" + team + "\"", "/auth/api-keys/" + team + "/rotate"},
|
||
|
|
{"/ai/xai/platform/billing", "{\"action\":\"top_up\",\"team_id\":\"" + team + "\",\"amount\":\"10\"",
|
||
|
|
"/v1/billing/teams/" + team + "/prepaid/top-up"},
|
||
|
|
{"/ai/xai/platform/billing", "{\"action\":\"set_spending_limit\",\"team_id\":\"" + team + "\",\"amount\":\"100\"",
|
||
|
|
"/v1/billing/teams/" + team + "/postpaid/spending-limits"},
|
||
|
|
{"/ai/xai/platform/realtime/phone-numbers", "{\"action\":\"delete\",\"phone_number_id\":\"phone_1\"", "/v2/phone-numbers/phone_1"},
|
||
|
|
{"/ai/xai/platform/realtime/calls", "{\"action\":\"refer\",\"call_id\":\"c1\",\"target_uri\":\"tel:+15551234567\"", "/v1/realtime/calls/c1/refer"},
|
||
|
|
{"/ai/xai/platform/tts", "{\"text\":\"hi\"", "/v1/tts"},
|
||
|
|
{"/ai/xai/platform/stt", "{\"url\":\"https://example.com/a.mp3\"", "/v1/stt"},
|
||
|
|
{"/ai/xai/platform/realtime/secret", "{\"expires_seconds\":60", "/v1/realtime/client_secrets"},
|
||
|
|
{"/ai/xai/platform/realtime/session", "{\"text\":\"hi\"", "wss://api.x.ai/v1/realtime"},
|
||
|
|
};
|
||
|
|
for (const auto& c : cases) {
|
||
|
|
std::string p = c.path;
|
||
|
|
auto dry = f.client->post(p, c.body + "}", "application/json");
|
||
|
|
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, c.expect_path),
|
||
|
|
p + " " + c.expect_path + ": default is a dry run");
|
||
|
|
auto off = f.client->post(p, c.body + ",\"confirm\":false}", "application/json");
|
||
|
|
check(off.status == 200 && contains(off.body, "\"dry_run\":true"), p + ": confirm false is a dry run");
|
||
|
|
auto real = f.client->post(p, c.body + ",\"confirm\":true}", "application/json");
|
||
|
|
check(real.status == 503 && contains(real.body, "not_configured"), p + ": confirmed without a key is 503");
|
||
|
|
check(f.client->post(p, c.body + ",\"confirm\":\"yes\"}", "application/json").status == 422,
|
||
|
|
p + ": non-bool confirm is 422");
|
||
|
|
}
|
||
|
|
// Dry runs still validate their input.
|
||
|
|
check(f.client->post("/ai/xai/platform/files", "{\"action\":\"delete\",\"id\":\"../x\"}", "application/json").status == 422,
|
||
|
|
"bad id is 422 even on a dry run");
|
||
|
|
check(f.client->post("/ai/xai/platform/billing", "{\"action\":\"top_up\",\"team_id\":\"" + team + "\",\"amount\":\"-5\",\"confirm\":true}", "application/json").status == 422,
|
||
|
|
"a bad amount is 422 before the key is needed");
|
||
|
|
check(f.client->post("/ai/xai/platform/tts", "{\"text\":\"hi\",\"speed\":9}", "application/json").status == 422,
|
||
|
|
"tts speed out of range is 422 on a dry run");
|
||
|
|
return finish("ai/xai/platform/confirm");
|
||
|
|
}
|