mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Added more tests for decorators.
This commit is contained in:
		
				
					committed by
					
						 Mariusz Felisiak
						Mariusz Felisiak
					
				
			
			
				
	
			
			
			
						parent
						
							fe19b33e2f
						
					
				
				
					commit
					059cb0dbc9
				
			| @@ -1,8 +1,77 @@ | ||||
| from asgiref.sync import iscoroutinefunction | ||||
|  | ||||
| from django.conf import settings | ||||
| from django.http import HttpRequest, HttpResponse | ||||
| from django.test import SimpleTestCase | ||||
| from django.views.decorators.csrf import csrf_exempt | ||||
| from django.views.decorators.csrf import ( | ||||
|     csrf_exempt, | ||||
|     csrf_protect, | ||||
|     ensure_csrf_cookie, | ||||
|     requires_csrf_token, | ||||
| ) | ||||
|  | ||||
| CSRF_TOKEN = "1bcdefghij2bcdefghij3bcdefghij4bcdefghij5bcdefghij6bcdefghijABCD" | ||||
|  | ||||
|  | ||||
| class CsrfTestMixin: | ||||
|     def get_request(self, token=CSRF_TOKEN): | ||||
|         request = HttpRequest() | ||||
|         request.method = "POST" | ||||
|         if token: | ||||
|             request.POST["csrfmiddlewaretoken"] = token | ||||
|             request.COOKIES[settings.CSRF_COOKIE_NAME] = token | ||||
|         return request | ||||
|  | ||||
|  | ||||
| class CsrfProtectTests(CsrfTestMixin, SimpleTestCase): | ||||
|     def test_csrf_protect_decorator(self): | ||||
|         @csrf_protect | ||||
|         def sync_view(request): | ||||
|             return HttpResponse() | ||||
|  | ||||
|         request = self.get_request() | ||||
|         response = sync_view(request) | ||||
|         self.assertEqual(response.status_code, 200) | ||||
|         self.assertIs(request.csrf_processing_done, True) | ||||
|  | ||||
|         with self.assertLogs("django.security.csrf", "WARNING"): | ||||
|             request = self.get_request(token=None) | ||||
|             response = sync_view(request) | ||||
|             self.assertEqual(response.status_code, 403) | ||||
|  | ||||
|  | ||||
| class RequiresCsrfTokenTests(CsrfTestMixin, SimpleTestCase): | ||||
|     def test_requires_csrf_token_decorator(self): | ||||
|         @requires_csrf_token | ||||
|         def sync_view(request): | ||||
|             return HttpResponse() | ||||
|  | ||||
|         request = self.get_request() | ||||
|         response = sync_view(request) | ||||
|         self.assertEqual(response.status_code, 200) | ||||
|         self.assertIs(request.csrf_processing_done, True) | ||||
|  | ||||
|         with self.assertNoLogs("django.security.csrf", "WARNING"): | ||||
|             request = self.get_request(token=None) | ||||
|             response = sync_view(request) | ||||
|             self.assertEqual(response.status_code, 200) | ||||
|  | ||||
|  | ||||
| class EnsureCsrfCookieTests(CsrfTestMixin, SimpleTestCase): | ||||
|     def test_ensure_csrf_cookie_decorator(self): | ||||
|         @ensure_csrf_cookie | ||||
|         def sync_view(request): | ||||
|             return HttpResponse() | ||||
|  | ||||
|         request = self.get_request() | ||||
|         response = sync_view(request) | ||||
|         self.assertEqual(response.status_code, 200) | ||||
|         self.assertIs(request.csrf_processing_done, True) | ||||
|  | ||||
|         with self.assertNoLogs("django.security.csrf", "WARNING"): | ||||
|             request = self.get_request(token=None) | ||||
|             response = sync_view(request) | ||||
|             self.assertEqual(response.status_code, 200) | ||||
|  | ||||
|  | ||||
| class CsrfExemptTests(SimpleTestCase): | ||||
|   | ||||
							
								
								
									
										19
									
								
								tests/decorators/test_gzip.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								tests/decorators/test_gzip.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| from django.http import HttpRequest, HttpResponse | ||||
| from django.test import SimpleTestCase | ||||
| from django.views.decorators.gzip import gzip_page | ||||
|  | ||||
|  | ||||
| class GzipPageTests(SimpleTestCase): | ||||
|     # Gzip ignores content that is too short. | ||||
|     content = "Content " * 100 | ||||
|  | ||||
|     def test_gzip_page_decorator(self): | ||||
|         @gzip_page | ||||
|         def sync_view(request): | ||||
|             return HttpResponse(content=self.content) | ||||
|  | ||||
|         request = HttpRequest() | ||||
|         request.META["HTTP_ACCEPT_ENCODING"] = "gzip" | ||||
|         response = sync_view(request) | ||||
|         self.assertEqual(response.status_code, 200) | ||||
|         self.assertEqual(response.get("Content-Encoding"), "gzip") | ||||
| @@ -4,7 +4,12 @@ from asgiref.sync import iscoroutinefunction | ||||
|  | ||||
| from django.http import HttpRequest, HttpResponse, HttpResponseNotAllowed | ||||
| from django.test import SimpleTestCase | ||||
| from django.views.decorators.http import condition, require_http_methods, require_safe | ||||
| from django.views.decorators.http import ( | ||||
|     condition, | ||||
|     conditional_page, | ||||
|     require_http_methods, | ||||
|     require_safe, | ||||
| ) | ||||
|  | ||||
|  | ||||
| class RequireHttpMethodsTest(SimpleTestCase): | ||||
| @@ -155,3 +160,19 @@ class ConditionDecoratorTest(SimpleTestCase): | ||||
|             response.headers["Last-Modified"], | ||||
|             "Mon, 02 Jan 2023 23:21:47 GMT", | ||||
|         ) | ||||
|  | ||||
|  | ||||
| class ConditionalPageTests(SimpleTestCase): | ||||
|     def test_conditional_page_decorator_successful(self): | ||||
|         @conditional_page | ||||
|         def sync_view(request): | ||||
|             response = HttpResponse() | ||||
|             response.content = b"test" | ||||
|             response["Cache-Control"] = "public" | ||||
|             return response | ||||
|  | ||||
|         request = HttpRequest() | ||||
|         request.method = "GET" | ||||
|         response = sync_view(request) | ||||
|         self.assertEqual(response.status_code, 200) | ||||
|         self.assertIsNotNone(response.get("Etag")) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user