feat(FastAPI): add Requests navigation item

Along with this, created a new test suite at test/test_html.py,
which has the responsibility of testing various HTML things
that are not suitable for another test suite.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-09-01 13:55:41 -07:00
parent 4d191b51f9
commit c164abe256
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
2 changed files with 104 additions and 0 deletions

View file

@ -7,6 +7,11 @@
{% endif %}
<li><a href="/packages/">{% trans %}Packages{% endtrans %}</a></li>
{% if request.user.is_authenticated() %}
<li>
<a href="/requests/">
{% trans %}Requests{% endtrans %}
</a>
</li>
{% if request.user.is_trusted_user() or request.user.is_developer() %}
<li>
<a href="/accounts/">

99
test/test_html.py Normal file
View file

@ -0,0 +1,99 @@
""" 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 import setup_test_db
from aurweb.testing.html import parse_root
from aurweb.testing.requests import Request
@pytest.fixture(autouse=True)
def setup():
setup_test_db(User.__tablename__)
@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]