Bring in latest updates to comments from source PR branch

This commit is contained in:
Rachid Mrad 2023-12-11 17:22:51 -05:00
commit 270e910520
No known key found for this signature in database
GPG key ID: EF38E4CEC4A8F3CF
22 changed files with 1083 additions and 76 deletions

View file

@ -0,0 +1,30 @@
import logging
from django.test import TestCase
from django.conf import settings
from djangooidc.oidc import Client
logger = logging.getLogger(__name__)
class OidcTest(TestCase):
def test_oidc_create_authn_request_with_acr_value(self):
"""Test that create_authn_request returns a redirect with an acr_value
when an acr_value is passed through session.
This test is only valid locally. On local, client can be initialized.
Client initialization does not work in pipeline, so test is useless in
pipeline. However, it will not fail in pipeline."""
try:
# Initialize provider using pyOICD
OP = getattr(settings, "OIDC_ACTIVE_PROVIDER")
CLIENT = Client(OP)
session = {"acr_value": "some_acr_value_maybe_ial2"}
response = CLIENT.create_authn_request(session)
self.assertEqual(response.status_code, 302)
self.assertIn("some_acr_value_maybe_ial2", response.url)
except Exception as err:
logger.warning(err)
logger.warning("Unable to configure OpenID Connect provider in pipeline. Cannot execute this test.")

View file

@ -84,9 +84,7 @@ class ViewsTest(TestCase):
def test_requires_step_up_auth(self, mock_client):
"""Invoke login_callback passing it a request when requires_step_up_auth returns True
and assert that session is updated and create_authn_request (mock) is called.
Possibly redundant with test_login_callback_no_step_up_auth"""
and assert that session is updated and create_authn_request (mock) is called."""
# Configure the mock to return an expected value for get_step_up_acr_value
mock_client.return_value.get_step_up_acr_value.return_value = "step_up_acr_value"
@ -101,7 +99,10 @@ class ViewsTest(TestCase):
) as mock_create_authn_request:
login_callback(request)
# Assert that get_step_up_acr_value was called and session was updated
# create_authn_request only gets called when requires_step_up_auth is True
# and it changes this acr_value in request.session
# Assert that acr_value is no longer empty string
self.assertNotEqual(request.session["acr_value"], "")
# And create_authn_request was called again
mock_create_authn_request.assert_called_once()
@ -122,9 +123,12 @@ class ViewsTest(TestCase):
) as mock_create_authn_request:
login_callback(request)
# Assert that get_step_up_acr_value was NOT called and session was NOT updated
# create_authn_request only gets called when requires_step_up_auth is True
# and it changes this acr_value in request.session
# Assert that acr_value is NOT updated by testing that it is still an empty string
self.assertEqual(request.session["acr_value"], "")
# create_authn_request was not called
# Assert create_authn_request was not called
mock_create_authn_request.assert_not_called()
@patch("djangooidc.views.authenticate")
@ -180,34 +184,3 @@ class ViewsTest(TestCase):
# assert
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, reverse("logout"))
class ViewsTestUnpatched(TestCase):
def setUp(self):
self.client = Client()
self.factory = RequestFactory()
def say_hi(*args):
return HttpResponse("Hi")
def user_info(*args):
return {
"sub": "TEST",
"email": "test@example.com",
"first_name": "Testy",
"last_name": "Tester",
"phone": "814564000",
}
def test_login_callback_requires_step_up_auth(self):
"""Walk through login_callback when requires_step_up_auth returns True
and assert that create_authn_request is returned."""
with patch("djangooidc.views.requires_step_up_auth", return_value=True), patch(
"djangooidc.views.Client.callback", return_value=self.user_info
), patch("djangooidc.views.Client.create_authn_request", side_effect=self.say_hi):
response = self.client.get(reverse("openid_login_callback"))
# assert
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Hi")

View file

@ -108,7 +108,14 @@ def requires_step_up_auth(userinfo):
acr_value = userinfo.get("ial", "")
uuid = userinfo.get("sub", "")
email = userinfo.get("email", "")
return User.needs_identity_verification(email, uuid) and acr_value != step_up_acr_value
if acr_value != step_up_acr_value:
# The acr of this attempt is not at the highest level
# so check if the user needs the higher level
return User.needs_identity_verification(email, uuid)
else:
# This attempt already came back at the highest level
# so does not require step up
return False
def logout(request, next_page=None):