32 lines
2.1 KiB
C++
32 lines
2.1 KiB
C++
// Route: POST /ai/openai/admin/write and /destroy (destructive calls are dry runs unless confirm:true)
|
|||
|
|
#include "support/Assert.hpp"
|
||
|
|
|
||
|
|
using namespace molo::example::test;
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
unsetenv("OPENAI_ADMIN_KEY");
|
||
|
|
unsetenv("OPENAI_API_KEY");
|
||
|
|
Fixture f;
|
||
|
|
auto w = [&](const std::string& b) { return f.client->post("/ai/openai/admin/write", b, "application/json"); };
|
||
|
|
auto d = [&](const std::string& b) { return f.client->post("/ai/openai/admin/destroy", b, "application/json"); };
|
||
|
|
check(w("{\"call\":\"invites.create\",\"body\":{\"email\":\"a@example.com\",\"role\":\"reader\"}}").status == 503,
|
||
|
|
"valid write without key is 503");
|
||
|
|
check(w("{\"call\":\"invites.create\",\"body\":{\"email\":\"a@example.com\"}}").status == 422, "missing required field is 422");
|
||
|
|
check(w("{\"call\":\"invites.create\",\"body\":{\"email\":\"a@example.com\",\"role\":\"reader\",\"x\":1}}").status == 422,
|
||
|
|
"foreign body field is 422");
|
||
|
|
check(w("{\"call\":\"users.delete\",\"ids\":[\"u\"]}").status == 422, "destructive via write is 422");
|
||
|
|
check(w("{\"call\":\"users.list\"}").status == 422, "read via write is 422");
|
||
|
|
auto dry = d("{\"call\":\"users.delete\",\"ids\":[\"user_1\"]}");
|
||
|
|
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true"), "default is a dry run");
|
||
|
|
check(contains(dry.body, "DELETE /organization/users/user_1"), "dry run names the call");
|
||
|
|
auto arch = d("{\"call\":\"projects.archive\",\"ids\":[\"proj_1\"]}");
|
||
|
|
check(arch.status == 200 && contains(arch.body, "dry_run"), "archive is a dry run");
|
||
|
|
auto real = d("{\"call\":\"users.delete\",\"ids\":[\"user_1\"],\"confirm\":true}");
|
||
|
|
check(real.status == 503 && contains(real.body, "not_configured"), "confirmed delete without key is 503");
|
||
|
|
check(d("{\"call\":\"users.get\",\"ids\":[\"u\"]}").status == 422, "non-destructive call on destroy is 422");
|
||
|
|
check(d("{\"call\":\"users.delete\",\"confirm\":\"yes\"}").status == 422, "non-bool confirm is 422");
|
||
|
|
check(d("{\"call\":\"users.delete\"}").status == 422, "dry run still needs the id");
|
||
|
|
check(f.client->get("/ai/openai/admin/destroy").status == 405, "get is 405");
|
||
|
|
return finish("ai/openai/admin/write");
|
||
|
|
}
|