Kelvin Wong avatar Kelvin Wong committed 666972a Draft

Fix for some utf-8 to unicode encoding issues

Comments (0)

Files changed (2)

tests/test_django_scrypt.py

 # -*- coding: utf-8 -*-
 
-from __future__ import with_statement
+from __future__ import with_statement, unicode_literals
 from django.conf.global_settings import PASSWORD_HASHERS as default_hashers
 from django.contrib.auth.hashers import (
     is_password_usable, check_password, make_password,
 import django_scrypt
 from django_scrypt.hashers import ScryptPasswordHasher
 import sys
-PYTHON3 = sys.version_info >= (3, 0)
 
 try:
     import scrypt
     scrypt = None
 
 
+PYTHON3 = sys.version_info >= (3, 0)
+
+
+def utf8(b):
+    """Returns Unicode string given UTF-8 encoded bytes"""
+    if PYTHON3:
+        return b
+    else:
+        return b.decode('utf-8')
+
+
 @skipUnless(scrypt, "Uninstalled scrypt module needed to generate hash")
 class TestScrypt(TestCase):
-    def utf8(self, s):
-        if PYTHON3:
-            return s
-        else:
-            return s.decode('utf-8')
 
     def setUp(self):
         scrypt_hashers = ("django_scrypt.hashers.ScryptPasswordHasher",) + default_hashers
         load_hashers(password_hashers=scrypt_hashers)
         self.password = 'letmein'
-        self.unicode_text = self.utf8('\xe1\x93\x84\xe1\x93\x87\xe1\x95\x97\xe1\x92\xbb\xe1\x92\xa5\xe1\x90\x85\xe1\x91\xa6')
+        self.unicode_text = utf8(b'\xe1\x93\x84\xe1\x93\x87\xe1\x95\x97\xe1\x92\xbb\xe1\x92\xa5\xe1\x90\x85\xe1\x91\xa6')
         self.bad_password = 'letmeinz'
         self.expected_hash_prefix = "scrypt"
         self.old_format_encoded_hash = "scrypt$FYY1dftUuK0b$16384$8$1$64$/JYOBEED7nMzJgvlqfzDj1JKGVLup0eYLyG39WA2KCywgnB1ubN0uzFYyaEQthINm6ynjjqr+D+U\nw5chi74WVw=="
         self.assertTrue(check_password(self.password, self.encoded_hash))
 
     def test_verify_old_hash_format_raises_error(self):
-        """Test that deprecated, old encoded hash format raises an Exception
+        """Ensure deprecated, old encoded hash format raises an Exception
 
         The old format hashes store N == 16384 whereas new format store Nexp == 14.
         The fix is to replace 16384 with 14 in each hash.
         self.assertTrue(
             str(cm.exception) in (
                 'could not compute hash',
-                'hash parameters are wrong (r*p should be < 2**30, and N should be a power of two > 1)'
+                'hash parameters are wrong (r*p should be < 2**30, and N '
+                'should be a power of two > 1)'
             )
         )
 

tests/test_subclassing.py

 # -*- coding: utf-8 -*-
 
-from __future__ import with_statement
+from __future__ import with_statement, unicode_literals
 import time
 from django.conf.global_settings import PASSWORD_HASHERS as default_hashers
 from django.contrib.auth.hashers import (
 class TestBigMemNScryptHasher(TestCase):
 
     def setUp(self):
-        scrypt_hashers = ("tests.test_subclassing.BigMemNScryptHasher",
-                          "django_scrypt.hashers.ScryptPasswordHasher") + default_hashers
+        scrypt_hashers = (
+            "tests.test_subclassing.BigMemNScryptHasher",
+            "django_scrypt.hashers.ScryptPasswordHasher") + default_hashers
         load_hashers(password_hashers=scrypt_hashers)
         self.password = 'letmein'
 
 class TestBigMemScryptHasher(TestCase):
 
     def setUp(self):
-        scrypt_hashers = ("tests.test_subclassing.BigMemScryptHasher",
-                          "django_scrypt.hashers.ScryptPasswordHasher") + default_hashers
+        scrypt_hashers = (
+            "tests.test_subclassing.BigMemScryptHasher",
+            "django_scrypt.hashers.ScryptPasswordHasher") + default_hashers
         load_hashers(password_hashers=scrypt_hashers)
         self.password = 'letmein'
 
 class TestLongTimeScryptHasher(TestCase):
 
     def setUp(self):
-        scrypt_hashers = ("tests.test_subclassing.LongTimeScryptHasher",
-                          "django_scrypt.hashers.ScryptPasswordHasher") + default_hashers
+        scrypt_hashers = (
+            "tests.test_subclassing.LongTimeScryptHasher",
+            "django_scrypt.hashers.ScryptPasswordHasher") + default_hashers
         load_hashers(password_hashers=scrypt_hashers)
         self.password = 'letmein'
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.