3
0
Fork 0

fix: workaround error when 'fanstatic.needed' missing from environ

apparently the previous fix doesn't actually get us past the error;
this one should do a better job although it's still not perfect
either.  for some reason the redirect to 'home' route is confused such
that it the new URL drops the ending filename but the rest remains, so
it's not really the home URL..  oh well at least this is better than
before
This commit is contained in:
Lance Edgar 2025-12-13 21:30:02 -06:00
parent 75b8de7ce3
commit 824889dfe0
4 changed files with 27 additions and 24 deletions

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# wuttaweb -- Web App for Wutta Framework # wuttaweb -- Web App for Wutta Framework
# Copyright © 2024 Lance Edgar # Copyright © 2024-2025 Lance Edgar
# #
# This file is part of Wutta Framework. # This file is part of Wutta Framework.
# #
@ -70,16 +70,8 @@ class WebHandler(GenericHandler):
url = self.config.get("wuttaweb.favicon_url") url = self.config.get("wuttaweb.favicon_url")
if url: if url:
return url return url
# nb. in rare circumstances i have received unhandled error email,
# which somehow was triggered by 'fanstatic.needed' being missing
# from the environ. not sure why that would happen, but it seems
# safe to ignore when it does - favicon is not *that* important.
if "fanstatic.needed" in request.environ:
return self.get_fanstatic_url(request, static.favicon) return self.get_fanstatic_url(request, static.favicon)
return ""
def get_header_logo_url(self, request): def get_header_logo_url(self, request):
""" """
Returns the canonical app header image URL. Returns the canonical app header image URL.

View file

@ -41,6 +41,7 @@ import logging
from collections import OrderedDict from collections import OrderedDict
from pyramid import threadlocal from pyramid import threadlocal
from pyramid.httpexceptions import HTTPFound
from wuttaweb import helpers from wuttaweb import helpers
from wuttaweb.db import Session from wuttaweb.db import Session
@ -123,6 +124,17 @@ def new_request(event):
config = request.registry.settings["wutta_config"] config = request.registry.settings["wutta_config"]
app = config.get_app() app = config.get_app()
# nb. in rare circumstances i have received unhandled error email,
# which somehow was triggered by 'fanstatic.needed' being missing
# from the environ. not sure why that would happen, but it seems
# to when crawlers request a non-existent filename under the
# fanstatic path. there isn't a great way to handle it since
# e.g. can't show the normal error page if JS resources won't
# load, so we try a hail-mary redirect..
# (nb. also we skip this if environ is empty, i.e. for tests)
if request.environ and "fanstatic.needed" not in request.environ:
raise HTTPFound(location=request.route_url("home"))
request.wutta_config = config request.wutta_config = config
def get_referrer(default=None): def get_referrer(default=None):

View file

@ -38,14 +38,6 @@ class TestWebHandler(WebTestCase):
url = handler.get_fanstatic_url(self.request, static.logo) url = handler.get_fanstatic_url(self.request, static.logo)
self.assertEqual(url, "/testing/fanstatic/wuttaweb_img/logo.png") self.assertEqual(url, "/testing/fanstatic/wuttaweb_img/logo.png")
# error if environ missing config/data
environ = dict(self.request.environ)
del environ["fanstatic.needed"]
with patch.object(self.request, "environ", new=environ):
self.assertRaises(
KeyError, handler.get_fanstatic_url, self.request, static.logo
)
def test_get_favicon_url(self): def test_get_favicon_url(self):
handler = self.make_handler() handler = self.make_handler()
@ -53,12 +45,6 @@ class TestWebHandler(WebTestCase):
url = handler.get_favicon_url(self.request) url = handler.get_favicon_url(self.request)
self.assertEqual(url, "/fanstatic/wuttaweb_img/favicon.ico") self.assertEqual(url, "/fanstatic/wuttaweb_img/favicon.ico")
# returns empty if environ missing config/data
environ = dict(self.request.environ)
del environ["fanstatic.needed"]
with patch.object(self.request, "environ", new=environ):
self.assertEqual(handler.get_favicon_url(self.request), "")
# config override # config override
self.config.setdefault("wuttaweb.favicon_url", "/testing/other.ico") self.config.setdefault("wuttaweb.favicon_url", "/testing/other.ico")
url = handler.get_favicon_url(self.request) url = handler.get_favicon_url(self.request)

View file

@ -7,6 +7,7 @@ from unittest.mock import MagicMock, patch
from wuttjamaican.conf import WuttaConfig from wuttjamaican.conf import WuttaConfig
from pyramid import testing from pyramid import testing
from pyramid.httpexceptions import HTTPFound
from pyramid.security import remember from pyramid.security import remember
from wuttaweb import subscribers from wuttaweb import subscribers
@ -14,6 +15,10 @@ from wuttaweb import helpers
from wuttaweb.auth import WuttaSecurityPolicy from wuttaweb.auth import WuttaSecurityPolicy
# TODO: change import above
mod = subscribers
class TestNewRequest(TestCase): class TestNewRequest(TestCase):
def setUp(self): def setUp(self):
@ -34,6 +39,14 @@ class TestNewRequest(TestCase):
# request.registry.settings = {'wutta_config': self.config} # request.registry.settings = {'wutta_config': self.config}
return request return request
def test_missing_fanstatic_needed(self):
self.pyramid_config.add_route("home", "/")
event = MagicMock(request=self.request)
# should redirect if 'fanstatic.needed' missing from environ
with patch.object(self.request, "environ", new={"foo": "bar"}):
self.assertRaises(HTTPFound, mod.new_request, event)
def test_wutta_config(self): def test_wutta_config(self):
event = MagicMock(request=self.request) event = MagicMock(request=self.request)