From cb76eb5aa4a78ff4a4d4bc1e1188ffec6d17e28e Mon Sep 17 00:00:00 2001 From: Muhammad Sameer Amin <35958006+sameeramin@users.noreply.github.com> Date: Tue, 28 Jan 2025 18:32:30 +0500 Subject: [PATCH 1/2] fix: Handle None identity providers in `_user_has_social_auth_record` --- openedx/features/enterprise_support/utils.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/openedx/features/enterprise_support/utils.py b/openedx/features/enterprise_support/utils.py index 6b007ebed57b..9e99bf599267 100644 --- a/openedx/features/enterprise_support/utils.py +++ b/openedx/features/enterprise_support/utils.py @@ -294,9 +294,12 @@ def _user_has_social_auth_record(user, enterprise_customer): identity_provider = third_party_auth.provider.Registry.get( provider_id=idp['provider_id'] ) - provider_backend_names.append(identity_provider.backend_name) - return UserSocialAuth.objects.select_related('user').\ - filter(provider__in=provider_backend_names, user=user).exists() + if identity_provider and hasattr(identity_provider, 'backend_name'): + provider_backend_names.append(identity_provider.backend_name) + + if provider_backend_names: + return UserSocialAuth.objects.select_related('user').\ + filter(provider__in=provider_backend_names, user=user).exists() return False From df523f2d64c754b2b31dcf80dd4916c93e913db9 Mon Sep 17 00:00:00 2001 From: Muhammad Sameer Amin <35958006+sameeramin@users.noreply.github.com> Date: Fri, 31 Jan 2025 12:36:32 +0500 Subject: [PATCH 2/2] test: added test cases for `_user_has_social_auth_record` --- .../enterprise_support/tests/test_utils.py | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/openedx/features/enterprise_support/tests/test_utils.py b/openedx/features/enterprise_support/tests/test_utils.py index d693f6be72b6..48a331083f41 100644 --- a/openedx/features/enterprise_support/tests/test_utils.py +++ b/openedx/features/enterprise_support/tests/test_utils.py @@ -29,6 +29,7 @@ ) from openedx.features.enterprise_support.utils import ( ENTERPRISE_HEADER_LINKS, + _user_has_social_auth_record, clear_data_consent_share_cache, enterprise_fields_only, fetch_enterprise_customer_by_id, @@ -539,6 +540,54 @@ def test_get_provider_login_url_with_redirect_url(self, mock_tpa, mock_next_logi ) assert not mock_next_login_url.called + @mock.patch('openedx.features.enterprise_support.utils.UserSocialAuth') + @mock.patch('openedx.features.enterprise_support.utils.third_party_auth') + def test_user_has_social_auth_record(self, mock_tpa, mock_user_social_auth): + user = mock.Mock() + enterprise_customer = { + 'identity_providers': [ + {'provider_id': 'mock-idp'}, + ], + } + mock_idp = mock.MagicMock(backend_name='mock-backend') + mock_tpa.provider.Registry.get.return_value = mock_idp + mock_user_social_auth.objects.select_related.return_value.filter.return_value.exists.return_value = True + + result = _user_has_social_auth_record(user, enterprise_customer) + assert result is True + + mock_tpa.provider.Registry.get.assert_called_once_with(provider_id='mock-idp') + mock_user_social_auth.objects.select_related.assert_called_once_with('user') + mock_user_social_auth.objects.select_related.return_value.filter.assert_called_once_with( + provider__in=['mock-backend'], user=user + ) + + @mock.patch('openedx.features.enterprise_support.utils.UserSocialAuth') + @mock.patch('openedx.features.enterprise_support.utils.third_party_auth') + def test_user_has_social_auth_record_no_providers(self, mock_tpa, mock_user_social_auth): + user = mock.Mock() + enterprise_customer = { + 'identity_providers': [], + } + + result = _user_has_social_auth_record(user, enterprise_customer) + assert result is False + + assert not mock_tpa.provider.Registry.get.called + assert not mock_user_social_auth.objects.select_related.called + + @mock.patch('openedx.features.enterprise_support.utils.UserSocialAuth') + @mock.patch('openedx.features.enterprise_support.utils.third_party_auth') + def test_user_has_social_auth_record_no_enterprise_customer(self, mock_tpa, mock_user_social_auth): + user = mock.Mock() + enterprise_customer = None + + result = _user_has_social_auth_record(user, enterprise_customer) + assert result is False + + assert not mock_tpa.provider.Registry.get.called + assert not mock_user_social_auth.objects.select_related.called + @override_settings(FEATURES=FEATURES_WITH_ENTERPRISE_ENABLED) @skip_unless_lms