From ed46d408bd98dcc83431d93a0aec633c64e00abb Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Mon, 4 Nov 2013 11:09:23 -0800 Subject: [PATCH] Fix comparison of password types. session.flush() on models with password type with the coercion listener active raised a TypeError before. --- sqlalchemy_utils/types/password.py | 5 +++++ tests/types/test_password.py | 12 ++++++++++++ 2 files changed, 17 insertions(+) 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