|
|
@@ -1696,3 +1696,108 @@ TEST_F(ApiFixture, ALiteralDataFieldCanStillBeStored) {
|
|
|
const nlohmann::json got = nlohmann::json::parse(c.Get((docs + "/d").c_str())->body);
|
|
|
EXPECT_EQ(storedFields(got), (nlohmann::json{{"data",{{"inner",1}}}}));
|
|
|
}
|
|
|
+
|
|
|
+// --- database failures carry their classification to the HTTP status ---------
|
|
|
+
|
|
|
+TEST_F(ApiFixture, UniqueViolationIsConflictNotInternal) {
|
|
|
+ // The database answers ALREADY_EXISTS for a duplicate; before client 2.11.4
|
|
|
+ // that classification was lost and every caller saw 500 internal.
|
|
|
+ auto c = admin();
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","uq"},{"kind","json"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+ const std::string docs = base + "/uq/documents";
|
|
|
+ ASSERT_EQ(c.Post((base + "/uq/indexes").c_str(),
|
|
|
+ nlohmann::json{{"field","sku"},{"unique",true}}.dump(), "application/json")->status, 201);
|
|
|
+
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(), nlohmann::json{{"id","one"},{"sku","A"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(), nlohmann::json{{"id","two"},{"sku","B"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+
|
|
|
+ // INSERT colliding on the unique field.
|
|
|
+ auto dup = c.Post(docs.c_str(), nlohmann::json{{"id","three"},{"sku","A"}}.dump(), "application/json");
|
|
|
+ ASSERT_TRUE(dup);
|
|
|
+ EXPECT_EQ(dup->status, 409) << dup->body;
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(dup->body)["error"]["code"], "duplicate_values");
|
|
|
+
|
|
|
+ // PUT moving an existing document onto a taken value.
|
|
|
+ auto put = c.Put((docs + "/two").c_str(), nlohmann::json{{"sku","A"}}.dump(), "application/json");
|
|
|
+ ASSERT_TRUE(put);
|
|
|
+ EXPECT_EQ(put->status, 409) << put->body;
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(put->body)["error"]["code"], "duplicate_values");
|
|
|
+
|
|
|
+ // PATCH doing the same.
|
|
|
+ auto patch = c.Patch((docs + "/two").c_str(), nlohmann::json{{"sku","A"}}.dump(), "application/json");
|
|
|
+ ASSERT_TRUE(patch);
|
|
|
+ EXPECT_EQ(patch->status, 409) << patch->body;
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(patch->body)["error"]["code"], "duplicate_values");
|
|
|
+}
|
|
|
+
|
|
|
+TEST_F(ApiFixture, MissingParentReferenceIsUnprocessableNotInternal) {
|
|
|
+ // validate_on_write makes the database answer FAILED_PRECONDITION for a
|
|
|
+ // child pointing at a parent that does not exist. That is the caller's
|
|
|
+ // doing, not a server fault.
|
|
|
+ auto c = admin();
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
+ for (const char* n : {"par", "chi"})
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name",n},{"kind","json"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+ ASSERT_EQ(c.Post(("/api/v1/projects/" + project_ + "/relations").c_str(),
|
|
|
+ nlohmann::json{{"name","chi_par"},{"parent","par"},{"child","chi"},
|
|
|
+ {"child_field","par_id"},{"on_delete","restrict"},
|
|
|
+ {"validate_on_write",true}}.dump(), "application/json")->status, 201);
|
|
|
+
|
|
|
+ auto orphan = c.Post((base + "/chi/documents").c_str(),
|
|
|
+ nlohmann::json{{"par_id","does-not-exist"}}.dump(), "application/json");
|
|
|
+ ASSERT_TRUE(orphan);
|
|
|
+ EXPECT_GE(orphan->status, 400) << orphan->body;
|
|
|
+ EXPECT_LT(orphan->status, 500) << "a dangling reference is the caller's fault: " << orphan->body;
|
|
|
+}
|
|
|
+
|
|
|
+TEST_F(ApiFixture, DeletingAMissingDocumentStaysNotFound) {
|
|
|
+ // Guards the delete path while its failure handling gains codes: a plain
|
|
|
+ // missing document must keep answering 404, not a database error.
|
|
|
+ auto c = admin();
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","del"},{"kind","json"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+ auto r = c.Delete((base + "/del/documents/nope").c_str());
|
|
|
+ ASSERT_TRUE(r);
|
|
|
+ EXPECT_EQ(r->status, 404) << r->body;
|
|
|
+}
|
|
|
+
|
|
|
+TEST_F(ApiFixture, CollectionReportsRelationsEnforced) {
|
|
|
+ // Before client 2.11.4 nothing could read this back, so the web UI could
|
|
|
+ // only offer it as a fire-and-forget action.
|
|
|
+ auto c = admin();
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","enf"},{"kind","json"}}.dump(),
|
|
|
+ "application/json")->status, 201);
|
|
|
+ const std::string one = base + "/enf";
|
|
|
+
|
|
|
+ auto readFlag = [&]() -> std::optional<bool> {
|
|
|
+ auto r = c.Get(one.c_str());
|
|
|
+ EXPECT_EQ(r->status, 200);
|
|
|
+ const nlohmann::json j = nlohmann::json::parse(r->body);
|
|
|
+ if (!j.contains("relations_enforced")) return std::nullopt;
|
|
|
+ return j["relations_enforced"].get<bool>();
|
|
|
+ };
|
|
|
+
|
|
|
+ // Never configured: enforcement is the default, and it is readable.
|
|
|
+ auto initial = readFlag();
|
|
|
+ ASSERT_TRUE(initial.has_value()) << "relations_enforced missing from the collection response";
|
|
|
+ EXPECT_TRUE(*initial);
|
|
|
+
|
|
|
+ ASSERT_EQ(c.Put((one + "/relations-enforced").c_str(),
|
|
|
+ nlohmann::json{{"enforced",false}}.dump(), "application/json")->status, 200);
|
|
|
+ auto off = readFlag();
|
|
|
+ ASSERT_TRUE(off.has_value());
|
|
|
+ EXPECT_FALSE(*off) << "the collection still reports enforcement after disabling it";
|
|
|
+
|
|
|
+ ASSERT_EQ(c.Put((one + "/relations-enforced").c_str(),
|
|
|
+ nlohmann::json{{"enforced",true}}.dump(), "application/json")->status, 200);
|
|
|
+ auto on = readFlag();
|
|
|
+ ASSERT_TRUE(on.has_value());
|
|
|
+ EXPECT_TRUE(*on);
|
|
|
+}
|