aurweb/test/test_html.py
Kevin Morris fa43f6bc3e
change(aurweb): add parallel tests and improve aurweb.db
This change utilizes pytest-xdist to perform a multiproc test
run and reworks aurweb.db's code. We no longer use a global
engine, session or Session, but we now use a memo of engines
and sessions as they are requested, based on the PYTEST_CURRENT_TEST
environment variable, which is available during testing.

Additionally, this change strips several SQLite components
out of the Python code-base.

SQLite is still compatible with PHP and sharness tests, but
not with our FastAPI implementation.

More changes:
------------
- Remove use of aurweb.db.session global in other code.
- Use new aurweb.db.name() dynamic db name function in env.py.
- Added 'addopts' to pytest.ini which utilizes multiprocessing.
    - Highly recommended to leave this be or modify `-n auto` to
      `-n {cpu_threads}` where cpu_threads is at least 2.

Signed-off-by: Kevin Morris <kevr@0cost.org>
2021-11-17 01:34:59 -08:00

125 lines
3.3 KiB
Python

""" A test suite used to test HTML renders in different cases. """
from http import HTTPStatus
import pytest
from fastapi.testclient import TestClient
from aurweb import asgi, db
from aurweb.models.account_type import TRUSTED_USER_ID, USER_ID, AccountType
from aurweb.models.user import User
from aurweb.testing.html import get_errors, get_successes, parse_root
from aurweb.testing.requests import Request
@pytest.fixture(autouse=True)
def setup(db_test):
return
@pytest.fixture
def client() -> TestClient:
yield TestClient(app=asgi.app)
@pytest.fixture
def user() -> User:
user_type = db.query(AccountType, AccountType.ID == USER_ID).first()
with db.begin():
user = db.create(User, Username="test", Email="test@example.org",
Passwd="testPassword", AccountType=user_type)
yield user
@pytest.fixture
def trusted_user(user: User) -> User:
tu_type = db.query(AccountType,
AccountType.ID == TRUSTED_USER_ID).first()
with db.begin():
user.AccountType = tu_type
yield user
def test_archdev_navbar(client: TestClient):
expected = [
"AUR Home",
"Packages",
"Register",
"Login"
]
with client as request:
resp = request.get("/")
assert resp.status_code == int(HTTPStatus.OK)
root = parse_root(resp.text)
items = root.xpath('//div[@id="archdev-navbar"]/ul/li/a')
for i, item in enumerate(items):
assert item.text.strip() == expected[i]
def test_archdev_navbar_authenticated(client: TestClient, user: User):
expected = [
"Dashboard",
"Packages",
"Requests",
"My Account",
"Logout"
]
cookies = {"AURSID": user.login(Request(), "testPassword")}
with client as request:
resp = request.get("/", cookies=cookies)
assert resp.status_code == int(HTTPStatus.OK)
root = parse_root(resp.text)
items = root.xpath('//div[@id="archdev-navbar"]/ul/li/a')
for i, item in enumerate(items):
assert item.text.strip() == expected[i]
def test_archdev_navbar_authenticated_tu(client: TestClient,
trusted_user: User):
expected = [
"Dashboard",
"Packages",
"Requests",
"Accounts",
"My Account",
"Trusted User",
"Logout"
]
cookies = {"AURSID": trusted_user.login(Request(), "testPassword")}
with client as request:
resp = request.get("/", cookies=cookies)
assert resp.status_code == int(HTTPStatus.OK)
root = parse_root(resp.text)
items = root.xpath('//div[@id="archdev-navbar"]/ul/li/a')
for i, item in enumerate(items):
assert item.text.strip() == expected[i]
def test_get_errors():
html = """
<ul class="errorlist">
<li>Test</li>
</ul>
"""
errors = get_errors(html)
assert errors[0].text.strip() == "Test"
def test_get_successes():
html = """
<ul class="success">
<li>Test</li>
</ul>
"""
successes = get_successes(html)
assert successes[0].text.strip() == "Test"
def test_metrics(client: TestClient):
with client as request:
resp = request.get("/metrics")
assert resp.status_code == int(HTTPStatus.OK)
assert resp.headers.get("Content-Type").startswith("text/plain")