From 6c6eb2c21b98805b6558c10f443a20e994c29119 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Mon, 3 Jan 2022 17:35:31 -0800 Subject: [PATCH] test: add tests to check various 404 paths and 503 Signed-off-by: Kevin Morris --- test/test_html.py | 54 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/test/test_html.py b/test/test_html.py index db47c5e5..5e9505dc 100644 --- a/test/test_html.py +++ b/test/test_html.py @@ -1,12 +1,15 @@ """ A test suite used to test HTML renders in different cases. """ from http import HTTPStatus +import fastapi import pytest +from fastapi import HTTPException 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 import PackageBase +from aurweb.models.account_type import TRUSTED_USER_ID, USER_ID from aurweb.models.user import User from aurweb.testing.html import get_errors, get_successes, parse_root from aurweb.testing.requests import Request @@ -24,22 +27,26 @@ def client() -> TestClient: @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) + Passwd="testPassword", AccountTypeID=USER_ID) 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 + user.AccountTypeID = TRUSTED_USER_ID 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): expected = [ "AUR Home", @@ -123,3 +130,38 @@ def test_metrics(client: TestClient): resp = request.get("/metrics") assert resp.status_code == int(HTTPStatus.OK) 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)