Changed the properties of the test cases from public to private.

This commit is contained in:
2023-06-10 10:23:33 +08:00
parent 356d2010cc
commit 0ae00bce79
11 changed files with 1069 additions and 1058 deletions
+146 -145
View File
@@ -65,32 +65,32 @@ class CurrencyTestCase(unittest.TestCase):
:return: None.
"""
self.app: Flask = create_test_app()
self.__app: Flask = create_test_app()
"""The Flask application."""
with self.app.app_context():
with self.__app.app_context():
from accounting.models import Currency, CurrencyL10n
CurrencyL10n.query.delete()
Currency.query.delete()
db.session.commit()
self.client: httpx.Client = 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)
self.__csrf_token: str = get_csrf_token(self.__client)
"""The CSRF token."""
response: httpx.Response
response = self.client.post(f"{PREFIX}/store",
data={"csrf_token": self.csrf_token,
"code": USD.code,
"name": USD.name})
response = self.__client.post(f"{PREFIX}/store",
data={"csrf_token": self.__csrf_token,
"code": USD.code,
"name": USD.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], f"{PREFIX}/{USD.code}")
response = self.client.post(f"{PREFIX}/store",
data={"csrf_token": self.csrf_token,
"code": EUR.code,
"name": EUR.name})
response = self.__client.post(f"{PREFIX}/store",
data={"csrf_token": self.__csrf_token,
"code": EUR.code,
"name": EUR.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], f"{PREFIX}/{EUR.code}")
@@ -99,7 +99,7 @@ class CurrencyTestCase(unittest.TestCase):
:return: None.
"""
client: httpx.Client = get_client(self.app, "nobody")
client: httpx.Client = get_client(self.__app, "nobody")
csrf_token: str = get_csrf_token(client)
response: httpx.Response
@@ -136,7 +136,7 @@ class CurrencyTestCase(unittest.TestCase):
:return: None.
"""
client: httpx.Client = get_client(self.app, "viewer")
client: httpx.Client = get_client(self.__app, "viewer")
csrf_token: str = get_csrf_token(client)
response: httpx.Response
@@ -175,34 +175,34 @@ class CurrencyTestCase(unittest.TestCase):
"""
response: httpx.Response
response = self.client.get(PREFIX)
response = self.__client.get(PREFIX)
self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/{USD.code}")
response = self.__client.get(f"{PREFIX}/{USD.code}")
self.assertEqual(response.status_code, 200)
response = self.client.get(f"{PREFIX}/create")
response = self.__client.get(f"{PREFIX}/create")
self.assertEqual(response.status_code, 200)
response = self.client.post(f"{PREFIX}/store",
data={"csrf_token": self.csrf_token,
"code": TWD.code,
"name": TWD.name})
response = self.__client.post(f"{PREFIX}/store",
data={"csrf_token": self.__csrf_token,
"code": TWD.code,
"name": TWD.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], f"{PREFIX}/{TWD.code}")
response = self.client.get(f"{PREFIX}/{USD.code}/edit")
response = self.__client.get(f"{PREFIX}/{USD.code}/edit")
self.assertEqual(response.status_code, 200)
response = self.client.post(f"{PREFIX}/{USD.code}/update",
data={"csrf_token": self.csrf_token,
"code": JPY.code,
"name": JPY.name})
response = self.__client.post(f"{PREFIX}/{USD.code}/update",
data={"csrf_token": self.__csrf_token,
"code": JPY.code,
"name": JPY.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], f"{PREFIX}/{JPY.code}")
response = self.client.post(f"{PREFIX}/{EUR.code}/delete",
data={"csrf_token": self.csrf_token})
response = self.__client.post(f"{PREFIX}/{EUR.code}/delete",
data={"csrf_token": self.__csrf_token})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], PREFIX)
@@ -217,72 +217,73 @@ class CurrencyTestCase(unittest.TestCase):
detail_uri: str = f"{PREFIX}/{TWD.code}"
response: httpx.Response
with self.app.app_context():
with self.__app.app_context():
self.assertEqual({x.code for x in Currency.query.all()},
{USD.code, EUR.code})
# Missing CSRF token
response = self.client.post(store_uri,
data={"code": TWD.code,
"name": TWD.name})
response = self.__client.post(store_uri,
data={"code": TWD.code,
"name": TWD.name})
self.assertEqual(response.status_code, 400)
# CSRF token mismatch
response = self.client.post(store_uri,
data={"csrf_token": f"{self.csrf_token}-2",
"code": TWD.code,
"name": TWD.name})
response = self.__client.post(store_uri,
data={"csrf_token":
f"{self.__csrf_token}-2",
"code": TWD.code,
"name": TWD.name})
self.assertEqual(response.status_code, 400)
# Empty code
response = self.client.post(store_uri,
data={"csrf_token": self.csrf_token,
"code": " ",
"name": TWD.name})
response = self.__client.post(store_uri,
data={"csrf_token": self.__csrf_token,
"code": " ",
"name": TWD.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# Blocked code, with spaces to be stripped
response = self.client.post(store_uri,
data={"csrf_token": self.csrf_token,
"code": " create ",
"name": TWD.name})
response = self.__client.post(store_uri,
data={"csrf_token": self.__csrf_token,
"code": " create ",
"name": TWD.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# Bad code
response = self.client.post(store_uri,
data={"csrf_token": self.csrf_token,
"code": " zzc ",
"name": TWD.name})
response = self.__client.post(store_uri,
data={"csrf_token": self.__csrf_token,
"code": " zzc ",
"name": TWD.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# Empty name
response = self.client.post(store_uri,
data={"csrf_token": self.csrf_token,
"code": TWD.code,
"name": " "})
response = self.__client.post(store_uri,
data={"csrf_token": self.__csrf_token,
"code": TWD.code,
"name": " "})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
# Success, with spaces to be stripped
response = self.client.post(store_uri,
data={"csrf_token": self.csrf_token,
"code": f" {TWD.code} ",
"name": f" {TWD.name} "})
response = self.__client.post(store_uri,
data={"csrf_token": self.__csrf_token,
"code": f" {TWD.code} ",
"name": f" {TWD.name} "})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri)
# Duplicated code
response = self.client.post(store_uri,
data={"csrf_token": self.csrf_token,
"code": TWD.code,
"name": TWD.name})
response = self.__client.post(store_uri,
data={"csrf_token": self.__csrf_token,
"code": TWD.code,
"name": TWD.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], create_uri)
with self.app.app_context():
with self.__app.app_context():
self.assertEqual({x.code for x in Currency.query.all()},
{USD.code, EUR.code, TWD.code})
@@ -303,70 +304,70 @@ class CurrencyTestCase(unittest.TestCase):
response: httpx.Response
# Success, with spaces to be stripped
response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token,
"code": f" {USD.code} ",
"name": f" {USD.name}-1 "})
response = self.__client.post(update_uri,
data={"csrf_token": self.__csrf_token,
"code": f" {USD.code} ",
"name": f" {USD.name}-1 "})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri)
with self.app.app_context():
with self.__app.app_context():
currency: Currency = db.session.get(Currency, USD.code)
self.assertEqual(currency.code, USD.code)
self.assertEqual(currency.name_l10n, f"{USD.name}-1")
# Empty code
response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token,
"code": " ",
"name": TWD.name})
response = self.__client.post(update_uri,
data={"csrf_token": self.__csrf_token,
"code": " ",
"name": TWD.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Blocked code, with spaces to be stripped
response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token,
"code": " create ",
"name": TWD.name})
response = self.__client.post(update_uri,
data={"csrf_token": self.__csrf_token,
"code": " create ",
"name": TWD.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Bad code
response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token,
"code": "abc/def",
"name": TWD.name})
response = self.__client.post(update_uri,
data={"csrf_token": self.__csrf_token,
"code": "abc/def",
"name": TWD.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Empty name
response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token,
"code": TWD.code,
"name": " "})
response = self.__client.post(update_uri,
data={"csrf_token": self.__csrf_token,
"code": TWD.code,
"name": " "})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Duplicated code
response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token,
"code": EUR.code,
"name": TWD.name})
response = self.__client.post(update_uri,
data={"csrf_token": self.__csrf_token,
"code": EUR.code,
"name": TWD.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], edit_uri)
# Change code
response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token,
"code": TWD.code,
"name": TWD.name})
response = self.__client.post(update_uri,
data={"csrf_token": self.__csrf_token,
"code": TWD.code,
"name": TWD.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_c_uri)
response = self.client.get(detail_uri)
response = self.__client.get(detail_uri)
self.assertEqual(response.status_code, 404)
response = self.client.get(detail_c_uri)
response = self.__client.get(detail_c_uri)
self.assertEqual(response.status_code, 200)
def test_update_not_modified(self) -> None:
@@ -380,14 +381,14 @@ class CurrencyTestCase(unittest.TestCase):
currency: Currency | None
response: httpx.Response
response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token,
"code": f" {USD.code} ",
"name": f" {USD.name} "})
response = self.__client.post(update_uri,
data={"csrf_token": self.__csrf_token,
"code": f" {USD.code} ",
"name": f" {USD.name} "})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri)
with self.app.app_context():
with self.__app.app_context():
currency = db.session.get(Currency, USD.code)
self.assertIsNotNone(currency)
currency.created_at \
@@ -395,14 +396,14 @@ class CurrencyTestCase(unittest.TestCase):
currency.updated_at = currency.created_at
db.session.commit()
response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token,
"code": USD.code,
"name": TWD.name})
response = self.__client.post(update_uri,
data={"csrf_token": self.__csrf_token,
"code": USD.code,
"name": TWD.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri)
with self.app.app_context():
with self.__app.app_context():
currency = db.session.get(Currency, USD.code)
self.assertIsNotNone(currency)
self.assertLess(currency.created_at,
@@ -415,14 +416,14 @@ class CurrencyTestCase(unittest.TestCase):
"""
from accounting.models import Currency
editor_username, admin_username = "editor", "admin"
client: httpx.Client = 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
response: httpx.Response
with self.app.app_context():
with self.__app.app_context():
currency = db.session.get(Currency, USD.code)
self.assertEqual(currency.created_by.username, editor_username)
self.assertEqual(currency.updated_by.username, editor_username)
@@ -434,7 +435,7 @@ class CurrencyTestCase(unittest.TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri)
with self.app.app_context():
with self.__app.app_context():
currency = db.session.get(Currency, USD.code)
self.assertEqual(currency.created_by.username, editor_username)
self.assertEqual(currency.updated_by.username, admin_username)
@@ -446,14 +447,14 @@ class CurrencyTestCase(unittest.TestCase):
"""
response: httpx.Response
response = self.client.get(
response = self.__client.get(
f"/accounting/api/currencies/exists-code?q={USD.code}")
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertEqual(set(data.keys()), {"exists"})
self.assertTrue(data["exists"])
response = self.client.get(
response = self.__client.get(
f"/accounting/api/currencies/exists-code?q={USD.code}-1")
self.assertEqual(response.status_code, 200)
data = response.json()
@@ -471,51 +472,51 @@ class CurrencyTestCase(unittest.TestCase):
currency: Currency
response: httpx.Response
with self.app.app_context():
with self.__app.app_context():
currency = db.session.get(Currency, USD.code)
self.assertEqual(currency.name_l10n, USD.name)
self.assertEqual(currency.l10n, [])
set_locale(self.app, self.client, self.csrf_token, "zh_Hant")
set_locale(self.__app, self.__client, self.__csrf_token, "zh_Hant")
response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token,
"code": USD.code,
"name": f"{USD.name}-zh_Hant"})
response = self.__client.post(update_uri,
data={"csrf_token": self.__csrf_token,
"code": USD.code,
"name": f"{USD.name}-zh_Hant"})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri)
with self.app.app_context():
with self.__app.app_context():
currency = db.session.get(Currency, USD.code)
self.assertEqual(currency.name_l10n, USD.name)
self.assertEqual({(x.locale, x.name) for x in currency.l10n},
{("zh_Hant", f"{USD.name}-zh_Hant")})
set_locale(self.app, self.client, self.csrf_token, "en")
set_locale(self.__app, self.__client, self.__csrf_token, "en")
response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token,
"code": USD.code,
"name": f"{USD.name}-2"})
response = self.__client.post(update_uri,
data={"csrf_token": self.__csrf_token,
"code": USD.code,
"name": f"{USD.name}-2"})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri)
with self.app.app_context():
with self.__app.app_context():
currency = db.session.get(Currency, USD.code)
self.assertEqual(currency.name_l10n, f"{USD.name}-2")
self.assertEqual({(x.locale, x.name) for x in currency.l10n},
{("zh_Hant", f"{USD.name}-zh_Hant")})
set_locale(self.app, self.client, self.csrf_token, "zh_Hant")
set_locale(self.__app, self.__client, self.__csrf_token, "zh_Hant")
response = self.client.post(update_uri,
data={"csrf_token": self.csrf_token,
"code": USD.code,
"name": f"{USD.name}-zh_Hant-2"})
response = self.__client.post(update_uri,
data={"csrf_token": self.__csrf_token,
"code": USD.code,
"name": f"{USD.name}-zh_Hant-2"})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri)
with self.app.app_context():
with self.__app.app_context():
currency = db.session.get(Currency, USD.code)
self.assertEqual(currency.name_l10n, f"{USD.name}-2")
self.assertEqual({(x.locale, x.name) for x in currency.l10n},
@@ -529,56 +530,56 @@ class CurrencyTestCase(unittest.TestCase):
from accounting.models import Currency
detail_uri: str = f"{PREFIX}/{JPY.code}"
delete_uri: str = f"{PREFIX}/{JPY.code}/delete"
with self.app.app_context():
with self.__app.app_context():
encoded_next_uri: str = encode_next(NEXT_URI)
list_uri: str = PREFIX
response: httpx.Response
response = self.client.post(f"{PREFIX}/store",
data={"csrf_token": self.csrf_token,
"code": JPY.code,
"name": JPY.name})
response = self.__client.post(f"{PREFIX}/store",
data={"csrf_token": self.__csrf_token,
"code": JPY.code,
"name": JPY.name})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], detail_uri)
add_journal_entry(self.client,
form={"csrf_token": self.csrf_token,
add_journal_entry(self.__client,
form={"csrf_token": self.__csrf_token,
"next": encoded_next_uri,
"date": dt.date.today().isoformat(),
"currency-1-code": EUR.code,
"currency-1-credit-1-account_code": "1111-001",
"currency-1-credit-1-amount": "20"})
with self.app.app_context():
with self.__app.app_context():
self.assertEqual({x.code for x in Currency.query.all()},
{USD.code, EUR.code, JPY.code})
# Cannot delete the default currency
response = self.client.post(f"{PREFIX}/{USD.code}/delete",
data={"csrf_token": self.csrf_token})
response = self.__client.post(f"{PREFIX}/{USD.code}/delete",
data={"csrf_token": self.__csrf_token})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], f"{PREFIX}/{USD.code}")
# Cannot delete the account that is in use
response = self.client.post(f"{PREFIX}/{EUR.code}/delete",
data={"csrf_token": self.csrf_token})
response = self.__client.post(f"{PREFIX}/{EUR.code}/delete",
data={"csrf_token": self.__csrf_token})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], f"{PREFIX}/{EUR.code}")
# Success
response = self.client.get(detail_uri)
response = self.__client.get(detail_uri)
self.assertEqual(response.status_code, 200)
response = self.client.post(delete_uri,
data={"csrf_token": self.csrf_token})
response = self.__client.post(delete_uri,
data={"csrf_token": self.__csrf_token})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], list_uri)
with self.app.app_context():
with self.__app.app_context():
self.assertEqual({x.code for x in Currency.query.all()},
{USD.code, EUR.code})
response = self.client.get(detail_uri)
response = self.__client.get(detail_uri)
self.assertEqual(response.status_code, 404)
response = self.client.post(delete_uri,
data={"csrf_token": self.csrf_token})
response = self.__client.post(delete_uri,
data={"csrf_token": self.__csrf_token})
self.assertEqual(response.status_code, 404)