From e3fffd9b234eb83ad2719d20b6876e312785da8d Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 5 Mar 2024 17:18:31 +0400 Subject: [PATCH] Negative tests for books selection during search --- test/server_search.cpp | 69 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/test/server_search.cpp b/test/server_search.cpp index 06f35aba7..4759a0c36 100644 --- a/test/server_search.cpp +++ b/test/server_search.cpp @@ -1572,7 +1572,10 @@ TEST(ServerSearchTest, searchResults) } } -std::string expectedConfusionOfTonguesErrorHtml(std::string url) +std::string invalidRequestErrorHtml(std::string url, + std::string errorMsgId, + std::string errorMsgParamsJSON, + std::string errorText) { return R"( @@ -1581,7 +1584,7 @@ std::string expectedConfusionOfTonguesErrorHtml(std::string url) Invalid request @@ -1590,19 +1593,30 @@ std::string expectedConfusionOfTonguesErrorHtml(std::string url) The requested URL ")" + url + R"(" is not a valid request.

- Two or more books in different languages would participate in search, which may lead to confusing results. + )" + errorText + R"(

)"; } +const char CONFUSION_OF_TONGUES_ERROR_TEXT[] = "Two or more books in different languages would participate in search, which may lead to confusing results."; + +std::string expectedConfusionOfTonguesErrorHtml(std::string url) +{ + return invalidRequestErrorHtml(url, + /* errorMsgId */ "confusion-of-tongues", + /* errorMsgParamsJSON */ "{ }", + /* errorText */ CONFUSION_OF_TONGUES_ERROR_TEXT + ); +} + std::string expectedConfusionOfTonguesErrorXml(std::string url) { return R"( Invalid request The requested URL ")" + url + R"(" is not a valid request. -Two or more books in different languages would participate in search, which may lead to confusing results. +)" + CONFUSION_OF_TONGUES_ERROR_TEXT + R"( )"; } @@ -1642,3 +1656,50 @@ TEST(ServerSearchTest, searchInMultilanguageBookSetIsDenied) } } } + +std::string noSuchBookErrorHtml(std::string url, std::string bookName) +{ + return invalidRequestErrorHtml(url, + /* errorMsgId */ "no-such-book", + /* errorMsgParamsJSON */ "{ \"BOOK_NAME\" : \"" + bookName + "\" }", + /* errorText */ "No such book: " + bookName + ); +} + +std::string noBookFoundErrorHtml(std::string url) +{ + return invalidRequestErrorHtml(url, + /* errorMsgId */ "no-book-found", + /* errorMsgParamsJSON */ "{ }", + /* errorText */ "No book matches selection criteria" + ); +} + +TEST(ServerSearchTest, bookSelectionNegativeTests) +{ + ZimFileServer zfs(SERVER_PORT, ZimFileServer::DEFAULT_OPTIONS, + "./test/lib_for_server_search_test.xml"); + + { + // books.name (unlike books.filter.name) DOESN'T consider the book name + // and reports an error (surprise!) + const std::string bookName = "wikipedia_en_ray_charles"; + const std::string q = "pattern=travel&books.name=" + bookName; + const std::string url = "/ROOT%23%3F/search?" + q; + + const auto r = zfs.GET(url.c_str()); + EXPECT_EQ(r->status, 400); + EXPECT_EQ(r->body, noSuchBookErrorHtml(url, bookName)); + } + + { + // books.filter.name (unlike books.name) DOESN'T consider the ZIM file name + // and reports an error (differently from books.name) + const std::string q = "pattern=travel&books.filter.name=zimfile"; + const std::string url = "/ROOT%23%3F/search?" + q; + + const auto r = zfs.GET(url.c_str()); + EXPECT_EQ(r->status, 400); + EXPECT_EQ(r->body, noBookFoundErrorHtml(url)); + } +}