Skip to content

Commit 4bcccce

Browse files
relroddmzoneill
authored andcommitted
Backend's ID_KEY might be None
Signed-off-by: Rick Elrod <[email protected]>
1 parent 7c71771 commit 4bcccce

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

ansible_base/authentication/utils/authentication.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,13 @@ def check_system_username(uid: str) -> None:
9696

9797
def determine_username_from_uid_social(**kwargs) -> dict:
9898
uid_field = getattr(kwargs.get('backend', None), 'ID_KEY', 'username')
99+
if uid_field is None:
100+
uid_field = 'username'
99101
selected_username = kwargs.get('details', {}).get(uid_field, None)
100102
if not selected_username:
101-
raise AuthException(_('Unable to get associated username from: %(details)s') % {'details': kwargs.get("details", None)})
103+
raise AuthException(
104+
_('Unable to get associated username from attribute {uid_field}: %(details)s') % {'uid_field': uid_field, 'details': kwargs.get("details", None)}
105+
)
102106

103107
authenticator = kwargs.get('backend')
104108
if not authenticator:

test_app/tests/authentication/utils/test_authentication.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from types import SimpleNamespace
2+
13
import pytest
24
from django.conf import settings
35
from social_core.exceptions import AuthException
@@ -197,3 +199,17 @@ def test_determine_username_from_uid_social_happy_path(self, ldap_authenticator)
197199
uid="Bob",
198200
)
199201
assert response == {'username': 'Bob'}
202+
203+
@pytest.mark.parametrize(
204+
"kwargs,expected_uid_field",
205+
[
206+
({}, 'username'),
207+
({'backend': None}, 'username'),
208+
({'backend': SimpleNamespace(ID_KEY='testing')}, 'testing'),
209+
({'backend': SimpleNamespace(ID_KEY=None)}, 'testing'),
210+
],
211+
)
212+
def test_determine_username_from_uid_social_authenticator_ID_KEY(self, kwargs, expected_uid_field):
213+
with pytest.raises(AuthException) as ae:
214+
authentication.determine_username_from_uid_social(**kwargs)
215+
assert f'Unable to get associated username from attribute {expected_uid_field}' in ae

0 commit comments

Comments
 (0)