Skip to content

Commit df523f2

Browse files
committed
test: added test cases for _user_has_social_auth_record
1 parent cb76eb5 commit df523f2

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

openedx/features/enterprise_support/tests/test_utils.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
)
3030
from openedx.features.enterprise_support.utils import (
3131
ENTERPRISE_HEADER_LINKS,
32+
_user_has_social_auth_record,
3233
clear_data_consent_share_cache,
3334
enterprise_fields_only,
3435
fetch_enterprise_customer_by_id,
@@ -539,6 +540,54 @@ def test_get_provider_login_url_with_redirect_url(self, mock_tpa, mock_next_logi
539540
)
540541
assert not mock_next_login_url.called
541542

543+
@mock.patch('openedx.features.enterprise_support.utils.UserSocialAuth')
544+
@mock.patch('openedx.features.enterprise_support.utils.third_party_auth')
545+
def test_user_has_social_auth_record(self, mock_tpa, mock_user_social_auth):
546+
user = mock.Mock()
547+
enterprise_customer = {
548+
'identity_providers': [
549+
{'provider_id': 'mock-idp'},
550+
],
551+
}
552+
mock_idp = mock.MagicMock(backend_name='mock-backend')
553+
mock_tpa.provider.Registry.get.return_value = mock_idp
554+
mock_user_social_auth.objects.select_related.return_value.filter.return_value.exists.return_value = True
555+
556+
result = _user_has_social_auth_record(user, enterprise_customer)
557+
assert result is True
558+
559+
mock_tpa.provider.Registry.get.assert_called_once_with(provider_id='mock-idp')
560+
mock_user_social_auth.objects.select_related.assert_called_once_with('user')
561+
mock_user_social_auth.objects.select_related.return_value.filter.assert_called_once_with(
562+
provider__in=['mock-backend'], user=user
563+
)
564+
565+
@mock.patch('openedx.features.enterprise_support.utils.UserSocialAuth')
566+
@mock.patch('openedx.features.enterprise_support.utils.third_party_auth')
567+
def test_user_has_social_auth_record_no_providers(self, mock_tpa, mock_user_social_auth):
568+
user = mock.Mock()
569+
enterprise_customer = {
570+
'identity_providers': [],
571+
}
572+
573+
result = _user_has_social_auth_record(user, enterprise_customer)
574+
assert result is False
575+
576+
assert not mock_tpa.provider.Registry.get.called
577+
assert not mock_user_social_auth.objects.select_related.called
578+
579+
@mock.patch('openedx.features.enterprise_support.utils.UserSocialAuth')
580+
@mock.patch('openedx.features.enterprise_support.utils.third_party_auth')
581+
def test_user_has_social_auth_record_no_enterprise_customer(self, mock_tpa, mock_user_social_auth):
582+
user = mock.Mock()
583+
enterprise_customer = None
584+
585+
result = _user_has_social_auth_record(user, enterprise_customer)
586+
assert result is False
587+
588+
assert not mock_tpa.provider.Registry.get.called
589+
assert not mock_user_social_auth.objects.select_related.called
590+
542591

543592
@override_settings(FEATURES=FEATURES_WITH_ENTERPRISE_ENABLED)
544593
@skip_unless_lms

0 commit comments

Comments
 (0)