test_handlers.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.contrib.auth.tests.custom_user import CustomUser
  7. from django.test import TransactionTestCase, override_settings
  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. ]
  18. def test_check_password(self):
  19. """
  20. Verify that check_password returns the correct values as per
  21. http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Authentication_Provider
  22. """
  23. User.objects.create_user('test', 'test@example.com', 'test')
  24. # User not in database
  25. self.assertIsNone(check_password({}, 'unknown', ''))
  26. # Valid user with correct password
  27. self.assertTrue(check_password({}, 'test', 'test'))
  28. # correct password, but user is inactive
  29. User.objects.filter(username='test').update(is_active=False)
  30. self.assertFalse(check_password({}, 'test', 'test'))
  31. # Valid user with incorrect password
  32. self.assertFalse(check_password({}, 'test', 'incorrect'))
  33. @override_settings(AUTH_USER_MODEL='auth.CustomUser')
  34. def test_check_password_custom_user(self):
  35. """
  36. Verify that check_password returns the correct values as per
  37. http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Authentication_Provider
  38. with custom user installed
  39. """
  40. CustomUser._default_manager.create_user('test@example.com', '1990-01-01', 'test')
  41. # User not in database
  42. self.assertIsNone(check_password({}, 'unknown', ''))
  43. # Valid user with correct password'
  44. self.assertTrue(check_password({}, 'test@example.com', 'test'))
  45. # Valid user with incorrect password
  46. self.assertFalse(check_password({}, 'test@example.com', 'incorrect'))
  47. def test_groups_for_user(self):
  48. """
  49. Check that groups_for_user returns correct values as per
  50. http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Group_Authorisation
  51. """
  52. user1 = User.objects.create_user('test', 'test@example.com', 'test')
  53. User.objects.create_user('test1', 'test1@example.com', 'test1')
  54. group = Group.objects.create(name='test_group')
  55. user1.groups.add(group)
  56. # User not in database
  57. self.assertEqual(groups_for_user({}, 'unknown'), [])
  58. self.assertEqual(groups_for_user({}, 'test'), [b'test_group'])
  59. self.assertEqual(groups_for_user({}, 'test1'), [])