test_handlers.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from __future__ import unicode_literals
  2. from django.contrib.auth.handlers.modwsgi import (
  3. check_password, groups_for_user,
  4. )
  5. from django.contrib.auth.models import Group, User
  6. from django.test import TransactionTestCase, override_settings
  7. from .models import CustomUser
  8. # This must be a TransactionTestCase because the WSGI auth handler performs
  9. # its own transaction management.
  10. class ModWsgiHandlerTestCase(TransactionTestCase):
  11. """
  12. Tests for the mod_wsgi authentication handler
  13. """
  14. available_apps = [
  15. 'django.contrib.auth',
  16. 'django.contrib.contenttypes',
  17. 'auth_tests',
  18. ]
  19. def test_check_password(self):
  20. """
  21. Verify that check_password returns the correct values as per
  22. https://modwsgi.readthedocs.org/en/develop/user-guides/access-control-mechanisms.html#apache-authentication-provider
  23. """
  24. User.objects.create_user('test', 'test@example.com', 'test')
  25. # User not in database
  26. self.assertIsNone(check_password({}, 'unknown', ''))
  27. # Valid user with correct password
  28. self.assertTrue(check_password({}, 'test', 'test'))
  29. # correct password, but user is inactive
  30. User.objects.filter(username='test').update(is_active=False)
  31. self.assertFalse(check_password({}, 'test', 'test'))
  32. # Valid user with incorrect password
  33. self.assertFalse(check_password({}, 'test', 'incorrect'))
  34. @override_settings(AUTH_USER_MODEL='auth_tests.CustomUser')
  35. def test_check_password_custom_user(self):
  36. """
  37. Verify that check_password returns the correct values as per
  38. https://modwsgi.readthedocs.org/en/develop/user-guides/access-control-mechanisms.html#apache-authentication-provider
  39. with custom user installed
  40. """
  41. CustomUser._default_manager.create_user('test@example.com', '1990-01-01', 'test')
  42. # User not in database
  43. self.assertIsNone(check_password({}, 'unknown', ''))
  44. # Valid user with correct password'
  45. self.assertTrue(check_password({}, 'test@example.com', 'test'))
  46. # Valid user with incorrect password
  47. self.assertFalse(check_password({}, 'test@example.com', 'incorrect'))
  48. def test_groups_for_user(self):
  49. """
  50. Check that groups_for_user returns correct values as per
  51. https://modwsgi.readthedocs.org/en/develop/user-guides/access-control-mechanisms.html#apache-group-authorisation
  52. """
  53. user1 = User.objects.create_user('test', 'test@example.com', 'test')
  54. User.objects.create_user('test1', 'test1@example.com', 'test1')
  55. group = Group.objects.create(name='test_group')
  56. user1.groups.add(group)
  57. # User not in database
  58. self.assertEqual(groups_for_user({}, 'unknown'), [])
  59. self.assertEqual(groups_for_user({}, 'test'), [b'test_group'])
  60. self.assertEqual(groups_for_user({}, 'test1'), [])