2023-02-07 00:07:23 +08:00
|
|
|
# The Mia! Accounting Flask Project.
|
|
|
|
# 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 currency management.
|
|
|
|
|
|
|
|
"""
|
2023-02-08 10:50:20 +08:00
|
|
|
import csv
|
|
|
|
import typing as t
|
2023-02-07 00:07:23 +08:00
|
|
|
import unittest
|
2023-03-22 00:37:39 +08:00
|
|
|
from datetime import timedelta, date
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
import httpx
|
|
|
|
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
|
|
|
|
from testlib_journal_entry import add_journal_entry
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
|
2023-02-07 08:21:44 +08:00
|
|
|
class CurrencyData:
|
|
|
|
"""The currency data."""
|
|
|
|
|
|
|
|
def __init__(self, code: str, name: str):
|
|
|
|
"""Constructs the currency data.
|
|
|
|
|
|
|
|
:param code: The code.
|
|
|
|
:param name: The name.
|
|
|
|
"""
|
|
|
|
self.code: str = code
|
|
|
|
"""The code."""
|
|
|
|
self.name: str = name
|
|
|
|
"""The name."""
|
|
|
|
|
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
USD: CurrencyData = CurrencyData("USD", "US Dollar")
|
|
|
|
"""The US dollars."""
|
|
|
|
EUR: CurrencyData = CurrencyData("EUR", "Euro")
|
|
|
|
"""The European dollars."""
|
|
|
|
TWD: CurrencyData = CurrencyData("TWD", "Taiwan dollars")
|
|
|
|
"""The Taiwan dollars."""
|
|
|
|
JPY: CurrencyData = CurrencyData("JPY", "Japanese yen")
|
|
|
|
"""The Japanese yen."""
|
2023-02-07 09:39:40 +08:00
|
|
|
PREFIX: str = "/accounting/currencies"
|
2023-03-01 20:22:27 +08:00
|
|
|
"""The URL prefix for the currency management."""
|
2023-02-07 08:21:44 +08:00
|
|
|
|
|
|
|
|
2023-02-07 00:07:23 +08:00
|
|
|
class CurrencyCommandTestCase(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-07 00:07:23 +08:00
|
|
|
|
|
|
|
runner: FlaskCliRunner = self.app.test_cli_runner()
|
|
|
|
with self.app.app_context():
|
|
|
|
from accounting.models import Currency, CurrencyL10n
|
|
|
|
result: Result
|
|
|
|
result = runner.invoke(args="init-db")
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
|
|
|
CurrencyL10n.query.delete()
|
|
|
|
Currency.query.delete()
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def test_init(self) -> None:
|
|
|
|
"""Tests the "accounting-init-currencies" console command.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-02-08 10:50:20 +08:00
|
|
|
from accounting import data_dir
|
|
|
|
from accounting.models import Currency
|
|
|
|
|
2023-02-09 00:02:14 +08:00
|
|
|
with open(data_dir / "currencies.csv") as fp:
|
2023-02-08 10:50:20 +08:00
|
|
|
data: dict[dict[str, t.Any]] \
|
|
|
|
= {x["code"]: {"code": x["code"],
|
|
|
|
"name": x["name"],
|
|
|
|
"l10n": {y[5:]: x[y]
|
|
|
|
for y in x if y.startswith("l10n-")}}
|
2023-02-09 00:02:14 +08:00
|
|
|
for x in csv.DictReader(fp)}
|
2023-02-08 10:50:20 +08:00
|
|
|
|
2023-02-07 00:07:23 +08:00
|
|
|
runner: FlaskCliRunner = self.app.test_cli_runner()
|
|
|
|
with self.app.app_context():
|
|
|
|
result: Result = runner.invoke(
|
|
|
|
args=["accounting-init-currencies", "-u", "editor"])
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
|
|
|
currencies: list[Currency] = Currency.query.all()
|
2023-02-08 10:50:20 +08:00
|
|
|
|
|
|
|
self.assertEqual(len(currencies), len(data))
|
2023-02-07 00:07:23 +08:00
|
|
|
for currency in currencies:
|
2023-02-08 10:50:20 +08:00
|
|
|
self.assertIn(currency.code, data)
|
|
|
|
self.assertEqual(currency.name_l10n, data[currency.code]["name"])
|
|
|
|
l10n: dict[str, str] = {x.locale: x.name for x in currency.l10n}
|
|
|
|
self.assertEqual(len(l10n), len(data[currency.code]["l10n"]))
|
|
|
|
for locale in l10n:
|
|
|
|
self.assertIn(locale, data[currency.code]["l10n"])
|
|
|
|
self.assertEqual(l10n[locale],
|
|
|
|
data[currency.code]["l10n"][locale])
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CurrencyTestCase(unittest.TestCase):
|
|
|
|
"""The currency 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-07 00:07:23 +08:00
|
|
|
|
|
|
|
runner: FlaskCliRunner = self.app.test_cli_runner()
|
|
|
|
with self.app.app_context():
|
|
|
|
from accounting.models import Currency, CurrencyL10n
|
|
|
|
result: Result
|
|
|
|
result = runner.invoke(args="init-db")
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
|
|
|
CurrencyL10n.query.delete()
|
|
|
|
Currency.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-07 00:07:23 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
2023-02-07 09:39:40 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/store",
|
2023-02-07 00:07:23 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": USD.code,
|
|
|
|
"name": USD.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-03-22 00:37:39 +08:00
|
|
|
self.assertEqual(response.headers["Location"], f"{PREFIX}/{USD.code}")
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-02-07 09:39:40 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/store",
|
2023-02-07 00:07:23 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": EUR.code,
|
|
|
|
"name": EUR.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-03-22 00:37:39 +08:00
|
|
|
self.assertEqual(response.headers["Location"], f"{PREFIX}/{EUR.code}")
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
def test_nobody(self) -> None:
|
|
|
|
"""Test the permission as nobody.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-02-13 19:18:15 +08:00
|
|
|
client, csrf_token = get_client(self.app, "nobody")
|
2023-02-07 00:07:23 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
2023-02-07 09:39:40 +08:00
|
|
|
response = client.get(PREFIX)
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
response = client.get(f"{PREFIX}/{USD.code}")
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-02-07 09:39:40 +08:00
|
|
|
response = client.get(f"{PREFIX}/create")
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-02-07 09:39:40 +08:00
|
|
|
response = client.post(f"{PREFIX}/store",
|
2023-02-07 00:07:23 +08:00
|
|
|
data={"csrf_token": csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": TWD.code,
|
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
response = client.get(f"{PREFIX}/{USD.code}/edit")
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
response = client.post(f"{PREFIX}/{USD.code}/update",
|
2023-02-07 00:07:23 +08:00
|
|
|
data={"csrf_token": csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": JPY.code,
|
|
|
|
"name": JPY.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
response = client.post(f"{PREFIX}/{EUR.code}/delete",
|
2023-02-07 00:07:23 +08:00
|
|
|
data={"csrf_token": csrf_token})
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
def test_viewer(self) -> None:
|
|
|
|
"""Test the permission as viewer.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-02-13 19:18:15 +08:00
|
|
|
client, csrf_token = get_client(self.app, "viewer")
|
2023-02-07 00:07:23 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
2023-02-07 09:39:40 +08:00
|
|
|
response = client.get(PREFIX)
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
response = client.get(f"{PREFIX}/{USD.code}")
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2023-02-07 09:39:40 +08:00
|
|
|
response = client.get(f"{PREFIX}/create")
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-02-07 09:39:40 +08:00
|
|
|
response = client.post(f"{PREFIX}/store",
|
2023-02-07 00:07:23 +08:00
|
|
|
data={"csrf_token": csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": TWD.code,
|
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
response = client.get(f"{PREFIX}/{USD.code}/edit")
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
response = client.post(f"{PREFIX}/{USD.code}/update",
|
2023-02-07 00:07:23 +08:00
|
|
|
data={"csrf_token": csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": JPY.code,
|
|
|
|
"name": JPY.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
response = client.post(f"{PREFIX}/{EUR.code}/delete",
|
2023-02-07 00:07:23 +08:00
|
|
|
data={"csrf_token": csrf_token})
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
def test_editor(self) -> None:
|
|
|
|
"""Test the permission as editor.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
response: httpx.Response
|
|
|
|
|
2023-02-07 09:39:40 +08:00
|
|
|
response = self.client.get(PREFIX)
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
response = self.client.get(f"{PREFIX}/{USD.code}")
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2023-02-07 09:39:40 +08:00
|
|
|
response = self.client.get(f"{PREFIX}/create")
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2023-02-07 09:39:40 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/store",
|
2023-02-07 00:07:23 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": TWD.code,
|
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-03-22 00:37:39 +08:00
|
|
|
self.assertEqual(response.headers["Location"], f"{PREFIX}/{TWD.code}")
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
response = self.client.get(f"{PREFIX}/{USD.code}/edit")
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/{USD.code}/update",
|
2023-02-07 09:39:40 +08:00
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": JPY.code,
|
|
|
|
"name": JPY.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-03-22 00:37:39 +08:00
|
|
|
self.assertEqual(response.headers["Location"], f"{PREFIX}/{JPY.code}")
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
response = self.client.post(f"{PREFIX}/{EUR.code}/delete",
|
2023-02-07 09:39:40 +08:00
|
|
|
data={"csrf_token": self.csrf_token})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-07 09:39:40 +08:00
|
|
|
self.assertEqual(response.headers["Location"], PREFIX)
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
def test_add(self) -> None:
|
|
|
|
"""Tests to add the currencies.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
from accounting.models import Currency
|
2023-02-07 09:39:40 +08:00
|
|
|
create_uri: str = f"{PREFIX}/create"
|
|
|
|
store_uri: str = f"{PREFIX}/store"
|
2023-03-22 00:37:39 +08:00
|
|
|
detail_uri: str = f"{PREFIX}/{TWD.code}"
|
2023-02-07 00:07:23 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
|
|
|
with self.app.app_context():
|
|
|
|
self.assertEqual({x.code for x in Currency.query.all()},
|
2023-03-22 00:37:39 +08:00
|
|
|
{USD.code, EUR.code})
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
# Missing CSRF token
|
|
|
|
response = self.client.post(store_uri,
|
2023-03-22 00:37:39 +08:00
|
|
|
data={"code": TWD.code,
|
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +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:37:39 +08:00
|
|
|
"code": TWD.code,
|
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
|
|
|
# Empty code
|
|
|
|
response = self.client.post(store_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
|
|
|
"code": " ",
|
2023-03-22 00:37:39 +08:00
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
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 ",
|
2023-03-22 00:37:39 +08:00
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
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 ",
|
2023-03-22 00:37:39 +08:00
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
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,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": TWD.code,
|
2023-02-07 00:07:23 +08:00
|
|
|
"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,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": f" {TWD.code} ",
|
|
|
|
"name": f" {TWD.name} "})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-07 08:21:44 +08:00
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
# Duplicated code
|
|
|
|
response = self.client.post(store_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": TWD.code,
|
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], create_uri)
|
|
|
|
|
|
|
|
with self.app.app_context():
|
|
|
|
self.assertEqual({x.code for x in Currency.query.all()},
|
2023-03-22 00:37:39 +08:00
|
|
|
{USD.code, EUR.code, TWD.code})
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
currency: Currency = db.session.get(Currency, TWD.code)
|
|
|
|
self.assertEqual(currency.code, TWD.code)
|
|
|
|
self.assertEqual(currency.name_l10n, TWD.name)
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
def test_basic_update(self) -> None:
|
|
|
|
"""Tests the basic rules to update a user.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
from accounting.models import Currency
|
2023-03-22 00:37:39 +08:00
|
|
|
detail_uri: str = f"{PREFIX}/{USD.code}"
|
|
|
|
edit_uri: str = f"{PREFIX}/{USD.code}/edit"
|
|
|
|
update_uri: str = f"{PREFIX}/{USD.code}/update"
|
|
|
|
detail_c_uri: str = f"{PREFIX}/{TWD.code}"
|
2023-02-07 00:07:23 +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:37:39 +08:00
|
|
|
"code": f" {USD.code} ",
|
|
|
|
"name": f" {USD.name}-1 "})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-07 08:21:44 +08:00
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:37:39 +08:00
|
|
|
currency: Currency = db.session.get(Currency, USD.code)
|
|
|
|
self.assertEqual(currency.code, USD.code)
|
|
|
|
self.assertEqual(currency.name_l10n, f"{USD.name}-1")
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
# Empty code
|
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
|
|
|
"code": " ",
|
2023-03-22 00:37:39 +08:00
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
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 ",
|
2023-03-22 00:37:39 +08:00
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
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",
|
2023-03-22 00:37:39 +08:00
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +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:37:39 +08:00
|
|
|
"code": TWD.code,
|
2023-02-07 00:07:23 +08:00
|
|
|
"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,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": EUR.code,
|
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
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,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": TWD.code,
|
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-07 08:21:44 +08:00
|
|
|
self.assertEqual(response.headers["Location"], detail_c_uri)
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-02-07 08:21:44 +08:00
|
|
|
response = self.client.get(detail_uri)
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
2023-02-07 08:21:44 +08:00
|
|
|
response = self.client.get(detail_c_uri)
|
2023-02-07 00:07:23 +08:00
|
|
|
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 Currency
|
2023-03-22 00:37:39 +08:00
|
|
|
detail_uri: str = f"{PREFIX}/{USD.code}"
|
|
|
|
update_uri: str = f"{PREFIX}/{USD.code}/update"
|
2023-03-01 21:09:14 +08:00
|
|
|
currency: Currency | None
|
2023-02-07 00:07:23 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": f" {USD.code} ",
|
|
|
|
"name": f" {USD.name} "})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:37:39 +08:00
|
|
|
currency = db.session.get(Currency, USD.code)
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertIsNotNone(currency)
|
|
|
|
currency.created_at \
|
|
|
|
= currency.created_at - timedelta(seconds=5)
|
|
|
|
currency.updated_at = currency.created_at
|
2023-02-25 17:58:55 +08:00
|
|
|
db.session.commit()
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": USD.code,
|
|
|
|
"name": TWD.name})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:37:39 +08:00
|
|
|
currency = db.session.get(Currency, USD.code)
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertIsNotNone(currency)
|
|
|
|
self.assertLess(currency.created_at,
|
|
|
|
currency.updated_at)
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
def test_created_updated_by(self) -> None:
|
|
|
|
"""Tests the created-by and updated-by record.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
from accounting.models import Currency
|
|
|
|
editor_username, editor2_username = "editor", "editor2"
|
2023-02-13 19:18:15 +08:00
|
|
|
client, csrf_token = get_client(self.app, editor2_username)
|
2023-03-22 00:37:39 +08:00
|
|
|
detail_uri: str = f"{PREFIX}/{USD.code}"
|
|
|
|
update_uri: str = f"{PREFIX}/{USD.code}/update"
|
2023-03-01 21:09:14 +08:00
|
|
|
currency: Currency
|
2023-02-07 00:07:23 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:37:39 +08:00
|
|
|
currency = db.session.get(Currency, USD.code)
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertEqual(currency.created_by.username, editor_username)
|
|
|
|
self.assertEqual(currency.updated_by.username, editor_username)
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-02-07 08:21:44 +08:00
|
|
|
response = client.post(update_uri,
|
2023-02-07 00:07:23 +08:00
|
|
|
data={"csrf_token": csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": USD.code,
|
|
|
|
"name": f"{USD.name}-2"})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-07 08:21:44 +08:00
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:37:39 +08:00
|
|
|
currency = db.session.get(Currency, USD.code)
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertEqual(currency.created_by.username, editor_username)
|
|
|
|
self.assertEqual(currency.updated_by.username, editor2_username)
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-02-07 21:44:58 +08:00
|
|
|
def test_api_exists(self) -> None:
|
|
|
|
"""Tests the API to check if a code exists.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
response: httpx.Response
|
|
|
|
|
|
|
|
response = self.client.get(
|
2023-03-22 00:37:39 +08:00
|
|
|
f"/accounting/api/currencies/exists-code?q={USD.code}")
|
2023-02-07 21:44:58 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
data = response.json()
|
|
|
|
self.assertEqual(set(data.keys()), {"exists"})
|
|
|
|
self.assertTrue(data["exists"])
|
|
|
|
|
|
|
|
response = self.client.get(
|
2023-03-22 00:37:39 +08:00
|
|
|
f"/accounting/api/currencies/exists-code?q={USD.code}-1")
|
2023-02-07 21:44:58 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
data = response.json()
|
|
|
|
self.assertEqual(set(data.keys()), {"exists"})
|
|
|
|
self.assertFalse(data["exists"])
|
|
|
|
|
2023-02-07 00:07:23 +08:00
|
|
|
def test_l10n(self) -> None:
|
|
|
|
"""Tests the localization.
|
|
|
|
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
from accounting.models import Currency
|
2023-03-22 00:37:39 +08:00
|
|
|
detail_uri: str = f"{PREFIX}/{USD.code}"
|
|
|
|
update_uri: str = f"{PREFIX}/{USD.code}/update"
|
2023-03-01 21:09:14 +08:00
|
|
|
currency: Currency
|
2023-02-07 08:21:44 +08:00
|
|
|
response: httpx.Response
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:37:39 +08:00
|
|
|
currency = db.session.get(Currency, USD.code)
|
|
|
|
self.assertEqual(currency.name_l10n, USD.name)
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertEqual(currency.l10n, [])
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-02-13 19:18:15 +08:00
|
|
|
set_locale(self.client, self.csrf_token, "zh_Hant")
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-02-07 00:38:41 +08:00
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": USD.code,
|
|
|
|
"name": f"{USD.name}-zh_Hant"})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-07 00:38:41 +08:00
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:37:39 +08:00
|
|
|
currency = db.session.get(Currency, USD.code)
|
|
|
|
self.assertEqual(currency.name_l10n, USD.name)
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertEqual({(x.locale, x.name) for x in currency.l10n},
|
2023-03-22 00:37:39 +08:00
|
|
|
{("zh_Hant", f"{USD.name}-zh_Hant")})
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-02-13 19:18:15 +08:00
|
|
|
set_locale(self.client, self.csrf_token, "en")
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-02-07 00:38:41 +08:00
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": USD.code,
|
|
|
|
"name": f"{USD.name}-2"})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-07 00:38:41 +08:00
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:37:39 +08:00
|
|
|
currency = db.session.get(Currency, USD.code)
|
|
|
|
self.assertEqual(currency.name_l10n, f"{USD.name}-2")
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertEqual({(x.locale, x.name) for x in currency.l10n},
|
2023-03-22 00:37:39 +08:00
|
|
|
{("zh_Hant", f"{USD.name}-zh_Hant")})
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-02-13 19:18:15 +08:00
|
|
|
set_locale(self.client, self.csrf_token, "zh_Hant")
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-02-07 00:38:41 +08:00
|
|
|
response = self.client.post(update_uri,
|
|
|
|
data={"csrf_token": self.csrf_token,
|
2023-03-22 00:37:39 +08:00
|
|
|
"code": USD.code,
|
|
|
|
"name": f"{USD.name}-zh_Hant-2"})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-07 00:38:41 +08:00
|
|
|
self.assertEqual(response.headers["Location"], detail_uri)
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
with self.app.app_context():
|
2023-03-22 00:37:39 +08:00
|
|
|
currency = db.session.get(Currency, USD.code)
|
|
|
|
self.assertEqual(currency.name_l10n, f"{USD.name}-2")
|
2023-03-01 21:09:14 +08:00
|
|
|
self.assertEqual({(x.locale, x.name) for x in currency.l10n},
|
2023-03-22 00:37:39 +08:00
|
|
|
{("zh_Hant", f"{USD.name}-zh_Hant-2")})
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
def test_delete(self) -> None:
|
|
|
|
"""Tests to delete a currency.
|
|
|
|
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
from accounting.models import Currency
|
2023-03-22 00:37:39 +08:00
|
|
|
detail_uri: str = f"{PREFIX}/{JPY.code}"
|
|
|
|
delete_uri: str = f"{PREFIX}/{JPY.code}/delete"
|
2023-02-07 09:39:40 +08:00
|
|
|
list_uri: str = PREFIX
|
2023-02-07 00:07:23 +08:00
|
|
|
response: httpx.Response
|
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
runner: FlaskCliRunner = self.app.test_cli_runner()
|
|
|
|
with self.app.app_context():
|
|
|
|
from accounting.models import BaseAccount
|
|
|
|
if BaseAccount.query.first() is None:
|
|
|
|
result = runner.invoke(args="accounting-init-base")
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
|
|
|
|
|
|
|
response = self.client.post("/accounting/accounts/store",
|
|
|
|
data={"csrf_token": self.csrf_token,
|
|
|
|
"base_code": "1111",
|
|
|
|
"title": "Cash"})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"],
|
|
|
|
"/accounting/accounts/1111-001")
|
|
|
|
|
|
|
|
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,
|
|
|
|
"next": NEXT_URI,
|
|
|
|
"date": date.today().isoformat(),
|
|
|
|
"currency-1-code": EUR.code,
|
|
|
|
"currency-1-credit-1-account_code": "1111-001",
|
|
|
|
"currency-1-credit-1-amount": "20"})
|
|
|
|
|
2023-02-07 00:07:23 +08:00
|
|
|
with self.app.app_context():
|
|
|
|
self.assertEqual({x.code for x in Currency.query.all()},
|
2023-03-22 00:37:39 +08:00
|
|
|
{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})
|
|
|
|
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})
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual(response.headers["Location"], f"{PREFIX}/{EUR.code}")
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-03-22 00:37:39 +08:00
|
|
|
# Success
|
2023-02-07 08:21:44 +08:00
|
|
|
response = self.client.get(detail_uri)
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2023-02-07 08:21:44 +08:00
|
|
|
response = self.client.post(delete_uri,
|
|
|
|
data={"csrf_token": self.csrf_token})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 302)
|
2023-02-07 08:21:44 +08:00
|
|
|
self.assertEqual(response.headers["Location"], list_uri)
|
2023-02-07 00:07:23 +08:00
|
|
|
|
|
|
|
with self.app.app_context():
|
|
|
|
self.assertEqual({x.code for x in Currency.query.all()},
|
2023-03-22 00:37:39 +08:00
|
|
|
{USD.code, EUR.code})
|
2023-02-07 00:07:23 +08:00
|
|
|
|
2023-02-07 08:21:44 +08:00
|
|
|
response = self.client.get(detail_uri)
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 404)
|
2023-02-07 08:21:44 +08:00
|
|
|
response = self.client.post(delete_uri,
|
|
|
|
data={"csrf_token": self.csrf_token})
|
2023-02-07 00:07:23 +08:00
|
|
|
self.assertEqual(response.status_code, 404)
|