From 71b1a6e7a3be1af81e16cd7ed60e702f7e0d8f60 Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Tue, 21 Sep 2021 13:44:15 +0530 Subject: [PATCH] Add tests for collection extraction for auth. --- src/core_api.cpp | 2 +- test/auth_manager_test.cpp | 48 ------------ test/core_api_utils_test.cpp | 146 +++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 49 deletions(-) diff --git a/src/core_api.cpp b/src/core_api.cpp index 8148132a..6d89dc51 100644 --- a/src/core_api.cpp +++ b/src/core_api.cpp @@ -80,7 +80,7 @@ void get_collections_for_auth(std::map &req_params, co } } - if(collections.empty()) { + else if(collections.empty()) { collections.emplace_back(""); } } diff --git a/test/auth_manager_test.cpp b/test/auth_manager_test.cpp index bf94b95f..16aa2018 100644 --- a/test/auth_manager_test.cpp +++ b/test/auth_manager_test.cpp @@ -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 req_params; - - std::vector 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); diff --git a/test/core_api_utils_test.cpp b/test/core_api_utils_test.cpp index 989de802..5185a309 100644 --- a/test/core_api_utils_test.cpp +++ b/test/core_api_utils_test.cpp @@ -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 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 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 req_params; + + std::vector 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()); +}