diff --git a/common/djangoapps/external_auth/tests/test_shib.py b/common/djangoapps/external_auth/tests/test_shib.py index 2b6e2a4acf..80aabcb8f9 100644 --- a/common/djangoapps/external_auth/tests/test_shib.py +++ b/common/djangoapps/external_auth/tests/test_shib.py @@ -514,8 +514,16 @@ class ShibUtilFnTest(TestCase): """ def test__flatten_to_ascii(self): DIACRITIC = u"àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸåÅçÇ" # pylint: disable=C0103 + STR_DIACRI = "àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸåÅçÇ" # pylint: disable=C0103 FLATTENED = u"aeiouAEIOUaeiouyAEIOUYaeiouAEIOUanoANOaeiouyAEIOUYaAcC" # pylint: disable=C0103 self.assertEqual(_flatten_to_ascii(u'jas\xf6n'), u'jason') # umlaut self.assertEqual(_flatten_to_ascii(u'Jason\u5305'), u'Jason') # mandarin, so it just gets dropped self.assertEqual(_flatten_to_ascii(u'abc'), u'abc') # pass through - self.assertEqual(_flatten_to_ascii(DIACRITIC), FLATTENED) + + unicode_test = _flatten_to_ascii(DIACRITIC) + self.assertEqual(unicode_test, FLATTENED) + self.assertIsInstance(unicode_test, unicode) + + str_test = _flatten_to_ascii(STR_DIACRI) + self.assertEqual(str_test, FLATTENED) + self.assertIsInstance(str_test, str) diff --git a/common/djangoapps/external_auth/views.py b/common/djangoapps/external_auth/views.py index cef3ed7206..5ab5855a63 100644 --- a/common/djangoapps/external_auth/views.py +++ b/common/djangoapps/external_auth/views.py @@ -233,10 +233,13 @@ def _flatten_to_ascii(txt): """ Flattens possibly unicode txt to ascii (django username limitation) @param name: - @return: + @return: the flattened txt (in the same type as was originally passed in) """ - return unicodedata.normalize('NFKD', txt).encode('ASCII', 'ignore') - + if isinstance(txt, str): + txt = txt.decode('utf-8') + return unicodedata.normalize('NFKD', txt).encode('ASCII', 'ignore') + else: + return unicode(unicodedata.normalize('NFKD', txt).encode('ASCII', 'ignore')) @ensure_csrf_cookie @cache_if_anonymous