Revised the AccountTestCase test case for simplicity.

This commit is contained in:
依瑪貓 2023-02-02 00:16:57 +08:00
parent 8364025668
commit d9624c7be6

View File

@ -109,24 +109,21 @@ class AccountTestCase(unittest.TestCase):
Account.query.delete()
db.session.commit()
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")
client: httpx.Client = self.editor.client
csrf_token: str = self.editor.csrf_token
editor: UserClient = get_user_client(self, self.app, "editor")
self.client: httpx.Client = editor.client
self.csrf_token: str = editor.csrf_token
response: httpx.Response
response = client.post("/accounting/accounts/store",
data={"csrf_token": csrf_token,
response = self.client.post("/accounting/accounts/store",
data={"csrf_token": self.csrf_token,
"base_code": "1111",
"title": "1111 title"})
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"],
"/accounting/accounts/1111-001")
response = client.post("/accounting/accounts/store",
data={"csrf_token": csrf_token,
response = self.client.post("/accounting/accounts/store",
data={"csrf_token": self.csrf_token,
"base_code": "1112",
"title": "1112 title"})
self.assertEqual(response.status_code, 302)
@ -139,35 +136,34 @@ class AccountTestCase(unittest.TestCase):
:return: None.
"""
response: httpx.Response
client: httpx.Client = self.nobody.client
csrf_token: str = self.nobody.csrf_token
nobody: UserClient = get_user_client(self, self.app, "nobody")
response = client.get("/accounting/accounts")
response = nobody.client.get("/accounting/accounts")
self.assertEqual(response.status_code, 403)
response = client.get("/accounting/accounts/1111-001")
response = nobody.client.get("/accounting/accounts/1111-001")
self.assertEqual(response.status_code, 403)
response = client.get("/accounting/accounts/create")
response = nobody.client.get("/accounting/accounts/create")
self.assertEqual(response.status_code, 403)
response = client.post("/accounting/accounts/store",
data={"csrf_token": csrf_token,
response = nobody.client.post("/accounting/accounts/store",
data={"csrf_token": nobody.csrf_token,
"base_code": "1113",
"title": "1113 title"})
self.assertEqual(response.status_code, 403)
response = client.get("/accounting/accounts/1111-001/edit")
response = nobody.client.get("/accounting/accounts/1111-001/edit")
self.assertEqual(response.status_code, 403)
response = client.post("/accounting/accounts/1111-001/update",
data={"csrf_token": csrf_token,
response = nobody.client.post("/accounting/accounts/1111-001/update",
data={"csrf_token": nobody.csrf_token,
"base_code": "1111",
"title": "1111 title #2"})
self.assertEqual(response.status_code, 403)
response = client.post("/accounting/accounts/1111-001/delete",
data={"csrf_token": csrf_token})
response = nobody.client.post("/accounting/accounts/1111-001/delete",
data={"csrf_token": nobody.csrf_token})
self.assertEqual(response.status_code, 403)
def test_viewer(self) -> None:
@ -176,33 +172,32 @@ class AccountTestCase(unittest.TestCase):
:return: None.
"""
response: httpx.Response
client: httpx.Client = self.viewer.client
csrf_token: str = self.viewer.csrf_token
viewer: UserClient = get_user_client(self, self.app, "viewer")
response = client.get("/accounting/accounts")
response = viewer.client.get("/accounting/accounts")
self.assertEqual(response.status_code, 200)
response = client.get("/accounting/accounts/1111-001")
response = viewer.client.get("/accounting/accounts/1111-001")
self.assertEqual(response.status_code, 200)
response = client.get("/accounting/accounts/create")
response = viewer.client.get("/accounting/accounts/create")
self.assertEqual(response.status_code, 403)
response = client.post("/accounting/accounts/store",
data={"csrf_token": csrf_token,
response = viewer.client.post("/accounting/accounts/store",
data={"csrf_token": viewer.csrf_token,
"base_code": "1113",
"title": "1113 title"})
self.assertEqual(response.status_code, 403)
response = client.get("/accounting/accounts/1111-001/edit")
response = viewer.client.get("/accounting/accounts/1111-001/edit")
self.assertEqual(response.status_code, 403)
response = client.post("/accounting/accounts/1111-001/update",
data={"csrf_token": csrf_token,
response = viewer.client.post("/accounting/accounts/1111-001/update",
data={"csrf_token": viewer.csrf_token,
"base_code": "1111",
"title": "1111 title #2"})
self.assertEqual(response.status_code, 403)
response = client.post("/accounting/accounts/1111-001/delete",
data={"csrf_token": csrf_token})
response = viewer.client.post("/accounting/accounts/1111-001/delete",
data={"csrf_token": viewer.csrf_token})
self.assertEqual(response.status_code, 403)