From 29036d22afbadb2ae64879ad2413b8b72fefe6ca Mon Sep 17 00:00:00 2001 From: James Stott <158563996+jamesstottmoj@users.noreply.github.com> Date: Tue, 9 Apr 2024 14:48:23 +0100 Subject: [PATCH] ensure redirect to index if no justice id regardless of route hit (#1283) * ensure redirect to index if no justice id regardless of route hit * updated code based on Michaels suggestions * added unit tests for success url property --- controlpanel/oidc.py | 9 +++++++++ tests/test_oidc.py | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/test_oidc.py diff --git a/controlpanel/oidc.py b/controlpanel/oidc.py index 0f923566a..2f148c7de 100644 --- a/controlpanel/oidc.py +++ b/controlpanel/oidc.py @@ -65,6 +65,7 @@ def verify_claims(self, claims): class StateMismatchHandler(OIDCAuthenticationCallbackView): + def get(self, *args, **kwargs): try: return super().get(*args, **kwargs) @@ -72,6 +73,14 @@ def get(self, *args, **kwargs): log.warning(f"Caught {e}: redirecting to login") return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL_FAILURE) + @property + def success_url(self): + + if not self.user.justice_email: + return reverse("index") + + return super().success_url + def logout(request): params = urlencode( diff --git a/tests/test_oidc.py b/tests/test_oidc.py new file mode 100644 index 000000000..7f0768fee --- /dev/null +++ b/tests/test_oidc.py @@ -0,0 +1,21 @@ +import pytest +from controlpanel.oidc import StateMismatchHandler +from unittest.mock import Mock + + +@pytest.mark.parametrize( + "email, success_url", + [ + ("", "/"), + ("example@justice.gov.uk", "/tools/"), + ], +) +def test_success_url(users, email, success_url): + request = Mock() + request.session.get.return_value = "/tools/" + user = users["normal_user"] + user.justice_email = email + view = StateMismatchHandler() + view.request = request + view.user = user + assert view.success_url == success_url