From 076d82970c610f8a1b576ba2bab01ff456268b43 Mon Sep 17 00:00:00 2001
From: pkulkark <pooja@opencraft.com>
Date: Fri, 2 Aug 2019 09:53:42 +0530
Subject: [PATCH] Fix unicode username hints with SSO

Usernames containing unicode characters were
reportedly not showing up correctly in the
registration form when registered with MOE/SAML,
FB and Google.
This change fixes the issue by overriding the
django setting SOCIAL_AUTH_CLEAN_USERNAMES to
disable the default username check that wasn't
allowing non-ascii values.
---
 common/djangoapps/third_party_auth/settings.py         |  5 ++++-
 .../djangoapps/third_party_auth/tests/test_settings.py | 10 ++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/common/djangoapps/third_party_auth/settings.py b/common/djangoapps/third_party_auth/settings.py
index 2f2c0230d24..aee84672786 100644
--- a/common/djangoapps/third_party_auth/settings.py
+++ b/common/djangoapps/third_party_auth/settings.py
@@ -11,7 +11,7 @@ If true, it:
 """
 
 from __future__ import absolute_import
-
+from django.conf import settings
 from openedx.features.enterprise_support.api import insert_enterprise_pipeline_elements
 
 
@@ -42,6 +42,9 @@ def apply_settings(django_settings):
     # Adding extra key value pair in the url query string for microsoft as per request
     django_settings.SOCIAL_AUTH_AZUREAD_OAUTH2_AUTH_EXTRA_ARGUMENTS = {'msafed': 0}
 
+    # Avoid default username check to allow non-ascii characters
+    django_settings.SOCIAL_AUTH_CLEAN_USERNAMES = not settings.FEATURES.get("ENABLE_UNICODE_USERNAME")
+
     # Inject our customized auth pipeline. All auth backends must work with
     # this pipeline.
     django_settings.SOCIAL_AUTH_PIPELINE = [
diff --git a/common/djangoapps/third_party_auth/tests/test_settings.py b/common/djangoapps/third_party_auth/tests/test_settings.py
index 8353814e4f0..e442280d395 100644
--- a/common/djangoapps/third_party_auth/tests/test_settings.py
+++ b/common/djangoapps/third_party_auth/tests/test_settings.py
@@ -4,6 +4,7 @@ from __future__ import absolute_import
 
 import unittest
 
+from mock import patch
 from third_party_auth import provider, settings
 from third_party_auth.tests import testutil
 
@@ -58,3 +59,12 @@ class SettingsUnitTest(testutil.TestCase):
     def test_apply_settings_turns_off_redirect_sanitization(self):
         settings.apply_settings(self.settings)
         self.assertFalse(self.settings.SOCIAL_AUTH_SANITIZE_REDIRECTS)
+
+    def test_apply_settings_avoids_default_username_check(self):
+        # Avoid the default username check where non-ascii characters are not
+        # allowed when unicode username is enabled
+        settings.apply_settings(self.settings)
+        self.assertTrue(self.settings.SOCIAL_AUTH_CLEAN_USERNAMES)   # verify default behavior
+        with patch.dict('django.conf.settings.FEATURES', {'ENABLE_UNICODE_USERNAME': True}):
+            settings.apply_settings(self.settings)
+            self.assertFalse(self.settings.SOCIAL_AUTH_CLEAN_USERNAMES)
-- 
GitLab