2023-04-04 18:17:44 +08:00
|
|
|
# The Mia! Accounting Project.
|
2023-02-01 23:39:09 +08:00
|
|
|
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/2/1
|
|
|
|
|
|
|
|
# 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 account management.
|
|
|
|
|
|
|
|
"""
|
|
|
|
import unittest
|
2023-03-22 00:09:06 +08:00
|
|
|
from datetime import timedelta, date
|
2023-02-01 23:39:09 +08:00
|
|
|
|
2023-02-01 23:59:42 +08:00
|
|
|
import httpx
|
2023-02-01 23:39:09 +08:00
|
|
|
import sqlalchemy as sa
|
|
|
|
from click.testing import Result
|
|
|
|
from flask import Flask
|
|
|
|
from flask.testing import FlaskCliRunner
|
|
|
|
|
2023-03-14 21:28:35 +08:00
|
|
|
from test_site import db
|
2023-03-23 17:22:57 +08:00
|
|
|
from testlib import NEXT_URI, create_test_app, get_client, set_locale
|
2023-03-22 00:09:06 +08:00
|
|
|
from testlib_journal_entry import add_journal_entry
|
2023-02-07 11:29:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
class AccountData:
|
|
|
|
"""The account data."""
|
|
|
|
|
|
|
|
def __init__(self, base_code: str, no: int, title: str):
|
|
|
|
"""Constructs the account data.
|
|
|
|
|
|
|
|
:param base_code: The base code.
|
|
|
|
:param no: The number.
|
|
|
|
:param title: The title.
|
|
|
|
"""
|
|
|
|
self.base_code: str = base_code
|
|
|
|
"""The base code."""
|
|
|
|
self.no: int = no
|
|
|
|
"""The number."""
|
|
|
|
self.title: str = title
|
|
|
|
"""The title."""
|
|
|
|
self.code: str = f"{self.base_code}-{self.no:03d}"
|
|
|
|
"""The code."""
|
|
|
|
|
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
CASH: AccountData = AccountData("1111", 1, "Cash")
|
2023-02-07 11:29:09 +08:00
|
|
|
"""The cash account."""
|
2023-03-22 00:23:49 +08:00
|
|
|
PETTY: AccountData = AccountData("1112", 1, "Bank")
|
2023-03-22 00:09:06 +08:00
|
|
|
"""The petty cash account."""
|
2023-03-22 00:23:49 +08:00
|
|
|
BANK: AccountData = AccountData("1113", 1, "Bank")
|
2023-02-07 11:29:09 +08:00
|
|
|
"""The bank account."""
|
2023-03-22 00:23:49 +08:00
|
|
|
STOCK: AccountData = AccountData("1121", 1, "Stock")
|
2023-02-07 11:29:09 +08:00
|
|
|
"""The stock account."""
|
2023-03-22 00:23:49 +08:00
|
|
|
LOAN: AccountData = AccountData("2112", 1, "Loan")
|
2023-02-07 11:29:09 +08:00
|
|
|
"""The loan account."""
|
|
|
|
PREFIX: str = "/accounting/accounts"
|
2023-03-01 20:22:27 +08:00
|
|
|
"""The URL prefix for the account management."""
|
2023-02-01 23:39:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
class AccountCommandTestCase(unittest.TestCase):
|
|
|
|
"""The account console command test case."""
|
|
|
|
|
|
|
|
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-01 23:39:09 +08:00
|
|
|
|
|
|
|
runner: FlaskCliRunner = self.app.test_cli_runner()
|
|
|
|
with self.app.app_context():
|
|
|
|
from accounting.models import BaseAccount, Account, AccountL10n
|
|
|
|
result: Result
|
|
|
|
result = runner.invoke(args="init-db")
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
|
|
|
if BaseAccount.query.first() is None:
|
|
|
|
result = runner.invoke(args="accounting-init-base")
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
|
|
|
AccountL10n.query.delete()
|
|
|
|
Account.query.delete()
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def test_init(self) -> None:
|
|
|
|
"""Tests the "accounting-init-account" console command.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
from accounting.models import BaseAccount, Account, AccountL10n
|
|
|
|
runner: FlaskCliRunner = self.app.test_cli_runner()
|
|
|
|
with self.app.app_context():
|
|
|
|
result: Result = runner.invoke(args=["accounting-init-accounts",
|
|
|
|
"-u", "editor"])
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
|
|
|
with self.app.app_context():
|
|
|
|
bases: list[BaseAccount] = BaseAccount.query\
|
|
|
|
.filter(sa.func.char_length(BaseAccount.code) == 4).all()
|
|
|
|
accounts: list[Account] = Account.query.all()
|
|
|
|
l10n: list[AccountL10n] = AccountL10n.query.all()
|
|
|
|
self.assertEqual({x.code for x in bases},
|
|
|
|
{x.base_code for x in accounts})
|
|
|
|
self.assertEqual(len(accounts), len(bases))
|
|
|
|
self.assertEqual(len(l10n), len(bases) * 2)
|
|
|
|
base_dict: dict[str, BaseAccount] = {x.code: x for x in bases}
|
|
|
|
for account in accounts:
|
|
|
|
base: BaseAccount = base_dict[account.base_code]
|
|
|
|
self.assertEqual(account.no, 1)
|
|
|
|
self.assertEqual(account.title_l10n, base.title_l10n)
|
|
|
|
self.assertEqual({x.locale: x.title for x in account.l10n},
|
|
|
|
{x.locale: x.title for x in base.l10n})
|
2023-02-01 23:59:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
class AccountTestCase(unittest.TestCase):
|
|
|
|
"""The account test case."""
|
|
|
|
|
|
|
|
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-01 23:59:42 +08:00
|
|
|
|
|
|
|
runner: FlaskCliRunner = self.app.test_cli_runner()
|
|
|
|
with self.app.app_context():
|
|
|
|
from accounting.models import BaseAccount, Account, AccountL10n
|
|
|
|
result: Result
|
|
|
|
result = runner.invoke(args="init-db")
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
|
|
|
if BaseAccount.query.first() is None:
|
|
|
|
result = runner.invoke(args="accounting-init-base")
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
|
|
|
AccountL10n.query.delete()
|
|
|
|
Account.query.delete()
|
|
|
|
db.session.commit()
|
|
|
|
|
2023-02-13 19:18:15 +08:00
|
|
|
self.client, self.csrf_token = get_client(self.app, "editor")
|
2023-02-01 23:59:42 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/store",
|
2023-02-02 00:16:57 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": CASH.base_code,
|
|
|
|
"title": CASH.title})
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"],
|
2023-03-22 00:23:49 +08:00
|
|
|
f"{PREFIX}/{CASH.code}")
|
2023-02-01 23:59:42 +08:00
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/store",
|
2023-02-02 00:16:57 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": BANK.base_code,
|
|
|
|
"title": BANK.title})
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"],
|
2023-03-22 00:23:49 +08:00
|
|
|
f"{PREFIX}/{BANK.code}")
|
2023-02-01 23:59:42 +08:00
|
|
|
|
|
|
|
def test_nobody(self) -> None:
|
|
|
|
"""Test the permission as nobody.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-02-03 12:41:01 +08:00
|
|
|
from accounting.models import Account
|
2023-02-13 19:18:15 +08:00
|
|
|
client, csrf_token = get_client(self.app, "nobody")
|
2023-02-01 23:59:42 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
response = client.get(PREFIX)
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = client.get(f"{PREFIX}/{CASH.code}")
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
response = client.get(f"{PREFIX}/create")
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
response = client.post(f"{PREFIX}/store",
|
2023-02-06 21:45:28 +08:00
|
|
|
data={"csrf_token": csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": STOCK.base_code,
|
|
|
|
"title": STOCK.title})
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = client.get(f"{PREFIX}/{CASH.code}/edit")
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = client.post(f"{PREFIX}/{CASH.code}/update",
|
2023-02-06 21:45:28 +08:00
|
|
|
data={"csrf_token": csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": CASH.base_code,
|
|
|
|
"title": f"{CASH.title}-2"})
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = client.post(f"{PREFIX}/{BANK.code}/delete",
|
2023-02-06 21:45:28 +08:00
|
|
|
data={"csrf_token": csrf_token})
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = client.get(f"{PREFIX}/bases/{CASH.base_code}")
|
2023-02-03 12:41:01 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:23:49 +08:00
|
|
|
cash_id: int = Account.find_by_code(CASH.code).id
|
2023-02-03 12:41:01 +08:00
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = client.post(f"{PREFIX}/bases/{CASH.base_code}",
|
2023-02-06 21:45:28 +08:00
|
|
|
data={"csrf_token": csrf_token,
|
2023-02-27 16:34:43 +08:00
|
|
|
"next": NEXT_URI,
|
2023-03-22 00:09:06 +08:00
|
|
|
f"{cash_id}-no": "5"})
|
2023-02-03 12:41:01 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-02-01 23:59:42 +08:00
|
|
|
def test_viewer(self) -> None:
|
|
|
|
"""Test the permission as viewer.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-02-03 12:41:01 +08:00
|
|
|
from accounting.models import Account
|
2023-02-13 19:18:15 +08:00
|
|
|
client, csrf_token = get_client(self.app, "viewer")
|
2023-02-01 23:59:42 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
response = client.get(PREFIX)
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = client.get(f"{PREFIX}/{CASH.code}")
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
response = client.get(f"{PREFIX}/create")
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
response = client.post(f"{PREFIX}/store",
|
2023-02-06 21:45:28 +08:00
|
|
|
data={"csrf_token": csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": STOCK.base_code,
|
|
|
|
"title": STOCK.title})
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = client.get(f"{PREFIX}/{CASH.code}/edit")
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = client.post(f"{PREFIX}/{CASH.code}/update",
|
2023-02-06 21:45:28 +08:00
|
|
|
data={"csrf_token": csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": CASH.base_code,
|
|
|
|
"title": f"{CASH.title}-2"})
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = client.post(f"{PREFIX}/{BANK.code}/delete",
|
2023-02-06 21:45:28 +08:00
|
|
|
data={"csrf_token": csrf_token})
|
2023-02-01 23:59:42 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
2023-02-02 00:19:11 +08:00
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = client.get(f"{PREFIX}/bases/{CASH.base_code}")
|
2023-02-03 12:41:01 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:23:49 +08:00
|
|
|
cash_id: int = Account.find_by_code(CASH.code).id
|
2023-02-03 12:41:01 +08:00
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = client.post(f"{PREFIX}/bases/{CASH.base_code}",
|
2023-02-06 21:45:28 +08:00
|
|
|
data={"csrf_token": csrf_token,
|
2023-02-27 16:34:43 +08:00
|
|
|
"next": NEXT_URI,
|
2023-03-22 00:09:06 +08:00
|
|
|
f"{cash_id}-no": "5"})
|
2023-02-03 12:41:01 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-02-02 00:19:11 +08:00
|
|
|
def test_editor(self) -> None:
|
|
|
|
"""Test the permission as editor.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-02-03 12:41:01 +08:00
|
|
|
from accounting.models import Account
|
2023-02-02 00:19:11 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
response = self.client.get(PREFIX)
|
2023-02-02 00:19:11 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = self.client.get(f"{PREFIX}/{CASH.code}")
|
2023-02-02 00:19:11 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
response = self.client.get(f"{PREFIX}/create")
|
2023-02-02 00:19:11 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/store",
|
2023-02-02 00:19:11 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": STOCK.base_code,
|
|
|
|
"title": STOCK.title})
|
2023-02-02 00:19:11 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"],
|
2023-03-22 00:23:49 +08:00
|
|
|
f"{PREFIX}/{STOCK.code}")
|
2023-02-02 00:19:11 +08:00
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = self.client.get(f"{PREFIX}/{CASH.code}/edit")
|
2023-02-02 00:19:11 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/{CASH.code}/update",
|
2023-02-02 00:19:11 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": CASH.base_code,
|
|
|
|
"title": f"{CASH.title}-2"})
|
2023-02-02 00:19:11 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-03-22 00:23:49 +08:00
|
|
|
self.assertEqual(response.headers["Location"], f"{PREFIX}/{CASH.code}")
|
2023-02-02 00:19:11 +08:00
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/{BANK.code}/delete",
|
2023-02-02 00:19:11 +08:00
|
|
|
data={"csrf_token": self.csrf_token})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.headers["Location"], PREFIX)
|
2023-02-03 09:45:17 +08:00
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = self.client.get(f"{PREFIX}/bases/{CASH.base_code}")
|
2023-02-03 12:41:01 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:23:49 +08:00
|
|
|
cash_id: int = Account.find_by_code(CASH.code).id
|
2023-02-03 12:41:01 +08:00
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/bases/{CASH.base_code}",
|
2023-02-03 12:41:01 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-02-27 16:34:43 +08:00
|
|
|
"next": NEXT_URI,
|
2023-03-22 00:09:06 +08:00
|
|
|
f"{cash_id}-no": "5"})
|
2023-02-03 12:41:01 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-27 16:34:43 +08:00
|
|
|
self.assertEqual(response.headers["Location"], NEXT_URI)
|
2023-02-03 12:41:01 +08:00
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
def test_add(self) -> None:
|
|
|
|
"""Tests to add the currencies.
|
2023-02-03 09:45:17 +08:00
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
from accounting.models import Account
|
2023-02-07 11:29:09 +08:00
|
|
|
create_uri: str = f"{PREFIX}/create"
|
|
|
|
store_uri: str = f"{PREFIX}/store"
|
2023-03-22 00:23:49 +08:00
|
|
|
detail_uri: str = f"{PREFIX}/{STOCK.code}"
|
2023-02-03 09:45:17 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
with self.app.app_context():
|
|
|
|
self.assertEqual({x.code for x in Account.query.all()},
|
2023-03-22 00:23:49 +08:00
|
|
|
{CASH.code, BANK.code})
|
2023-02-07 11:29:09 +08:00
|
|
|
|
|
|
|
# Missing CSRF token
|
|
|
|
response = self.client.post(store_uri,
|
2023-03-22 00:23:49 +08:00
|
|
|
data={"base_code": STOCK.base_code,
|
|
|
|
"title": STOCK.title})
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
|
|
|
# CSRF token mismatch
|
|
|
|
response = self.client.post(store_uri,
|
|
|
|
data={"csrf_token": f"{self.csrf_token}-2",
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": STOCK.base_code,
|
|
|
|
"title": STOCK.title})
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
|
|
|
# Empty base account code
|
|
|
|
response = self.client.post(store_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
|
|
|
"base_code": " ",
|
2023-03-22 00:23:49 +08:00
|
|
|
"title": STOCK.title})
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
|
|
|
# Non-existing base account
|
|
|
|
response = self.client.post(store_uri,
|
2023-02-03 09:45:17 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-02-07 11:29:09 +08:00
|
|
|
"base_code": "9999",
|
2023-03-22 00:23:49 +08:00
|
|
|
"title": STOCK.title})
|
2023-02-03 09:45:17 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
2023-02-03 09:45:17 +08:00
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
# Unavailable base account
|
|
|
|
response = self.client.post(store_uri,
|
2023-02-03 09:45:17 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-02-07 11:29:09 +08:00
|
|
|
"base_code": "1",
|
2023-03-22 00:23:49 +08:00
|
|
|
"title": STOCK.title})
|
2023-02-03 09:45:17 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
|
|
|
# Empty name
|
|
|
|
response = self.client.post(store_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": STOCK.base_code,
|
2023-02-07 11:29:09 +08:00
|
|
|
"title": " "})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
2023-02-03 09:45:17 +08:00
|
|
|
|
2023-03-11 23:31:09 +08:00
|
|
|
# A nominal account that needs offset
|
|
|
|
response = self.client.post(store_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
|
|
|
"base_code": "6172",
|
2023-03-22 00:23:49 +08:00
|
|
|
"title": STOCK.title,
|
2023-03-18 22:52:29 +08:00
|
|
|
"is_need_offset": "yes"})
|
2023-03-11 23:31:09 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
# Success, with spaces to be stripped
|
|
|
|
response = self.client.post(store_uri,
|
2023-02-03 09:45:17 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": f" {STOCK.base_code} ",
|
|
|
|
"title": f" {STOCK.title} "})
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
|
|
|
|
|
|
|
# Success under the same base
|
|
|
|
response = self.client.post(store_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": STOCK.base_code,
|
|
|
|
"title": STOCK.title})
|
2023-02-03 09:45:17 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"],
|
2023-03-22 00:23:49 +08:00
|
|
|
f"{PREFIX}/{STOCK.base_code}-002")
|
2023-02-03 09:45:17 +08:00
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
# Success under the same base, with order in a mess.
|
2023-02-03 09:45:17 +08:00
|
|
|
with self.app.app_context():
|
2023-03-22 00:23:49 +08:00
|
|
|
stock_2: Account = Account.find_by_code(f"{STOCK.base_code}-002")
|
2023-02-07 11:29:09 +08:00
|
|
|
stock_2.no = 66
|
|
|
|
db.session.commit()
|
2023-02-03 09:45:17 +08:00
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
response = self.client.post(store_uri,
|
2023-02-03 09:45:17 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": STOCK.base_code,
|
|
|
|
"title": STOCK.title})
|
2023-02-03 09:45:17 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"],
|
2023-03-22 00:23:49 +08:00
|
|
|
f"{PREFIX}/{STOCK.base_code}-003")
|
2023-02-03 09:45:17 +08:00
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual({x.code for x in Account.query.all()},
|
2023-03-22 00:23:49 +08:00
|
|
|
{CASH.code, BANK.code, STOCK.code,
|
|
|
|
f"{STOCK.base_code}-002",
|
|
|
|
f"{STOCK.base_code}-003"})
|
2023-02-07 11:29:09 +08:00
|
|
|
|
2023-03-22 00:23:49 +08:00
|
|
|
account: Account = Account.find_by_code(STOCK.code)
|
|
|
|
self.assertEqual(account.base_code, STOCK.base_code)
|
|
|
|
self.assertEqual(account.title_l10n, STOCK.title)
|
2023-02-07 11:29:09 +08:00
|
|
|
|
|
|
|
def test_basic_update(self) -> None:
|
|
|
|
"""Tests the basic rules to update a user.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
from accounting.models import Account
|
2023-03-22 00:23:49 +08:00
|
|
|
detail_uri: str = f"{PREFIX}/{CASH.code}"
|
|
|
|
edit_uri: str = f"{PREFIX}/{CASH.code}/edit"
|
|
|
|
update_uri: str = f"{PREFIX}/{CASH.code}/update"
|
|
|
|
detail_c_uri: str = f"{PREFIX}/{STOCK.code}"
|
2023-02-07 11:29:09 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
|
|
|
# Success, with spaces to be stripped
|
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": f" {CASH.base_code} ",
|
|
|
|
"title": f" {CASH.title}-1 "})
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:23:49 +08:00
|
|
|
account: Account = Account.find_by_code(CASH.code)
|
|
|
|
self.assertEqual(account.base_code, CASH.base_code)
|
|
|
|
self.assertEqual(account.title_l10n, f"{CASH.title}-1")
|
2023-02-07 11:29:09 +08:00
|
|
|
|
|
|
|
# Empty base account code
|
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
|
|
|
"base_code": " ",
|
2023-03-22 00:23:49 +08:00
|
|
|
"title": STOCK.title})
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Non-existing base account
|
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
|
|
|
"base_code": "9999",
|
2023-03-22 00:23:49 +08:00
|
|
|
"title": STOCK.title})
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
|
|
|
|
|
|
|
# Unavailable base account
|
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
|
|
|
"base_code": "1",
|
2023-03-22 00:23:49 +08:00
|
|
|
"title": STOCK.title})
|
2023-02-07 11:29:09 +08:00
|
|
|
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,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": STOCK.base_code,
|
2023-02-07 11:29:09 +08:00
|
|
|
"title": " "})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
2023-03-11 23:31:09 +08:00
|
|
|
|
|
|
|
# A nominal account that needs offset
|
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
|
|
|
"base_code": "6172",
|
2023-03-22 00:23:49 +08:00
|
|
|
"title": STOCK.title,
|
2023-03-18 22:52:29 +08:00
|
|
|
"is_need_offset": "yes"})
|
2023-03-11 23:31:09 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], edit_uri)
|
2023-02-07 11:29:09 +08:00
|
|
|
|
|
|
|
# Change the base account
|
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": STOCK.base_code,
|
|
|
|
"title": STOCK.title})
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], detail_c_uri)
|
|
|
|
|
|
|
|
response = self.client.get(detail_uri)
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
|
|
response = self.client.get(detail_c_uri)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_update_not_modified(self) -> None:
|
|
|
|
"""Tests that the data is not modified.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
from accounting.models import Account
|
2023-03-22 00:23:49 +08:00
|
|
|
detail_uri: str = f"{PREFIX}/{CASH.code}"
|
|
|
|
update_uri: str = f"{PREFIX}/{CASH.code}/update"
|
2023-03-01 21:09:14 +08:00
|
|
|
account: Account
|
2023-02-07 11:29:09 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": f" {CASH.base_code} ",
|
|
|
|
"title": f" {CASH.title} "})
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:23:49 +08:00
|
|
|
account = Account.find_by_code(CASH.code)
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertIsNotNone(account)
|
|
|
|
account.created_at \
|
|
|
|
= account.created_at - timedelta(seconds=5)
|
|
|
|
account.updated_at = account.created_at
|
2023-02-25 17:58:55 +08:00
|
|
|
db.session.commit()
|
2023-02-07 11:29:09 +08:00
|
|
|
|
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": CASH.base_code,
|
|
|
|
"title": STOCK.title})
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:23:49 +08:00
|
|
|
account = Account.find_by_code(CASH.code)
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertIsNotNone(account)
|
|
|
|
self.assertLess(account.created_at,
|
|
|
|
account.updated_at)
|
2023-02-07 11:29:09 +08:00
|
|
|
|
|
|
|
def test_created_updated_by(self) -> None:
|
|
|
|
"""Tests the created-by and updated-by record.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
from accounting.models import Account
|
2023-03-24 08:32:28 +08:00
|
|
|
editor_username, admin_username = "editor", "admin"
|
|
|
|
client, csrf_token = get_client(self.app, admin_username)
|
2023-03-22 00:23:49 +08:00
|
|
|
detail_uri: str = f"{PREFIX}/{CASH.code}"
|
|
|
|
update_uri: str = f"{PREFIX}/{CASH.code}/update"
|
2023-03-01 21:09:14 +08:00
|
|
|
account: Account
|
2023-02-07 11:29:09 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:23:49 +08:00
|
|
|
account = Account.find_by_code(CASH.code)
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertEqual(account.created_by.username, editor_username)
|
|
|
|
self.assertEqual(account.updated_by.username, editor_username)
|
2023-02-07 11:29:09 +08:00
|
|
|
|
|
|
|
response = client.post(update_uri,
|
|
|
|
data={"csrf_token": csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": CASH.base_code,
|
|
|
|
"title": f"{CASH.title}-2"})
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:23:49 +08:00
|
|
|
account = Account.find_by_code(CASH.code)
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertEqual(account.created_by.username,
|
2023-02-07 11:29:09 +08:00
|
|
|
editor_username)
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertEqual(account.updated_by.username,
|
2023-03-24 08:32:28 +08:00
|
|
|
admin_username)
|
2023-02-07 11:29:09 +08:00
|
|
|
|
|
|
|
def test_l10n(self) -> None:
|
|
|
|
"""Tests the localization.
|
|
|
|
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
from accounting.models import Account
|
2023-03-22 00:23:49 +08:00
|
|
|
detail_uri: str = f"{PREFIX}/{CASH.code}"
|
|
|
|
update_uri: str = f"{PREFIX}/{CASH.code}/update"
|
2023-03-01 21:09:14 +08:00
|
|
|
account: Account
|
2023-02-07 11:29:09 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:23:49 +08:00
|
|
|
account = Account.find_by_code(CASH.code)
|
|
|
|
self.assertEqual(account.title_l10n, CASH.title)
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertEqual(account.l10n, [])
|
2023-02-07 11:29:09 +08:00
|
|
|
|
2023-02-13 19:18:15 +08:00
|
|
|
set_locale(self.client, self.csrf_token, "zh_Hant")
|
2023-02-07 11:29:09 +08:00
|
|
|
|
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": CASH.base_code,
|
|
|
|
"title": f"{CASH.title}-zh_Hant"})
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:23:49 +08:00
|
|
|
account = Account.find_by_code(CASH.code)
|
|
|
|
self.assertEqual(account.title_l10n, CASH.title)
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertEqual({(x.locale, x.title) for x in account.l10n},
|
2023-03-22 00:23:49 +08:00
|
|
|
{("zh_Hant", f"{CASH.title}-zh_Hant")})
|
2023-02-07 11:29:09 +08:00
|
|
|
|
2023-02-13 19:18:15 +08:00
|
|
|
set_locale(self.client, self.csrf_token, "en")
|
2023-02-07 11:29:09 +08:00
|
|
|
|
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": CASH.base_code,
|
|
|
|
"title": f"{CASH.title}-2"})
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:23:49 +08:00
|
|
|
account = Account.find_by_code(CASH.code)
|
|
|
|
self.assertEqual(account.title_l10n, f"{CASH.title}-2")
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertEqual({(x.locale, x.title) for x in account.l10n},
|
2023-03-22 00:23:49 +08:00
|
|
|
{("zh_Hant", f"{CASH.title}-zh_Hant")})
|
2023-02-07 11:29:09 +08:00
|
|
|
|
2023-02-13 19:18:15 +08:00
|
|
|
set_locale(self.client, self.csrf_token, "zh_Hant")
|
2023-02-07 11:29:09 +08:00
|
|
|
|
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": CASH.base_code,
|
|
|
|
"title": f"{CASH.title}-zh_Hant-2"})
|
2023-02-07 11:29:09 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:23:49 +08:00
|
|
|
account = Account.find_by_code(CASH.code)
|
|
|
|
self.assertEqual(account.title_l10n, f"{CASH.title}-2")
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertEqual({(x.locale, x.title) for x in account.l10n},
|
2023-03-22 00:23:49 +08:00
|
|
|
{("zh_Hant", f"{CASH.title}-zh_Hant-2")})
|
2023-02-07 11:29:09 +08:00
|
|
|
|
|
|
|
def test_delete(self) -> None:
|
|
|
|
"""Tests to delete a currency.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
from accounting.models import Account
|
2023-03-22 00:33:25 +08:00
|
|
|
detail_uri: str = f"{PREFIX}/{PETTY.code}"
|
|
|
|
delete_uri: str = f"{PREFIX}/{PETTY.code}/delete"
|
2023-02-07 11:29:09 +08:00
|
|
|
list_uri: str = PREFIX
|
|
|
|
response: httpx.Response
|
|
|
|
|
2023-03-22 00:09:06 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/store",
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:23:49 +08:00
|
|
|
"base_code": PETTY.base_code,
|
|
|
|
"title": PETTY.title})
|
2023-03-22 00:09:06 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-03-22 00:33:25 +08:00
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
2023-03-22 00:09:06 +08:00
|
|
|
|
|
|
|
response = self.client.post("/accounting/currencies/store",
|
|
|
|
data={"csrf_token": self.csrf_token,
|
|
|
|
"code": "USD",
|
|
|
|
"name": "US Dollars"})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"],
|
|
|
|
"/accounting/currencies/USD")
|
|
|
|
|
|
|
|
add_journal_entry(self.client,
|
|
|
|
form={"csrf_token": self.csrf_token,
|
|
|
|
"next": NEXT_URI,
|
|
|
|
"date": date.today().isoformat(),
|
|
|
|
"currency-1-code": "USD",
|
2023-03-22 00:33:25 +08:00
|
|
|
"currency-1-credit-1-account_code": BANK.code,
|
2023-03-22 00:09:06 +08:00
|
|
|
"currency-1-credit-1-amount": "20"})
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
with self.app.app_context():
|
|
|
|
self.assertEqual({x.code for x in Account.query.all()},
|
2023-03-22 00:23:49 +08:00
|
|
|
{CASH.code, PETTY.code, BANK.code})
|
2023-03-22 00:09:06 +08:00
|
|
|
|
|
|
|
# Cannot delete the cash account
|
2023-03-22 00:23:49 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/{CASH.code}/delete",
|
2023-03-22 00:09:06 +08:00
|
|
|
data={"csrf_token": self.csrf_token})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-03-22 00:33:25 +08:00
|
|
|
self.assertEqual(response.headers["Location"], f"{PREFIX}/{CASH.code}")
|
2023-03-22 00:09:06 +08:00
|
|
|
|
|
|
|
# Cannot delete the account that is in use
|
2023-03-22 00:33:25 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/{BANK.code}/delete",
|
2023-03-22 00:09:06 +08:00
|
|
|
data={"csrf_token": self.csrf_token})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-03-22 00:33:25 +08:00
|
|
|
self.assertEqual(response.headers["Location"], f"{PREFIX}/{BANK.code}")
|
2023-02-07 11:29:09 +08:00
|
|
|
|
2023-03-22 00:09:06 +08:00
|
|
|
# Success
|
2023-02-07 11:29:09 +08:00
|
|
|
response = self.client.get(detail_uri)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
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():
|
|
|
|
self.assertEqual({x.code for x in Account.query.all()},
|
2023-03-22 00:33:25 +08:00
|
|
|
{CASH.code, BANK.code})
|
2023-02-07 11:29:09 +08:00
|
|
|
|
|
|
|
response = self.client.get(detail_uri)
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
response = self.client.post(delete_uri,
|
|
|
|
data={"csrf_token": self.csrf_token})
|
|
|
|
self.assertEqual(response.status_code, 404)
|
2023-02-03 12:23:26 +08:00
|
|
|
|
2023-02-25 09:21:10 +08:00
|
|
|
def test_change_base_code(self) -> None:
|
|
|
|
"""Tests to change the base code of an account.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
from accounting.models import Account
|
|
|
|
response: httpx.Response
|
|
|
|
|
|
|
|
for i in range(2, 6):
|
|
|
|
response = self.client.post(f"{PREFIX}/store",
|
|
|
|
data={"csrf_token": self.csrf_token,
|
|
|
|
"base_code": "1111",
|
|
|
|
"title": "Title"})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"],
|
|
|
|
f"{PREFIX}/1111-00{i}")
|
|
|
|
|
|
|
|
with self.app.app_context():
|
|
|
|
account_1: Account = Account.find_by_code("1111-001")
|
|
|
|
id_1: int = account_1.id
|
|
|
|
account_2: Account = Account.find_by_code("1111-002")
|
|
|
|
id_2: int = account_2.id
|
|
|
|
account_3: Account = Account.find_by_code("1111-003")
|
|
|
|
id_3: int = account_3.id
|
|
|
|
account_4: Account = Account.find_by_code("1111-004")
|
|
|
|
id_4: int = account_4.id
|
|
|
|
account_5: Account = Account.find_by_code("1111-005")
|
|
|
|
id_5: int = account_5.id
|
|
|
|
account_1.no = 3
|
|
|
|
account_2.no = 5
|
|
|
|
account_3.no = 8
|
|
|
|
account_4.base_code = "1112"
|
|
|
|
account_4.no = 2
|
|
|
|
account_5.base_code = "1112"
|
|
|
|
account_5.no = 6
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
response = self.client.post(f"{PREFIX}/1111-005/update",
|
|
|
|
data={"csrf_token": self.csrf_token,
|
|
|
|
"base_code": "1112",
|
|
|
|
"title": "Title"})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], f"{PREFIX}/1112-003")
|
|
|
|
|
|
|
|
with self.app.app_context():
|
|
|
|
self.assertEqual(db.session.get(Account, id_1).no, 1)
|
|
|
|
self.assertEqual(db.session.get(Account, id_2).no, 3)
|
|
|
|
self.assertEqual(db.session.get(Account, id_3).no, 2)
|
|
|
|
self.assertEqual(db.session.get(Account, id_4).no, 1)
|
|
|
|
self.assertEqual(db.session.get(Account, id_5).no, 2)
|
|
|
|
|
2023-02-03 12:23:26 +08:00
|
|
|
def test_reorder(self) -> None:
|
|
|
|
"""Tests to reorder the accounts under a same base account.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
from accounting.models import Account
|
|
|
|
response: httpx.Response
|
|
|
|
|
|
|
|
for i in range(2, 6):
|
2023-02-07 11:29:09 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/store",
|
2023-02-03 12:23:26 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
|
|
|
"base_code": "1111",
|
|
|
|
"title": "Title"})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"],
|
2023-02-07 11:29:09 +08:00
|
|
|
f"{PREFIX}/1111-00{i}")
|
2023-02-03 12:23:26 +08:00
|
|
|
|
|
|
|
# Normal reorder
|
|
|
|
with self.app.app_context():
|
|
|
|
id_1: int = Account.find_by_code("1111-001").id
|
|
|
|
id_2: int = Account.find_by_code("1111-002").id
|
|
|
|
id_3: int = Account.find_by_code("1111-003").id
|
|
|
|
id_4: int = Account.find_by_code("1111-004").id
|
|
|
|
id_5: int = Account.find_by_code("1111-005").id
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/bases/1111",
|
2023-02-03 12:23:26 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-02-27 16:34:43 +08:00
|
|
|
"next": NEXT_URI,
|
2023-02-03 12:23:26 +08:00
|
|
|
f"{id_1}-no": "4",
|
|
|
|
f"{id_2}-no": "1",
|
|
|
|
f"{id_3}-no": "5",
|
|
|
|
f"{id_4}-no": "2",
|
|
|
|
f"{id_5}-no": "3"})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-27 16:34:43 +08:00
|
|
|
self.assertEqual(response.headers["Location"], NEXT_URI)
|
2023-02-03 12:23:26 +08:00
|
|
|
|
|
|
|
with self.app.app_context():
|
|
|
|
self.assertEqual(db.session.get(Account, id_1).code, "1111-004")
|
|
|
|
self.assertEqual(db.session.get(Account, id_2).code, "1111-001")
|
|
|
|
self.assertEqual(db.session.get(Account, id_3).code, "1111-005")
|
|
|
|
self.assertEqual(db.session.get(Account, id_4).code, "1111-002")
|
|
|
|
self.assertEqual(db.session.get(Account, id_5).code, "1111-003")
|
|
|
|
|
|
|
|
# Malformed orders
|
|
|
|
with self.app.app_context():
|
|
|
|
db.session.get(Account, id_1).no = 3
|
|
|
|
db.session.get(Account, id_2).no = 4
|
|
|
|
db.session.get(Account, id_3).no = 6
|
|
|
|
db.session.get(Account, id_4).no = 8
|
|
|
|
db.session.get(Account, id_5).no = 9
|
|
|
|
db.session.commit()
|
|
|
|
|
2023-02-07 11:29:09 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/bases/1111",
|
2023-02-03 12:23:26 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-02-27 16:34:43 +08:00
|
|
|
"next": NEXT_URI,
|
2023-02-03 12:23:26 +08:00
|
|
|
f"{id_2}-no": "3a",
|
|
|
|
f"{id_3}-no": "5",
|
|
|
|
f"{id_4}-no": "2"})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-27 16:34:43 +08:00
|
|
|
self.assertEqual(response.headers["Location"], NEXT_URI)
|
2023-02-03 12:23:26 +08:00
|
|
|
|
|
|
|
with self.app.app_context():
|
|
|
|
self.assertEqual(db.session.get(Account, id_1).code, "1111-003")
|
|
|
|
self.assertEqual(db.session.get(Account, id_2).code, "1111-004")
|
|
|
|
self.assertEqual(db.session.get(Account, id_3).code, "1111-002")
|
|
|
|
self.assertEqual(db.session.get(Account, id_4).code, "1111-001")
|
|
|
|
self.assertEqual(db.session.get(Account, id_5).code, "1111-005")
|