Split the BaseAccountTestCase into BaseAccountCommandTestCase and BaseAccountTestCase, and rewrote the BaseAccountTestCase for simplicity.
This commit is contained in:
parent
dd3690dd6a
commit
8364025668
@ -25,12 +25,12 @@ from click.testing import Result
|
|||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask.testing import FlaskCliRunner
|
from flask.testing import FlaskCliRunner
|
||||||
|
|
||||||
from testlib import get_csrf_token
|
from testlib import UserClient, get_user_client
|
||||||
from testsite import create_app
|
from testsite import create_app
|
||||||
|
|
||||||
|
|
||||||
class BaseAccountTestCase(unittest.TestCase):
|
class BaseAccountCommandTestCase(unittest.TestCase):
|
||||||
"""The base account test case."""
|
"""The base account console command test case."""
|
||||||
|
|
||||||
def setUp(self) -> None:
|
def setUp(self) -> None:
|
||||||
"""Sets up the test.
|
"""Sets up the test.
|
||||||
@ -38,16 +38,15 @@ class BaseAccountTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
:return: None.
|
:return: None.
|
||||||
"""
|
"""
|
||||||
|
from accounting.models import BaseAccount, BaseAccountL10n
|
||||||
self.app: Flask = create_app(is_testing=True)
|
self.app: Flask = create_app(is_testing=True)
|
||||||
|
|
||||||
runner: FlaskCliRunner = self.app.test_cli_runner()
|
runner: FlaskCliRunner = self.app.test_cli_runner()
|
||||||
with self.app.app_context():
|
with self.app.app_context():
|
||||||
result: Result = runner.invoke(args="init-db")
|
result: Result = runner.invoke(args="init-db")
|
||||||
self.assertEqual(result.exit_code, 0)
|
self.assertEqual(result.exit_code, 0)
|
||||||
self.client: httpx.Client = httpx.Client(app=self.app,
|
BaseAccountL10n.query.delete()
|
||||||
base_url="https://testserver")
|
BaseAccount.query.delete()
|
||||||
self.client.headers["Referer"] = "https://testserver"
|
|
||||||
self.csrf_token: str = get_csrf_token(self, self.client, "/login")
|
|
||||||
|
|
||||||
def test_init(self) -> None:
|
def test_init(self) -> None:
|
||||||
"""Tests the "accounting-init-base" console command.
|
"""Tests the "accounting-init-base" console command.
|
||||||
@ -68,46 +67,69 @@ class BaseAccountTestCase(unittest.TestCase):
|
|||||||
self.assertIn(f"{account.code}-zh_Hant", l10n_keys)
|
self.assertIn(f"{account.code}-zh_Hant", l10n_keys)
|
||||||
self.assertIn(f"{account.code}-zh_Hant", l10n_keys)
|
self.assertIn(f"{account.code}-zh_Hant", l10n_keys)
|
||||||
|
|
||||||
list_uri: str = "/accounting/base-accounts"
|
|
||||||
|
class BaseAccountTestCase(unittest.TestCase):
|
||||||
|
"""The base account test case."""
|
||||||
|
|
||||||
|
def setUp(self) -> None:
|
||||||
|
"""Sets up the test.
|
||||||
|
This is run once per test.
|
||||||
|
|
||||||
|
:return: None.
|
||||||
|
"""
|
||||||
|
from accounting.models import BaseAccount
|
||||||
|
self.app: Flask = create_app(is_testing=True)
|
||||||
|
|
||||||
|
runner: FlaskCliRunner = self.app.test_cli_runner()
|
||||||
|
with self.app.app_context():
|
||||||
|
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)
|
||||||
|
|
||||||
|
self.viewer: UserClient = get_user_client(self, self.app, "viewer")
|
||||||
|
self.editor: UserClient = get_user_client(self, self.app, "editor")
|
||||||
|
self.nobody: UserClient = get_user_client(self, self.app, "nobody")
|
||||||
|
|
||||||
|
def test_nobody(self) -> None:
|
||||||
|
"""Test the permission as nobody.
|
||||||
|
|
||||||
|
:return: None.
|
||||||
|
"""
|
||||||
response: httpx.Response
|
response: httpx.Response
|
||||||
|
nobody: UserClient = get_user_client(self, self.app, "nobody")
|
||||||
|
|
||||||
self.__logout()
|
response = nobody.client.get("/accounting/base-accounts")
|
||||||
response = self.client.get(list_uri)
|
|
||||||
self.assertEqual(response.status_code, 403)
|
self.assertEqual(response.status_code, 403)
|
||||||
|
|
||||||
self.__logout()
|
response = nobody.client.get("/accounting/base-accounts/1111")
|
||||||
self.__login_as("viewer")
|
|
||||||
response = self.client.get(list_uri)
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
self.__logout()
|
|
||||||
self.__login_as("editor")
|
|
||||||
response = self.client.get(list_uri)
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
self.__logout()
|
|
||||||
self.__login_as("nobody")
|
|
||||||
response = self.client.get(list_uri)
|
|
||||||
self.assertEqual(response.status_code, 403)
|
self.assertEqual(response.status_code, 403)
|
||||||
|
|
||||||
def __logout(self) -> None:
|
def test_viewer(self) -> None:
|
||||||
"""Logs out the currently logged-in user.
|
"""Test the permission as viewer.
|
||||||
|
|
||||||
:return: None.
|
:return: None.
|
||||||
"""
|
"""
|
||||||
response: httpx.Response = self.client.post(
|
response: httpx.Response
|
||||||
"/logout", data={"csrf_token": self.csrf_token})
|
viewer: UserClient = get_user_client(self, self.app, "viewer")
|
||||||
self.assertEqual(response.status_code, 302)
|
|
||||||
self.assertEqual(response.headers["Location"], "/")
|
|
||||||
|
|
||||||
def __login_as(self, username: str) -> None:
|
response = viewer.client.get("/accounting/base-accounts")
|
||||||
"""Logs in as a specific user.
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
response = viewer.client.get("/accounting/base-accounts/1111")
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
def test_editor(self) -> None:
|
||||||
|
"""Test the permission as editor.
|
||||||
|
|
||||||
:param username: The username.
|
|
||||||
:return: None.
|
:return: None.
|
||||||
"""
|
"""
|
||||||
response: httpx.Response = self.client.post(
|
response: httpx.Response
|
||||||
"/login", data={"csrf_token": self.csrf_token,
|
editor: UserClient = get_user_client(self, self.app, "editor")
|
||||||
"username": username})
|
|
||||||
self.assertEqual(response.status_code, 302)
|
response = editor.client.get("/accounting/base-accounts")
|
||||||
self.assertEqual(response.headers["Location"], "/")
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
response = editor.client.get("/accounting/base-accounts/1111")
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
Loading…
Reference in New Issue
Block a user