Kelvin Wong avatar Kelvin Wong committed feaa87f Draft

Remove liststringify as it was not used. Enhance docstrings. More testing

Comments (0)

Files changed (2)

django_scrypt/hashers.py

 
 
 def base64(s):
-    """Returns a Base64 encoded string"""
+    """Returns Base64 encoded bytes"""
     if PYTHON3:
         import base64
         return base64.encodestring(s)
 
 
 def stringify(s):
-    """Returns a string suitable for use with Django-Scrypt
+    """Returns a stripped string appropriate for a Scrypt hash function
+
+    Returns UTF-8 encoded string if Python 2 or
+            Unicode string if Python 3
     """
     if PYTHON3:
         if type(s) is bytes:
             return stripws(str(s))
 
 
-def liststringify(x):
-    """Stringify items in iterable x"""
-    return [stringify(i) for i in x]
-
-
 class ScryptPasswordHasher(BasePasswordHasher):
     """
     A secure password hasher using the Scrypt algorithm

tests/test_django_scrypt.py

 # -*- coding: utf-8 -*-
 
 from __future__ import with_statement, unicode_literals
+import sys
 import django
 from django.conf.global_settings import PASSWORD_HASHERS as default_hashers
 from django.contrib.auth.hashers import (
 from django.utils.translation import ugettext_noop as _
 
 import django_scrypt
-from django_scrypt.hashers import ScryptPasswordHasher, base64, stripws
-import sys
+from django_scrypt.hashers import (
+    ScryptPasswordHasher, base64, stripws, stringify)
 
 try:
     import scrypt
 class TestHelpers(TestCase):
     def setUp(self):
         self.utf8_encoded = b'a\xc2\xac\xe1\x88\xb4\xe2\x82\xac\xe8\x80\x80'
+        self.unicode_str = utf8(self.utf8_encoded)
         self.test_str = b'Test'
         self.test_str_base64 = b'VGVzdA==\n'
         self.spacey_str = ' Test \ntest \n'
         encoded = base64(self.test_str)
         expected = self.test_str_base64
         self.assertEqual(encoded, expected)
+        self.assertTrue(isinstance(encoded, bytes))
 
     def test_stripws(self):
         """Return a copy of the string with leading and trailing characters
         expected = self.stripped_str
         self.assertEqual(stripped, expected)
 
+    @skipUnless(not PYTHON3, "Python 2 required")
+    def test_stringify_py2(self):
+        """String returned is suitable for use by hasher"""
+        # String is already UTF-8 encoded, it should pass
+        self.assertTrue(isinstance(stringify(self.utf8_encoded), str))
+        # Unicode string is encoded to UTF-8
+        self.assertTrue(isinstance(stringify(self.unicode_str), str))
+
+    @skipUnless(PYTHON3, "Python 3 required")
+    def test_stringify_py3(self):
+        """String returned is suitable for use by hasher"""
+        # Input bytes are UTF-8 encoded, it should pass
+        self.assertTrue(isinstance(stringify(self.utf8_encoded), str))
+        # Unicode string is encoded to UTF-8
+        self.assertTrue(isinstance(stringify(self.unicode_str), str))
+
+
 @skipUnless(scrypt, "Uninstalled scrypt module needed to generate hash")
 class TestDjangoScrypt(TestCase):
 
     def test_upgrade_14_to_15(self):
         """Hash encoded with Django 1.4 works with Django 1.5"""
         django_14_hash = utf8(
-            b"scrypt$xUHzJGn4lszk$14$8$1$64$dxfJjF/gDXSD4fnl7Dk5E0u4vwPtEyL1Fq"
-            b"7xqZ/p+b8UUspTXv+eTQweMVbVgZmNY6umZsA3u8VF2Okx/7mzKg==")
-        encoded = make_password(self.unicode_text)
-        self.assertTrue(check_password(self.unicode_text, encoded))
+            b'scrypt$YfVpr72fFnUk$14$8$1$64$TULcyO5CycVvRBM7q6Bw4XNozJe0NwlOQu'
+            b'llq102Hn6r9OsgmHTAqzDBC4e8kklF9mcChavVtqKz\n0N6s+2q7tQ==')
+        #encoded = make_password(self.unicode_text)
+        #print(encoded)
+        self.assertTrue(check_password(self.unicode_text, django_14_hash))
+
+    @skipUnless(PYTHON3, "Requires Python 3")
+    def test_cross_interpreter_portability(self):
+        """Hash encoded with Python 2 usable on system with Python 3"""
+        py26_hash = utf8(
+            b'scrypt$h64FF0EdQ1f9$14$8$1$64$refoMN0aZci6vt0VOPKB7tfgPS/wgAWXxz'
+            b'e1z8CSzTzVSUV0hT3ByyE3YKPCpOegxJfZ6CZgQ+DS\nhd3G65p24Q==')
+        unicode_pwd = 'a\xac\u1234\u20ac\u8000'
+        #encoded = make_password(unicode_pwd)
+        #print(encoded)
+        self.assertTrue(check_password(unicode_pwd, py26_hash))
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.