From 69cbbfce657977d0c9d4854ef13d366157326e64 Mon Sep 17 00:00:00 2001 From: Evgenii Alekseev Date: Mon, 6 Nov 2023 14:53:55 +0200 Subject: [PATCH] feat: serve favicon in root --- docs/ahriman.web.views.rst | 8 ++++ package/share/ahriman/templates/api.jinja2 | 6 +-- .../ahriman/templates/build-status.jinja2 | 2 - package/share/ahriman/templates/error.jinja2 | 2 - src/ahriman/web/views/static.py | 44 +++++++++++++++++++ tests/ahriman/web/views/test_view_static.py | 31 +++++++++++++ 6 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 src/ahriman/web/views/static.py create mode 100644 tests/ahriman/web/views/test_view_static.py diff --git a/docs/ahriman.web.views.rst b/docs/ahriman.web.views.rst index abe48061..0a44ca66 100644 --- a/docs/ahriman.web.views.rst +++ b/docs/ahriman.web.views.rst @@ -30,6 +30,14 @@ ahriman.web.views.index module :no-undoc-members: :show-inheritance: +ahriman.web.views.static module +------------------------------- + +.. automodule:: ahriman.web.views.static + :members: + :no-undoc-members: + :show-inheritance: + Module contents --------------- diff --git a/package/share/ahriman/templates/api.jinja2 b/package/share/ahriman/templates/api.jinja2 index 8fcff59e..993b0d24 100644 --- a/package/share/ahriman/templates/api.jinja2 +++ b/package/share/ahriman/templates/api.jinja2 @@ -6,10 +6,8 @@ - - - - + + diff --git a/package/share/ahriman/templates/build-status.jinja2 b/package/share/ahriman/templates/build-status.jinja2 index edd1076a..a99b437b 100644 --- a/package/share/ahriman/templates/build-status.jinja2 +++ b/package/share/ahriman/templates/build-status.jinja2 @@ -5,8 +5,6 @@ - - {% include "utils/style.jinja2" %} {% include "user-style.jinja2" ignore missing %} diff --git a/package/share/ahriman/templates/error.jinja2 b/package/share/ahriman/templates/error.jinja2 index f18f5257..19e52c1e 100644 --- a/package/share/ahriman/templates/error.jinja2 +++ b/package/share/ahriman/templates/error.jinja2 @@ -5,8 +5,6 @@ - - {% include "utils/style.jinja2" %} {% include "user-style.jinja2" ignore missing %} diff --git a/src/ahriman/web/views/static.py b/src/ahriman/web/views/static.py new file mode 100644 index 00000000..1fd6e7a3 --- /dev/null +++ b/src/ahriman/web/views/static.py @@ -0,0 +1,44 @@ +# +# Copyright (c) 2021-2023 ahriman team. +# +# This file is part of ahriman +# (see https://github.com/arcan1s/ahriman). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +from aiohttp.web import HTTPFound + +from ahriman.models.user_access import UserAccess +from ahriman.web.views.base import BaseView + + +class StaticView(BaseView): + """ + special workaround for static files redirection (e.g. favicon) + + Attributes: + GET_PERMISSION(UserAccess): (class attribute) get permissions of self + """ + + GET_PERMISSION = UserAccess.Unauthorized + ROUTES = ["/favicon.ico"] + + async def get(self) -> None: + """ + process get request. No parameters supported here + + Raises: + HTTPFound: on success response + """ + raise HTTPFound(f"/static{self.request.path}") diff --git a/tests/ahriman/web/views/test_view_static.py b/tests/ahriman/web/views/test_view_static.py new file mode 100644 index 00000000..5722e224 --- /dev/null +++ b/tests/ahriman/web/views/test_view_static.py @@ -0,0 +1,31 @@ +import pytest + +from aiohttp.test_utils import TestClient + +from ahriman.models.user_access import UserAccess +from ahriman.web.views.static import StaticView + + +async def test_get_permission() -> None: + """ + must return correct permission for the request + """ + for method in ("GET",): + request = pytest.helpers.request("", "", method) + assert await StaticView.get_permission(request) == UserAccess.Unauthorized + + +def test_routes() -> None: + """ + must return correct routes + """ + assert StaticView.ROUTES == ["/favicon.ico"] + + +async def test_get(client_with_auth: TestClient) -> None: + """ + must generate status page correctly (/) + """ + response = await client_with_auth.get("/favicon.ico", allow_redirects=False) + assert response.status == 302 + assert response.headers["Location"] == "/static/favicon.ico"