|
| 1 | +from types import SimpleNamespace |
1 | 2 | from unittest import mock |
2 | 3 |
|
3 | 4 | import pytest |
@@ -213,6 +214,91 @@ def __init__(self): |
213 | 214 | assert "mygroup" in rDict["Group"] |
214 | 215 |
|
215 | 216 |
|
| 217 | +@pytest.mark.django_db |
| 218 | +@pytest.mark.parametrize( |
| 219 | + "idp_fields,expected_results", |
| 220 | + [ |
| 221 | + ( |
| 222 | + { |
| 223 | + 'attr_email': 'email', |
| 224 | + 'attr_groups': 'Groups', |
| 225 | + 'attr_username': 'username', |
| 226 | + 'attr_last_name': 'last_name', |
| 227 | + 'attr_first_name': 'first_name', |
| 228 | + 'attr_user_permanent_id': 'name_id', |
| 229 | + }, |
| 230 | + { |
| 231 | + |
| 232 | + 'last_name': ['Admin'], |
| 233 | + 'username': ['gateway_admin'], |
| 234 | + 'first_name': ['Gateway'], |
| 235 | + 'name_id': 'gateway_admin', |
| 236 | + }, |
| 237 | + ), |
| 238 | + ( |
| 239 | + { |
| 240 | + 'attr_email': 'not_email', |
| 241 | + 'attr_groups': 'Groups', |
| 242 | + 'attr_username': 'username', |
| 243 | + 'attr_last_name': 'last_name', |
| 244 | + 'attr_first_name': 'first_name', |
| 245 | + 'attr_user_permanent_id': 'name_id', |
| 246 | + }, |
| 247 | + { |
| 248 | + 'last_name': ['Admin'], |
| 249 | + 'username': ['gateway_admin'], |
| 250 | + 'first_name': ['Gateway'], |
| 251 | + 'name_id': 'gateway_admin', |
| 252 | + }, |
| 253 | + ), |
| 254 | + ( |
| 255 | + { |
| 256 | + 'attr_username': 'username', |
| 257 | + 'attr_last_name': 'last_name', |
| 258 | + 'attr_first_name': 'first_name', |
| 259 | + 'attr_user_permanent_id': 'name_id', |
| 260 | + }, |
| 261 | + { |
| 262 | + 'last_name': ['Admin'], |
| 263 | + 'username': ['gateway_admin'], |
| 264 | + 'first_name': ['Gateway'], |
| 265 | + 'name_id': 'gateway_admin', |
| 266 | + }, |
| 267 | + ), |
| 268 | + ], |
| 269 | +) |
| 270 | +def test_extra_data_default_attrs(idp_fields, expected_results): |
| 271 | + from ansible_base.authentication.authenticator_plugins.saml import idp_string |
| 272 | + from ansible_base.authentication.models import AuthenticatorUser |
| 273 | + |
| 274 | + ap = AuthenticatorPlugin() |
| 275 | + database_instance = SimpleNamespace() |
| 276 | + enabled_idps = { |
| 277 | + 'ENABLED_IDPS': { |
| 278 | + idp_string: idp_fields, |
| 279 | + } |
| 280 | + } |
| 281 | + database_instance.configuration = enabled_idps |
| 282 | + ap.database_instance = database_instance |
| 283 | + |
| 284 | + response = { |
| 285 | + 'idp_name': 'IdP', |
| 286 | + 'attributes': { |
| 287 | + |
| 288 | + 'last_name': ['Admin'], |
| 289 | + 'is_superuser': ['true'], |
| 290 | + 'username': ['gateway_admin'], |
| 291 | + 'first_name': ['Gateway'], |
| 292 | + 'Role': ['default-roles-gateway realm', 'manage-account', 'uma_authorization', 'view-profile', 'offline_access', 'manage-account-links'], |
| 293 | + 'name_id': 'gateway_admin', |
| 294 | + }, |
| 295 | + } |
| 296 | + au = AuthenticatorUser() |
| 297 | + with mock.patch('social_core.backends.saml.SAMLAuth.extra_data', return_value={}): |
| 298 | + results = ap.extra_data(None, 'IdP:gateway_admin', response, **{'social': au}) |
| 299 | + assert results == expected_results |
| 300 | + |
| 301 | + |
216 | 302 | def test_saml_create_via_api_without_callback_url(admin_api_client, saml_configuration): |
217 | 303 | del saml_configuration['CALLBACK_URL'] |
218 | 304 |
|
|
0 commit comments