Skip to content

Commit

Permalink
Negative tests for books selection during search
Browse files Browse the repository at this point in the history
  • Loading branch information
veloman-yunkan authored and mgautierfr committed Mar 6, 2024
1 parent 6ef4f63 commit e3fffd9
Showing 1 changed file with 65 additions and 4 deletions.
69 changes: 65 additions & 4 deletions test/server_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -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>
Expand All @@ -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>
)";
}

Expand Down Expand Up @@ -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));
}
}

2 comments on commit e3fffd9

@Perplexitus
Copy link

@Perplexitus Perplexitus commented on e3fffd9 Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

I am getting this "Confusion of Tongues" error when doing a generic search using books.name=ted_mul_all_2024-07 (All Ted Videos ZIM).

http://192.168.1.40:3000/kiwix/viewer#search?books.name=ted_mul_all_2024-07&pattern=test Invalid request The requested URL "/kiwix/search?books.name=ted_mul_all_2024-07&pattern=test&userlang=en" is not a valid request. Two or more books in different languages would participate in search, which may lead to confusing results.

It also seems to be the default response when I input a blank URL.
http://192.168.1.40:3000/kiwix/search?books.name=ted_mul_all_2024-07

--Same output as above. Two or more books... blah blah.--

Using a Different ZIM file:
What's weird is that the same exact query works properly for a different book, such as Wikipedia:
http://192.168.1.40:3000/kiwix/viewer#search?books.name=wikipedia_en_all_maxi_2024-01&pattern=test

...provides search results as expected...

Using a blank query for the Wikipedia ZIM gives me a "No query provided" response:
http://192.168.1.40:3000/kiwix/search?books.name=wikipedia_en_all_maxi_2024-01

The requested URL "/kiwix/search?books.name=wikipedia_en_all_maxi_2024-01" is not a valid request. No query provided.

Due to this issue in the TED videos ZIM, I am unable to search for anything when using this ZIM.

@Perplexitus
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened a new issue. #1159

Please sign in to comment.