mirror of https://github.com/kiwix/libkiwix.git
Negative tests for books selection during search
This commit is contained in:
parent
6ef4f6396e
commit
e3fffd9b23
|
@ -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"(<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
|
@ -1581,7 +1584,7 @@ std::string expectedConfusionOfTonguesErrorHtml(std::string url)
|
|||
<title>Invalid request</title>
|
||||
<script>
|
||||
window.KIWIX_RESPONSE_TEMPLATE = )" + ERROR_HTML_TEMPLATE_JS_STRING + R"(;
|
||||
window.KIWIX_RESPONSE_DATA = { "CSS_URL" : false, "PAGE_HEADING" : { "msgid" : "400-page-heading", "params" : { } }, "PAGE_TITLE" : { "msgid" : "400-page-title", "params" : { } }, "details" : [ { "p" : { "msgid" : "invalid-request", "params" : { "url" : ")" + url + R"(" } } }, { "p" : { "msgid" : "confusion-of-tongues", "params" : { } } } ] };
|
||||
window.KIWIX_RESPONSE_DATA = { "CSS_URL" : false, "PAGE_HEADING" : { "msgid" : "400-page-heading", "params" : { } }, "PAGE_TITLE" : { "msgid" : "400-page-title", "params" : { } }, "details" : [ { "p" : { "msgid" : "invalid-request", "params" : { "url" : ")" + url + R"(" } } }, { "p" : { "msgid" : ")" + errorMsgId + R"(", "params" : )" + errorMsgParamsJSON + R"( } } ] };
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
@ -1590,19 +1593,30 @@ std::string expectedConfusionOfTonguesErrorHtml(std::string url)
|
|||
The requested URL ")" + url + R"(" is not a valid request.
|
||||
</p>
|
||||
<p>
|
||||
Two or more books in different languages would participate in search, which may lead to confusing results.
|
||||
)" + errorText + R"(
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
)";
|
||||
}
|
||||
|
||||
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"(<?xml version="1.0" encoding="UTF-8">
|
||||
<error>Invalid request</error>
|
||||
<detail>The requested URL ")" + url + R"(" is not a valid request.</detail>
|
||||
<detail>Two or more books in different languages would participate in search, which may lead to confusing results.</detail>
|
||||
<detail>)" + CONFUSION_OF_TONGUES_ERROR_TEXT + R"(</detail>
|
||||
)";
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue