1
  C   ˆl–ß=¿Jå5*�vãm\dJbÑ¿�þY$bY¸Â"<ã.´àüÿÂ„m²÷Ç_ŠI|¥�+Wé3ž¯ÆÅ <²    """passlib.tests.test_handlers_django - tests for passlib hash algorithms"""
#=============================================================================
# imports
#=============================================================================
from __future__ import with_statement
# core
import logging; log = logging.getLogger(__name__)
import re
import warnings
# site
# pkg
from passlib import hash
from passlib.utils import repeat_string
from passlib.utils.compat import u
from passlib.tests.utils import TestCase, HandlerCase, skipUnless, SkipTest
from passlib.tests.test_handlers import UPASS_USD, UPASS_TABLE
from passlib.tests.test_ext_django import DJANGO_VERSION, MIN_DJANGO_VERSION, \
    check_django_hasher_has_backend
# module

#=============================================================================
# django
#=============================================================================

# standard string django uses
UPASS_LETMEIN = u('l\xe8tmein')

def vstr(version):
    return ".".join(str(e) for e in version)

class _DjangoHelper(TestCase):
    """
    mixin for HandlerCase subclasses that are testing a hasher
    which is also present in django.
    """
    __unittest_skip = True

    #: minimum django version where hash alg is present / that we support testing against
    min_django_version = MIN_DJANGO_VERSION

    #: max django version where hash alg is present
    #: TODO: for a bunch of the tests below, this is just max version where
    #:       settings.PASSWORD_HASHERS includes it by default -- could add helper to patch
    #:       desired django hasher back in for duration of test.
    #: XXX: change this to "disabled_in_django_version" instead?
    max_django_version = None

    def _require_django_support(self):
        # make sure min django version
        if DJANGO_VERSION < self.min_django_version:
            raise self.skipTest("Django >= %s not installed" % vstr(self.min_django_version))
        if self.max_django_version and DJANGO_VERSION > self.max_django_version:
            raise self.skipTest("Django <= %s not installed" % vstr(self.max_django_version))

        # make sure django has a backend for specified hasher
        name = self.handler.django_name
        if not check_django_hasher_has_backend(name):
            raise self.skipTest('django hasher %r not available' % name)

        return True

    extra_fuzz_verifiers = HandlerCase.fuzz_verifiers + (
        "fuzz_verifier_django",
    )

    def fuzz_verifier_django(self):
        try:
            self._require_django_support()
        except SkipTest:
            return None
        from django.contrib.auth.hashers import check_password

        def verify_django(secret, hash):
            """django/check_password"""
            if self.handler.name == "django_bcrypt" and hash.startswith("bcrypt$$2y$"):
                hash = hash.replace("$$2y$", "$$2a$")
            if isinstance(secret, bytes):
                secret = secret.decode("utf-8")
            return check_password(secret, hash)
        return verify_django

    def test_90_django_reference(self):
        """run known correct hashes through Django's check_password()"""
        self._require_django_support()
        # XXX: esp. when it's no longer supported by django,
        #      should verify it's *NOT* recognized
        from django.contrib.auth.hashers import check_password
        assert self.known_correct_hashes
        for secret, hash in self.iter_known_hashes():
            self.assertTrue(check_password(secret, hash),
                            "secret=%r hash=%r failed to verify" %
                            (secret, hash))
            self.assertFalse(check_password('x' + secret, hash),
                            "mangled secret=%r hash=%r incorrect verified" %
                            (secret, hash))

    def test_91_django_generation(self):
        """test against output of Django's make_password()"""
        self._require_django_support()
        # XXX: