Files
moloapp/tests/example/ai/media/routes_test.cpp
T

129 lines
11 KiB
C++
Raw Normal View History

// Routes: POST /ai/media/* (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 : {"STABILITY_API_KEY", "BFL_API_KEY", "RUNWAY_API_KEY", "RUNWAYML_API_SECRET", "LUMA_API_KEY",
"REPLICATE_API_TOKEN", "FAL_KEY", "IDEOGRAM_API_KEY", "RECRAFT_API_TOKEN", "LEONARDO_API_KEY",
"MINIMAX_API_KEY", "KLING_ACCESS_KEY", "KLING_SECRET_KEY", "HF_API_KEY_ID", "HF_API_KEY_SECRET"})
unsetenv(v);
Fixture f;
auto post = [&](const std::string& route, const std::string& body) {
return f.client->post("/ai/media/" + route, body, "application/json");
};
const char* posts[] = {"image", "video", "poll", "wait", "call", "account", "fetch", "webhook"};
for (const char* r : posts) {
std::string route = std::string("/ai/media/") + 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");
check(post(r, "{}").status == 422, route + " empty body is 422");
}
check(f.client->get("/ai/media/job").status == 405, "job get is 405");
check(f.client->request(molo::example::fw::Method::Delete, "/ai/media/job", "not json").status == 422, "job bad json is 422");
// 422: input rules run before anything else
check(post("image", "{\"provider\":\"midjourney\",\"prompt\":\"x\"}").status == 422, "midjourney has no API");
check(post("image", "{\"provider\":\"minimax\",\"prompt\":\"x\"}").status == 422, "minimax has no image generation");
check(post("video", "{\"provider\":\"stability\",\"prompt\":\"x\"}").status == 422, "stability has no video generation");
check(post("image", "{\"provider\":\"bfl\"}").status == 422, "prompt is required");
check(post("image", "{\"provider\":\"bfl\",\"prompt\":\"x\",\"count\":0}").status == 422, "count 0");
check(post("image", "{\"provider\":\"bfl\",\"prompt\":\"x\",\"count\":9}").status == 422, "count over 8");
check(post("image", "{\"provider\":\"bfl\",\"prompt\":\"x\",\"width\":512}").status == 422, "width without height");
check(post("image", "{\"provider\":\"bfl\",\"prompt\":\"x\",\"width\":9000,\"height\":9000}").status == 422, "dimension over the cap");
check(post("image", "{\"provider\":\"bfl\",\"prompt\":\"x\",\"extra\":{\"bogus\":1}}").status == 422, "field not in the call table");
check(post("image", "{\"provider\":\"bfl\",\"prompt\":\"x\",\"model\":\"flux-9000\"}").status == 422, "model not in the call table");
check(post("image", "{\"provider\":\"bfl\",\"prompt\":\"x\",\"count\":2}").status == 422, "bfl takes one image per call");
check(post("image", "{\"provider\":\"stability\",\"prompt\":\"x\",\"width\":512,\"height\":512}").status == 422, "stability sizes by aspect ratio");
check(post("image", "{\"provider\":\"fal\",\"prompt\":\"x\"}").status == 422, "fal needs an endpoint id");
check(post("image", "{\"provider\":\"fal\",\"prompt\":\"x\",\"model\":\"a/../b\"}").status == 422, "bad endpoint id");
check(post("image", "{\"provider\":\"replicate\",\"prompt\":\"x\",\"model\":\"nope\"}").status == 422, "replicate needs owner/name");
check(post("image", "{\"provider\":\"bfl\",\"prompt\":\"x\",\"seed\":\"a\"}").status == 422, "seed must be a number");
check(post("image", "{\"provider\":\"bfl\",\"prompt\":\"x\",\"confirm\":\"yes\"}").status == 422, "confirm must be a boolean");
check(post("video", "{\"provider\":\"runway\"}").status == 422, "video needs a prompt or an image");
check(post("video", "{\"provider\":\"runway\",\"prompt\":\"x\",\"duration_seconds\":999}").status == 422, "duration over the cap");
check(post("video", "{\"provider\":\"minimax\",\"prompt\":\"x\",\"aspect_ratio\":\"16:9\"}").status == 422, "minimax v1 has no aspect ratio");
check(post("poll", "{\"ticket\":{\"provider\":\"bfl\"}}").status == 422, "ticket needs an id");
check(post("poll", "{\"ticket\":{\"provider\":\"nope\",\"id\":\"1\"}}").status == 422, "ticket with an unknown provider");
check(post("wait", "{\"ticket\":{\"provider\":\"bfl\",\"id\":\"1\"},\"max_attempts\":31}").status == 422, "demo attempt cap");
check(post("wait", "{\"ticket\":{\"provider\":\"bfl\",\"id\":\"1\"},\"max_wait_seconds\":61}").status == 422, "demo wait cap");
check(post("wait", "{\"ticket\":{\"provider\":\"bfl\",\"id\":\"1\"},\"max_attempts\":0}").status == 422, "attempts under 1");
check(post("call", "{\"provider\":\"bfl\",\"key\":\"nope\"}").status == 422, "unknown call key");
check(post("call", "{\"provider\":\"bfl\",\"key\":\"get_result\",\"query\":{\"nope\":\"1\"}}").status == 422, "unknown query parameter");
check(post("call", "{\"provider\":\"replicate\",\"key\":\"predictions.get\",\"path\":{\"prediction_id\":\"../x\"}}").status == 422, "path traversal");
check(post("call", "{\"provider\":\"replicate\",\"key\":\"predictions.get\"}").status == 422, "missing path argument");
check(post("call", "{\"provider\":\"bfl\",\"key\":\"credits\",\"body\":{\"x\":1}}").status == 422, "a GET takes no body");
check(post("call", "{\"provider\":\"stability\",\"key\":\"stable-image.upscale.fast\",\"files\":[{\"field\":\"image\",\"data_base64\":\"@@\"}]}").status == 422,
"bad base64 file");
check(post("call", "{\"provider\":\"stability\",\"key\":\"stable-image.upscale.fast\",\"files\":[{\"field\":\"picture\",\"data_base64\":\"aGk=\"}]}").status == 422,
"file field not in the table");
check(post("fetch", "{\"url\":\"http://example.com/a.png\"}").status == 422, "fetch needs https");
check(post("fetch", "{\"url\":\"https://user:pw@example.com/a.png\"}").status == 422, "fetch refuses embedded credentials");
check(post("webhook", "{\"secret\":\"s\",\"webhook_id\":\"i\",\"timestamp\":\"1\",\"signature\":\"v1,x\"}").status == 422, "webhook needs a payload");
// dry runs: paid or resource-changing routes show the exact request
auto dry = post("image", "{\"provider\":\"bfl\",\"prompt\":\"a cat\",\"width\":1024,\"height\":768}");
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "/v1/flux-2-pro") &&
contains(dry.body, "\"effect\":\"paid\"") && contains(dry.body, "\"prompt\":\"a cat\""),
"image is a dry run without confirm");
check(contains(post("image", "{\"provider\":\"stability\",\"prompt\":\"a cat\",\"model\":\"ultra\",\"aspect_ratio\":\"16:9\"}").body,
"/v2beta/stable-image/generate/ultra"),
"stability dry run shows the endpoint");
check(contains(post("image", "{\"provider\":\"fal\",\"prompt\":\"x\",\"model\":\"fal-ai/flux/dev\"}").body, "\"target\":\"/fal-ai/flux/dev\""),
"fal dry run shows the queue target");
check(contains(post("image", "{\"provider\":\"replicate\",\"prompt\":\"x\",\"model\":\"black-forest-labs/flux-schnell\"}").body,
"/models/black-forest-labs/flux-schnell/predictions"),
"replicate dry run shows the prediction target");
check(contains(post("image", "{\"provider\":\"leonardo\",\"prompt\":\"x\",\"model\":\"flux-dev\"}").body, "\"public\":false"),
"leonardo never publishes by default");
check(contains(post("image", "{\"provider\":\"ideogram\",\"prompt\":\"x\",\"model\":\"ideogram-4\"}").body, "/v2/image/generate/ideogram-4"),
"ideogram dry run");
check(contains(post("image", "{\"provider\":\"recraft\",\"prompt\":\"x\",\"width\":1024,\"height\":1024}").body, "\"size\":\"1024x1024\""),
"recraft dry run");
check(contains(post("video", "{\"provider\":\"runway\",\"prompt\":\"waves\",\"duration_seconds\":5}").body, "/v1/text_to_video"),
"runway video dry run");
check(contains(post("video", "{\"provider\":\"luma\",\"prompt\":\"waves\",\"duration_seconds\":5}").body, "\"duration\":\"5s\""),
"luma video dry run");
check(contains(post("video", "{\"provider\":\"kling\",\"prompt\":\"waves\"}").body, "/v1/videos/text2video"), "kling video dry run");
check(contains(post("video", "{\"provider\":\"higgsfield\",\"prompt\":\"waves\",\"model\":\"minimax/hailuo-2.3/standard/text-to-video\"}").body,
"/minimax/hailuo-2.3/standard/text-to-video"),
"higgsfield video dry run");
check(contains(post("call", "{\"provider\":\"bfl\",\"key\":\"flux-dev\",\"body\":{\"prompt\":\"x\"}}").body, "\"dry_run\":true"),
"a paid table call is a dry run");
check(contains(post("call", "{\"provider\":\"replicate\",\"key\":\"files.delete\",\"path\":{\"file_id\":\"f1\"}}").body, "\"effect\":\"delete\""),
"a destructive table call is a dry run");
check(contains(post("call", "{\"provider\":\"bfl\",\"key\":\"delete_finetune\",\"body\":{\"finetune_id\":\"my-ft\"}}").body, "\"dry_run\":true"),
"delete_finetune is a dry run");
auto cancel = f.client->request(molo::example::fw::Method::Delete, "/ai/media/job", "{\"ticket\":{\"provider\":\"luma\",\"id\":\"g1\",\"kind\":\"video\"}}");
check(cancel.status == 200 && contains(cancel.body, "\"dry_run\":true"), "cancel is a dry run");
// pure routes
auto hook = post("webhook",
"{\"secret\":\"whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw\",\"webhook_id\":\"msg_p5jXN8AQM9LWM0D4loKWxJek\","
"\"timestamp\":\"1614265330\",\"signature\":\"v1,g0hM9SsE+OTPJTGt/tmIKtSyZlE3uFJELVlNIOLJ1OE=\","
"\"payload\":\"{\\\"test\\\": 2432232314}\"}");
check(hook.status == 200 && contains(hook.body, "\"valid\":false"), "a vector from 2021 is stale now, so it is rejected");
// 503: reads and confirmed calls need the credential
auto acct = post("account", "{\"provider\":\"bfl\"}");
check(acct.status == 503 && contains(acct.body, "media_not_configured"), "account without key is 503");
check(post("account", "{\"provider\":\"minimax\"}").status == 422, "minimax has no free account read");
check(post("account", "{\"provider\":\"midjourney\"}").status == 422, "unknown provider account");
check(post("call", "{\"provider\":\"bfl\",\"key\":\"credits\"}").status == 503, "read call without key is 503");
check(post("call", "{\"provider\":\"fal\",\"key\":\"platform.meta\"}").status == 503, "fal platform read without key is 503");
check(post("call", "{\"provider\":\"bfl\",\"key\":\"flux-dev\",\"body\":{\"prompt\":\"x\"},\"confirm\":true}").status == 503,
"confirmed paid call without key is 503");
check(post("image", "{\"provider\":\"bfl\",\"prompt\":\"x\",\"confirm\":true}").status == 503, "confirmed image without key is 503");
check(post("video", "{\"provider\":\"runway\",\"prompt\":\"x\",\"confirm\":true}").status == 503, "confirmed video without key is 503");
check(post("video", "{\"provider\":\"kling\",\"prompt\":\"x\",\"confirm\":true}").status == 503, "kling without keys is 503");
check(post("poll", "{\"ticket\":{\"provider\":\"bfl\",\"id\":\"1\"}}").status == 503, "poll without key is 503");
check(post("wait", "{\"ticket\":{\"provider\":\"fal\",\"id\":\"1\",\"meta\":{\"endpoint\":\"fal-ai/flux/dev\"}}}").status == 503, "wait without key is 503");
check(f.client->request(molo::example::fw::Method::Delete, "/ai/media/job", "{\"ticket\":{\"provider\":\"luma\",\"id\":\"g1\"},\"confirm\":true}").status == 503,
"confirmed cancel without key is 503");
return finish("ai/media/routes");
}