Files
moloapp/tests/example/google/enterprise/cases.hpp
T

54 lines
2.3 KiB
C++
Raw Normal View History

#pragma once
// Shared case for the enterprise route tests (not a test itself). Every route
// that would call Google is a POST that must: answer 503 google_not_configured
// without a credential, answer 422 validation_error on bad input before any
// upstream call, and answer 405 to GET.
#include <cstdlib>
#include <string>
#include "support/GoogleRig.hpp"
namespace molo::example::test {
inline void check_enterprise_post_route(const std::string& path, const std::string& good_body,
const std::string& bad_body, const std::string& what) {
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
const std::string json = "application/json";
{
GoogleRig rig(false);
Fixture f(rig.client);
auto res = f.client->post(path, good_body, json);
check(res.status == 503, path + ": no credential is 503");
check(contains(res.body, "google_not_configured"), "error code google_not_configured");
check(contains(res.headers.get("Content-Type"), "application/json"), "error body is json");
check(rig.mock->send_count() == 0, "side effect: no upstream traffic");
}
{
GoogleRig rig;
Fixture f(rig.client);
auto res = f.client->post(path, bad_body, json);
check(res.status == 422, path + ": " + what + " is 422");
check(contains(res.body, "validation_error"), "error code validation_error");
check(rig.mock->send_count() == 0, "side effect: rejected before any upstream call");
check(f.client->get(path).status == 405, path + ": get is 405");
}
}
// With a credential, a non-read call is a dry run: 200, dry_run true, nothing sent.
inline void check_enterprise_dry_run(const std::string& path, const std::string& good_body,
const std::string& expect_in_url) {
setenv("GOOGLE_CLOUD_PROJECT", "test-project", 1);
GoogleRig rig;
Fixture f(rig.client);
auto res = f.client->post(path, good_body, "application/json");
check(res.status == 200, path + ": dry run is 200");
check(contains(res.body, "\"dry_run\":true"), "dry_run is true without confirm");
check(contains(res.body, expect_in_url), "dry run shows the url: " + expect_in_url);
check(!contains(res.body, kTestKey), "credential never shown");
check(rig.mock->send_count() == 0, "side effect: a dry run sends nothing");
}
} // namespace molo::example::test