Files

88 lines
6.6 KiB
C++

// Routes: POST /ai/huggingface/* (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);
unsetenv("HF_TOKEN");
unsetenv("HUGGING_FACE_HUB_TOKEN");
Fixture f;
auto post = [&](const std::string& route, const std::string& body) {
return f.client->post("/ai/huggingface/" + route, body, "application/json");
};
const char* all[] = {"hub/list", "hub/repo", "hub/search", "hub/whoami", "hub/discussions", "hub/collections",
"hub/webhooks", "chat", "chat/stream", "task", "endpoints", "tgi", "tei"};
for (const char* r : all) {
std::string route = std::string("/ai/huggingface/") + 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("hub/list", "{\"kind\":\"kernels\"}").status == 422, "unknown kind");
check(post("hub/list", "{\"limit\":101}").status == 422, "limit over 100");
check(post("hub/list", "{\"pipeline_tag\":\"x\",\"kind\":\"datasets\"}").status == 422, "pipeline_tag is models only");
check(post("hub/repo", "{}").status == 422, "repo needs action");
check(post("hub/repo", "{\"action\":\"info\",\"id\":\"gpt2\"}").status == 422, "legacy id without owner");
check(post("hub/repo", "{\"action\":\"info\",\"id\":\"../x/y\"}").status == 422, "path traversal id");
check(post("hub/repo", "{\"action\":\"fly\",\"id\":\"a/b\"}").status == 422, "unknown repo action");
check(post("hub/repo", "{\"action\":\"file\",\"id\":\"a/b\",\"path\":\"../etc\"}").status == 422, "file path traversal");
check(post("hub/search", "{\"action\":\"quicksearch\"}").status == 422, "quicksearch needs q");
check(post("hub/search", "{\"action\":\"nope\"}").status == 422, "unknown search action");
check(post("hub/discussions", "{\"action\":\"get\",\"id\":\"a/b\"}").status == 422, "discussion get needs num");
check(post("hub/discussions", "{\"action\":\"create\",\"id\":\"a/b\"}").status == 422, "discussion create needs title");
check(post("hub/collections", "{\"action\":\"add_item\",\"owner\":\"me\",\"slug\":\"x-1\",\"item_type\":\"video\",\"item_id\":\"x\"}").status == 422,
"bad collection item type");
check(post("hub/webhooks", "{\"action\":\"create\",\"url\":\"https://example.com/hook\"}").status == 422, "webhook needs watched");
check(post("chat", "{\"model\":\"a/b\"}").status == 422, "chat needs prompt");
check(post("chat", "{\"prompt\":\"hi\"}").status == 422, "chat needs model");
check(post("chat", "{\"model\":\"a b\",\"prompt\":\"hi\"}").status == 422, "bad model id");
check(post("chat", "{\"model\":\"a/b\",\"prompt\":\"hi\",\"max_tokens\":0}").status == 422, "max_tokens 0");
check(post("task", "{\"task\":\"nope\",\"model\":\"a/b\",\"inputs\":\"x\"}").status == 422, "unknown task");
check(post("task", "{\"task\":\"translation\",\"model\":\"a/b\"}").status == 422, "task needs inputs");
check(post("task", "{\"task\":\"image-classification\",\"model\":\"a/b\",\"data_base64\":\"@@\",\"mime\":\"image/png\"}").status == 422,
"bad base64");
check(post("endpoints", "{\"action\":\"get\",\"namespace\":\"acme\"}").status == 422, "endpoint get needs name");
check(post("endpoints", "{\"action\":\"get\",\"namespace\":\"acme\",\"name\":\"Bad_Name\"}").status == 422, "bad endpoint name");
check(post("endpoints", "{\"action\":\"create\",\"namespace\":\"acme\",\"spec\":{\"name\":\"x\"}}").status == 422, "incomplete endpoint spec");
check(post("tgi", "{\"action\":\"health\",\"url\":\"ftp://x\"}").status == 422, "tgi url must be http(s)");
check(post("tei", "{\"action\":\"embed\",\"url\":\"http://127.0.0.1:1\"}").status == 422, "tei embed needs inputs");
// dry runs: paid or resource-changing routes show the exact request
auto dry = post("chat", "{\"model\":\"openai/gpt-oss-20b:fastest\",\"prompt\":\"hi\"}");
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "/v1/chat/completions") &&
contains(dry.body, "openai/gpt-oss-20b:fastest"),
"chat is a dry run without confirm");
check(contains(post("chat/stream", "{\"model\":\"a/b\",\"prompt\":\"hi\"}").body, "\"stream\":true"), "stream preview");
check(contains(post("task", "{\"task\":\"translation\",\"model\":\"t5/x\",\"inputs\":\"hi\"}").body, "\"dry_run\":true"), "task is a dry run");
check(contains(post("hub/discussions", "{\"action\":\"create\",\"id\":\"a/b\",\"title\":\"Typo\"}").body, "\"dry_run\":true"),
"discussion create is a dry run");
check(contains(post("hub/discussions", "{\"action\":\"remove\",\"id\":\"a/b\",\"num\":3}").body, "DELETE"), "discussion remove is a dry run");
check(contains(post("hub/collections", "{\"action\":\"create\",\"owner\":\"me\",\"title\":\"T\"}").body, "\"dry_run\":true"),
"collection create is a dry run");
check(contains(post("hub/webhooks", "{\"action\":\"remove\",\"id\":\"w1\"}").body, "\"dry_run\":true"), "webhook remove is a dry run");
check(contains(post("endpoints", "{\"action\":\"pause\",\"namespace\":\"acme\",\"name\":\"ep\"}").body, "\"dry_run\":true"),
"endpoint pause is a dry run");
check(contains(post("endpoints", "{\"action\":\"remove\",\"namespace\":\"acme\",\"name\":\"ep\"}").body, "DELETE"),
"endpoint remove is a dry run");
// 503: reads and confirmed calls need the credential, before any network traffic
for (const char* r : {"hub/whoami"}) {
auto res = post(r, "{}");
check(res.status == 503 && contains(res.body, "huggingface_not_configured"), std::string(r) + " without a token is 503");
}
check(post("hub/list", "{}").status == 503, "list without a token is 503");
check(post("hub/repo", "{\"action\":\"info\",\"id\":\"openai-community/gpt2\"}").status == 503, "repo info without a token is 503");
check(post("hub/search", "{\"action\":\"tasks\"}").status == 503, "search without a token is 503");
check(post("endpoints", "{\"action\":\"vendors\"}").status == 503, "endpoints without a token is 503");
check(post("tgi", "{\"action\":\"health\",\"url\":\"http://127.0.0.1:1\"}").status == 503, "tgi without a token is 503");
check(post("chat", "{\"model\":\"a/b\",\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed chat without a token is 503");
check(post("endpoints", "{\"action\":\"remove\",\"namespace\":\"acme\",\"name\":\"ep\",\"confirm\":true}").status == 503,
"confirmed endpoint remove without a token is 503");
return finish("ai/huggingface/routes");
}