Skip to content

Commit

Permalink
Merge pull request #36186 from openedx/sameeramin/ENT-9870
Browse files Browse the repository at this point in the history
fix: Handle None identity providers in `_user_has_social_auth_record`
  • Loading branch information
sameeramin authored Jan 31, 2025
2 parents 4f13ee0 + df523f2 commit c7c9681
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
49 changes: 49 additions & 0 deletions openedx/features/enterprise_support/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions openedx/features/enterprise_support/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit c7c9681

Please sign in to comment.