aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/tests/throttling.py
diff options
context:
space:
mode:
authormarkotibold2011-06-11 03:16:35 +0200
committermarkotibold2011-06-11 03:16:35 +0200
commitf854bc9065e53882a83a7878cdaaace15e32acca (patch)
tree372897d4a64124e3ab3b384dd9520b10740f11d2 /djangorestframework/tests/throttling.py
parent87db5fbda550e0dc1b808d1df30700bf2606c10d (diff)
downloaddjango-rest-framework-f854bc9065e53882a83a7878cdaaace15e32acca.tar.bz2
* fixed `test_request_throttling_is_per_user` - it didn't make a request for the 2nd user
* implemented per_resource_throttling + test needs refactoring
Diffstat (limited to 'djangorestframework/tests/throttling.py')
-rw-r--r--djangorestframework/tests/throttling.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/djangorestframework/tests/throttling.py b/djangorestframework/tests/throttling.py
index b33836a2..a9e6803b 100644
--- a/djangorestframework/tests/throttling.py
+++ b/djangorestframework/tests/throttling.py
@@ -8,7 +8,7 @@ from django.core.cache import cache
from djangorestframework.compat import RequestFactory
from djangorestframework.views import View
-from djangorestframework.permissions import PerUserThrottling
+from djangorestframework.permissions import PerUserThrottling, PerResourceThrottling
class MockView(View):
@@ -18,8 +18,16 @@ class MockView(View):
def get(self, request):
return 'foo'
+class MockView1(View):
+ permissions = ( PerResourceThrottling, )
+ throttle = (3, 1) # 3 requests per second
+
+ def get(self, request):
+ return 'foo'
+
urlpatterns = patterns('',
(r'^$', MockView.as_view()),
+ (r'^1$', MockView1.as_view()),
)
class ThrottlingTests(TestCase):
@@ -37,7 +45,6 @@ class ThrottlingTests(TestCase):
self.assertEqual(503, response.status_code)
def test_request_throttling_is_per_user(self):
- #Can not login user.....Dunno why...
"""Ensure request rate is only limited per user, not globally"""
for username in ('testuser', 'another_testuser'):
user = User.objects.create(username=username)
@@ -49,7 +56,23 @@ class ThrottlingTests(TestCase):
response = self.client.get('/')
self.client.logout()
self.assertTrue(self.client.login(username='another_testuser', password='test'), msg='Login failed')
+ response = self.client.get('/')
self.assertEqual(200, response.status_code)
+
+ def test_request_throttling_is_per_resource(self):
+ """Ensure request rate is limited globally per View"""
+ for username in ('testuser', 'another_testuser'):
+ user = User.objects.create(username=username)
+ user.set_password('test')
+ user.save()
+
+ self.assertTrue(self.client.login(username='testuser', password='test'), msg='Login Failed')
+ for dummy in range(3):
+ response = self.client.get('/1')
+ self.client.logout()
+ self.assertTrue(self.client.login(username='another_testuser', password='test'), msg='Login failed')
+ response = self.client.get('/1')
+ self.assertEqual(503, response.status_code)
def test_request_throttling_expires(self):
"""Ensure request rate is limited for a limited duration only"""