summaryrefslogtreecommitdiff
path: root/passlib/tests
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2020-10-06 14:13:33 -0400
committerEli Collins <elic@assurancetechnologies.com>2020-10-06 14:13:33 -0400
commit65f4a25efd41639552df52dba40bfc377ae00871 (patch)
tree363bf55e05d7484d8b407e58fa2526719138b201 /passlib/tests
parentcc1c32da8866bdcc65bee916cd202f660b0cd88e (diff)
downloadpasslib-65f4a25efd41639552df52dba40bfc377ae00871.tar.gz
cleanup old python compat -- removed PY2 & PY3 conditionals
Diffstat (limited to 'passlib/tests')
-rw-r--r--passlib/tests/test_context.py21
-rw-r--r--passlib/tests/test_crypto_builtin_md4.py16
-rw-r--r--passlib/tests/test_crypto_digest.py9
-rw-r--r--passlib/tests/test_handlers.py10
-rw-r--r--passlib/tests/test_handlers_bcrypt.py2
-rw-r--r--passlib/tests/test_utils.py51
-rw-r--r--passlib/tests/utils.py28
7 files changed, 38 insertions, 99 deletions
diff --git a/passlib/tests/test_context.py b/passlib/tests/test_context.py
index 7e5996f..3459adc 100644
--- a/passlib/tests/test_context.py
+++ b/passlib/tests/test_context.py
@@ -3,11 +3,7 @@
# imports
#=============================================================================
# core
-from passlib.utils.compat import PY3
-if PY3:
- from configparser import NoSectionError
-else:
- from ConfigParser import NoSectionError
+from configparser import NoSectionError
import datetime
from functools import partial
import logging; log = logging.getLogger(__name__)
@@ -19,7 +15,7 @@ from passlib import hash
from passlib.context import CryptContext, LazyCryptContext
from passlib.exc import PasslibConfigWarning, PasslibHashWarning
from passlib.utils import tick, to_unicode
-from passlib.utils.compat import irange, unicode, str_to_uascii, PY2
+from passlib.utils.compat import irange, unicode, str_to_uascii
import passlib.utils.handlers as uh
from passlib.tests.utils import (TestCase, set_file, TICK_RESOLUTION,
quicksleep, time_call, handler_derived_from)
@@ -747,11 +743,6 @@ sha512_crypt__min_rounds = 45000
self.assertEqual(ctx.handler(category="admin", unconfigured=True), hash.md5_crypt)
self.assertHandlerDerivedFrom(ctx.handler(category="staff"), hash.sha256_crypt)
- # test unicode category strings are accepted under py2
- if PY2:
- self.assertEqual(ctx.handler(category=u"staff", unconfigured=True), hash.sha256_crypt)
- self.assertEqual(ctx.handler(category=u"admin", unconfigured=True), hash.md5_crypt)
-
def test_33_options(self):
"""test internal _get_record_options() method"""
@@ -928,14 +919,6 @@ sha512_crypt__min_rounds = 45000
# border cases
#--------------------------------------------------------------
- # test unicode category strings are accepted under py2
- # this tests basic _get_record() used by hash/genhash/verify.
- # we have to omit scheme=xxx so codepath is tested fully
- if PY2:
- c2 = cc.copy(default="phpass")
- self.assertTrue(c2.genconfig(category=u"admin").startswith("$P$5"))
- self.assertTrue(c2.genconfig(category=u"staff").startswith("$H$5"))
-
# throws error without schemes
self.assertRaises(KeyError, CryptContext().genconfig)
self.assertRaises(KeyError, CryptContext().genconfig, scheme='md5_crypt')
diff --git a/passlib/tests/test_crypto_builtin_md4.py b/passlib/tests/test_crypto_builtin_md4.py
index 2ee76b5..0610cb9 100644
--- a/passlib/tests/test_crypto_builtin_md4.py
+++ b/passlib/tests/test_crypto_builtin_md4.py
@@ -8,7 +8,7 @@ import hashlib
# site
# pkg
# module
-from passlib.utils.compat import bascii_to_str, PY3
+from passlib.utils.compat import bascii_to_str
from passlib.crypto.digest import lookup_hash
from passlib.tests.utils import TestCase, skipUnless
# local
@@ -61,16 +61,10 @@ class _Common_MD4_Test(TestCase):
h.update(b'bcdefghijklmnopqrstuvwxyz')
self.assertEqual(h.hexdigest(), "d79e1c308aa5bbcdeea8ed63df412da9")
- if PY3:
- # reject unicode, hash should return digest of b''
- h = md4()
- self.assertRaises(TypeError, h.update, u'a')
- self.assertEqual(h.hexdigest(), "31d6cfe0d16ae931b73c59d7e0c089c0")
- else:
- # coerce unicode to ascii, hash should return digest of b'a'
- h = md4()
- h.update(u'a')
- self.assertEqual(h.hexdigest(), "bde52cb31de33e46245e05fbdbd6fb24")
+ # reject unicode, hash should return digest of b''
+ h = md4()
+ self.assertRaises(TypeError, h.update, u'a')
+ self.assertEqual(h.hexdigest(), "31d6cfe0d16ae931b73c59d7e0c089c0")
def test_md4_hexdigest(self):
"""hexdigest() method"""
diff --git a/passlib/tests/test_crypto_digest.py b/passlib/tests/test_crypto_digest.py
index 4b33323..5f0dd40 100644
--- a/passlib/tests/test_crypto_digest.py
+++ b/passlib/tests/test_crypto_digest.py
@@ -10,7 +10,7 @@ import warnings
# pkg
# module
from passlib.exc import UnknownHashError
-from passlib.utils.compat import PY3, JYTHON
+from passlib.utils.compat import JYTHON
from passlib.tests.utils import TestCase, TEST_MODE, skipUnless, hb
#=============================================================================
@@ -496,12 +496,7 @@ class Pbkdf2Test(TestCase):
self.assertEqual("hashlib-ssl" in PBKDF2_BACKENDS, has_hashlib_ssl)
# check for appropriate builtin
- from passlib.utils.compat import PY3
- if PY3:
- self.assertIn("builtin-from-bytes", PBKDF2_BACKENDS)
- else:
- # XXX: only true as long as this is preferred over hexlify
- self.assertIn("builtin-unpack", PBKDF2_BACKENDS)
+ self.assertIn("builtin-from-bytes", PBKDF2_BACKENDS)
def test_border(self):
"""test border cases"""
diff --git a/passlib/tests/test_handlers.py b/passlib/tests/test_handlers.py
index cffa425..38836fa 100644
--- a/passlib/tests/test_handlers.py
+++ b/passlib/tests/test_handlers.py
@@ -11,7 +11,7 @@ import warnings
# pkg
from passlib import exc, hash
from passlib.utils import repeat_string
-from passlib.utils.compat import irange, PY3, get_method_function
+from passlib.utils.compat import irange, get_method_function
from passlib.tests.utils import TestCase, HandlerCase, skipUnless, \
TEST_MODE, UserHandlerMixin, EncodingHandlerMixin
# module
@@ -641,8 +641,8 @@ class ldap_plaintext_test(HandlerCase):
handler = hash.ldap_plaintext
known_correct_hashes = [
("password", 'password'),
- (UPASS_TABLE, UPASS_TABLE if PY3 else PASS_TABLE_UTF8),
- (PASS_TABLE_UTF8, UPASS_TABLE if PY3 else PASS_TABLE_UTF8),
+ (UPASS_TABLE, UPASS_TABLE),
+ (PASS_TABLE_UTF8, UPASS_TABLE),
]
known_unidentified_hashes = [
"{FOO}bar",
@@ -1321,8 +1321,8 @@ class plaintext_test(HandlerCase):
('password', 'password'),
# ensure unicode uses utf-8
- (UPASS_TABLE, UPASS_TABLE if PY3 else PASS_TABLE_UTF8),
- (PASS_TABLE_UTF8, UPASS_TABLE if PY3 else PASS_TABLE_UTF8),
+ (UPASS_TABLE, UPASS_TABLE),
+ (PASS_TABLE_UTF8, UPASS_TABLE),
]
#=============================================================================
diff --git a/passlib/tests/test_handlers_bcrypt.py b/passlib/tests/test_handlers_bcrypt.py
index 156604d..55d5203 100644
--- a/passlib/tests/test_handlers_bcrypt.py
+++ b/passlib/tests/test_handlers_bcrypt.py
@@ -11,7 +11,7 @@ import warnings
from passlib import hash
from passlib.handlers.bcrypt import IDENT_2, IDENT_2X
from passlib.utils import repeat_string, to_bytes, is_safe_crypt_input
-from passlib.utils.compat import irange, PY3
+from passlib.utils.compat import irange
from passlib.tests.utils import HandlerCase, TEST_MODE
from passlib.tests.test_handlers import UPASS_TABLE
# module
diff --git a/passlib/tests/test_utils.py b/passlib/tests/test_utils.py
index 83630b0..6790794 100644
--- a/passlib/tests/test_utils.py
+++ b/passlib/tests/test_utils.py
@@ -9,7 +9,7 @@ import warnings
# pkg
# module
from passlib.utils import is_ascii_safe, to_bytes
-from passlib.utils.compat import irange, PY2, PY3, unicode, join_bytes, PYPY
+from passlib.utils.compat import irange, unicode, join_bytes, PYPY
from passlib.tests.utils import TestCase, hb, run_with_fixed_seeds
#=============================================================================
@@ -80,10 +80,6 @@ class MiscTest(TestCase):
self.assertEqual(d.value, 0)
self.assertEqual(d.counter, 1)
- prop = dummy.value
- if not PY3:
- self.assertIs(prop.im_func, prop.__func__)
-
def test_getrandbytes(self):
"""getrandbytes()"""
from passlib.utils import getrandbytes
@@ -272,10 +268,9 @@ class MiscTest(TestCase):
self.assertRaises(TypeError, consteq, 1, b'')
def consteq_supports_string(value):
- # under PY2, it supports all unicode strings (when present at all),
- # under PY3, compare_digest() only supports ascii unicode strings.
- # confirmed for: cpython 2.7.9, cpython 3.4, pypy, pypy3, pyston
- return (consteq is str_consteq or PY2 or is_ascii_safe(value))
+ # compare_digest() only supports ascii unicode strings.
+ # confirmed for: cpython 3.4, pypy3, pyston
+ return (consteq is str_consteq or is_ascii_safe(value))
# check equal inputs compare correctly
for value in [
@@ -568,19 +563,12 @@ class CodecTest(TestCase):
def test_bytes(self):
"""test b() helper, bytes and native str type"""
- if PY3:
- import builtins
- self.assertIs(bytes, builtins.bytes)
- else:
- import __builtin__ as builtins
- self.assertIs(bytes, builtins.str)
+ import builtins
+ self.assertIs(bytes, builtins.bytes)
self.assertIsInstance(b'', bytes)
self.assertIsInstance(b'\x00\xff', bytes)
- if PY3:
- self.assertEqual(b'\x00\xff'.decode("latin-1"), "\x00\xff")
- else:
- self.assertEqual(b'\x00\xff', "\x00\xff")
+ self.assertEqual(b'\x00\xff'.decode("latin-1"), "\x00\xff")
def test_to_bytes(self):
"""test to_bytes()"""
@@ -642,24 +630,17 @@ class CodecTest(TestCase):
self.assertEqual(to_native_str(b'abc', 'ascii'), 'abc')
# test invalid ascii
- if PY3:
- self.assertEqual(to_native_str(u'\xE0', 'ascii'), '\xE0')
- self.assertRaises(UnicodeDecodeError, to_native_str, b'\xC3\xA0',
- 'ascii')
- else:
- self.assertRaises(UnicodeEncodeError, to_native_str, u'\xE0',
- 'ascii')
- self.assertEqual(to_native_str(b'\xC3\xA0', 'ascii'), '\xC3\xA0')
+ self.assertEqual(to_native_str(u'\xE0', 'ascii'), '\xE0')
+ self.assertRaises(UnicodeDecodeError, to_native_str, b'\xC3\xA0',
+ 'ascii')
# test latin-1
self.assertEqual(to_native_str(u'\xE0', 'latin-1'), '\xE0')
self.assertEqual(to_native_str(b'\xE0', 'latin-1'), '\xE0')
# test utf-8
- self.assertEqual(to_native_str(u'\xE0', 'utf-8'),
- '\xE0' if PY3 else '\xC3\xA0')
- self.assertEqual(to_native_str(b'\xC3\xA0', 'utf-8'),
- '\xE0' if PY3 else '\xC3\xA0')
+ self.assertEqual(to_native_str(u'\xE0', 'utf-8'), '\xE0')
+ self.assertEqual(to_native_str(b'\xC3\xA0', 'utf-8'), '\xE0')
# other types rejected
self.assertRaises(TypeError, to_native_str, None, 'ascii')
@@ -743,8 +724,7 @@ class Base64EngineTest(TestCase):
self.assertEqual(ab64_encode(hb("69b7")), b"abc")
# reject unicode
- self.assertRaises(TypeError if PY3 else UnicodeEncodeError,
- ab64_encode, hb("69b7").decode("latin-1"))
+ self.assertRaises(TypeError, ab64_encode, hb("69b7").decode("latin-1"))
# insert correct padding before decoding
self.assertEqual(ab64_encode(hb("69b71d")), b"abcd") # 0 mod 4
@@ -785,8 +765,7 @@ class Base64EngineTest(TestCase):
self.assertEqual(b64s_encode(hb("69b7")), b"abc")
# reject unicode
- self.assertRaises(TypeError if PY3 else UnicodeEncodeError,
- b64s_encode, hb("69b7").decode("latin-1"))
+ self.assertRaises(TypeError, b64s_encode, hb("69b7").decode("latin-1"))
# insert correct padding before decoding
self.assertEqual(b64s_encode(hb("69b71d")), b"abcd") # 0 mod 4
@@ -850,7 +829,7 @@ class _Base64Test(TestCase):
def test_decode_bytes_padding(self):
"""test decode_bytes() ignores padding bits"""
- bchr = (lambda v: bytes([v])) if PY3 else chr
+ bchr = lambda v: bytes([v])
engine = self.engine
m = self.m
decode = engine.decode_bytes
diff --git a/passlib/tests/utils.py b/passlib/tests/utils.py
index 9fb97f7..e55ce3e 100644
--- a/passlib/tests/utils.py
+++ b/passlib/tests/utils.py
@@ -17,7 +17,7 @@ import threading
import time
import unittest
from passlib.exc import PasslibHashWarning, PasslibConfigWarning
-from passlib.utils.compat import PY3, JYTHON
+from passlib.utils.compat import JYTHON
import warnings
from warnings import warn
# site
@@ -28,7 +28,7 @@ import passlib.registry as registry
from passlib.utils import has_rounds_info, has_salt_info, rounds_cost_values, \
rng as sys_rng, getrandstr, is_ascii_safe, to_native_str, \
repeat_string, tick, batch
-from passlib.utils.compat import iteritems, irange, unicode, PY2
+from passlib.utils.compat import iteritems, irange, unicode
from passlib.utils.decor import classproperty
import passlib.utils.handlers as uh
# local
@@ -230,15 +230,10 @@ def get_file(path):
def tonn(source):
"""convert native string to non-native string"""
- if not isinstance(source, str):
- return source
- elif PY3:
+ if isinstance(source, str):
return source.encode("utf-8")
else:
- try:
- return source.decode("utf-8")
- except UnicodeDecodeError:
- return source.decode("latin-1")
+ return source
def hb(source):
"""
@@ -672,8 +667,7 @@ class TestCase(unittest.TestCase):
return logger named after current test.
"""
cls = type(self)
- # NOTE: conditional on qualname for PY2 compat
- path = cls.__module__ + "." + getattr(cls, "__qualname__", cls.__name__)
+ path = cls.__module__ + "." + cls.__qualname__
name = self._testMethodName
if name:
path = path + "." + name
@@ -1470,9 +1464,8 @@ class HandlerCase(TestCase):
if salt_type is not unicode:
self.assertRaises(TypeError, self.do_encrypt, 'stub', salt=u'x' * salt_size)
- # bytes should be accepted only if salt_type is bytes,
- # OR if salt type is unicode and running PY2 - to allow native strings.
- if not (salt_type is bytes or (PY2 and salt_type is unicode)):
+ # bytes should be accepted only if salt_type is bytes
+ if salt_type is not bytes:
self.assertRaises(TypeError, self.do_encrypt, 'stub', salt=b'x' * salt_size)
def test_using_salt_size(self):
@@ -2303,7 +2296,7 @@ class HandlerCase(TestCase):
"""
check if we're expecting potential verify failure due to crypt.crypt() encoding limitation
"""
- if PY3 and self.backend == "os_crypt" and isinstance(secret, bytes):
+ if self.backend == "os_crypt" and isinstance(secret, bytes):
try:
secret.decode("utf-8")
except UnicodeDecodeError:
@@ -2575,11 +2568,6 @@ class HandlerCase(TestCase):
# but all else should be the same
result3 = handler.parsehash(hash, sanitize=True)
correct3 = result.copy()
- if PY2:
- # silence warning about bytes & unicode not comparing
- # (sanitize may convert bytes into base64 text)
- warnings.filterwarnings("ignore", ".*unequal comparison failed to convert.*",
- category=UnicodeWarning)
for key in ("salt", "checksum"):
if key in result3:
self.assertNotEqual(result3[key], correct3[key])