From 5b984dff383ffe31fd264633848f8fc33e399d88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sat, 10 Jun 2023 09:35:51 +0800 Subject: [PATCH] Removed the CSRF token from the get_client function in testlib.py, so that type hints and documentation can be added to the client and the CSRF token properties separately. --- tests/test_account.py | 18 ++++++++---- tests/test_base_account.py | 6 ++-- tests/test_currency.py | 18 ++++++++---- tests/test_description_editor.py | 8 ++++-- tests/test_journal_entry.py | 49 +++++++++++++++++++++++--------- tests/test_offset.py | 7 +++-- tests/test_option.py | 17 +++++++---- tests/test_report.py | 11 ++++--- tests/test_unmatched_offset.py | 14 ++++++--- tests/testlib.py | 6 ++-- 10 files changed, 105 insertions(+), 49 deletions(-) diff --git a/tests/test_account.py b/tests/test_account.py index b486262..ed01103 100644 --- a/tests/test_account.py +++ b/tests/test_account.py @@ -25,8 +25,8 @@ from flask import Flask from accounting.utils.next_uri import encode_next from test_site import db -from testlib import NEXT_URI, create_test_app, get_client, set_locale, \ - add_journal_entry +from testlib import NEXT_URI, create_test_app, get_client, get_csrf_token, \ + set_locale, add_journal_entry class AccountData: @@ -83,7 +83,10 @@ class AccountTestCase(unittest.TestCase): self.encoded_next_uri: str = encode_next(NEXT_URI) """The encoded next URI.""" - self.client, self.csrf_token = get_client(self.app, "editor") + self.client: httpx.Client = get_client(self.app, "editor") + """The user client.""" + self.csrf_token: str = get_csrf_token(self.client) + """The CSRF token.""" response: httpx.Response response = self.client.post(f"{PREFIX}/store", @@ -108,7 +111,8 @@ class AccountTestCase(unittest.TestCase): :return: None. """ from accounting.models import Account - client, csrf_token = get_client(self.app, "nobody") + client: httpx.Client = get_client(self.app, "nobody") + csrf_token: str = get_csrf_token(client) response: httpx.Response response = client.get(PREFIX) @@ -157,7 +161,8 @@ class AccountTestCase(unittest.TestCase): :return: None. """ from accounting.models import Account - client, csrf_token = get_client(self.app, "viewer") + client: httpx.Client = get_client(self.app, "viewer") + csrf_token: str = get_csrf_token(client) response: httpx.Response response = client.get(PREFIX) @@ -489,7 +494,8 @@ class AccountTestCase(unittest.TestCase): """ from accounting.models import Account editor_username, admin_username = "editor", "admin" - client, csrf_token = get_client(self.app, admin_username) + client: httpx.Client = get_client(self.app, admin_username) + csrf_token: str = get_csrf_token(client) detail_uri: str = f"{PREFIX}/{CASH.code}" update_uri: str = f"{PREFIX}/{CASH.code}/update" account: Account diff --git a/tests/test_base_account.py b/tests/test_base_account.py index 9728044..015b66c 100644 --- a/tests/test_base_account.py +++ b/tests/test_base_account.py @@ -47,7 +47,7 @@ class BaseAccountTestCase(unittest.TestCase): :return: None. """ - client, csrf_token = get_client(self.app, "nobody") + client: httpx.Client = get_client(self.app, "nobody") response: httpx.Response response = client.get(LIST_URI) @@ -61,7 +61,7 @@ class BaseAccountTestCase(unittest.TestCase): :return: None. """ - client, csrf_token = get_client(self.app, "viewer") + client: httpx.Client = get_client(self.app, "viewer") response: httpx.Response response = client.get(LIST_URI) @@ -75,7 +75,7 @@ class BaseAccountTestCase(unittest.TestCase): :return: None. """ - client, csrf_token = get_client(self.app, "editor") + client: httpx.Client = get_client(self.app, "editor") response: httpx.Response response = client.get(LIST_URI) diff --git a/tests/test_currency.py b/tests/test_currency.py index e04b40d..d620696 100644 --- a/tests/test_currency.py +++ b/tests/test_currency.py @@ -25,8 +25,8 @@ from flask import Flask from accounting.utils.next_uri import encode_next from test_site import db -from testlib import NEXT_URI, create_test_app, get_client, set_locale, \ - add_journal_entry +from testlib import NEXT_URI, create_test_app, get_client, get_csrf_token, \ + set_locale, add_journal_entry class CurrencyData: @@ -74,7 +74,10 @@ class CurrencyTestCase(unittest.TestCase): Currency.query.delete() db.session.commit() - self.client, self.csrf_token = get_client(self.app, "editor") + self.client: httpx.Client = get_client(self.app, "editor") + """The user client.""" + self.csrf_token: str = get_csrf_token(self.client) + """The CSRF token.""" response: httpx.Response response = self.client.post(f"{PREFIX}/store", @@ -96,7 +99,8 @@ class CurrencyTestCase(unittest.TestCase): :return: None. """ - client, csrf_token = get_client(self.app, "nobody") + client: httpx.Client = get_client(self.app, "nobody") + csrf_token: str = get_csrf_token(client) response: httpx.Response response = client.get(PREFIX) @@ -132,7 +136,8 @@ class CurrencyTestCase(unittest.TestCase): :return: None. """ - client, csrf_token = get_client(self.app, "viewer") + client: httpx.Client = get_client(self.app, "viewer") + csrf_token: str = get_csrf_token(client) response: httpx.Response response = client.get(PREFIX) @@ -410,7 +415,8 @@ class CurrencyTestCase(unittest.TestCase): """ from accounting.models import Currency editor_username, admin_username = "editor", "admin" - client, csrf_token = get_client(self.app, admin_username) + client: httpx.Client = get_client(self.app, admin_username) + csrf_token: str = get_csrf_token(client) detail_uri: str = f"{PREFIX}/{USD.code}" update_uri: str = f"{PREFIX}/{USD.code}/update" currency: Currency diff --git a/tests/test_description_editor.py b/tests/test_description_editor.py index 9a5d227..d8408ff 100644 --- a/tests/test_description_editor.py +++ b/tests/test_description_editor.py @@ -20,11 +20,12 @@ import datetime as dt import unittest +import httpx from flask import Flask from accounting.utils.next_uri import encode_next from testlib import NEXT_URI, Accounts, create_test_app, get_client, \ - add_journal_entry + get_csrf_token, add_journal_entry class DescriptionEditorTestCase(unittest.TestCase): @@ -46,7 +47,10 @@ class DescriptionEditorTestCase(unittest.TestCase): self.encoded_next_uri: str = encode_next(NEXT_URI) """The encoded next URI.""" - self.client, self.csrf_token = get_client(self.app, "editor") + self.client: httpx.Client = get_client(self.app, "editor") + """The user client.""" + self.csrf_token: str = get_csrf_token(self.client) + """The CSRF token.""" def test_description_editor(self) -> None: """Test the description editor. diff --git a/tests/test_journal_entry.py b/tests/test_journal_entry.py index 2fb2338..5eed96e 100644 --- a/tests/test_journal_entry.py +++ b/tests/test_journal_entry.py @@ -27,7 +27,7 @@ from flask import Flask from accounting.utils.next_uri import encode_next from test_site import db from testlib import NEXT_URI, Accounts, create_test_app, get_client, \ - add_journal_entry, match_journal_entry_detail + get_csrf_token, add_journal_entry, match_journal_entry_detail from testlib_journal_entry import NON_EMPTY_NOTE, EMPTY_NOTE, \ get_add_form, get_unchanged_update_form, get_update_form, \ set_negative_amount, remove_debit_in_a_currency, \ @@ -58,14 +58,18 @@ class CashReceiptJournalEntryTestCase(unittest.TestCase): self.encoded_next_uri: str = encode_next(NEXT_URI) """The encoded next URI.""" - self.client, self.csrf_token = get_client(self.app, "editor") + self.client: httpx.Client = get_client(self.app, "editor") + """The user client.""" + self.csrf_token: str = get_csrf_token(self.client) + """The CSRF token.""" def test_nobody(self) -> None: """Test the permission as nobody. :return: None. """ - client, csrf_token = get_client(self.app, "nobody") + client: httpx.Client = get_client(self.app, "nobody") + csrf_token: str = get_csrf_token(client) journal_entry_id: int = add_journal_entry(self.client, self.__get_add_form()) add_form: dict[str, str] = self.__get_add_form() @@ -99,7 +103,8 @@ class CashReceiptJournalEntryTestCase(unittest.TestCase): :return: None. """ - client, csrf_token = get_client(self.app, "viewer") + client: httpx.Client = get_client(self.app, "viewer") + csrf_token: str = get_csrf_token(client) journal_entry_id: int = add_journal_entry(self.client, self.__get_add_form()) add_form: dict[str, str] = self.__get_add_form() @@ -532,7 +537,8 @@ class CashReceiptJournalEntryTestCase(unittest.TestCase): journal_entry_id: int \ = add_journal_entry(self.client, self.__get_add_form()) editor_username, admin_username = "editor", "admin" - client, csrf_token = get_client(self.app, admin_username) + client: httpx.Client = get_client(self.app, admin_username) + csrf_token: str = get_csrf_token(client) detail_uri: str = (f"{PREFIX}/{journal_entry_id}?" f"next={self.encoded_next_uri}") update_uri: str = f"{PREFIX}/{journal_entry_id}/update" @@ -676,14 +682,18 @@ class CashDisbursementJournalEntryTestCase(unittest.TestCase): self.encoded_next_uri: str = encode_next(NEXT_URI) """The encoded next URI.""" - self.client, self.csrf_token = get_client(self.app, "editor") + self.client: httpx.Client = get_client(self.app, "editor") + """The user client.""" + self.csrf_token: str = get_csrf_token(self.client) + """The CSRF token.""" def test_nobody(self) -> None: """Test the permission as nobody. :return: None. """ - client, csrf_token = get_client(self.app, "nobody") + client: httpx.Client = get_client(self.app, "nobody") + csrf_token: str = get_csrf_token(client) journal_entry_id: int \ = add_journal_entry(self.client, self.__get_add_form()) add_form: dict[str, str] = self.__get_add_form() @@ -717,7 +727,8 @@ class CashDisbursementJournalEntryTestCase(unittest.TestCase): :return: None. """ - client, csrf_token = get_client(self.app, "viewer") + client: httpx.Client = get_client(self.app, "viewer") + csrf_token: str = get_csrf_token(client) journal_entry_id: int \ = add_journal_entry(self.client, self.__get_add_form()) add_form: dict[str, str] = self.__get_add_form() @@ -1157,7 +1168,8 @@ class CashDisbursementJournalEntryTestCase(unittest.TestCase): journal_entry_id: int \ = add_journal_entry(self.client, self.__get_add_form()) editor_username, admin_username = "editor", "admin" - client, csrf_token = get_client(self.app, admin_username) + client: httpx.Client = get_client(self.app, admin_username) + csrf_token: str = get_csrf_token(client) detail_uri: str = (f"{PREFIX}/{journal_entry_id}?" f"next={self.encoded_next_uri}") update_uri: str = f"{PREFIX}/{journal_entry_id}/update" @@ -1270,14 +1282,18 @@ class TransferJournalEntryTestCase(unittest.TestCase): self.encoded_next_uri: str = encode_next(NEXT_URI) """The encoded next URI.""" - self.client, self.csrf_token = get_client(self.app, "editor") + self.client: httpx.Client = get_client(self.app, "editor") + """The user client.""" + self.csrf_token: str = get_csrf_token(self.client) + """The CSRF token.""" def test_nobody(self) -> None: """Test the permission as nobody. :return: None. """ - client, csrf_token = get_client(self.app, "nobody") + client: httpx.Client = get_client(self.app, "nobody") + csrf_token: str = get_csrf_token(client) journal_entry_id: int \ = add_journal_entry(self.client, self.__get_add_form()) add_form: dict[str, str] = self.__get_add_form() @@ -1311,7 +1327,8 @@ class TransferJournalEntryTestCase(unittest.TestCase): :return: None. """ - client, csrf_token = get_client(self.app, "viewer") + client: httpx.Client = get_client(self.app, "viewer") + csrf_token: str = get_csrf_token(client) journal_entry_id: int \ = add_journal_entry(self.client, self.__get_add_form()) add_form: dict[str, str] = self.__get_add_form() @@ -1830,7 +1847,8 @@ class TransferJournalEntryTestCase(unittest.TestCase): journal_entry_id: int \ = add_journal_entry(self.client, self.__get_add_form()) editor_username, admin_username = "editor", "admin" - client, csrf_token = get_client(self.app, admin_username) + client: httpx.Client = get_client(self.app, admin_username) + csrf_token: str = get_csrf_token(client) detail_uri: str = (f"{PREFIX}/{journal_entry_id}?" f"next={self.encoded_next_uri}") update_uri: str = f"{PREFIX}/{journal_entry_id}/update" @@ -2143,7 +2161,10 @@ class JournalEntryReorderTestCase(unittest.TestCase): self.encoded_next_uri: str = encode_next(NEXT_URI) """The encoded next URI.""" - self.client, self.csrf_token = get_client(self.app, "editor") + self.client: httpx.Client = get_client(self.app, "editor") + """The user client.""" + self.csrf_token: str = get_csrf_token(self.client) + """The CSRF token.""" def test_change_date(self) -> None: """Tests to change the date of a journal entry. diff --git a/tests/test_offset.py b/tests/test_offset.py index 41b3bb7..192258e 100644 --- a/tests/test_offset.py +++ b/tests/test_offset.py @@ -30,7 +30,7 @@ from test_site import db from test_site.lib import JournalEntryLineItemData, JournalEntryCurrencyData, \ JournalEntryData, BaseTestData from testlib import NEXT_URI, Accounts, create_test_app, get_client, \ - match_journal_entry_detail + get_csrf_token, match_journal_entry_detail PREFIX: str = "/accounting/journal-entries" """The URL prefix for the journal entry management.""" @@ -55,7 +55,10 @@ class OffsetTestCase(unittest.TestCase): self.encoded_next_uri: str = encode_next(NEXT_URI) """The encoded next URI.""" - self.client, self.csrf_token = get_client(self.app, "editor") + self.client: httpx.Client = get_client(self.app, "editor") + """The user client.""" + self.csrf_token: str = get_csrf_token(self.client) + """The CSRF token.""" self.data: OffsetTestData = OffsetTestData(self.app, "editor") """The offset test data.""" self.data.populate() diff --git a/tests/test_option.py b/tests/test_option.py index bad1798..8847473 100644 --- a/tests/test_option.py +++ b/tests/test_option.py @@ -25,7 +25,8 @@ from flask import Flask from accounting.utils.next_uri import encode_next from test_site import db -from testlib import NEXT_URI, Accounts, create_test_app, get_client +from testlib import NEXT_URI, Accounts, create_test_app, get_client, \ + get_csrf_token PREFIX: str = "/accounting/options" """The URL prefix for the option management.""" @@ -49,14 +50,18 @@ class OptionTestCase(unittest.TestCase): self.encoded_next_uri: str = encode_next(NEXT_URI) """The encoded next URI.""" - self.client, self.csrf_token = get_client(self.app, "admin") + self.client: httpx.Client = get_client(self.app, "admin") + """The user client.""" + self.csrf_token: str = get_csrf_token(self.client) + """The CSRF token.""" def test_nobody(self) -> None: """Test the permission as nobody. :return: None. """ - client, csrf_token = get_client(self.app, "nobody") + client: httpx.Client = get_client(self.app, "nobody") + csrf_token: str = get_csrf_token(client) detail_uri: str = f"{PREFIX}?next={self.encoded_next_uri}" edit_uri: str = f"{PREFIX}/edit?next={self.encoded_next_uri}" update_uri: str = f"{PREFIX}/update" @@ -76,7 +81,8 @@ class OptionTestCase(unittest.TestCase): :return: None. """ - client, csrf_token = get_client(self.app, "viewer") + client: httpx.Client = get_client(self.app, "viewer") + csrf_token: str = get_csrf_token(client) detail_uri: str = f"{PREFIX}?next={self.encoded_next_uri}" edit_uri: str = f"{PREFIX}/edit?next={self.encoded_next_uri}" update_uri: str = f"{PREFIX}/update" @@ -96,7 +102,8 @@ class OptionTestCase(unittest.TestCase): :return: None. """ - client, csrf_token = get_client(self.app, "editor") + client: httpx.Client = get_client(self.app, "editor") + csrf_token: str = get_csrf_token(client) detail_uri: str = f"{PREFIX}?next={self.encoded_next_uri}" edit_uri: str = f"{PREFIX}/edit?next={self.encoded_next_uri}" update_uri: str = f"{PREFIX}/update" diff --git a/tests/test_report.py b/tests/test_report.py index ce5ec79..0aab8f4 100644 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -24,7 +24,7 @@ import httpx from flask import Flask from test_site.lib import BaseTestData -from testlib import create_test_app, get_client, Accounts +from testlib import create_test_app, get_client, get_csrf_token, Accounts PREFIX: str = "/accounting" """The URL prefix for the reports.""" @@ -49,14 +49,17 @@ class ReportTestCase(unittest.TestCase): JournalEntry.query.delete() JournalEntryLineItem.query.delete() - self.client, self.csrf_token = get_client(self.app, "editor") + self.client: httpx.Client = get_client(self.app, "editor") + """The user client.""" + self.csrf_token: str = get_csrf_token(self.client) + """The CSRF token.""" def test_nobody(self) -> None: """Test the permission as nobody. :return: None. """ - client, csrf_token = get_client(self.app, "nobody") + client: httpx.Client = get_client(self.app, "nobody") ReportTestData(self.app, "editor").populate() response: httpx.Response @@ -147,7 +150,7 @@ class ReportTestCase(unittest.TestCase): :return: None. """ - client, csrf_token = get_client(self.app, "viewer") + client: httpx.Client = get_client(self.app, "viewer") ReportTestData(self.app, "editor").populate() response: httpx.Response diff --git a/tests/test_unmatched_offset.py b/tests/test_unmatched_offset.py index 0f5c20f..1418cd4 100644 --- a/tests/test_unmatched_offset.py +++ b/tests/test_unmatched_offset.py @@ -26,7 +26,8 @@ from accounting.utils.next_uri import encode_next from test_site import db from test_site.lib import JournalEntryCurrencyData, JournalEntryData, \ BaseTestData -from testlib import NEXT_URI, create_test_app, get_client, Accounts +from testlib import NEXT_URI, create_test_app, get_client, get_csrf_token, \ + Accounts PREFIX: str = "/accounting/match-offsets/USD" """The URL prefix for the unmatched offset management.""" @@ -51,14 +52,18 @@ class UnmatchedOffsetTestCase(unittest.TestCase): self.encoded_next_uri: str = encode_next(NEXT_URI) """The encoded next URI.""" - self.client, self.csrf_token = get_client(self.app, "editor") + self.client: httpx.Client = get_client(self.app, "editor") + """The user client.""" + self.csrf_token: str = get_csrf_token(self.client) + """The CSRF token.""" def test_nobody(self) -> None: """Test the permission as nobody. :return: None. """ - client, csrf_token = get_client(self.app, "nobody") + client: httpx.Client = get_client(self.app, "nobody") + csrf_token: str = get_csrf_token(client) DifferentTestData(self.app, "nobody").populate() response: httpx.Response @@ -72,7 +77,8 @@ class UnmatchedOffsetTestCase(unittest.TestCase): :return: None. """ - client, csrf_token = get_client(self.app, "viewer") + client: httpx.Client = get_client(self.app, "viewer") + csrf_token: str = get_csrf_token(client) DifferentTestData(self.app, "viewer").populate() response: httpx.Response diff --git a/tests/testlib.py b/tests/testlib.py index a3de4e8..ff1d5b0 100644 --- a/tests/testlib.py +++ b/tests/testlib.py @@ -89,12 +89,12 @@ def get_csrf_token(client: httpx.Client) -> str: return client.get("/.csrf-token").text -def get_client(app: Flask, username: str) -> tuple[httpx.Client, str]: +def get_client(app: Flask, username: str) -> httpx.Client: """Returns a user client. :param app: The Flask application. :param username: The username. - :return: A tuple of the client and the CSRF token. + :return: The user client. """ client: httpx.Client = httpx.Client(app=app, base_url=TEST_SERVER) client.headers["Referer"] = TEST_SERVER @@ -107,7 +107,7 @@ def get_client(app: Flask, username: str) -> tuple[httpx.Client, str]: "username": username}) assert response.status_code == 302 assert response.headers["Location"] == NEXT_URI - return client, csrf_token + return client def set_locale(app: Flask, client: httpx.Client, csrf_token: str,