# Path: user_management/models/tests/factories.py
# class UserFactory(factory.DjangoModelFactory):
#     name = factory.Sequence('Test User {}'.format)
#     email = factory.Sequence('email{}@example.com'.format)
#     is_active = True
# 
#     class Meta:
#         model = get_user_model()
# 
#     @factory.post_generation
#     def password(self, create, extracted='default password', **kwargs):
#         self.raw_password = extracted
#         self.set_password(self.raw_password)
#         if create:
#             self.save()
# 
# Path: user_management/models/backends.py
# class CaseInsensitiveEmailBackend(ModelBackend):
#     def authenticate(self, request, username=None, password=None, **kwargs):
#         UserModel = get_user_model()
#         if username is None:
#             username = kwargs.get(UserModel.USERNAME_FIELD)
# 
#         username_field = UserModel.USERNAME_FIELD + '__iexact'
#         try:
#             user = UserModel.objects.get(**{username_field: username})
#         except UserModel.DoesNotExist:
#             # Run the default password hasher once to reduce the timing
#             # difference between an existing and a non-existing user (#20760).
#             UserModel().set_password(password)
#         else:
#             if user.check_password(password) and self.user_can_authenticate(user):
#                 return user


from django.test import TestCase
from .factories import UserFactory
from ..backends import CaseInsensitiveEmailBackend



class CaseInsensitveEmailBackendTest(TestCase):
    request = 'any'

    def test_authenticate(self):
        """
        Check case-insensitive username authentication
        """

        email = 'test-Email@example.com'
        password = 'arandomsuperstrongpassword'


# Path: user_management/models/tests/factories.py
# class UserFactory(factory.DjangoModelFactory):
#     name = factory.Sequence('Test User {}'.format)
#     email = factory.Sequence('email{}@example.com'.format)
#     is_active = True
# 
#     class Meta:
#         model = get_user_model()
# 
#     @factory.post_generation
#     def password(self, create, extracted='default password', **kwargs):
#         self.raw_password = extracted
#         self.set_password(self.raw_password)
#         if create:
#             self.save()
# 
# Path: user_management/models/backends.py
# class CaseInsensitiveEmailBackend(ModelBackend):
#     def authenticate(self, request, username=None, password=None, **kwargs):
#         UserModel = get_user_model()
#         if username is None:
#             username = kwargs.get(UserModel.USERNAME_FIELD)
# 
#         username_field = UserModel.USERNAME_FIELD + '__iexact'
#         try:
#             user = UserModel.objects.get(**{username_field: username})
#         except UserModel.DoesNotExist:
#             # Run the default password hasher once to reduce the timing
#             # difference between an existing and a non-existing user (#20760).
#             UserModel().set_password(password)
#         else:
#             if user.check_password(password) and self.user_can_authenticate(user):
#                 return user


# Path: user_management/models/tests/test_backends.py
from django.test import TestCase
from .factories import UserFactory
from ..backends import CaseInsensitiveEmailBackend




class CaseInsensitveEmailBackendTest(TestCase):
    request = 'any'

    def test_authenticate(self):
        """
        Check case-insensitive username authentication
        """

        email = 'test-Email@example.com'
        password = 'arandomsuperstrongpassword'


        user = UserFactory.create(email=email, password=password, is_active=True)