From 1d5827007f205c8972d07eee138372e8f9303684 Mon Sep 17 00:00:00 2001 From: Marcus Andersson <marcus@tojoma.se> Date: Thu, 13 May 2021 22:02:50 +0200 Subject: [PATCH] Adding route tests Removing status code from 404 title Removing status code from 503 title Adding id to 503 error box Indatation fix --- aurweb/routers/errors.py | 11 ++++------- aurweb/routers/html.py | 8 +++++++- templates/errors/404.html | 4 ++-- templates/errors/503.html | 2 +- test/test_routes.py | 7 +++++++ 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/aurweb/routers/errors.py b/aurweb/routers/errors.py index 3bdaeb9da..111d802ad 100644 --- a/aurweb/routers/errors.py +++ b/aurweb/routers/errors.py @@ -1,17 +1,14 @@ from aurweb.templates import make_context, render_template -from aurweb import l10n async def not_found(request, exc): - _ = l10n.get_translator_for_request(request) - context = make_context(request, f"404 - {_('Page Not Found')}") - return render_template("errors/404.html", context) + context = make_context(request, "Page Not Found") + return render_template("errors/404.html", context, 404) async def service_unavailable(request, exc): - _ = l10n.get_translator_for_request(request) - context = make_context(request, "503 - {_('Service Unavailable')}") - return render_template("errors/503.html", context) + context = make_context(request, "Service Unavailable") + return render_template("errors/503.html", context, 503) # Maps HTTP errors to functions exceptions = { diff --git a/aurweb/routers/html.py b/aurweb/routers/html.py index ae08c764b..50b62450b 100644 --- a/aurweb/routers/html.py +++ b/aurweb/routers/html.py @@ -4,7 +4,7 @@ own modules and imported here. """ from http import HTTPStatus from urllib.parse import unquote -from fastapi import APIRouter, Form, Request +from fastapi import APIRouter, Form, Request, HTTPException from fastapi.responses import HTMLResponse, RedirectResponse from aurweb.templates import make_context, render_template @@ -48,3 +48,9 @@ async def index(request: Request): """ Homepage route. """ context = make_context(request, "Home") return render_template("index.html", context) + + +# A route that returns a error 503. For testing purposes. +@router.get("/raisefivethree", response_class=HTMLResponse) +async def raise_service_unavailable(request: Request): + raise HTTPException(status_code=503) diff --git a/templates/errors/404.html b/templates/errors/404.html index 0afdd2fa8..4926aff6e 100644 --- a/templates/errors/404.html +++ b/templates/errors/404.html @@ -1,8 +1,8 @@ {% extends 'partials/layout.html' %} {% block pageContent %} - <div class="box 404"> + <div id="error-page" class="box 404"> <h2>404 - {% trans %}Page Not Found{% endtrans %}</h2> <p>{% trans %}Sorry, the page you've requested does not exist.{% endtrans %}</p> </div> - {% endblock %} +{% endblock %} diff --git a/templates/errors/503.html b/templates/errors/503.html index d31666a1f..9a0ed56ae 100644 --- a/templates/errors/503.html +++ b/templates/errors/503.html @@ -1,7 +1,7 @@ {% extends 'partials/layout.html' %} {% block pageContent %} - <div class="box 404"> + <div id="error-page" class="box 503"> <h2>503 - {% trans %}Service Unavailable{% endtrans %}</h2> <p>{% trans %}Don't panic! This site is down due to maintenance. We will be back soon.{% endtrans %}</p> </div> diff --git a/test/test_routes.py b/test/test_routes.py index 46ba39f50..862211087 100644 --- a/test/test_routes.py +++ b/test/test_routes.py @@ -67,3 +67,10 @@ def test_language_query_params(): response = req.post("/language", data=post_data) assert response.headers.get("location") == f"/?{q}" assert response.status_code == int(HTTPStatus.SEE_OTHER) + + +def test_error_messages(): + response1 = client.get("/thisroutedoesnotexist") + response2 = client.get("/raisefivethree") + assert response1.status_code == int(HTTPStatus.NOT_FOUND) + assert response2.status_code == int(HTTPStatus.SERVICE_UNAVAILABLE) \ No newline at end of file -- GitLab