From 5bb6301b7f53e3815ab1a81a5fa38721dc95b113 Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Thu, 2 Feb 2012 18:19:44 +0200 Subject: Response as a subclass of HttpResponse - first draft, not quite there yet. --- djangorestframework/tests/throttling.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'djangorestframework/tests/throttling.py') diff --git a/djangorestframework/tests/throttling.py b/djangorestframework/tests/throttling.py index 7fdc6491..393c3ec8 100644 --- a/djangorestframework/tests/throttling.py +++ b/djangorestframework/tests/throttling.py @@ -10,13 +10,14 @@ from djangorestframework.compat import RequestFactory from djangorestframework.views import View from djangorestframework.permissions import PerUserThrottling, PerViewThrottling, PerResourceThrottling from djangorestframework.resources import FormResource +from djangorestframework.response import Response class MockView(View): permissions = ( PerUserThrottling, ) throttle = '3/sec' def get(self, request): - return 'foo' + return Response('foo') class MockView_PerViewThrottling(MockView): permissions = ( PerViewThrottling, ) -- cgit v1.2.3 From afd490238a38c5445013f030547b1019f484f0bc Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Thu, 23 Feb 2012 22:47:45 +0200 Subject: authentication refactor : request.user + tests pass --- djangorestframework/tests/throttling.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'djangorestframework/tests/throttling.py') diff --git a/djangorestframework/tests/throttling.py b/djangorestframework/tests/throttling.py index 393c3ec8..73a4c02b 100644 --- a/djangorestframework/tests/throttling.py +++ b/djangorestframework/tests/throttling.py @@ -13,17 +13,17 @@ from djangorestframework.resources import FormResource from djangorestframework.response import Response class MockView(View): - permissions = ( PerUserThrottling, ) + permissions_classes = ( PerUserThrottling, ) throttle = '3/sec' def get(self, request): return Response('foo') class MockView_PerViewThrottling(MockView): - permissions = ( PerViewThrottling, ) + permissions_classes = ( PerViewThrottling, ) class MockView_PerResourceThrottling(MockView): - permissions = ( PerResourceThrottling, ) + permissions_classes = ( PerResourceThrottling, ) resource = FormResource class MockView_MinuteThrottling(MockView): @@ -54,7 +54,7 @@ class ThrottlingTests(TestCase): """ Explicitly set the timer, overriding time.time() """ - view.permissions[0].timer = lambda self: value + view.permissions_classes[0].timer = lambda self: value def test_request_throttling_expires(self): """ -- cgit v1.2.3 From 87b363f7bc5f73d850df123a61895d65ec0b05e7 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 24 Aug 2012 20:57:10 +0100 Subject: Remove PermissionsMixin --- djangorestframework/tests/throttling.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'djangorestframework/tests/throttling.py') diff --git a/djangorestframework/tests/throttling.py b/djangorestframework/tests/throttling.py index 73a4c02b..8c5457d3 100644 --- a/djangorestframework/tests/throttling.py +++ b/djangorestframework/tests/throttling.py @@ -12,25 +12,28 @@ from djangorestframework.permissions import PerUserThrottling, PerViewThrottling from djangorestframework.resources import FormResource from djangorestframework.response import Response + class MockView(View): - permissions_classes = ( PerUserThrottling, ) + permission_classes = (PerUserThrottling,) throttle = '3/sec' def get(self, request): return Response('foo') + class MockView_PerViewThrottling(MockView): - permissions_classes = ( PerViewThrottling, ) + permission_classes = (PerViewThrottling,) + class MockView_PerResourceThrottling(MockView): - permissions_classes = ( PerResourceThrottling, ) + permission_classes = (PerResourceThrottling,) resource = FormResource + class MockView_MinuteThrottling(MockView): throttle = '3/min' - class ThrottlingTests(TestCase): urls = 'djangorestframework.tests.throttling' @@ -54,7 +57,7 @@ class ThrottlingTests(TestCase): """ Explicitly set the timer, overriding time.time() """ - view.permissions_classes[0].timer = lambda self: value + view.permission_classes[0].timer = lambda self: value def test_request_throttling_expires(self): """ @@ -101,7 +104,6 @@ class ThrottlingTests(TestCase): """ self.ensure_is_throttled(MockView_PerResourceThrottling, 503) - def ensure_response_header_contains_proper_throttle_field(self, view, expected_headers): """ Ensure the response returns an X-Throttle field with status and next attributes -- cgit v1.2.3 From aed26b218ea39110489e85abc6f412399a1774a1 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 24 Aug 2012 22:11:00 +0100 Subject: Drop out resources & mixins --- djangorestframework/tests/throttling.py | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) (limited to 'djangorestframework/tests/throttling.py') diff --git a/djangorestframework/tests/throttling.py b/djangorestframework/tests/throttling.py index 8c5457d3..d307cd32 100644 --- a/djangorestframework/tests/throttling.py +++ b/djangorestframework/tests/throttling.py @@ -8,8 +8,7 @@ from django.core.cache import cache from djangorestframework.compat import RequestFactory from djangorestframework.views import View -from djangorestframework.permissions import PerUserThrottling, PerViewThrottling, PerResourceThrottling -from djangorestframework.resources import FormResource +from djangorestframework.permissions import PerUserThrottling, PerViewThrottling from djangorestframework.response import Response @@ -25,11 +24,6 @@ class MockView_PerViewThrottling(MockView): permission_classes = (PerViewThrottling,) -class MockView_PerResourceThrottling(MockView): - permission_classes = (PerResourceThrottling,) - resource = FormResource - - class MockView_MinuteThrottling(MockView): throttle = '3/min' @@ -98,12 +92,6 @@ class ThrottlingTests(TestCase): """ self.ensure_is_throttled(MockView_PerViewThrottling, 503) - def test_request_throttling_is_per_resource(self): - """ - Ensure request rate is limited globally per Resource for PerResourceThrottles - """ - self.ensure_is_throttled(MockView_PerResourceThrottling, 503) - def ensure_response_header_contains_proper_throttle_field(self, view, expected_headers): """ Ensure the response returns an X-Throttle field with status and next attributes -- cgit v1.2.3 From 73cc77553ed5411f1959a51574b156a47ad5340d Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Sun, 26 Aug 2012 23:06:52 +0100 Subject: Drop ImmediateResponse --- djangorestframework/tests/throttling.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'djangorestframework/tests/throttling.py') diff --git a/djangorestframework/tests/throttling.py b/djangorestframework/tests/throttling.py index d307cd32..ad22d2d2 100644 --- a/djangorestframework/tests/throttling.py +++ b/djangorestframework/tests/throttling.py @@ -45,7 +45,7 @@ class ThrottlingTests(TestCase): request = self.factory.get('/') for dummy in range(4): response = MockView.as_view()(request) - self.assertEqual(503, response.status_code) + self.assertEqual(429, response.status_code) def set_throttle_timer(self, view, value): """ @@ -62,7 +62,7 @@ class ThrottlingTests(TestCase): request = self.factory.get('/') for dummy in range(4): response = MockView.as_view()(request) - self.assertEqual(503, response.status_code) + self.assertEqual(429, response.status_code) # Advance the timer by one second self.set_throttle_timer(MockView, 1) @@ -90,7 +90,7 @@ class ThrottlingTests(TestCase): """ Ensure request rate is limited globally per View for PerViewThrottles """ - self.ensure_is_throttled(MockView_PerViewThrottling, 503) + self.ensure_is_throttled(MockView_PerViewThrottling, 429) def ensure_response_header_contains_proper_throttle_field(self, view, expected_headers): """ -- cgit v1.2.3 From a092a72844705e3129b8996b81d8424997b5d37f Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 3 Sep 2012 16:54:17 +0100 Subject: View -> APIView --- djangorestframework/tests/throttling.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'djangorestframework/tests/throttling.py') diff --git a/djangorestframework/tests/throttling.py b/djangorestframework/tests/throttling.py index ad22d2d2..a8e446e8 100644 --- a/djangorestframework/tests/throttling.py +++ b/djangorestframework/tests/throttling.py @@ -7,12 +7,12 @@ from django.contrib.auth.models import User from django.core.cache import cache from djangorestframework.compat import RequestFactory -from djangorestframework.views import View +from djangorestframework.views import APIView from djangorestframework.permissions import PerUserThrottling, PerViewThrottling from djangorestframework.response import Response -class MockView(View): +class MockView(APIView): permission_classes = (PerUserThrottling,) throttle = '3/sec' -- cgit v1.2.3 From c28b719333b16935e53c76fef79b096cb11322ed Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 4 Sep 2012 21:58:35 +0100 Subject: Refactored throttling --- djangorestframework/tests/throttling.py | 43 ++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 20 deletions(-) (limited to 'djangorestframework/tests/throttling.py') diff --git a/djangorestframework/tests/throttling.py b/djangorestframework/tests/throttling.py index a8e446e8..d144d956 100644 --- a/djangorestframework/tests/throttling.py +++ b/djangorestframework/tests/throttling.py @@ -8,24 +8,24 @@ from django.core.cache import cache from djangorestframework.compat import RequestFactory from djangorestframework.views import APIView -from djangorestframework.permissions import PerUserThrottling, PerViewThrottling +from djangorestframework.throttling import PerUserThrottling, PerViewThrottling from djangorestframework.response import Response class MockView(APIView): - permission_classes = (PerUserThrottling,) - throttle = '3/sec' + throttle_classes = (PerUserThrottling,) + rate = '3/sec' def get(self, request): return Response('foo') class MockView_PerViewThrottling(MockView): - permission_classes = (PerViewThrottling,) + throttle_classes = (PerViewThrottling,) class MockView_MinuteThrottling(MockView): - throttle = '3/min' + rate = '3/min' class ThrottlingTests(TestCase): @@ -51,7 +51,7 @@ class ThrottlingTests(TestCase): """ Explicitly set the timer, overriding time.time() """ - view.permission_classes[0].timer = lambda self: value + view.throttle_classes[0].timer = lambda self: value def test_request_throttling_expires(self): """ @@ -101,17 +101,20 @@ class ThrottlingTests(TestCase): for timer, expect in expected_headers: self.set_throttle_timer(view, timer) response = view.as_view()(request) - self.assertEquals(response['X-Throttle'], expect) + if expect is not None: + self.assertEquals(response['X-Throttle-Wait-Seconds'], expect) + else: + self.assertFalse('X-Throttle-Wait-Seconds' in response.headers) def test_seconds_fields(self): """ Ensure for second based throttles. """ self.ensure_response_header_contains_proper_throttle_field(MockView, - ((0, 'status=SUCCESS; next=0.33 sec'), - (0, 'status=SUCCESS; next=0.50 sec'), - (0, 'status=SUCCESS; next=1.00 sec'), - (0, 'status=FAILURE; next=1.00 sec') + ((0, None), + (0, None), + (0, None), + (0, '1') )) def test_minutes_fields(self): @@ -119,10 +122,10 @@ class ThrottlingTests(TestCase): Ensure for minute based throttles. """ self.ensure_response_header_contains_proper_throttle_field(MockView_MinuteThrottling, - ((0, 'status=SUCCESS; next=20.00 sec'), - (0, 'status=SUCCESS; next=30.00 sec'), - (0, 'status=SUCCESS; next=60.00 sec'), - (0, 'status=FAILURE; next=60.00 sec') + ((0, None), + (0, None), + (0, None), + (0, '60') )) def test_next_rate_remains_constant_if_followed(self): @@ -131,9 +134,9 @@ class ThrottlingTests(TestCase): the throttling rate should stay constant. """ self.ensure_response_header_contains_proper_throttle_field(MockView_MinuteThrottling, - ((0, 'status=SUCCESS; next=20.00 sec'), - (20, 'status=SUCCESS; next=20.00 sec'), - (40, 'status=SUCCESS; next=20.00 sec'), - (60, 'status=SUCCESS; next=20.00 sec'), - (80, 'status=SUCCESS; next=20.00 sec') + ((0, None), + (20, None), + (40, None), + (60, None), + (80, None) )) -- cgit v1.2.3 From 6c109ac60f891955df367c61d4d7094039098076 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 13 Sep 2012 18:32:56 +0100 Subject: Improve throttles and docs --- djangorestframework/tests/throttling.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'djangorestframework/tests/throttling.py') diff --git a/djangorestframework/tests/throttling.py b/djangorestframework/tests/throttling.py index d144d956..9ee4ffa4 100644 --- a/djangorestframework/tests/throttling.py +++ b/djangorestframework/tests/throttling.py @@ -8,24 +8,30 @@ from django.core.cache import cache from djangorestframework.compat import RequestFactory from djangorestframework.views import APIView -from djangorestframework.throttling import PerUserThrottling, PerViewThrottling +from djangorestframework.throttling import UserRateThrottle from djangorestframework.response import Response -class MockView(APIView): - throttle_classes = (PerUserThrottling,) +class User3SecRateThrottle(UserRateThrottle): rate = '3/sec' + +class User3MinRateThrottle(UserRateThrottle): + rate = '3/min' + + +class MockView(APIView): + throttle_classes = (User3SecRateThrottle,) + def get(self, request): return Response('foo') -class MockView_PerViewThrottling(MockView): - throttle_classes = (PerViewThrottling,) +class MockView_MinuteThrottling(APIView): + throttle_classes = (User3MinRateThrottle,) - -class MockView_MinuteThrottling(MockView): - rate = '3/min' + def get(self, request): + return Response('foo') class ThrottlingTests(TestCase): @@ -86,12 +92,6 @@ class ThrottlingTests(TestCase): """ self.ensure_is_throttled(MockView, 200) - def test_request_throttling_is_per_view(self): - """ - Ensure request rate is limited globally per View for PerViewThrottles - """ - self.ensure_is_throttled(MockView_PerViewThrottling, 429) - def ensure_response_header_contains_proper_throttle_field(self, view, expected_headers): """ Ensure the response returns an X-Throttle field with status and next attributes -- cgit v1.2.3 From 886f8b47510c830483b5adae1855593cdc3df2dc Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 14 Sep 2012 08:54:58 +0100 Subject: Tweak throttles and improve docs --- djangorestframework/tests/throttling.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'djangorestframework/tests/throttling.py') diff --git a/djangorestframework/tests/throttling.py b/djangorestframework/tests/throttling.py index 9ee4ffa4..3033614f 100644 --- a/djangorestframework/tests/throttling.py +++ b/djangorestframework/tests/throttling.py @@ -14,10 +14,12 @@ from djangorestframework.response import Response class User3SecRateThrottle(UserRateThrottle): rate = '3/sec' + scope = 'seconds' class User3MinRateThrottle(UserRateThrottle): rate = '3/min' + scope = 'minutes' class MockView(APIView): -- cgit v1.2.3 From 87dae4d8549c02fa9a57adb3bb876d249dae1f79 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 20 Sep 2012 13:19:43 +0100 Subject: Remove old 'djangorestframework directories --- djangorestframework/tests/throttling.py | 144 -------------------------------- 1 file changed, 144 deletions(-) delete mode 100644 djangorestframework/tests/throttling.py (limited to 'djangorestframework/tests/throttling.py') diff --git a/djangorestframework/tests/throttling.py b/djangorestframework/tests/throttling.py deleted file mode 100644 index 3033614f..00000000 --- a/djangorestframework/tests/throttling.py +++ /dev/null @@ -1,144 +0,0 @@ -""" -Tests for the throttling implementations in the permissions module. -""" - -from django.test import TestCase -from django.contrib.auth.models import User -from django.core.cache import cache - -from djangorestframework.compat import RequestFactory -from djangorestframework.views import APIView -from djangorestframework.throttling import UserRateThrottle -from djangorestframework.response import Response - - -class User3SecRateThrottle(UserRateThrottle): - rate = '3/sec' - scope = 'seconds' - - -class User3MinRateThrottle(UserRateThrottle): - rate = '3/min' - scope = 'minutes' - - -class MockView(APIView): - throttle_classes = (User3SecRateThrottle,) - - def get(self, request): - return Response('foo') - - -class MockView_MinuteThrottling(APIView): - throttle_classes = (User3MinRateThrottle,) - - def get(self, request): - return Response('foo') - - -class ThrottlingTests(TestCase): - urls = 'djangorestframework.tests.throttling' - - def setUp(self): - """ - Reset the cache so that no throttles will be active - """ - cache.clear() - self.factory = RequestFactory() - - def test_requests_are_throttled(self): - """ - Ensure request rate is limited - """ - request = self.factory.get('/') - for dummy in range(4): - response = MockView.as_view()(request) - self.assertEqual(429, response.status_code) - - def set_throttle_timer(self, view, value): - """ - Explicitly set the timer, overriding time.time() - """ - view.throttle_classes[0].timer = lambda self: value - - def test_request_throttling_expires(self): - """ - Ensure request rate is limited for a limited duration only - """ - self.set_throttle_timer(MockView, 0) - - request = self.factory.get('/') - for dummy in range(4): - response = MockView.as_view()(request) - self.assertEqual(429, response.status_code) - - # Advance the timer by one second - self.set_throttle_timer(MockView, 1) - - response = MockView.as_view()(request) - self.assertEqual(200, response.status_code) - - def ensure_is_throttled(self, view, expect): - request = self.factory.get('/') - request.user = User.objects.create(username='a') - for dummy in range(3): - view.as_view()(request) - request.user = User.objects.create(username='b') - response = view.as_view()(request) - self.assertEqual(expect, response.status_code) - - def test_request_throttling_is_per_user(self): - """ - Ensure request rate is only limited per user, not globally for - PerUserThrottles - """ - self.ensure_is_throttled(MockView, 200) - - def ensure_response_header_contains_proper_throttle_field(self, view, expected_headers): - """ - Ensure the response returns an X-Throttle field with status and next attributes - set properly. - """ - request = self.factory.get('/') - for timer, expect in expected_headers: - self.set_throttle_timer(view, timer) - response = view.as_view()(request) - if expect is not None: - self.assertEquals(response['X-Throttle-Wait-Seconds'], expect) - else: - self.assertFalse('X-Throttle-Wait-Seconds' in response.headers) - - def test_seconds_fields(self): - """ - Ensure for second based throttles. - """ - self.ensure_response_header_contains_proper_throttle_field(MockView, - ((0, None), - (0, None), - (0, None), - (0, '1') - )) - - def test_minutes_fields(self): - """ - Ensure for minute based throttles. - """ - self.ensure_response_header_contains_proper_throttle_field(MockView_MinuteThrottling, - ((0, None), - (0, None), - (0, None), - (0, '60') - )) - - def test_next_rate_remains_constant_if_followed(self): - """ - If a client follows the recommended next request rate, - the throttling rate should stay constant. - """ - self.ensure_response_header_contains_proper_throttle_field(MockView_MinuteThrottling, - ((0, None), - (20, None), - (40, None), - (60, None), - (80, None) - )) -- cgit v1.2.3