|
@@ -2379,6 +2379,208 @@ TEST_F(ApiFixture, ARecycledCollectionNameInheritsNoConstraint) {
|
|
|
"be refused; body: " << declared->body;
|
|
"be refused; body: " << declared->body;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// --- Project files ---------------------------------------------------------
|
|
|
|
|
+//
|
|
|
|
|
+// The store is name-addressed per project, and PROJECT ISOLATION IS OURS: the
|
|
|
|
|
+// database namespaces files by the client's project, this service runs one
|
|
|
|
|
+// client for `default`, and separation comes only from the related_id we stamp
|
|
|
|
|
+// and filter on. So the isolation test below is not a nice-to-have.
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, FileRoundTripKeepsBytesAndContentType) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/files/logo.png";
|
|
|
|
|
+ // Deliberately includes a NUL and high bytes: a store that went through a
|
|
|
|
|
+ // C string, or re-encoded as text, would corrupt exactly this.
|
|
|
|
|
+ std::string body("\x89PNG\r\n\x1a\n", 8);
|
|
|
|
|
+ body += std::string("\0\xff\xfe binary", 10);
|
|
|
|
|
+
|
|
|
|
|
+ auto put = c.Put(base.c_str(), body, "image/png");
|
|
|
|
|
+ ASSERT_TRUE(put); ASSERT_EQ(put->status, 201) << put->body;
|
|
|
|
|
+ auto meta = nlohmann::json::parse(put->body);
|
|
|
|
|
+ EXPECT_EQ(meta["name"], "logo.png");
|
|
|
|
|
+ EXPECT_EQ(meta["content_type"], "image/png");
|
|
|
|
|
+ EXPECT_EQ(meta["size"], body.size());
|
|
|
|
|
+ EXPECT_FALSE(meta["checksum"].get<std::string>().empty());
|
|
|
|
|
+
|
|
|
|
|
+ auto got = c.Get(base.c_str());
|
|
|
|
|
+ ASSERT_TRUE(got); ASSERT_EQ(got->status, 200);
|
|
|
|
|
+ EXPECT_EQ(got->body, body) << "bytes must round-trip exactly";
|
|
|
|
|
+ EXPECT_EQ(got->get_header_value("Content-Type"), "image/png");
|
|
|
|
|
+
|
|
|
|
|
+ // A replacement is 200, not 201, so a caller can tell which happened.
|
|
|
|
|
+ auto again = c.Put(base.c_str(), std::string("replaced"), "text/plain");
|
|
|
|
|
+ ASSERT_TRUE(again); EXPECT_EQ(again->status, 200) << again->body;
|
|
|
|
|
+ auto after = c.Get(base.c_str());
|
|
|
|
|
+ ASSERT_TRUE(after);
|
|
|
|
|
+ EXPECT_EQ(after->body, "replaced") << "a replacement must not leave the old bytes readable";
|
|
|
|
|
+ EXPECT_EQ(after->get_header_value("Content-Type"), "text/plain");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, FilesAreIsolatedBetweenProjects) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ const std::string other = project_ + "b";
|
|
|
|
|
+ ASSERT_EQ(c.Post("/api/v1/projects", nlohmann::json{{"name", other}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+
|
|
|
|
|
+ const std::string mine = "/api/v1/projects/" + project_ + "/files/shared.txt";
|
|
|
|
|
+ const std::string yours = "/api/v1/projects/" + other + "/files/shared.txt";
|
|
|
|
|
+ ASSERT_EQ(c.Put(mine.c_str(), std::string("MINE"), "text/plain")->status, 201);
|
|
|
|
|
+ ASSERT_EQ(c.Put(yours.c_str(), std::string("YOURS"), "text/plain")->status, 201);
|
|
|
|
|
+
|
|
|
|
|
+ // The same name in two projects is two files, and neither sees the other.
|
|
|
|
|
+ auto a = c.Get(mine.c_str());
|
|
|
|
|
+ auto b = c.Get(yours.c_str());
|
|
|
|
|
+ ASSERT_TRUE(a); ASSERT_TRUE(b);
|
|
|
|
|
+ EXPECT_EQ(a->body, "MINE");
|
|
|
|
|
+ EXPECT_EQ(b->body, "YOURS");
|
|
|
|
|
+
|
|
|
|
|
+ // And a listing shows only the project's own. Checked with a name that
|
|
|
|
|
+ // exists ONLY in the other project, because a shared name cannot tell the
|
|
|
|
|
+ // two apart - which is exactly the mistake worth not making in the test
|
|
|
|
|
+ // that guards tenancy.
|
|
|
|
|
+ const std::string onlyTheirs = "/api/v1/projects/" + other + "/files/theirs-only.txt";
|
|
|
|
|
+ ASSERT_EQ(c.Put(onlyTheirs.c_str(), std::string("theirs"), "text/plain")->status, 201);
|
|
|
|
|
+
|
|
|
|
|
+ auto list = c.Get(("/api/v1/projects/" + project_ + "/files").c_str());
|
|
|
|
|
+ ASSERT_TRUE(list); ASSERT_EQ(list->status, 200);
|
|
|
|
|
+ const auto files = nlohmann::json::parse(list->body)["files"];
|
|
|
|
|
+ int sawShared = 0;
|
|
|
|
|
+ for (const auto& f : files) {
|
|
|
|
|
+ EXPECT_NE(f["name"], "theirs-only.txt") << "another project's file appeared in our listing";
|
|
|
|
|
+ if (f["name"] == "shared.txt") ++sawShared;
|
|
|
|
|
+ }
|
|
|
|
|
+ EXPECT_EQ(sawShared, 1) << "our own shared.txt should appear exactly once";
|
|
|
|
|
+ // And their file is not reachable by name through our project either.
|
|
|
|
|
+ EXPECT_EQ(c.Get(("/api/v1/projects/" + project_ + "/files/theirs-only.txt").c_str())->status,
|
|
|
|
|
+ 404);
|
|
|
|
|
+
|
|
|
|
|
+ // Deleting one project's file leaves the other's readable. The database
|
|
|
|
|
+ // dedups identical content by checksum and a delete decrements a reference
|
|
|
|
|
+ // count, so this is the case where getting it wrong loses someone's data.
|
|
|
|
|
+ ASSERT_EQ(c.Delete(mine.c_str())->status, 200);
|
|
|
|
|
+ EXPECT_EQ(c.Get(mine.c_str())->status, 404);
|
|
|
|
|
+ auto survivor = c.Get(yours.c_str());
|
|
|
|
|
+ ASSERT_TRUE(survivor);
|
|
|
|
|
+ EXPECT_EQ(survivor->status, 200);
|
|
|
|
|
+ EXPECT_EQ(survivor->body, "YOURS");
|
|
|
|
|
+
|
|
|
|
|
+ c.Delete(("/api/v1/projects/" + other).c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, IdenticalContentUnderTwoNamesSurvivesDeletingOne) {
|
|
|
|
|
+ // The dedup case explicitly: same bytes, two names, delete one.
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ const std::string a = "/api/v1/projects/" + project_ + "/files/dup-a.bin";
|
|
|
|
|
+ const std::string b = "/api/v1/projects/" + project_ + "/files/dup-b.bin";
|
|
|
|
|
+ const std::string same = "IDENTICAL-CONTENT-FOR-DEDUP";
|
|
|
|
|
+ ASSERT_EQ(c.Put(a.c_str(), same, "application/octet-stream")->status, 201);
|
|
|
|
|
+ ASSERT_EQ(c.Put(b.c_str(), same, "application/octet-stream")->status, 201);
|
|
|
|
|
+ ASSERT_EQ(c.Delete(a.c_str())->status, 200);
|
|
|
|
|
+ auto survivor = c.Get(b.c_str());
|
|
|
|
|
+ ASSERT_TRUE(survivor);
|
|
|
|
|
+ ASSERT_EQ(survivor->status, 200) << "deleting one name destroyed the shared blob";
|
|
|
|
|
+ EXPECT_EQ(survivor->body, same);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, FileNamesAreValidatedNotFourOhFoured) {
|
|
|
|
|
+ // 422 with a reason, never 404: the name is unusable, not missing, and a
|
|
|
|
|
+ // caller told "not found" retries a name that can never work.
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/files/";
|
|
|
|
|
+ for (const char* bad : {"a:b", ".env", "sp%20ace"}) {
|
|
|
|
|
+ auto r = c.Get((base + bad).c_str());
|
|
|
|
|
+ ASSERT_TRUE(r) << bad;
|
|
|
|
|
+ EXPECT_EQ(r->status, 422) << bad << " -> " << r->body;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, AnOversizeFileIsRefused) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ // The default cap is 2 MiB, so 3 MiB must be refused rather than stored.
|
|
|
|
|
+ const std::string big(3 * 1024 * 1024, 'x');
|
|
|
|
|
+ auto r = c.Put(("/api/v1/projects/" + project_ + "/files/big.bin").c_str(), big,
|
|
|
|
|
+ "application/octet-stream");
|
|
|
|
|
+ ASSERT_TRUE(r);
|
|
|
|
|
+ EXPECT_EQ(r->status, 422) << r->body;
|
|
|
|
|
+ EXPECT_NE(r->body.find("file_too_large"), std::string::npos) << r->body;
|
|
|
|
|
+ // And nothing was stored under that name.
|
|
|
|
|
+ EXPECT_EQ(c.Get(("/api/v1/projects/" + project_ + "/files/big.bin").c_str())->status, 404);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, AnEmptyBodyIsRefused) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ auto r = c.Put(("/api/v1/projects/" + project_ + "/files/empty.bin").c_str(), std::string(),
|
|
|
|
|
+ "application/octet-stream");
|
|
|
|
|
+ ASSERT_TRUE(r);
|
|
|
|
|
+ EXPECT_EQ(r->status, 422) << r->body;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, AMissingFileIs404AndDeleteOfItToo) {
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ const std::string p = "/api/v1/projects/" + project_ + "/files/nope.bin";
|
|
|
|
|
+ EXPECT_EQ(c.Get(p.c_str())->status, 404);
|
|
|
|
|
+ EXPECT_EQ(c.Get((p + "/info").c_str())->status, 404);
|
|
|
|
|
+ EXPECT_EQ(c.Delete(p.c_str())->status, 404);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, DeletingAProjectTakesItsFiles) {
|
|
|
|
|
+ // Otherwise a recycled project name hands the next tenant the previous
|
|
|
|
|
+ // one's logo - the same class of bug as a recycled collection name
|
|
|
|
|
+ // inheriting its constraints.
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ const std::string tmp = project_ + "c";
|
|
|
|
|
+ ASSERT_EQ(c.Post("/api/v1/projects", nlohmann::json{{"name", tmp}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ const std::string f = "/api/v1/projects/" + tmp + "/files/asset.txt";
|
|
|
|
|
+ ASSERT_EQ(c.Put(f.c_str(), std::string("tenant data"), "text/plain")->status, 201);
|
|
|
|
|
+ ASSERT_EQ(c.Delete(("/api/v1/projects/" + tmp).c_str())->status, 200);
|
|
|
|
|
+
|
|
|
|
|
+ // Recreate the same project name; the file must be gone.
|
|
|
|
|
+ ASSERT_EQ(c.Post("/api/v1/projects", nlohmann::json{{"name", tmp}}.dump(),
|
|
|
|
|
+ "application/json")->status, 201);
|
|
|
|
|
+ auto leaked = c.Get(f.c_str());
|
|
|
|
|
+ ASSERT_TRUE(leaked);
|
|
|
|
|
+ EXPECT_EQ(leaked->status, 404) << "a deleted project's file outlived it: " << leaked->body;
|
|
|
|
|
+ c.Delete(("/api/v1/projects/" + tmp).c_str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST_F(ApiFixture, AScopedKeyNeedsTheFilesGrant) {
|
|
|
|
|
+ // Files answer to the same rule machinery as collections, under the
|
|
|
|
|
+ // reserved `_files` pseudo-collection. A key scoped to a collection must
|
|
|
|
|
+ // not reach them.
|
|
|
|
|
+ auto c = admin();
|
|
|
|
|
+ const std::string base = "/api/v1/projects/" + project_ + "/files/grant.txt";
|
|
|
|
|
+ ASSERT_EQ(c.Put(base.c_str(), std::string("secret-ish"), "text/plain")->status, 201);
|
|
|
|
|
+
|
|
|
|
|
+ // Scoped to a collection, not to files.
|
|
|
|
|
+ auto mk = c.Post("/api/v1/keys",
|
|
|
|
|
+ nlohmann::json{{"label","no-files"},{"projects",{project_}},{"admin",false},
|
|
|
|
|
+ {"scope",{{"rules",{{{"collection","something"},{"ops",{"read"}}}}}}}}
|
|
|
|
|
+ .dump(), "application/json");
|
|
|
|
|
+ ASSERT_TRUE(mk); ASSERT_EQ(mk->status, 201) << mk->body;
|
|
|
|
|
+ const std::string noFiles = nlohmann::json::parse(mk->body)["key"];
|
|
|
|
|
+ auto denied = request("GET", base, "", "", noFiles);
|
|
|
|
|
+ ASSERT_TRUE(denied);
|
|
|
|
|
+ EXPECT_EQ(denied->status, 403) << denied->body;
|
|
|
|
|
+
|
|
|
|
|
+ // Scoped to _files, it works.
|
|
|
|
|
+ auto mk2 = c.Post("/api/v1/keys",
|
|
|
|
|
+ nlohmann::json{{"label","yes-files"},{"projects",{project_}},{"admin",false},
|
|
|
|
|
+ {"scope",{{"rules",{{{"collection","_files"},{"ops",{"read"}}}}}}}}
|
|
|
|
|
+ .dump(), "application/json");
|
|
|
|
|
+ ASSERT_TRUE(mk2); ASSERT_EQ(mk2->status, 201) << mk2->body;
|
|
|
|
|
+ const std::string withFiles = nlohmann::json::parse(mk2->body)["key"];
|
|
|
|
|
+ auto allowed = request("GET", base, "", "", withFiles);
|
|
|
|
|
+ ASSERT_TRUE(allowed);
|
|
|
|
|
+ EXPECT_EQ(allowed->status, 200) << allowed->body;
|
|
|
|
|
+ EXPECT_EQ(allowed->body, "secret-ish");
|
|
|
|
|
+
|
|
|
|
|
+ // But a scoped key still cannot WRITE one - that is project-manage.
|
|
|
|
|
+ auto write = request("PUT", base, "nope", "text/plain", withFiles);
|
|
|
|
|
+ ASSERT_TRUE(write);
|
|
|
|
|
+ EXPECT_EQ(write->status, 403) << write->body;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// --- Atomic increment (database 2.12.0) ------------------------------------
|
|
// --- Atomic increment (database 2.12.0) ------------------------------------
|
|
|
|
|
|
|
|
TEST_F(ApiFixture, IncrementAddsAndReturnsNewValues) {
|
|
TEST_F(ApiFixture, IncrementAddsAndReturnsNewValues) {
|