From e0c6d98b010206cbd69de3d8d2907087fc772165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Thu, 8 Jun 2023 09:27:39 +0800 Subject: [PATCH] Added the "decode_next" utility in the "swr_assess.utils.next_uri" module, and applied the "encode_next" and "decode_next" utilities to the NextUriTestCase test case, so that the test case do not need to get involved into the detail of the next URI encryption. --- src/accounting/utils/next_uri.py | 13 +++++++++-- tests/test_utils.py | 39 +++++++++++--------------------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/accounting/utils/next_uri.py b/src/accounting/utils/next_uri.py index c58a216..07da176 100644 --- a/src/accounting/utils/next_uri.py +++ b/src/accounting/utils/next_uri.py @@ -75,8 +75,7 @@ def __get_next() -> str | None: if next_uri is None: return None try: - return URLSafeSerializer(current_app.config["SECRET_KEY"])\ - .loads(next_uri, "next") + return decode_next(next_uri) except BadData: return None @@ -107,6 +106,16 @@ def encode_next(uri: str) -> str: .dumps(uri, "next") +def decode_next(uri: str) -> str: + """Decodes the encoded next URI. + + :param uri: The encoded next URI. + :return: The next URI. + """ + return URLSafeSerializer(current_app.config["SECRET_KEY"])\ + .loads(uri, "next") + + def init_app(bp: Blueprint) -> None: """Initializes the application. diff --git a/tests/test_utils.py b/tests/test_utils.py index 10adb92..7b59b4e 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -22,9 +22,9 @@ from urllib.parse import quote_plus import httpx from flask import Flask, request -from itsdangerous import URLSafeSerializer -from accounting.utils.next_uri import append_next, inherit_next, or_next +from accounting.utils.next_uri import append_next, inherit_next, or_next, \ + encode_next, decode_next from accounting.utils.pagination import Pagination, DEFAULT_PAGE_SIZE from accounting.utils.query import parse_query_keywords from testlib import TEST_SERVER, create_test_app, get_csrf_token, NEXT_URI @@ -41,8 +41,6 @@ class NextUriTestCase(unittest.TestCase): :return: None. """ self.app: Flask = create_test_app() - self.serializer: URLSafeSerializer \ - = URLSafeSerializer(self.app.config["SECRET_KEY"]) def test_next_uri(self) -> None: """Tests the next URI utilities with the next URI. @@ -53,13 +51,17 @@ class NextUriTestCase(unittest.TestCase): """The test view with the next URI.""" current_uri: str = request.full_path if request.query_string \ else request.path + with self.app.app_context(): + encoded_current: str = encode_next(current_uri) self.assertEqual(append_next(self.TARGET), - f"{self.TARGET}?next={self.__encode(current_uri)}") - next_uri: str = request.form["next"] if request.method == "POST" \ - else request.args["next"] + f"{self.TARGET}?next={encoded_current}") + encoded_next_uri: str = request.form["next"] \ + if request.method == "POST" else request.args["next"] self.assertEqual(inherit_next(self.TARGET), - f"{self.TARGET}?next={next_uri}") - self.assertEqual(or_next(self.TARGET), self.__decode(next_uri)) + f"{self.TARGET}?next={encoded_next_uri}") + with self.app.app_context(): + next_uri: str = decode_next(encoded_next_uri) + self.assertEqual(or_next(self.TARGET), next_uri) return "" self.app.add_url_rule("/test-next", view_func=test_next_uri_view, @@ -69,7 +71,8 @@ class NextUriTestCase(unittest.TestCase): csrf_token: str = get_csrf_token(client) response: httpx.Response - encoded_uri: str = self.__encode(NEXT_URI) + with self.app.app_context(): + encoded_uri: str = encode_next(NEXT_URI) response = client.get(f"/test-next?next={encoded_uri}&q=abc&page-no=4") self.assertEqual(response.status_code, 200) response = client.post("/test-next", data={"csrf_token": csrf_token, @@ -132,22 +135,6 @@ class NextUriTestCase(unittest.TestCase): "next": next_uri}) self.assertEqual(response.status_code, 200) - def __encode(self, uri: str) -> str: - """Encodes the next URI. - - :param uri: The next URI. - :return: The encoded next URI. - """ - return self.serializer.dumps(uri, "next") - - def __decode(self, uri: str) -> str: - """Decodes the next URI. - - :param uri: The encoded next URI. - :return: The next URI. - """ - return self.serializer.loads(uri, "next") - class QueryKeywordParserTestCase(unittest.TestCase): """The test case for the query keyword parser."""