Skip to content

Commit

Permalink
Refactored HistoricalRecords.context.request test
Browse files Browse the repository at this point in the history
Now it also tests that the `request` attribute of
`HistoricalRecords.context` actually exists while handling each request.
  • Loading branch information
ddabble committed Aug 4, 2023
1 parent 6c9aad3 commit 3f46755
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions simple_history/tests/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,33 @@ def test_bucket_member_is_set_on_create_view_when_logged_in(self):
# even if this setting is set to `True`
@override_settings(DEBUG_PROPAGATE_EXCEPTIONS=True)
@mock.patch("simple_history.tests.view.MockableView.get")
def test_previous_request_object_is_deleted(self, func_mock):
def test_request_attr_is_deleted_after_each_response(self, func_mock):
"""https://github.com/jazzband/django-simple-history/issues/1189"""

def assert_has_request_attr(has_attr: bool):
self.assertEqual(hasattr(HistoricalRecords.context, "request"), has_attr)

def mocked_get(*args, **kwargs):
assert_has_request_attr(True)
response_ = HttpResponse(status=200)
response_.historical_records_request = HistoricalRecords.context.request
return response_

func_mock.side_effect = mocked_get
self.client.force_login(self.user)
mockable_url = reverse("mockable")
func_mock.return_value = HttpResponse(status=200)

self.assertFalse(hasattr(HistoricalRecords.context, "request"))
self.client.get(mockable_url)
self.assertFalse(hasattr(HistoricalRecords.context, "request"))
assert_has_request_attr(False)
response = self.client.get(mockable_url)
assert_has_request_attr(False)
# Check that the `request` attr existed while handling the request
self.assertEqual(response.historical_records_request.user, self.user)

func_mock.side_effect = RuntimeError()
with self.assertRaises(RuntimeError):
self.client.get(mockable_url)
# The request variable should be deleted even if an exception was raised
self.assertFalse(hasattr(HistoricalRecords.context, "request"))
assert_has_request_attr(False)


@override_settings(**middleware_override_settings)
Expand Down

0 comments on commit 3f46755

Please sign in to comment.