From 865a6650d0739e857fb605767afebdce4f8c54c3 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Mon, 26 Jun 2023 08:57:46 +0200 Subject: [PATCH] Moved http decorator tests into decorators/test_http.py. --- tests/decorators/test_http.py | 22 ++++++++++++++++++++++ tests/decorators/tests.py | 25 +------------------------ 2 files changed, 23 insertions(+), 24 deletions(-) create mode 100644 tests/decorators/test_http.py diff --git a/tests/decorators/test_http.py b/tests/decorators/test_http.py new file mode 100644 index 0000000000..344e66f417 --- /dev/null +++ b/tests/decorators/test_http.py @@ -0,0 +1,22 @@ +from django.http import HttpRequest, HttpResponse, HttpResponseNotAllowed +from django.test import SimpleTestCase +from django.views.decorators.http import require_safe + + +class RequireSafeDecoratorTest(SimpleTestCase): + def test_require_safe_accepts_only_safe_methods(self): + def my_view(request): + return HttpResponse("OK") + + my_safe_view = require_safe(my_view) + request = HttpRequest() + request.method = "GET" + self.assertIsInstance(my_safe_view(request), HttpResponse) + request.method = "HEAD" + self.assertIsInstance(my_safe_view(request), HttpResponse) + request.method = "POST" + self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed) + request.method = "PUT" + self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed) + request.method = "DELETE" + self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed) diff --git a/tests/decorators/tests.py b/tests/decorators/tests.py index 2d10c33188..58f822f2a5 100644 --- a/tests/decorators/tests.py +++ b/tests/decorators/tests.py @@ -7,7 +7,7 @@ from django.contrib.auth.decorators import ( permission_required, user_passes_test, ) -from django.http import HttpRequest, HttpResponse, HttpResponseNotAllowed +from django.http import HttpResponse from django.test import SimpleTestCase from django.utils.decorators import method_decorator from django.utils.functional import keep_lazy, keep_lazy_text, lazy @@ -117,29 +117,6 @@ class DecoratorsTest(TestCase): self.assertEqual(response, ["test2", "test1"]) - def test_require_safe_accepts_only_safe_methods(self): - """ - Test for the require_safe decorator. - A view returns either a response or an exception. - Refs #15637. - """ - - def my_view(request): - return HttpResponse("OK") - - my_safe_view = require_safe(my_view) - request = HttpRequest() - request.method = "GET" - self.assertIsInstance(my_safe_view(request), HttpResponse) - request.method = "HEAD" - self.assertIsInstance(my_safe_view(request), HttpResponse) - request.method = "POST" - self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed) - request.method = "PUT" - self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed) - request.method = "DELETE" - self.assertIsInstance(my_safe_view(request), HttpResponseNotAllowed) - # For testing method_decorator, a decorator that assumes a single argument. # We will get type arguments if there is a mismatch in the number of arguments.