Skip to content

Commit

Permalink
Test HistoricalRecords.context.request is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
ddabble committed Aug 3, 2023
1 parent eee190c commit a7a30a6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
23 changes: 23 additions & 0 deletions simple_history/tests/tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from datetime import date
from unittest import mock

from django.http import HttpResponse
from django.test import TestCase, override_settings
from django.urls import reverse

from simple_history.models import HistoricalRecords
from simple_history.tests.custom_user.models import CustomUser
from simple_history.tests.models import (
BucketDataRegisterRequestUser,
Expand Down Expand Up @@ -146,6 +149,26 @@ def test_bucket_member_is_set_on_create_view_when_logged_in(self):

self.assertListEqual([h.history_user_id for h in history], [member1.id])

# The `request` attribute of `HistoricalRecords.context` should be deleted
# 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):
"""https://github.com/jazzband/django-simple-history/issues/1189"""
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"))

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"))


@override_settings(**middleware_override_settings)
class MiddlewareBulkOpsTest(TestCase):
Expand Down
2 changes: 2 additions & 0 deletions simple_history/tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from simple_history.tests.view import (
BucketDataRegisterRequestUserCreate,
BucketDataRegisterRequestUserDetail,
MockableView,
PollBulkCreateView,
PollBulkCreateWithDefaultUserView,
PollBulkUpdateView,
Expand Down Expand Up @@ -55,4 +56,5 @@
PollBulkUpdateWithDefaultUserView.as_view(),
name="poll-bulk-update-with-default-user",
),
path("mockable/", MockableView.as_view(), name="mockable"),
]
7 changes: 7 additions & 0 deletions simple_history/tests/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,10 @@ class BucketDataRegisterRequestUserCreate(CreateView):
class BucketDataRegisterRequestUserDetail(DetailView):
model = BucketDataRegisterRequestUser
fields = ["data"]


class MockableView(View):
"""This view exists to easily mock a response."""

def get(self, request, *args, **kwargs):
return HttpResponse(status=200)

0 comments on commit a7a30a6

Please sign in to comment.