Files
moloapp/tests/example/google/data/call_test.cpp
T

49 lines
3.5 KiB
C++
Raw Normal View History

// Routes: POST /google/data/call and /google/data/storage/sign
// The generic call route validates against the Discovery table; writes are
// dry runs unless confirm:true; the signer is a dry run unless confirm:true.
#include <cstdlib>
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
GoogleRig rig(false);
Fixture f(rig.client);
auto j = [&](const char* body) { return f.client->post("/google/data/call", body, "application/json"); };
auto read = j("{\"rpc\":\"bigquery.datasets.list\",\"path\":{\"projectId\":\"p1\"},\"query\":{\"maxResults\":5}}");
check(read.status == 503 && contains(read.body, "google_not_configured"), "valid GET without credential is 503");
auto post = j("{\"rpc\":\"pubsub.projects.topics.publish\",\"path\":{\"topic\":\"projects/p1/topics/t1\"},\"body\":{\"messages\":[]}}");
check(post.status == 200 && contains(post.body, "\"dry_run\":true") &&
contains(post.body, "https://pubsub.googleapis.com/v1/projects/p1/topics/t1:publish"),
"non-GET is a dry run that shows the URL");
auto confirmed = j("{\"rpc\":\"pubsub.projects.topics.publish\",\"path\":{\"topic\":\"projects/p1/topics/t1\"},\"body\":{\"messages\":[]},\"confirm\":true}");
check(confirmed.status == 503, "confirmed non-GET without credential is 503");
check(j("{}").status == 422, "missing rpc is 422");
check(j("{\"rpc\":\"nope.x\"}").status == 422, "unknown rpc is 422");
check(j("{\"rpc\":\"bigquery.datasets.list\"}").status == 422, "missing path value is 422");
check(j("{\"rpc\":\"bigquery.datasets.list\",\"path\":{\"projectId\":\"../x\"}}").status == 422, "path traversal is 422");
check(j("{\"rpc\":\"bigquery.datasets.list\",\"path\":{\"projectId\":\"p\"},\"query\":{\"zzz\":1}}").status == 422, "unknown query parameter is 422");
check(j("{\"rpc\":\"bigquery.jobs.query\",\"path\":{\"projectId\":\"p\"},\"body\":{\"zzz\":1}}").status == 422, "unknown body field is 422");
check(j("{\"rpc\":\"bigquery.datasets.list\",\"path\":{\"projectId\":5}}").status == 422, "non-string path value is 422");
check(f.client->get("/google/data/call").status == 405, "get is 405");
auto sign = [&](const char* body) { return f.client->post("/google/data/storage/sign", body, "application/json"); };
auto dry = sign("{\"bucket\":\"my-bucket\",\"object\":\"a/b.txt\",\"expires_seconds\":600}");
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && !contains(dry.body, "X-Goog-Signature"),
"signing is a dry run by default");
check(sign("{\"bucket\":\"my-bucket\",\"expires_seconds\":604801}").status == 422, "expiry over 7 days is 422");
check(sign("{\"bucket\":\"my-bucket\",\"expires_seconds\":0}").status == 422, "expiry 0 is 422");
check(sign("{\"bucket\":\"Bad\"}").status == 422, "bad bucket is 422");
check(sign("{\"bucket\":\"my-bucket\",\"method\":\"TRACE\"}").status == 422, "bad method is 422");
check(sign("{}").status == 422, "missing bucket is 422");
check(sign("{\"bucket\":\"my-bucket\",\"headers\":{\"host\":\"x\"}}").status == 422, "host header is 422");
auto real = sign("{\"bucket\":\"my-bucket\",\"object\":\"o\",\"confirm\":true}");
check(real.status == 503 && contains(real.body, "google_not_configured"), "confirmed signing without a key is 503");
check(f.client->get("/google/data/storage/sign").status == 405, "sign get is 405");
check(rig.mock->send_count() == 0, "nothing reached Google");
return finish("google/data/call");
}