Skip to content
Snippets Groups Projects
Commit 6cb30056 authored by Antoine Lambert's avatar Antoine Lambert
Browse files

common.utils.context_processor: Check user attr existence in request

Fix the numerous "'WSGIRequest' object has no attribute 'user'" error
reported in production.
parent 6c89d4d9
No related branches found
No related tags found
1 merge request!293common.utils.context_processor: Check user attr existence in request
......@@ -247,7 +247,11 @@ def context_processor(request):
in all swh-web templates.
"""
config = get_config()
if request.user.is_authenticated and not hasattr(request.user, "backend"):
if (
hasattr(request, "user")
and request.user.is_authenticated
and not hasattr(request.user, "backend")
):
# To avoid django.template.base.VariableDoesNotExist errors
# when rendering templates when standard Django user is logged in.
request.user.backend = "django.contrib.auth.backends.ModelBackend"
......
......@@ -15,6 +15,7 @@ from swh.web.auth.models import OIDCUser
from swh.web.auth.utils import OIDC_SWH_WEB_CLIENT_ID
from swh.web.common.utils import reverse
from swh.web.tests.django_asserts import assert_template_used, assert_contains
from swh.web.urls import _default_view as homepage_view
from . import sample_data
from .keycloak_mock import mock_keycloak
......@@ -326,3 +327,12 @@ def test_oidc_silent_refresh_failure(client, mocker):
"logout", query_params={"next_path": next_path, "remote_user": 1}
)
assert response["location"] == logout_url
def test_view_rendering_when_user_not_set_in_request(request_factory):
request = request_factory.get("/")
# Django RequestFactory do not set any user by default
assert not hasattr(request, "user")
response = homepage_view(request)
assert response.status_code == 200
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment