diff options
| author | Jaap Roes <jaap@eight.nl> | 2013-06-14 16:19:53 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2013-06-15 10:50:55 +0200 |
| commit | 990f8d92dca913462f656387b012801f01c96244 (patch) | |
| tree | 6343012382c19e2034d9697c1d2d77ca8eeefce4 | |
| parent | a1122e14a6d2ace8907923507e7d31c44a6f7c68 (diff) | |
Fixed #20599 -- Changed wording of ValueError raised by _load_library
The _load_library method on BasePasswordHasher turns ImportErrors
into ValueErrors, this masks ImportErrors in the algorithm library.
Changed it to a clearer worded error message that includes
the ImportError string.
| -rw-r--r-- | django/contrib/auth/hashers.py | 10 | ||||
| -rw-r--r-- | django/contrib/auth/tests/test_hashers.py | 19 |
2 files changed, 21 insertions, 8 deletions
diff --git a/django/contrib/auth/hashers.py b/django/contrib/auth/hashers.py index 6abdb5f476..36d247d71e 100644 --- a/django/contrib/auth/hashers.py +++ b/django/contrib/auth/hashers.py @@ -171,12 +171,12 @@ class BasePasswordHasher(object): name = mod_path = self.library try: module = importlib.import_module(mod_path) - except ImportError: - raise ValueError("Couldn't load %s password algorithm " - "library" % name) + except ImportError as e: + raise ValueError("Couldn't load %r algorithm library: %s" % + (self.__class__.__name__, e)) return module - raise ValueError("Hasher '%s' doesn't specify a library attribute" % - self.__class__) + raise ValueError("Hasher %r doesn't specify a library attribute" % + self.__class__.__name__) def salt(self): """ diff --git a/django/contrib/auth/tests/test_hashers.py b/django/contrib/auth/tests/test_hashers.py index d49fdc412e..9a72df2075 100644 --- a/django/contrib/auth/tests/test_hashers.py +++ b/django/contrib/auth/tests/test_hashers.py @@ -2,7 +2,7 @@ from __future__ import unicode_literals from django.conf.global_settings import PASSWORD_HASHERS as default_hashers -from django.contrib.auth.hashers import (is_password_usable, +from django.contrib.auth.hashers import (is_password_usable, BasePasswordHasher, check_password, make_password, PBKDF2PasswordHasher, load_hashers, PBKDF2SHA1PasswordHasher, get_hasher, identify_hasher, UNUSABLE_PASSWORD) from django.utils import unittest @@ -128,9 +128,8 @@ class TestUtilsHashPass(unittest.TestCase): self.assertRaises(ValueError, identify_hasher, encoded) def test_bad_algorithm(self): - def doit(): + with self.assertRaises(ValueError): make_password('lètmein', hasher='lolcat') - self.assertRaises(ValueError, doit) self.assertRaises(ValueError, identify_hasher, "lolcat$salt$hash") def test_bad_encoded(self): @@ -178,3 +177,17 @@ class TestUtilsHashPass(unittest.TestCase): state['upgraded'] = True self.assertFalse(check_password('WRONG', encoded, setter)) self.assertFalse(state['upgraded']) + + def test_load_library_no_algorithm(self): + with self.assertRaises(ValueError) as e: + BasePasswordHasher()._load_library() + self.assertEqual("Hasher 'BasePasswordHasher' doesn't specify a " + "library attribute", str(e.exception)) + + def test_load_library_importerror(self): + PlainHasher = type(str('PlainHasher'), (BasePasswordHasher,), + {'algorithm': 'plain', 'library': 'plain'}) + with self.assertRaises(ValueError) as e: + PlainHasher()._load_library() + self.assertEqual("Couldn't load 'PlainHasher' algorithm library: " + "No module named plain", str(e.exception)) |
