test: add tests to check various 404 paths and 503

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2022-01-03 17:35:31 -08:00
parent e126d431d7
commit 6c6eb2c21b
No known key found for this signature in database
GPG key ID: F7E46DED420788F3

View file

@ -1,12 +1,15 @@
""" A test suite used to test HTML renders in different cases. """ """ A test suite used to test HTML renders in different cases. """
from http import HTTPStatus from http import HTTPStatus
import fastapi
import pytest import pytest
from fastapi import HTTPException
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from aurweb import asgi, db from aurweb import asgi, db
from aurweb.models.account_type import TRUSTED_USER_ID, USER_ID, AccountType from aurweb.models import PackageBase
from aurweb.models.account_type import TRUSTED_USER_ID, USER_ID
from aurweb.models.user import User from aurweb.models.user import User
from aurweb.testing.html import get_errors, get_successes, parse_root from aurweb.testing.html import get_errors, get_successes, parse_root
from aurweb.testing.requests import Request from aurweb.testing.requests import Request
@ -24,22 +27,26 @@ def client() -> TestClient:
@pytest.fixture @pytest.fixture
def user() -> User: def user() -> User:
user_type = db.query(AccountType, AccountType.ID == USER_ID).first()
with db.begin(): with db.begin():
user = db.create(User, Username="test", Email="test@example.org", user = db.create(User, Username="test", Email="test@example.org",
Passwd="testPassword", AccountType=user_type) Passwd="testPassword", AccountTypeID=USER_ID)
yield user yield user
@pytest.fixture @pytest.fixture
def trusted_user(user: User) -> User: def trusted_user(user: User) -> User:
tu_type = db.query(AccountType,
AccountType.ID == TRUSTED_USER_ID).first()
with db.begin(): with db.begin():
user.AccountType = tu_type user.AccountTypeID = TRUSTED_USER_ID
yield user yield user
@pytest.fixture
def pkgbase(user: User) -> PackageBase:
with db.begin():
pkgbase = db.create(PackageBase, Name="test-pkg", Maintainer=user)
yield pkgbase
def test_archdev_navbar(client: TestClient): def test_archdev_navbar(client: TestClient):
expected = [ expected = [
"AUR Home", "AUR Home",
@ -123,3 +130,38 @@ def test_metrics(client: TestClient):
resp = request.get("/metrics") resp = request.get("/metrics")
assert resp.status_code == int(HTTPStatus.OK) assert resp.status_code == int(HTTPStatus.OK)
assert resp.headers.get("Content-Type").startswith("text/plain") assert resp.headers.get("Content-Type").startswith("text/plain")
def test_404_with_valid_pkgbase(client: TestClient, pkgbase: PackageBase):
""" Test HTTPException with status_code == 404 and valid pkgbase. """
endpoint = f"/{pkgbase.Name}"
with client as request:
response = request.get(endpoint)
assert response.status_code == int(HTTPStatus.NOT_FOUND)
body = response.text
assert "404 - Page Not Found" in body
assert "To clone the Git repository" in body
def test_404(client: TestClient):
""" Test HTTPException with status_code == 404 without a valid pkgbase. """
with client as request:
response = request.get("/nonexistentroute")
assert response.status_code == int(HTTPStatus.NOT_FOUND)
body = response.text
assert "404 - Page Not Found" in body
# No `pkgbase` is provided here; we don't see the extra info.
assert "To clone the Git repository" not in body
def test_503(client: TestClient):
""" Test HTTPException with status_code == 503 (Service Unavailable). """
@asgi.app.get("/raise-503")
async def raise_503(request: fastapi.Request):
raise HTTPException(status_code=HTTPStatus.SERVICE_UNAVAILABLE)
with TestClient(app=asgi.app) as request:
response = request.get("/raise-503")
assert response.status_code == int(HTTPStatus.SERVICE_UNAVAILABLE)