summaryrefslogtreecommitdiff
path: root/passlib/tests
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2011-08-12 19:35:08 -0400
committerEli Collins <elic@assurancetechnologies.com>2011-08-12 19:35:08 -0400
commit0b0a6972743d9da344beb17cb9df03ea083cd082 (patch)
tree5dc6deb1fba9c099a3913da591c5957038214529 /passlib/tests
parent4cb8a444b8566c273632a07e04eb44e6b928b5e7 (diff)
downloadpasslib-0b0a6972743d9da344beb17cb9df03ea083cd082.tar.gz
unittest fixes
* backport of ut2's assertAlmostEquals delta kwd * test_context uses set_file so we can always write bytes
Diffstat (limited to 'passlib/tests')
-rw-r--r--passlib/tests/test_apache.py12
-rw-r--r--passlib/tests/test_apps.py4
-rw-r--r--passlib/tests/test_context.py22
-rw-r--r--passlib/tests/utils.py55
4 files changed, 66 insertions, 27 deletions
diff --git a/passlib/tests/test_apache.py b/passlib/tests/test_apache.py
index 253487e..3aec67c 100644
--- a/passlib/tests/test_apache.py
+++ b/passlib/tests/test_apache.py
@@ -12,20 +12,10 @@ import time
#pkg
from passlib import apache
from passlib.utils import b, native_str, bytes
-from passlib.tests.utils import TestCase, mktemp, gae_env
+from passlib.tests.utils import TestCase, mktemp, gae_env, get_file, set_file
#module
log = getLogger(__name__)
-def set_file(path, content):
- if isinstance(content, unicode):
- content = content.encode("utf-8")
- with open(path, "wb") as fh:
- fh.write(content)
-
-def get_file(path):
- with open(path, "rb") as fh:
- return fh.read()
-
def backdate_file_mtime(path, offset=10):
"backdate file's mtime by specified amount"
#NOTE: this is used so we can test code which detects mtime changes,
diff --git a/passlib/tests/test_apps.py b/passlib/tests/test_apps.py
index 1c9f736..d48654d 100644
--- a/passlib/tests/test_apps.py
+++ b/passlib/tests/test_apps.py
@@ -25,7 +25,7 @@ class AppsTest(TestCase):
def test_custom_app_context(self):
ctx = apps.custom_app_context
- self.assertEquals(ctx.policy.schemes(), ["sha512_crypt", "sha256_crypt"])
+ self.assertEqual(ctx.policy.schemes(), ["sha512_crypt", "sha256_crypt"])
for hash in [
('$6$rounds=41128$VoQLvDjkaZ6L6BIE$4pt.1Ll1XdDYduEwEYPCMOBiR6W6'
'znsyUEoNlcVXpv2gKKIbQolgmTGe6uEEVJ7azUxuc8Tf7zV9SD2z7Ij751'),
@@ -44,7 +44,7 @@ class AppsTest(TestCase):
]:
self.assertTrue(ctx.verify("test", hash))
- self.assertEquals(ctx.identify("!"), "django_disabled")
+ self.assertEqual(ctx.identify("!"), "django_disabled")
self.assertFalse(ctx.verify("test", "!"))
def test_ldap_nocrypt_context(self):
diff --git a/passlib/tests/test_context.py b/passlib/tests/test_context.py
index 0907600..16effa4 100644
--- a/passlib/tests/test_context.py
+++ b/passlib/tests/test_context.py
@@ -20,9 +20,10 @@ from passlib import hash
from passlib.context import CryptContext, CryptPolicy, LazyCryptContext
from passlib.utils import to_bytes, to_unicode
import passlib.utils.handlers as uh
-from passlib.tests.utils import TestCase, mktemp, catch_warnings, gae_env
+from passlib.tests.utils import TestCase, mktemp, catch_warnings, \
+ gae_env, set_file
from passlib.registry import register_crypt_handler_path, has_crypt_handler, \
- _unload_handler_name as unload_handler_name
+ _unload_handler_name as unload_handler_name
#module
log = getLogger(__name__)
@@ -209,27 +210,24 @@ admin.sha512_crypt.max_rounds = 40000
def test_01_from_path(self):
"test CryptPolicy.from_path() constructor with encodings"
- if not gae_env:
+ if gae_env:
return self.skipTest("GAE doesn't offer read/write filesystem access")
path = mktemp()
#test "\n" linesep
- with open(path, "wb") as fh:
- fh.write(self.sample_config_1s)
+ set_file(path, self.sample_config_1s)
policy = CryptPolicy.from_path(path)
self.assertEqual(policy.to_dict(), self.sample_config_1pd)
#test "\r\n" linesep
- with open(path, "wb") as fh:
- fh.write(self.sample_config_1s.replace("\n","\r\n"))
+ set_file(path, self.sample_config_1s.replace("\n","\r\n"))
policy = CryptPolicy.from_path(path)
self.assertEqual(policy.to_dict(), self.sample_config_1pd)
#test with custom encoding
uc2 = to_bytes(self.sample_config_1s, "utf-16", source_encoding="utf-8")
- with open(path, "wb") as fh:
- fh.write(uc2)
+ set_file(path, uc2)
policy = CryptPolicy.from_path(path, encoding="utf-16")
self.assertEqual(policy.to_dict(), self.sample_config_1pd)
@@ -787,18 +785,18 @@ class CryptContextTest(TestCase):
#verify hashing works
TimedHash.delay = .05
elapsed, _ = timecall(TimedHash.genhash, 'stub', 'stub')
- self.assertAlmostEquals(elapsed, .05, delta=.01)
+ self.assertAlmostEqual(elapsed, .05, delta=.01)
#ensure min verify time is honored
elapsed, _ = timecall(cc.verify, "stub", "stub")
- self.assertAlmostEquals(elapsed, .1, delta=.01)
+ self.assertAlmostEqual(elapsed, .1, delta=.01)
#ensure taking longer emits a warning.
TimedHash.delay = .15
with catch_warnings(record=True) as wlog:
warnings.simplefilter("always")
elapsed, _ = timecall(cc.verify, "stub", "stub")
- self.assertAlmostEquals(elapsed, .15, delta=.01)
+ self.assertAlmostEqual(elapsed, .15, delta=.01)
self.assertEqual(len(wlog), 1)
self.assertWarningMatches(wlog[0],
message_re="CryptContext: verify exceeded min_verify_time")
diff --git a/passlib/tests/utils.py b/passlib/tests/utils.py
index ab8f515..abd809b 100644
--- a/passlib/tests/utils.py
+++ b/passlib/tests/utils.py
@@ -2,6 +2,7 @@
#=========================================================
#imports
#=========================================================
+from __future__ import with_statement
#core
import atexit
import logging; log = logging.getLogger(__name__)
@@ -41,6 +42,7 @@ __all__ = [
#util funcs
'enable_option',
'Params',
+ 'set_file', 'get_file',
#unit testing
'TestCase',
@@ -61,7 +63,6 @@ except ImportError:
gae_env = False
else:
gae_env = True
-assert gae_env
#=========================================================
#option flags
@@ -114,6 +115,18 @@ class Params(object):
txt = txt[:-2]
return txt
+def set_file(path, content):
+ "set file to specified bytes"
+ if isinstance(content, unicode):
+ content = content.encode("utf-8")
+ with open(path, "wb") as fh:
+ fh.write(content)
+
+def get_file(path):
+ "read file as bytes"
+ with open(path, "rb") as fh:
+ return fh.read()
+
#=========================================================
#custom test base
#=========================================================
@@ -217,7 +230,7 @@ class TestCase(unittest.TestCase):
raise self.failureException(msg)
#===============================================================
- #add some extra methods (these are already present in unittest2)
+ #backport some methods from unittest2
#===============================================================
if ut_version < 2:
@@ -242,6 +255,44 @@ class TestCase(unittest.TestCase):
def skipTest(self, reason):
raise SkipTest(reason)
+ def assertAlmostEqual(self, first, second, places=None, msg=None, delta=None):
+ """Fail if the two objects are unequal as determined by their
+ difference rounded to the given number of decimal places
+ (default 7) and comparing to zero, or by comparing that the
+ between the two objects is more than the given delta.
+
+ Note that decimal places (from zero) are usually not the same
+ as significant digits (measured from the most signficant digit).
+
+ If the two objects compare equal then they will automatically
+ compare almost equal.
+ """
+ if first == second:
+ # shortcut
+ return
+ if delta is not None and places is not None:
+ raise TypeError("specify delta or places not both")
+
+ if delta is not None:
+ if abs(first - second) <= delta:
+ return
+
+ standardMsg = '%s != %s within %s delta' % (repr(first),
+ repr(second),
+ repr(delta))
+ else:
+ if places is None:
+ places = 7
+
+ if round(abs(second-first), places) == 0:
+ return
+
+ standardMsg = '%s != %s within %r places' % (repr(first),
+ repr(second),
+ places)
+ msg = self._formatMessage(msg, standardMsg)
+ raise self.failureException(msg)
+
#============================================================
#add some custom methods
#============================================================