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

52 lines
3.1 KiB
C++
Raw Normal View History

// Route: POST /google/ops/call (422 first, dry run for changes, 503 for reads)
#include "support/GoogleRig.hpp"
using namespace molo::example::test;
int main() {
GoogleRig rig(false);
Fixture f(rig.client);
auto post = [&](const std::string& body) {
return f.client->post("/google/ops/call", body, "application/json");
};
// Reads need a credential.
auto read = post("{\"rpc\":\"run.projects.locations.services.get\",\"path\":{\"name\":"
"\"projects/p/locations/l/services/s\"}}");
check(read.status == 503 && contains(read.body, "google_not_configured"), "read without credential is 503");
auto list = post("{\"rpc\":\"serviceusage.services.list\",\"path\":{\"parent\":\"projects/p\"}}");
check(list.status == 503, "list without credential is 503");
// Writes and deletes are dry runs, even without a credential.
auto enable = post("{\"rpc\":\"serviceusage.services.enable\",\"path\":{\"name\":"
"\"projects/p/services/iam.googleapis.com\"}}");
check(enable.status == 200 && contains(enable.body, "\"executed\":false") &&
contains(enable.body, "iam.googleapis.com:enable"),
"enable is a dry run");
auto del = post("{\"rpc\":\"iam.projects.serviceAccounts.delete\",\"path\":{\"name\":"
"\"projects/p/serviceAccounts/a@p.iam.gserviceaccount.com\"}}");
check(del.status == 200 && contains(del.body, "\"safety\":\"destructive\"") &&
contains(del.body, "\"executed\":false"),
"delete is a dry run");
auto sure = post("{\"rpc\":\"iam.projects.serviceAccounts.delete\",\"confirm\":true,\"path\":{\"name\":"
"\"projects/p/serviceAccounts/a@p.iam.gserviceaccount.com\"}}");
check(sure.status == 503, "confirmed delete without credential is 503");
// Input is checked against the Discovery-derived table.
check(post("{}").status == 422, "missing rpc is 422");
check(post("{\"rpc\":\"run.nope\"}").status == 422, "unknown rpc is 422");
check(post("{\"rpc\":\"run.projects.locations.services.get\"}").status == 422, "missing path param is 422");
check(post("{\"rpc\":\"run.projects.locations.services.get\",\"path\":{\"name\":\"projects/p/../x\"}}").status == 422,
"traversal is 422");
check(post("{\"rpc\":\"serviceusage.services.list\",\"path\":{\"parent\":\"projects/p\"},"
"\"query\":{\"bogus\":1}}").status == 422,
"unknown query parameter is 422");
check(post("{\"rpc\":\"serviceusage.services.batchEnable\",\"path\":{\"parent\":\"projects/p\"},"
"\"body\":{\"typo\":1}}").status == 422,
"unknown body field is 422");
check(post("{\"rpc\":\"serviceusage.services.list\",\"confirm\":\"yes\"}").status == 422, "non-bool confirm is 422");
check(post("[]").status == 422, "non-object body is 422");
check(post("nope").status == 422, "bad json is 422");
check(post(std::string(300 * 1024, ' ')).status == 413, "oversize body is 413");
check(f.client->get("/google/ops/call").status == 405, "get is 405");
check(rig.mock->send_count() == 0, "nothing reached Google");
return finish("google/ops/call");
}