|
@@ -249,7 +249,7 @@ TEST_F(ApiFixture, ProjectRecycleAfterDelete) {
|
|
|
ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","users"},{"kind","json"}}.dump(), "application/json")->status, 201);
|
|
ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","users"},{"kind","json"}}.dump(), "application/json")->status, 201);
|
|
|
// Delete the project (should purge collections + registry)
|
|
// Delete the project (should purge collections + registry)
|
|
|
ASSERT_EQ(c.Delete(("/api/v1/projects/" + p2).c_str())->status, 200);
|
|
ASSERT_EQ(c.Delete(("/api/v1/projects/" + p2).c_str())->status, 200);
|
|
|
- // Re-create project and collection — must succeed (201, not 422)
|
|
|
|
|
|
|
+ // Re-create project and collection - must succeed (201, not 422)
|
|
|
ASSERT_EQ(c.Post("/api/v1/projects", nlohmann::json{{"name", p2}}.dump(), "application/json")->status, 201);
|
|
ASSERT_EQ(c.Post("/api/v1/projects", nlohmann::json{{"name", p2}}.dump(), "application/json")->status, 201);
|
|
|
auto r = c.Post(base.c_str(), nlohmann::json{{"name","users"},{"kind","json"}}.dump(), "application/json");
|
|
auto r = c.Post(base.c_str(), nlohmann::json{{"name","users"},{"kind","json"}}.dump(), "application/json");
|
|
|
ASSERT_TRUE(r);
|
|
ASSERT_TRUE(r);
|
|
@@ -532,7 +532,7 @@ TEST_F(ApiFixture, EmbeddingCacheBypassedByNoCacheControlHeader) {
|
|
|
// in a key scope; a leaked publishable key must not be able to wipe the catalog.
|
|
// in a key scope; a leaked publishable key must not be able to wipe the catalog.
|
|
|
TEST_F(ApiFixture, ScopedKeyCannotManageCollections) {
|
|
TEST_F(ApiFixture, ScopedKeyCannotManageCollections) {
|
|
|
// Build a capability-scoped key directly in the DB (key create API does not yet
|
|
// Build a capability-scoped key directly in the DB (key create API does not yet
|
|
|
- // expose scope on POST — that is task U4). The key is granted project_ with a
|
|
|
|
|
|
|
+ // expose scope on POST - that is task U4). The key is granted project_ with a
|
|
|
// single data-level rule so it is a real scoped key (k.scope is populated).
|
|
// single data-level rule so it is a real scoped key (k.scope is populated).
|
|
|
svapi::ApiKey scopedKey;
|
|
svapi::ApiKey scopedKey;
|
|
|
scopedKey.id = svapi::generateApiKey();
|
|
scopedKey.id = svapi::generateApiKey();
|
|
@@ -551,18 +551,18 @@ TEST_F(ApiFixture, ScopedKeyCannotManageCollections) {
|
|
|
}
|
|
}
|
|
|
// Insert directly into the key collection so the server's KeyStore picks it up.
|
|
// Insert directly into the key collection so the server's KeyStore picks it up.
|
|
|
db_->client().upsert(keysColl_, scopedKey.toJson(), scopedKey.id);
|
|
db_->client().upsert(keysColl_, scopedKey.toJson(), scopedKey.id);
|
|
|
- keys_->bootstrap(""); // triggers reload() — bootstrap is idempotent if keys exist
|
|
|
|
|
|
|
+ keys_->bootstrap(""); // triggers reload() - bootstrap is idempotent if keys exist
|
|
|
|
|
|
|
|
const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
auto scoped = http(scopedKey.key);
|
|
auto scoped = http(scopedKey.key);
|
|
|
|
|
|
|
|
- // POST /collections — scoped key must be denied (403)
|
|
|
|
|
|
|
+ // POST /collections - scoped key must be denied (403)
|
|
|
auto createRes = scoped.Post(base.c_str(),
|
|
auto createRes = scoped.Post(base.c_str(),
|
|
|
nlohmann::json{{"name", "scopedtest"}, {"kind", "json"}}.dump(), "application/json");
|
|
nlohmann::json{{"name", "scopedtest"}, {"kind", "json"}}.dump(), "application/json");
|
|
|
ASSERT_TRUE(createRes);
|
|
ASSERT_TRUE(createRes);
|
|
|
EXPECT_EQ(createRes->status, 403) << "scoped key must not create collections; body: " << createRes->body;
|
|
EXPECT_EQ(createRes->status, 403) << "scoped key must not create collections; body: " << createRes->body;
|
|
|
|
|
|
|
|
- // DELETE /collections/<name> — scoped key must be denied (403)
|
|
|
|
|
|
|
+ // DELETE /collections/<name> - scoped key must be denied (403)
|
|
|
auto delRes = scoped.Delete((base + "/any_collection").c_str());
|
|
auto delRes = scoped.Delete((base + "/any_collection").c_str());
|
|
|
ASSERT_TRUE(delRes);
|
|
ASSERT_TRUE(delRes);
|
|
|
EXPECT_EQ(delRes->status, 403) << "scoped key must not delete collections; body: " << delRes->body;
|
|
EXPECT_EQ(delRes->status, 403) << "scoped key must not delete collections; body: " << delRes->body;
|
|
@@ -578,7 +578,7 @@ TEST_F(ApiFixture, ScopedKeyCannotManageCollections) {
|
|
|
db_->client().remove(keysColl_, scopedKey.id);
|
|
db_->client().remove(keysColl_, scopedKey.id);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// U5a: scoped key op+collection matrix — only explicitly granted (collection, op) pairs succeed.
|
|
|
|
|
|
|
+// U5a: scoped key op+collection matrix - only explicitly granted (collection, op) pairs succeed.
|
|
|
TEST_F(ApiFixture, ScopedKeyOpAndCollectionMatrix) {
|
|
TEST_F(ApiFixture, ScopedKeyOpAndCollectionMatrix) {
|
|
|
// Create collections: designs (json), quotes (json), other (json).
|
|
// Create collections: designs (json), quotes (json), other (json).
|
|
|
const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
@@ -664,7 +664,7 @@ TEST_F(ApiFixture, ScopedKeyOpAndCollectionMatrix) {
|
|
|
db_->client().remove(keysColl_, sk.id);
|
|
db_->client().remove(keysColl_, sk.id);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// U5b: scoped key with an origins list enforces the Origin header — fails closed (no header → 403).
|
|
|
|
|
|
|
+// U5b: scoped key with an origins list enforces the Origin header - fails closed (no header → 403).
|
|
|
TEST_F(ApiFixture, ScopedKeyOriginEnforced) {
|
|
TEST_F(ApiFixture, ScopedKeyOriginEnforced) {
|
|
|
const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
const std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
ASSERT_EQ(admin().Post(base.c_str(),
|
|
ASSERT_EQ(admin().Post(base.c_str(),
|
|
@@ -1001,16 +1001,21 @@ TEST_F(ApiFixture, IndexValuesRejectsOutOfRangeLimit) {
|
|
|
ASSERT_TRUE(absent); EXPECT_EQ(absent->status, 200);
|
|
ASSERT_TRUE(absent); EXPECT_EQ(absent->status, 200);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Named "uqdup" rather than "uq": a database run before the index-undeclare
|
|
|
|
|
+// fixes left a unique sku index declared on the dead collection
|
|
|
|
|
+// svapitest_apiproj:uq, and that declaration cannot be removed through any
|
|
|
|
|
+// client call, so the old name reports already_existed forever on a persistent
|
|
|
|
|
+// database. See the dropCollection caveat in CLAUDE.md.
|
|
|
TEST_F(ApiFixture, UniqueIndexRejectsExistingDuplicates) {
|
|
TEST_F(ApiFixture, UniqueIndexRejectsExistingDuplicates) {
|
|
|
auto c = admin();
|
|
auto c = admin();
|
|
|
std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
- ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","uq"},{"kind","json"}}.dump(),
|
|
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","uqdup"},{"kind","json"}}.dump(),
|
|
|
"application/json")->status, 201);
|
|
"application/json")->status, 201);
|
|
|
- std::string docs = base + "/uq/documents";
|
|
|
|
|
|
|
+ std::string docs = base + "/uqdup/documents";
|
|
|
ASSERT_EQ(c.Post(docs.c_str(), nlohmann::json{{"sku","A1"}}.dump(), "application/json")->status, 201);
|
|
ASSERT_EQ(c.Post(docs.c_str(), nlohmann::json{{"sku","A1"}}.dump(), "application/json")->status, 201);
|
|
|
ASSERT_EQ(c.Post(docs.c_str(), nlohmann::json{{"sku","A1"}}.dump(), "application/json")->status, 201);
|
|
ASSERT_EQ(c.Post(docs.c_str(), nlohmann::json{{"sku","A1"}}.dump(), "application/json")->status, 201);
|
|
|
|
|
|
|
|
- auto conflict = c.Post((base + "/uq/indexes").c_str(),
|
|
|
|
|
|
|
+ auto conflict = c.Post((base + "/uqdup/indexes").c_str(),
|
|
|
nlohmann::json{{"field","sku"},{"unique",true}}.dump(), "application/json");
|
|
nlohmann::json{{"field","sku"},{"unique",true}}.dump(), "application/json");
|
|
|
ASSERT_TRUE(conflict); EXPECT_EQ(conflict->status, 409);
|
|
ASSERT_TRUE(conflict); EXPECT_EQ(conflict->status, 409);
|
|
|
auto body = nlohmann::json::parse(conflict->body);
|
|
auto body = nlohmann::json::parse(conflict->body);
|
|
@@ -2105,3 +2110,349 @@ TEST_F(ApiFixture, ScopedUpdateOnlyKeyCannotCreateViaPut) {
|
|
|
|
|
|
|
|
db_->client().remove(keysColl_, sk.id);
|
|
db_->client().remove(keysColl_, sk.id);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+// --- Composite unique constraints (database 2.12.0) -------------------------
|
|
|
|
|
+//
|
|
|
|
|
+// A composite is declared on the SAME route as a single-field index, with
|
|
|
|
|
+// `fields` instead of `field`: same grant, same backfill, same
|
|
|
|
|
+// rows_indexed/already_existed/duplicate_values vocabulary. These tests pin the
|
|
|
|
|
+// three things a caller can get wrong (both spellings at once, one field, an
|
|
|
|
|
+// explicit unique:false) and the one thing the feature is for - that the
|
|
|
|
|
+// COMBINATION is enforced while a single column stays free to repeat.
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, CompositeUniqueIndexDeclareEnforceListDrop) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","cuq"},{"kind","json"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ const std::string idx = base + "/cuq/indexes";
|
|
|
|
|
+ const std::string docs = base + "/cuq/documents";
|
|
|
|
|
+
|
|
|
|
|
+ auto made = c.Post(idx.c_str(),
|
|
|
|
|
+ nlohmann::json{{"fields",{"chat_id","file_unique_id"}}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ ASSERT_TRUE(made); ASSERT_EQ(made->status, 201) << made->body;
|
|
|
|
|
+ auto madeJson = nlohmann::json::parse(made->body);
|
|
|
|
|
+ ASSERT_TRUE(madeJson.contains("fields"));
|
|
|
|
|
+ EXPECT_EQ(madeJson["fields"], nlohmann::json({"chat_id","file_unique_id"}));
|
|
|
|
|
+ EXPECT_TRUE(madeJson["unique"].get<bool>());
|
|
|
|
|
+ EXPECT_TRUE(madeJson.contains("already_existed"));
|
|
|
|
|
+
|
|
|
|
|
+ // Re-declaring is idempotent, exactly as it is for a single field.
|
|
|
|
|
+ auto again = c.Post(idx.c_str(),
|
|
|
|
|
+ nlohmann::json{{"fields",{"chat_id","file_unique_id"}}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ ASSERT_TRUE(again); EXPECT_EQ(again->status, 201) << again->body;
|
|
|
|
|
+ EXPECT_TRUE(nlohmann::json::parse(again->body)["already_existed"].get<bool>());
|
|
|
|
|
+
|
|
|
|
|
+ // GET keeps its existing shape for single-field indexes and reports the
|
|
|
|
|
+ // composite under its own key - a new key, so an existing reader of
|
|
|
|
|
+ // `indexes` is untouched.
|
|
|
|
|
+ auto list = c.Get(idx.c_str());
|
|
|
|
|
+ ASSERT_TRUE(list); ASSERT_EQ(list->status, 200) << list->body;
|
|
|
|
|
+ auto listJson = nlohmann::json::parse(list->body);
|
|
|
|
|
+ ASSERT_TRUE(listJson.contains("indexes"));
|
|
|
|
|
+ ASSERT_TRUE(listJson.contains("composite_unique"));
|
|
|
|
|
+ bool found = false;
|
|
|
|
|
+ for (auto& e : listJson["composite_unique"]) {
|
|
|
|
|
+ if (e["fields"] == nlohmann::json({"chat_id","file_unique_id"})) {
|
|
|
|
|
+ found = true;
|
|
|
|
|
+ EXPECT_TRUE(e["unique"].get<bool>());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ EXPECT_TRUE(found) << list->body;
|
|
|
|
|
+
|
|
|
|
|
+ // The COMBINATION is what is constrained: either column alone may repeat.
|
|
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(),
|
|
|
|
|
+ nlohmann::json{{"chat_id",1},{"file_unique_id","f1"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(),
|
|
|
|
|
+ nlohmann::json{{"chat_id",1},{"file_unique_id","f2"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(),
|
|
|
|
|
+ nlohmann::json{{"chat_id",2},{"file_unique_id","f1"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+
|
|
|
|
|
+ auto dup = c.Post(docs.c_str(),
|
|
|
|
|
+ nlohmann::json{{"chat_id",1},{"file_unique_id","f1"}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ ASSERT_TRUE(dup); EXPECT_EQ(dup->status, 409) << dup->body;
|
|
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(dup->body)["error"]["code"], "duplicate_values");
|
|
|
|
|
+
|
|
|
|
|
+ // Dropped by its exact field list, in a repeatable query parameter.
|
|
|
|
|
+ auto dropped = c.Delete((idx + "?fields=chat_id&fields=file_unique_id").c_str());
|
|
|
|
|
+ ASSERT_TRUE(dropped); EXPECT_EQ(dropped->status, 200) << dropped->body;
|
|
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(dropped->body)["dropped_fields"],
|
|
|
|
|
+ nlohmann::json({"chat_id","file_unique_id"}));
|
|
|
|
|
+
|
|
|
|
|
+ // Gone from the listing, and the write it refused now succeeds.
|
|
|
|
|
+ auto after = nlohmann::json::parse(c.Get(idx.c_str())->body);
|
|
|
|
|
+ for (auto& e : after["composite_unique"])
|
|
|
|
|
+ EXPECT_NE(e["fields"], nlohmann::json({"chat_id","file_unique_id"}));
|
|
|
|
|
+ EXPECT_EQ(c.Post(docs.c_str(),
|
|
|
|
|
+ nlohmann::json{{"chat_id",1},{"file_unique_id","f1"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// A document missing any of the fields does not participate, which is the SQL
|
|
|
|
|
+// rule for a NULL in a unique key. Worth an integration test because it is the
|
|
|
|
|
+// one part of the semantics the API cannot see: it is enforced in the database.
|
|
|
|
|
+TEST_F(ApiFixture, CompositeUniqueIgnoresAbsentAndNullFields) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","cuqnull"},{"kind","json"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ ASSERT_EQ(c.Post((base + "/cuqnull/indexes").c_str(),
|
|
|
|
|
+ nlohmann::json{{"fields",{"chat_id","file_unique_id"}}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ const std::string docs = base + "/cuqnull/documents";
|
|
|
|
|
+
|
|
|
|
|
+ // Same chat_id, file_unique_id absent in both: not constrained.
|
|
|
|
|
+ EXPECT_EQ(c.Post(docs.c_str(), nlohmann::json{{"chat_id",9}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ EXPECT_EQ(c.Post(docs.c_str(), nlohmann::json{{"chat_id",9}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+
|
|
|
|
|
+ // Same again with an explicit null.
|
|
|
|
|
+ EXPECT_EQ(c.Post(docs.c_str(),
|
|
|
|
|
+ nlohmann::json{{"chat_id",10},{"file_unique_id",nullptr}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ EXPECT_EQ(c.Post(docs.c_str(),
|
|
|
|
|
+ nlohmann::json{{"chat_id",10},{"file_unique_id",nullptr}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// Declaring a composite over rows that ALREADY collide is refused, with the
|
|
|
|
|
+// colliding ids - the same answer, and the same slug, as the single-field case.
|
|
|
|
|
+TEST_F(ApiFixture, CompositeUniqueIndexRejectsExistingDuplicates) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","cuqdup"},{"kind","json"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ const std::string docs = base + "/cuqdup/documents";
|
|
|
|
|
+ for (int i = 0; i < 2; ++i)
|
|
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(),
|
|
|
|
|
+ nlohmann::json{{"chat_id",5},{"file_unique_id","same"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+
|
|
|
|
|
+ auto conflict = c.Post((base + "/cuqdup/indexes").c_str(),
|
|
|
|
|
+ nlohmann::json{{"fields",{"chat_id","file_unique_id"}}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ ASSERT_TRUE(conflict); ASSERT_EQ(conflict->status, 409) << conflict->body;
|
|
|
|
|
+ auto body = nlohmann::json::parse(conflict->body);
|
|
|
|
|
+ EXPECT_EQ(body["error"]["code"], "duplicate_values");
|
|
|
|
|
+ ASSERT_TRUE(body["error"]["details"].contains("duplicate_examples"));
|
|
|
|
|
+ EXPECT_GE(body["error"]["details"]["duplicate_examples"].size(), 1u);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// `field` and `fields` are two spellings of one request. Sending both is
|
|
|
|
|
+// refused rather than resolved by precedence, which would silently discard one
|
|
|
|
|
+// of the caller's two intents.
|
|
|
|
|
+TEST_F(ApiFixture, IndexCreateRefusesFieldAndFieldsTogether) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","cuqamb"},{"kind","json"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+
|
|
|
|
|
+ auto both = c.Post((base + "/cuqamb/indexes").c_str(),
|
|
|
|
|
+ nlohmann::json{{"field","chat_id"},{"fields",{"chat_id","file_unique_id"}}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ ASSERT_TRUE(both); EXPECT_EQ(both->status, 422) << both->body;
|
|
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(both->body)["error"]["code"], "ambiguous_body");
|
|
|
|
|
+
|
|
|
|
|
+ // Nothing was declared by the refused request.
|
|
|
|
|
+ auto list = nlohmann::json::parse(c.Get((base + "/cuqamb/indexes").c_str())->body);
|
|
|
|
|
+ EXPECT_TRUE(list["composite_unique"].empty()) << list.dump();
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, CompositeIndexRequestValidation) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","cuqval"},{"kind","json"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ const std::string idx = base + "/cuqval/indexes";
|
|
|
|
|
+ const std::string ct = "application/json";
|
|
|
|
|
+
|
|
|
|
|
+ // One field is not a composite; the caller means the single-field form.
|
|
|
|
|
+ EXPECT_EQ(c.Post(idx.c_str(), nlohmann::json{{"fields",{"chat_id"}}}.dump(), ct)->status, 422);
|
|
|
|
|
+ // A composite exists only as a unique constraint.
|
|
|
|
|
+ EXPECT_EQ(c.Post(idx.c_str(),
|
|
|
|
|
+ nlohmann::json{{"fields",{"a","b"}},{"unique",false}}.dump(), ct)->status, 422);
|
|
|
|
|
+ // Identified by its exact list, so a repeated field is two constraints for one rule.
|
|
|
|
|
+ EXPECT_EQ(c.Post(idx.c_str(), nlohmann::json{{"fields",{"a","a"}}}.dump(), ct)->status, 422);
|
|
|
|
|
+ EXPECT_EQ(c.Post(idx.c_str(), nlohmann::json{{"fields",{"a",7}}}.dump(), ct)->status, 422);
|
|
|
|
|
+ EXPECT_EQ(c.Post(idx.c_str(), nlohmann::json{{"fields","a,b"}}.dump(), ct)->status, 422);
|
|
|
|
|
+ EXPECT_EQ(c.Post(idx.c_str(), nlohmann::json{{"fields",{"a",""}}}.dump(), ct)->status, 422);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// The drop never means "drop everything": an unnamed or single-field list is a
|
|
|
|
|
+// client whose query-string building went wrong, and gets 422. A list naming no
|
|
|
|
|
+// existing constraint is NOT an error - the database's drop is idempotent, so
|
|
|
|
|
+// the requested end state is already reached and the answer is 200. That is
|
|
|
|
|
+// also why a falsey drop must not be reported as 404: it means the call failed.
|
|
|
|
|
+TEST_F(ApiFixture, CompositeIndexDropValidation) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","cuqdrop"},{"kind","json"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ const std::string idx = base + "/cuqdrop/indexes";
|
|
|
|
|
+
|
|
|
|
|
+ EXPECT_EQ(c.Delete(idx.c_str())->status, 422);
|
|
|
|
|
+ EXPECT_EQ(c.Delete((idx + "?fields=chat_id").c_str())->status, 422);
|
|
|
|
|
+ EXPECT_EQ(c.Delete((idx + "?fields=a&fields=a").c_str())->status, 422);
|
|
|
|
|
+ // Idempotent, and it echoes back exactly what the caller named.
|
|
|
|
|
+ auto gone = c.Delete((idx + "?fields=nope_a&fields=nope_b").c_str());
|
|
|
|
|
+ ASSERT_EQ(gone->status, 200);
|
|
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(gone->body)["dropped_fields"],
|
|
|
|
|
+ nlohmann::json({"nope_a", "nope_b"}));
|
|
|
|
|
+ // Dropping twice is the same answer, which is what idempotent has to mean.
|
|
|
|
|
+ EXPECT_EQ(c.Delete((idx + "?fields=nope_a&fields=nope_b").c_str())->status, 200);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// --- Atomic increment (database 2.12.0) ------------------------------------
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, IncrementAddsAndReturnsNewValues) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","counters"},{"kind","json"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ const std::string docs = base + "/counters/documents";
|
|
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(), nlohmann::json{{"id","day-1"},{"calls",17}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+
|
|
|
|
|
+ // `minutes` is absent and counts as 0, so it comes back as the delta itself.
|
|
|
|
|
+ auto bumped = c.Post((docs + "/day-1/increment").c_str(),
|
|
|
|
|
+ nlohmann::json{{"calls",1},{"minutes",3.5}}.dump(), "application/json");
|
|
|
|
|
+ ASSERT_TRUE(bumped); ASSERT_EQ(bumped->status, 200) << bumped->body;
|
|
|
|
|
+ auto body = nlohmann::json::parse(bumped->body);
|
|
|
|
|
+ EXPECT_EQ(body["id"], "day-1");
|
|
|
|
|
+ ASSERT_TRUE(body.contains("values"));
|
|
|
|
|
+ EXPECT_EQ(body["values"]["calls"], 18);
|
|
|
|
|
+ EXPECT_DOUBLE_EQ(body["values"]["minutes"].get<double>(), 3.5);
|
|
|
|
|
+
|
|
|
|
|
+ // A negative delta decrements; the stored document agrees with the answer.
|
|
|
|
|
+ auto down = c.Post((docs + "/day-1/increment").c_str(),
|
|
|
|
|
+ nlohmann::json{{"calls",-8}}.dump(), "application/json");
|
|
|
|
|
+ ASSERT_TRUE(down); ASSERT_EQ(down->status, 200) << down->body;
|
|
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(down->body)["values"]["calls"], 10);
|
|
|
|
|
+ auto stored = nlohmann::json::parse(c.Get((docs + "/day-1").c_str())->body);
|
|
|
|
|
+ EXPECT_EQ(stored["calls"], 10);
|
|
|
|
|
+
|
|
|
|
|
+ // A dotted key addresses a nested field.
|
|
|
|
|
+ auto nested = c.Post((docs + "/day-1/increment").c_str(),
|
|
|
|
|
+ nlohmann::json{{"counts.sent",2}}.dump(), "application/json");
|
|
|
|
|
+ ASSERT_TRUE(nested); ASSERT_EQ(nested->status, 200) << nested->body;
|
|
|
|
|
+ auto deep = nlohmann::json::parse(c.Get((docs + "/day-1").c_str())->body);
|
|
|
|
|
+ EXPECT_EQ(deep["counts"]["sent"], 2) << deep.dump();
|
|
|
|
|
+
|
|
|
|
|
+ // The `{"data":...}` wrapper works here too, so a client that wraps every
|
|
|
|
|
+ // body does not have to stop wrapping for this one route.
|
|
|
|
|
+ auto wrapped = c.Post((docs + "/day-1/increment").c_str(),
|
|
|
|
|
+ nlohmann::json{{"data",{{"calls",1}}}}.dump(), "application/json");
|
|
|
|
|
+ ASSERT_TRUE(wrapped); ASSERT_EQ(wrapped->status, 200) << wrapped->body;
|
|
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(wrapped->body)["values"]["calls"], 11);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// It never creates: the starting document is the caller's to describe, and the
|
|
|
|
|
+// documented idiom is increment -> 404 -> POST at a fixed id -> increment again.
|
|
|
|
|
+TEST_F(ApiFixture, IncrementRequiresTheDocumentToExist) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","counters404"},{"kind","json"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ const std::string docs = base + "/counters404/documents";
|
|
|
|
|
+
|
|
|
|
|
+ auto missing = c.Post((docs + "/never-made/increment").c_str(),
|
|
|
|
|
+ nlohmann::json{{"calls",1}}.dump(), "application/json");
|
|
|
|
|
+ ASSERT_TRUE(missing); EXPECT_EQ(missing->status, 404) << missing->body;
|
|
|
|
|
+ EXPECT_EQ(c.Get((docs + "/never-made").c_str())->status, 404);
|
|
|
|
|
+
|
|
|
|
|
+ // The idiom, end to end.
|
|
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(), nlohmann::json{{"id","never-made"},{"calls",1}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ auto now = c.Post((docs + "/never-made/increment").c_str(),
|
|
|
|
|
+ nlohmann::json{{"calls",1}}.dump(), "application/json");
|
|
|
|
|
+ ASSERT_TRUE(now); ASSERT_EQ(now->status, 200) << now->body;
|
|
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(now->body)["values"]["calls"], 2);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// The whole set applies or none of it does, so a non-number anywhere must leave
|
|
|
|
|
+// every other counter in the request untouched.
|
|
|
|
|
+TEST_F(ApiFixture, IncrementRejectsNonNumbersAndAppliesNothing) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","counters422"},{"kind","json"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ const std::string docs = base + "/counters422/documents";
|
|
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(),
|
|
|
|
|
+ nlohmann::json{{"id","d"},{"calls",4},{"label","hello"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ const std::string inc = docs + "/d/increment";
|
|
|
|
|
+ const std::string ct = "application/json";
|
|
|
|
|
+
|
|
|
|
|
+ // Our own refusals: a non-numeric delta, an empty body, an empty path
|
|
|
|
|
+ // segment, and `data` beside siblings.
|
|
|
|
|
+ EXPECT_EQ(c.Post(inc.c_str(), nlohmann::json{{"calls","1"}}.dump(), ct)->status, 422);
|
|
|
|
|
+ EXPECT_EQ(c.Post(inc.c_str(), nlohmann::json{{"calls",true}}.dump(), ct)->status, 422);
|
|
|
|
|
+ EXPECT_EQ(c.Post(inc.c_str(), nlohmann::json::object().dump(), ct)->status, 422);
|
|
|
|
|
+ EXPECT_EQ(c.Post(inc.c_str(), nlohmann::json{{"counts..sent",1}}.dump(), ct)->status, 422);
|
|
|
|
|
+ auto ambiguous = c.Post(inc.c_str(),
|
|
|
|
|
+ nlohmann::json{{"data",{{"calls",1}}},{"minutes",1}}.dump(), ct);
|
|
|
|
|
+ ASSERT_TRUE(ambiguous); ASSERT_EQ(ambiguous->status, 422) << ambiguous->body;
|
|
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(ambiguous->body)["error"]["code"], "ambiguous_body");
|
|
|
|
|
+
|
|
|
|
|
+ // The database's own refusal: `label` holds a string. 422, and NOTHING is
|
|
|
|
|
+ // applied - `calls` must still be 4.
|
|
|
|
|
+ auto dbRefusal = c.Post(inc.c_str(), nlohmann::json{{"calls",1},{"label",1}}.dump(), ct);
|
|
|
|
|
+ ASSERT_TRUE(dbRefusal); EXPECT_EQ(dbRefusal->status, 422) << dbRefusal->body;
|
|
|
|
|
+ auto stored = nlohmann::json::parse(c.Get((docs + "/d").c_str())->body);
|
|
|
|
|
+ EXPECT_EQ(stored["calls"], 4) << stored.dump();
|
|
|
|
|
+ EXPECT_EQ(stored["label"], "hello");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// It is a WRITE: a scope granting only `read` must not reach it, and `update`
|
|
|
|
|
+// is what authorizes it - the same grant that could already set the number
|
|
|
|
|
+// directly.
|
|
|
|
|
+TEST_F(ApiFixture, IncrementNeedsTheUpdateGrant) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ std::string base = "/api/v1/projects/" + project_ + "/collections";
|
|
|
|
|
+ ASSERT_EQ(c.Post(base.c_str(), nlohmann::json{{"name","counters403"},{"kind","json"}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ const std::string docs = base + "/counters403/documents";
|
|
|
|
|
+ ASSERT_EQ(c.Post(docs.c_str(), nlohmann::json{{"id","d"},{"calls",1}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ const std::string inc = docs + "/d/increment";
|
|
|
|
|
+
|
|
|
|
|
+ auto makeKey = [&](std::set<svapi::KeyOp> ops) {
|
|
|
|
|
+ svapi::ApiKey sk;
|
|
|
|
|
+ sk.id = svapi::generateApiKey();
|
|
|
|
|
+ sk.key = svapi::generateApiKey();
|
|
|
|
|
+ sk.label = "increment-grant";
|
|
|
|
|
+ sk.projects = {project_};
|
|
|
|
|
+ sk.admin = false;
|
|
|
|
|
+ sk.createdAt = 0;
|
|
|
|
|
+ svapi::KeyScope sc;
|
|
|
|
|
+ svapi::KeyScopeRule r;
|
|
|
|
|
+ r.collection = "counters403";
|
|
|
|
|
+ r.ops = std::move(ops);
|
|
|
|
|
+ sc.rules.push_back(std::move(r));
|
|
|
|
|
+ sk.scope = sc;
|
|
|
|
|
+ db_->client().upsert(keysColl_, sk.toJson(), sk.id);
|
|
|
|
|
+ keys_->bootstrap("");
|
|
|
|
|
+ return sk;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ svapi::ApiKey readOnly = makeKey({svapi::KeyOp::Read});
|
|
|
|
|
+ auto denied = http(readOnly.key).Post(inc.c_str(), nlohmann::json{{"calls",1}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ ASSERT_TRUE(denied); EXPECT_EQ(denied->status, 403) << denied->body;
|
|
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(admin().Get((docs + "/d").c_str())->body)["calls"], 1);
|
|
|
|
|
+ db_->client().remove(keysColl_, readOnly.id);
|
|
|
|
|
+
|
|
|
|
|
+ svapi::ApiKey writer = makeKey({svapi::KeyOp::Update});
|
|
|
|
|
+ auto allowed = http(writer.key).Post(inc.c_str(), nlohmann::json{{"calls",1}}.dump(),
|
|
|
|
|
+ "application/json");
|
|
|
|
|
+ ASSERT_TRUE(allowed); EXPECT_EQ(allowed->status, 200) << allowed->body;
|
|
|
|
|
+ EXPECT_EQ(nlohmann::json::parse(allowed->body)["values"]["calls"], 2);
|
|
|
|
|
+ db_->client().remove(keysColl_, writer.id);
|
|
|
|
|
+}
|