Commits

Calin Furau committed 0de8b69

Refactored bcrypt_check_password method, fixed unit tests

Comments (0)

Files changed (2)

django_bcrypt/models.py

 
 def migrate_to_bcrypt():
     """Returns ``True`` if password migration is activated. """
-    migrated = getattr(settings, "BCRYPT_MIGRATE", False)
-    if migrated:
-        return True
-    return False
+    return getattr(settings, "BCRYPT_MIGRATE", True)
 
 
 def bcrypt_check_password(self, raw_password):
     if self.password.startswith('bc$'):
         salt_and_hash = self.password[3:]
         return bcrypt.hashpw(raw_password, salt_and_hash) == salt_and_hash
-    elif self.password.startswith('sha1$') and migrate_to_bcrypt:
-        bcrypt_password = bcrypt_set_password(raw_password)
-        salt_and_hash = bcrypt_password[3:]
-        return bcrypt.hashpw(raw_password, salt_and_hash) == salt_and_hash
+    elif _check_password(self, raw_password):
+        if is_enabled() and migrate_to_bcrypt():
+            self.set_password(raw_password)
+            salt_and_hash = self.password[3:]
+            return bcrypt.hashpw(raw_password, salt_and_hash) == salt_and_hash
+        return True
+    return False
 _check_password = User.check_password
 User.check_password = bcrypt_check_password
 
     else:
         salt = bcrypt.gensalt(get_rounds())
         self.password = 'bc$' + bcrypt.hashpw(raw_password, salt)
+        self.save()
 _set_password = User.set_password
 User.set_password = bcrypt_set_password

django_bcrypt/tests.py

 
 from django_bcrypt.models import (bcrypt_check_password, bcrypt_set_password,
                                   _check_password, _set_password,
-                                  get_rounds, is_enabled)
+                                  get_rounds, is_enabled, migrate_to_bcrypt)
 
 
 class CheckPasswordTest(TestCase):
         password_12 = user.password
         self.assertTrue(bcrypt_check_password(user, 'password'))
 
-    def test_migrated_password(self):
+
+class MigratePasswordTest(TestCase):
+    def test_migrate_password(self):
         user = User()
-        with settings(BCRYPT_MIGRATE=True):
-            bcrypt_set_password(user, 'password')
-        self.assertTrue(bcrypt_check_password(user, 'password'))
+        with settings(BCRYPT_MIGRATE=True, BCRYPT_ENABLED_UNDER_TEST=True):
+            _set_password(user, 'password')
+            self.assertTrue(user.password.startswith('sha1$'))
+            self.assertTrue(bcrypt_check_password(user, 'password'))
+            self.assertTrue(user.password.startswith('bc$'))
 
-    def test_non_migrated_password(self):
+    def test_no_migrate_password(self):
         user = User()
-        with settings(BCRYPT_MIGRATE=False):
+        with settings(BCRYPT_MIGRATE=False, BCRYPT_ENABLED_UNDER_TEST=True):
             _set_password(user, 'password')
-        self.assertTrue(bcrypt_check_password(user, 'password'))
-        self.assertFalse(bcrypt_check_password(user, 'invalid'))
+            self.assertTrue(user.password.startswith('sha1$'))
+            self.assertTrue(bcrypt_check_password(user, 'password'))
+            self.assertTrue(user.password.startswith('sha1$'))
 
 
 class SetPasswordTest(TestCase):
         with settings(BCRYPT_ENABLED_UNDER_TEST=NotImplemented):
             self.assertFalse(is_enabled())
 
+    def test_migrate_to_bcrypt(self):
+        with settings(BCRYPT_MIGRATE=False):
+            self.assertEqual(migrate_to_bcrypt(), False)
+        with settings(BCRYPT_MIGRATE=True):
+            self.assertEqual(migrate_to_bcrypt(), True)
+
 
 def settings(**kwargs):
     kwargs = dict({'BCRYPT_ENABLED': True,