You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1.1 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. from unittest import mock
  2. from django.contrib.auth.models import User
  3. from django.test import TestCase, override_settings
  4. from api.middleware import HeaderAuthMiddleware
  5. @override_settings(HEADER_AUTH_USER_GROUPS="X-AuthProxy-Groups")
  6. @override_settings(HEADER_AUTH_ADMIN_GROUP_NAME="Admin")
  7. @override_settings(HEADER_AUTH_GROUPS_SEPERATOR=";")
  8. class HeaderAuthMiddlewareTest(TestCase):
  9. def test_process_user_groups_is_super(self):
  10. user = User.objects.create_user(username="TestUser")
  11. user.is_superuser = False
  12. get_response = mock.MagicMock()
  13. middleware = HeaderAuthMiddleware(get_response)
  14. middleware.process_user_groups(user, {"HTTP_X_AUTHPROXY_GROUPS": "Admin;Reader"})
  15. self.assertTrue(user.is_superuser)
  16. def test_process_user_groups_is_not_super(self):
  17. user = User.objects.create_user(username="TestUser")
  18. user.is_superuser = True
  19. get_response = mock.MagicMock()
  20. middleware = HeaderAuthMiddleware(get_response)
  21. middleware.process_user_groups(user, {"HTTP_X_AUTHPROXY_GROUPS": "Guest;Reader"})
  22. self.assertFalse(user.is_superuser)