mirror of
https://github.com/typesense/typesense.git
synced 2025-05-19 05:08:43 +08:00
Add tests for collection extraction for auth.
This commit is contained in:
parent
071750c663
commit
71b1a6e7a3
@ -80,7 +80,7 @@ void get_collections_for_auth(std::map<std::string, std::string> &req_params, co
|
||||
}
|
||||
}
|
||||
|
||||
if(collections.empty()) {
|
||||
else if(collections.empty()) {
|
||||
collections.emplace_back("");
|
||||
}
|
||||
}
|
||||
|
@ -232,54 +232,6 @@ TEST_F(AuthManagerTest, VerifyAuthentication) {
|
||||
ASSERT_FALSE(auth_manager.authenticate(create_action_coll_key.value, "collections:create", {"collection2"}, sparams));
|
||||
}
|
||||
|
||||
TEST_F(AuthManagerTest, HandleAuthentication) {
|
||||
route_path rpath_multi_search = route_path("POST", {"multi_search"}, post_multi_search, false, false);
|
||||
std::map<std::string, std::string> req_params;
|
||||
|
||||
std::vector<std::string> collections;
|
||||
get_collections_for_auth(req_params, "{]", rpath_multi_search, collections);
|
||||
|
||||
ASSERT_EQ(1, collections.size());
|
||||
ASSERT_STREQ("", collections[0].c_str());
|
||||
|
||||
nlohmann::json sample_search_body;
|
||||
sample_search_body["searches"] = nlohmann::json::array();
|
||||
nlohmann::json search_query;
|
||||
search_query["q"] = "aaa";
|
||||
search_query["collection"] = "company1";
|
||||
|
||||
sample_search_body["searches"].push_back(search_query);
|
||||
|
||||
search_query["collection"] = "company2";
|
||||
sample_search_body["searches"].push_back(search_query);
|
||||
|
||||
collections.clear();
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_multi_search, collections);
|
||||
|
||||
ASSERT_EQ(2, collections.size());
|
||||
ASSERT_STREQ("company1", collections[0].c_str());
|
||||
ASSERT_STREQ("company2", collections[1].c_str());
|
||||
|
||||
collections.clear();
|
||||
req_params["collection"] = "foo";
|
||||
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_multi_search, collections);
|
||||
|
||||
ASSERT_EQ(3, collections.size());
|
||||
ASSERT_STREQ("foo", collections[0].c_str());
|
||||
ASSERT_STREQ("company1", collections[1].c_str());
|
||||
ASSERT_STREQ("company2", collections[2].c_str());
|
||||
|
||||
collections.clear();
|
||||
req_params.clear();
|
||||
|
||||
route_path rpath_search = route_path("GET", {"collections", ":collection", "documents", "search"}, get_search, false, false);
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_search, collections);
|
||||
|
||||
ASSERT_EQ(1, collections.size());
|
||||
ASSERT_STREQ("", collections[0].c_str());
|
||||
}
|
||||
|
||||
TEST_F(AuthManagerTest, GenerationOfAPIAction) {
|
||||
route_path rpath_search = route_path("GET", {"collections", ":collection", "documents", "search"}, nullptr, false, false);
|
||||
route_path rpath_multi_search = route_path("POST", {"multi_search"}, nullptr, false, false);
|
||||
|
@ -196,3 +196,149 @@ TEST_F(CoreAPIUtilsTest, MultiSearchEmbeddedKeys) {
|
||||
// ensure that req params are appended to (embedded params are also rolled into req params)
|
||||
ASSERT_EQ("user_id: 100&&age: > 100", req->params["filter_by"]);
|
||||
}
|
||||
|
||||
TEST_F(CoreAPIUtilsTest, ExtractCollectionsFromRequestBody) {
|
||||
std::map<std::string, std::string> req_params;
|
||||
std::string body = R"(
|
||||
{
|
||||
"name": "coll1",
|
||||
"fields": [
|
||||
{"name": "title", "type": "string" },
|
||||
{"name": "points", "type": "int32" }
|
||||
],
|
||||
"default_sorting_field": "points"
|
||||
}
|
||||
)";
|
||||
|
||||
route_path rpath("POST", {"collections"}, post_create_collection, false, false);
|
||||
std::vector<std::string> collections;
|
||||
|
||||
get_collections_for_auth(req_params, body, rpath, collections);
|
||||
ASSERT_EQ(1, collections.size());
|
||||
ASSERT_STREQ("coll1", collections[0].c_str());
|
||||
|
||||
// badly constructed collection schema body
|
||||
collections.clear();
|
||||
body = R"(
|
||||
{
|
||||
"name": "coll1
|
||||
"fields": [
|
||||
{"name": "title", "type": "string" },
|
||||
{"name": "points", "type": "int32" }
|
||||
],
|
||||
"default_sorting_field": "points"
|
||||
}
|
||||
)";
|
||||
|
||||
get_collections_for_auth(req_params, body, rpath, collections);
|
||||
ASSERT_EQ(0, collections.size());
|
||||
|
||||
collections.clear();
|
||||
body = R"(
|
||||
{
|
||||
"fields": [
|
||||
{"name": "title", "type": "string" },
|
||||
{"name": "points", "type": "int32" }
|
||||
],
|
||||
"default_sorting_field": "points"
|
||||
}
|
||||
)";
|
||||
|
||||
get_collections_for_auth(req_params, body, rpath, collections);
|
||||
ASSERT_EQ(0, collections.size());
|
||||
|
||||
// check for multi_search
|
||||
collections.clear();
|
||||
rpath = route_path("GET", {"collections"}, post_multi_search, false, false);
|
||||
body = R"(
|
||||
{"searches":[
|
||||
{
|
||||
"query_by": "concat",
|
||||
"collection": "products",
|
||||
"q": "battery"
|
||||
}
|
||||
]
|
||||
}
|
||||
)";
|
||||
|
||||
get_collections_for_auth(req_params, body, rpath, collections);
|
||||
ASSERT_EQ(1, collections.size());
|
||||
ASSERT_STREQ("products", collections[0].c_str());
|
||||
|
||||
// get collection for multi-search
|
||||
collections.clear();
|
||||
body = R"(
|
||||
{"searches":
|
||||
{
|
||||
"query_by": "concat",
|
||||
"collection": "products",
|
||||
"q": "battery"
|
||||
}
|
||||
]
|
||||
}
|
||||
)";
|
||||
|
||||
get_collections_for_auth(req_params, body, rpath, collections);
|
||||
ASSERT_EQ(0, collections.size());
|
||||
|
||||
collections.clear();
|
||||
body = R"(
|
||||
{"searches":[
|
||||
{
|
||||
"query_by": "concat",
|
||||
"q": "battery"
|
||||
}
|
||||
]
|
||||
}
|
||||
)";
|
||||
|
||||
get_collections_for_auth(req_params, body, rpath, collections);
|
||||
ASSERT_EQ(0, collections.size());
|
||||
}
|
||||
|
||||
TEST_F(CoreAPIUtilsTest, ExtractCollectionsFromRequestBodyExtended) {
|
||||
route_path rpath_multi_search = route_path("POST", {"multi_search"}, post_multi_search, false, false);
|
||||
std::map<std::string, std::string> req_params;
|
||||
|
||||
std::vector<std::string> collections;
|
||||
get_collections_for_auth(req_params, "{]", rpath_multi_search, collections);
|
||||
|
||||
ASSERT_EQ(0, collections.size());
|
||||
|
||||
nlohmann::json sample_search_body;
|
||||
sample_search_body["searches"] = nlohmann::json::array();
|
||||
nlohmann::json search_query;
|
||||
search_query["q"] = "aaa";
|
||||
search_query["collection"] = "company1";
|
||||
|
||||
sample_search_body["searches"].push_back(search_query);
|
||||
|
||||
search_query["collection"] = "company2";
|
||||
sample_search_body["searches"].push_back(search_query);
|
||||
|
||||
collections.clear();
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_multi_search, collections);
|
||||
|
||||
ASSERT_EQ(2, collections.size());
|
||||
ASSERT_STREQ("company1", collections[0].c_str());
|
||||
ASSERT_STREQ("company2", collections[1].c_str());
|
||||
|
||||
collections.clear();
|
||||
req_params["collection"] = "foo";
|
||||
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_multi_search, collections);
|
||||
|
||||
ASSERT_EQ(3, collections.size());
|
||||
ASSERT_STREQ("foo", collections[0].c_str());
|
||||
ASSERT_STREQ("company1", collections[1].c_str());
|
||||
ASSERT_STREQ("company2", collections[2].c_str());
|
||||
|
||||
collections.clear();
|
||||
req_params.clear();
|
||||
|
||||
route_path rpath_search = route_path("GET", {"collections", ":collection", "documents", "search"}, get_search, false, false);
|
||||
get_collections_for_auth(req_params, sample_search_body.dump(), rpath_search, collections);
|
||||
|
||||
ASSERT_EQ(1, collections.size());
|
||||
ASSERT_STREQ("", collections[0].c_str());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user