2023-01-29 22:28:27 +08:00
|
|
|
# The Mia! Accounting Flask Project.
|
|
|
|
# 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-02-07 00:07:23 +08:00
|
|
|
import typing as t
|
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
|
|
|
|
|
|
|
|
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-02-01 23:59:42 +08:00
|
|
|
|
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()}}")
|
|
|
|
|
|
|
|
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-02-13 19:18:15 +08:00
|
|
|
def get_client(app: Flask, username: str) -> tuple[httpx.Client, str]:
|
2023-02-01 23:59:42 +08:00
|
|
|
"""Returns a user client.
|
|
|
|
|
|
|
|
:param app: The Flask application.
|
|
|
|
:param username: The username.
|
2023-02-06 21:45:28 +08:00
|
|
|
:return: A tuple of the client and the CSRF token.
|
2023-02-01 23:59:42 +08:00
|
|
|
"""
|
2023-02-27 16:25:36 +08:00
|
|
|
client: httpx.Client = httpx.Client(app=app, base_url=TEST_SERVER)
|
|
|
|
client.headers["Referer"] = TEST_SERVER
|
2023-03-14 21:28:35 +08:00
|
|
|
csrf_token: str = get_csrf_token(client)
|
2023-02-01 23:59:42 +08:00
|
|
|
response: httpx.Response = client.post("/login",
|
|
|
|
data={"csrf_token": csrf_token,
|
|
|
|
"username": username})
|
2023-02-13 19:18:15 +08:00
|
|
|
assert response.status_code == 302
|
|
|
|
assert response.headers["Location"] == "/"
|
2023-02-06 21:45:28 +08:00
|
|
|
return client, csrf_token
|
2023-01-29 22:28:27 +08:00
|
|
|
|
|
|
|
|
2023-02-13 19:18:15 +08:00
|
|
|
def set_locale(client: httpx.Client, csrf_token: str,
|
2023-02-07 00:07:23 +08:00
|
|
|
locale: t.Literal["en", "zh_Hant", "zh_Hans"]) -> None:
|
|
|
|
"""Sets the current locale.
|
|
|
|
|
|
|
|
:param client: The test client.
|
|
|
|
:param csrf_token: The CSRF token.
|
|
|
|
:param locale: The locale.
|
|
|
|
:return: None.
|
|
|
|
"""
|
|
|
|
response: httpx.Response = client.post("/locale",
|
|
|
|
data={"csrf_token": csrf_token,
|
|
|
|
"locale": locale,
|
|
|
|
"next": "/next"})
|
2023-02-13 19:18:15 +08:00
|
|
|
assert response.status_code == 302
|
|
|
|
assert response.headers["Location"] == "/next"
|