Files

112 lines
8.4 KiB
C++
Raw Permalink Normal View History

// Routes: POST /ai/azure/* (405, 422, dry runs, 503 without a credential)
#include <cstdlib>
#include "support/Assert.hpp"
using namespace molo::example::test;
int main() {
setenv("MOLO_ENV_FILES", "off", 1);
for (const char* v : {"AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_API_KEY", "AZURE_OPENAI_API_VERSION", "AZURE_TENANT_ID",
"AZURE_CLIENT_ID", "AZURE_CLIENT_SECRET", "AZURE_AI_ENDPOINT", "AZURE_CONTENT_SAFETY_ENDPOINT",
"AZURE_CONTENT_SAFETY_KEY"})
unsetenv(v);
Fixture f;
auto post = [&](const std::string& route, const std::string& body) {
return f.client->post("/ai/azure/" + route, body, "application/json");
};
const char* all[] = {"chat", "embeddings", "images", "speech", "models", "foundry", "safety", "entra", "realtime", "call"};
for (const char* r : all) {
std::string route = std::string("/ai/azure/") + r;
check(f.client->get(route).status == 405, route + " get is 405");
check(post(r, "not json").status == 422, route + " bad json is 422");
check(post(r, "[1]").status == 422, route + " non-object is 422");
}
// 422: input rules run before anything else
check(post("chat", "{}").status == 422, "chat needs deployment");
check(post("chat", "{\"deployment\":\"gpt4o\"}").status == 422, "chat needs prompt");
check(post("chat", "{\"deployment\":\"bad name\",\"prompt\":\"hi\"}").status == 422, "bad deployment name");
check(post("chat", "{\"deployment\":\"" + std::string(65, 'a') + "\",\"prompt\":\"hi\"}").status == 422, "deployment over 64");
check(post("chat", "{\"deployment\":\"gpt4o\",\"prompt\":\"hi\",\"dialect\":\"soap\"}").status == 422, "bad dialect");
check(post("chat", "{\"deployment\":\"gpt4o\",\"prompt\":\"hi\",\"max_tokens\":\"x\"}").status == 422, "max_tokens type");
check(post("embeddings", "{\"deployment\":\"e\"}").status == 422, "embeddings need input");
check(post("images", "{\"deployment\":\"i\"}").status == 422, "images need a prompt");
check(post("images", "{\"deployment\":\"i\",\"prompt\":\"x\",\"n\":\"two\"}").status == 422, "image n must be a number");
check(post("speech", "{\"deployment\":\"t\",\"input\":\"x\"}").status == 422, "speech needs a voice");
check(post("foundry", "{}").status == 422, "foundry needs an action");
check(post("foundry", "{\"action\":\"fly\"}").status == 422, "bad foundry action");
check(post("foundry", "{\"action\":\"chat\"}").status == 422, "foundry chat needs a prompt");
check(post("foundry", "{\"action\":\"embeddings\"}").status == 422, "foundry embeddings need input");
check(post("safety", "{\"action\":\"text\"}").status == 422, "safety text needs text");
check(post("safety", "{\"action\":\"shield\"}").status == 422, "shield needs a prompt or documents");
check(post("safety", "{\"action\":\"fly\"}").status == 422, "bad safety action");
check(post("realtime", "{\"action\":\"url\"}").status == 422, "realtime url needs a deployment");
check(post("realtime", "{\"action\":\"url\",\"deployment\":\"bad name\"}").status == 422, "realtime bad deployment");
check(post("realtime", "{\"action\":\"fly\"}").status == 422, "bad realtime action");
check(post("call", "{}").status == 422, "call needs an operation");
check(post("call", "{\"operation\":\"NoSuchCall\"}").status == 422, "unknown operation");
check(post("call", "{\"operation\":\"CreateFile\"}").status == 422, "multipart operation is refused");
check(post("call", "{\"operation\":\"RetrieveFile\",\"ids\":[\"../x\"]}").status == 422, "path traversal id");
check(post("call", "{\"operation\":\"RetrieveFile\"}").status == 422, "missing id");
check(post("call", "{\"operation\":\"CreateConversation\",\"body\":{\"bogus\":1}}").status == 422, "field not in the spec");
check(post("call", "{\"operation\":\"ListContainers\",\"query\":{\"zzz\":\"1\"}}").status == 422, "query not in the spec");
check(post("call", "{\"operation\":\"ListContainers\",\"query\":{\"limit\":5}}").status == 422, "query values are strings");
// dry runs: paid or resource-changing routes show the exact request
auto dry = post("chat", "{\"deployment\":\"gpt4o\",\"prompt\":\"hi\"}");
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "/openai/v1/chat/completions") &&
contains(dry.body, "\"model\":\"gpt4o\""),
"chat is a dry run on the v1 api");
check(contains(post("chat", "{\"deployment\":\"gpt41\",\"prompt\":\"hi\",\"dialect\":\"responses\"}").body, "/openai/v1/responses"),
"responses dialect preview");
check(contains(post("chat", "{\"deployment\":\"Phi-4\",\"prompt\":\"hi\",\"dialect\":\"foundry\"}").body,
"/models/chat/completions?api-version=2024-05-01-preview"),
"foundry dialect preview");
check(contains(post("embeddings", "{\"deployment\":\"e\",\"input\":\"hi\"}").body, "/openai/v1/embeddings"), "embeddings dry run");
check(contains(post("images", "{\"deployment\":\"i\",\"prompt\":\"a cat\"}").body, "/openai/v1/images/generations"), "images dry run");
check(contains(post("speech", "{\"deployment\":\"t\",\"voice\":\"alloy\",\"input\":\"hi\"}").body, "/openai/v1/audio/speech"),
"speech dry run");
check(contains(post("foundry", "{\"action\":\"chat\",\"prompt\":\"hi\"}").body, "\"dry_run\":true"), "foundry chat dry run");
check(contains(post("safety", "{\"action\":\"text\",\"text\":\"hello\"}").body,
"/contentsafety/text:analyze?api-version=2024-09-01"),
"safety text dry run");
check(contains(post("safety", "{\"action\":\"protected\",\"text\":\"hello\"}").body, "detectProtectedMaterial"), "protected dry run");
check(contains(post("realtime", "{\"action\":\"secret\",\"deployment\":\"rt\",\"seconds\":60}").body,
"/openai/v1/realtime/client_secrets"),
"client secret dry run");
auto del = post("call", "{\"operation\":\"DeleteFile\",\"ids\":[\"file-1\"]}");
check(del.status == 200 && contains(del.body, "\"dry_run\":true") && contains(del.body, "DELETE /openai/v1/files/file-1"),
"delete is a dry run");
check(contains(post("call", "{\"operation\":\"CreateVectorStore\",\"body\":{\"name\":\"docs\"}}").body, "\"dry_run\":true"),
"create is a dry run");
check(contains(post("call", "{\"operation\":\"CreateAssistant\",\"body\":{\"model\":\"gpt4o\"}}").body,
"/openai/assistants?api-version=2025-04-01-preview"),
"dated-only operation shows the fallback api version");
check(contains(post("call", "{\"operation\":\"CreateChatCompletion\",\"deployment\":\"gpt4o\",\"body\":{\"messages\":[]}}").body,
"/openai/v1/chat/completions"),
"generic chat preview");
// 503: free calls and confirmed calls need the credential
for (const char* r : {"models"}) {
auto res = post(r, "{}");
check(res.status == 503 && contains(res.body, "not_configured"), std::string(r) + " without key is 503");
}
check(post("entra", "{}").status == 503, "entra check without client credentials is 503");
check(post("realtime", "{\"action\":\"url\",\"deployment\":\"rt\"}").status == 503, "realtime url without an endpoint is 503");
check(post("foundry", "{\"action\":\"info\"}").status == 503, "foundry info without key is 503");
check(post("safety", "{\"action\":\"blocklists\"}").status == 503, "blocklists without key is 503");
check(post("call", "{\"operation\":\"ListVectorStores\"}").status == 503, "generic GET without key is 503");
check(post("chat", "{\"deployment\":\"gpt4o\",\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed chat without key is 503");
check(post("embeddings", "{\"deployment\":\"e\",\"input\":\"hi\",\"confirm\":true}").status == 503, "confirmed embeddings without key is 503");
check(post("images", "{\"deployment\":\"i\",\"prompt\":\"x\",\"confirm\":true}").status == 503, "confirmed images without key is 503");
check(post("speech", "{\"deployment\":\"t\",\"voice\":\"alloy\",\"input\":\"x\",\"confirm\":true}").status == 503,
"confirmed speech without key is 503");
check(post("safety", "{\"action\":\"text\",\"text\":\"hi\",\"confirm\":true}").status == 503, "confirmed safety without key is 503");
check(post("call", "{\"operation\":\"DeleteFile\",\"ids\":[\"file-1\"],\"confirm\":true}").status == 503,
"confirmed delete without key is 503");
check(post("realtime", "{\"action\":\"secret\",\"deployment\":\"rt\",\"confirm\":true}").status == 503,
"confirmed client secret without key is 503");
return finish("ai/azure/routes");
}