2023-04-04 18:17:44 +08:00
|
|
|
# The Mia! Accounting Project.
|
2023-01-29 22:28:27 +08:00
|
|
|
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/1/27
|
|
|
|
|
|
|
|
# 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 common test libraries.
|
|
|
|
|
|
|
|
"""
|
2023-04-09 11:38:22 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-04-09 11:35:28 +08:00
|
|
|
import re
|
2023-04-26 18:22:45 +08:00
|
|
|
from typing import Literal
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
import httpx
|
2023-03-14 21:28:35 +08:00
|
|
|
from flask import Flask, render_template_string
|
|
|
|
|
2023-05-23 08:24:12 +08:00
|
|
|
from accounting.utils.next_uri import encode_next
|
2023-04-13 08:30:07 +08:00
|
|
|
from test_site import create_app
|
2023-02-01 23:59:42 +08:00
|
|
|
|
2023-02-27 16:25:36 +08:00
|
|
|
TEST_SERVER: str = "https://testserver"
|
|
|
|
"""The test server URI."""
|
2023-03-23 17:22:57 +08:00
|
|
|
NEXT_URI: str = "/_next"
|
|
|
|
"""The next URI."""
|
2023-02-27 16:25:36 +08:00
|
|
|
|
2023-02-01 23:59:42 +08:00
|
|
|
|
2023-03-23 17:26:27 +08:00
|
|
|
class Accounts:
|
|
|
|
"""The shortcuts to the common accounts."""
|
|
|
|
CASH: str = "1111-001"
|
|
|
|
PETTY_CASH: str = "1112-001"
|
|
|
|
BANK: str = "1113-001"
|
|
|
|
NOTES_RECEIVABLE: str = "1131-001"
|
|
|
|
RECEIVABLE: str = "1141-001"
|
2023-04-12 17:43:05 +08:00
|
|
|
MACHINERY: str = "1441-001"
|
2023-03-23 17:26:27 +08:00
|
|
|
PREPAID: str = "1258-001"
|
|
|
|
NOTES_PAYABLE: str = "2131-001"
|
|
|
|
PAYABLE: str = "2141-001"
|
|
|
|
SALES: str = "4111-001"
|
|
|
|
SERVICE: str = "4611-001"
|
|
|
|
AGENCY: str = "4711-001"
|
|
|
|
RENT_EXPENSE: str = "6252-001"
|
|
|
|
OFFICE: str = "6253-001"
|
|
|
|
TRAVEL: str = "6254-001"
|
|
|
|
POSTAGE: str = "6256-001"
|
|
|
|
UTILITIES: str = "6261-001"
|
|
|
|
INSURANCE: str = "6262-001"
|
|
|
|
MEAL: str = "6272-001"
|
|
|
|
INTEREST: str = "7111-001"
|
|
|
|
DONATION: str = "7481-001"
|
|
|
|
RENT_INCOME: str = "7482-001"
|
|
|
|
|
|
|
|
|
2023-03-14 21:28:35 +08:00
|
|
|
def create_test_app() -> Flask:
|
|
|
|
"""Creates and returns the testing Flask application.
|
|
|
|
|
|
|
|
:return: The testing Flask application.
|
|
|
|
"""
|
|
|
|
app: Flask = create_app(is_testing=True)
|
|
|
|
|
|
|
|
@app.get("/.csrf-token")
|
|
|
|
def get_csrf_token_view() -> str:
|
|
|
|
"""The test view to return the CSRF token."""
|
|
|
|
return render_template_string("{{csrf_token()}}")
|
|
|
|
|
2023-05-22 00:03:13 +08:00
|
|
|
@app.get("/.messages")
|
|
|
|
def get_messages_view() -> str:
|
|
|
|
"""The test view to return the flashed messages."""
|
2023-03-18 02:59:28 +08:00
|
|
|
return render_template_string("{{get_flashed_messages()|tojson}}")
|
|
|
|
|
2023-03-14 21:28:35 +08:00
|
|
|
return app
|
|
|
|
|
|
|
|
|
|
|
|
def get_csrf_token(client: httpx.Client) -> str:
|
|
|
|
"""Returns the CSRF token.
|
|
|
|
|
|
|
|
:param client: The httpx client.
|
|
|
|
:return: The CSRF token.
|
|
|
|
"""
|
|
|
|
return client.get("/.csrf-token").text
|
|
|
|
|
|
|
|
|
2023-06-10 09:35:51 +08:00
|
|
|
def get_client(app: Flask, username: str) -> httpx.Client:
|
2023-02-01 23:59:42 +08:00
|
|
|
"""Returns a user client.
|
|
|
|
|
|
|
|
:param app: The Flask application.
|
|
|
|
:param username: The username.
|
2023-06-10 09:35:51 +08:00
|
|
|
:return: The user client.
|
2023-02-01 23:59:42 +08:00
|
|
|
"""
|
2024-12-02 08:09:17 +08:00
|
|
|
client: httpx.Client = httpx.Client(
|
|
|
|
transport=httpx.WSGITransport(app=app),
|
|
|
|
base_url=TEST_SERVER)
|
2023-02-27 16:25:36 +08:00
|
|
|
client.headers["Referer"] = TEST_SERVER
|
2023-03-14 21:28:35 +08:00
|
|
|
csrf_token: str = get_csrf_token(client)
|
2023-05-23 08:24:12 +08:00
|
|
|
with app.app_context():
|
|
|
|
encoded_next_uri: str = encode_next(NEXT_URI)
|
2023-02-01 23:59:42 +08:00
|
|
|
response: httpx.Response = client.post("/login",
|
|
|
|
data={"csrf_token": csrf_token,
|
2023-05-23 08:24:12 +08:00
|
|
|
"next": encoded_next_uri,
|
2023-02-01 23:59:42 +08:00
|
|
|
"username": username})
|
2023-02-13 19:18:15 +08:00
|
|
|
assert response.status_code == 302
|
2023-05-23 08:24:12 +08:00
|
|
|
assert response.headers["Location"] == NEXT_URI
|
2023-06-10 09:35:51 +08:00
|
|
|
return client
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
|
2023-05-23 08:24:12 +08:00
|
|
|
def set_locale(app: Flask, client: httpx.Client, csrf_token: str,
|
2023-04-26 18:22:45 +08:00
|
|
|
locale: Literal["en", "zh_Hant", "zh_Hans"]) -> None:
|
2023-02-07 00:07:23 +08:00
|
|
|
"""Sets the current locale.
|
|
|
|
|
2023-05-23 08:24:12 +08:00
|
|
|
:param app: The Flask application.
|
2023-02-07 00:07:23 +08:00
|
|
|
:param client: The test client.
|
|
|
|
:param csrf_token: The CSRF token.
|
|
|
|
:param locale: The locale.
|
|
|
|
:return: None.
|
|
|
|
"""
|
2023-05-23 08:24:12 +08:00
|
|
|
with app.app_context():
|
|
|
|
encoded_next_uri: str = encode_next(NEXT_URI)
|
2023-02-07 00:07:23 +08:00
|
|
|
response: httpx.Response = client.post("/locale",
|
|
|
|
data={"csrf_token": csrf_token,
|
|
|
|
"locale": locale,
|
2023-05-23 08:24:12 +08:00
|
|
|
"next": encoded_next_uri})
|
2023-02-13 19:18:15 +08:00
|
|
|
assert response.status_code == 302
|
2023-05-23 08:24:12 +08:00
|
|
|
assert response.headers["Location"] == NEXT_URI
|
2023-04-09 11:35:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
def add_journal_entry(client: httpx.Client, form: dict[str, str]) -> int:
|
|
|
|
"""Adds a transfer journal entry.
|
|
|
|
|
|
|
|
:param client: The client.
|
|
|
|
:param form: The form data.
|
|
|
|
:return: The newly-added journal entry ID.
|
|
|
|
"""
|
|
|
|
prefix: str = "/accounting/journal-entries"
|
|
|
|
journal_entry_type: str = "transfer"
|
|
|
|
if len({x for x in form if "-debit-" in x}) == 0:
|
|
|
|
journal_entry_type = "receipt"
|
|
|
|
elif len({x for x in form if "-credit-" in x}) == 0:
|
|
|
|
journal_entry_type = "disbursement"
|
|
|
|
store_uri = f"{prefix}/store/{journal_entry_type}"
|
|
|
|
response: httpx.Response = client.post(store_uri, data=form)
|
|
|
|
assert response.status_code == 302
|
|
|
|
return match_journal_entry_detail(response.headers["Location"])
|
|
|
|
|
|
|
|
|
|
|
|
def match_journal_entry_detail(location: str) -> int:
|
|
|
|
"""Validates if the redirect location is the journal entry detail, and
|
|
|
|
returns the journal entry ID on success.
|
|
|
|
|
|
|
|
:param location: The redirect location.
|
|
|
|
:return: The journal entry ID.
|
|
|
|
:raise AssertionError: When the location is not the journal entry detail.
|
|
|
|
"""
|
|
|
|
m: re.Match = re.match(
|
2023-05-23 08:24:12 +08:00
|
|
|
r"^/accounting/journal-entries/(\d+)\?next=", location)
|
2023-04-09 11:35:28 +08:00
|
|
|
assert m is not None
|
|
|
|
return int(m.group(1))
|