diff --git a/sqlalchemy_utils/types/password.py b/sqlalchemy_utils/types/password.py index fc13772..2b050c9 100644 --- a/sqlalchemy_utils/types/password.py +++ b/sqlalchemy_utils/types/password.py @@ -23,6 +23,11 @@ class Password(object): self.context = weakref.proxy(context) 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) if valid and new: # New hash was calculated due to various reasons; stored one diff --git a/tests/types/test_password.py b/tests/types/test_password.py index 2210e9c..7115ce6 100644 --- a/tests/types/test_password.py +++ b/tests/types/test_password.py @@ -88,3 +88,15 @@ class TestPasswordType(TestCase): def test_without_schemes(self): 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