2023-02-04 08:12:24 +08:00
|
|
|
# The Mia! Accounting Flask Project.
|
|
|
|
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/2/3
|
|
|
|
|
|
|
|
# Copyright (c) 2023 imacat.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
"""The test for the independent utilities.
|
|
|
|
|
|
|
|
"""
|
|
|
|
import unittest
|
|
|
|
from urllib.parse import quote_plus
|
|
|
|
|
|
|
|
import httpx
|
2023-03-14 21:28:35 +08:00
|
|
|
from flask import Flask, request
|
2023-02-04 08:12:24 +08:00
|
|
|
|
2023-02-20 08:19:10 +08:00
|
|
|
from accounting.utils.next_uri import append_next, inherit_next, or_next
|
2023-02-04 12:19:30 +08:00
|
|
|
from accounting.utils.pagination import Pagination, DEFAULT_PAGE_SIZE
|
2023-02-04 08:12:24 +08:00
|
|
|
from accounting.utils.query import parse_query_keywords
|
2023-03-14 21:28:35 +08:00
|
|
|
from testlib import TEST_SERVER, create_test_app, get_csrf_token
|
2023-02-04 08:12:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
class NextUriTestCase(unittest.TestCase):
|
|
|
|
"""The test case for the next URI utilities."""
|
2023-02-27 15:57:39 +08:00
|
|
|
TARGET: str = "/target"
|
2023-02-04 08:12:24 +08:00
|
|
|
|
2023-02-27 15:57:39 +08:00
|
|
|
def setUp(self) -> None:
|
|
|
|
"""Sets up the test.
|
|
|
|
This is run once per test.
|
2023-02-04 08:12:24 +08:00
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-03-14 21:28:35 +08:00
|
|
|
self.app: Flask = create_test_app()
|
2023-02-27 15:35:35 +08:00
|
|
|
|
2023-02-27 15:57:39 +08:00
|
|
|
def test_next_uri(self) -> None:
|
|
|
|
"""Tests the next URI utilities with the next URI.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
def test_next_uri_view() -> str:
|
2023-02-04 08:12:24 +08:00
|
|
|
"""The test view with the next URI."""
|
|
|
|
current_uri: str = request.full_path if request.query_string \
|
|
|
|
else request.path
|
2023-02-27 15:57:39 +08:00
|
|
|
self.assertEqual(append_next(self.TARGET),
|
|
|
|
f"{self.TARGET}?next={quote_plus(current_uri)}")
|
2023-02-04 08:12:24 +08:00
|
|
|
next_uri: str = request.form["next"] if request.method == "POST" \
|
|
|
|
else request.args["next"]
|
2023-02-27 15:57:39 +08:00
|
|
|
self.assertEqual(inherit_next(self.TARGET),
|
|
|
|
f"{self.TARGET}?next={quote_plus(next_uri)}")
|
|
|
|
self.assertEqual(or_next(self.TARGET), next_uri)
|
2023-02-04 08:12:24 +08:00
|
|
|
return ""
|
|
|
|
|
2023-02-27 15:57:39 +08:00
|
|
|
self.app.add_url_rule("/test-next", view_func=test_next_uri_view,
|
|
|
|
methods=["GET", "POST"])
|
2023-02-27 16:25:36 +08:00
|
|
|
client: httpx.Client = httpx.Client(app=self.app, base_url=TEST_SERVER)
|
|
|
|
client.headers["Referer"] = TEST_SERVER
|
2023-03-14 21:28:35 +08:00
|
|
|
csrf_token: str = get_csrf_token(client)
|
2023-02-04 08:12:24 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
|
|
|
response = client.get("/test-next?next=/next&q=abc&page-no=4")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2023-02-27 15:35:35 +08:00
|
|
|
response = client.post("/test-next", data={"csrf_token": csrf_token,
|
|
|
|
"next": "/next",
|
2023-02-04 08:12:24 +08:00
|
|
|
"name": "viewer"})
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2023-02-27 15:57:39 +08:00
|
|
|
def test_no_next_uri(self) -> None:
|
|
|
|
"""Tests the next URI utilities without the next URI.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
def test_no_next_uri_view() -> str:
|
|
|
|
"""The test view without the next URI."""
|
|
|
|
current_uri: str = request.full_path if request.query_string \
|
|
|
|
else request.path
|
|
|
|
self.assertEqual(append_next(self.TARGET),
|
|
|
|
f"{self.TARGET}?next={quote_plus(current_uri)}")
|
|
|
|
self.assertEqual(inherit_next(self.TARGET), self.TARGET)
|
|
|
|
self.assertEqual(or_next(self.TARGET), self.TARGET)
|
|
|
|
return ""
|
|
|
|
|
|
|
|
self.app.add_url_rule("/test-no-next", view_func=test_no_next_uri_view,
|
|
|
|
methods=["GET", "POST"])
|
2023-02-27 16:25:36 +08:00
|
|
|
client: httpx.Client = httpx.Client(app=self.app, base_url=TEST_SERVER)
|
|
|
|
client.headers["Referer"] = TEST_SERVER
|
2023-03-14 21:28:35 +08:00
|
|
|
csrf_token: str = get_csrf_token(client)
|
2023-02-27 15:57:39 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
2023-02-04 08:12:24 +08:00
|
|
|
response = client.get("/test-no-next?q=abc&page-no=4")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2023-02-27 15:35:35 +08:00
|
|
|
response = client.post("/test-no-next", data={"csrf_token": csrf_token,
|
|
|
|
"name": "viewer"})
|
2023-02-04 08:12:24 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
|
|
|
|
class QueryKeywordParserTestCase(unittest.TestCase):
|
|
|
|
"""The test case for the query keyword parser."""
|
|
|
|
|
|
|
|
def test_default(self) -> None:
|
|
|
|
"""Tests the query keyword parser.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
self.assertEqual(parse_query_keywords("coffee"), ["coffee"])
|
|
|
|
self.assertEqual(parse_query_keywords("coffee tea"), ["coffee", "tea"])
|
|
|
|
self.assertEqual(parse_query_keywords("\"coffee\" \"tea cake\""),
|
|
|
|
["coffee", "tea cake"])
|
2023-02-04 14:53:18 +08:00
|
|
|
self.assertEqual(parse_query_keywords("\"coffee tea\" cheese "
|
|
|
|
"\"cake candy\" sugar"),
|
|
|
|
["coffee tea", "cheese", "cake candy", "sugar"])
|
2023-02-04 08:12:24 +08:00
|
|
|
|
|
|
|
def test_malformed(self) -> None:
|
|
|
|
"""Tests the malformed query.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-02-04 14:51:09 +08:00
|
|
|
self.assertEqual(parse_query_keywords("coffee \"tea cake"),
|
|
|
|
["coffee", "tea cake"])
|
2023-02-04 08:12:24 +08:00
|
|
|
self.assertEqual(parse_query_keywords("coffee te\"a ca\"ke"),
|
|
|
|
["coffee", "te\"a", "ca\"ke"])
|
2023-02-04 14:54:32 +08:00
|
|
|
self.assertEqual(parse_query_keywords("coffee\" tea cake\""),
|
|
|
|
["coffee\"", "tea", "cake\""])
|
2023-02-04 08:12:24 +08:00
|
|
|
|
|
|
|
def test_empty(self) -> None:
|
|
|
|
"""Tests the empty query.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
self.assertEqual(parse_query_keywords(None), [])
|
|
|
|
self.assertEqual(parse_query_keywords(""), [])
|
|
|
|
|
|
|
|
|
|
|
|
class PaginationTestCase(unittest.TestCase):
|
|
|
|
"""The test case for pagination."""
|
|
|
|
|
|
|
|
class Params:
|
|
|
|
"""The testing parameters."""
|
|
|
|
|
|
|
|
def __init__(self, items: list[int], is_reversed: bool | None,
|
2023-02-04 10:26:28 +08:00
|
|
|
result: list[int], is_paged: bool):
|
2023-02-04 08:12:24 +08:00
|
|
|
"""Constructs the expected pagination.
|
|
|
|
|
|
|
|
:param items: All the items in the list.
|
|
|
|
:param is_reversed: Whether the default page is the last page.
|
|
|
|
:param result: The expected items on the page.
|
2023-03-18 22:52:29 +08:00
|
|
|
:param is_paged: Whether we need pagination.
|
2023-02-04 08:12:24 +08:00
|
|
|
"""
|
|
|
|
self.items: list[int] = items
|
|
|
|
self.is_reversed: bool | None = is_reversed
|
|
|
|
self.result: list[int] = result
|
2023-02-04 10:26:28 +08:00
|
|
|
self.is_paged: bool = is_paged
|
2023-02-04 08:12:24 +08:00
|
|
|
|
|
|
|
def setUp(self) -> None:
|
|
|
|
"""Sets up the test.
|
|
|
|
This is run once per test.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-03-14 21:28:35 +08:00
|
|
|
self.app: Flask = create_test_app()
|
2023-02-04 08:12:24 +08:00
|
|
|
self.params = self.Params([], None, [], True)
|
|
|
|
|
|
|
|
@self.app.get("/test-pagination")
|
|
|
|
def test_pagination_view() -> str:
|
|
|
|
"""The test view with the pagination."""
|
|
|
|
pagination: Pagination
|
|
|
|
if self.params.is_reversed is not None:
|
2023-02-04 11:09:20 +08:00
|
|
|
pagination = Pagination[int](
|
|
|
|
self.params.items, is_reversed=self.params.is_reversed)
|
2023-02-04 08:12:24 +08:00
|
|
|
else:
|
2023-02-04 11:09:20 +08:00
|
|
|
pagination = Pagination[int](self.params.items)
|
2023-02-04 10:26:28 +08:00
|
|
|
self.assertEqual(pagination.is_paged, self.params.is_paged)
|
2023-02-04 08:12:24 +08:00
|
|
|
self.assertEqual(pagination.list, self.params.result)
|
|
|
|
return ""
|
|
|
|
|
2023-02-27 16:25:36 +08:00
|
|
|
self.client = httpx.Client(app=self.app, base_url=TEST_SERVER)
|
|
|
|
self.client.headers["Referer"] = TEST_SERVER
|
2023-02-04 08:12:24 +08:00
|
|
|
|
2023-02-04 09:34:52 +08:00
|
|
|
def __test_success(self, query: str, items: range,
|
2023-02-04 10:26:28 +08:00
|
|
|
result: range, is_paged: bool = True,
|
2023-02-04 09:34:52 +08:00
|
|
|
is_reversed: bool | None = None) -> None:
|
2023-02-04 08:12:24 +08:00
|
|
|
"""Tests the pagination.
|
|
|
|
|
|
|
|
:param query: The query string.
|
|
|
|
:param items: The original items.
|
|
|
|
:param result: The expected page content.
|
2023-03-18 22:52:29 +08:00
|
|
|
:param is_paged: Whether we need pagination.
|
2023-02-04 08:12:24 +08:00
|
|
|
:param is_reversed: Whether the list is reversed.
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
target: str = "/test-pagination"
|
|
|
|
if query != "":
|
|
|
|
target = f"{target}?{query}"
|
|
|
|
self.params = self.Params(list(items), is_reversed,
|
2023-02-04 10:26:28 +08:00
|
|
|
list(result), is_paged)
|
2023-02-04 08:12:24 +08:00
|
|
|
response: httpx.Response = self.client.get(target)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2023-02-04 09:34:52 +08:00
|
|
|
def __test_malformed(self, query: str, items: range, redirect_to: str,
|
|
|
|
is_reversed: bool | None = None) -> None:
|
|
|
|
"""Tests the pagination.
|
|
|
|
|
|
|
|
:param query: The query string.
|
|
|
|
:param items: The original items.
|
|
|
|
:param redirect_to: The expected target query of the redirection.
|
|
|
|
:param is_reversed: Whether the list is reversed.
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
target: str = "/test-pagination"
|
|
|
|
self.params = self.Params(list(items), is_reversed, [], True)
|
|
|
|
response: httpx.Response = self.client.get(f"{target}?{query}")
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"],
|
|
|
|
f"{target}?{redirect_to}")
|
|
|
|
|
2023-02-04 08:12:24 +08:00
|
|
|
def test_default(self) -> None:
|
|
|
|
"""Tests the default pagination.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
# The default first page
|
2023-02-04 09:34:52 +08:00
|
|
|
self.__test_success("", range(1, 687), range(1, 11))
|
2023-02-04 08:12:24 +08:00
|
|
|
# Some page in the middle
|
2023-02-04 09:34:52 +08:00
|
|
|
self.__test_success("page-no=37", range(1, 687), range(361, 371))
|
2023-02-04 08:12:24 +08:00
|
|
|
# The last page
|
2023-02-04 09:34:52 +08:00
|
|
|
self.__test_success("page-no=69", range(1, 687), range(681, 687))
|
2023-02-04 08:12:24 +08:00
|
|
|
|
|
|
|
def test_page_size(self) -> None:
|
|
|
|
"""Tests the pagination with a different page size.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
# The default page with a different page size
|
2023-02-04 09:34:52 +08:00
|
|
|
self.__test_success("page-size=15", range(1, 687), range(1, 16))
|
2023-02-04 08:12:24 +08:00
|
|
|
# Some page with a different page size
|
2023-02-04 09:34:52 +08:00
|
|
|
self.__test_success("page-no=37&page-size=15", range(1, 687),
|
|
|
|
range(541, 556))
|
2023-02-04 08:12:24 +08:00
|
|
|
# The last page with a different page size.
|
2023-02-04 09:34:52 +08:00
|
|
|
self.__test_success("page-no=46&page-size=15", range(1, 687),
|
|
|
|
range(676, 687))
|
2023-02-04 08:12:24 +08:00
|
|
|
|
2023-03-18 22:43:53 +08:00
|
|
|
def test_not_need(self) -> None:
|
|
|
|
"""Tests that the data does not need pagination.
|
2023-02-04 08:12:24 +08:00
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
# Empty list
|
2023-02-04 10:26:28 +08:00
|
|
|
self.__test_success("", range(0, 0), range(0, 0), is_paged=False)
|
2023-02-04 08:12:24 +08:00
|
|
|
# A list that fits in one page
|
2023-02-04 10:26:28 +08:00
|
|
|
self.__test_success("", range(1, 4), range(1, 4), is_paged=False)
|
2023-02-04 08:12:24 +08:00
|
|
|
# A large page size that fits in everything
|
2023-02-04 09:34:52 +08:00
|
|
|
self.__test_success("page-size=1000", range(1, 687), range(1, 687),
|
2023-02-04 10:26:28 +08:00
|
|
|
is_paged=False)
|
2023-02-04 08:12:24 +08:00
|
|
|
|
|
|
|
def test_reversed(self) -> None:
|
|
|
|
"""Tests the default page on a reversed list.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
# The default page
|
2023-02-04 09:34:52 +08:00
|
|
|
self.__test_success("", range(1, 687), range(681, 687),
|
|
|
|
is_reversed=True)
|
2023-02-04 08:12:24 +08:00
|
|
|
# The default page with a different page size
|
2023-02-04 09:34:52 +08:00
|
|
|
self.__test_success("page-size=15", range(1, 687), range(676, 687),
|
|
|
|
is_reversed=True)
|
2023-02-04 08:12:24 +08:00
|
|
|
|
|
|
|
def test_last_page(self) -> None:
|
|
|
|
"""Tests the calculation of the items on the last page.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
# The last page that fits in one page
|
2023-02-04 09:34:52 +08:00
|
|
|
self.__test_success("page-no=69", range(1, 691), range(681, 691))
|
2023-02-04 08:12:24 +08:00
|
|
|
# A danging item in the last page
|
2023-02-04 09:34:52 +08:00
|
|
|
self.__test_success("page-no=70", range(1, 692), range(691, 692))
|
|
|
|
|
|
|
|
def test_malformed(self) -> None:
|
|
|
|
"""Tests the malformed pagination parameters.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-02-27 17:15:29 +08:00
|
|
|
page_37: str = "q=word&page-no=37&next=%2F"
|
|
|
|
page_size_15_default: str = "q=word&page-size=15&next=%2F"
|
|
|
|
|
2023-02-04 09:34:52 +08:00
|
|
|
# A malformed page size
|
|
|
|
self.__test_malformed("q=word&page-size=100a&page-no=37&next=%2F",
|
2023-02-27 17:15:29 +08:00
|
|
|
range(1, 691), page_37)
|
2023-02-04 10:37:39 +08:00
|
|
|
# A default page size
|
2023-02-04 12:19:30 +08:00
|
|
|
self.__test_malformed(f"q=word&page-size={DEFAULT_PAGE_SIZE}"
|
2023-02-04 10:37:39 +08:00
|
|
|
"&page-no=37&next=%2F",
|
2023-02-27 17:15:29 +08:00
|
|
|
range(1, 691), page_37)
|
2023-02-04 10:55:49 +08:00
|
|
|
# An invalid page size
|
|
|
|
self.__test_malformed("q=word&page-size=0&page-no=37&next=%2F",
|
2023-02-27 17:15:29 +08:00
|
|
|
range(1, 691), page_37)
|
2023-02-04 09:34:52 +08:00
|
|
|
# A malformed page number
|
|
|
|
self.__test_malformed("q=word&page-size=15&page-no=37a&next=%2F",
|
2023-02-27 17:15:29 +08:00
|
|
|
range(1, 691), page_size_15_default)
|
2023-02-04 10:37:39 +08:00
|
|
|
# A default page number
|
|
|
|
self.__test_malformed("q=word&page-size=15&page-no=1&next=%2F",
|
2023-02-27 17:15:29 +08:00
|
|
|
range(1, 691), page_size_15_default)
|
2023-02-04 10:37:39 +08:00
|
|
|
# A default page number, on a reversed list
|
|
|
|
self.__test_malformed("q=word&page-size=15&page-no=46&next=%2F",
|
2023-02-27 17:15:29 +08:00
|
|
|
range(1, 691), page_size_15_default,
|
2023-02-04 10:37:39 +08:00
|
|
|
is_reversed=True)
|
2023-02-04 09:34:52 +08:00
|
|
|
# A page number beyond the last page
|
|
|
|
self.__test_malformed("q=word&page-size=15&page-no=100&next=%2F",
|
|
|
|
range(1, 691),
|
|
|
|
"q=word&page-size=15&page-no=46&next=%2F")
|
|
|
|
# A page number beyond the last page, on a reversed list
|
|
|
|
self.__test_malformed("q=word&page-size=15&page-no=100&next=%2F",
|
|
|
|
range(1, 691),
|
2023-02-27 17:15:29 +08:00
|
|
|
page_size_15_default, is_reversed=True)
|
2023-02-04 09:34:52 +08:00
|
|
|
# A page number before the first page
|
|
|
|
self.__test_malformed("q=word&page-size=15&page-no=0&next=%2F",
|
|
|
|
range(1, 691),
|
2023-02-27 17:15:29 +08:00
|
|
|
page_size_15_default)
|
2023-02-04 09:34:52 +08:00
|
|
|
# A page number before the first page, on a reversed list
|
|
|
|
self.__test_malformed("q=word&page-size=15&page-no=0&next=%2F",
|
|
|
|
range(1, 691),
|
|
|
|
"q=word&page-size=15&page-no=1&next=%2F",
|
|
|
|
is_reversed=True)
|