@@ -369,12 +369,30 @@ def __init__(self):
369369 'attr_last_name' : 'last_name' ,
370370 'attr_first_name' : 'first_name' ,
371371 'attr_user_permanent_id' : 'name_id' ,
372+ 'attr_groups' : 'member' ,
372373 },
373374 {
374375 'last_name' : ['Admin' ],
375376 'username' : ['gateway_admin' ],
376377 'first_name' : ['Gateway' ],
377378 'name_id' : 'gateway_admin' ,
379+ 'member' : ['group-1' , 'group-2' ],
380+ },
381+ ),
382+ (
383+ {
384+ 'attr_username' : 'username' ,
385+ 'attr_last_name' : 'last_name' ,
386+ 'attr_first_name' : 'first_name' ,
387+ 'attr_user_permanent_id' : 'name_id' ,
388+ 'attr_groups' : 'nonexistent_group_attr' , # Configure a group attribute that won't be in response
389+ },
390+ {
391+ 'last_name' : ['Admin' ],
392+ 'username' : ['gateway_admin' ],
393+ 'first_name' : ['Gateway' ],
394+ 'name_id' : 'gateway_admin' ,
395+ # No group data should be present - will hit the "Unable to get any group claims" branch
378396 },
379397 ),
380398 ],
@@ -403,6 +421,7 @@ def test_extra_data_default_attrs(idp_fields, expected_results):
403421 'first_name' : ['Gateway' ],
404422 'Role' : ['default-roles-gateway realm' , 'manage-account' , 'uma_authorization' , 'view-profile' , 'offline_access' , 'manage-account-links' ],
405423 'name_id' : 'gateway_admin' ,
424+ 'member' : ['group-1' , 'group-2' ],
406425 },
407426 }
408427 au = AuthenticatorUser ()
@@ -411,6 +430,51 @@ def test_extra_data_default_attrs(idp_fields, expected_results):
411430 assert results == expected_results
412431
413432
433+ def test_extra_data_no_group_claims_logging (caplog ):
434+ """Test that the 'Unable to get any group claims' logging is triggered when no group attributes are found."""
435+ import logging
436+
437+ from ansible_base .authentication .authenticator_plugins .saml import idp_string
438+ from ansible_base .authentication .models import AuthenticatorUser
439+
440+ ap = AuthenticatorPlugin ()
441+ database_instance = SimpleNamespace ()
442+ enabled_idps = {
443+ 'ENABLED_IDPS' : {
444+ idp_string : {
445+ 'attr_username' : 'username' ,
446+ 'attr_user_permanent_id' : 'name_id' ,
447+ 'attr_groups' : 'missing_group_attr' , # This attribute won't be in the response
448+ },
449+ }
450+ }
451+ database_instance .configuration = enabled_idps
452+ ap .database_instance = database_instance
453+
454+ response = {
455+ 'idp_name' : 'IdP' ,
456+ 'attributes' : {
457+ 'username' : ['gateway_admin' ],
458+ 'name_id' : 'gateway_admin' ,
459+ # Note: No 'missing_group_attr' and no default 'Group' attribute
460+ },
461+ }
462+
463+ au = AuthenticatorUser ()
464+
465+ # Set logging level to DEBUG to capture the debug message
466+ with caplog .at_level (logging .DEBUG , logger = 'ansible_base.authentication.authenticator_plugins.saml' ):
467+ with mock .patch ('social_core.backends.saml.SAMLAuth.extra_data' , return_value = {}):
468+ results = ap .extra_data (None , 'IdP:gateway_admin' , response , ** {'social' : au })
469+
470+ # Verify the log message was captured
471+ assert "Unable to get any group claims from the SAML response" in caplog .text
472+
473+ # Verify no group data in results
474+ assert 'missing_group_attr' not in results
475+ assert 'Group' not in results
476+
477+
414478def test_saml_create_via_api_without_callback_url (admin_api_client , saml_configuration ):
415479 del saml_configuration ['CALLBACK_URL' ]
416480
0 commit comments