this must be a transactiontestcase because the wsgi auth handler performs its own transaction management tests for the modwsgi authentication handler checkpassword returns the correct values as per https modwsgi readthedocs ioendevelopuserguidesaccesscontrolmechanisms htmlapacheauthenticationprovider user not in database valid user with correct password correct password but user is inactive valid user with incorrect password checkpassword returns the correct values as per https modwsgi readthedocs ioendevelopuserguidesaccesscontrolmechanisms htmlapacheauthenticationprovider with a custom user installed user not in database valid user with correct password valid user with incorrect password groupsforuser returns correct values as per https modwsgi readthedocs ioendevelopuserguidesaccesscontrolmechanisms htmlapachegroupisation user not in database this must be a transactiontestcase because the wsgi auth handler performs its own transaction management tests for the mod_wsgi authentication handler check_password returns the correct values as per https modwsgi readthedocs io en develop user guides access control mechanisms html apache authentication provider user not in database valid user with correct password correct password but user is inactive valid user with incorrect password check_password returns the correct values as per https modwsgi readthedocs io en develop user guides access control mechanisms html apache authentication provider with a custom user installed user not in database valid user with correct password valid user with incorrect password groups_for_user returns correct values as per https modwsgi readthedocs io en develop user guides access control mechanisms html apache group isation user not in database
from django.contrib.auth.handlers.modwsgi import check_password, groups_for_user
from django.contrib.auth.models import Group, User
from django.test import TransactionTestCase, override_settings

from .models import CustomUser




class ModWsgiHandlerTestCase(TransactionTestCase):
    

    available_apps = [
        "django.contrib.auth",
        "django.contrib.contenttypes",
        "auth_tests",
    ]

    def test_check_password(self):
        
        User.objects.create_user("test", "test@example.com", "test")

        
        self.assertIsNone(check_password({}, "unknown", ""))

        
        self.assertTrue(check_password({}, "test", "test"))

        
        User.objects.filter(username="test").update(is_active=False)
        self.assertFalse(check_password({}, "test", "test"))

        
        self.assertFalse(check_password({}, "test", "incorrect"))

    @override_settings(AUTH_USER_MODEL="auth_tests.CustomUser")
    def test_check_password_custom_user(self):
        
        CustomUser._default_manager.create_user(
            "test@example.com", "1990-01-01", "test"
        )

        
        self.assertIsNone(check_password({}, "unknown", ""))

        
        self.assertTrue(check_password({}, "test@example.com", "test"))

        
        self.assertFalse(check_password({}, "test@example.com", "incorrect"))

    def test_groups_for_user(self):
        
        user1 = User.objects.create_user("test", "test@example.com", "test")
        User.objects.create_user("test1", "test1@example.com", "test1")
        group = Group.objects.create(name="test_group")
        user1.groups.add(group)

        
        self.assertEqual(groups_for_user({}, "unknown"), [])

        self.assertEqual(groups_for_user({}, "test"), [b"test_group"])
        self.assertEqual(groups_for_user({}, "test1"), [])