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.

This commit is contained in:
依瑪貓 2023-06-10 09:35:51 +08:00
parent 501c4b1d22
commit 356d2010cc
10 changed files with 105 additions and 49 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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.

View File

@ -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.

View File

@ -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()

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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,