Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle None identity providers in _user_has_social_auth_record #36186

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading