test_middleware.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import pytest
  2. from django.http import HttpResponse
  3. from fixtures.sudo_testutils import BaseTestCase
  4. from sentry.middleware.placeholder import placeholder_get_response
  5. from sudo.middleware import SudoMiddleware
  6. from sudo.settings import COOKIE_NAME
  7. from sudo.utils import grant_sudo_privileges, revoke_sudo_privileges
  8. class SudoMiddlewareTestCase(BaseTestCase):
  9. middleware = SudoMiddleware(placeholder_get_response)
  10. def assertSignedCookieEqual(self, v1, v2, reason=None):
  11. value, _, _ = v1.split(":")
  12. return self.assertEqual(value, v2, reason)
  13. def test_process_request_raises_without_session(self):
  14. del self.request.session
  15. with pytest.raises(AssertionError):
  16. self.middleware.process_request(self.request)
  17. def test_process_request_adds_is_sudo(self):
  18. self.middleware.process_request(self.request)
  19. self.assertFalse(self.request.is_sudo())
  20. def test_process_response_noop(self):
  21. response = self.middleware.process_response(self.request, HttpResponse())
  22. self.assertEqual(len(response.cookies.items()), 0)
  23. def test_process_response_with_sudo_sets_cookie(self):
  24. self.login()
  25. self.middleware.process_request(self.request)
  26. grant_sudo_privileges(self.request)
  27. response = self.middleware.process_response(self.request, HttpResponse())
  28. morsels = list(response.cookies.items())
  29. self.assertEqual(len(morsels), 1)
  30. self.assertEqual(morsels[0][0], COOKIE_NAME)
  31. _, sudo = morsels[0]
  32. self.assertEqual(sudo.key, COOKIE_NAME)
  33. self.assertSignedCookieEqual(sudo.value, self.request._sudo_token)
  34. self.assertEqual(sudo["max-age"], self.request._sudo_max_age)
  35. self.assertTrue(sudo["httponly"])
  36. # Asserting that these are insecure together explicitly
  37. # since it's a big deal to not bungle.
  38. self.assertFalse(self.request.is_secure())
  39. self.assertFalse(sudo["secure"]) # insecure request
  40. def test_process_response_sets_secure_cookie(self):
  41. self.login()
  42. self.request.is_secure = lambda: True
  43. self.middleware.process_request(self.request)
  44. grant_sudo_privileges(self.request)
  45. response = self.middleware.process_response(self.request, HttpResponse())
  46. morsels = list(response.cookies.items())
  47. self.assertEqual(len(morsels), 1)
  48. self.assertEqual(morsels[0][0], COOKIE_NAME)
  49. _, sudo = morsels[0]
  50. self.assertTrue(self.request.is_secure())
  51. # XXX: Even if sudo.settings.COOKIE_SECURE is patched to be None
  52. # from False (from sentry initializer), we need to move the import
  53. # into the middleware's process_response rather than at module level.
  54. # self.assertTrue(sudo["secure"])
  55. def test_process_response_sudo_revoked_removes_cookie(self):
  56. self.login()
  57. self.middleware.process_request(self.request)
  58. grant_sudo_privileges(self.request)
  59. self.request.COOKIES[COOKIE_NAME] = self.request._sudo_token
  60. revoke_sudo_privileges(self.request)
  61. response = self.middleware.process_response(self.request, HttpResponse())
  62. morsels = list(response.cookies.items())
  63. self.assertEqual(len(morsels), 1)
  64. self.assertEqual(morsels[0][0], COOKIE_NAME)
  65. _, sudo = morsels[0]
  66. # Deleting a cookie is just setting it's value to empty
  67. # and telling it to expire
  68. self.assertEqual(sudo.key, COOKIE_NAME)
  69. self.assertFalse(sudo.value)
  70. self.assertEqual(sudo["max-age"], 0)
  71. def test_process_response_sudo_revoked_without_cookie(self):
  72. self.login()
  73. self.middleware.process_request(self.request)
  74. grant_sudo_privileges(self.request)
  75. revoke_sudo_privileges(self.request)
  76. response = self.middleware.process_response(self.request, HttpResponse())
  77. morsels = list(response.cookies.items())
  78. self.assertEqual(len(morsels), 0)