Fix comparison of password types.

session.flush() on models with password type with the coercion
listener active raised a TypeError before.
This commit is contained in:
Ryan Leckey
2013-11-04 11:09:23 -08:00
parent 7af808366a
commit ed46d408bd
2 changed files with 17 additions and 0 deletions

View File

@@ -23,6 +23,11 @@ class Password(object):
self.context = weakref.proxy(context) self.context = weakref.proxy(context)
def __eq__(self, value): def __eq__(self, value):
if isinstance(value, Password):
# Comparing 2 hashes isn't very useful; but this equality
# method breaks otherwise.
return value.hash == self.hash
valid, new = self.context.verify_and_update(value, self.hash) valid, new = self.context.verify_and_update(value, self.hash)
if valid and new: if valid and new:
# New hash was calculated due to various reasons; stored one # New hash was calculated due to various reasons; stored one

View File

@@ -88,3 +88,15 @@ class TestPasswordType(TestCase):
def test_without_schemes(self): def test_without_schemes(self):
assert PasswordType(schemes=[]).length == 1024 assert PasswordType(schemes=[]).length == 1024
def test_compare(self):
from passlib.hash import md5_crypt
obj = self.User()
obj.password = Password(md5_crypt.encrypt('b'))
other = self.User()
other.password = Password(md5_crypt.encrypt('b'))
# Not sure what to assert here; the test raised an error before.
assert obj.password != other.password